1c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Copyright (c) 2013 The Chromium Authors. All rights reserved.
2c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// found in the LICENSE file.
4c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
5c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "net/quic/quic_utils.h"
6c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
71e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "net/quic/crypto/crypto_protocol.h"
8c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h"
9c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)using base::StringPiece;
1190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)using std::string;
1290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
13c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)namespace net {
14c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)namespace test {
15c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)namespace {
16c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// A test string and a hex+ASCII dump of the same string.
1890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)const unsigned char kString[] = {
1990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  0x00, 0x90, 0x69, 0xbd, 0x54, 0x00, 0x00, 0x0d,
2090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  0x61, 0x0f, 0x01, 0x89, 0x08, 0x00, 0x45, 0x00,
2190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  0x00, 0x1c, 0xfb, 0x98, 0x40, 0x00, 0x40, 0x01,
2290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  0x7e, 0x18, 0xd8, 0xef, 0x23, 0x01, 0x45, 0x5d,
2390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  0x7f, 0xe2, 0x08, 0x00, 0x6b, 0xcb, 0x0b, 0xc6,
2490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  0x80, 0x6e
2590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)};
2690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
2790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)const unsigned char kHexDump[] =
2890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)"0x0000:  0090 69bd 5400 000d 610f 0189 0800 4500  ..i.T...a.....E.\n"
2990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)"0x0010:  001c fb98 4000 4001 7e18 d8ef 2301 455d  ....@.@.~...#.E]\n"
3090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)"0x0020:  7fe2 0800 6bcb 0bc6 806e                 ....k....n\n";
3190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
32c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)TEST(QuicUtilsTest, StreamErrorToString) {
33c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  EXPECT_STREQ("QUIC_BAD_APPLICATION_PAYLOAD",
34c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)               QuicUtils::StreamErrorToString(QUIC_BAD_APPLICATION_PAYLOAD));
35c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
36c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
37c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)TEST(QuicUtilsTest, ErrorToString) {
38c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  EXPECT_STREQ("QUIC_NO_ERROR",
39c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)               QuicUtils::ErrorToString(QUIC_NO_ERROR));
40c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
41c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
4290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)TEST(QuicUtilsTest, StringToHexASCIIDumpArgTypes) {
4390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Verify that char*, string and StringPiece are all valid argument types.
4490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  struct {
4590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    const string input;
4690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    const string expected;
4790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  } tests[] = {
4890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    { "", "", },
4990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    { "A",   "0x0000:  41                                       A\n", },
5090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    { "AB",  "0x0000:  4142                                     AB\n", },
5190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    { "ABC", "0x0000:  4142 43                                  ABC\n", },
5290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    { "original",
5390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      "0x0000:  6f72 6967 696e 616c                      original\n", },
5490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  };
5590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
5690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
5790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    EXPECT_EQ(tests[i].expected,
5890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)              QuicUtils::StringToHexASCIIDump(tests[i].input.c_str()));
5990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    EXPECT_EQ(tests[i].expected,
6090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)              QuicUtils::StringToHexASCIIDump(tests[i].input));
6190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    EXPECT_EQ(tests[i].expected,
6290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)              QuicUtils::StringToHexASCIIDump(StringPiece(tests[i].input)));
6390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
6490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
6590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
6690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)TEST(QuicUtilsTest, StringToHexASCIIDumpSuccess) {
6790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  EXPECT_EQ(string(reinterpret_cast<const char*>(kHexDump)),
6890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      QuicUtils::StringToHexASCIIDump(
6990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)          string(reinterpret_cast<const char*>(kString), sizeof(kString))));
7090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
7190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
721e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)TEST(QuicUtilsTest, TagToString) {
731e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  EXPECT_EQ("SCFG",
741e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)            QuicUtils::TagToString(kSCFG));
751e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  EXPECT_EQ("SNO ",
761e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)            QuicUtils::TagToString(kServerNonceTag));
771e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  EXPECT_EQ("CRT ",
781e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)            QuicUtils::TagToString(kCertificateTag));
791e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  EXPECT_EQ("CHLO",
801e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)            QuicUtils::TagToString(MakeQuicTag('C', 'H', 'L', 'O')));
811e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // A tag that contains a non-printing character will be printed as a decimal
821e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // number.
831e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  EXPECT_EQ("525092931",
841e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)            QuicUtils::TagToString(MakeQuicTag('C', 'H', 'L', '\x1f')));
851e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
861e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
87c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}  // namespace
88c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}  // namespace test
89c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}  // namespace net
90