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"
16 TEST(QuicSocketAddressCoderTest
, EncodeIPv4
) {
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
) {
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"
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"
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"
66 QuicSocketAddressCoder coder
;
67 EXPECT_TRUE(coder
.Decode(serialized
.data(), serialized
.length()));
69 serialized
.push_back('\0');
70 EXPECT_FALSE(coder
.Decode(serialized
.data(), serialized
.length()));
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()));
79 serialized
[0] = '\x0a';
80 EXPECT_TRUE(coder
.Decode(serialized
.data(), serialized
.length()));
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
) {
94 const char* ip_literal
;
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 },
105 for (size_t i
= 0; i
< arraysize(test_case
); i
++) {
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());