BrowserBundleTests.pm 23.2 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
package TBBTestSuite::BrowserBundleTests;

use warnings;
use strict;
use English;
use FindBin;
use File::Slurp;
use File::Spec;
use File::Find;
use File::Type;
use File::Copy;
use JSON;
use Digest::SHA qw(sha256_hex);
use LWP::UserAgent;
15
use TBBTestSuite::Common qw(exit_error winpath clone_strip_coderef);
16
17
use TBBTestSuite::Options qw($options);
use TBBTestSuite::Tests::VirusTotal qw(virustotal_run);
18
use TBBTestSuite::Tests::Command qw(command_run);
19
use TBBTestSuite::Tests::TorBootstrap;
boklm's avatar
boklm committed
20
use TBBTestSuite::XServer qw(start_X stop_X set_Xmode);
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
47
48
49
50

our (@ISA, @EXPORT_OK);
BEGIN {
    require Exporter;
    @ISA       = qw(Exporter);
    @EXPORT_OK = qw(tbb_filename_infos);
}

my $screenshot_thumbnail;
BEGIN {
    # For some reason that I did not understand yet, Image::Magick does
    # not work on Windows, so we're not creating thumbnails if we're
    # on Windows. In that case, the thumbnails should be created by the
    # server that receives the results.
    if ($OSNAME ne 'cygwin') {
        require TBBTestSuite::Thumbnail;
        $screenshot_thumbnail = \&TBBTestSuite::Thumbnail::screenshot_thumbnail;
    } else {
        $screenshot_thumbnail = sub { };
    }
}

my $test_types = {
    tor_bootstrap => \&TBBTestSuite::Tests::TorBootstrap::start_tor,
    mozmill       => \&mozmill_run,
    selenium      => \&selenium_run,
    virustotal    => \&virustotal_run,
    command       => \&command_run,
};

51
our %testsuite = (
boklm's avatar
boklm committed
52
53
54
55
    description => 'Tor Browser Bundle integration tests',
    test_types  => $test_types,
    pre_tests   => \&pre_tests,
    post_tests  => \&post_tests,
56
57
);

58
59
60
61
62
our %testsuite_virustotal = (
    %testsuite,
    description => 'Tor Browser Bundle Virustotal checks',
);

