1// Copyright (c) 2013 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/quic_utils.h"
6
7#include "net/quic/crypto/crypto_protocol.h"
8#include "testing/gtest/include/gtest/gtest.h"
9
10using base::StringPiece;
11using std::string;
12
13namespace net {
14namespace test {
15namespace {
16
17// A test string and a hex+ASCII dump of the same string.
18const unsigned char kString[] = {
19  0x00, 0x90, 0x69, 0xbd, 0x54, 0x00, 0x00, 0x0d,
20  0x61, 0x0f, 0x01, 0x89, 0x08, 0x00, 0x45, 0x00,
21  0x00, 0x1c, 0xfb, 0x98, 0x40, 0x00, 0x40, 0x01,
22  0x7e, 0x18, 0xd8, 0xef, 0x23, 0x01, 0x45, 0x5d,
23  0x7f, 0xe2, 0x08, 0x00, 0x6b, 0xcb, 0x0b, 0xc6,
24  0x80, 0x6e
25};
26
27const unsigned char kHexDump[] =
28"0x0000:  0090 69bd 5400 000d 610f 0189 0800 4500  ..i.T...a.....E.\n"
29"0x0010:  001c fb98 4000 4001 7e18 d8ef 2301 455d  ....@.@.~...#.E]\n"
30"0x0020:  7fe2 0800 6bcb 0bc6 806e                 ....k....n\n";
31
32TEST(QuicUtilsTest, StreamErrorToString) {
33  EXPECT_STREQ("QUIC_BAD_APPLICATION_PAYLOAD",
34               QuicUtils::StreamErrorToString(QUIC_BAD_APPLICATION_PAYLOAD));
35}
36
37TEST(QuicUtilsTest, ErrorToString) {
38  EXPECT_STREQ("QUIC_NO_ERROR",
39               QuicUtils::ErrorToString(QUIC_NO_ERROR));
40}
41
42TEST(QuicUtilsTest, StringToHexASCIIDumpArgTypes) {
43  // Verify that char*, string and StringPiece are all valid argument types.
44  struct {
45    const string input;
46    const string expected;
47  } tests[] = {
48    { "", "", },
49    { "A",   "0x0000:  41                                       A\n", },
50    { "AB",  "0x0000:  4142                                     AB\n", },
51    { "ABC", "0x0000:  4142 43                                  ABC\n", },
52    { "original",
53      "0x0000:  6f72 6967 696e 616c                      original\n", },
54  };
55
56  for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
57    EXPECT_EQ(tests[i].expected,
58              QuicUtils::StringToHexASCIIDump(tests[i].input.c_str()));
59    EXPECT_EQ(tests[i].expected,
60              QuicUtils::StringToHexASCIIDump(tests[i].input));
61    EXPECT_EQ(tests[i].expected,
62              QuicUtils::StringToHexASCIIDump(StringPiece(tests[i].input)));
63  }
64}
65
66TEST(QuicUtilsTest, StringToHexASCIIDumpSuccess) {
67  EXPECT_EQ(string(reinterpret_cast<const char*>(kHexDump)),
68      QuicUtils::StringToHexASCIIDump(
69          string(reinterpret_cast<const char*>(kString), sizeof(kString))));
70}
71
72TEST(QuicUtilsTest, TagToString) {
73  EXPECT_EQ("SCFG",
74            QuicUtils::TagToString(kSCFG));
75  EXPECT_EQ("SNO ",
76            QuicUtils::TagToString(kServerNonceTag));
77  EXPECT_EQ("CRT ",
78            QuicUtils::TagToString(kCertificateTag));
79  EXPECT_EQ("CHLO",
80            QuicUtils::TagToString(MakeQuicTag('C', 'H', 'L', 'O')));
81  // A tag that contains a non-printing character will be printed as a decimal
82  // number.
83  EXPECT_EQ("525092931",
84            QuicUtils::TagToString(MakeQuicTag('C', 'H', 'L', '\x1f')));
85}
86
87}  // namespace
88}  // namespace test
89}  // namespace net
90