forked from OpenKore/openkore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpReaderTest.pm
More file actions
122 lines (103 loc) · 3.17 KB
/
HttpReaderTest.pm
File metadata and controls
122 lines (103 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package HttpReaderTest;
use strict;
use Test::More;
use Utils::HttpReader;
use Time::HiRes qw(time sleep);
use constant SMALL_TEST_URL => "http://www.openkore.com/misc/testHttpReader.txt";
use constant SMALL_TEST_CONTENT => "Hello world!\n";
use constant SMALL_TEST_SIZE => 13;
use constant SMALL_TEST_CHECKSUM => 2773980202;
use constant ERROR_URL => "http://www.openkore.com/FileNotFound.txt";
use constant ERROR_URL2 => "https://sourceforge.net/fooooooooooo/";
use constant INVALID_URL => "http://111.111.111.111:82";
sub start {
print "### Starting HttpReaderTest\n";
StdHttpReader::init();
HttpReaderTest->new()->run();
}
################
sub new {
return bless {}, $_[0];
}
sub calcChecksum {
use bytes;
my ($data, $seed) = @_;
$seed = 0 if (!defined $seed);
my $max = 2 ** 32; # Warning: this assumes we're on a 32-bit system
for (my $i = 0; $i < length($data); $i++) {
$seed = ($seed * 32 + ord(substr($data, $i, 1))) % $max;
}
return $seed;
}
sub run {
my ($self) = @_;
$self->testMirrorSelection();
$self->testDownload();
$self->testFailedDownload();
}
sub testMirrorSelection {
use constant TIMEOUT => 3000;
my @urls = (ERROR_URL, INVALID_URL, SMALL_TEST_URL);
my $beginTime = time;
my $http = new MirrorHttpReader(\@urls, TIMEOUT);
while ($http->getStatus != HttpReader::DONE && $http->getStatus != HttpReader::ERROR) {
sleep 0.01;
}
# Note that this test isn't entirely reliable because
# it assumes that your network connection can connect
# to SMALL_TEST_URL within TIMEOUT miliseconds.
ok(time - $beginTime < TIMEOUT * scalar(@urls) + 1,
"Mirror selection timeout works properly");
is($http->getStatus, HttpReader::DONE,
"Status is HTTP_READER_DONE");
my $len;
my $data = $http->getData($len);
is(calcChecksum($data), SMALL_TEST_CHECKSUM,
"Downloaded data is correct");
}
sub testDownload {
my @urls = (SMALL_TEST_URL);
my $http = new MirrorHttpReader(\@urls);
while ($http->getStatus == HttpReader::CONNECTING) {
sleep 0.01;
}
isnt($http->getStatus, HttpReader::CONNECTING,
"Status is not HTTP_READER_CONNECTING");
my $done;
my $checksum = 0;
my $totalSize = 0;
while (!$done) {
my $buf;
my $ret = $http->pullData($buf, 2);
ok($ret == int($ret), "pullData() returns an integer");
ok($ret >= -2, "pullData() returns >= -2");
isnt($ret, -2, "pullData() never fails for valid test URL");
if ($ret == -1) {
# Try again
sleep 0.01;
} elsif ($ret > 0) {
# There is data
is(length($buf), $ret, "Size of buffer equals pullData() return value");
$checksum = calcChecksum($buf, $checksum);
$totalSize += $ret;
} else {
# $ret == 0: EOF
$done = 1;
}
}
is($http->getStatus, HttpReader::DONE, "Status is HTTP_READER_DONE");
is($checksum, SMALL_TEST_CHECKSUM, "Checksum is OK");
is($totalSize, SMALL_TEST_SIZE, "Size is OK");
}
sub testFailedDownload {
my @urls = (ERROR_URL2);
my $http = new MirrorHttpReader(\@urls, 3000);
while ($http->getStatus != HttpReader::DONE && $http->getStatus != HttpReader::ERROR) {
sleep 0.01;
}
is($http->getStatus, HttpReader::ERROR, "Status for ERROR_URL is HTTP_READER_ERROR");
my $buf;
my $ret = $http->pullData($buf, 1024);
is($ret, -2, "pullData() returns -2 for ERROR_URL");
}
1;