63
64
65
our @tests = (
    {
        name         => 'readelf_RELRO',
66
        fail_type    => 'warning',
67
68
69
70
71
        type         => 'command',
        descr        => 'Check if binaries are RELocation Read-Only',
        files        => \&tbb_binfiles,
        command      => [ 'readelf', '-ld' ],
        check_output => sub { ( $_[0] =~ m/GNU_RELRO/ )
boklm's avatar
boklm committed
72
                                && ( $_[0] =~ m/BIND_NOW/ ) },
73
        enable       => sub { $OSNAME eq 'linux' },
74
75
76
    },
    {
        name         => 'readelf_stack_canary',
77
        fail_type    => 'warning',
78
79
80
81
82
        type         => 'command',
        descr        => 'Check for stack canary support',
        files        => \&tbb_binfiles,
        command      => [ 'readelf', '-s' ],
        check_output => sub { $_[0] =~ m/__stack_chk_fail/ },
83
        enable       => sub { $OSNAME eq 'linux' },
84
85
86
87
88
89
90
91
    },
    {
        name         => 'readelf_NX',
        type         => 'command',
        descr        => 'Check for NX support',
        files        => \&tbb_binfiles,
        command      => [ 'readelf', '-W', '-l' ],
        check_output => sub { ! ($_[0] =~ m/GNU_STACK.+RWE/) },
92
        enable       => sub { $OSNAME eq 'linux' },
93
94
95
96
97
98
99
100
    },
    {
        name         => 'readelf_PIE',
        type         => 'command',
        descr        => 'Check for PIE support',
        files        => \&tbb_binfiles,
        command      => [ 'readelf', '-h' ],
        check_output => sub { $_[0] =~ m/Type:\s+DYN/ },
101
        enable       => sub { $OSNAME eq 'linux' },
102
103
104
    },
    {
        name         => 'readelf_no_rpath',
105
        fail_type    => 'warning',
106
107
108
109
110
        type         => 'command',
        descr        => 'Check for no rpath',
        files        => \&tbb_binfiles,
        command      => [ 'readelf', '-d' ],
        check_output => sub { ! ( $_[0] =~ m/RPATH/ ) },
111
        enable       => sub { $OSNAME eq 'linux' },
112
113
114
115
116
117
118
119
    },
    {
        name         => 'readelf_no_runpath',
        type         => 'command',
        descr        => 'Check for no runpath',
        files        => \&tbb_binfiles,
        command      => [ 'readelf', '-d' ],
        check_output => sub { ! ( $_[0] =~ m/runpath/ ) },
120
        enable       => sub { $OSNAME eq 'linux' },
121
122
123
124
125
126
    },
    {
        name      => 'tor_httpproxy',
        type      => 'tor_bootstrap',
        descr     => 'Access tor using an http proxy',
        httpproxy => 1,
127
        enable    => sub { $OSNAME eq 'linux' },
128
129
130
131
132
    },
    {
        name   => 'tor_bridge',
        type   => 'tor_bootstrap',
        descr  => 'Access tor using a bridge',
133
        enable => sub { $OSNAME eq 'linux' },
134
135
136
137
138
139
    },
    {
        name      => 'tor_bridge_httpproxy',
        type      => 'tor_bootstrap',
        descr     => 'Access tor using a bridge and an http proxy',
        httpproxy => 1,
140
        enable    => sub { $OSNAME eq 'linux' },
141
142
143
144
145
    },
    {
        name   => 'tor_obfs3',
        type   => 'tor_bootstrap',
        descr  => 'Access tor using obfs3',
146
        enable => sub { $OSNAME eq 'linux' },
147
148
149
150
151
152
    },
    {
        name      => 'tor_obfs3_httpproxy',
        type      => 'tor_bootstrap',
        descr     => 'Access tor using obfs3 and an http proxy',
        httpproxy => 1,
153
        enable    => sub { $OSNAME eq 'linux' },
154
    },
155
156
157
158
159
160
161
162
163
164
165
166
167
    {
        name   => 'tor_obfs4',
        type   => 'tor_bootstrap',
        descr  => 'Access tor using obfs4',
        enable => sub { $OSNAME eq 'linux' && $_[0]->{version} !~ m/^4.0/ },
    },
    {
        name      => 'tor_obfs4_httpproxy',
        type      => 'tor_bootstrap',
        descr     => 'Access tor using obfs4 and an http proxy',
        httpproxy => 1,
        enable    => sub { $OSNAME eq 'linux' && $_[0]->{version} !~ m/^4.0/ },
    },
168
169
170
171
    {
        name   => 'tor_fte',
        type   => 'tor_bootstrap',
        descr  => 'Access tor using fteproxy',
172
        enable => sub { $OSNAME eq 'linux' },
173
174
175
176
177
178
    },
    {
        name      => 'tor_fte_httpproxy',
        type      => 'tor_bootstrap',
        descr     => 'Access tor using fteproxy and an http proxy',
        httpproxy => 1,
179
        enable    => sub { $OSNAME eq 'linux' },
180
181
182
183
184
    },
    {
        name   => 'tor_bootstrap',
        type   => 'tor_bootstrap',
        descr  => 'Check that we can bootstrap tor',
185
        fail_type => 'fatal',
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
        use_default_config => 1,
        no_kill => 1,
    },
    {
        name => 'screenshots',
        type  => 'mozmill',
        descr => 'Take some screenshots',
    },
    {
        name => 'check',
        type  => 'selenium',
        descr => 'Check that http://check.torproject.org/ think we are using tor',
    },
    {
        name  => 'https-everywhere',
        type  => 'mozmill',
        descr => 'Check that https everywhere is enabled and working',
    },
    {
        name  => 'https-everywhere-disabled',
        type  => 'mozmill',
        descr => 'Check that https everywhere is not doing anything when disabled',
        pre   => sub { toggle_https_everywhere($_[0], 0) },
        post  => sub { toggle_https_everywhere($_[0], 1) },
    },
    {
        name  => 'settings',
        type  => 'mozmill',
        descr => 'Check that some important settings are correctly set',
    },
boklm's avatar
boklm committed
216
217
218
219
220
    {
        name  => 'acid3',
        type  => 'mozmill',
        descr => 'acid3 tests',
    },
boklm's avatar
boklm committed
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
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
    {
        name         => 'slider_settings_1',
        mozmill_test => 'slider_settings',
        type         => 'mozmill',
        descr        => 'Check that settings are set according to security slider mode',
        slider_mode  => 1,
        pre          => \&set_slider_mode,
        post         => \&reset_slider_mode,
        enable       => sub { $_[0]->{version} !~ m/^4.0/ },
    },
    {
        name         => 'slider_settings_2',
        mozmill_test => 'slider_settings',
        type         => 'mozmill',
        descr        => 'Check that settings are set according to security slider mode',
        slider_mode  => 2,
        pre          => \&set_slider_mode,
        post         => \&reset_slider_mode,
        enable       => sub { $_[0]->{version} !~ m/^4.0/ },
    },
    {
        name         => 'slider_settings_3',
        mozmill_test => 'slider_settings',
        type         => 'mozmill',
        descr        => 'Check that settings are set according to security slider mode',
        slider_mode  => 3,
        pre          => \&set_slider_mode,
        post         => \&reset_slider_mode,
        enable       => sub { $_[0]->{version} !~ m/^4.0/ },
    },
    {
        name         => 'slider_settings_4',
        mozmill_test => 'slider_settings',
        type         => 'mozmill',
        descr        => 'Check that settings are set according to security slider mode',
        slider_mode  => 4,
        pre          => \&set_slider_mode,
        post         => \&reset_slider_mode,
        enable       => sub { $_[0]->{version} !~ m/^4.0/ },
    },
boklm's avatar
boklm committed
261
262
263
264
265
    {
        name  => 'dom-objects-enumeration',
        type  => 'mozmill',
        descr => 'Check the list of DOM Objects exposed in the global namespace',
    },
266
267
268
269
270
    {
        name  => 'navigation-timing',
        type  => 'mozmill',
        descr => 'Check that the Navigation Timing API is really disabled',
    },
271
272
273
274
275
    {
        name  => 'resource-timing',
        type  => 'mozmill',
        descr => 'Check that the Resource Timing API is really disabled',
    },
276
277
278
279
280
    {
        name  => 'searchengines',
        type  => 'mozmill',
        descr => 'Check that we have the default search engines set',
    },
boklm's avatar
boklm committed
281
282
283
284
285
286
287
288
289
290
    {
        name  => 'noscript',
        type  => 'mozmill',
        descr => 'Check that noscript options are working',
        retry => 1,
        prefs => {
            'extensions.torbutton.security_slider' => 3,
        },
        enable       => sub { $_[0]->{version} !~ m/^4.0/ },
    },
boklm's avatar
boklm committed
291
    {
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
        name => 'fp_screen_dimensions',
        type  => 'selenium',
        descr => 'Check that screen dimensions are spoofed correctly',
    },
    {
        name => 'fp_screen_coords',
        type  => 'selenium',
        descr => 'Check that screenX, screenY, screenLeft, screenTop, mozInnerScreenX, mozInnerScreenY are 0',
    },
    {
        name => 'fp_plugins',
        type  => 'selenium',
        descr => 'Check that plugins are disabled',
    },
    {
        name => 'fp_useragent',
        type  => 'selenium',
        descr => 'Check that userAgent is as expected',
    },        {
        name => 'fp_navigator',
        type  => 'selenium',
        descr => 'Check that navigator properties are as expected',
    },
boklm's avatar
boklm committed
315
316
317
318
319
320
321
322
323
    {
        name => 'play_videos',
        type => 'mozmill',
        descr => 'Play some videos',
        mozmill_test => 'test_page',
        remote => 1,
        timeout => 50000,
        interval => 100,
    },
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
);

