tbb-testsuite 8.76 KB
Newer Older
1
2
3
4
5
6
#!/usr/bin/perl -w
use strict;
use Getopt::Long;
use Cwd qw(getcwd);
use File::Spec;
use File::Temp;
boklm's avatar
boklm committed
7
use File::Slurp;
8
use File::Path qw(make_path);
9
10
use Data::Dump qw/dd/;
use FindBin;
boklm's avatar
boklm committed
11
use LWP::UserAgent;
boklm's avatar
boklm committed
12
use Digest::SHA qw(sha256_hex);
boklm's avatar
boklm committed
13
14
use IO::CaptureOutput qw(capture_exec);
use IO::Socket::INET;
15
16
use JSON;
use File::Copy;
boklm's avatar
boklm committed
17
use Image::Resize;
18
19

my %default_options = (
boklm's avatar
boklm committed
20
21
    os       => 'Linux',
    arch     => 'x86_64',
22
23
    mozmill  => 1,
    selenium => 1,
boklm's avatar
boklm committed
24
25
26
    starttor => 1,
    'tor-control-port' => '9551',
    'tor-socks-port'   => '9550',
27
28
29
30
);
my $options = get_options(@ARGV);
sub exit_error {
    print STDERR "Error: ", $_[0], "\n";
boklm's avatar
boklm committed
31
    chdir '/';
32
33
34
35
    exit (exists $_[1] ? $_[1] : 1);
}

sub get_options {
boklm's avatar
boklm committed
36
    my @options = qw(mozmill! selenium! starttor! tor-control-port=i
37
                     tor-socks-port=i reports-dir=s);
38
39
    my %res = %default_options;
    Getopt::Long::GetOptionsFromArray(\@_, \%res, @options) || exit 1;
40
    $res{files} = \@_;
41
42
43
    return \%res;
}

44
45
46
47
48
49
50
51
52
53
54
55
56
sub set_reports_dir {
    if ($options->{'reports-dir'}) {
        make_path($options->{'reports-dir'});
        return;
    }
    my $r = $FindBin::Bin . '/reports';
    mkdir $r;
    return $options->{'reports-dir'} = File::Temp::newdir(
        'XXXXXX',
        DIR => $r,
        CLEANUP => 0)->dirname;
}

boklm's avatar
boklm committed
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
sub get_tbbfile {
    my ($tbbfile) = @_;
    if ($tbbfile =~ m/^https?:\/\//) {
        print "Downloading $tbbfile\n";
        my (undef, undef, $file) = File::Spec->splitpath($tbbfile);
        my $output = "$options->{tbbinfos}{tmpdir}/$file";
        my $ua = LWP::UserAgent->new;
        my $resp = $ua->get($tbbfile, ':content_file' => $output);
        exit_error "Error downloading $tbbfile:\n" . $resp->status_line
                unless $resp->is_success;
        return $output;
    }
    exit_error "File $tbbfile does not exist" unless -f $tbbfile;
    return $tbbfile;
}

73
74
75
sub tbb_filename_infos {
    my ($tbbfile) = @_;
    my (undef, undef, $file) = File::Spec->splitpath($tbbfile);
76
    my %res = (filename => $file);
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
    if ($file =~ m/^tor-browser-linux(..)-([^_]+)_(.+)\.tar\.xz$/) {
        @res{qw(type os version language)} = ('tbbfile', 'Linux', $2, $3);
        $res{arch} = $1 eq '64' ? 'x86_64' : 'x86';
    } elsif ($file =~ m/^torbrowser-install-([^_]+)_(.+)\.exe$/) {
        @res{qw(type os arch version language)} =
                ('tbbfile', 'Windows', 'x86', $1, $2);
    } elsif ($file =~ m/^TorBrowserBundle-(.+)-osx32_(.+)\.zip$/) {
        @res{qw(type os arch version language)} =
                ('tbbfile', 'MacOSX', 'x86', $1, $2);
    } elsif ($file eq 'sha256sums.txt') {
        $res{type} = 'sha256sum';
    } else {
        $res{type} = 'Unknown';
    }
    return \%res;
}
93
94
95
96
97

