Revert "Fix broken channel icon in chrome://help on CrOS" and try again
[chromium-blink-merge.git] / net / spdy / spdy_test_utils.cc
blobeb721ef32c0348a90e1e173c6b52435488ed3f31
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.
5 #include "net/spdy/spdy_test_utils.h"
7 #include <cstring>
8 #include <vector>
10 #include "base/base64.h"
11 #include "base/logging.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/strings/string_number_conversions.h"
14 #include "base/sys_byteorder.h"
15 #include "net/http/transport_security_state.h"
16 #include "net/ssl/ssl_info.h"
17 #include "testing/gtest/include/gtest/gtest.h"
19 namespace net {
20 namespace test {
22 std::string HexDumpWithMarks(const unsigned char* data, int length,
23 const bool* marks, int mark_length) {
24 static const char kHexChars[] = "0123456789abcdef";
25 static const int kColumns = 4;
27 const int kSizeLimit = 1024;
28 if (length > kSizeLimit || mark_length > kSizeLimit) {
29 LOG(ERROR) << "Only dumping first " << kSizeLimit << " bytes.";
30 length = std::min(length, kSizeLimit);
31 mark_length = std::min(mark_length, kSizeLimit);
34 std::string hex;
35 for (const unsigned char* row = data; length > 0;
36 row += kColumns, length -= kColumns) {
37 for (const unsigned char *p = row; p < row + 4; ++p) {
38 if (p < row + length) {
39 const bool mark =
40 (marks && (p - data) < mark_length && marks[p - data]);
41 hex += mark ? '*' : ' ';
42 hex += kHexChars[(*p & 0xf0) >> 4];
43 hex += kHexChars[*p & 0x0f];
44 hex += mark ? '*' : ' ';
45 } else {
46 hex += " ";
49 hex = hex + " ";
51 for (const unsigned char *p = row; p < row + 4 && p < row + length; ++p)
52 hex += (*p >= 0x20 && *p <= 0x7f) ? (*p) : '.';
54 hex = hex + '\n';
56 return hex;
59 void CompareCharArraysWithHexError(
60 const std::string& description,
61 const unsigned char* actual,
62 const int actual_len,
63 const unsigned char* expected,
64 const int expected_len) {
65 const int min_len = std::min(actual_len, expected_len);
66 const int max_len = std::max(actual_len, expected_len);
67 scoped_ptr<bool[]> marks(new bool[max_len]);
68 bool identical = (actual_len == expected_len);
69 for (int i = 0; i < min_len; ++i) {
70 if (actual[i] != expected[i]) {
71 marks[i] = true;
72 identical = false;
73 } else {
74 marks[i] = false;
77 for (int i = min_len; i < max_len; ++i) {
78 marks[i] = true;
80 if (identical) return;
81 ADD_FAILURE()
82 << "Description:\n"
83 << description
84 << "\n\nExpected:\n"
85 << HexDumpWithMarks(expected, expected_len, marks.get(), max_len)
86 << "\nActual:\n"
87 << HexDumpWithMarks(actual, actual_len, marks.get(), max_len);
90 void SetFrameFlags(SpdyFrame* frame,
91 uint8 flags,
92 SpdyMajorVersion spdy_version) {
93 switch (spdy_version) {
94 case SPDY2:
95 case SPDY3:
96 case HTTP2:
97 frame->data()[4] = flags;
98 break;
99 default:
100 LOG(FATAL) << "Unsupported SPDY version.";
104 void SetFrameLength(SpdyFrame* frame,
105 size_t length,
106 SpdyMajorVersion spdy_version) {
107 switch (spdy_version) {
108 case SPDY2:
109 case SPDY3:
110 CHECK_EQ(0u, length & ~kLengthMask);
112 int32 wire_length = base::HostToNet32(length);
113 // The length field in SPDY 2 and 3 is a 24-bit (3B) integer starting at
114 // offset 5.
115 memcpy(frame->data() + 5, reinterpret_cast<char*>(&wire_length) + 1, 3);
117 break;
118 case HTTP2:
119 CHECK_GT(1u<<14, length);
121 int32 wire_length = base::HostToNet32(length);
122 memcpy(frame->data(),
123 reinterpret_cast<char*>(&wire_length) + 1,
126 break;
127 default:
128 LOG(FATAL) << "Unsupported SPDY version.";
132 bool CompareSpdyHeaderBlocks(const SpdyHeaderBlock& a,
133 const SpdyHeaderBlock& b) {
134 return a.size() == b.size() && std::equal(a.begin(), a.end(), b.begin());
137 std::string a2b_hex(const char* hex_data) {
138 std::vector<uint8> output;
139 std::string result;
140 if (base::HexStringToBytes(hex_data, &output))
141 result.assign(reinterpret_cast<const char*>(&output[0]), output.size());
142 return result;
145 HashValue GetTestHashValue(uint8_t label) {
146 HashValue hash_value(HASH_VALUE_SHA256);
147 memset(hash_value.data(), label, hash_value.size());
148 return hash_value;
151 std::string GetTestPin(uint8_t label) {
152 HashValue hash_value = GetTestHashValue(label);
153 std::string base64;
154 base::Base64Encode(base::StringPiece(
155 reinterpret_cast<char*>(hash_value.data()), hash_value.size()), &base64);
157 return std::string("pin-sha256=\"") + base64 + "\"";
160 void AddPin(TransportSecurityState* state,
161 const std::string& host,
162 uint8_t primary_label,
163 uint8_t backup_label) {
164 std::string primary_pin = GetTestPin(primary_label);
165 std::string backup_pin = GetTestPin(backup_label);
166 std::string header = "max-age = 10000; " + primary_pin + "; " + backup_pin;
168 // Construct a fake SSLInfo that will pass AddHPKPHeader's checks.
169 SSLInfo ssl_info;
170 ssl_info.is_issued_by_known_root = true;
171 ssl_info.public_key_hashes.push_back(GetTestHashValue(primary_label));
172 EXPECT_TRUE(state->AddHPKPHeader(host, header, ssl_info));
175 } // namespace test
176 } // namespace net