sub toggle_https_everywhere {
    my ($tbbinfos, $t) = @_;
    my $prefs = $tbbinfos->{ffprofiledir} . '/extensions/'
        . 'https-everywhere@eff.org/defaults/preferences/preferences.js';
    my @f = read_file($prefs);
    foreach (@f) {
        if ($t) {
            s/pref\("extensions\.https_everywhere\.globalEnabled",false\);
             /pref("extensions.https_everywhere.globalEnabled",true);/x;
        } else {
            s/pref\("extensions\.https_everywhere\.globalEnabled",true\);
             /pref("extensions.https_everywhere.globalEnabled",false);/x;
        }
    }
    write_file($prefs, @f);
}

343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
sub set_test_prefs {
    my ($tbbinfos, $t) = @_;
    return unless $t->{prefs};
    my $prefs = "$tbbinfos->{ffprofiledir}/preferences/extension-overrides.js";
    copy $prefs, "$prefs.backup";
    my $new_prefs = '';
    foreach my $prefname (sort keys %{$t->{prefs}}) {
        $new_prefs .= "pref(\"$prefname\", $t->{prefs}{$prefname});\n";
    }
    write_file($prefs, {append => 1}, $new_prefs);
    print "prefs file: $prefs\n";
}

sub reset_test_prefs {
    my ($tbbinfos, $t) = @_;
    return unless $t->{prefs};
    my $prefs = "$tbbinfos->{ffprofiledir}/preferences/extension-overrides.js";
    move "$prefs.backup", $prefs;
}

