Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / net / spdy / spdy_test_utils.cc
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 "net/spdy/spdy_test_utils.h"
6
7 #include <cstring>
8
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"
13
14 namespace net {
15
16 namespace test {
17
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;
22
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);
28   }
29
30   std::string hex;
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) {
35         const bool mark =
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 ? '*' : ' ';
41       } else {
42         hex += "    ";
43       }
44     }
45     hex = hex + "  ";
46
47     for (const unsigned char *p = row; p < row + 4 && p < row + length; ++p)
48       hex += (*p >= 0x20 && *p <= 0x7f) ? (*p) : '.';
49
50     hex = hex + '\n';
51   }
52   return hex;
53 }
54
55 void CompareCharArraysWithHexError(
56     const std::string& description,
57     const unsigned char* actual,
58     const int actual_len,
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]) {
67       marks[i] = true;
68       identical = false;
69     } else {
70       marks[i] = false;
71     }
72   }
73   for (int i = min_len; i < max_len; ++i) {
74     marks[i] = true;
75   }
76   if (identical) return;
77   ADD_FAILURE()
78       << "Description:\n"
79       << description
80       << "\n\nExpected:\n"
81       << HexDumpWithMarks(expected, expected_len, marks.get(), max_len)
82       << "\nActual:\n"
83       << HexDumpWithMarks(actual, actual_len, marks.get(), max_len);
84 }
85
86 void SetFrameFlags(SpdyFrame* frame,
87                    uint8 flags,
88                    SpdyMajorVersion spdy_version) {
89   switch (spdy_version) {
90     case SPDY2:
91     case SPDY3:
92       frame->data()[4] = flags;
93       break;
94     case SPDY4:
95     case SPDY5:
96       frame->data()[3] = flags;
97       break;
98     default:
99       LOG(FATAL) << "Unsupported SPDY version.";
100   }
101 }
102
103 void SetFrameLength(SpdyFrame* frame,
104                     size_t length,
105                     SpdyMajorVersion spdy_version) {
106   switch (spdy_version) {
107     case SPDY2:
108     case SPDY3:
109       CHECK_EQ(0u, length & ~kLengthMask);
110       {
111         int32 wire_length = base::HostToNet32(length);
112         // The length field in SPDY 2 and 3 is a 24-bit (3B) integer starting at
113         // offset 5.
114         memcpy(frame->data() + 5, reinterpret_cast<char*>(&wire_length) + 1, 3);
115       }
116       break;
117     case SPDY4:
118     case SPDY5:
119       CHECK_GT(1u<<14, length);
120       {
121         int32 wire_length = base::HostToNet16(static_cast<uint16>(length));
122         memcpy(frame->data(),
123                reinterpret_cast<char*>(&wire_length),
124                sizeof(uint16));
125       }
126       break;
127     default:
128       LOG(FATAL) << "Unsupported SPDY version.";
129   }
130 }
131
132
133 }  // namespace test
134
135 }  // namespace net