GitLab is used only for code review, issue tracking and project management. Canonical locations for source code are still https://gitweb.torproject.org/ https://git.torproject.org/ and git-rw.torproject.org.

BrowserBundleTests.pm 29.9 KB
Newer Older
1
package TBBTestSuite::TestSuite::BrowserBundleTests;
2 3 4

use warnings;
use strict;
boklm's avatar
boklm committed
5 6 7

use parent 'TBBTestSuite::TestSuite';

8 9 10 11 12 13 14
use English;
use FindBin;
use File::Slurp;
use File::Spec;
use File::Find;
use File::Type;
use File::Copy;
15
use File::Temp;
16 17 18
use JSON;
use Digest::SHA qw(sha256_hex);
use LWP::UserAgent;
19
use TBBTestSuite::Common qw(exit_error winpath clone_strip_coderef);
20 21
use TBBTestSuite::Options qw($options);
use TBBTestSuite::Tests::VirusTotal qw(virustotal_run);
22
use TBBTestSuite::Tests::Command qw(command_run);
23
use TBBTestSuite::Tests::TorBootstrap;
boklm's avatar
boklm committed
24
use TBBTestSuite::XServer qw(start_X stop_X set_Xmode);
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39

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 { };
    }
}

boklm's avatar
boklm committed
40 41 42 43
sub test_types {
    return {
        tor_bootstrap => \&TBBTestSuite::Tests::TorBootstrap::start_tor,
        mozmill       => \&mozmill_run,
44
        marionette    => \&marionette_run,
boklm's avatar
boklm committed
45 46 47 48 49
        selenium      => \&selenium_run,
        virustotal    => \&virustotal_run,
        command       => \&command_run,
    };
}
50

boklm's avatar
boklm committed
51 52 53
sub type {
    'browserbundle';
}
54

boklm's avatar
boklm committed
55 56 57
sub description {
    'Tor Browser Bundle integration tests';
}
58

59 60
our @tests = (
    {
boklm's avatar
boklm committed
61 62 63 64 65 66 67 68 69
        name            => 'readelf_RELRO',
        fail_type       => 'warning',
        type            => 'command',
        descr           => 'Check if binaries are RELocation Read-Only',
        files           => \&tbb_binfiles,
        command         => [ 'readelf', '-ld' ],
        check_output    => sub { ( $_[0] =~ m/GNU_RELRO/ )
                                 && ( $_[0] =~ m/BIND_NOW/ ) },
        enable          => sub { $OSNAME eq 'linux' },
70 71
    },
    {
boklm's avatar
boklm committed
72 73 74 75 76 77 78 79
        name            => 'readelf_stack_canary',
        fail_type       => 'warning',
        type            => 'command',
        descr           => 'Check for stack canary support',
        files           => \&tbb_binfiles,
        command         => [ 'readelf', '-s' ],
        check_output    => sub { $_[0] =~ m/__stack_chk_fail/ },
        enable          => sub { $OSNAME eq 'linux' },
80 81
    },
    {
boklm's avatar
boklm committed
82 83 84 85 86 87 88
        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/) },
        enable          => sub { $OSNAME eq 'linux' },