boklm's avatar
boklm committed
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
sub set_slider_mode {
    my ($tbbinfos, $t) = @_;
    my $prefs = "$tbbinfos->{ffprofiledir}/preferences/extension-overrides.js";
    copy $prefs, "$prefs.slider_backup";
    write_file($prefs, {append => 1},
      'pref("extensions.torbutton.security_custom", false);' . "\n" .
      "pref(\"extensions.torbutton.security_slider\", $t->{slider_mode});\n");
}

sub reset_slider_mode {
    my ($tbbinfos, $t) = @_;
    my $prefs = "$tbbinfos->{ffprofiledir}/preferences/extension-overrides.js";
    move "$prefs.slider_backup", $prefs;
}

378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
sub tbb_binfiles {
    my ($tbbinfos, $test) = @_;
    return $tbbinfos->{binfiles} if $tbbinfos->{binfiles};
    my %binfiles = (
        $tbbinfos->{ffbin} => 1,
        "$tbbinfos->{tordir}/tor" => 1,
    );
    my $wanted = sub {
        return unless -f $File::Find::name;
        return unless -x $File::Find::name;
        my $type = File::Type->new->checktype_filename($File::Find::name);
        return unless $type eq 'application/x-executable-file';
        $binfiles{$File::Find::name} = 1;
    };
    find($wanted, $tbbinfos->{tbbdir});
    return $tbbinfos->{binfiles} = [ keys %binfiles ];
}

sub list_tests {
    foreach my $test (@tests) {
        print "$test->{name} ($test->{type})\n   $test->{descr}\n\n";
    }
}

sub get_tbbfile {
    my ($tbbinfos) = @_;
    $tbbinfos->{tbbfile_orig} = $tbbinfos->{tbbfile};
    if ($tbbinfos->{tbbfile} =~ m/^https?:\/\//) {
        my (undef, undef, $file) = File::Spec->splitpath($tbbinfos->{tbbfile});
        my $output = $options->{'download-dir'} ?
                "$options->{'download-dir'}/$file" : "$tbbinfos->{tmpdir}/$file";
        return $output if -f $output;
        print "Downloading $tbbinfos->{tbbfile}\n";
        my $ua = LWP::UserAgent->new;
        my $resp = $ua->get($tbbinfos->{tbbfile}, ':content_file' => $output);
        exit_error "Error downloading $tbbinfos->{tbbfile}:\n" . $resp->status_line
                unless $resp->is_success;
        $tbbinfos->{tbbfile} = $output;
    }
    exit_error "File $tbbinfos->{tbbfile} does not exist"
                unless -f $tbbinfos->{tbbfile};
}

sub tbb_filename_infos {
    my ($tbbfile) = @_;
    my (undef, undef, $file) = File::Spec->splitpath($tbbfile);
boklm's avatar
boklm committed
424
    my %res = (filename => $file, tbbfile => $tbbfile);
425
    if ($file =~ m/^tor-browser-linux(..)-([^_]+)_(.+)\.tar\.xz$/) {
426
        @res{qw(os version language)} = ('Linux', $2, $3);
427
428
        $res{arch} = $1 eq '64' ? 'x86_64' : 'x86';
    } elsif ($file =~ m/^torbrowser-install-([^_]+)_(.+)\.exe$/) {
429
        @res{qw(os arch version language)} = ('Windows', 'x86', $1, $2);
430
    } elsif ($file =~ m/^TorBrowserBundle-(.+)-osx32_(.+)\.zip$/) {
431
432
433
434
435
436
437
438
439
440
441
442
443
        @res{qw(os arch version language)} = ('MacOSX', 'x86', $1, $2);
    } else {
        return undef;
    }
    if ($options->{virustotal}) {
        $res{type} = 'browserbundle_virustotal';
        $res{tests} = [
            {
                name   => 'virustotal',
                type   => 'virustotal',
                descr  => 'Analyze files on virustotal.com',
            },
        ];
444
    } else {
445
446
        $res{type} = 'browserbundle';
        $res{tests} = [ map { { %$_ } } @tests ];
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
    }
    return \%res;
}

