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.
4
5 #include "quiche/spdy/test_tools/spdy_test_utils.h"
6
7 #include <algorithm>
8 #include <cstdint>
9 #include <cstring>
10 #include <memory>
11 #include <string>
12
13 #include "quiche/common/platform/api/quiche_logging.h"
14 #include "quiche/common/platform/api/quiche_test.h"
15 #include "quiche/common/quiche_endian.h"
16 #include "quiche/spdy/core/spdy_protocol.h"
17
18 namespace spdy {
19 namespace test {
20
HexDumpWithMarks(const unsigned char * data,int length,const bool * marks,int mark_length)21 std::string HexDumpWithMarks(const unsigned char* data, int length,
22 const bool* marks, int mark_length) {
23 static const char kHexChars[] = "0123456789abcdef";
24 static const int kColumns = 4;
25
26 const int kSizeLimit = 1024;
27 if (length > kSizeLimit || mark_length > kSizeLimit) {
28 QUICHE_LOG(ERROR) << "Only dumping first " << kSizeLimit << " bytes.";
29 length = std::min(length, kSizeLimit);
30 mark_length = std::min(mark_length, kSizeLimit);
31 }
32
33 std::string hex;
34 for (const unsigned char* row = data; length > 0;
35 row += kColumns, length -= kColumns) {
36 for (const unsigned char* p = row; p < row + 4; ++p) {
37 if (p < row + length) {
38 const bool mark =
39 (marks && (p - data) < mark_length && marks[p - data]);
40 hex += mark ? '*' : ' ';
41 hex += kHexChars[(*p & 0xf0) >> 4];
42 hex += kHexChars[*p & 0x0f];
43 hex += mark ? '*' : ' ';
44 } else {
45 hex += " ";
46 }
47 }
48 hex = hex + " ";
49
50 for (const unsigned char* p = row; p < row + 4 && p < row + length; ++p) {
51 hex += (*p >= 0x20 && *p <= 0x7f) ? (*p) : '.';
52 }
53
54 hex = hex + '\n';
55 }
56 return hex;
57 }
58
CompareCharArraysWithHexError(const std::string & description,const unsigned char * actual,const int actual_len,const unsigned char * expected,const int expected_len)59 void CompareCharArraysWithHexError(const std::string& description,
60 const unsigned char* actual,
61 const int actual_len,
62 const unsigned char* expected,
63 const int expected_len) {
64 const int min_len = std::min(actual_len, expected_len);
65 const int max_len = std::max(actual_len, expected_len);
66 std::unique_ptr<bool[]> marks(new bool[max_len]);
67 bool identical = (actual_len == expected_len);
68 for (int i = 0; i < min_len; ++i) {
69 if (actual[i] != expected[i]) {
70 marks[i] = true;
71 identical = false;
72 } else {
73 marks[i] = false;
74 }
75 }
76 for (int i = min_len; i < max_len; ++i) {
77 marks[i] = true;
78 }
79 if (identical) return;
80 ADD_FAILURE() << "Description:\n"
81 << description << "\n\nExpected:\n"
82 << HexDumpWithMarks(expected, expected_len, marks.get(),
83 max_len)
84 << "\nActual:\n"
85 << HexDumpWithMarks(actual, actual_len, marks.get(), max_len);
86 }
87
SetFrameFlags(SpdySerializedFrame * frame,uint8_t flags)88 void SetFrameFlags(SpdySerializedFrame* frame, uint8_t flags) {
89 frame->data()[4] = flags;
90 }
91
SetFrameLength(SpdySerializedFrame * frame,size_t length)92 void SetFrameLength(SpdySerializedFrame* frame, size_t length) {
93 QUICHE_CHECK_GT(1u << 14, length);
94 {
95 int32_t wire_length = quiche::QuicheEndian::HostToNet32(length);
96 memcpy(frame->data(), reinterpret_cast<char*>(&wire_length) + 1, 3);
97 }
98 }
99
MakeSerializedFrame(const char * data,size_t length)100 SpdySerializedFrame MakeSerializedFrame(const char* data, size_t length) {
101 std::unique_ptr<char[]> copy = std::make_unique<char[]>(length);
102 std::copy(data, data + length, copy.get());
103 return SpdySerializedFrame(std::move(copy), length);
104 }
105
106 } // namespace test
107 } // namespace spdy
108