89 90
    },
    {
boklm's avatar
boklm committed
91 92 93 94 95 96 97
        name            => 'readelf_PIE',
        type            => 'command',
        descr           => 'Check for PIE support',
        files           => \&tbb_binfiles,
        command         => [ 'readelf', '-h' ],
        check_output    => sub { $_[0] =~ m/Type:\s+DYN/ },
        enable          => sub { $OSNAME eq 'linux' },
98 99
    },
    {
boklm's avatar
boklm committed
100 101 102 103 104 105 106 107
        name            => 'readelf_no_rpath',
        fail_type       => 'warning',
        type            => 'command',
        descr           => 'Check for no rpath',
        files           => \&tbb_binfiles,
        command         => [ 'readelf', '-d' ],
        check_output    => sub { ! ( $_[0] =~ m/RPATH/ ) },
        enable          => sub { $OSNAME eq 'linux' },
108 109
    },
    {
boklm's avatar
boklm committed
110 111 112 113 114 115 116
        name            => 'readelf_no_runpath',
        type            => 'command',
        descr           => 'Check for no runpath',
        files           => \&tbb_binfiles,
        command         => [ 'readelf', '-d' ],
        check_output    => sub { ! ( $_[0] =~ m/runpath/ ) },
        enable          => sub { $OSNAME eq 'linux' },
117 118
    },
    {
boklm's avatar
boklm committed
119 120 121 122 123
        name            => 'tor_httpproxy',
        type            => 'tor_bootstrap',
        descr           => 'Access tor using an http proxy',
        httpproxy       => 1,
        enable          => sub { $OSNAME eq 'linux' },
124
        run_once        => 1,
125 126
    },
    {
boklm's avatar
boklm committed
127 128 129
        name            => 'tor_bridge',
        type            => 'tor_bootstrap',
        descr           => 'Access tor using a bridge',
130
        enable          => sub { $OSNAME eq 'linux' },
131
        run_once        => 1,
132 133
    },
    {
boklm's avatar
boklm committed
134 135 136 137
        name            => 'tor_bridge_httpproxy',
        type            => 'tor_bootstrap',
        descr           => 'Access tor using a bridge and an http proxy',
        httpproxy       => 1,
138
        enable          => sub { $OSNAME eq 'linux' },
139
        run_once        => 1,
140 141
    },
    {
boklm's avatar
boklm committed
142 143 144 145
        name            => 'tor_obfs3',
        type            => 'tor_bootstrap',
        descr           => 'Access tor using obfs3',
        enable          => sub { $OSNAME eq 'linux' },
146
        run_once        => 1,
147 148
    },
    {
boklm's avatar
boklm committed
149 150 151 152 153
        name            => 'tor_obfs3_httpproxy',
        type            => 'tor_bootstrap',
        descr           => 'Access tor using obfs3 and an http proxy',
        httpproxy       => 1,
        enable          => sub { $OSNAME eq 'linux' },
154
        run_once        => 1,
155
    },
156
    {
boklm's avatar
boklm committed
157 158 159 160
        name            => 'tor_obfs4',
        type            => 'tor_bootstrap',
        descr           => 'Access tor using obfs4',
        enable          => sub { $OSNAME eq 'linux' && $_[0]->{version} !~ m/^4.0/ },
161
        run_once        => 1,
162 163
    },
    {
boklm's avatar
boklm committed
164 165 166 167 168
        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/ },
169
        run_once        => 1,
170
    },
171
    {
boklm's avatar
boklm committed
172 173 174 175
        name            => 'tor_fte',
        type            => 'tor_bootstrap',
        descr           => 'Access tor using fteproxy',
        enable          => sub { $OSNAME eq 'linux' },
176
        run_once        => 1,
177 178
    },
    {
boklm's avatar
boklm committed
179 180 181 182 183
        name            => 'tor_fte_httpproxy',
        type            => 'tor_bootstrap',
        descr           => 'Access tor using fteproxy and an http proxy',
        httpproxy       => 1,
        enable          => sub { $OSNAME eq 'linux' },
184
        run_once        => 1,
185
    },
186 187 188 189 190
    {
        name            => 'tor_scramblesuit',
        type            => 'tor_bootstrap',
        descr           => 'Access tor using scramblesuit',
        enable          => sub { $OSNAME eq 'linux' },
191
        run_once        => 1,
192 193 194 195 196 197 198
    },
    {
        name            => 'tor_scramblesuit_httpproxy',
        type            => 'tor_bootstrap',
        descr           => 'Access tor using scramblesuit and an http proxy',
        httpproxy       => 1,
        enable          => sub { $OSNAME eq 'linux' },
199
        run_once        => 1,
200
    },
201 202 203 204 205
    {
        name            => 'tor_meek-google',
        type            => 'tor_bootstrap',
        descr           => 'Access tor using meek-google',
        enable          => sub { $OSNAME eq 'linux' },
206
        run_once        => 1,
207 208 209 210 211 212
    },
    {
        name            => 'tor_meek-amazon',
        type            => 'tor_bootstrap',
        descr           => 'Access tor using meek-amazon',
        enable          => sub { $OSNAME eq 'linux' },
213
        run_once        => 1,
214 215 216 217 218 219
    },
    {
        name            => 'tor_meek-azure',
        type            => 'tor_bootstrap',
        descr           => 'Access tor using meek-azure',
        enable          => sub { $OSNAME eq 'linux' },
220
        run_once        => 1,
221
    },