sub extract_tbb {
    my ($tbbinfos) = @_;
    exit_error "Can't open file $tbbinfos->{tbbfile}" unless -f $tbbinfos->{tbbfile};
    my $tbbfile = File::Spec->rel2abs($tbbinfos->{tbbfile});
    my $tmpdir = $tbbinfos->{tmpdir};
    chdir $tmpdir;
    if ($tbbinfos->{os} eq 'Linux') {
        system('tar', 'xf', $tbbfile);
        $tbbinfos->{tbbdir} = "$tmpdir/tor-browser_$tbbinfos->{language}";
        $tbbinfos->{tbbdir} .= '/Browser' if $options->{newlayout};
    } elsif ($tbbinfos->{os} eq 'Windows') {
        my (undef, undef, $f) = File::Spec->splitpath($tbbfile);
        copy($tbbfile, "$tmpdir/$f");
        system('7z', 'x', $f);
465
        $tbbinfos->{tbbdir} = "$tmpdir/torbrowser/Browser";
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
        move("$tmpdir/\$_OUTDIR", "$tmpdir/torbrowser");
        move ("$tmpdir/Start Tor Browser.exe", "$tmpdir/torbrowser/");
    }
}

sub xvfb_run {
    my ($test) = @_;
    return () unless $options->{xvfb};
    my $resolution = $test->{resolution} ? $test->{resolution}
                                         : $options->{resolution};
    return ('xvfb-run', '--auto-servernum', '-s', "-screen 0 ${resolution}x24");
}

sub mozmill_cmd {
    if ($OSNAME eq 'cygwin') {
        return ( "$options->{'mozmill-dir'}\\run.cmd", 'mozmill' );
    }
    return ("$options->{virtualenv}/bin/mozmill");
}

sub check_opened_connections {
    my ($tbbinfos, $test) = @_;
    return unless $options->{mbox};
    my $mbox_log = "$tbbinfos->{'results-dir'}/$test->{name}.mbox.log";
    $test->{results}{connections} = {};
    foreach my $line (read_file($mbox_log)) {
        next unless $line =~ m/ > \[\d+\] -> (.+)/;
        $test->{results}{connections}{$1}++;
    }
495
496
497
498
499
500
    my %bad_connections =  %{$test->{results}{connections}};
    delete $bad_connections{"127.0.0.1:$options->{'tor-control-port'}"};
    delete $bad_connections{"127.0.0.1:$options->{'tor-socks-port'}"};
    # For some reasons, tor-browser creates two connections to the default
    # socks port even when when TOR_SOCKS_PORT is set
    # https://lists.torproject.org/pipermail/tbb-dev/2014-May/000050.html
boklm's avatar
boklm committed
501
502
503
504
    if (defined $bad_connections{'127.0.0.1:9150'}
        && $bad_connections{'127.0.0.1:9150'} <= 2) {
        delete $bad_connections{'127.0.0.1:9150'}
    }
505
506
    $test->{results}{success} = 0 if %bad_connections;
    $test->{results}{bad_connections} = \%bad_connections;
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
}

sub check_modified_files {
    my ($tbbinfos, $test) = @_;
    return unless $options->{mbox};
    my $sandbox_dir = "$tbbinfos->{'results-dir'}/$test->{name}.sandbox";
    return unless -d $sandbox_dir;
    my $add_modified_file = sub {
        return if -d $File::Find::name;
        my $fname = $File::Find::name;
        $fname =~ s{^\Q$sandbox_dir\E}{};
        $fname =~ s{^\Q$tbbinfos->{tbbdir}\E/}{};
        push @{$test->{results}{modified_files}}, $fname;
    };
    find($add_modified_file, $sandbox_dir);
522
    return unless -f "$sandbox_dir.meta";
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
    foreach my $meta (read_file("$sandbox_dir.meta")) {
        if ($meta =~ m/^D:(.*):1$/) {
            my $fname = $1;
            $fname =~ s{^\Q$tbbinfos->{tbbdir}\E/}{};
            push @{$test->{results}{removed_files}}, $fname;
        }
    }
}

