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"
10 #include "base/logging.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/strings/string_number_conversions.h"
13 #include "base/sys_byteorder.h"
14 #include "testing/gtest/include/gtest/gtest.h"
20 std::string
HexDumpWithMarks(const unsigned char* data
, int length
,
21 const bool* marks
, int mark_length
) {
22 static const char kHexChars
[] = "0123456789abcdef";
23 static const int kColumns
= 4;
25 const int kSizeLimit
= 1024;
26 if (length
> kSizeLimit
|| mark_length
> kSizeLimit
) {
27 LOG(ERROR
) << "Only dumping first " << kSizeLimit
<< " bytes.";
28 length
= std::min(length
, kSizeLimit
);
29 mark_length
= std::min(mark_length
, kSizeLimit
);
33 for (const unsigned char* row
= data
; length
> 0;
34 row
+= kColumns
, length
-= kColumns
) {
35 for (const unsigned char *p
= row
; p
< row
+ 4; ++p
) {
36 if (p
< row
+ length
) {
38 (marks
&& (p
- data
) < mark_length
&& marks
[p
- data
]);
39 hex
+= mark
? '*' : ' ';
40 hex
+= kHexChars
[(*p
& 0xf0) >> 4];
41 hex
+= kHexChars
[*p
& 0x0f];
42 hex
+= mark
? '*' : ' ';
49 for (const unsigned char *p
= row
; p
< row
+ 4 && p
< row
+ length
; ++p
)
50 hex
+= (*p
>= 0x20 && *p
<= 0x7f) ? (*p
) : '.';
57 void CompareCharArraysWithHexError(
58 const std::string
& description
,
59 const unsigned char* actual
,
61 const unsigned char* expected
,
62 const int expected_len
) {
63 const int min_len
= std::min(actual_len
, expected_len
);
64 const int max_len
= std::max(actual_len
, expected_len
);
65 scoped_ptr
<bool[]> marks(new bool[max_len
]);
66 bool identical
= (actual_len
== expected_len
);
67 for (int i
= 0; i
< min_len
; ++i
) {
68 if (actual
[i
] != expected
[i
]) {
75 for (int i
= min_len
; i
< max_len
; ++i
) {
78 if (identical
) return;
83 << HexDumpWithMarks(expected
, expected_len
, marks
.get(), max_len
)
85 << HexDumpWithMarks(actual
, actual_len
, marks
.get(), max_len
);
88 void SetFrameFlags(SpdyFrame
* frame
,
90 SpdyMajorVersion spdy_version
) {
91 switch (spdy_version
) {
94 frame
->data()[4] = flags
;
98 frame
->data()[3] = flags
;
101 LOG(FATAL
) << "Unsupported SPDY version.";
105 void SetFrameLength(SpdyFrame
* frame
,
107 SpdyMajorVersion spdy_version
) {
108 switch (spdy_version
) {
111 CHECK_EQ(0u, length
& ~kLengthMask
);
113 int32 wire_length
= base::HostToNet32(length
);
114 // The length field in SPDY 2 and 3 is a 24-bit (3B) integer starting at
116 memcpy(frame
->data() + 5, reinterpret_cast<char*>(&wire_length
) + 1, 3);
121 CHECK_GT(1u<<14, length
);
123 int32 wire_length
= base::HostToNet16(static_cast<uint16
>(length
));
124 memcpy(frame
->data(),
125 reinterpret_cast<char*>(&wire_length
),
130 LOG(FATAL
) << "Unsupported SPDY version.";
134 std::string
a2b_hex(const char* hex_data
) {
135 std::vector
<uint8
> output
;
137 if (base::HexStringToBytes(hex_data
, &output
))
138 result
.assign(reinterpret_cast<const char*>(&output
[0]), output
.size());