Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1703)

Side by Side Diff: components/data_reduction_proxy/common/data_reduction_proxy_headers_unittest.cc

Issue 444723003: Revert of Modify data_reduction_proxy_header to support tamper detection logic. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@work
Patch Set: Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « components/data_reduction_proxy/common/data_reduction_proxy_headers.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/data_reduction_proxy/common/data_reduction_proxy_headers.h" 5 #include "components/data_reduction_proxy/common/data_reduction_proxy_headers.h"
6 6
7 #include "net/http/http_response_headers.h" 7 #include "net/http/http_response_headers.h"
8 #include "net/proxy/proxy_service.h" 8 #include "net/proxy/proxy_service.h"
9 #include "testing/gtest/include/gtest/gtest.h" 9 #include "testing/gtest/include/gtest/gtest.h"
10 10
11 namespace { 11 namespace {
12 12
13 // Transform "normal"-looking headers (\n-separated) to the appropriate 13 // Transform "normal"-looking headers (\n-separated) to the appropriate
14 // input format for ParseRawHeaders (\0-separated). 14 // input format for ParseRawHeaders (\0-separated).
15 void HeadersToRaw(std::string* headers) { 15 void HeadersToRaw(std::string* headers) {
16 std::replace(headers->begin(), headers->end(), '\n', '\0'); 16 std::replace(headers->begin(), headers->end(), '\n', '\0');
17 if (!headers->empty()) 17 if (!headers->empty())
18 *headers += '\0'; 18 *headers += '\0';
19 } 19 }
20 20
21 } // namespace 21 } // namespace
22 22
23 namespace data_reduction_proxy { 23 namespace data_reduction_proxy {
24 24
25 class DataReductionProxyHeadersTest : public testing::Test {}; 25 class DataReductionProxyHeadersTest : public testing::Test {};
26 26
27 TEST_F(DataReductionProxyHeadersTest, GetDataReductionProxyActionValue) {
28 const struct {
29 const char* headers;
30 std::string action_key;
31 bool expected_result;
32 std::string expected_action_value;
33 } tests[] = {
34 { "HTTP/1.1 200 OK\n"
35 "Content-Length: 999\n",
36 "a",
37 false,
38 "",
39 },
40 { "HTTP/1.1 200 OK\n"
41 "connection: keep-alive\n"
42 "Content-Length: 999\n",
43 "a",
44 false,
45 "",
46 },
47 { "HTTP/1.1 200 OK\n"
48 "connection: keep-alive\n"
49 "Chrome-Proxy: bypass=86400\n"
50 "Content-Length: 999\n",
51 "bypass",
52 true,
53 "86400",
54 },
55 { "HTTP/1.1 200 OK\n"
56 "connection: keep-alive\n"
57 "Chrome-Proxy: bypass86400\n"
58 "Content-Length: 999\n",
59 "bypass",
60 false,
61 "",
62 },
63 { "HTTP/1.1 200 OK\n"
64 "connection: keep-alive\n"
65 "Chrome-Proxy: bypass=0\n"
66 "Content-Length: 999\n",
67 "bypass",
68 true,
69 "0",
70 },
71 { "HTTP/1.1 200 OK\n"
72 "connection: keep-alive\n"
73 "Chrome-Proxy: bypass=1500\n"
74 "Chrome-Proxy: bypass=86400\n"
75 "Content-Length: 999\n",
76 "bypass",
77 true,
78 "1500",
79 },
80 { "HTTP/1.1 200 OK\n"
81 "connection: keep-alive\n"
82 "Chrome-Proxy: block=1500, block=3600\n"
83 "Content-Length: 999\n",
84 "block",
85 true,
86 "1500",
87 },
88 { "HTTP/1.1 200 OK\n"
89 "connection: proxy-bypass\n"
90 "Chrome-Proxy: key=123 \n"
91 "Content-Length: 999\n",
92 "key",
93 true,
94 "123",
95 },
96 };
97 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
98 std::string headers(tests[i].headers);
99 HeadersToRaw(&headers);
100 scoped_refptr<net::HttpResponseHeaders> parsed(
101 new net::HttpResponseHeaders(headers));
102
103 std::string action_value;
104 bool has_action_key = GetDataReductionProxyActionValue(
105 parsed, tests[i].action_key, &action_value);
106 EXPECT_EQ(tests[i].expected_result, has_action_key);
107 if (has_action_key) {
108 EXPECT_EQ(tests[i].expected_action_value, action_value);
109 }
110 }
111 }
112
113 TEST_F(DataReductionProxyHeadersTest, GetProxyBypassInfo) { 27 TEST_F(DataReductionProxyHeadersTest, GetProxyBypassInfo) {
114 const struct { 28 const struct {
115 const char* headers; 29 const char* headers;
116 bool expected_result; 30 bool expected_result;
117 int64 expected_retry_delay; 31 int64 expected_retry_delay;
118 bool expected_bypass_all; 32 bool expected_bypass_all;
119 } tests[] = { 33 } tests[] = {
120 { "HTTP/1.1 200 OK\n" 34 { "HTTP/1.1 200 OK\n"
121 "Content-Length: 999\n", 35 "Content-Length: 999\n",
122 false, 36 false,
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after
281 195
282 DataReductionProxyInfo data_reduction_proxy_info; 196 DataReductionProxyInfo data_reduction_proxy_info;
283 EXPECT_TRUE(ParseHeadersAndSetProxyInfo(parsed, &data_reduction_proxy_info)); 197 EXPECT_TRUE(ParseHeadersAndSetProxyInfo(parsed, &data_reduction_proxy_info));
284 EXPECT_LE(60, data_reduction_proxy_info.bypass_duration.InSeconds()); 198 EXPECT_LE(60, data_reduction_proxy_info.bypass_duration.InSeconds());
285 EXPECT_GE(5 * 60, data_reduction_proxy_info.bypass_duration.InSeconds()); 199 EXPECT_GE(5 * 60, data_reduction_proxy_info.bypass_duration.InSeconds());
286 EXPECT_FALSE(data_reduction_proxy_info.bypass_all); 200 EXPECT_FALSE(data_reduction_proxy_info.bypass_all);
287 } 201 }
288 202
289 TEST_F(DataReductionProxyHeadersTest, HasDataReductionProxyViaHeader) { 203 TEST_F(DataReductionProxyHeadersTest, HasDataReductionProxyViaHeader) {
290 const struct { 204 const struct {
291 const char* headers; 205 const char* headers;
292 bool expected_result; 206 bool expected_result;
293 bool expected_has_intermediary;
294 bool ignore_intermediary;
295 } tests[] = { 207 } tests[] = {
296 { "HTTP/1.1 200 OK\n" 208 { "HTTP/1.1 200 OK\n"
297 "Via: 1.1 Chrome-Proxy\n", 209 "Via: 1.1 Chrome-Proxy\n",
298 false, 210 false,
299 false,
300 false,
301 }, 211 },
302 { "HTTP/1.1 200 OK\n" 212 { "HTTP/1.1 200 OK\n"
303 "Via: 1\n", 213 "Via: 1\n",
304 false, 214 false,
305 false,
306 false,
307 }, 215 },
308 { "HTTP/1.1 200 OK\n" 216 { "HTTP/1.1 200 OK\n"
309 "Via: 1.1 Chrome-Compression-Proxy\n", 217 "Via: 1.1 Chrome-Compression-Proxy\n",
310 true, 218 true,
311 true,
312 false,
313 }, 219 },
314 { "HTTP/1.1 200 OK\n" 220 { "HTTP/1.1 200 OK\n"
315 "Via: 1.0 Chrome-Compression-Proxy\n", 221 "Via: 1.0 Chrome-Compression-Proxy\n",
316 true, 222 true,
317 true,
318 false,
319 }, 223 },
320 { "HTTP/1.1 200 OK\n" 224 { "HTTP/1.1 200 OK\n"
321 "Via: 1.1 Foo-Bar, 1.1 Chrome-Compression-Proxy\n", 225 "Via: 1.1 Foo-Bar, 1.1 Chrome-Compression-Proxy\n",
322 true, 226 true,
323 true,
324 false,
325 }, 227 },
326 { "HTTP/1.1 200 OK\n" 228 { "HTTP/1.1 200 OK\n"
327 "Via: 1.1 Chrome-Compression-Proxy, 1.1 Bar-Foo\n", 229 "Via: 1.1 Chrome-Compression-Proxy, 1.1 Bar-Foo\n",
328 true, 230 true,
329 false,
330 false,
331 }, 231 },
332 { "HTTP/1.1 200 OK\n" 232 { "HTTP/1.1 200 OK\n"
333 "Via: 1.1 chrome-compression-proxy\n", 233 "Via: 1.1 chrome-compression-proxy\n",
334 false, 234 false,
335 false,
336 false,
337 }, 235 },
338 { "HTTP/1.1 200 OK\n" 236 { "HTTP/1.1 200 OK\n"
339 "Via: 1.1 Foo-Bar\n" 237 "Via: 1.1 Foo-Bar\n"
340 "Via: 1.1 Chrome-Compression-Proxy\n", 238 "Via: 1.1 Chrome-Compression-Proxy\n",
341 true, 239 true,
342 true,
343 false,
344 },
345 { "HTTP/1.1 200 OK\n"
346 "Via: 1.1 Chrome-Compression-Proxy\n"
347 "Via: 1.1 Foo-Bar\n",
348 true,
349 false,
350 false,
351 }, 240 },
352 { "HTTP/1.1 200 OK\n" 241 { "HTTP/1.1 200 OK\n"
353 "Via: 1.1 Chrome-Proxy\n", 242 "Via: 1.1 Chrome-Proxy\n",
354 false, 243 false,
355 false,
356 false,
357 }, 244 },
358 { "HTTP/1.1 200 OK\n" 245 { "HTTP/1.1 200 OK\n"
359 "Via: 1.1 Chrome Compression Proxy\n", 246 "Via: 1.1 Chrome Compression Proxy\n",
360 true, 247 true,
361 true,
362 false,
363 }, 248 },
364 { "HTTP/1.1 200 OK\n" 249 { "HTTP/1.1 200 OK\n"
365 "Via: 1.1 Foo-Bar, 1.1 Chrome Compression Proxy\n", 250 "Via: 1.1 Foo-Bar, 1.1 Chrome Compression Proxy\n",
366 true, 251 true,
367 true,
368 false,
369 }, 252 },
370 { "HTTP/1.1 200 OK\n" 253 { "HTTP/1.1 200 OK\n"
371 "Via: 1.1 Chrome Compression Proxy, 1.1 Bar-Foo\n", 254 "Via: 1.1 Chrome Compression Proxy, 1.1 Bar-Foo\n",
372 true, 255 true,
373 false,
374 false,
375 }, 256 },
376 { "HTTP/1.1 200 OK\n" 257 { "HTTP/1.1 200 OK\n"
377 "Via: 1.1 chrome compression proxy\n", 258 "Via: 1.1 chrome compression proxy\n",
378 false, 259 false,
379 false,
380 false,
381 }, 260 },
382 { "HTTP/1.1 200 OK\n" 261 { "HTTP/1.1 200 OK\n"
383 "Via: 1.1 Foo-Bar\n" 262 "Via: 1.1 Foo-Bar\n"
384 "Via: 1.1 Chrome Compression Proxy\n", 263 "Via: 1.1 Chrome Compression Proxy\n",
385 true, 264 true,
386 true,
387 false,
388 },
389 { "HTTP/1.1 200 OK\n"
390 "Via: 1.1 Chrome Compression Proxy\n"
391 "Via: 1.1 Foo-Bar\n",
392 true,
393 false,
394 false,
395 },
396 { "HTTP/1.1 200 OK\n"
397 "Via: 1.1 Chrome Compression Proxy\n"
398 "Via: 1.1 Foo-Bar\n",
399 true,
400 false,
401 true,
402 }, 265 },
403 }; 266 };
404 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { 267 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
405 std::string headers(tests[i].headers); 268 std::string headers(tests[i].headers);
406 HeadersToRaw(&headers); 269 HeadersToRaw(&headers);
407 scoped_refptr<net::HttpResponseHeaders> parsed( 270 scoped_refptr<net::HttpResponseHeaders> parsed(
408 new net::HttpResponseHeaders(headers)); 271 new net::HttpResponseHeaders(headers));
409 272
410 bool has_chrome_proxy_via_header, has_intermediary; 273 EXPECT_EQ(tests[i].expected_result,
411 if (tests[i].ignore_intermediary) { 274 HasDataReductionProxyViaHeader(parsed));
412 has_chrome_proxy_via_header =
413 HasDataReductionProxyViaHeader(parsed, NULL);
414 }
415 else {
416 has_chrome_proxy_via_header =
417 HasDataReductionProxyViaHeader(parsed, &has_intermediary);
418 }
419 EXPECT_EQ(tests[i].expected_result, has_chrome_proxy_via_header);
420 if (has_chrome_proxy_via_header && !tests[i].ignore_intermediary) {
421 EXPECT_EQ(tests[i].expected_has_intermediary, has_intermediary);
422 }
423 } 275 }
424 } 276 }
425 277
426 TEST_F(DataReductionProxyHeadersTest, GetDataReductionProxyBypassEventType) { 278 TEST_F(DataReductionProxyHeadersTest, GetDataReductionProxyBypassEventType) {
427 const struct { 279 const struct {
428 const char* headers; 280 const char* headers;
429 net::ProxyService::DataReductionProxyBypassType expected_result; 281 net::ProxyService::DataReductionProxyBypassType expected_result;
430 } tests[] = { 282 } tests[] = {
431 { "HTTP/1.1 200 OK\n" 283 { "HTTP/1.1 200 OK\n"
432 "Chrome-Proxy: bypass=0\n" 284 "Chrome-Proxy: bypass=0\n"
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
522 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { 374 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
523 std::string headers(tests[i].headers); 375 std::string headers(tests[i].headers);
524 HeadersToRaw(&headers); 376 HeadersToRaw(&headers);
525 scoped_refptr<net::HttpResponseHeaders> parsed( 377 scoped_refptr<net::HttpResponseHeaders> parsed(
526 new net::HttpResponseHeaders(headers)); 378 new net::HttpResponseHeaders(headers));
527 DataReductionProxyInfo chrome_proxy_info; 379 DataReductionProxyInfo chrome_proxy_info;
528 EXPECT_EQ(tests[i].expected_result, 380 EXPECT_EQ(tests[i].expected_result,
529 GetDataReductionProxyBypassType(parsed, &chrome_proxy_info)); 381 GetDataReductionProxyBypassType(parsed, &chrome_proxy_info));
530 } 382 }
531 } 383 }
532
533 TEST_F(DataReductionProxyHeadersTest,
534 GetDataReductionProxyActionFingerprintChromeProxy) {
535 const struct {
536 std::string label;
537 const char* headers;
538 bool expected_fingerprint_exist;
539 std::string expected_fingerprint;
540 } tests[] = {
541 { "fingerprint doesn't exist",
542 "HTTP/1.1 200 OK\n"
543 "Chrome-Proxy: bypass=0\n",
544 false,
545 "",
546 },
547 { "fingerprint occurs once",
548 "HTTP/1.1 200 OK\n"
549 "Chrome-Proxy: bypass=1, fcp=fp\n",
550 true,
551 "fp",
552 },
553 { "fingerprint occurs twice",
554 "HTTP/1.1 200 OK\n"
555 "Chrome-Proxy: bypass=2, fcp=fp1, fcp=fp2\n",
556 true,
557 "fp1",
558 },
559 };
560 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
561 std::string headers(tests[i].headers);
562 HeadersToRaw(&headers);
563 scoped_refptr<net::HttpResponseHeaders> parsed(
564 new net::HttpResponseHeaders(headers));
565
566 std::string fingerprint;
567 bool fingerprint_exist = GetDataReductionProxyActionFingerprintChromeProxy(
568 parsed, &fingerprint);
569 EXPECT_EQ(tests[i].expected_fingerprint_exist, fingerprint_exist)
570 << tests[i].label;
571
572 if (fingerprint_exist)
573 EXPECT_EQ(tests[i].expected_fingerprint, fingerprint) << tests[i].label;
574 }
575 }
576
577 TEST_F(DataReductionProxyHeadersTest,
578 GetDataReductionProxyActionFingerprintVia) {
579 const struct {
580 std::string label;
581 const char* headers;
582 bool expected_fingerprint_exist;
583 std::string expected_fingerprint;
584 } tests[] = {
585 { "fingerprint doesn't exist",
586 "HTTP/1.1 200 OK\n"
587 "Chrome-Proxy: bypass=0\n",
588 false,
589 "",
590 },
591 { "fingerprint occurs once",
592 "HTTP/1.1 200 OK\n"
593 "Chrome-Proxy: bypass=1, fvia=fvia\n",
594 true,
595 "fvia",
596 },
597 { "fingerprint occurs twice",
598 "HTTP/1.1 200 OK\n"
599 "Chrome-Proxy: bypass=2, fvia=fvia1, fvia=fvia2\n",
600 true,
601 "fvia1",
602 },
603 };
604 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
605 std::string headers(tests[i].headers);
606 HeadersToRaw(&headers);
607 scoped_refptr<net::HttpResponseHeaders> parsed(
608 new net::HttpResponseHeaders(headers));
609
610 std::string fingerprint;
611 bool fingerprint_exist =
612 GetDataReductionProxyActionFingerprintVia(parsed, &fingerprint);
613 EXPECT_EQ(tests[i].expected_fingerprint_exist, fingerprint_exist)
614 << tests[i].label;
615
616 if (fingerprint_exist)
617 EXPECT_EQ(tests[i].expected_fingerprint, fingerprint) << tests[i].label;
618 }
619 }
620
621 TEST_F(DataReductionProxyHeadersTest,
622 GetDataReductionProxyActionFingerprintOtherHeaders) {
623 const struct {
624 std::string label;
625 const char* headers;
626 bool expected_fingerprint_exist;
627 std::string expected_fingerprint;
628 } tests[] = {
629 { "fingerprint doesn't exist",
630 "HTTP/1.1 200 OK\n"
631 "Chrome-Proxy: bypass=0\n",
632 false,
633 "",
634 },
635 { "fingerprint occurs once",
636 "HTTP/1.1 200 OK\n"
637 "Chrome-Proxy: bypass=1, foh=foh\n",
638 true,
639 "foh",
640 },
641 { "fingerprint occurs twice",
642 "HTTP/1.1 200 OK\n"
643 "Chrome-Proxy: bypass=2, foh=foh1, foh=foh2\n",
644 true,
645 "foh1",
646 },
647 };
648 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
649 std::string headers(tests[i].headers);
650 HeadersToRaw(&headers);
651 scoped_refptr<net::HttpResponseHeaders> parsed(
652 new net::HttpResponseHeaders(headers));
653
654 std::string fingerprint;
655 bool fingerprint_exist =
656 GetDataReductionProxyActionFingerprintOtherHeaders(
657 parsed, &fingerprint);
658 EXPECT_EQ(tests[i].expected_fingerprint_exist, fingerprint_exist)
659 << tests[i].label;
660
661 if (fingerprint_exist)
662 EXPECT_EQ(tests[i].expected_fingerprint, fingerprint) << tests[i].label;
663 }
664 }
665
666 TEST_F(DataReductionProxyHeadersTest,
667 GetDataReductionProxyActionFingerprintContentLength) {
668 const struct {
669 std::string label;
670 const char* headers;
671 bool expected_fingerprint_exist;
672 std::string expected_fingerprint;
673 } tests[] = {
674 { "fingerprint doesn't exist",
675 "HTTP/1.1 200 OK\n"
676 "Chrome-Proxy: bypass=0\n",
677 false,
678 "",
679 },
680 { "fingerprint occurs once",
681 "HTTP/1.1 200 OK\n"
682 "Chrome-Proxy: bypass=1, fcl=fcl\n",
683 true,
684 "fcl",
685 },
686 { "fingerprint occurs twice",
687 "HTTP/1.1 200 OK\n"
688 "Chrome-Proxy: bypass=2, fcl=fcl1, fcl=fcl2\n",
689 true,
690 "fcl1",
691 },
692 };
693 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
694 std::string headers(tests[i].headers);
695 HeadersToRaw(&headers);
696 scoped_refptr<net::HttpResponseHeaders> parsed(
697 new net::HttpResponseHeaders(headers));
698
699 std::string fingerprint;
700 bool fingerprint_exist =
701 GetDataReductionProxyActionFingerprintContentLength(
702 parsed, &fingerprint);
703 EXPECT_EQ(tests[i].expected_fingerprint_exist, fingerprint_exist)
704 << tests[i].label;
705
706 if (fingerprint_exist)
707 EXPECT_EQ(tests[i].expected_fingerprint, fingerprint) << tests[i].label;
708 }
709 }
710
711 TEST_F(DataReductionProxyHeadersTest,
712 GetDataReductionProxyHeaderWithFingerprintRemoved) {
713 struct {
714 std::string label;
715 const char* headers;
716 std::string expected_output_values_string;
717 } test[] = {
718 {
719 "Checks the case that there is no Chrome-Proxy header's fingerprint.",
720 "HTTP/1.1 200 OK\n"
721 "Chrome-Proxy: 1,2,3,5\n",
722 "1,2,3,5,",
723 },
724 {
725 "Checks the case that there is Chrome-Proxy header's fingerprint.",
726 "HTTP/1.1 200 OK\n"
727 "Chrome-Proxy: 1,2,3,fcp=4,5\n",
728 "1,2,3,5,",
729 },
730 {
731 "Checks the case that there is Chrome-Proxy header's fingerprint, and it"
732 "occurs at the end.",
733 "HTTP/1.1 200 OK\n"
734 "Chrome-Proxy: 1,2,3,fcp=4,",
735 "1,2,3,",
736 },
737 {
738 "Checks the case that there is Chrome-Proxy header's fingerprint, and it"
739 "occurs at the beginning.",
740 "HTTP/1.1 200 OK\n"
741 "Chrome-Proxy: fcp=1,2,3,",
742 "2,3,",
743 },
744 };
745
746 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test); ++i) {
747 std::string headers(test[i].headers);
748 HeadersToRaw(&headers);
749 scoped_refptr<net::HttpResponseHeaders> parsed(
750 new net::HttpResponseHeaders(headers));
751
752 std::vector<std::string> output_values;
753 GetDataReductionProxyHeaderWithFingerprintRemoved(parsed, &output_values);
754
755 std::string output_values_string;
756 for (size_t j = 0; j < output_values.size(); ++j)
757 output_values_string += output_values[j] + ",";
758
759 EXPECT_EQ(test[i].expected_output_values_string, output_values_string)
760 << test[i].label;
761 }
762 }
763
764 } // namespace data_reduction_proxy 384 } // namespace data_reduction_proxy
OLDNEW
« no previous file with comments | « components/data_reduction_proxy/common/data_reduction_proxy_headers.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698