sub ff_wrapper {
    my ($tbbinfos, $test) = @_;
    my $wrapper_file = "$tbbinfos->{tbbdir}/ff_wrapper";
    return $wrapper_file if -f $wrapper_file;
    my $wrapper = <<EOF;
#!/bin/sh
set -e
539
export LD_LIBRARY_PATH="$tbbinfos->{tbbdir}:$tbbinfos->{tordir}"
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
exec \'$tbbinfos->{ffbin}\' "\$@"
EOF
    write_file($wrapper_file, $wrapper);
    chmod 0700, $wrapper_file;
    return $wrapper_file;
}

sub ff_mbox_wrapper {
    my ($tbbinfos, $test) = @_;
    mkdir "$tbbinfos->{'results-dir'}/$test->{name}.sandbox";
    my $ff_wrapper = ff_wrapper($tbbinfos, $test);
    my $wrapper = <<EOF;
#!/bin/sh
set -e
echo log file: $tbbinfos->{'results-dir'}/$test->{name}.mbox.log
exec mbox -i -r \'$tbbinfos->{'results-dir'}/$test->{name}.sandbox\' \\
        -o \'!cat >> $tbbinfos->{'results-dir'}/$test->{name}.mbox.log\' \\
        -s -p $FindBin::Bin/mbox.profile \\
        -- \\
        \'$ff_wrapper\' "\$@"
EOF
    my $wrapper_file = "$tbbinfos->{tbbdir}/ff_$test->{name}";
    write_file($wrapper_file, $wrapper);
    chmod 0700, $wrapper_file;
    return $wrapper_file;
}

sub ffbin_path {
    my ($tbbinfos, $test) = @_;
    if ($OSNAME eq 'cygwin') {
        return winpath("$tbbinfos->{ffbin}.exe");
    }
    return $options->{mbox} ? ff_mbox_wrapper($tbbinfos, $test)
           : ff_wrapper($tbbinfos, $test);
}

576
577
578
sub mozmill_export_options {
    my ($tbbinfos, $test) = @_;
    my $options_file = winpath("$FindBin::Bin/mozmill-tests/lib/testsuite.js");
579
580
    my $json_opts = encode_json clone_strip_coderef $options;
    my $json_test = encode_json clone_strip_coderef $test;
boklm's avatar
boklm committed
581
582
    my $json_tbbinfos = encode_json clone_strip_coderef
                                { %$tbbinfos, tests => undef };
583
584
585
    my $content = <<EOF;
var options = $json_opts;
var test = $json_test;
boklm's avatar
boklm committed
586
var tbbinfos = $json_tbbinfos;
587
588
exports.options = options;
exports.test = test;
boklm's avatar
boklm committed
589
exports.tbbinfos = tbbinfos;
590
591
592
593
EOF
    write_file($options_file, $content);
}

594
595
596
sub mozmill_run {
    my ($tbbinfos, $test) = @_;
    return unless $options->{mozmill};
597
    mozmill_export_options($tbbinfos, $test);
598
    set_test_prefs($tbbinfos, $test);
599
600
601
602
    $test->{screenshots} = [];
    my $screenshots_tmp = File::Temp::newdir('XXXXXX', DIR => $options->{tmpdir});
    $ENV{'MOZMILL_SCREENSHOTS'} = winpath($screenshots_tmp);
    my $results_file = "$tbbinfos->{'results-dir'}/$test->{name}.json";
603
    my $mozmill_test = $test->{mozmill_test} // $test->{name};
604
605
    system(xvfb_run($test), mozmill_cmd(), '-b', ffbin_path($tbbinfos, $test),
        '-p', winpath($tbbinfos->{ffprofiledir}),
606
        '-t', winpath("$FindBin::Bin/mozmill-tests/tbb-tests/$mozmill_test.js"),
607
608
609
610
611
612
613
614
615
        '--report', 'file://' . winpath($results_file));
    my $i = 0;
    for my $screenshot_file (reverse sort glob "$screenshots_tmp/*.png") {
        move($screenshot_file, "$tbbinfos->{'results-dir'}/$test->{name}-$i.png");
        $screenshot_thumbnail->($tbbinfos->{'results-dir'}, "$test->{name}-$i.png");
        push @{$test->{screenshots}}, "$test->{name}-$i.png";
        $i++;
    }
    $test->{results} = decode_json(read_file($results_file));
616
617
    $test->{results}{success} = $test->{results}{results}->[0]->{passed} ?
                        !$test->{results}{results}->[0]->{failed} : 0;
618
    reset_test_prefs($tbbinfos, $test);
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
    check_opened_connections($tbbinfos, $test);
    check_modified_files($tbbinfos, $test);
}

