Home | History | Annotate | Download | only in shill
      1 //
      2 // Copyright (C) 2013 The Android Open Source Project
      3 //
      4 // Licensed under the Apache License, Version 2.0 (the "License");
      5 // you may not use this file except in compliance with the License.
      6 // You may obtain a copy of the License at
      7 //
      8 //      http://www.apache.org/licenses/LICENSE-2.0
      9 //
     10 // Unless required by applicable law or agreed to in writing, software
     11 // distributed under the License is distributed on an "AS IS" BASIS,
     12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13 // See the License for the specific language governing permissions and
     14 // limitations under the License.
     15 //
     16 
     17 #include "shill/connection_info.h"
     18 
     19 #include <netinet/in.h>
     20 
     21 #include <gtest/gtest.h>
     22 
     23 namespace shill {
     24 
     25 namespace {
     26 
     27 const unsigned char kIPAddress1[] = { 192, 168, 1, 1 };
     28 const unsigned char kIPAddress2[] = { 192, 168, 1, 2 };
     29 const unsigned char kIPAddress3[] = { 192, 168, 1, 3 };
     30 const unsigned char kIPAddress4[] = { 192, 168, 1, 4 };
     31 const uint16_t kPort1 = 1000;
     32 const uint16_t kPort2 = 2000;
     33 const uint16_t kPort3 = 3000;
     34 const uint16_t kPort4 = 4000;
     35 
     36 }  // namespace
     37 
     38 class ConnectionInfoTest : public testing::Test {
     39  protected:
     40   void ExpectConnectionInfoEqual(const ConnectionInfo& info1,
     41                                  const ConnectionInfo& info2) {
     42     EXPECT_EQ(info1.protocol(), info2.protocol());
     43     EXPECT_EQ(info1.time_to_expire_seconds(), info2.time_to_expire_seconds());
     44     EXPECT_EQ(info1.is_unreplied(), info2.is_unreplied());
     45     EXPECT_TRUE(info1.original_source_ip_address()
     46                     .Equals(info2.original_source_ip_address()));
     47     EXPECT_EQ(info1.original_source_port(), info2.original_source_port());
     48     EXPECT_TRUE(info1.original_destination_ip_address()
     49                     .Equals(info2.original_destination_ip_address()));
     50     EXPECT_EQ(info1.original_destination_port(),
     51               info2.original_destination_port());
     52     EXPECT_TRUE(info1.reply_source_ip_address()
     53                     .Equals(info2.reply_source_ip_address()));
     54     EXPECT_EQ(info1.reply_source_port(), info2.reply_source_port());
     55     EXPECT_TRUE(info1.reply_destination_ip_address()
     56                     .Equals(info2.reply_destination_ip_address()));
     57     EXPECT_EQ(info1.reply_destination_port(), info2.reply_destination_port());
     58   }
     59 };
     60 
     61 TEST_F(ConnectionInfoTest, CopyConstructor) {
     62   ConnectionInfo info(IPPROTO_UDP,
     63                       10,
     64                       true,
     65                       IPAddress(IPAddress::kFamilyIPv4,
     66                                 ByteString(kIPAddress1, sizeof(kIPAddress1))),
     67                       kPort1,
     68                       IPAddress(IPAddress::kFamilyIPv4,
     69                                 ByteString(kIPAddress2, sizeof(kIPAddress2))),
     70                       kPort2,
     71                       IPAddress(IPAddress::kFamilyIPv4,
     72                                 ByteString(kIPAddress3, sizeof(kIPAddress3))),
     73                       kPort3,
     74                       IPAddress(IPAddress::kFamilyIPv4,
     75                                 ByteString(kIPAddress4, sizeof(kIPAddress4))),
     76                       kPort4);
     77 
     78   ConnectionInfo info_copy(info);
     79   ExpectConnectionInfoEqual(info, info_copy);
     80 }
     81 
     82 TEST_F(ConnectionInfoTest, AssignmentOperator) {
     83   ConnectionInfo info(IPPROTO_UDP,
     84                       10,
     85                       true,
     86                       IPAddress(IPAddress::kFamilyIPv4,
     87                                 ByteString(kIPAddress1, sizeof(kIPAddress1))),
     88                       kPort1,
     89                       IPAddress(IPAddress::kFamilyIPv4,
     90                                 ByteString(kIPAddress2, sizeof(kIPAddress2))),
     91                       kPort2,
     92                       IPAddress(IPAddress::kFamilyIPv4,
     93                                 ByteString(kIPAddress3, sizeof(kIPAddress3))),
     94                       kPort3,
     95                       IPAddress(IPAddress::kFamilyIPv4,
     96                                 ByteString(kIPAddress4, sizeof(kIPAddress4))),
     97                       kPort4);
     98 
     99   ConnectionInfo info_copy = info;
    100   ExpectConnectionInfoEqual(info, info_copy);
    101 }
    102 
    103 }  // namespace shill
    104