222
    {
boklm's avatar
boklm committed
223 224 225 226 227
        name            => 'tor_bootstrap',
        type            => 'tor_bootstrap',
        descr           => 'Check that we can bootstrap tor',
        fail_type       => 'fatal',
        no_kill         => 1,
228 229 230
        use_default_config => 1,
    },
    {
boklm's avatar
boklm committed
231 232 233
        name            => 'screenshots',
        type            => 'mozmill',
        descr           => 'Take some screenshots',
234 235
    },
    {
boklm's avatar
boklm committed
236
        name            => 'check',
237
        type            => 'marionette',
boklm's avatar
boklm committed
238 239
        use_net         => 1,
        descr           => 'Check that http://check.torproject.org/ think we are using tor',
240 241
    },
    {
boklm's avatar
boklm committed
242 243 244 245
        name            => 'https-everywhere',
        type            => 'mozmill',
        use_net         => 1,
        descr           => 'Check that https everywhere is enabled and working',
246 247
    },
    {
boklm's avatar
boklm committed
248 249 250 251 252 253
        name            => 'https-everywhere-disabled',
        type            => 'mozmill',
        descr           => 'Check that https everywhere is not doing anything when disabled',
        use_net         => 1,
        pre             => sub { toggle_https_everywhere($_[0], 0) },
        post            => sub { toggle_https_everywhere($_[0], 1) },
254 255
    },
    {
boklm's avatar
boklm committed
256
        name            => 'settings',
257
        type            => 'marionette',
boklm's avatar
boklm committed
258
        descr           => 'Check that some important settings are correctly set',
259
    },
boklm's avatar
boklm committed
260
    {
boklm's avatar
boklm committed
261 262 263 264
        name            => 'acid3',
        type            => 'mozmill',
        descr           => 'acid3 tests',
        use_net         => 1,
boklm's avatar
boklm committed
265
        retry           => 4,
boklm's avatar
boklm committed
266
    },
boklm's avatar
boklm committed
267
    {
boklm's avatar
boklm committed
268 269 270 271 272 273 274 275
        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/ },
boklm's avatar
boklm committed
276 277
    },
    {
boklm's avatar
boklm committed
278 279 280 281 282 283 284 285
        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/ },
boklm's avatar
boklm committed
286 287
    },
    {
boklm's avatar
boklm committed
288 289 290 291 292 293 294 295
        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/ },
boklm's avatar
boklm committed
296 297
    },
    {
boklm's avatar
boklm committed
298 299 300 301 302 303 304 305
        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
306
    },
boklm's avatar
boklm committed
307
    {
boklm's avatar
boklm committed
308 309 310
        name            => 'dom-objects-enumeration',
        type            => 'mozmill',
        descr           => 'Check the list of DOM Objects exposed in the global namespace',
boklm's avatar
boklm committed
311
    },
312
    {
boklm's avatar
boklm committed
313 314 315 316
        name            => 'navigation-timing',
        type            => 'mozmill',
        descr           => 'Check that the Navigation Timing API is really disabled',
        use_net         => 1,
317
    },
318
    {
boklm's avatar
boklm committed
319 320 321 322
        name            => 'resource-timing',
        type            => 'mozmill',
        descr           => 'Check that the Resource Timing API is really disabled',
        use_net         => 1,
323
    },
324
    {
boklm's avatar
boklm committed
325 326 327
        name            => 'searchengines',
        type            => 'mozmill',
        descr           => 'Check that we have the default search engines set',
328
    },
boklm's avatar
boklm committed
329
    {
boklm's avatar
boklm committed
330 331 332 333 334
        name            => 'noscript',
        type            => 'mozmill',
        descr           => 'Check that noscript options are working',
        use_net         => 1,
        prefs           => {
335
            'extensions.torbutton.security_slider' => 2,
boklm's avatar
boklm committed
336
        },
boklm's avatar
boklm committed
337
        enable          => sub { $_[0]->{version} !~ m/^4.0/ },
boklm's avatar
boklm committed
338
    },