sub selenium_run {
    my ($tbbinfos, $test) = @_;
    return unless $options->{selenium};
    my $result_file = $ENV{SELENIUM_TEST_RESULT_FILE} =
        "$tbbinfos->{'results-dir'}/$test->{name}.json";
    $ENV{TBB_BIN} = ffbin_path($tbbinfos, $test);
    $ENV{TBB_PROFILE} = $tbbinfos->{ffprofiledir};
    system(xvfb_run($test), "$options->{virtualenv}/bin/python",
        "$FindBin::Bin/selenium-tests/run_test", $test->{name});
    $test->{results} = decode_json(read_file($result_file));
    check_opened_connections($tbbinfos, $test);
    check_modified_files($tbbinfos, $test);
}

sub set_tbbpaths {
    my ($tbbinfos) = @_;
    if ($options->{newlayout}) {
        $tbbinfos->{ffbin} = "$tbbinfos->{tbbdir}/firefox";
        $tbbinfos->{tordir} = "$tbbinfos->{tbbdir}/TorBrowser/Tor";
        $tbbinfos->{datadir} = "$tbbinfos->{tbbdir}/TorBrowser/Data";
    } else {
        $tbbinfos->{ffbin} =  "$tbbinfos->{tbbdir}/Browser/firefox";
        $tbbinfos->{tordir} = "$tbbinfos->{tbbdir}/Tor";
        $tbbinfos->{datadir} = "$tbbinfos->{tbbdir}/Data";
    }
    $tbbinfos->{torbin} = "$tbbinfos->{tordir}/tor";
boklm's avatar
boklm committed
649
    $tbbinfos->{ptdir} = winpath("$tbbinfos->{tordir}/PluggableTransports");
650
651
652
653
654
655
656
657
658
659
660
661
662
663
    $tbbinfos->{ffprofiledir} = "$tbbinfos->{datadir}/Browser/profile.default";
}

sub pre_tests {
    my ($tbbinfos) = @_;
    get_tbbfile($tbbinfos);
    if ($tbbinfos->{sha256sum} &&
        $tbbinfos->{sha256sum} ne sha256_hex(read_file($tbbinfos->{tbbfile}))) {
        exit_error "Wrong sha256sum for $tbbinfos->{tbbfile}";
    }
    $tbbinfos->{sha256sum} //= sha256_hex(read_file($tbbinfos->{tbbfile}));
    extract_tbb($tbbinfos);
    set_tbbpaths($tbbinfos);
    chdir $tbbinfos->{tbbdir} || exit_error "Can't enter directory $tbbinfos->{tbbdir}";
boklm's avatar
boklm committed
664
665
    copy "$FindBin::Bin/data/cert_override.txt",
          "TorBrowser/Data/Browser/profile.default/cert_override.txt";
666
    $ENV{TOR_SKIP_LAUNCH} = 1;
667
668
    $ENV{TOR_SOCKS_PORT} = $options->{'tor-socks-port'};
    $ENV{TOR_CONTROL_PORT} = $options->{'tor-control-port'};
boklm's avatar
boklm committed
669
670
671
    if ($options->{xdummy}) {
        $tbbinfos->{Xdisplay} = start_X("$tbbinfos->{'results-dir'}/xorg.log");
    }
672
673
674
675
676
}

sub post_tests {
    my ($tbbinfos) = @_;
    TBBTestSuite::Tests::TorBootstrap::stop_tor($tbbinfos);
boklm's avatar
boklm committed
677
    stop_X($tbbinfos->{Xdisplay}) if $options->{xdummy};
678
679
680
}

1;