OLD | NEW |
(Empty) | |
| 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/quic/test_tools/quic_test_utils.h" |
| 6 #include "net/quic/crypto/crypto_framer.h" |
| 7 |
| 8 using std::max; |
| 9 using std::min; |
| 10 using std::string; |
| 11 |
| 12 namespace net { |
| 13 namespace test { |
| 14 |
| 15 MockFramerVisitor::MockFramerVisitor() { |
| 16 // By default, we want to accept packets. |
| 17 ON_CALL(*this, OnPacketHeader(testing::_)) |
| 18 .WillByDefault(testing::Return(true)); |
| 19 } |
| 20 |
| 21 MockFramerVisitor::~MockFramerVisitor() {} |
| 22 |
| 23 bool NoOpFramerVisitor::OnPacketHeader(const QuicPacketHeader& header) { |
| 24 return true; |
| 25 } |
| 26 |
| 27 namespace { |
| 28 |
| 29 string HexDumpWithMarks(const char* data, int length, |
| 30 const bool* marks, int mark_length) { |
| 31 static const char kHexChars[] = "0123456789abcdef"; |
| 32 static const int kColumns = 4; |
| 33 |
| 34 const int kSizeLimit = 1024; |
| 35 if (length > kSizeLimit || mark_length > kSizeLimit) { |
| 36 LOG(ERROR) << "Only dumping first " << kSizeLimit << " bytes."; |
| 37 length = min(length, kSizeLimit); |
| 38 mark_length = min(mark_length, kSizeLimit); |
| 39 } |
| 40 |
| 41 string hex; |
| 42 for (const char* row = data; length > 0; |
| 43 row += kColumns, length -= kColumns) { |
| 44 for (const char *p = row; p < row + 4; ++p) { |
| 45 if (p < row + length) { |
| 46 const bool mark = |
| 47 (marks && (p - data) < mark_length && marks[p - data]); |
| 48 hex += mark ? '*' : ' '; |
| 49 hex += kHexChars[(*p & 0xf0) >> 4]; |
| 50 hex += kHexChars[*p & 0x0f]; |
| 51 hex += mark ? '*' : ' '; |
| 52 } else { |
| 53 hex += " "; |
| 54 } |
| 55 } |
| 56 hex = hex + " "; |
| 57 |
| 58 for (const char *p = row; p < row + 4 && p < row + length; ++p) |
| 59 hex += (*p >= 0x20 && *p <= 0x7f) ? (*p) : '.'; |
| 60 |
| 61 hex = hex + '\n'; |
| 62 } |
| 63 return hex; |
| 64 } |
| 65 |
| 66 } // namespace |
| 67 |
| 68 void CompareCharArraysWithHexError( |
| 69 const string& description, |
| 70 const char* actual, |
| 71 const int actual_len, |
| 72 const char* expected, |
| 73 const int expected_len) { |
| 74 const int min_len = min(actual_len, expected_len); |
| 75 const int max_len = max(actual_len, expected_len); |
| 76 scoped_array<bool> marks(new bool[max_len]); |
| 77 bool identical = (actual_len == expected_len); |
| 78 for (int i = 0; i < min_len; ++i) { |
| 79 if (actual[i] != expected[i]) { |
| 80 marks[i] = true; |
| 81 identical = false; |
| 82 } else { |
| 83 marks[i] = false; |
| 84 } |
| 85 } |
| 86 for (int i = min_len; i < max_len; ++i) { |
| 87 marks[i] = true; |
| 88 } |
| 89 if (identical) return; |
| 90 ADD_FAILURE() |
| 91 << "Description:\n" |
| 92 << description |
| 93 << "\n\nExpected:\n" |
| 94 << HexDumpWithMarks(expected, expected_len, marks.get(), max_len) |
| 95 << "\nActual:\n" |
| 96 << HexDumpWithMarks(actual, actual_len, marks.get(), max_len); |
| 97 } |
| 98 |
| 99 void CompareQuicDataWithHexError( |
| 100 const string& description, |
| 101 QuicData* actual, |
| 102 QuicData* expected) { |
| 103 CompareCharArraysWithHexError( |
| 104 description, |
| 105 actual->data(), actual->length(), |
| 106 expected->data(), expected->length()); |
| 107 } |
| 108 |
| 109 QuicPacket* ConstructHandshakePacket(QuicGuid guid, CryptoTag tag) { |
| 110 CryptoHandshakeMessage message; |
| 111 message.tag = tag; |
| 112 CryptoFramer crypto_framer; |
| 113 QuicData* data; |
| 114 crypto_framer.ConstructHandshakeMessage(message, &data); |
| 115 QuicFramer quic_framer(QuicDecrypter::Create(kNULL), |
| 116 QuicEncrypter::Create(kNULL)); |
| 117 |
| 118 QuicPacketHeader header; |
| 119 header.guid = guid; |
| 120 header.retransmission_count = 0; |
| 121 header.packet_sequence_number = 1; |
| 122 header.transmission_time = 0; |
| 123 header.flags = PACKET_FLAGS_NONE; |
| 124 header.fec_group = 0; |
| 125 |
| 126 QuicStreamFragment stream_fragment(kCryptoStreamId, false, 0, |
| 127 data->AsStringPiece()); |
| 128 |
| 129 QuicFragment fragment(&stream_fragment); |
| 130 QuicFragments fragments; |
| 131 fragments.push_back(fragment); |
| 132 QuicPacket* packet; |
| 133 quic_framer.ConstructFragementDataPacket(header, fragments, &packet); |
| 134 delete data; |
| 135 return packet; |
| 136 } |
| 137 |
| 138 } // namespace test |
| 139 |
| 140 } // namespace net |
OLD | NEW |