boklm's avatar
boklm committed
339
    {
boklm's avatar
boklm committed
340
        name            => 'fp_screen_dimensions',
341
        type            => 'marionette',
boklm's avatar
boklm committed
342
        descr           => 'Check that screen dimensions are spoofed correctly',
343 344
    },
    {
boklm's avatar
boklm committed
345
        name            => 'fp_screen_coords',
346
        type            => 'marionette',
boklm's avatar
boklm committed
347
        descr           => 'Check that screenX, screenY, screenLeft, screenTop, mozInnerScreenX, mozInnerScreenY are 0',
348 349
    },
    {
boklm's avatar
boklm committed
350
        name            => 'fp_plugins',
351
        type            => 'marionette',
boklm's avatar
boklm committed
352
        descr           => 'Check that plugins are disabled',
353 354
    },
    {
boklm's avatar
boklm committed
355
        name            => 'fp_useragent',
356
        type            => 'marionette',
boklm's avatar
boklm committed
357
        descr           => 'Check that userAgent is as expected',
358 359
    },
    {
boklm's avatar
boklm committed
360
        name            => 'fp_navigator',
361
        type            => 'marionette',
boklm's avatar
boklm committed
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
        descr           => 'Check that navigator properties are as expected',
    },
    {
        name            => 'play_videos',
        type            => 'mozmill',
        descr           => 'Play some videos',
        use_net         => 1,
        mozmill_test    => 'test_page',
        remote          => 1,
        timeout         => 50000,
        interval        => 100,
    },
    {
        name            => 'svg-disable',
        type            => 'mozmill',
        descr           => 'Check if disabling svg is working',
        mozmill_test    => 'svg',
        svg_enabled     => 0,
        use_net         => 1,
        prefs           => {
382 383 384
            'extensions.torbutton.security_custom' => 'true',
            'svg.in-content.enabled' => 'false',
        },
boklm's avatar
boklm committed
385
        enable          => sub { $OSNAME eq 'linux' },
386 387
    },
    {
boklm's avatar
boklm committed
388 389 390 391 392 393 394
        name            => 'svg-enable',
        type            => 'mozmill',
        descr           => 'Check if enabling svg is working',
        mozmill_test    => 'svg',
        use_net         => 1,
        svg_enabled     => 1,
        prefs           => {
395 396 397
            'extensions.torbutton.security_custom' => 'true',
            'svg.in-content.enabled' => 'true',
        },
boklm's avatar
boklm committed
398
        enable          => sub { $OSNAME eq 'linux' },
399
    },
400 401 402 403 404 405
);

sub toggle_https_everywhere {
    my ($tbbinfos, $t) = @_;
    my $prefs = $tbbinfos->{ffprofiledir} . '/extensions/'
        . 'https-everywhere@eff.org/defaults/preferences/preferences.js';
406 407 408
    my $prefs_eff = $tbbinfos->{ffprofiledir} . '/extensions/'
        . 'https-everywhere-eff@eff.org/defaults/preferences/preferences.js';
    $prefs = $prefs_eff unless -f $prefs;
409 410 411 412 413 414 415 416 417 418 419 420 421
    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);
}

422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
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
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
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;
}

457 458 459
sub tbb_binfiles {
    my ($tbbinfos, $test) = @_;
    return $tbbinfos->{binfiles} if $tbbinfos->{binfiles};
460
    my %binfiles;
461 462 463 464
    my %wanted_types = (
        'application/x-executable-file' => 1,
        'application/x-ms-dos-executable' => 1,
    );
465 466 467
    my $wanted = sub {
        return unless -f $File::Find::name;
        my $type = File::Type->new->checktype_filename($File::Find::name);
468
        return unless $wanted_types{$type};
boklm's avatar
boklm committed
469 470 471
        my $name = $File::Find::name;
        $name =~ s/^$tbbinfos->{tbbdir}\///;
        $binfiles{$name} = 1;
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509
    };
    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 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);
510 511 512 513 514
        if ($tbbinfos->{language} eq 'ALL') {
            $tbbinfos->{tbbdir} = "$tmpdir/tor-browser";
        } else {
            $tbbinfos->{tbbdir} = "$tmpdir/tor-browser_$tbbinfos->{language}";
        }
515
        $tbbinfos->{tbbdir} .= '/Browser';
516 517 518 519
    } elsif ($tbbinfos->{os} eq 'Windows') {
        my (undef, undef, $f) = File::Spec->splitpath($tbbfile);
        copy($tbbfile, "$tmpdir/$f");
        system('7z', 'x', $f);
520
        $tbbinfos->{tbbdir} = "$tmpdir/torbrowser/Browser";
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
        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) = @_;
543 544 545 546 547 548
    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
549 550 551 552
    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'}
    }
553 554 555 556
    if (%bad_connections) {
        $test->{results}{success} = 0;
        $test->{retry} = 0;
    }
