mangle.py 3.01 KB
Newer Older
zorglub's avatar
zorglub committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import VLCUtil
import shutil
import os
from random import randint
import glob

# Todo
# - Correctly handle errors
# - Launch VLC in a separate thread and detect hangs
# - Correct path handling

global conf
global l

def play_mangled( filename, logfile ):
    os.chdir( "../.." )
    vlc_pid = os.spawnvp( os.P_NOWAIT, "./vlc",
        [ "vlc", "-I", "logger", "--quiet",  "--stop-time", "1",
          filename , "vlc:quit", "--logfile", logfile ])
    ( exit_pid, exit_status ) = os.waitpid( vlc_pid, 0 )
    os.chdir( "test/mangle" )
    l.debug( "VLC exited with status %i" % exit_status )
    return exit_status

def mangle_file( filename, header_size, percentage , new_file):
    shutil.copyfile( filename, new_file )
    file = open ( new_file, "r+" )
    for i in range( header_size ):
        file.seek( i)
        if( randint(0, 100/percentage) == 0 ):
            file.write( "%i" % randint (0, 255 ));
    file.flush()
    file.close()

def process_file_once( file, header_size ):
    suffix = randint( 0, 65535 )
    new_file = conf["temp_folder"] + conf["prefix"] + "%i" % suffix
    log_file = conf["temp_folder"] + conf["log_prefix"] + "%i" % suffix
    
    mangle_file( file, header_size, conf["mangle_ratio"], new_file )
    status = play_mangled( new_file, log_file )
    if( status == 0 ):
        os.remove( new_file )
        os.remove( log_file )
    else:
        l.info( "Potential crash detected : %i, saving results" % suffix )
zorglub's avatar
zorglub committed
47
48
49
50
51
        try:
            shutil.move( new_file , conf["crashdir"] )
            shutil.move( log_file , conf["crashdir"] )
        except:
            l.error( "Unable to move file" )
zorglub's avatar
zorglub committed
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92

def process_file( file, source, header_size ):
     l.info( "Starting work on " + file )

     if( len( glob.glob( conf["inputdir"] + "/" + file ) ) == 0 ):
         l.warn( "%s does not exist in %s" % (file, conf["inputdir"] ) )
         if( VLCUtil.downloadFile( file, source, conf["inputdir"], l ) != 0 ):
             l.error( "Unable to download %s" % file )
             return 
     
     for i in range( conf["loops"] ):
         process_file_once( conf["inputdir"] + "/" + file, header_size )

l =  VLCUtil.getLogger( "Mangle" )

conf = {}
conf["inputdir"] = "input"
conf["crashdir"] = "crashers"
conf["temp_folder"] = "/tmp/"
conf["prefix"] = "mangle."
conf["log_prefix"] = "vlc-log."
conf["mangle_ratio"] = 4 # Change X% of bytes within header
conf["loops"]  = 20

l.debug( "Creating folders" )

try:
    os.makedirs( conf["crashdir"] )
    os.makedirs( conf["inputdir"] )
    os.makedirs( conf["temp_folder"] )
except:
    pass


##########
process_file( "bl.mp4", "ftp://streams.videolan.org/streams-videolan/reference/mp4", 3000 )
process_file( "Win98Crash.mov", "ftp://streams.videolan.org/streams-videolan/reference/mov", 3000 )
process_file( "x264.avi", "ftp://streams.videolan.org/streams-videolan/reference/avi", 3000 )
process_file( "batidadomontoya.wmv", "ftp://streams.videolan.org/streams-videolan/reference/asf", 3000 )
process_file( "tarzan.ogm", "ftp://streams.videolan.org/streams-videolan/reference/ogm", 3000 )