OLD | NEW |
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 <cstring> | 7 #include <cstring> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
11 #include "base/sys_byteorder.h" | 11 #include "base/sys_byteorder.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
13 | 13 |
14 namespace net { | 14 namespace net { |
15 | 15 |
16 namespace test { | 16 namespace test { |
17 | 17 |
18 std::string HexDumpWithMarks(const unsigned char* data, int length, | 18 std::string HexDumpWithMarks(const unsigned char* data, |
19 const bool* marks, int mark_length) { | 19 int length, |
| 20 const bool* marks, |
| 21 int mark_length) { |
20 static const char kHexChars[] = "0123456789abcdef"; | 22 static const char kHexChars[] = "0123456789abcdef"; |
21 static const int kColumns = 4; | 23 static const int kColumns = 4; |
22 | 24 |
23 const int kSizeLimit = 1024; | 25 const int kSizeLimit = 1024; |
24 if (length > kSizeLimit || mark_length > kSizeLimit) { | 26 if (length > kSizeLimit || mark_length > kSizeLimit) { |
25 LOG(ERROR) << "Only dumping first " << kSizeLimit << " bytes."; | 27 LOG(ERROR) << "Only dumping first " << kSizeLimit << " bytes."; |
26 length = std::min(length, kSizeLimit); | 28 length = std::min(length, kSizeLimit); |
27 mark_length = std::min(mark_length, kSizeLimit); | 29 mark_length = std::min(mark_length, kSizeLimit); |
28 } | 30 } |
29 | 31 |
30 std::string hex; | 32 std::string hex; |
31 for (const unsigned char* row = data; length > 0; | 33 for (const unsigned char* row = data; length > 0; |
32 row += kColumns, length -= kColumns) { | 34 row += kColumns, length -= kColumns) { |
33 for (const unsigned char *p = row; p < row + 4; ++p) { | 35 for (const unsigned char* p = row; p < row + 4; ++p) { |
34 if (p < row + length) { | 36 if (p < row + length) { |
35 const bool mark = | 37 const bool mark = |
36 (marks && (p - data) < mark_length && marks[p - data]); | 38 (marks && (p - data) < mark_length && marks[p - data]); |
37 hex += mark ? '*' : ' '; | 39 hex += mark ? '*' : ' '; |
38 hex += kHexChars[(*p & 0xf0) >> 4]; | 40 hex += kHexChars[(*p & 0xf0) >> 4]; |
39 hex += kHexChars[*p & 0x0f]; | 41 hex += kHexChars[*p & 0x0f]; |
40 hex += mark ? '*' : ' '; | 42 hex += mark ? '*' : ' '; |
41 } else { | 43 } else { |
42 hex += " "; | 44 hex += " "; |
43 } | 45 } |
44 } | 46 } |
45 hex = hex + " "; | 47 hex = hex + " "; |
46 | 48 |
47 for (const unsigned char *p = row; p < row + 4 && p < row + length; ++p) | 49 for (const unsigned char* p = row; p < row + 4 && p < row + length; ++p) |
48 hex += (*p >= 0x20 && *p <= 0x7f) ? (*p) : '.'; | 50 hex += (*p >= 0x20 && *p <= 0x7f) ? (*p) : '.'; |
49 | 51 |
50 hex = hex + '\n'; | 52 hex = hex + '\n'; |
51 } | 53 } |
52 return hex; | 54 return hex; |
53 } | 55 } |
54 | 56 |
55 void CompareCharArraysWithHexError( | 57 void CompareCharArraysWithHexError(const std::string& description, |
56 const std::string& description, | 58 const unsigned char* actual, |
57 const unsigned char* actual, | 59 const int actual_len, |
58 const int actual_len, | 60 const unsigned char* expected, |
59 const unsigned char* expected, | 61 const int expected_len) { |
60 const int expected_len) { | |
61 const int min_len = std::min(actual_len, expected_len); | 62 const int min_len = std::min(actual_len, expected_len); |
62 const int max_len = std::max(actual_len, expected_len); | 63 const int max_len = std::max(actual_len, expected_len); |
63 scoped_ptr<bool[]> marks(new bool[max_len]); | 64 scoped_ptr<bool[]> marks(new bool[max_len]); |
64 bool identical = (actual_len == expected_len); | 65 bool identical = (actual_len == expected_len); |
65 for (int i = 0; i < min_len; ++i) { | 66 for (int i = 0; i < min_len; ++i) { |
66 if (actual[i] != expected[i]) { | 67 if (actual[i] != expected[i]) { |
67 marks[i] = true; | 68 marks[i] = true; |
68 identical = false; | 69 identical = false; |
69 } else { | 70 } else { |
70 marks[i] = false; | 71 marks[i] = false; |
71 } | 72 } |
72 } | 73 } |
73 for (int i = min_len; i < max_len; ++i) { | 74 for (int i = min_len; i < max_len; ++i) { |
74 marks[i] = true; | 75 marks[i] = true; |
75 } | 76 } |
76 if (identical) return; | 77 if (identical) |
77 ADD_FAILURE() | 78 return; |
78 << "Description:\n" | 79 ADD_FAILURE() << "Description:\n" << description << "\n\nExpected:\n" |
79 << description | 80 << HexDumpWithMarks( |
80 << "\n\nExpected:\n" | 81 expected, expected_len, marks.get(), max_len) |
81 << HexDumpWithMarks(expected, expected_len, marks.get(), max_len) | 82 << "\nActual:\n" |
82 << "\nActual:\n" | 83 << HexDumpWithMarks(actual, actual_len, marks.get(), max_len); |
83 << HexDumpWithMarks(actual, actual_len, marks.get(), max_len); | |
84 } | 84 } |
85 | 85 |
86 void SetFrameFlags(SpdyFrame* frame, | 86 void SetFrameFlags(SpdyFrame* frame, |
87 uint8 flags, | 87 uint8 flags, |
88 SpdyMajorVersion spdy_version) { | 88 SpdyMajorVersion spdy_version) { |
89 switch (spdy_version) { | 89 switch (spdy_version) { |
90 case SPDY2: | 90 case SPDY2: |
91 case SPDY3: | 91 case SPDY3: |
92 frame->data()[4] = flags; | 92 frame->data()[4] = flags; |
93 break; | 93 break; |
(...skipping 15 matching lines...) Expand all Loading... |
109 CHECK_EQ(0u, length & ~kLengthMask); | 109 CHECK_EQ(0u, length & ~kLengthMask); |
110 { | 110 { |
111 int32 wire_length = base::HostToNet32(length); | 111 int32 wire_length = base::HostToNet32(length); |
112 // The length field in SPDY 2 and 3 is a 24-bit (3B) integer starting at | 112 // The length field in SPDY 2 and 3 is a 24-bit (3B) integer starting at |
113 // offset 5. | 113 // offset 5. |
114 memcpy(frame->data() + 5, reinterpret_cast<char*>(&wire_length) + 1, 3); | 114 memcpy(frame->data() + 5, reinterpret_cast<char*>(&wire_length) + 1, 3); |
115 } | 115 } |
116 break; | 116 break; |
117 case SPDY4: | 117 case SPDY4: |
118 case SPDY5: | 118 case SPDY5: |
119 CHECK_GT(1u<<14, length); | 119 CHECK_GT(1u << 14, length); |
120 { | 120 { |
121 int32 wire_length = base::HostToNet16(static_cast<uint16>(length)); | 121 int32 wire_length = base::HostToNet16(static_cast<uint16>(length)); |
122 memcpy(frame->data(), | 122 memcpy(frame->data(), |
123 reinterpret_cast<char*>(&wire_length), | 123 reinterpret_cast<char*>(&wire_length), |
124 sizeof(uint16)); | 124 sizeof(uint16)); |
125 } | 125 } |
126 break; | 126 break; |
127 default: | 127 default: |
128 LOG(FATAL) << "Unsupported SPDY version."; | 128 LOG(FATAL) << "Unsupported SPDY version."; |
129 } | 129 } |
130 } | 130 } |
131 | 131 |
132 | |
133 } // namespace test | 132 } // namespace test |
134 | 133 |
135 } // namespace net | 134 } // namespace net |
OLD | NEW |