557
    $test->{results}{bad_connections} = \%bad_connections;
558 559 560 561
}

sub check_modified_files {
    my ($tbbinfos, $test) = @_;
562
    my @bad_modified_files = @{$test->{results}{modified_files}};
563 564 565 566
    if (@bad_modified_files) {
        $test->{results}{success} = 0;
        $test->{retry} = 0;
    }
567 568 569
    $test->{results}{bad_modified_files} = \@bad_modified_files;
}

570 571 572 573
sub clean_strace {
    my ($tbbinfos, $test) = @_;
    my $logfile = "$tbbinfos->{'results-dir'}/$test->{name}.strace";
    unlink $logfile;
boklm's avatar
boklm committed
574
    unlink "$logfile.tmp";
575 576
}

577 578 579 580 581
sub parse_strace {
    my ($tbbinfos, $test) = @_;
    my %ignore_files = map { $_ => 1 } qw(/dev/null /dev/tty);
    my %files;
    my $logfile = "$tbbinfos->{'results-dir'}/$test->{name}.strace";
boklm's avatar
boklm committed
582
    my $logfile_tmp = "$tbbinfos->{'results-dir'}/$test->{name}.strace.tmp";
583 584 585
    $test->{results}{connections} = {};
    my %modified_files;
    my %removed_files;
586 587 588 589 590
    if (-f "$logfile.tmp") {
        my $txt = read_file("$logfile.tmp");
        write_file($logfile, { append => 1 }, $txt);
        unlink "$logfile.tmp";
    }
591
    my @lines = read_file($logfile) if -f $logfile;
592
    push @lines, read_file($logfile_tmp) if -f $logfile_tmp;
593
    foreach my $line (@lines) {
594 595 596 597 598
        if ($line =~ m/^\d+ open\("((?:[^"\\]++|\\.)*+)", ([^\)]+)/ ||
            $line =~ m/^\d+ openat\([^,]+, "((?:[^"\\]++|\\.)*+)", ([^\)]+)/) {
            next if $2 =~ m/O_RDONLY/;
            next if $1 =~ m/^$tbbinfos->{tbbdir}/;
            next if $ignore_files{$1};
599 600 601
            if ($ENV{'MOZMILL_SCREENSHOTS'}) {
                next if $1 =~ m/^$ENV{'MOZMILL_SCREENSHOTS'}/;
            }
602 603 604 605 606 607 608 609 610
            $modified_files{$1}++;
        }
        if ($line =~ m/^\d+ unlink\("((?:[^"\\]++|\\.)*+)"/) {
            next if $1 =~ m/^$tbbinfos->{tbbdir}/;
            $removed_files{$1}++;
            delete $modified_files{$1} unless -f $1;
        }
        if ($line =~ m/^\d+ connect\(\d+, {sa_family=AF_INET, sin_port=htons\((\d+)\), sin_addr=inet_addr\("((?:[^"\\]++|\\.)*+)"\)/) {
            $test->{results}{connections}{"$2:$1"}++;
611 612
        }
    }
613 614
    $test->{results}{modified_files} = [ keys %modified_files ];
    $test->{results}{removed_files} = [ keys %removed_files ];
615 616 617 618 619 620 621 622 623
}

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
624
export HOME="$tbbinfos->{tbbdir}"
625
export LD_LIBRARY_PATH="$tbbinfos->{tbbdir}:$tbbinfos->{tordir}"
626 627 628 629 630 631 632
exec \'$tbbinfos->{ffbin}\' "\$@"
EOF
    write_file($wrapper_file, $wrapper);
    chmod 0700, $wrapper_file;
    return $wrapper_file;
}

633
sub ff_strace_wrapper {
634 635
    my ($tbbinfos, $test) = @_;
    my $ff_wrapper = ff_wrapper($tbbinfos, $test);
636
    my $logfile = "$tbbinfos->{'results-dir'}/$test->{name}.strace";
637 638
    my $wrapper = <<EOF;
#!/bin/sh
639 640 641 642 643 644
if [ -f $logfile.tmp ]
then
   cat $logfile.tmp >> $logfile
   rm $logfile.tmp
fi
echo \$@ >> /tmp/ff_run.log
645 646 647 648 649
strace -f -o $logfile.tmp -- \'$ff_wrapper\' "\$@"
exit_code=\$?
cat $logfile.tmp >> $logfile
rm $logfile.tmp
exit \$?
650 651 652 653 654 655 656 657 658 659 660 661
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");
    }
