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

Side by Side Diff: net/spdy/spdy_test_utils.cc

Issue 2801603003: Add SpdyString alias for std::string in net/spdy. (Closed)
Patch Set: Created 3 years, 8 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 | « net/spdy/spdy_test_utils.h ('k') | net/spdy/spdy_write_queue_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 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 "net/spdy/spdy_test_utils.h" 5 #include "net/spdy/spdy_test_utils.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <cstring> 8 #include <cstring>
9 #include <memory> 9 #include <memory>
10 #include <utility> 10 #include <utility>
11 #include <vector> 11 #include <vector>
12 12
13 #include "base/base64.h" 13 #include "base/base64.h"
14 #include "base/logging.h" 14 #include "base/logging.h"
15 #include "base/strings/string_number_conversions.h" 15 #include "base/strings/string_number_conversions.h"
16 #include "base/sys_byteorder.h" 16 #include "base/sys_byteorder.h"
17 #include "net/http/transport_security_state.h" 17 #include "net/http/transport_security_state.h"
18 #include "net/spdy/spdy_flags.h" 18 #include "net/spdy/spdy_flags.h"
19 #include "net/ssl/ssl_info.h" 19 #include "net/ssl/ssl_info.h"
20 #include "testing/gtest/include/gtest/gtest.h" 20 #include "testing/gtest/include/gtest/gtest.h"
21 21
22 namespace net { 22 namespace net {
23 namespace test { 23 namespace test {
24 24
25 using std::string; 25 SpdyString HexDumpWithMarks(const unsigned char* data,
26 26 int length,
27 string HexDumpWithMarks(const unsigned char* data, 27 const bool* marks,
28 int length, 28 int mark_length) {
29 const bool* marks,
30 int mark_length) {
31 static const char kHexChars[] = "0123456789abcdef"; 29 static const char kHexChars[] = "0123456789abcdef";
32 static const int kColumns = 4; 30 static const int kColumns = 4;
33 31
34 const int kSizeLimit = 1024; 32 const int kSizeLimit = 1024;
35 if (length > kSizeLimit || mark_length > kSizeLimit) { 33 if (length > kSizeLimit || mark_length > kSizeLimit) {
36 LOG(ERROR) << "Only dumping first " << kSizeLimit << " bytes."; 34 LOG(ERROR) << "Only dumping first " << kSizeLimit << " bytes.";
37 length = std::min(length, kSizeLimit); 35 length = std::min(length, kSizeLimit);
38 mark_length = std::min(mark_length, kSizeLimit); 36 mark_length = std::min(mark_length, kSizeLimit);
39 } 37 }
40 38
41 string hex; 39 SpdyString hex;
42 for (const unsigned char* row = data; length > 0; 40 for (const unsigned char* row = data; length > 0;
43 row += kColumns, length -= kColumns) { 41 row += kColumns, length -= kColumns) {
44 for (const unsigned char *p = row; p < row + 4; ++p) { 42 for (const unsigned char *p = row; p < row + 4; ++p) {
45 if (p < row + length) { 43 if (p < row + length) {
46 const bool mark = 44 const bool mark =
47 (marks && (p - data) < mark_length && marks[p - data]); 45 (marks && (p - data) < mark_length && marks[p - data]);
48 hex += mark ? '*' : ' '; 46 hex += mark ? '*' : ' ';
49 hex += kHexChars[(*p & 0xf0) >> 4]; 47 hex += kHexChars[(*p & 0xf0) >> 4];
50 hex += kHexChars[*p & 0x0f]; 48 hex += kHexChars[*p & 0x0f];
51 hex += mark ? '*' : ' '; 49 hex += mark ? '*' : ' ';
52 } else { 50 } else {
53 hex += " "; 51 hex += " ";
54 } 52 }
55 } 53 }
56 hex = hex + " "; 54 hex = hex + " ";
57 55
58 for (const unsigned char* p = row; p < row + 4 && p < row + length; ++p) { 56 for (const unsigned char* p = row; p < row + 4 && p < row + length; ++p) {
59 hex += (*p >= 0x20 && *p <= 0x7f) ? (*p) : '.'; 57 hex += (*p >= 0x20 && *p <= 0x7f) ? (*p) : '.';
60 } 58 }
61 59
62 hex = hex + '\n'; 60 hex = hex + '\n';
63 } 61 }
64 return hex; 62 return hex;
65 } 63 }
66 64
67 void CompareCharArraysWithHexError(const string& description, 65 void CompareCharArraysWithHexError(const SpdyString& description,
68 const unsigned char* actual, 66 const unsigned char* actual,
69 const int actual_len, 67 const int actual_len,
70 const unsigned char* expected, 68 const unsigned char* expected,
71 const int expected_len) { 69 const int expected_len) {
72 const int min_len = std::min(actual_len, expected_len); 70 const int min_len = std::min(actual_len, expected_len);
73 const int max_len = std::max(actual_len, expected_len); 71 const int max_len = std::max(actual_len, expected_len);
74 std::unique_ptr<bool[]> marks(new bool[max_len]); 72 std::unique_ptr<bool[]> marks(new bool[max_len]);
75 bool identical = (actual_len == expected_len); 73 bool identical = (actual_len == expected_len);
76 for (int i = 0; i < min_len; ++i) { 74 for (int i = 0; i < min_len; ++i) {
77 if (actual[i] != expected[i]) { 75 if (actual[i] != expected[i]) {
(...skipping 21 matching lines...) Expand all
99 } 97 }
100 98
101 void SetFrameLength(SpdySerializedFrame* frame, size_t length) { 99 void SetFrameLength(SpdySerializedFrame* frame, size_t length) {
102 CHECK_GT(1u << 14, length); 100 CHECK_GT(1u << 14, length);
103 { 101 {
104 int32_t wire_length = base::HostToNet32(length); 102 int32_t wire_length = base::HostToNet32(length);
105 memcpy(frame->data(), reinterpret_cast<char*>(&wire_length) + 1, 3); 103 memcpy(frame->data(), reinterpret_cast<char*>(&wire_length) + 1, 3);
106 } 104 }
107 } 105 }
108 106
109 string a2b_hex(const char* hex_data) { 107 SpdyString a2b_hex(const char* hex_data) {
110 std::vector<uint8_t> output; 108 std::vector<uint8_t> output;
111 string result; 109 SpdyString result;
112 if (base::HexStringToBytes(hex_data, &output)) 110 if (base::HexStringToBytes(hex_data, &output))
113 result.assign(reinterpret_cast<const char*>(&output[0]), output.size()); 111 result.assign(reinterpret_cast<const char*>(&output[0]), output.size());
114 return result; 112 return result;
115 } 113 }
116 114
117 HashValue GetTestHashValue(uint8_t label) { 115 HashValue GetTestHashValue(uint8_t label) {
118 HashValue hash_value(HASH_VALUE_SHA256); 116 HashValue hash_value(HASH_VALUE_SHA256);
119 memset(hash_value.data(), label, hash_value.size()); 117 memset(hash_value.data(), label, hash_value.size());
120 return hash_value; 118 return hash_value;
121 } 119 }
122 120
123 string GetTestPin(uint8_t label) { 121 SpdyString GetTestPin(uint8_t label) {
124 HashValue hash_value = GetTestHashValue(label); 122 HashValue hash_value = GetTestHashValue(label);
125 string base64; 123 SpdyString base64;
126 base::Base64Encode(SpdyStringPiece(reinterpret_cast<char*>(hash_value.data()), 124 base::Base64Encode(SpdyStringPiece(reinterpret_cast<char*>(hash_value.data()),
127 hash_value.size()), 125 hash_value.size()),
128 &base64); 126 &base64);
129 127
130 return string("pin-sha256=\"") + base64 + "\""; 128 return SpdyString("pin-sha256=\"") + base64 + "\"";
131 } 129 }
132 130
133 void AddPin(TransportSecurityState* state, 131 void AddPin(TransportSecurityState* state,
134 const string& host, 132 const SpdyString& host,
135 uint8_t primary_label, 133 uint8_t primary_label,
136 uint8_t backup_label) { 134 uint8_t backup_label) {
137 string primary_pin = GetTestPin(primary_label); 135 SpdyString primary_pin = GetTestPin(primary_label);
138 string backup_pin = GetTestPin(backup_label); 136 SpdyString backup_pin = GetTestPin(backup_label);
139 string header = "max-age = 10000; " + primary_pin + "; " + backup_pin; 137 SpdyString header = "max-age = 10000; " + primary_pin + "; " + backup_pin;
140 138
141 // Construct a fake SSLInfo that will pass AddHPKPHeader's checks. 139 // Construct a fake SSLInfo that will pass AddHPKPHeader's checks.
142 SSLInfo ssl_info; 140 SSLInfo ssl_info;
143 ssl_info.is_issued_by_known_root = true; 141 ssl_info.is_issued_by_known_root = true;
144 ssl_info.public_key_hashes.push_back(GetTestHashValue(primary_label)); 142 ssl_info.public_key_hashes.push_back(GetTestHashValue(primary_label));
145 EXPECT_TRUE(state->AddHPKPHeader(host, header, ssl_info)); 143 EXPECT_TRUE(state->AddHPKPHeader(host, header, ssl_info));
146 } 144 }
147 145
148 void TestHeadersHandler::OnHeaderBlockStart() { 146 void TestHeadersHandler::OnHeaderBlockStart() {
149 block_.clear(); 147 block_.clear();
(...skipping 26 matching lines...) Expand all
176 bool TestServerPushDelegate::CancelPush(GURL url) { 174 bool TestServerPushDelegate::CancelPush(GURL url) {
177 auto itr = push_helpers.find(url); 175 auto itr = push_helpers.find(url);
178 DCHECK(itr != push_helpers.end()); 176 DCHECK(itr != push_helpers.end());
179 itr->second->Cancel(); 177 itr->second->Cancel();
180 push_helpers.erase(itr); 178 push_helpers.erase(itr);
181 return true; 179 return true;
182 } 180 }
183 181
184 } // namespace test 182 } // namespace test
185 } // namespace net 183 } // namespace net
OLDNEW
« no previous file with comments | « net/spdy/spdy_test_utils.h ('k') | net/spdy/spdy_write_queue_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698