OLD | NEW |
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/core/browser/data_reduction_proxy_tamp
er_detection.h" | 5 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_tamp
er_detection.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <algorithm> | 10 #include <algorithm> |
(...skipping 22 matching lines...) Expand all Loading... |
33 | 33 |
34 namespace { | 34 namespace { |
35 | 35 |
36 // Calcuates MD5 hash value for a string and then base64 encode it. Testcases | 36 // Calcuates MD5 hash value for a string and then base64 encode it. Testcases |
37 // contain expected fingerprint in plain text, which needs to be encoded before | 37 // contain expected fingerprint in plain text, which needs to be encoded before |
38 // comparison. | 38 // comparison. |
39 std::string GetEncoded(const std::string& input) { | 39 std::string GetEncoded(const std::string& input) { |
40 base::MD5Digest digest; | 40 base::MD5Digest digest; |
41 base::MD5Sum(input.c_str(), input.size(), &digest); | 41 base::MD5Sum(input.c_str(), input.size(), &digest); |
42 std::string base64encoded; | 42 std::string base64encoded; |
43 base::Base64Encode(std::string((char*)digest.a, arraysize(digest.a)), | 43 base::Base64Encode( |
44 &base64encoded); | 44 std::string(reinterpret_cast<char*>(digest.a), arraysize(digest.a)), |
| 45 &base64encoded); |
45 return base64encoded; | 46 return base64encoded; |
46 } | 47 } |
47 | 48 |
48 // Replaces all contents within "[]" by corresponding base64 encoded MD5 value. | 49 // Replaces all contents within "[]" by corresponding base64 encoded MD5 value. |
49 // It can handle nested case like: [[abc]def]. This helper function transforms | 50 // It can handle nested case like: [[abc]def]. This helper function transforms |
50 // fingerprint in plain text to actual encoded fingerprint. | 51 // fingerprint in plain text to actual encoded fingerprint. |
51 void ReplaceWithEncodedString(std::string* input) { | 52 void ReplaceWithEncodedString(std::string* input) { |
52 size_t start, end, temp; | 53 size_t start, end, temp; |
53 while (true) { | 54 while (true) { |
54 start = input->find("["); | 55 start = input->find("["); |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 net::android::RegisterNetworkLibrary(env); | 92 net::android::RegisterNetworkLibrary(env); |
92 inited = true; | 93 inited = true; |
93 } | 94 } |
94 #endif | 95 #endif |
95 } | 96 } |
96 | 97 |
97 } // namespace | 98 } // namespace |
98 | 99 |
99 namespace data_reduction_proxy { | 100 namespace data_reduction_proxy { |
100 | 101 |
101 class DataReductionProxyTamperDetectionTest : public testing::Test { | 102 using DataReductionProxyTamperDetectionTest = testing::Test; |
102 | |
103 }; | |
104 | 103 |
105 // Tests function ValidateChromeProxyHeader. | 104 // Tests function ValidateChromeProxyHeader. |
106 TEST_F(DataReductionProxyTamperDetectionTest, ChromeProxy) { | 105 TEST_F(DataReductionProxyTamperDetectionTest, ChromeProxy) { |
107 // |received_fingerprint| is not the actual fingerprint from Data Reduction | 106 // |received_fingerprint| is not the actual fingerprint from Data Reduction |
108 // Proxy, instead, the base64 encoded field is in plain text (within "[]") | 107 // Proxy, instead, the base64 encoded field is in plain text (within "[]") |
109 // and needs to be encoded first. | 108 // and needs to be encoded first. |
110 struct { | 109 struct { |
111 std::string label; | 110 std::string label; |
112 std::string raw_header; | 111 std::string raw_header; |
113 std::string received_fingerprint; | 112 std::string received_fingerprint; |
(...skipping 588 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
702 | 701 |
703 // Tests function ReportUMAForContentLength, with the focus on compression | 702 // Tests function ReportUMAForContentLength, with the focus on compression |
704 // ratio. | 703 // ratio. |
705 TEST_F(DataReductionProxyTamperDetectionTest, CompressionRatio) { | 704 TEST_F(DataReductionProxyTamperDetectionTest, CompressionRatio) { |
706 struct { | 705 struct { |
707 std::string raw_header; | 706 std::string raw_header; |
708 std::string histogram_name_suffix; | 707 std::string histogram_name_suffix; |
709 int original_content_length; | 708 int original_content_length; |
710 int content_length; | 709 int content_length; |
711 int compression_ratio; | 710 int compression_ratio; |
712 ; | |
713 } tests[] = { | 711 } tests[] = { |
714 // Checks the correctness of histogram for Video | 712 // Checks the correctness of histogram for Video |
715 {"HTTP/1.1 200 OK\n" | 713 {"HTTP/1.1 200 OK\n" |
716 "Content-Type: video/webm\n", | 714 "Content-Type: video/webm\n", |
717 "_Video", 1000, 800, 80}, | 715 "_Video", 1000, 800, 80}, |
718 // Checks the correctness of histogram for JPEG | 716 // Checks the correctness of histogram for JPEG |
719 {"HTTP/1.1 200 OK\n" | 717 {"HTTP/1.1 200 OK\n" |
720 "Content-Type: image/jpg\n", | 718 "Content-Type: image/jpg\n", |
721 "_Image_JPG", 1000, 1, 0}, | 719 "_Image_JPG", 1000, 1, 0}, |
722 // Checks the correctness of histogram for PNG | 720 // Checks the correctness of histogram for PNG |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
840 scoped_refptr<net::HttpResponseHeaders> headers( | 838 scoped_refptr<net::HttpResponseHeaders> headers( |
841 new net::HttpResponseHeaders(raw_headers)); | 839 new net::HttpResponseHeaders(raw_headers)); |
842 | 840 |
843 EXPECT_EQ( | 841 EXPECT_EQ( |
844 test[i].expected_tampered_with, | 842 test[i].expected_tampered_with, |
845 DataReductionProxyTamperDetection::DetectAndReport( | 843 DataReductionProxyTamperDetection::DetectAndReport( |
846 headers.get(), true, test[i].content_length)) << test[i].label; | 844 headers.get(), true, test[i].content_length)) << test[i].label; |
847 } | 845 } |
848 } | 846 } |
849 | 847 |
850 } // namespace | 848 } // namespace data_reduction_proxy |
OLD | NEW |