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 "components/data_reduction_proxy/browser/data_reduction_proxy_tamper_de tection.h" | |
6 | |
7 #include <string.h> | |
8 #include <algorithm> | |
9 #include <map> | |
10 #include <vector> | |
11 | |
12 #include "base/base64.h" | |
13 #include "base/md5.h" | |
bengr
2014/08/04 16:54:21
#include "base/memory/scoped_ptr.h"
xingx1
2014/08/04 18:17:12
Done.
| |
14 #include "base/strings/string_number_conversions.h" | |
15 #include "base/strings/string_split.h" | |
16 #include "components/data_reduction_proxy/common/data_reduction_proxy_headers.h" | |
17 #include "net/android/network_library.h" | |
18 #include "net/http/http_response_headers.h" | |
19 #include "testing/gtest/include/gtest/gtest.h" | |
20 | |
21 #if defined(OS_ANDROID) | |
22 #include "base/android/jni_android.h" | |
23 #include "net/android/network_library.h" | |
24 #endif | |
25 | |
26 namespace { | |
27 | |
28 void HeadersToRaw(std::string* headers) { | |
29 std::replace(headers->begin(), headers->end(), '\n', '\0'); | |
30 if (!headers->empty()) | |
31 *headers += '\0'; | |
32 } | |
33 | |
34 // Calcuates MD5 hash value for a string and then base64 encode it. Testcases | |
35 // contain expected fingerprint in plain text, which needs to be encoded before | |
36 // comparison. | |
37 std::string GetEncoded(const std::string& input) { | |
38 base::MD5Digest digest; | |
39 base::MD5Sum(input.c_str(), input.size(), &digest); | |
40 std::string base64encoded; | |
41 base::Base64Encode(std::string((char*)digest.a, | |
42 ARRAYSIZE_UNSAFE(digest.a)), &base64encoded); | |
43 return base64encoded; | |
44 } | |
45 | |
46 // Replaces all contents within "[]" by corresponding base64 encoded MD5 value. | |
47 // It can handle nested case like: [[abc]def]. This helper function transforms | |
48 // fingerprint in plain text to actual encoded fingerprint. | |
49 void ReplaceWithEncodedString(std::string* input) { | |
50 size_t start, end, temp; | |
51 while (true) { | |
52 start = input->find("["); | |
53 if (start == std::string::npos) break; | |
54 while (true) { | |
55 temp = input->find("[", start + 1); | |
56 end = input->find("]", start + 1); | |
57 if (end >= 0 && end < temp) | |
58 break; | |
59 else | |
60 start = temp; | |
61 } | |
62 std::string need_to_encode = input->substr(start + 1, end - start - 1); | |
63 *input = input->substr(0, start) + GetEncoded(need_to_encode) + | |
64 input->substr(end + 1); | |
65 } | |
66 } | |
67 | |
68 // Returns a vector contains all the values from a comma-separated string. | |
69 // Some testcases contain string representation of a vector, this helper | |
70 // function generates a vector from a input string. | |
71 std::vector<std::string> StringsToVector(std::string& values) { | |
72 std::vector<std::string> ret; | |
73 if (values.size() == 0) | |
74 return ret; | |
75 size_t now = -1, next; | |
76 while((next = values.find(",", now+1)) != std::string::npos) | |
77 { | |
78 ret.push_back(values.substr(now+1, next-now-1)); | |
79 now = next; | |
80 }; | |
81 return ret; | |
82 } | |
83 | |
84 } // namespace | |
85 | |
86 namespace data_reduction_proxy { | |
87 | |
88 class DataReductionProxyTamperDetectionTest : public testing::Test {}; | |
89 | |
90 // Tests function ValidateChromeProxyHeader. | |
91 TEST_F(DataReductionProxyTamperDetectionTest, ChromeProxy) { | |
92 // |received_fingerprint| is not the actual fingerprint from data reduction | |
93 // proxy, instead, the base64 encoded field is in plain text (within "[]") | |
94 // and needs to be encoded first. | |
95 struct { | |
96 std::string label; | |
97 std::string raw_header; | |
98 std::string received_fingerprint; | |
99 bool expected_tampered_with; | |
100 } test[] = { | |
101 { | |
102 "Checks sorting.", | |
103 "HTTP/1.1 200 OK\n" | |
104 "Chrome-Proxy: c,b,a,3,2,1,fcp=f\n", | |
105 "[1,2,3,a,b,c,]", | |
106 false, | |
107 }, | |
108 { | |
109 "Checks Chrome-Proxy's fingerprint removing.", | |
110 "HTTP/1.1 200 OK\n" | |
111 "Chrome-Proxy: a,b,c,d,e,3,2,1,fcp=f\n", | |
112 "[1,2,3,a,b,c,d,e,]", | |
113 false, | |
114 }, | |
115 { | |
116 "Checks no Chrome-Proxy header case (should not happen).", | |
117 "HTTP/1.1 200 OK\n", | |
118 "[]", | |
119 false, | |
120 }, | |
121 { | |
122 "Checks empty Chrome-Proxy header case (should not happen).", | |
123 "HTTP/1.1 200 OK\n" | |
124 "Chrome-Proxy: \n", | |
125 "[,]", | |
126 false, | |
127 }, | |
128 { | |
129 "Checks Chrome-Proxy header with its fingerprint only case.", | |
130 "HTTP/1.1 200 OK\n" | |
131 "Chrome-Proxy: fcp=f\n", | |
132 "[]", | |
133 false, | |
134 }, | |
135 { | |
136 "Checks empty Chrome-Proxy header case, with extra ',' and ' '", | |
137 "HTTP/1.1 200 OK\n" | |
138 "Chrome-Proxy: fcp=f , \n", | |
139 "[]", | |
140 false, | |
141 }, | |
142 { | |
143 "Changed no value to empty value.", | |
144 "HTTP/1.1 200 OK\n" | |
145 "Chrome-Proxy: fcp=f\n", | |
146 "[,]", | |
147 true, | |
148 }, | |
149 { | |
150 "Changed header values.", | |
151 "HTTP/1.1 200 OK\n" | |
152 "Chrome-Proxy: a,b=2,c,d=1,fcp=f\n", | |
153 "[a,b=3,c,d=1,]", | |
154 true, | |
155 }, | |
156 { | |
157 "Changed order of header values.", | |
158 "HTTP/1.1 200 OK\n" | |
159 "Chrome-Proxy: c,b,a,fcp=1\n", | |
160 "[c,b,a,]", | |
161 true, | |
162 }, | |
163 { | |
164 "Checks Chrome-Proxy header with extra ' '.", | |
165 "HTTP/1.1 200 OK\n" | |
166 "Chrome-Proxy: a , b , c, d, fcp=f\n", | |
167 "[a,b,c,d,]", | |
168 false | |
169 }, | |
170 { | |
171 "Check Chrome-Proxy header with multiple lines and ' '.", | |
172 "HTTP/1.1 200 OK\n" | |
173 "Chrome-Proxy: a , c , d, fcp=f \n" | |
174 "Chrome-Proxy: b \n", | |
175 "[a,b,c,d,]", | |
176 false | |
177 }, | |
178 { | |
179 "Checks Chrome-Proxy header with multiple lines, at different positions", | |
180 "HTTP/1.1 200 OK\n" | |
181 "Chrome-Proxy: a \n" | |
182 "Chrome-Proxy: c \n" | |
183 "Content-Type: 1\n" | |
184 "Cache-Control: 2\n" | |
185 "ETag: 3\n" | |
186 "Chrome-Proxy: b \n" | |
187 "Connection: 4\n" | |
188 "Expires: 5\n" | |
189 "Chrome-Proxy: fcp=f \n" | |
190 "Via: \n" | |
191 "Content-Length: 12345\n", | |
192 "[a,b,c,]", | |
193 false | |
194 }, | |
195 { | |
196 "Checks Chrome-Proxy header with multiple same values.", | |
197 "HTTP/1.1 200 OK\n" | |
198 "Chrome-Proxy: a \n" | |
199 "Chrome-Proxy: b\n" | |
200 "Chrome-Proxy: c\n" | |
201 "Chrome-Proxy: d, fcp=f \n" | |
202 "Chrome-Proxy: a \n", | |
203 "[a,a,b,c,d,]", | |
204 false | |
205 }, | |
206 { | |
207 "Changed Chrome-Proxy header with multiple lines..", | |
208 "HTTP/1.1 200 OK\n" | |
209 "Chrome-Proxy: a\n" | |
210 "Chrome-Proxy: a\n" | |
211 "Chrome-Proxy: b\n" | |
212 "Chrome-Proxy: c,fcp=f\n", | |
213 "[a,b,c,]", | |
214 true, | |
215 }, | |
216 { | |
217 "Checks case whose received fingerprint is empty.", | |
218 "HTTP/1.1 200 OK\n" | |
219 "Chrome-Proxy: a,b,c,fcp=1\n", | |
220 "[]", | |
221 true, | |
222 }, | |
223 { | |
224 "Checks case whose received fingerprint cannot be base64 decoded.", | |
225 "HTTP/1.1 200 OK\n" | |
226 "Chrome-Proxy: a,b,c,fcp=1\n", | |
227 "not_base64_encoded", | |
228 true, | |
229 }, | |
230 }; | |
231 | |
232 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test); ++i) { | |
233 ReplaceWithEncodedString(&test[i].received_fingerprint); | |
234 | |
235 std::string raw_headers(test[i].raw_header); | |
236 HeadersToRaw(&raw_headers); | |
237 net::HttpResponseHeaders* headers = | |
bengr
2014/08/04 16:54:21
scoped_ptr<net::HttpResponseHeaders> headers(new .
xingx1
2014/08/04 18:17:12
Done.
| |
238 new net::HttpResponseHeaders(raw_headers); | |
239 | |
240 DataReductionProxyTamperDetection tamper_detection(headers, true, 0); | |
241 | |
242 bool tampered = tamper_detection.ValidateChromeProxyHeader( | |
243 test[i].received_fingerprint); | |
bengr
2014/08/04 16:54:21
indentation
xingx1
2014/08/04 18:17:12
Done.
| |
244 | |
245 EXPECT_EQ(test[i].expected_tampered_with, tampered) << test[i].label; | |
246 } | |
247 } | |
248 | |
249 // Tests function ValidateViaHeader. | |
250 TEST_F(DataReductionProxyTamperDetectionTest, Via) { | |
251 struct { | |
252 std::string label; | |
253 std::string raw_header; | |
254 std::string received_fingerprint; | |
255 bool expected_tampered_with; | |
256 bool expected_has_chrome_proxy_via_header; | |
257 } test[] = { | |
258 { | |
259 "Checks the case that Chrome-Compression-Proxy occurs at the last.", | |
260 "HTTP/1.1 200 OK\n" | |
261 "Via: a, b, c, 1.1 Chrome-Compression-Proxy\n", | |
262 "", | |
263 false, | |
264 true, | |
265 }, | |
266 { | |
267 "Checks when there is intermediary.", | |
268 "HTTP/1.1 200 OK\n" | |
269 "Via: a, b, c, 1.1 Chrome-Compression-Proxy, xyz\n", | |
270 "", | |
271 true, | |
272 true, | |
273 }, | |
274 { | |
275 "Checks the case of empty Via header.", | |
276 "HTTP/1.1 200 OK\n" | |
277 "Via: \n", | |
278 "", | |
279 false, | |
280 false, | |
281 }, | |
282 { | |
283 "Checks the case that only the data reduction proxy's Via header occurs.", | |
284 "HTTP/1.1 200 OK\n" | |
285 "Via: 1.1 Chrome-Compression-Proxy \n", | |
286 "", | |
287 false, | |
288 true, | |
289 }, | |
290 { | |
291 "Checks the case that there are ' ', i.e., empty value after the data" | |
292 " reduction proxy's Via header.", | |
293 "HTTP/1.1 200 OK\n" | |
294 "Via: 1.1 Chrome-Compression-Proxy , , \n", | |
295 "", | |
296 false, | |
297 true, | |
298 }, | |
299 { | |
300 "Checks the case when there is no Via header", | |
301 "HTTP/1.1 200 OK\n", | |
302 "", | |
303 false, | |
304 false, | |
305 }, | |
306 // Same to above test cases, but with deprecated data reduciton proxy Via | |
307 // header. | |
308 { | |
309 "Checks the case that Chrome Compression Proxy occurs at the last.", | |
310 "HTTP/1.1 200 OK\n" | |
311 "Via: a, b, c, 1.1 Chrome Compression Proxy\n", | |
312 "", | |
313 false, | |
314 true, | |
315 }, | |
316 { | |
317 "Checks when there is intermediary.", | |
318 "HTTP/1.1 200 OK\n" | |
319 "Via: a, b, c, 1.1 Chrome Compression Proxy, xyz\n", | |
320 "", | |
321 true, | |
322 true, | |
323 }, | |
324 { | |
325 "Checks the case of empty Via header.", | |
326 "HTTP/1.1 200 OK\n" | |
327 "Via: \n", | |
328 "", | |
329 false, | |
330 false, | |
331 }, | |
332 { | |
333 "Checks the case that only the data reduction proxy's Via header occurs.", | |
334 "HTTP/1.1 200 OK\n" | |
335 "Via: 1.1 Chrome Compression Proxy \n", | |
336 "", | |
337 false, | |
338 true, | |
339 }, | |
340 { | |
341 "Checks the case that there are ' ', i.e., empty value after the data" | |
342 "reduction proxy's Via header.", | |
343 "HTTP/1.1 200 OK\n" | |
344 "Via: 1.1 Chrome Compression Proxy , , \n", | |
345 "", | |
346 false, | |
347 true, | |
348 }, | |
349 { | |
350 "Checks the case when there is no Via header", | |
351 "HTTP/1.1 200 OK\n", | |
352 "", | |
353 false, | |
354 false, | |
355 }, | |
356 }; | |
357 | |
358 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test); ++i) { | |
359 std::string raw_headers(test[i].raw_header); | |
360 HeadersToRaw(&raw_headers); | |
361 net::HttpResponseHeaders* headers = | |
362 new net::HttpResponseHeaders(raw_headers); | |
bengr
2014/08/04 16:54:22
scoped_ptr<net::HttpResponseHeaders> headers(new .
xingx1
2014/08/04 18:17:12
Done.
| |
363 | |
364 DataReductionProxyTamperDetection tamper_detection(headers, true, 0); | |
365 | |
366 bool has_chrome_proxy_via_header; | |
367 bool tampered = tamper_detection.ValidateViaHeader( | |
368 test[i].received_fingerprint, &has_chrome_proxy_via_header); | |
369 | |
370 EXPECT_EQ(test[i].expected_tampered_with, tampered) << test[i].label; | |
371 EXPECT_EQ(test[i].expected_has_chrome_proxy_via_header, | |
372 has_chrome_proxy_via_header) << test[i].label; | |
373 } | |
374 } | |
375 | |
376 // Tests function ValidateOtherHeaders. | |
377 TEST_F(DataReductionProxyTamperDetectionTest, OtherHeaders) { | |
378 // For following testcases, |received_fingerprint| is not the actual | |
379 // fingerprint from data reduction proxy, instead, the base64 encoded field | |
380 // is in plain text (within "[]") and needs to be encoded first. For example, | |
381 // "[12345;]|content-length" needs to be encoded to | |
382 // "Base64Encoded(MD5(12345;))|content-length" before calling the checking | |
383 // function. | |
384 struct { | |
385 std::string label; | |
386 std::string raw_header; | |
387 std::string received_fingerprint; | |
388 bool expected_tampered_with; | |
389 } test[] = { | |
390 { | |
391 "Checks the case that only one header is requested.", | |
392 "HTTP/1.1 200 OK\n" | |
393 "Content-Length: 12345\n", | |
394 "[12345,;]|content-length", | |
395 false | |
396 }, | |
397 { | |
398 "Checks the case that there is only one requested header and it does not" | |
399 "exist.", | |
400 "HTTP/1.1 200 OK\n", | |
401 "[;]|non_exist_header", | |
402 false | |
403 }, | |
404 { | |
405 "Checks the case of multiple headers are requested.", | |
406 "HTTP/1.1 200 OK\n" | |
407 "Content-Type: 1\n" | |
408 "Cache-Control: 2\n" | |
409 "ETag: 3\n" | |
410 "Connection: 4\n" | |
411 "Expires: 5\n", | |
412 "[1,;2,;3,;4,;5,;]|content-type|cache-control|etag|connection|expires", | |
413 false | |
414 }, | |
415 { | |
416 "Checks the case that one header has multiple values.", | |
417 "HTTP/1.1 200 OK\n" | |
418 "Content-Type: aaa1, bbb1, ccc1\n" | |
419 "Cache-Control: aaa2\n", | |
420 "[aaa1,bbb1,ccc1,;aaa2,;]|content-type|cache-control", | |
421 false | |
422 }, | |
423 { | |
424 "Checks the case that one header has multiple lines.", | |
425 "HTTP/1.1 200 OK\n" | |
426 "Content-Type: aaa1, ccc1\n" | |
427 "Content-Type: xxx1, bbb1, ccc1\n" | |
428 "Cache-Control: aaa2\n", | |
429 "[aaa1,bbb1,ccc1,ccc1,xxx1,;aaa2,;]|content-type|cache-control", | |
430 false | |
431 }, | |
432 { | |
433 "Checks the case that more than one headers have multiple values.", | |
434 "HTTP/1.1 200 OK\n" | |
435 "Content-Type: aaa1, ccc1\n" | |
436 "Cache-Control: ccc2 , bbb2\n" | |
437 "Content-Type: bbb1, ccc1\n" | |
438 "Cache-Control: aaa2 \n", | |
439 "[aaa1,bbb1,ccc1,ccc1,;aaa2,bbb2,ccc2,;]|content-type|cache-control", | |
440 false | |
441 }, | |
442 { | |
443 "Checks the case that one of the requested headers is missing (Expires).", | |
444 "HTTP/1.1 200 OK\n" | |
445 "Content-Type: aaa1, ccc1\n", | |
446 "[aaa1,ccc1,;;]|content-type|expires", | |
447 false | |
448 }, | |
449 { | |
450 "Checks the case that some of the requested headers have empty value.", | |
451 "HTTP/1.1 200 OK\n" | |
452 "Content-Type: \n" | |
453 "Cache-Control: \n", | |
454 "[,;,;]|content-type|cache-control", | |
455 false | |
456 }, | |
457 { | |
458 "Checks the case that all the requested headers are missing.", | |
459 "HTTP/1.1 200 OK\n", | |
460 "[;;]|content-type|expires", | |
461 false | |
462 }, | |
463 { | |
464 "Checks the case that some headers are missing, some of them are empty.", | |
465 "HTTP/1.1 200 OK\n" | |
466 "Cache-Control: \n", | |
467 "[;,;]|content-type|cache-control", | |
468 false | |
469 }, | |
470 { | |
471 "Checks the case there is no requested header (header list is empty).", | |
472 "HTTP/1.1 200 OK\n" | |
473 "Chrome-Proxy: aut=aauutthh,bbbypas=0,aaxxx=xxx,bbbloc=1\n" | |
474 "Content-Type: 1\n" | |
475 "Cache-Control: 2\n", | |
476 "[]", | |
477 false | |
478 }, | |
479 { | |
480 "Checks tampered requested header values.", | |
481 "HTTP/1.1 200 OK\n" | |
482 "Content-Type: aaa1, ccc1\n" | |
483 "Cache-Control: ccc2 , bbb2\n", | |
484 "[aaa1,bbb1,;bbb2,ccc2,;]|content-type|cache-control", | |
485 true | |
486 }, | |
487 }; | |
488 | |
489 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test); ++i) { | |
490 ReplaceWithEncodedString(&(test[i].received_fingerprint)); | |
491 | |
492 std::string raw_headers(test[i].raw_header); | |
493 HeadersToRaw(&raw_headers); | |
494 net::HttpResponseHeaders* headers = | |
495 new net::HttpResponseHeaders(raw_headers); | |
bengr
2014/08/04 16:54:22
scoped_ptr<net::HttpResponseHeaders> headers(new .
xingx1
2014/08/04 18:17:12
Done.
| |
496 | |
497 DataReductionProxyTamperDetection tamper_detection(headers, true, 0); | |
498 | |
499 bool tampered = tamper_detection.ValidateOtherHeaders( | |
500 test[i].received_fingerprint); | |
501 | |
502 EXPECT_EQ(test[i].expected_tampered_with, tampered) << test[i].label; | |
503 } | |
504 } | |
505 | |
506 // Tests function ValidateContentLengthHeader. | |
507 TEST_F(DataReductionProxyTamperDetectionTest, ContentLength) { | |
508 struct { | |
509 std::string label; | |
510 std::string raw_header; | |
511 std::string received_fingerprint; | |
512 bool expected_tampered_with; | |
513 } test[] = { | |
514 { | |
515 "Checks the case fingerprint matches received response.", | |
516 "HTTP/1.1 200 OK\n" | |
517 "Content-Length: 12345\n", | |
518 "12345", | |
519 false, | |
520 }, | |
521 { | |
522 "Checks case that response got modified.", | |
523 "HTTP/1.1 200 OK\n" | |
524 "Content-Length: 12345\n", | |
525 "125", | |
526 true, | |
527 }, | |
528 { | |
529 "Checks the case that the data reduction proxy has not sent" | |
530 "Content-Length header.", | |
531 "HTTP/1.1 200 OK\n" | |
532 "Content-Length: 12345\n", | |
533 "", | |
534 false, | |
535 }, | |
536 { | |
537 "Checks the case that the data reduction proxy sends invalid" | |
538 "Content-Length header.", | |
539 "HTTP/1.1 200 OK\n" | |
540 "Content-Length: 12345\n", | |
541 "aaa", | |
542 false, | |
543 }, | |
544 { | |
545 "Checks the case that the data reduction proxy sends invalid" | |
546 "Content-Length header.", | |
547 "HTTP/1.1 200 OK\n" | |
548 "Content-Length: aaa\n", | |
549 "aaa", | |
550 false, | |
551 }, | |
552 { | |
553 "Checks the case that Content-Length header is missing at the Chromium" | |
554 "client side.", | |
555 "HTTP/1.1 200 OK\n", | |
556 "123", | |
557 false, | |
558 }, | |
559 { | |
560 "Checks the case that Content-Length header are missing at both end.", | |
561 "HTTP/1.1 200 OK\n", | |
562 "", | |
563 false, | |
564 }, | |
565 }; | |
566 | |
567 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test); ++i) { | |
568 std::string raw_headers(test[i].raw_header); | |
569 HeadersToRaw(&raw_headers); | |
570 net::HttpResponseHeaders* headers = | |
571 new net::HttpResponseHeaders(raw_headers); | |
bengr
2014/08/04 16:54:21
scoped_ptr<net::HttpResponseHeaders> headers(new .
xingx1
2014/08/04 18:17:12
Done.
| |
572 | |
573 DataReductionProxyTamperDetection tamper_detection(headers, true, 0); | |
574 | |
575 bool tampered = tamper_detection.ValidateContentLengthHeader( | |
576 test[i].received_fingerprint); | |
577 | |
578 EXPECT_EQ(test[i].expected_tampered_with, tampered) << test[i].label; | |
579 } | |
580 } | |
581 | |
582 // Tests ValuesToSortedString function. | |
583 TEST_F(DataReductionProxyTamperDetectionTest, ValuesToSortedString) { | |
584 struct { | |
585 std::string label; | |
586 std::string input_values; | |
587 std::string expected_output_string; | |
588 } test[] = { | |
589 { | |
590 "Checks the correctness of sorting.", | |
591 "3,2,1,", | |
592 "1,2,3,", | |
593 }, | |
594 { | |
595 "Checks the case that there is an empty input vector.", | |
596 "", | |
597 "", | |
598 }, | |
599 { | |
600 "Checks the case that there is an empty string in the input vector.", | |
601 ",", | |
602 ",", | |
603 }, | |
604 }; | |
605 | |
606 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test); ++i) { | |
607 std::vector<std::string> input_values = | |
608 StringsToVector(test[i].input_values); | |
609 | |
bengr
2014/08/04 16:54:22
remove blank line
xingx1
2014/08/04 18:17:12
Done.
| |
610 std::string output_string = | |
611 DataReductionProxyTamperDetection::ValuesToSortedString(&input_values); | |
612 | |
bengr
2014/08/04 16:54:22
remove blank line
xingx1
2014/08/04 18:17:12
Done.
| |
613 EXPECT_EQ(output_string, test[i].expected_output_string) << test[i].label; | |
614 } | |
615 } | |
616 | |
617 // Tests GetHeaderValues function. | |
618 TEST_F(DataReductionProxyTamperDetectionTest, GetHeaderValues) { | |
619 struct { | |
620 std::string label; | |
621 std::string raw_header; | |
622 std::string header_name; | |
623 std::string expected_output_values; | |
624 } test[] = { | |
625 { | |
626 "Checks the correctness of getting single line header.", | |
627 "HTTP/1.1 200 OK\n" | |
628 "test: 1, 2, 3\n", | |
629 "test", | |
630 "1,2,3,", | |
631 }, | |
632 { | |
633 "Checks the correctness of getting multiple lines header.", | |
634 "HTTP/1.1 200 OK\n" | |
635 "test: 1, 2, 3\n" | |
636 "test: 4, 5, 6\n" | |
637 "test: 7, 8, 9\n", | |
638 "test", | |
639 "1,2,3,4,5,6,7,8,9,", | |
640 }, | |
641 { | |
642 "Checks the correctness of getting missing header.", | |
643 "HTTP/1.1 200 OK\n", | |
644 "test", | |
645 "", | |
646 }, | |
647 { | |
648 "Checks the correctness of getting empty header.", | |
649 "HTTP/1.1 200 OK\n" | |
650 "test: \n", | |
651 "test", | |
652 ",", | |
653 }, | |
654 }; | |
655 | |
656 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test); ++i) { | |
657 std::string raw_headers(test[i].raw_header); | |
658 HeadersToRaw(&raw_headers); | |
659 net::HttpResponseHeaders* headers = | |
660 new net::HttpResponseHeaders(raw_headers); | |
bengr
2014/08/04 16:54:21
scoped_ptr<net::HttpResponseHeaders> headers(new .
xingx1
2014/08/04 18:17:12
Done.
| |
661 | |
662 std::vector<std::string> expected_output_values = | |
663 StringsToVector(test[i].expected_output_values); | |
664 | |
665 std::vector<std::string> output_values = | |
666 DataReductionProxyTamperDetection::GetHeaderValues( | |
667 headers, test[i].header_name); | |
668 EXPECT_EQ(expected_output_values, output_values) << test[i].label; | |
669 } | |
670 } | |
671 | |
672 // Tests main function DetectAndReport. | |
673 TEST_F(DataReductionProxyTamperDetectionTest, DetectAndReport) { | |
674 struct { | |
675 std::string label; | |
676 std::string raw_header; | |
677 bool expected_tampered_with; | |
678 } test[] = { | |
679 { | |
680 "Check no fingerprint added case.", | |
681 "HTTP/1.1 200 OK\n" | |
682 "Via: a1, b2, 1.1 Chrome-Compression-Proxy\n" | |
683 "Content-Length: 12345\n" | |
684 "Chrome-Proxy: bypass=0\n", | |
685 false, | |
686 }, | |
687 { | |
688 "Check the case Chrome-Proxy fingerprint doesn't match.", | |
689 "HTTP/1.1 200 OK\n" | |
690 "Via: a1, b2, 1.1 Chrome-Compression-Proxy\n" | |
691 "Content-Length: 12345\n" | |
692 "header1: header_1\n" | |
693 "header2: header_2\n" | |
694 "header3: header_3\n" | |
695 "Chrome-Proxy: fcl=12345, " | |
696 "foh=[header_1,;header_2,;header_3,;]|header1|header2|header3,fvia=0," | |
697 "fcp=abc\n", | |
698 true, | |
699 }, | |
700 { | |
701 "Check the case response matches the fingerprint completely.", | |
702 "HTTP/1.1 200 OK\n" | |
703 "Via: a1, b2, 1.1 Chrome-Compression-Proxy\n" | |
704 "Content-Length: 12345\n" | |
705 "header1: header_1\n" | |
706 "header2: header_2\n" | |
707 "header3: header_3\n" | |
708 "Chrome-Proxy: fcl=12345, " | |
709 "foh=[header_1,;header_2,;header_3,;]|header1|header2|header3," | |
710 "fvia=0, fcp=[fcl=12345,foh=[header_1,;header_2,;header_3,;]" | |
711 "|header1|header2|header3,fvia=0,]\n", | |
712 false, | |
713 }, | |
714 { | |
715 "Check the case that Content-Length doesn't match.", | |
716 "HTTP/1.1 200 OK\n" | |
717 "Via: a1, b2, 1.1 Chrome-Compression-Proxy\n" | |
718 "Content-Length: 0\n" | |
719 "header1: header_1\n" | |
720 "header2: header_2\n" | |
721 "header3: header_3\n" | |
722 "Chrome-Proxy: fcl=12345, " | |
723 "foh=[header_1,;header_2,;header_3,;]|header1|header2|header3, fvia=0, " | |
724 "fcp=[fcl=12345,foh=[header_1,;header_2,;header_3,;]|" | |
725 "header1|header2|header3,fvia=0,]\n", | |
726 true, | |
727 }, | |
728 }; | |
729 | |
730 #if defined(OS_ANDROID) | |
731 JNIEnv* env = base::android::AttachCurrentThread(); | |
732 net::android::RegisterNetworkLibrary(env); | |
733 #endif | |
734 | |
735 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test); ++i) { | |
736 std::string raw_headers(test[i].raw_header); | |
737 ReplaceWithEncodedString(&raw_headers); | |
738 HeadersToRaw(&raw_headers); | |
739 net::HttpResponseHeaders* headers = | |
740 new net::HttpResponseHeaders(raw_headers); | |
bengr
2014/08/04 16:54:21
scoped_ptr<net::HttpResponseHeaders> headers(new .
xingx1
2014/08/04 18:17:12
Done.
| |
741 | |
742 EXPECT_EQ(test[i].expected_tampered_with, | |
743 DataReductionProxyTamperDetection::DetectAndReport(headers, true)) | |
744 << test[i].label; | |
745 } | |
746 } | |
747 | |
748 } // namespace | |
OLD | NEW |