summaryrefslogtreecommitdiffstats
path: root/net/quic/quic_utils_test.cc
blob: 4108bd7507a308456130e049673abebc982c75ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "net/quic/quic_utils.h"

#include "net/quic/crypto/crypto_protocol.h"
#include "testing/gtest/include/gtest/gtest.h"

using base::StringPiece;
using std::string;

namespace net {
namespace test {
namespace {

// A test string and a hex+ASCII dump of the same string.
const unsigned char kString[] = {
    0x00, 0x90, 0x69, 0xbd, 0x54, 0x00, 0x00, 0x0d, 0x61, 0x0f, 0x01,
    0x89, 0x08, 0x00, 0x45, 0x00, 0x00, 0x1c, 0xfb, 0x98, 0x40, 0x00,
    0x40, 0x01, 0x7e, 0x18, 0xd8, 0xef, 0x23, 0x01, 0x45, 0x5d, 0x7f,
    0xe2, 0x08, 0x00, 0x6b, 0xcb, 0x0b, 0xc6, 0x80, 0x6e};

const unsigned char kHexDump[] =
    "0x0000:  0090 69bd 5400 000d 610f 0189 0800 4500  ..i.T...a.....E.\n"
    "0x0010:  001c fb98 4000 4001 7e18 d8ef 2301 455d  ....@.@.~...#.E]\n"
    "0x0020:  7fe2 0800 6bcb 0bc6 806e                 ....k....n\n";

TEST(QuicUtilsTest, StreamErrorToString) {
  EXPECT_STREQ("QUIC_BAD_APPLICATION_PAYLOAD",
               QuicUtils::StreamErrorToString(QUIC_BAD_APPLICATION_PAYLOAD));
}

TEST(QuicUtilsTest, ErrorToString) {
  EXPECT_STREQ("QUIC_NO_ERROR", QuicUtils::ErrorToString(QUIC_NO_ERROR));
}

TEST(QuicUtilsTest, StringToHexASCIIDumpArgTypes) {
  // Verify that char*, string and StringPiece are all valid argument types.
  struct {
    const string input;
    const string expected;
  } tests[] = {
      {
          "", "",
      },
      {
          "A", "0x0000:  41                                       A\n",
      },
      {
          "AB", "0x0000:  4142                                     AB\n",
      },
      {
          "ABC", "0x0000:  4142 43                                  ABC\n",
      },
      {
          "original",
          "0x0000:  6f72 6967 696e 616c                      original\n",
      },
  };

  for (size_t i = 0; i < arraysize(tests); ++i) {
    EXPECT_EQ(tests[i].expected,
              QuicUtils::StringToHexASCIIDump(tests[i].input.c_str()));
    EXPECT_EQ(tests[i].expected,
              QuicUtils::StringToHexASCIIDump(tests[i].input));
    EXPECT_EQ(tests[i].expected,
              QuicUtils::StringToHexASCIIDump(StringPiece(tests[i].input)));
  }
}

TEST(QuicUtilsTest, StringToHexASCIIDumpSuccess) {
  EXPECT_EQ(string(reinterpret_cast<const char*>(kHexDump)),
            QuicUtils::StringToHexASCIIDump(string(
                reinterpret_cast<const char*>(kString), sizeof(kString))));
}

TEST(QuicUtilsTest, TagToString) {
  EXPECT_EQ("SCFG", QuicUtils::TagToString(kSCFG));
  EXPECT_EQ("SNO ", QuicUtils::TagToString(kServerNonceTag));
  EXPECT_EQ("CRT ", QuicUtils::TagToString(kCertificateTag));
  EXPECT_EQ("CHLO", QuicUtils::TagToString(MakeQuicTag('C', 'H', 'L', 'O')));
  // A tag that contains a non-printing character will be printed as a decimal
  // number.
  EXPECT_EQ("525092931",
            QuicUtils::TagToString(MakeQuicTag('C', 'H', 'L', '\x1f')));
}

TEST(QuicUtilsTest, ParseQuicConnectionOptions) {
  QuicTagVector empty_options = QuicUtils::ParseQuicConnectionOptions("");
  EXPECT_EQ(0ul, empty_options.size());

  QuicTagVector parsed_options =
      QuicUtils::ParseQuicConnectionOptions("TIMER,TBBR,REJ");
  QuicTagVector expected_options;
  expected_options.push_back(kTIME);
  expected_options.push_back(kTBBR);
  expected_options.push_back(kREJ);
  EXPECT_EQ(expected_options, parsed_options);
}

}  // namespace
}  // namespace test
}  // namespace net