tbb-testsuite 9.56 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
20
21
22
23
24
25
my %tests = (
    tbbScreenshot => { type => 'mozmill' },
    'check.tpo' => { type => 'selenium' },
);

my %results;

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

sub get_options {
boklm's avatar
boklm committed
43
    my @options = qw(mozmill! selenium! starttor! tor-control-port=i
44
                     tor-socks-port=i reports-dir=s);
45
46
    my %res = %default_options;
    Getopt::Long::GetOptionsFromArray(\@_, \%res, @options) || exit 1;
47
    $res{files} = \@_;
48
49
50
    return \%res;
}

51
52
53
54
55
56
57
58
59
60
61
62
63
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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;
}

80
81
82
sub tbb_filename_infos {
    my ($tbbfile) = @_;
    my (undef, undef, $file) = File::Spec->splitpath($tbbfile);
83
    my %res = (filename => $file);
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
    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;
}
100
101
102
103
104

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
105
    my $tmpdir = $options->{tbbinfos}{tmpdir};
106
107
    chdir $tmpdir;
    system('tar', 'xf', $tbbfile);
108
    return "$tmpdir/tor-browser_$options->{tbbinfos}{language}";
109
110
111
}

sub setup_tbb {
112
    $ENV{TOR_SKIP_LAUNCH} = 1;
113
114
}

boklm's avatar
boklm committed
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
172
173
174
175
176
177
178
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
179
180
181
182
183
184
sub screenshot_thumbnail {
    my ($dir, $name) = @_;
    my $image = Image::Resize->new("$dir/$name");
    write_file("$dir/t-$name", $image->resize(600, 600)->png);
}

185
sub mozmill_run {
186
    my ($test) = @_;
187
    return unless $options->{mozmill};
188
189
190
191
192
    $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";
193
194
    system('mozmill', '-b', "$options->{tbbdir}/Browser/firefox", '-p',
        "$options->{tbbdir}/Data/Browser/profile.default", '-t',
195
196
197
198
199
        "$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
200
        screenshot_thumbnail($options->{tbbinfos}{'results-dir'}, "$test->{name}-$i.png");
201
        push @{$test->{screenshots}}, "$test->{name}-$i.png";
202
203
204
        $i++;
    }
    $test->{results} = decode_json(read_file($results_file));
205
206
}

207
208
209
210
sub selenium_run {
    my ($test) = @_;
    return unless $options->{selenium};
    system("$FindBin::Bin/selenium-tests/test_$test->{name}.py");
211
212
}

213
214
215
216
217
sub run_tests {
    my ($tests) = @_;
    my %types = (
        mozmill  => \&mozmill_run,
        selenium => \&selenium_run,
boklm's avatar
boklm committed
218
    );
219
220
221
    foreach my $test (keys %$tests) {
        $tests->{$test}{name} = $test;
        $types{$tests->{$test}{type}}->($tests->{$test});
boklm's avatar
boklm committed
222
223
224
    }
}

boklm's avatar
boklm committed
225
226
227
228
229
230
231
232
233
234
235
sub is_success {
    my ($tests) = @_;
    foreach my $test (keys %$tests) {
        if ($tests->{$test}{type} eq 'mozmill'
            && $tests->{$test}{results}{results}->[0]->{failed}) {
            return 0;
        }
    }
    return 1;
}

boklm's avatar
boklm committed
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
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]);
    }
}

262
sub test_tbb {
boklm's avatar
boklm committed
263
    my ($tbbfile, $sha256sum) = @_;
264
    my $oldcwd = getcwd;
265
    $options->{tbbinfos} = tbb_filename_infos($tbbfile);
boklm's avatar
boklm committed
266
    return test_sha($tbbfile) if $options->{tbbinfos}{type} eq 'sha256sum';
boklm's avatar
boklm committed
267
268
    $options->{tbbinfos}{tmpdir} = File::Temp::newdir;
    $tbbfile = get_tbbfile($tbbfile);
boklm's avatar
boklm committed
269
270
271
    if ($sha256sum && $sha256sum ne sha256_hex(read_file($tbbfile))) {
        exit_error "Wrong sha256sum for $tbbfile";
    }
boklm's avatar
boklm committed
272
273
    $options->{tbbinfos}{sha256sum} = $sha256sum ? $sha256sum
                                        : sha256_hex(read_file($tbbfile));
274
    $options->{tbbinfos}{tests} = { map { $_ => { %{$tests{$_}} } } keys %tests };
275
276
277
    $options->{tbbinfos}{'results-dir'} =
        "$options->{'reports-dir'}/results-$options->{tbbinfos}{filename}";
    mkdir $options->{tbbinfos}{'results-dir'};
278
279
    $options->{tbbdir} = extract_tbb($tbbfile);
    chdir $options->{tbbdir} || exit_error "Can't enter directory $options->{tbbdir}";
280
281
    $ENV{TBB_BIN} = "$options->{tbbdir}/Browser/firefox";
    $ENV{TBB_PROFILE} = "$options->{tbbdir}/Data/Browser/profile.default";
boklm's avatar
boklm committed
282
    start_tor;
283
284
    setup_tbb;
    print "tbbdir: $options->{tbbdir}\n";
285
    run_tests($options->{tbbinfos}{tests});
286
    chdir $oldcwd;
boklm's avatar
boklm committed
287
    stop_tor;
boklm's avatar
boklm committed
288
    $options->{tbbinfos}{success} = is_success($options->{tbbinfos}{tests});
289
    $results{$options->{tbbinfos}{filename}} = $options->{tbbinfos};
290
}
291

292
set_reports_dir;
293
294
295
foreach my $tbbfile (@{$options->{files}}) {
    test_tbb($tbbfile);
}
296
print "Reports directory: $options->{'reports-dir'}\n";