662 663
    my %t = map { $_ => 1 } qw(mozmill marionette);
    if ($options->{use_strace} && $t{$test->{type}}) {
boklm's avatar
boklm committed
664 665
        return ff_strace_wrapper($tbbinfos, $test);
    }
666
    return ff_wrapper($tbbinfos, $test);
667 668
}

669 670 671
sub mozmill_export_options {
    my ($tbbinfos, $test) = @_;
    my $options_file = winpath("$FindBin::Bin/mozmill-tests/lib/testsuite.js");
672 673
    my $json_opts = encode_json clone_strip_coderef $options;
    my $json_test = encode_json clone_strip_coderef $test;
boklm's avatar
boklm committed
674 675
    my $json_tbbinfos = encode_json clone_strip_coderef
                                { %$tbbinfos, tests => undef };
676 677 678
    my $content = <<EOF;
var options = $json_opts;
var test = $json_test;
boklm's avatar
boklm committed
679
var tbbinfos = $json_tbbinfos;
680 681
exports.options = options;
exports.test = test;
boklm's avatar
boklm committed
682
exports.tbbinfos = tbbinfos;
683 684 685 686
EOF
    write_file($options_file, $content);
}

687 688 689 690 691 692 693 694 695 696 697 698
sub marionette_export_options {
    my ($tbbinfos, $test) = @_;
    my $options_file = File::Temp->new();
    my $json = {
        options  => clone_strip_coderef($options),
        test     => clone_strip_coderef($test),
        tbbinfos => clone_strip_coderef({ %$tbbinfos, tests => undef }),
    };
    write_file($options_file, encode_json($json));
    return $options_file;
}

699 700 701 702 703 704 705
sub marionette_run {
    my ($tbbinfos, $test) = @_;
    if ($test->{tried} && $test->{use_net}) {
        TBBTestSuite::Tests::TorBootstrap::send_newnym($tbbinfos);
    }
    set_test_prefs($tbbinfos, $test);

706 707
    my $options_file = marionette_export_options($tbbinfos, $test);
    $ENV{TESTSUITE_DATA_FILE} = winpath($options_file);
708 709 710 711
    my $result_file_html = "$tbbinfos->{'results-dir'}/$test->{name}.html";
    my $result_file_txt = "$tbbinfos->{'results-dir'}/$test->{name}.txt";
    #--log-unittest  ./res.txt --log-html ./res.html
    my $bin = $OSNAME eq 'cygwin' ? 'Scripts' : 'bin';
712 713 714 715
    my $pypath = $ENV{PYTHONPATH};
    $ENV{PYTHONPATH} //= '';
    $ENV{PYTHONPATH} = winpath("$FindBin::Bin/marionette/tor_browser_tests/lib")
        . ":$ENV{PYTHONPATH}";
716 717 718 719 720 721
    system(xvfb_run($test), "$FindBin::Bin/virtualenv-marionette/$bin/tor-browser-tests",
        '--log-unittest', winpath($result_file_txt),
        '--log-html', winpath($result_file_html),
        '--binary', ffbin_path($tbbinfos, $test),
        '--profile', winpath($tbbinfos->{ffprofiledir}),
        winpath("$FindBin::Bin/marionette/tor_browser_tests/test_$test->{name}.py"));
722
    $ENV{PYTHONPATH} = $pypath;
723 724 725 726 727 728 729 730 731 732
    my @txt_log = read_file($result_file_txt);
    my $res_line = shift @txt_log;
    $test->{results}{success} = $res_line eq ".\n" || $res_line eq ".\r\n";
    $test->{results}{log} = join '', @txt_log;
    reset_test_prefs($tbbinfos, $test);
    parse_strace($tbbinfos, $test);
    check_opened_connections($tbbinfos, $test);
    check_modified_files($tbbinfos, $test);
}

