OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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 "net/spdy/spdy_test_utils.h" | |
6 | |
7 #include <algorithm> | |
8 #include <cstring> | |
9 #include <memory> | |
10 #include <utility> | |
11 #include <vector> | |
12 | |
13 #include "base/base64.h" | |
14 #include "base/logging.h" | |
15 #include "base/strings/string_number_conversions.h" | |
16 #include "base/sys_byteorder.h" | |
17 #include "net/http/transport_security_state.h" | |
18 #include "net/spdy/spdy_flags.h" | |
19 #include "net/ssl/ssl_info.h" | |
20 #include "testing/gtest/include/gtest/gtest.h" | |
21 | |
22 namespace net { | |
23 namespace test { | |
24 | |
25 SpdyString HexDumpWithMarks(const unsigned char* data, | |
26 int length, | |
27 const bool* marks, | |
28 int mark_length) { | |
29 static const char kHexChars[] = "0123456789abcdef"; | |
30 static const int kColumns = 4; | |
31 | |
32 const int kSizeLimit = 1024; | |
33 if (length > kSizeLimit || mark_length > kSizeLimit) { | |
34 LOG(ERROR) << "Only dumping first " << kSizeLimit << " bytes."; | |
35 length = std::min(length, kSizeLimit); | |
36 mark_length = std::min(mark_length, kSizeLimit); | |
37 } | |
38 | |
39 SpdyString hex; | |
40 for (const unsigned char* row = data; length > 0; | |
41 row += kColumns, length -= kColumns) { | |
42 for (const unsigned char *p = row; p < row + 4; ++p) { | |
43 if (p < row + length) { | |
44 const bool mark = | |
45 (marks && (p - data) < mark_length && marks[p - data]); | |
46 hex += mark ? '*' : ' '; | |
47 hex += kHexChars[(*p & 0xf0) >> 4]; | |
48 hex += kHexChars[*p & 0x0f]; | |
49 hex += mark ? '*' : ' '; | |
50 } else { | |
51 hex += " "; | |
52 } | |
53 } | |
54 hex = hex + " "; | |
55 | |
56 for (const unsigned char* p = row; p < row + 4 && p < row + length; ++p) { | |
57 hex += (*p >= 0x20 && *p <= 0x7f) ? (*p) : '.'; | |
58 } | |
59 | |
60 hex = hex + '\n'; | |
61 } | |
62 return hex; | |
63 } | |
64 | |
65 void CompareCharArraysWithHexError(const SpdyString& description, | |
66 const unsigned char* actual, | |
67 const int actual_len, | |
68 const unsigned char* expected, | |
69 const int expected_len) { | |
70 const int min_len = std::min(actual_len, expected_len); | |
71 const int max_len = std::max(actual_len, expected_len); | |
72 std::unique_ptr<bool[]> marks(new bool[max_len]); | |
73 bool identical = (actual_len == expected_len); | |
74 for (int i = 0; i < min_len; ++i) { | |
75 if (actual[i] != expected[i]) { | |
76 marks[i] = true; | |
77 identical = false; | |
78 } else { | |
79 marks[i] = false; | |
80 } | |
81 } | |
82 for (int i = min_len; i < max_len; ++i) { | |
83 marks[i] = true; | |
84 } | |
85 if (identical) return; | |
86 ADD_FAILURE() | |
87 << "Description:\n" | |
88 << description | |
89 << "\n\nExpected:\n" | |
90 << HexDumpWithMarks(expected, expected_len, marks.get(), max_len) | |
91 << "\nActual:\n" | |
92 << HexDumpWithMarks(actual, actual_len, marks.get(), max_len); | |
93 } | |
94 | |
95 void SetFrameFlags(SpdySerializedFrame* frame, uint8_t flags) { | |
96 frame->data()[4] = flags; | |
97 } | |
98 | |
99 void SetFrameLength(SpdySerializedFrame* frame, size_t length) { | |
100 CHECK_GT(1u << 14, length); | |
101 { | |
102 int32_t wire_length = base::HostToNet32(length); | |
103 memcpy(frame->data(), reinterpret_cast<char*>(&wire_length) + 1, 3); | |
104 } | |
105 } | |
106 | |
107 SpdyString a2b_hex(const char* hex_data) { | |
108 std::vector<uint8_t> output; | |
109 SpdyString result; | |
110 if (base::HexStringToBytes(hex_data, &output)) | |
111 result.assign(reinterpret_cast<const char*>(&output[0]), output.size()); | |
112 return result; | |
113 } | |
114 | |
115 HashValue GetTestHashValue(uint8_t label) { | |
116 HashValue hash_value(HASH_VALUE_SHA256); | |
117 memset(hash_value.data(), label, hash_value.size()); | |
118 return hash_value; | |
119 } | |
120 | |
121 SpdyString GetTestPin(uint8_t label) { | |
122 HashValue hash_value = GetTestHashValue(label); | |
123 SpdyString base64; | |
124 base::Base64Encode(SpdyStringPiece(reinterpret_cast<char*>(hash_value.data()), | |
125 hash_value.size()), | |
126 &base64); | |
127 | |
128 return SpdyString("pin-sha256=\"") + base64 + "\""; | |
129 } | |
130 | |
131 void AddPin(TransportSecurityState* state, | |
132 const SpdyString& host, | |
133 uint8_t primary_label, | |
134 uint8_t backup_label) { | |
135 SpdyString primary_pin = GetTestPin(primary_label); | |
136 SpdyString backup_pin = GetTestPin(backup_label); | |
137 SpdyString header = "max-age = 10000; " + primary_pin + "; " + backup_pin; | |
138 | |
139 // Construct a fake SSLInfo that will pass AddHPKPHeader's checks. | |
140 SSLInfo ssl_info; | |
141 ssl_info.is_issued_by_known_root = true; | |
142 ssl_info.public_key_hashes.push_back(GetTestHashValue(primary_label)); | |
143 EXPECT_TRUE(state->AddHPKPHeader(host, header, ssl_info)); | |
144 } | |
145 | |
146 void TestHeadersHandler::OnHeaderBlockStart() { | |
147 block_.clear(); | |
148 } | |
149 | |
150 void TestHeadersHandler::OnHeader(SpdyStringPiece name, SpdyStringPiece value) { | |
151 block_.AppendValueOrAddHeader(name, value); | |
152 } | |
153 | |
154 void TestHeadersHandler::OnHeaderBlockEnd(size_t header_bytes_parsed) { | |
155 OnHeaderBlockEnd(header_bytes_parsed, 0); | |
156 } | |
157 | |
158 void TestHeadersHandler::OnHeaderBlockEnd( | |
159 size_t header_bytes_parsed, | |
160 size_t compressed_header_bytes_parsed) { | |
161 header_bytes_parsed_ = header_bytes_parsed; | |
162 compressed_header_bytes_parsed_ = compressed_header_bytes_parsed; | |
163 } | |
164 | |
165 TestServerPushDelegate::TestServerPushDelegate() {} | |
166 | |
167 TestServerPushDelegate::~TestServerPushDelegate() {} | |
168 | |
169 void TestServerPushDelegate::OnPush( | |
170 std::unique_ptr<ServerPushHelper> push_helper, | |
171 const NetLogWithSource& session_net_log) { | |
172 push_helpers[push_helper->GetURL()] = std::move(push_helper); | |
173 } | |
174 | |
175 bool TestServerPushDelegate::CancelPush(GURL url) { | |
176 auto itr = push_helpers.find(url); | |
177 DCHECK(itr != push_helpers.end()); | |
178 itr->second->Cancel(); | |
179 push_helpers.erase(itr); | |
180 return true; | |
181 } | |
182 | |
183 } // namespace test | |
184 } // namespace net | |
OLD | NEW |