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"
9 #include "base/logging.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/sys_byteorder.h"
12 #include "testing/gtest/include/gtest/gtest.h"
18 std::string
HexDumpWithMarks(const unsigned char* data
, int length
,
19 const bool* marks
, int mark_length
) {
20 static const char kHexChars
[] = "0123456789abcdef";
21 static const int kColumns
= 4;
23 const int kSizeLimit
= 1024;
24 if (length
> kSizeLimit
|| mark_length
> kSizeLimit
) {
25 LOG(ERROR
) << "Only dumping first " << kSizeLimit
<< " bytes.";
26 length
= std::min(length
, kSizeLimit
);
27 mark_length
= std::min(mark_length
, kSizeLimit
);
31 for (const unsigned char* row
= data
; length
> 0;
32 row
+= kColumns
, length
-= kColumns
) {
33 for (const unsigned char *p
= row
; p
< row
+ 4; ++p
) {
34 if (p
< row
+ length
) {
36 (marks
&& (p
- data
) < mark_length
&& marks
[p
- data
]);
37 hex
+= mark
? '*' : ' ';
38 hex
+= kHexChars
[(*p
& 0xf0) >> 4];
39 hex
+= kHexChars
[*p
& 0x0f];
40 hex
+= mark
? '*' : ' ';
47 for (const unsigned char *p
= row
; p
< row
+ 4 && p
< row
+ length
; ++p
)
48 hex
+= (*p
>= 0x20 && *p
<= 0x7f) ? (*p
) : '.';
55 void CompareCharArraysWithHexError(
56 const std::string
& description
,
57 const unsigned char* actual
,
59 const unsigned char* expected
,
60 const int expected_len
) {
61 const int min_len
= std::min(actual_len
, expected_len
);
62 const int max_len
= std::max(actual_len
, expected_len
);
63 scoped_ptr
<bool[]> marks(new bool[max_len
]);
64 bool identical
= (actual_len
== expected_len
);
65 for (int i
= 0; i
< min_len
; ++i
) {
66 if (actual
[i
] != expected
[i
]) {
73 for (int i
= min_len
; i
< max_len
; ++i
) {
76 if (identical
) return;
81 << HexDumpWithMarks(expected
, expected_len
, marks
.get(), max_len
)
83 << HexDumpWithMarks(actual
, actual_len
, marks
.get(), max_len
);
86 void SetFrameFlags(SpdyFrame
* frame
, uint8 flags
, int spdy_version
) {
87 switch (spdy_version
) {
90 frame
->data()[4] = flags
;
93 frame
->data()[3] = flags
;
96 LOG(FATAL
) << "Unsupported SPDY version.";
100 void SetFrameLength(SpdyFrame
* frame
, size_t length
, int spdy_version
) {
101 switch (spdy_version
) {
104 CHECK_EQ(0u, length
& ~kLengthMask
);
106 int32 wire_length
= base::HostToNet32(length
);
107 // The length field in SPDY 2 and 3 is a 24-bit (3B) integer starting at
109 memcpy(frame
->data() + 5, reinterpret_cast<char*>(&wire_length
) + 1, 3);
113 CHECK_GT(1u<<16, length
);
115 int32 wire_length
= base::HostToNet16(static_cast<uint16
>(length
));
116 memcpy(frame
->data(),
117 reinterpret_cast<char*>(&wire_length
),
122 LOG(FATAL
) << "Unsupported SPDY version.";