733 734 735
sub mozmill_run {
    my ($tbbinfos, $test) = @_;
    return unless $options->{mozmill};
736 737 738
    if ($test->{tried} && $test->{use_net}) {
        TBBTestSuite::Tests::TorBootstrap::send_newnym($tbbinfos);
    }
739
    clean_strace($tbbinfos, $test) if $options->{use_strace};
740
    mozmill_export_options($tbbinfos, $test);
741
    set_test_prefs($tbbinfos, $test);
742 743 744 745
    $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";
746
    my $mozmill_test = $test->{mozmill_test} // $test->{name};
747 748
    system(xvfb_run($test), mozmill_cmd(), '-b', ffbin_path($tbbinfos, $test),
        '-p', winpath($tbbinfos->{ffprofiledir}),
749
        '-t', winpath("$FindBin::Bin/mozmill-tests/tbb-tests/$mozmill_test.js"),
750 751 752 753 754 755 756 757
        '--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++;
    }
758 759 760 761 762 763 764
    if (-f $results_file) {
        $test->{results} = decode_json(read_file($results_file));
        $test->{results}{success} = $test->{results}{results}->[0]->{passed} ?
                                !$test->{results}{results}->[0]->{failed} : 0;
    } else {
        $test->{results}{success} = 0;
    }
765
    reset_test_prefs($tbbinfos, $test);
boklm's avatar
boklm committed
766 767 768 769
    if ($options->{use_strace}) {
        parse_strace($tbbinfos, $test);
        check_opened_connections($tbbinfos, $test);
        check_modified_files($tbbinfos, $test);
770
        clean_strace($tbbinfos, $test) if $test->{results}{success};
boklm's avatar
boklm committed
771
    }
772 773 774 775 776
}

sub selenium_run {
    my ($tbbinfos, $test) = @_;
    return unless $options->{selenium};
777 778 779
    if ($test->{tried} && $test->{use_net}) {
        TBBTestSuite::Tests::TorBootstrap::send_newnym($tbbinfos);
    }
780 781 782 783 784 785 786 787 788 789 790
    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));
}

sub set_tbbpaths {
    my ($tbbinfos) = @_;
791 792 793
    $tbbinfos->{ffbin} = "$tbbinfos->{tbbdir}/firefox";
    $tbbinfos->{tordir} = "$tbbinfos->{tbbdir}/TorBrowser/Tor";
    $tbbinfos->{datadir} = "$tbbinfos->{tbbdir}/TorBrowser/Data";
794
    $tbbinfos->{torbin} = "$tbbinfos->{tordir}/tor";
boklm's avatar
boklm committed
795
    $tbbinfos->{ptdir} = winpath("$tbbinfos->{tordir}/PluggableTransports");
796 797 798
    $tbbinfos->{ffprofiledir} = "$tbbinfos->{datadir}/Browser/profile.default";
}

boklm's avatar
boklm committed
799 800 801 802
sub new {
    my ($ts, $testsuite) = @_;
    $testsuite->{type} = 'browserbundle';
    $testsuite->{tests} = [ map { { %$_ } } @tests ];
803 804
    return undef unless $testsuite->{os} eq $options->{os};
    return undef unless $testsuite->{arch} eq $options->{arch};
boklm's avatar
boklm committed
805 806 807
    return bless $testsuite, $ts;
}

808 809 810 811 812 813 814 815 816 817
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);
818 819 820 821
    my $prefs_file = "$tbbinfos->{ffprofiledir}/preferences/extension-overrides.js";
    open(my $prefs_fh, '>>', $prefs_file);
    print $prefs_fh 'pref("extensions.torbutton.prompted_language", true);', "\n";
    close $prefs_fh;
822
    chdir $tbbinfos->{tbbdir} || exit_error "Can't enter directory $tbbinfos->{tbbdir}";
boklm's avatar
boklm committed
823 824
    copy "$FindBin::Bin/data/cert_override.txt",
          "TorBrowser/Data/Browser/profile.default/cert_override.txt";
825
    $ENV{TOR_SKIP_LAUNCH} = 1;
826 827
    $ENV{TOR_SOCKS_PORT} = $options->{'tor-socks-port'};
    $ENV{TOR_CONTROL_PORT} = $options->{'tor-control-port'};
boklm's avatar
boklm committed
828 829 830
    if ($options->{xdummy}) {
        $tbbinfos->{Xdisplay} = start_X("$tbbinfos->{'results-dir'}/xorg.log");
    }
831 832 833 834 835
}

sub post_tests {
    my ($tbbinfos) = @_;
    TBBTestSuite::Tests::TorBootstrap::stop_tor($tbbinfos);
boklm's avatar
boklm committed
836
    stop_X($tbbinfos->{Xdisplay}) if $options->{xdummy};
837 838 839
}

1;