OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include <string.h> | |
6 #include <algorithm> | |
7 #include <vector> | |
8 | |
9 #include "base/base64.h" | |
10 #include "base/md5.h" | |
11 | |
12 #include "components/data_reduction_proxy/browser/data_reduction_proxy_tamper_de tect.h" | |
13 #include "testing/gtest/include/gtest/gtest.h" | |
14 | |
15 std::string GetEncoded(std::string input) { | |
16 base::MD5Context context; | |
17 base::MD5Init(&context); | |
18 base::MD5Update(&context, input); | |
19 base::MD5Digest new_digest; | |
20 base::MD5Final(&new_digest, &context); | |
21 | |
22 std::string temp = std::string((char*)new_digest.a, 16); | |
23 std::string ret; | |
24 base::Base64Encode(temp, &ret); | |
25 return ret; | |
26 } | |
27 | |
28 namespace { | |
29 | |
30 void HeadersToRaw(std::string* headers) { | |
31 std::replace(headers->begin(), headers->end(), '\n', '\0'); | |
32 if (!headers->empty()) | |
33 *headers += '\0'; | |
34 } | |
35 | |
36 struct TestCase { | |
37 const char* raw_header; | |
38 std::string received_fingerprint; | |
39 bool expected_result; | |
bolian
2014/06/25 18:55:56
What does the expected_result mean? Tampered or no
| |
40 }; | |
41 | |
42 struct TestCaseContentLength { | |
43 const char* raw_header; | |
44 std::string received_fingerprint; | |
45 bool expected_result; | |
46 std::string mime_type; | |
47 }; | |
48 | |
49 class DataReductionProxyTamperDetectTest : public testing::Test { | |
50 }; | |
51 | |
52 void TestFingerprintCommon(const TestCase& test, int fingerprintNumber) { | |
53 std::string raw_headers(test.raw_header); | |
54 HeadersToRaw(&raw_headers); | |
55 scoped_refptr<net::HttpResponseHeaders> headers( | |
56 new net::HttpResponseHeaders(raw_headers)); | |
57 | |
58 typedef bool (*CheckHeader)(const std::string, | |
59 const net::HttpResponseHeaders*); | |
60 | |
61 CheckHeader checkFuncs[] = {&data_reduction_proxy::CheckHeaderChromeProxy, | |
62 &data_reduction_proxy::CheckHeaderVia, | |
63 &data_reduction_proxy::CheckHeaderOtherHeaders}; | |
64 | |
65 EXPECT_EQ(test.expected_result, checkFuncs[fingerprintNumber]( | |
66 test.received_fingerprint, headers)); | |
67 } | |
68 | |
69 void TestFingerprintContentLength(const TestCaseContentLength& test) { | |
70 std::string raw_headers(test.raw_header); | |
71 HeadersToRaw(&raw_headers); | |
72 scoped_refptr<net::HttpResponseHeaders> headers( | |
73 new net::HttpResponseHeaders(raw_headers)); | |
74 | |
75 std::string mime_type; | |
76 bool equal = data_reduction_proxy::CheckHeaderContentLength( | |
77 test.received_fingerprint, headers, &mime_type); | |
78 EXPECT_EQ(test.expected_result, equal); | |
79 if (!equal) | |
80 EXPECT_EQ(mime_type, test.mime_type); | |
81 } | |
82 | |
83 void TestParsingCommon(const std::string fp) { | |
84 std::string raw_headers(fp); | |
85 HeadersToRaw(&raw_headers); | |
86 scoped_refptr<net::HttpResponseHeaders> headers( | |
87 new net::HttpResponseHeaders(raw_headers)); | |
88 | |
89 data_reduction_proxy::CheckResponseFingerprint(headers, true); | |
90 } | |
91 | |
92 TEST(DataReductionProxyTamperDetectTest, ChromeProxy) { | |
93 TestCase test[] = { | |
94 // check sorting values and decoding | |
95 { | |
96 "HTTP/1.1 202 Accepted \n" | |
97 "Chrome-Proxy: aut=aauutthh,fp=123,bbbypas=0,aaxxx=xxx,bbbloc=1\n", | |
98 | |
99 "aaxxx=xxx,aut=aauutthh,bbbloc=1,bbbypas=0,", | |
bolian
2014/06/25 18:55:56
Can you address my previous comment about why not
xingx
2014/06/25 20:55:43
Right now the fp value is manually input, then cal
| |
100 true, | |
101 }, | |
102 | |
103 // check sorting | |
104 { | |
105 "HTTP/1.1 202 Accepted \n" | |
106 "Chrome-Proxy: a,b,c,d,e,3,2,1,fp=1231\n", | |
107 | |
108 "1,2,3,a,b,c,d,e,", | |
109 true, | |
110 }, | |
111 | |
112 // check no Chrome-Proxy header case (should not happen) | |
113 { | |
114 "HTTP/1.1 202 Accepted \n" | |
115 "Content-Length: 12345\n", | |
116 | |
117 "", | |
118 true, | |
119 }, | |
120 | |
121 // check empty Chrome-Proxy header case (should not happen) | |
122 { | |
123 "HTTP/1.1 202 Accepted \n" | |
124 "Chrome-Proxy: \n", | |
125 | |
126 ",", | |
127 true, | |
128 }, | |
129 | |
130 // check empty Chrome-Proxy header case | |
131 { | |
132 "HTTP/1.1 202 Accepted \n" | |
133 "Chrome-Proxy: fp=xyz\n", | |
134 | |
135 "", | |
136 true, | |
137 }, | |
138 | |
139 // check empty Chrome-Proxy header case, with extra "," | |
140 { | |
141 "HTTP/1.1 202 Accepted \n" | |
142 "Chrome-Proxy: fp=abcde , \n", | |
143 | |
144 "", | |
145 true, | |
146 }, | |
147 | |
148 // check empty Chrome-Proxy header, different fingerprint | |
149 { | |
150 "HTTP/1.1 202 Accepted \n" | |
151 "Chrome-Proxy: fp=xyz\n", | |
152 | |
153 ",", | |
154 false, | |
155 }, | |
156 | |
157 // check regular Chrome-Proxy header, different fingerprint | |
158 { | |
159 "HTTP/1.1 202 Accepted \n" | |
160 "Chrome-Proxy: aut=aauutthh,bbbypas=2,aaxxx=xxx,bbbloc=1\n", | |
161 | |
162 "aaxxx=xxx,aut=aauutthh,bbbloc=1,bbbypas=0,", | |
163 false, | |
164 }, | |
165 | |
166 // check regular Chrome-Proxy header, different fingerprint | |
167 { | |
168 "HTTP/1.1 202 Accepted \n" | |
169 "Chrome-Proxy: a,aut=aauutthh,bbbypas=0,aaxxx=xxx,bbbloc=1\n", | |
170 | |
171 "aaxxx=xxx,aut=aauutthh,bbbloc=1,bbbypas=0,", | |
172 false, | |
173 }, | |
174 | |
175 // check regular Chrome-Proxy header, with extra " " | |
176 { | |
177 "HTTP/1.1 202 Accepted \n" | |
178 "Chrome-Proxy: aut=aauutthh , bbbypas=0 , aaxxx=xxx" | |
179 " ,bbbloc=1 \n", | |
180 | |
181 "aaxxx=xxx,aut=aauutthh,bbbloc=1,bbbypas=0,", | |
182 true | |
183 }, | |
184 | |
185 // check regular Chrome-Proxy header, with extra lines and " " | |
186 { | |
187 "HTTP/1.1 202 Accepted \n" | |
188 "Chrome-Proxy: aut=aauutthh , bbbypas=0 , bbbloc=1 \n" | |
189 "Chrome-Proxy: aaxxx=xxx \n", | |
190 | |
191 "aaxxx=xxx,aut=aauutthh,bbbloc=1,bbbypas=0,", | |
192 true | |
193 }, | |
194 | |
195 // check Chrome-Proxy header with multiple lines | |
196 { | |
197 "HTTP/1.1 202 Accepted \n" | |
198 "Chrome-Proxy: aaxxx=xxx \n" | |
199 "Chrome-Proxy: aut=aauutthh\n" | |
200 "Chrome-Proxy: bbbypas=0\n" | |
201 "Chrome-Proxy:bbbloc=1 \n", | |
202 | |
203 "aaxxx=xxx,aut=aauutthh,bbbloc=1,bbbypas=0,", | |
204 true | |
205 }, | |
206 | |
207 // check Chrome-Proxy header with multiple lines, at different position | |
208 // of the entire header | |
209 { | |
210 "HTTP/1.1 202 Accepted \n" | |
211 "Chrome-Proxy: aaxxx=xxx \n" | |
212 "Chrome-Proxy: aut=aauutthh\n" | |
213 "Content-Type: 1\n" | |
214 "Cache-Control: 2\n" | |
215 "ETag: 3\n" | |
216 "Chrome-Proxy: bbbypas=0\n" | |
217 "Connection: 4\n" | |
218 "Expires: 5\n" | |
219 "Chrome-Proxy: bbbloc=1\n" | |
220 "Via: \n" | |
221 "Content-Length: 12345\n", | |
222 | |
223 "aaxxx=xxx,aut=aauutthh,bbbloc=1,bbbypas=0,", | |
224 true | |
225 }, | |
226 | |
227 // check Chrome-Proxy header with multiple same values | |
228 { | |
229 "HTTP/1.1 202 Accepted \n" | |
230 "Chrome-Proxy: aaxxx=xxx \n" | |
231 "Chrome-Proxy: aut=aauutthh\n" | |
232 "Content-Type: 1\n" | |
233 "Cache-Control: 2\n" | |
234 "ETag: 3\n" | |
235 "Chrome-Proxy: bbbypas=0\n" | |
236 "Connection: 4\n" | |
237 "Expires: 5\n" | |
238 "Chrome-Proxy: bbbloc=1, fp=123 \n" | |
239 "Chrome-Proxy: aaxxx=xxx \n" | |
240 "Via: \n" | |
241 "Content-Length: 12345\n", | |
242 | |
243 "aaxxx=xxx,aaxxx=xxx,aut=aauutthh,bbbloc=1,bbbypas=0,", | |
244 true | |
245 }, | |
246 | |
247 // check Chrome-Proxy header with multiple same values, | |
248 // but different fingerprint | |
249 { | |
250 "HTTP/1.1 202 Accepted \n" | |
251 "Chrome-Proxy: aaxxx=xxx \n" | |
252 "Chrome-Proxy: aaxxx=xxx \n" | |
253 "Chrome-Proxy: aut=aauutthh\n" | |
254 "Content-Type: 1\n" | |
255 "Cache-Control: 2\n" | |
256 "ETag: 3\n" | |
257 "Chrome-Proxy: bbbypas=0\n" | |
258 "Connection: 4\n" | |
259 "Expires: 5\n" | |
260 "Chrome-Proxy: bbbloc=1\n" | |
261 "Chrome-Proxy: aaxxx=xxx \n" | |
262 "Via: \n" | |
263 "Content-Length: 12345\n", | |
264 | |
265 "aaxxx=xxx,aaxxx=xxx,aut=aauutthh,bbbloc=1,bbbypas=0,", | |
266 false, | |
267 }, | |
268 | |
269 // check Chrome-Proxy header with multiple lines, | |
270 // but different fingerprint | |
271 { | |
272 "HTTP/1.1 202 Accepted \n" | |
273 "Content-Type: 1\n" | |
274 "Cache-Control: 2\n" | |
275 "ETag: 3\n" | |
276 "Chrome-Proxy: bbbypas=0\n" | |
277 "Connection: 4\n" | |
278 "Expires: 5\n" | |
279 "Chrome-Proxy: bbbloc=1\n" | |
280 "Chrome-Proxy: aaxxx=xxx \n" | |
281 "Via: \n" | |
282 "Content-Length: 12345\n", | |
283 | |
284 "aaxxx=xxx,aaxxx=xxx,aut=aauutthh,bbbloc=1,bbbypas=0,", | |
285 false, | |
286 }, | |
287 | |
288 // check regular Chrome-Proxy header, but received fingerprint is empty | |
289 { | |
290 "HTTP/1.1 202 Accepted \n" | |
291 "Chrome-Proxy: aaxxx=xxx \n" | |
292 "Chrome-Proxy: aaxxx=xxx \n" | |
293 "Chrome-Proxy: aut=aauutthh\n" | |
294 "Content-Type: 1\n" | |
295 "Cache-Control: 2\n" | |
296 "ETag: 3\n" | |
297 "Chrome-Proxy: bbbypas=0\n" | |
298 "Connection: 4\n" | |
299 "Expires: 5\n" | |
300 "Chrome-Proxy: bbbloc=1\n" | |
301 "Chrome-Proxy: aaxxx=xxx \n" | |
302 "Via: \n" | |
303 "Content-Length: 12345\n", | |
304 | |
305 "", | |
306 false, | |
307 }, | |
308 | |
309 }; | |
310 | |
311 for (size_t i=0; i<ARRAYSIZE_UNSAFE(test); ++i) { | |
312 test[i].received_fingerprint = | |
313 GetEncoded(test[i].received_fingerprint); | |
314 TestFingerprintCommon(test[i], 0); | |
315 LOG(WARNING) << "f1 " <<i; | |
316 } | |
317 } | |
318 | |
319 TEST(DataReductionProxyTamperDetectTest, Via) { | |
320 TestCase test[] = { | |
321 // check regular case, where Chrome Proxy occurs at the last | |
322 { | |
323 "HTTP/1.1 202 Accepted \n" | |
324 "Via: a, b, c, Chrome Proxy\n", | |
325 | |
326 "0", | |
327 true | |
328 }, | |
329 | |
330 // check when there is extra middlebox | |
331 // between data-reduction-proxy and phone | |
332 { | |
333 "HTTP/1.1 202 Accepted \n" | |
334 "Via: a, b, c, Chrome Proxy, xyz\n", | |
335 | |
336 "0", | |
337 false, | |
338 }, | |
339 | |
340 // emtpy Via header, even no Chrome Proxy tag | |
341 { | |
342 "HTTP/1.1 202 Accepted \n" | |
343 "Via: \n", | |
344 | |
345 "0", | |
346 true, | |
347 }, | |
348 | |
349 // only Chrome Proxy tag occurs in Via header | |
350 { | |
351 "HTTP/1.1 202 Accepted \n" | |
352 "Via: Chrome \n", | |
353 | |
354 "0", | |
355 true | |
356 }, | |
357 | |
358 // there are " ", i.e., empty value after Chrome Proxy tag | |
359 // should not count as extra middleboxes | |
360 { | |
361 "HTTP/1.1 202 Accepted \n" | |
362 "Via: Chrome , , \n", | |
363 | |
364 "0", | |
365 true | |
366 }, | |
367 | |
368 // special case when there is no Via header | |
369 { | |
370 "HTTP/1.1 202 Accepted \n", | |
371 | |
372 "0", | |
373 true | |
374 }, | |
375 }; | |
376 | |
377 for (size_t i=0; i<ARRAYSIZE_UNSAFE(test); ++i) { | |
378 TestFingerprintCommon(test[i], 1); | |
379 LOG(WARNING) << "Xing "<<i; | |
380 } | |
381 } | |
382 | |
383 TEST(DataReductionProxyTamperDetectTest, OtherHeaders) { | |
384 TestCase test[] = { | |
385 // regular case, with correct fingerprint | |
386 { | |
387 "HTTP/1.1 202 Accepted \n" | |
388 "Chrome-Proxy: aut=aauutthh,bbbypas=0,aaxxx=xxx,bbbloc=1\n" | |
389 "Content-Type: 1\n" | |
390 "Cache-Control: 2\n" | |
391 "ETag: 3\n" | |
392 "Connection: 4\n" | |
393 "Expires: 5\n" | |
394 "Via: \n" | |
395 "Content-Length: 12345\n", | |
396 | |
397 "1,;2,;3,;4,;5,;:content-type:cache-control:etag:connection:expires", | |
398 true | |
399 }, | |
400 | |
401 // regular case, with correct fingerprint | |
402 { | |
403 "HTTP/1.1 202 Accepted \n" | |
404 "Chrome-Proxy: aut=aauutthh,bbbypas=0,aaxxx=xxx,bbbloc=1\n" | |
405 "Content-Type: aaa1\n" | |
406 "Cache-Control: aaa2\n" | |
407 "ETag: aaa3\n" | |
408 "Connection: aaa4\n" | |
409 "Expires: aaa5\n" | |
410 "Via: \n" | |
411 "Content-Length: 12345\n", | |
412 | |
413 "aaa1,;aaa2,;aaa3,;aaa4,;aaa5,;:content-type:cache-control:" | |
414 "etag:connection:expires", | |
415 true | |
416 }, | |
417 | |
418 // regular case, one header is with multiple values | |
419 { | |
420 "HTTP/1.1 202 Accepted \n" | |
421 "Chrome-Proxy: aut=aauutthh,bbbypas=0,aaxxx=xxx,bbbloc=1\n" | |
422 "Content-Type: aaa1, bbb1, ccc1\n" | |
423 "Cache-Control: aaa2\n" | |
424 "ETag: aaa3\n" | |
425 "Connection: aaa4\n" | |
426 "Expires: aaa5\n" | |
427 "Via: \n" | |
428 "Content-Length: 12345\n", | |
429 | |
430 "aaa1,bbb1,ccc1,;aaa2,;aaa3,;aaa4,;aaa5,;:" | |
431 "content-type:cache-control:etag:connection:expires", | |
432 true | |
433 }, | |
434 | |
435 // regular case, one header has multiple lines | |
436 { | |
437 "HTTP/1.1 202 Accepted \n" | |
438 "Chrome-Proxy: aut=aauutthh,bbbypas=0,aaxxx=xxx,bbbloc=1\n" | |
439 "Content-Type: aaa1, ccc1\n" | |
440 "Content-Type: xxx1, bbb1, ccc1\n" | |
441 "Cache-Control: aaa2\n" | |
442 "ETag: aaa3\n" | |
443 "Connection: aaa4\n" | |
444 "Expires: aaa5\n" | |
445 "Via: \n" | |
446 "Content-Length: 12345\n", | |
447 | |
448 "aaa1,bbb1,ccc1,ccc1,xxx1,;aaa2,;aaa3,;aaa4,;aaa5,;:" | |
449 "content-type:cache-control:etag:connection:expires", | |
450 true | |
451 }, | |
452 | |
453 // regular case, one header has multiple lines, | |
454 // and such multiple lines occur at different positions | |
455 { | |
456 "HTTP/1.1 202 Accepted \n" | |
457 "Chrome-Proxy: aut=aauutthh,bbbypas=0,aaxxx=xxx,bbbloc=1\n" | |
458 "Content-Type: aaa1, ccc1\n" | |
459 "Cache-Control: aaa2\n" | |
460 "ETag: aaa3\n" | |
461 "Content-Type: xxx1, bbb1, ccc1\n" | |
462 "Connection: aaa4\n" | |
463 "Expires: aaa5\n" | |
464 "Via: \n" | |
465 "Content-Length: 12345\n", | |
466 | |
467 "aaa1,bbb1,ccc1,ccc1,xxx1,;aaa2,;aaa3,;aaa4,;aaa5,;" | |
468 ":content-type:cache-control:etag:connection:expires", | |
469 true | |
470 }, | |
471 | |
472 // regular case, more than one header have multiple lines, | |
473 // and such multiple lines occur at different positions | |
474 { | |
475 "HTTP/1.1 202 Accepted \n" | |
476 "Chrome-Proxy: aut=aauutthh,bbbypas=0,aaxxx=xxx,bbbloc=1\n" | |
477 "Content-Type: aaa1, ccc1\n" | |
478 "Cache-Control: ccc2 , bbb2\n" | |
479 "ETag: aaa3\n" | |
480 "Content-Type: xxx1, bbb1, ccc1\n" | |
481 "Connection: aaa4\n" | |
482 "Cache-Control: aaa2 \n" | |
483 "Expires: aaa5\n" | |
484 "Via: \n" | |
485 "Content-Length: 12345\n", | |
486 | |
487 "aaa1,bbb1,ccc1,ccc1,xxx1,;aaa2,bbb2,ccc2,;aaa3,;aaa4,;aaa5,;:" | |
488 "content-type:cache-control:etag:connection:expires", | |
489 true | |
490 }, | |
491 | |
492 // regular case, response header does not have one header we need | |
493 // for fingerprint (expires) | |
494 { | |
495 "HTTP/1.1 202 Accepted \n" | |
496 "Chrome-Proxy: aut=aauutthh,bbbypas=0,aaxxx=xxx,bbbloc=1\n" | |
497 "Content-Type: aaa1, ccc1\n" | |
498 "Cache-Control: ccc2 , bbb2\n" | |
499 "ETag: aaa3\n" | |
500 "Content-Type: xxx1, bbb1, ccc1\n" | |
501 "Connection: aaa4\n" | |
502 "Cache-Control: aaa2 \n" | |
503 "Via: \n" | |
504 "Content-Length: 12345\n", | |
505 | |
506 "aaa1,bbb1,ccc1,ccc1,xxx1,;aaa2,bbb2,ccc2,;aaa3,;aaa4,;;:" | |
507 "content-type:cache-control:etag:connection:expires", | |
508 true | |
509 }, | |
510 | |
511 // regular case, response header does not have more than one header | |
512 // we need for fingerprint (content-type, expires) | |
513 { | |
514 "HTTP/1.1 202 Accepted \n" | |
515 "Chrome-Proxy: aut=aauutthh,bbbypas=0,aaxxx=xxx,bbbloc=1\n" | |
516 "Cache-Control: ccc2 , bbb2\n" | |
517 "ETag: aaa3\n" | |
518 "Connection: aaa4\n" | |
519 "Cache-Control: aaa2 \n" | |
520 "Via: \n" | |
521 "Content-Length: 12345\n", | |
522 | |
523 ";aaa2,bbb2,ccc2,;aaa3,;aaa4,;;:content-type:cache-control:" | |
524 "etag:connection:expires", | |
525 true | |
526 }, | |
527 | |
528 // regular case, all the headers we need for fingerprint are missing | |
529 { | |
530 "HTTP/1.1 202 Accepted \n" | |
531 "Chrome-Proxy: aut=aauutthh,bbbypas=0,aaxxx=xxx,bbbloc=1\n" | |
532 "Via: \n" | |
533 "Content-Length: 12345\n", | |
534 | |
535 ";;;;;:content-type:cache-control:etag:connection:expires", | |
536 true | |
537 }, | |
538 | |
539 // regular case, but differ to received fingerprint | |
540 { | |
541 "HTTP/1.1 202 Accepted \n" | |
542 "Chrome-Proxy: aut=aauutthh,bbbypas=0,aaxxx=xxx,bbbloc=1\n" | |
543 "Content-Type: aaa1, ccc1\n" | |
544 "Cache-Control: ccc2 , bbb2\n" | |
545 "ETag: etag\n" | |
546 "Content-Type: xxx1, bbb1, ccc1\n" | |
547 "Connection: aaa4\n" | |
548 "Cache-Control: aaa2 \n" | |
549 "Via: \n" | |
550 "Content-Length: 12345\n", | |
551 | |
552 "aaa1,bbb1,ccc1,ccc1,xxx1,;aaa2,bbb2,ccc2,;aaa3,;aaa4,;;:" | |
553 "content-type:cache-control:etag:connection:expires", | |
554 false, | |
555 }, | |
556 | |
557 // special case, headers are not missing but some of them are empty | |
558 { | |
559 "HTTP/1.1 202 Accepted \n" | |
560 "Content-Type: \n" | |
561 "Cache-Control: \n" | |
562 "ETag: \n" | |
563 "Connection: \n" | |
564 "Expires: 5\n" | |
565 "Via: \n" | |
566 "Content-Length: 12345\n", | |
567 | |
568 ",;,;,;,;5,;:content-type:cache-control:etag:connection:expires", | |
569 true | |
570 }, | |
571 | |
572 // special case, some headers do not exist, some are of empty value. | |
573 // check delimiter "," and ";" work correctly. | |
574 { | |
575 "HTTP/1.1 202 Accepted \n" | |
576 "Cache-Control: \n" | |
577 "Connection: \n" | |
578 "Expires: 5\n" | |
579 "Via: \n" | |
580 "Content-Length: 12345\n", | |
581 | |
582 ";,;;,;5,;:content-type:cache-control:etag:connection:expires", | |
583 true | |
584 }, | |
585 | |
586 // special case, check if we don't check any header, i.e., | |
587 // header list is empty | |
588 { | |
589 "HTTP/1.1 202 Accepted \n" | |
590 "Chrome-Proxy: aut=aauutthh,bbbypas=0,aaxxx=xxx,bbbloc=1\n" | |
591 "Content-Type: 1\n" | |
592 "Cache-Control: 2\n" | |
593 "ETag: 3\n" | |
594 "Connection: 4\n" | |
595 "Expires: 5\n" | |
596 "Via: \n" | |
597 "Content-Length: 12345\n", | |
598 | |
599 "", | |
600 true | |
601 }, | |
602 | |
603 // special case, we only want to check one header, which does not | |
604 // exist in received header | |
605 { | |
606 "HTTP/1.1 202 Accepted \n" | |
607 "Chrome-Proxy: aut=aauutthh,bbbypas=0,aaxxx=xxx,bbbloc=1\n" | |
608 "Content-Type: 1\n" | |
609 "Cache-Control: 2\n" | |
610 "ETag: 3\n" | |
611 "Connection: 4\n" | |
612 "Expires: 5\n" | |
613 "Via: \n" | |
614 "Content-Length: 12345\n", | |
615 | |
616 ";:non_exist_header", | |
617 true | |
618 }, | |
619 | |
620 // there is only one header in our header list | |
621 { | |
622 "HTTP/1.1 202 Accepted \n" | |
623 "Cache-Control: \n" | |
624 "Connection: \n" | |
625 "Expires: 5\n" | |
626 "Via: \n" | |
627 "Content-Length: 12345\n", | |
628 | |
629 ";:content-type", | |
630 true | |
631 }, | |
632 | |
633 // special case, if base64 decoding fails | |
634 { | |
635 "HTTP/1.1 202 Accepted \n" | |
636 "Cache-Control: \n" | |
637 "Connection: \n" | |
638 "Expires: 5\n" | |
639 "Via: \n" | |
640 "Content-Length: 12345\n", | |
641 | |
642 ";:content-type", | |
643 true | |
644 }, | |
645 | |
646 // special case, if base64 decoding fails | |
647 { | |
648 "HTTP/1.1 202 Accepted \n" | |
649 "Cache-Control: \n" | |
650 "Connection: \n" | |
651 "Expires: 5\n" | |
652 "Via: \n" | |
653 "Content-Length: 12345\n", | |
654 | |
655 "abcde:content-type:cache-control:etag:connection:expires", | |
656 true | |
657 }, | |
658 }; | |
659 | |
660 for (size_t i=0; i<ARRAYSIZE_UNSAFE(test); ++i) { | |
661 if (i >= ARRAYSIZE_UNSAFE(test) - 2) | |
662 { | |
663 TestFingerprintCommon(test[i], 2); | |
664 continue; | |
665 } | |
666 size_t delimiter_pos = test[i].received_fingerprint.find(":"); | |
667 std::string hash, rest; | |
668 if (delimiter_pos == std::string::npos) | |
669 { | |
670 delimiter_pos = test[i].received_fingerprint.size(); | |
671 rest = ""; | |
672 } | |
673 else | |
674 rest = test[i].received_fingerprint.substr( | |
675 delimiter_pos, | |
676 test[i].received_fingerprint.size() - delimiter_pos); | |
677 hash = test[i].received_fingerprint.substr(0, delimiter_pos); | |
678 test[i].received_fingerprint = GetEncoded(hash) + rest; | |
679 | |
680 TestFingerprintCommon(test[i], 2); | |
681 } | |
682 } | |
683 | |
684 TEST(DataReductionProxyTamperDetectTest, ContentLength) { | |
685 TestCaseContentLength test[] = { | |
686 // regular case, content-length is the same | |
687 { | |
688 "HTTP/1.1 202 Accepted \n" | |
689 "Content-Type: 1\n" | |
690 "Content-Length: 12345\n", | |
691 | |
692 "12345", | |
693 true, | |
694 "1" | |
695 }, | |
696 | |
697 // regular case, content-length is not the same | |
698 // also check if retrieved content-type is correct | |
699 { | |
700 "HTTP/1.1 202 Accepted \n" | |
701 "Content-Type: text/html; charset=ISO-8859-4\n" | |
702 "Content-Length: 12345\n", | |
703 | |
704 "125", | |
705 false, | |
706 "text/html" | |
707 }, | |
708 | |
709 // special case, data reduction proxy does not sent content-length | |
710 // i.e., content-length at data reduction proxy side is missing | |
711 { | |
712 "HTTP/1.1 202 Accepted \n" | |
713 "Content-Type: text/javascript\n" | |
714 "Content-Length: 12345\n", | |
715 | |
716 "", | |
717 true, | |
718 "text/javascript" | |
719 }, | |
720 | |
721 // special case, content-length are missing at both end | |
722 // i.e., both data reduction proxy and chrome | |
723 { | |
724 "HTTP/1.1 202 Accepted \n" | |
725 "Content-Type: 1\n", | |
726 | |
727 "", | |
728 true, | |
729 "1" | |
730 }, | |
731 | |
732 // special case, check when content-length is 0 | |
733 { | |
734 "HTTP/1.1 202 Accepted \n" | |
735 "Content-Type: application/x-javascript\n" | |
736 "Content-Length: 0\n", | |
737 | |
738 "0", | |
739 true, | |
740 "application/x-javascript" | |
741 }, | |
742 | |
743 // special case, check when data reduction proxy side's | |
744 // content-length is empty (header exist, but value is empty) | |
745 { | |
746 "HTTP/1.1 202 Accepted \n" | |
747 "Content-Type: application/x-javascript\n" | |
748 "Content-Length: 123\n", | |
749 | |
750 ",", | |
751 true, | |
752 "application/x-javascript" | |
753 }, | |
754 | |
755 // when content-length is different, check whether it recognizes image. | |
756 { | |
757 "HTTP/1.1 202 Accepted \n" | |
758 "Content-Type: image/gif \n" | |
759 "Content-Length: 123\n", | |
760 | |
761 "0", | |
762 false, | |
763 "image/gif" | |
764 }, | |
765 | |
766 // when content-length is different, check whether it recognizes JS | |
767 { | |
768 "HTTP/1.1 202 Accepted \n" | |
769 "Content-Type: application/x-javascript \n" | |
770 "Content-Length: 0\n", | |
771 | |
772 "120", | |
773 false, | |
774 "application/x-javascript" | |
775 }, | |
776 | |
777 // when content-length is different, check whether it recognizes JS | |
778 { | |
779 "HTTP/1.1 202 Accepted \n" | |
780 "Content-Type: text/javascript \n" | |
781 "Content-Length: 123\n", | |
782 | |
783 "0", | |
784 false, | |
785 "text/javascript" | |
786 }, | |
787 | |
788 // when content-length is different, check whether it recognizes CSS | |
789 { | |
790 "HTTP/1.1 202 Accepted \n" | |
791 "Content-Type: text/css\n" | |
792 "Content-Length: 111\n", | |
793 | |
794 "0", | |
795 false, | |
796 "text/css" | |
797 }, | |
798 | |
799 // when content-length is different (chrome side is missing), | |
800 // check whether it recognizes JS. | |
801 // (if phone side's content-length has been removed, shall we report? | |
802 // current implementation: not reporting.) | |
803 { | |
804 "HTTP/1.1 202 Accepted \n" | |
805 "Content-Type: application/javascript \n", | |
806 | |
807 "123", | |
808 true, | |
809 "application/javascript" | |
810 }, | |
811 | |
812 }; | |
813 | |
814 for (size_t i=0; i<ARRAYSIZE_UNSAFE(test); ++i) { | |
815 TestFingerprintContentLength(test[i]); | |
816 } | |
817 } | |
818 | |
819 TEST(DataReductionProxyTamperDetectTest, Parsing) { | |
820 std::string test[] = { | |
821 // check normal case | |
822 "Chrome-Proxy: f1:f1&f2:f2&f3:f3&f4:f4\n", | |
823 "Chrome-Proxy: fp=aa|bb|cc|dd\n", | |
824 // check special case if there are more delimiters | |
825 "Chrome-Proxy: fp=||||||||\n", | |
826 "Chrome-Proxy: fp=a|a|a|a|a|a|a|\n", | |
827 // check if there is no Chrome-Proxy header | |
828 "Content-Type: text/css\n", | |
829 // check if there is less delimiters | |
830 "Chrome-Proxy: fp= a | b | cde \n", | |
831 "Chrome-Proxy: a=1, b=2, c=5", | |
832 }; | |
833 | |
834 for (size_t i=0; i<ARRAYSIZE_UNSAFE(test); ++i) { | |
835 TestParsingCommon(test[i]); | |
836 } | |
837 } | |
838 | |
839 } // namespace | |
OLD | NEW |