sub extract_tbb {
    my ($tbbfile) = @_;
    exit_error "Can't open file $tbbfile" unless -f $tbbfile;
    $tbbfile = File::Spec->rel2abs($tbbfile);
boklm's avatar
boklm committed
98
    my $tmpdir = $options->{tbbinfos}{tmpdir};
99
100
    chdir $tmpdir;
    system('tar', 'xf', $tbbfile);
101
    return "$tmpdir/tor-browser_$options->{tbbinfos}{language}";
102
103
104
}

sub setup_tbb {
105
    $ENV{TOR_SKIP_LAUNCH} = 1;
106
107
}

boklm's avatar
boklm committed
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
sub monitor_bootstrap {
    my ($control_passwd) = @_;
    sleep 10;
    my $sock = new IO::Socket::INET(
        PeerAddr => 'localhost',
        PeerPort => $options->{'tor-control-port'},
        Proto => 'tcp',
    );
    exit_error "Error connecting to control port: $!\n" unless $sock;
    print $sock 'AUTHENTICATE "', $control_passwd, "\"\n";
    my $r = <$sock>;
    exit_error "Authentication error: $r" unless $r =~ m/^250 OK/;
    my $i = 0;
    while (1) {
        print $sock "GETINFO status/bootstrap-phase\n";
        $r = <$sock>;
        print $r;
        last if $r =~ m/^250-status\/bootstrap-phase.* TAG=done/;
        sleep 1;
        $i++;
        exit_error "Could not bootstrap after $i seconds" if $i > 300;
    }
    print "Bootstraping done\n";
    return 3;
}

# TODO: In the future, we should start tor using tor-launcher
sub start_tor {
    return unless $options->{starttor};
    my $control_passwd = map { ('a'..'z', 'A'..'Z', 0..9)[rand 62] } 0..8;
    my $cwd = getcwd;
    $ENV{LD_LIBRARY_PATH} = "$cwd/Tor/";
    $ENV{TOR_SOCKS_PORT} = $options->{'tor-socks-port'};
    $ENV{TOR_CONTROL_PORT} = $options->{'tor-control-port'};
    $ENV{TOR_CONTROL_HOST} = '127.0.0.1';
    $ENV{TOR_CONTROL_COOKIE_AUTH_FILE} = "$cwd/Data/Tor/control_auth_cookie";
    my ($hashed_password, undef, $success) =
        capture_exec("$cwd/Tor/tor", '--quiet', '--hash-password', $control_passwd);
    exit_error "Error running tor --hash-password" unless $success;
    chomp $hashed_password;
    my @torrc = read_file('Data/Tor/torrc-defaults');
    foreach (@torrc) {
        s/^ControlPort .*/ControlPort $options->{'tor-control-port'}/;
        s/^SocksPort .*/SocksPort $options->{'tor-socks-port'}/;
    }
    write_file('Data/Tor/torrc-defaults', @torrc);
    my @cmd = ("$cwd/Tor/tor", '--defaults-torrc', "$cwd/Data/Tor/torrc-defaults",
        '-f', "$cwd/Data/Tor/torrc", 'DataDirectory', "$cwd/Data/Tor",
        'GeoIPFile', "$cwd/Data/Tor/geoip", '__OwningControllerProcess', $$,
        'HashedControlPassword', $hashed_password);
    $options->{tbbinfos}{torpid} = fork;
    if ($options->{tbbinfos}{torpid} == 0) {
        open(STDOUT, '>', '/dev/null');
        open(STDERR, '>', '/dev/null');
        exec @cmd;
    }
    return monitor_bootstrap($control_passwd);
}

sub stop_tor {
    return unless $options->{starttor};
    kill 9, $options->{tbbinfos}{torpid};
}

boklm's avatar
boklm committed
172
173
174
175
176
177
sub screenshot_thumbnail {
    my ($dir, $name) = @_;
    my $image = Image::Resize->new("$dir/$name");
    write_file("$dir/t-$name", $image->resize(600, 600)->png);
}

