File mangler
[vlc.git] / test / mangle / mangle.py
1 import VLCUtil
2 import shutil
3 import os
4 from random import randint
5 import glob
6
7 # Todo
8 # - Correctly handle errors
9 # - Launch VLC in a separate thread and detect hangs
10 # - Correct path handling
11
12 global conf
13 global l
14
15 def play_mangled( filename, logfile ):
16     os.chdir( "../.." )
17     vlc_pid = os.spawnvp( os.P_NOWAIT, "./vlc",
18         [ "vlc", "-I", "logger", "--quiet",  "--stop-time", "1",
19           filename , "vlc:quit", "--logfile", logfile ])
20     ( exit_pid, exit_status ) = os.waitpid( vlc_pid, 0 )
21     os.chdir( "test/mangle" )
22     l.debug( "VLC exited with status %i" % exit_status )
23     return exit_status
24
25 def mangle_file( filename, header_size, percentage , new_file):
26     shutil.copyfile( filename, new_file )
27     file = open ( new_file, "r+" )
28     for i in range( header_size ):
29         file.seek( i)
30         if( randint(0, 100/percentage) == 0 ):
31             file.write( "%i" % randint (0, 255 ));
32     file.flush()
33     file.close()
34
35 def process_file_once( file, header_size ):
36     suffix = randint( 0, 65535 )
37     new_file = conf["temp_folder"] + conf["prefix"] + "%i" % suffix
38     log_file = conf["temp_folder"] + conf["log_prefix"] + "%i" % suffix
39     
40     mangle_file( file, header_size, conf["mangle_ratio"], new_file )
41     status = play_mangled( new_file, log_file )
42     if( status == 0 ):
43         os.remove( new_file )
44         os.remove( log_file )
45     else:
46         l.info( "Potential crash detected : %i, saving results" % suffix )
47         shutil.move( new_file , conf["crashdir"] )
48         shutil.move( log_file , conf["crashdir"] )
49
50 def process_file( file, source, header_size ):
51      l.info( "Starting work on " + file )
52
53      if( len( glob.glob( conf["inputdir"] + "/" + file ) ) == 0 ):
54          l.warn( "%s does not exist in %s" % (file, conf["inputdir"] ) )
55          if( VLCUtil.downloadFile( file, source, conf["inputdir"], l ) != 0 ):
56              l.error( "Unable to download %s" % file )
57              return 
58      
59      for i in range( conf["loops"] ):
60          process_file_once( conf["inputdir"] + "/" + file, header_size )
61
62 l =  VLCUtil.getLogger( "Mangle" )
63
64 conf = {}
65 conf["inputdir"] = "input"
66 conf["crashdir"] = "crashers"
67 conf["temp_folder"] = "/tmp/"
68 conf["prefix"] = "mangle."
69 conf["log_prefix"] = "vlc-log."
70 conf["mangle_ratio"] = 4 # Change X% of bytes within header
71 conf["loops"]  = 20
72
73 l.debug( "Creating folders" )
74
75 try:
76     os.makedirs( conf["crashdir"] )
77     os.makedirs( conf["inputdir"] )
78     os.makedirs( conf["temp_folder"] )
79 except:
80     pass
81
82
83 ##########
84 process_file( "bl.mp4", "ftp://streams.videolan.org/streams-videolan/reference/mp4", 3000 )
85 process_file( "Win98Crash.mov", "ftp://streams.videolan.org/streams-videolan/reference/mov", 3000 )
86 process_file( "x264.avi", "ftp://streams.videolan.org/streams-videolan/reference/avi", 3000 )
87 process_file( "batidadomontoya.wmv", "ftp://streams.videolan.org/streams-videolan/reference/asf", 3000 )
88 process_file( "tarzan.ogm", "ftp://streams.videolan.org/streams-videolan/reference/ogm", 3000 )
89