Revert of Move fetching logic out of ApplicationManager, eliminate url mappings....
[chromium-blink-merge.git] / net / quic / quic_socket_address_coder_test.cc
blob6a94550d8e0d0c465a073ed5603a27ee0c7894a6
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 "net/base/net_util.h"
8 #include "net/base/sys_addrinfo.h"
9 #include "testing/gtest/include/gtest/gtest.h"
11 using std::string;
13 namespace net {
14 namespace test {
16 TEST(QuicSocketAddressCoderTest, EncodeIPv4) {
17 IPAddressNumber ip;
18 ASSERT_TRUE(ParseIPLiteralToNumber("4.31.198.44", &ip));
19 QuicSocketAddressCoder coder(IPEndPoint(ip, 0x1234));
20 string serialized = coder.Encode();
21 string expected("\x02\x00\x04\x1f\xc6\x2c\x34\x12", 8);
22 EXPECT_EQ(expected, serialized);
25 TEST(QuicSocketAddressCoderTest, EncodeIPv6) {
26 IPAddressNumber ip;
27 ASSERT_TRUE(ParseIPLiteralToNumber("2001:700:300:1800::f", &ip));
28 QuicSocketAddressCoder coder(IPEndPoint(ip, 0x5678));
29 string serialized = coder.Encode();
30 string expected("\x0a\x00"
31 "\x20\x01\x07\x00\x03\x00\x18\x00"
32 "\x00\x00\x00\x00\x00\x00\x00\x0f"
33 "\x78\x56", 20);
34 EXPECT_EQ(expected, serialized);
37 TEST(QuicSocketAddressCoderTest, DecodeIPv4) {
38 string serialized("\x02\x00\x04\x1f\xc6\x2c\x34\x12", 8);
39 QuicSocketAddressCoder coder;
40 ASSERT_TRUE(coder.Decode(serialized.data(), serialized.length()));
41 EXPECT_EQ(AF_INET, ConvertAddressFamily(GetAddressFamily(coder.ip())));
42 string expected_addr("\x04\x1f\xc6\x2c", 4);
43 EXPECT_EQ(expected_addr, IPAddressToPackedString(coder.ip()));
44 EXPECT_EQ(0x1234, coder.port());
47 TEST(QuicSocketAddressCoderTest, DecodeIPv6) {
48 string serialized("\x0a\x00"
49 "\x20\x01\x07\x00\x03\x00\x18\x00"
50 "\x00\x00\x00\x00\x00\x00\x00\x0f"
51 "\x78\x56", 20);
52 QuicSocketAddressCoder coder;
53 ASSERT_TRUE(coder.Decode(serialized.data(), serialized.length()));
54 EXPECT_EQ(AF_INET6, ConvertAddressFamily(GetAddressFamily(coder.ip())));
55 string expected_addr("\x20\x01\x07\x00\x03\x00\x18\x00"
56 "\x00\x00\x00\x00\x00\x00\x00\x0f", 16);
57 EXPECT_EQ(expected_addr, IPAddressToPackedString(coder.ip()));
58 EXPECT_EQ(0x5678, coder.port());
61 TEST(QuicSocketAddressCoderTest, DecodeBad) {
62 string serialized("\x0a\x00"
63 "\x20\x01\x07\x00\x03\x00\x18\x00"
64 "\x00\x00\x00\x00\x00\x00\x00\x0f"
65 "\x78\x56", 20);
66 QuicSocketAddressCoder coder;
67 EXPECT_TRUE(coder.Decode(serialized.data(), serialized.length()));
68 // Append junk.
69 serialized.push_back('\0');
70 EXPECT_FALSE(coder.Decode(serialized.data(), serialized.length()));
71 // Undo.
72 serialized.resize(20);
73 EXPECT_TRUE(coder.Decode(serialized.data(), serialized.length()));
75 // Set an unknown address family.
76 serialized[0] = '\x03';
77 EXPECT_FALSE(coder.Decode(serialized.data(), serialized.length()));
78 // Undo.
79 serialized[0] = '\x0a';
80 EXPECT_TRUE(coder.Decode(serialized.data(), serialized.length()));
82 // Truncate.
83 size_t len = serialized.length();
84 for (size_t i = 0; i < len; i++) {
85 ASSERT_FALSE(serialized.empty());
86 serialized.erase(serialized.length() - 1);
87 EXPECT_FALSE(coder.Decode(serialized.data(), serialized.length()));
89 EXPECT_TRUE(serialized.empty());
92 TEST(QuicSocketAddressCoderTest, EncodeAndDecode) {
93 struct {
94 const char* ip_literal;
95 uint16 port;
96 } test_case[] = {
97 { "93.184.216.119", 0x1234 },
98 { "199.204.44.194", 80 },
99 { "149.20.4.69", 443 },
100 { "127.0.0.1", 8080 },
101 { "2001:700:300:1800::", 0x5678 },
102 { "::1", 65534 },
105 for (size_t i = 0; i < arraysize(test_case); i++) {
106 IPAddressNumber ip;
107 ASSERT_TRUE(ParseIPLiteralToNumber(test_case[i].ip_literal, &ip));
108 QuicSocketAddressCoder encoder(IPEndPoint(ip, test_case[i].port));
109 string serialized = encoder.Encode();
111 QuicSocketAddressCoder decoder;
112 ASSERT_TRUE(decoder.Decode(serialized.data(), serialized.length()));
113 EXPECT_EQ(encoder.ip(), decoder.ip());
114 EXPECT_EQ(encoder.port(), decoder.port());
118 } // namespace test
119 } // namespace net