Revert of Clean up Smart Lock cryptohome keys logic: (patchset #5 id:80001 of https...
[chromium-blink-merge.git] / net / quic / quic_socket_address_coder_test.cc
blobcbfac2b12d84c21a8912bcf80f435b9945cadeef
1 // Copyright 2014 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.
5 #include "net/quic/quic_socket_address_coder.h"
7 #include "testing/gtest/include/gtest/gtest.h"
9 using std::string;
11 namespace net {
12 namespace test {
14 TEST(QuicSocketAddressCoderTest, EncodeIPv4) {
15 IPAddressNumber ip;
16 ASSERT_TRUE(ParseIPLiteralToNumber("4.31.198.44", &ip));
17 QuicSocketAddressCoder coder(IPEndPoint(ip, 0x1234));
18 string serialized = coder.Encode();
19 string expected("\x02\x00\x04\x1f\xc6\x2c\x34\x12", 8);
20 EXPECT_EQ(expected, serialized);
23 TEST(QuicSocketAddressCoderTest, EncodeIPv6) {
24 IPAddressNumber ip;
25 ASSERT_TRUE(ParseIPLiteralToNumber("2001:700:300:1800::f", &ip));
26 QuicSocketAddressCoder coder(IPEndPoint(ip, 0x5678));
27 string serialized = coder.Encode();
28 string expected("\x0a\x00"
29 "\x20\x01\x07\x00\x03\x00\x18\x00"
30 "\x00\x00\x00\x00\x00\x00\x00\x0f"
31 "\x78\x56", 20);
32 EXPECT_EQ(expected, serialized);
35 TEST(QuicSocketAddressCoderTest, DecodeIPv4) {
36 string serialized("\x02\x00\x04\x1f\xc6\x2c\x34\x12", 8);
37 QuicSocketAddressCoder coder;
38 ASSERT_TRUE(coder.Decode(serialized.data(), serialized.length()));
39 EXPECT_EQ(AF_INET, ConvertAddressFamily(GetAddressFamily(coder.ip())));
40 string expected_addr("\x04\x1f\xc6\x2c", 4);
41 EXPECT_EQ(expected_addr, IPAddressToPackedString(coder.ip()));
42 EXPECT_EQ(0x1234, coder.port());
45 TEST(QuicSocketAddressCoderTest, DecodeIPv6) {
46 string serialized("\x0a\x00"
47 "\x20\x01\x07\x00\x03\x00\x18\x00"
48 "\x00\x00\x00\x00\x00\x00\x00\x0f"
49 "\x78\x56", 20);
50 QuicSocketAddressCoder coder;
51 ASSERT_TRUE(coder.Decode(serialized.data(), serialized.length()));
52 EXPECT_EQ(AF_INET6, ConvertAddressFamily(GetAddressFamily(coder.ip())));
53 string expected_addr("\x20\x01\x07\x00\x03\x00\x18\x00"
54 "\x00\x00\x00\x00\x00\x00\x00\x0f", 16);
55 EXPECT_EQ(expected_addr, IPAddressToPackedString(coder.ip()));
56 EXPECT_EQ(0x5678, coder.port());
59 TEST(QuicSocketAddressCoderTest, DecodeBad) {
60 string serialized("\x0a\x00"
61 "\x20\x01\x07\x00\x03\x00\x18\x00"
62 "\x00\x00\x00\x00\x00\x00\x00\x0f"
63 "\x78\x56", 20);
64 QuicSocketAddressCoder coder;
65 EXPECT_TRUE(coder.Decode(serialized.data(), serialized.length()));
66 // Append junk.
67 serialized.push_back('\0');
68 EXPECT_FALSE(coder.Decode(serialized.data(), serialized.length()));
69 // Undo.
70 serialized.resize(20);
71 EXPECT_TRUE(coder.Decode(serialized.data(), serialized.length()));
73 // Set an unknown address family.
74 serialized[0] = '\x03';
75 EXPECT_FALSE(coder.Decode(serialized.data(), serialized.length()));
76 // Undo.
77 serialized[0] = '\x0a';
78 EXPECT_TRUE(coder.Decode(serialized.data(), serialized.length()));
80 // Truncate.
81 size_t len = serialized.length();
82 for (size_t i = 0; i < len; i++) {
83 ASSERT_FALSE(serialized.empty());
84 serialized.erase(serialized.length() - 1);
85 EXPECT_FALSE(coder.Decode(serialized.data(), serialized.length()));
87 EXPECT_TRUE(serialized.empty());
90 TEST(QuicSocketAddressCoderTest, EncodeAndDecode) {
91 struct {
92 const char* ip_literal;
93 uint16 port;
94 } test_case[] = {
95 { "93.184.216.119", 0x1234 },
96 { "199.204.44.194", 80 },
97 { "149.20.4.69", 443 },
98 { "127.0.0.1", 8080 },
99 { "2001:700:300:1800::", 0x5678 },
100 { "::1", 65534 },
103 for (size_t i = 0; i < arraysize(test_case); i++) {
104 IPAddressNumber ip;
105 ASSERT_TRUE(ParseIPLiteralToNumber(test_case[i].ip_literal, &ip));
106 QuicSocketAddressCoder encoder(IPEndPoint(ip, test_case[i].port));
107 string serialized = encoder.Encode();
109 QuicSocketAddressCoder decoder;
110 ASSERT_TRUE(decoder.Decode(serialized.data(), serialized.length()));
111 EXPECT_EQ(encoder.ip(), decoder.ip());
112 EXPECT_EQ(encoder.port(), decoder.port());
116 } // namespace test
117 } // namespace net