forked from OpenKore/openkore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp-reader-test.cpp
More file actions
415 lines (354 loc) · 11.1 KB
/
http-reader-test.cpp
File metadata and controls
415 lines (354 loc) · 11.1 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
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
179
180
181
182
183
184
185
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
216
217
218
219
220
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
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
/* HttpReader unit test program
* Copyright (C) 2006 Written by VCL
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#undef NDEBUG
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <time.h>
#include <list>
#ifdef WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#else
#include <unistd.h>
#define Sleep(miliseconds) usleep(miliseconds * 1000)
#endif
#include "std-http-reader.h"
#include "mirror-http-reader.h"
using namespace std;
using namespace OpenKore;
typedef HttpReader * (*HttpReaderCreator) (const char *url);
typedef HttpReader * (*HttpReaderPostCreator) (const char *url, const char *postData, int postDataSize);
#define SMALL_TEST_URL "http://www.openkore.com/test/HttpReader.txt"
#define SMALL_TEST_CONTENT "Hello world!\n"
#define SMALL_TEST_SIZE 13
#define SMALL_TEST_CHECKSUM 2773980202U
#define LARGE_TEST_URL "http://www.openkore.com:80/test/HttpReaderLarge.txt"
#define LARGE_TEST_SIZE 74048
#define LARGE_TEST_CHECKSUM 1690026430U
#define SLOW_TEST_URL "http://kambing.vlsm.org/gnu/gcc/gcc-4.1.0/gcc-core-4.1.0.tar.bz2"
#define ERROR_URL "http://www.openkore.com/test/FileNotFound.txt"
#define INVALID_URL "http://111.111.111.111:82/"
#define INVALID_URL2 "http://www.fooooooo.com"
#define SECURE_URL "https://sourceforge.net"
#define POST_TEST_URL "http://www.openkore.com/test/TestPost.php"
#define POST_TEST_DATA "mydata=hello+world"
static HttpReader *
createStdHttpReader(const char *url) {
return StdHttpReader::create(url);
}
static HttpReader *
createStdHttpReaderPost(const char *url, const char *postData, int postDataSize) {
return StdHttpReader::createAndPost(url, postData, postDataSize);
}
static HttpReader *
createMirrorHttpReader(const char *url) {
list<const char *> urls;
urls.push_back(url);
return new MirrorHttpReader(urls, 3000);
}
/**
* A class for testing a HttpReader implementation.
*/
class Tester {
public:
/**
* Create a new Tester object.
*
* @param creatorFunc A function which creates a HttpReader instance.
* @require creatorFunc != NULL
*/
Tester(HttpReaderCreator creatorFunc, HttpReaderPostCreator creatorPostFunc) {
this->createHttpReader = creatorFunc;
this->createHttpReaderPost = creatorPostFunc;
}
virtual ~Tester() {}
/** Run the unit tests. */
void
virtual run() {
printf("Testing status transitions (1)...\n");
assert( testStatusTransitions(SMALL_TEST_URL) );
printf("Testing status transitions (2)...\n");
assert( testStatusTransitions(LARGE_TEST_URL) );
printf("Testing status transitions (3)...\n");
assert( !testStatusTransitions(ERROR_URL) );
printf("Testing status transitions (4)...\n");
assert( testStatusTransitions(SECURE_URL) );
printf("Testing status transitions (5)...\n");
printf("Testing getData (1)...\n");
assert( testGetData(SMALL_TEST_URL, SMALL_TEST_CONTENT, SMALL_TEST_SIZE) );
printf("Testing getData (2)...\n");
assert( testGetData(LARGE_TEST_URL, NULL, LARGE_TEST_SIZE) );
printf("Testing getData (3)...\n");
assert( !testGetData(ERROR_URL, NULL, 0) );
printf("Testing getData (4)...\n");
assert( !testGetData(INVALID_URL2, NULL, 0) );
printf("Testing pullData (1)...\n");
assert( testPullData(SMALL_TEST_URL, SMALL_TEST_SIZE, SMALL_TEST_CHECKSUM) );
printf("Testing pullData (2)...\n");
assert( testPullData(LARGE_TEST_URL, LARGE_TEST_SIZE, LARGE_TEST_CHECKSUM) );
printf("Testing pullData (3)...\n");
assert( !testPullData(ERROR_URL, 0, 0) );
printf("Testing pullData (4)...\n");
assert( !testPullData(INVALID_URL2, 0, 0) );
if (createHttpReaderPost != NULL) {
printf("Testing POST...\n");
testPOST(POST_TEST_URL, POST_TEST_DATA);
}
printf("Testing cancellation while connecting (1)...\n");
testConnectCancellation(INVALID_URL);
printf("Testing cancellation while connecting (2)...\n");
testConnectCancellation(INVALID_URL2);
printf("Testing cancellation while downloading...\n");
testDownloadCancellation(SLOW_TEST_URL);
}
protected:
/**
* Calculate a simple checksum of the specified data.
*/
unsigned int
calcChecksum(const char *data, unsigned int len, unsigned int seed = 0) {
for (unsigned int i = 0; i < len; i++) {
seed = seed * 32 + data[i];
}
return seed;
}
private:
HttpReaderCreator createHttpReader;
HttpReaderPostCreator createHttpReaderPost;
protected:
// Test whether status transitions behave as documented.
bool
testStatusTransitions(const char *url) {
HttpReader *http = createHttpReader(url);
HttpReaderStatus status = HTTP_READER_CONNECTING;
HttpReaderStatus oldStatus;
do {
oldStatus = status;
status = http->getStatus();
switch (oldStatus) {
case HTTP_READER_CONNECTING:
assert(status == HTTP_READER_CONNECTING
|| status == HTTP_READER_DOWNLOADING
|| status == HTTP_READER_DONE
|| status == HTTP_READER_ERROR);
break;
case HTTP_READER_DOWNLOADING:
assert(status == HTTP_READER_DOWNLOADING
|| status == HTTP_READER_DONE
|| status == HTTP_READER_ERROR);
break;
case HTTP_READER_DONE:
assert(status == HTTP_READER_DONE);
break;
case HTTP_READER_ERROR:
assert(status == HTTP_READER_ERROR);
break;
default:
printf("Unknown status %d\n", (int) status);
abort();
break;
};
Sleep(10);
} while (status != HTTP_READER_DONE && status != HTTP_READER_ERROR);
Sleep(1000);
if (status == HTTP_READER_DONE) {
assert(http->getStatus() == HTTP_READER_DONE);
} else {
assert(http->getStatus() == HTTP_READER_ERROR);
assert(http->getSize() == -2);
}
delete http;
return status == HTTP_READER_DONE;
}
// Test whether getData() works
bool
testGetData(const char *url, const char *content, unsigned int size) {
HttpReader *http = createHttpReader(url);
while (http->getStatus() != HTTP_READER_DONE
&& http->getStatus() != HTTP_READER_ERROR) {
Sleep(10);
}
if (http->getStatus() != HTTP_READER_DONE) {
assert(http->getSize() == -2);
delete http;
return false;
}
unsigned int downloadedLen = 0;
const char *downloadedData = http->getData(downloadedLen);
assert(downloadedLen == size);
assert(http->getSize() == (int) size);
if (content != NULL) {
assert(strcmp(downloadedData, content) == 0);
}
delete http;
return true;
}
// Test whether pullData() works
bool
testPullData(const char *url, unsigned int expectedSize, unsigned int expectedChecksum) {
HttpReader *http = createHttpReader(url);
bool result;
unsigned int checksum = 0;
unsigned int size = 0;
char buffer[1024];
int ret;
bool done = false;
while (http->getStatus() == HTTP_READER_CONNECTING) {
Sleep(10);
}
while (!done) {
ret = http->pullData(buffer, sizeof(buffer));
if (ret == -1) {
Sleep(10);
} else if (ret > 0) {
checksum = calcChecksum(buffer, ret, checksum);
size += ret;
} else if (ret == -2 || ret == 0) {
done = true;
} else {
printf("pullData() returned an invalid value: %d\n", ret);
abort();
}
}
result = http->getStatus() == HTTP_READER_DONE;
if (result) {
assert(expectedSize == size);
assert(expectedChecksum == checksum);
} else {
assert(http->getSize() == -2);
}
delete http;
return result;
}
// Test whether cancellation while connecting works.
void
testConnectCancellation(const char *url) {
HttpReader *http = createHttpReader(url);
time_t time1, time2;
Sleep(1000);
assert(http->getStatus() == HTTP_READER_CONNECTING
|| http->getStatus() == HTTP_READER_ERROR);
time1 = time(NULL);
delete http;
time2 = time(NULL);
// Verify that cancellation doesn't take more than 2 seconds
assert(time1 + 2 > time2);
}
// Test whether cancellation while downloading works.
// You must pass an URL to a large file so that download
// takes a while to complete.
void
testDownloadCancellation(const char *url) {
HttpReader *http = createHttpReader(url);
time_t time1, time2;
while (http->getStatus() == HTTP_READER_CONNECTING) {
Sleep(10);
}
assert(http->getStatus() == HTTP_READER_DOWNLOADING);
Sleep(1000);
time1 = time(NULL);
delete http;
time2 = time(NULL);
assert(time1 + 2 > time2);
}
void testPOST(const char *url, const char *data) {
HttpReader *http = createHttpReaderPost(url, data, -1);
while (http->getStatus() != HTTP_READER_DONE
&& http->getStatus() != HTTP_READER_ERROR) {
Sleep(10);
}
assert(http->getStatus() == HTTP_READER_DONE);
unsigned int downloadedLen = 0;
const char *downloadedData = http->getData(downloadedLen);
assert(strcmp(downloadedData, "yes") == 0);
delete http;
}
};
/**
* A class for testing MirrorHttpReader.
*/
class MirrorTester: public Tester {
public:
MirrorTester() : Tester(createMirrorHttpReader, NULL) {
}
virtual void
run() {
list<const char *> urls;
Tester::run();
printf("Testing usage of multiple mirrors (1)...\n");
urls.push_back(INVALID_URL);
urls.push_back(ERROR_URL);
urls.push_back(LARGE_TEST_URL);
urls.push_back(SECURE_URL); // Will never be used
assert( testMirrors(urls, LARGE_TEST_SIZE, LARGE_TEST_CHECKSUM) );
printf("Testing usage of multiple mirrors (2)...\n");
urls.clear();
urls.push_back(INVALID_URL);
urls.push_back(ERROR_URL);
urls.push_back("http://www.gnome.org:90");
assert( !testMirrors(urls, 0, 0) );
printf("Testing usage of multiple mirrors (3)...\n");
urls.clear();
urls.push_back(SECURE_URL);
urls.push_back(INVALID_URL); // Never used
urls.push_back(ERROR_URL); // ditto
assert( testMirrors(urls, 0, 0) );
printf("Testing getData (5)...\n");
assert( !testGetData(INVALID_URL, NULL, 0) );
printf("Testing pullData (5)...\n");
assert( !testPullData(INVALID_URL, 0, 0) );
}
private:
bool
testMirrors(const list<const char *> &urls, unsigned int expectedSize,
unsigned int expectedChecksum) {
HttpReader *http = new MirrorHttpReader(urls, 3000);
HttpReaderStatus status;
status = http->getStatus();
while (status != HTTP_READER_DONE && status != HTTP_READER_ERROR) {
Sleep(10);
status = http->getStatus();
}
if (status == HTTP_READER_DONE && expectedChecksum != 0) {
unsigned int len, checksum;
const char *data;
data = http->getData(len);
assert(len == expectedSize);
checksum = calcChecksum(data, len);
assert(checksum == expectedChecksum);
}
delete http;
return status == HTTP_READER_DONE;
}
};
int
main() {
StdHttpReader::init();
Tester *tester;
printf("### StdHttpReader\n");
tester = new Tester(createStdHttpReader, createStdHttpReaderPost);
tester->run();
delete tester;
printf("### MirrorHttpReader\n");
tester = new MirrorTester();
tester->run();
delete tester;
return 0;
}