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