blob: bdf27526cb792d63839e6951b83a2f730e68560d [file] [log] [blame]
Andrew Top0d1858f2019-05-15 22:01:47 -07001// Copyright 2017 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/base/hex_utils.h"
6#include "testing/gtest/include/gtest/gtest.h"
7
8namespace net {
9
10namespace test {
11
12TEST(HexUtilsTest, HexDecode) {
13 EXPECT_EQ("", HexDecode(""));
14 EXPECT_EQ("a", HexDecode("61"));
15 // Mixed case input.
16 EXPECT_EQ("Hello world!", HexDecode("48656c6C6F20776f726C6421"));
17}
18
19TEST(HexUtilsTest, HexDump) {
20 EXPECT_EQ("", HexDump(""));
21 EXPECT_EQ("0x0000: 4865 6c6c 6f20 776f 726c 6421 Hello.world!\n",
22 HexDump("Hello world!"));
23 EXPECT_EQ(
24 "0x0000: 5052 4920 2a20 4854 5450 2f32 2e30 0d0a PRI.*.HTTP/2.0..\n"
25 "0x0010: 0d0a 534d 0d0a 0d0a ..SM....\n",
26 HexDump("PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n"));
27 // Verify that 0x21 and 0x7e are printable, 0x20 and 0x7f are not.
28 EXPECT_EQ("0x0000: 2021 7e7f .!~.\n",
29 HexDump(HexDecode("20217e7f")));
30 // Verify that values above numeric_limits<unsigned char>::max() are cast
31 // properly on platforms where char is unsigned.
32 EXPECT_EQ("0x0000: 90aa ff ...\n",
33 HexDump(HexDecode("90aaff")));
34}
35
36} // namespace test
37
38} // namespace net