Ben Chan | 0364fb6 | 2013-04-12 13:04:24 -0700 | [diff] [blame] | 1 | // Copyright (c) 2013 The Chromium OS 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 "shill/connection_info.h" |
| 6 | |
| 7 | #include <netinet/in.h> |
| 8 | |
| 9 | #include <gtest/gtest.h> |
| 10 | |
| 11 | namespace shill { |
| 12 | |
| 13 | namespace { |
| 14 | |
| 15 | const unsigned char kIPAddress1[] = { 192, 168, 1, 1 }; |
| 16 | const unsigned char kIPAddress2[] = { 192, 168, 1, 2 }; |
| 17 | const unsigned char kIPAddress3[] = { 192, 168, 1, 3 }; |
| 18 | const unsigned char kIPAddress4[] = { 192, 168, 1, 4 }; |
| 19 | const uint16 kPort1 = 1000; |
| 20 | const uint16 kPort2 = 2000; |
| 21 | const uint16 kPort3 = 3000; |
| 22 | const uint16 kPort4 = 4000; |
| 23 | |
| 24 | } // namespace |
| 25 | |
| 26 | class ConnectionInfoTest : public testing::Test { |
| 27 | protected: |
| 28 | void ExpectConnectionInfoEqual(const ConnectionInfo &info1, |
| 29 | const ConnectionInfo &info2) { |
| 30 | EXPECT_EQ(info1.protocol(), info2.protocol()); |
| 31 | EXPECT_EQ(info1.time_to_expire_seconds(), info2.time_to_expire_seconds()); |
| 32 | EXPECT_EQ(info1.is_unreplied(), info2.is_unreplied()); |
| 33 | EXPECT_TRUE(info1.original_source_ip_address() |
| 34 | .Equals(info2.original_source_ip_address())); |
| 35 | EXPECT_EQ(info1.original_source_port(), info2.original_source_port()); |
| 36 | EXPECT_TRUE(info1.original_destination_ip_address() |
| 37 | .Equals(info2.original_destination_ip_address())); |
| 38 | EXPECT_EQ(info1.original_destination_port(), |
| 39 | info2.original_destination_port()); |
| 40 | EXPECT_TRUE(info1.reply_source_ip_address() |
| 41 | .Equals(info2.reply_source_ip_address())); |
| 42 | EXPECT_EQ(info1.reply_source_port(), info2.reply_source_port()); |
| 43 | EXPECT_TRUE(info1.reply_destination_ip_address() |
| 44 | .Equals(info2.reply_destination_ip_address())); |
| 45 | EXPECT_EQ(info1.reply_destination_port(), info2.reply_destination_port()); |
| 46 | } |
| 47 | }; |
| 48 | |
| 49 | TEST_F(ConnectionInfoTest, CopyConstructor) { |
| 50 | ConnectionInfo info(IPPROTO_UDP, |
| 51 | 10, |
| 52 | true, |
| 53 | IPAddress(IPAddress::kFamilyIPv4, |
| 54 | ByteString(kIPAddress1, sizeof(kIPAddress1))), |
| 55 | kPort1, |
| 56 | IPAddress(IPAddress::kFamilyIPv4, |
| 57 | ByteString(kIPAddress2, sizeof(kIPAddress2))), |
| 58 | kPort2, |
| 59 | IPAddress(IPAddress::kFamilyIPv4, |
| 60 | ByteString(kIPAddress3, sizeof(kIPAddress3))), |
| 61 | kPort3, |
| 62 | IPAddress(IPAddress::kFamilyIPv4, |
| 63 | ByteString(kIPAddress4, sizeof(kIPAddress4))), |
| 64 | kPort4); |
| 65 | |
| 66 | ConnectionInfo info_copy(info); |
| 67 | ExpectConnectionInfoEqual(info, info_copy); |
| 68 | } |
| 69 | |
| 70 | TEST_F(ConnectionInfoTest, AssignmentOperator) { |
| 71 | ConnectionInfo info(IPPROTO_UDP, |
| 72 | 10, |
| 73 | true, |
| 74 | IPAddress(IPAddress::kFamilyIPv4, |
| 75 | ByteString(kIPAddress1, sizeof(kIPAddress1))), |
| 76 | kPort1, |
| 77 | IPAddress(IPAddress::kFamilyIPv4, |
| 78 | ByteString(kIPAddress2, sizeof(kIPAddress2))), |
| 79 | kPort2, |
| 80 | IPAddress(IPAddress::kFamilyIPv4, |
| 81 | ByteString(kIPAddress3, sizeof(kIPAddress3))), |
| 82 | kPort3, |
| 83 | IPAddress(IPAddress::kFamilyIPv4, |
| 84 | ByteString(kIPAddress4, sizeof(kIPAddress4))), |
| 85 | kPort4); |
| 86 | |
| 87 | ConnectionInfo info_copy = info; |
| 88 | ExpectConnectionInfoEqual(info, info_copy); |
| 89 | } |
| 90 | |
| 91 | } // namespace shill |