tbb-testsuite 4.68 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
9
use Data::Dump qw/dd/;
use FindBin;
boklm's avatar
boklm committed
10
use LWP::UserAgent;
boklm's avatar
boklm committed
11
use Digest::SHA qw(sha256_hex);
12
13

my %default_options = (
boklm's avatar
boklm committed
14
15
    os       => 'Linux',
    arch     => 'x86_64',
16
17
    mozmill  => 1,
    selenium => 1,
18
19
20
21
22
23
24
25
);
my $options = get_options(@ARGV);
sub exit_error {
    print STDERR "Error: ", $_[0], "\n";
    exit (exists $_[1] ? $_[1] : 1);
}

sub get_options {
26
    my @options = qw(mozmill! selenium!);
27
28
    my %res = %default_options;
    Getopt::Long::GetOptionsFromArray(\@_, \%res, @options) || exit 1;
29
    $res{files} = \@_;
30
31
32
    return \%res;
}

boklm's avatar
boklm committed
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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;
}

49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
sub tbb_filename_infos {
    my ($tbbfile) = @_;
    my (undef, undef, $file) = File::Spec->splitpath($tbbfile);
    my %res;
    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;
}
69
70
71
72
73

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
74
    my $tmpdir = $options->{tbbinfos}{tmpdir};
75
76
    chdir $tmpdir;
    system('tar', 'xf', $tbbfile);
77
    return "$tmpdir/tor-browser_$options->{tbbinfos}{language}";
78
79
80
}

sub setup_tbb {
81
    $ENV{TOR_SKIP_LAUNCH} = 1;
82
83
84
85
86
87
88
89
90
91
}

sub mozmill_run {
    my ($test_path) = @_;
    system('mozmill', '-b', "$options->{tbbdir}/Browser/firefox", '-p',
        "$options->{tbbdir}/Data/Browser/profile.default", '-t',
        "$FindBin::Bin/mozmill-tests/tbb-tests/$test_path");
}

sub mozmill_tests {
92
    return unless $options->{mozmill};
93
94
95
96
97
98
99
100
    my @tests = (
        'tbbScreenshot.js',
    );
    foreach my $test (@tests) {
        mozmill_run($test);
    }
}

boklm's avatar
boklm committed
101
sub selenium_tests {
102
    return unless $options->{selenium};
boklm's avatar
boklm committed
103
104
105
106
107
108
109
110
111
112
    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
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
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]);
    }
}

139
sub test_tbb {
boklm's avatar
boklm committed
140
    my ($tbbfile, $sha256sum) = @_;
141
    my $oldcwd = getcwd;
142
    $options->{tbbinfos} = tbb_filename_infos($tbbfile);
boklm's avatar
boklm committed
143
    return test_sha($tbbfile) if $options->{tbbinfos}{type} eq 'sha256sum';
boklm's avatar
boklm committed
144
145
    $options->{tbbinfos}{tmpdir} = File::Temp::newdir;
    $tbbfile = get_tbbfile($tbbfile);
boklm's avatar
boklm committed
146
147
148
    if ($sha256sum && $sha256sum ne sha256_hex(read_file($tbbfile))) {
        exit_error "Wrong sha256sum for $tbbfile";
    }
149
150
151
152
153
154
155
156
    $options->{tbbdir} = extract_tbb($tbbfile);
    chdir $options->{tbbdir} || exit_error "Can't enter directory $options->{tbbdir}";
    setup_tbb;
    print "tbbdir: $options->{tbbdir}\n";
    mozmill_tests;
    selenium_tests;
    chdir $oldcwd;
}
157

158
159
160
foreach my $tbbfile (@{$options->{files}}) {
    test_tbb($tbbfile);
}