178
sub mozmill_run {
179
180
181
182
183
184
    my ($test) = @_;
    $test->{screenshots} = [];
    my %res = ( screenshots => [], );
    my $screenshots_tmp = File::Temp::newdir;
    $ENV{'MOZMILL_SCREENSHOTS'} = $screenshots_tmp;
    my $results_file = "$options->{tbbinfos}{'results-dir'}/$test->{name}.json";
185
186
    system('mozmill', '-b', "$options->{tbbdir}/Browser/firefox", '-p',
        "$options->{tbbdir}/Data/Browser/profile.default", '-t',
187
188
189
190
191
        "$FindBin::Bin/mozmill-tests/tbb-tests/$test->{name}.js",
        '--report', "file://$results_file");
    my $i = 0;
    for my $screenshot_file (glob "$screenshots_tmp/*.png") {
        move($screenshot_file, "$options->{tbbinfos}{'results-dir'}/$test->{name}-$i.png");
boklm's avatar
boklm committed
192
        screenshot_thumbnail($options->{tbbinfos}{'results-dir'}, "$test->{name}-$i.png");
193
194
195
196
        push @{$test->{screenshots}}, "$test-$i.png";
        $i++;
    }
    $test->{results} = decode_json(read_file($results_file));
197
198
199
}

sub mozmill_tests {
200
    return unless $options->{mozmill};
201
    my @tests = ({ name => 'tbbScreenshot', });
202
203
204
205
206
    foreach my $test (@tests) {
        mozmill_run($test);
    }
}

boklm's avatar
boklm committed
207
sub selenium_tests {
208
    return unless $options->{selenium};
boklm's avatar
boklm committed
209
210
211
212
213
214
215
216
217
218
    my @tests = (
        'test_check.tpo.py',
    );
    $ENV{TBB_BIN} = "$options->{tbbdir}/Browser/firefox";
    $ENV{TBB_PROFILE} = "$options->{tbbdir}/Data/Browser/profile.default";
    foreach my $test (@tests) {
        system("$FindBin::Bin/selenium-tests/$test");
    }
}

boklm's avatar
boklm committed
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
sub matching_tbbfile {
    my $o = tbb_filename_infos($_[0]);
    return $o->{type} eq 'tbbfile' && $o->{os} eq $options->{os}
        && $o->{arch} eq $options->{arch};
}

sub test_sha {
    my ($shafile) = @_;
    my $content;
    if ($shafile =~ m/^https?:\/\//) {
        my $ua = LWP::UserAgent->new;
        my $resp = $ua->get($shafile);
        exit_error "Error downloading $shafile:\n" . $resp->status_line
                unless $resp->is_success;
        $content = $resp->decoded_content;
    } else {
        $content = read_file($shafile);
    }
    my (undef, $dir) = File::Spec->splitpath($shafile);
    my @files = map { [ reverse split /  /, $_ ] } split /\n/, $content;
    @files = grep { matching_tbbfile($_->[0]) } @files;
    foreach my $file (@files) {
        test_tbb("$dir/$file->[0]", $file->[1]);
    }
}

245
sub test_tbb {
boklm's avatar
boklm committed
246
    my ($tbbfile, $sha256sum) = @_;
247
    my $oldcwd = getcwd;
248
    $options->{tbbinfos} = tbb_filename_infos($tbbfile);
boklm's avatar
boklm committed
249
    return test_sha($tbbfile) if $options->{tbbinfos}{type} eq 'sha256sum';
boklm's avatar
boklm committed
250
251
    $options->{tbbinfos}{tmpdir} = File::Temp::newdir;
    $tbbfile = get_tbbfile($tbbfile);
boklm's avatar
boklm committed
252
253
254
    if ($sha256sum && $sha256sum ne sha256_hex(read_file($tbbfile))) {
        exit_error "Wrong sha256sum for $tbbfile";
    }
255
256
257
    $options->{tbbinfos}{'results-dir'} =
        "$options->{'reports-dir'}/results-$options->{tbbinfos}{filename}";
    mkdir $options->{tbbinfos}{'results-dir'};
258
259
    $options->{tbbdir} = extract_tbb($tbbfile);
    chdir $options->{tbbdir} || exit_error "Can't enter directory $options->{tbbdir}";
boklm's avatar
boklm committed
260
    start_tor;
261
262
263
264
265
    setup_tbb;
    print "tbbdir: $options->{tbbdir}\n";
    mozmill_tests;
    selenium_tests;
    chdir $oldcwd;
boklm's avatar
boklm committed
266
    stop_tor;
267
}
268

269
set_reports_dir;
270
271
272
foreach my $tbbfile (@{$options->{files}}) {
    test_tbb($tbbfile);
}
273
print "Reports directory: $options->{'reports-dir'}\n";