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/socket_info.h"
     18 
     19 #include <gtest/gtest.h>
     20 
     21 namespace shill {
     22 
     23 namespace {
     24 
     25 const unsigned char kIPAddress1[] = { 192, 168, 1, 1 };
     26 const unsigned char kIPAddress2[] = { 192, 168, 1, 2 };
     27 const unsigned char kIPAddress3[] = { 192, 168, 1, 3 };
     28 const uint16_t kPort1 = 1000;
     29 const uint16_t kPort2 = 2000;
     30 const uint16_t kPort3 = 3000;
     31 
     32 }  // namespace
     33 
     34 class SocketInfoTest : public testing::Test {
     35  protected:
     36   void ExpectSocketInfoEqual(const SocketInfo& info1, const SocketInfo& info2) {
     37     EXPECT_EQ(info1.connection_state(), info2.connection_state());
     38     EXPECT_TRUE(info1.local_ip_address().Equals(info2.local_ip_address()));
     39     EXPECT_EQ(info1.local_port(), info2.local_port());
     40     EXPECT_TRUE(info1.remote_ip_address().Equals(info2.remote_ip_address()));
     41     EXPECT_EQ(info1.remote_port(), info2.remote_port());
     42     EXPECT_EQ(info1.transmit_queue_value(), info2.transmit_queue_value());
     43     EXPECT_EQ(info1.receive_queue_value(), info2.receive_queue_value());
     44     EXPECT_EQ(info1.timer_state(), info2.timer_state());
     45   }
     46 };
     47 
     48 TEST_F(SocketInfoTest, CopyConstructor) {
     49   SocketInfo info(SocketInfo::kConnectionStateEstablished,
     50                   IPAddress(IPAddress::kFamilyIPv4,
     51                             ByteString(kIPAddress1, sizeof(kIPAddress1))),
     52                   kPort1,
     53                   IPAddress(IPAddress::kFamilyIPv4,
     54                             ByteString(kIPAddress2, sizeof(kIPAddress2))),
     55                   kPort2,
     56                   10,
     57                   20,
     58                   SocketInfo::kTimerStateRetransmitTimerPending);
     59 
     60   SocketInfo info_copy(info);
     61   ExpectSocketInfoEqual(info, info_copy);
     62 }
     63 
     64 TEST_F(SocketInfoTest, AssignmentOperator) {
     65   SocketInfo info(SocketInfo::kConnectionStateEstablished,
     66                   IPAddress(IPAddress::kFamilyIPv4,
     67                             ByteString(kIPAddress1, sizeof(kIPAddress1))),
     68                   kPort1,
     69                   IPAddress(IPAddress::kFamilyIPv4,
     70                             ByteString(kIPAddress2, sizeof(kIPAddress2))),
     71                   kPort2,
     72                   10,
     73                   20,
     74                   SocketInfo::kTimerStateRetransmitTimerPending);
     75 
     76   SocketInfo info_copy = info;
     77   ExpectSocketInfoEqual(info, info_copy);
     78 }
     79 
     80 TEST_F(SocketInfoTest, IsSameSocketAs) {
     81   IPAddress ip_address1(IPAddress::kFamilyIPv4,
     82                         ByteString(kIPAddress1, sizeof(kIPAddress1)));
     83   IPAddress ip_address2(IPAddress::kFamilyIPv4,
     84                         ByteString(kIPAddress2, sizeof(kIPAddress2)));
     85   IPAddress ip_address3(IPAddress::kFamilyIPv4,
     86                         ByteString(kIPAddress3, sizeof(kIPAddress3)));
     87 
     88   SocketInfo info(SocketInfo::kConnectionStateEstablished,
     89                   ip_address1,
     90                   kPort1,
     91                   ip_address2,
     92                   kPort2,
     93                   0,
     94                   0,
     95                   SocketInfo::kTimerStateNoTimerPending);
     96 
     97   // Differs only by local address.
     98   EXPECT_FALSE(info.IsSameSocketAs(
     99       SocketInfo(SocketInfo::kConnectionStateEstablished,
    100                  ip_address3,
    101                  kPort1,
    102                  ip_address2,
    103                  kPort2,
    104                  0,
    105                  0,
    106                  SocketInfo::kTimerStateNoTimerPending)));
    107 
    108   // Differs only by local port.
    109   EXPECT_FALSE(info.IsSameSocketAs(
    110       SocketInfo(SocketInfo::kConnectionStateEstablished,
    111                  ip_address1,
    112                  kPort3,
    113                  ip_address2,
    114                  kPort2,
    115                  0,
    116                  0,
    117                  SocketInfo::kTimerStateNoTimerPending)));
    118 
    119   // Differs only by remote address.
    120   EXPECT_FALSE(info.IsSameSocketAs(
    121       SocketInfo(SocketInfo::kConnectionStateEstablished,
    122                  ip_address1,
    123                  kPort1,
    124                  ip_address3,
    125                  kPort2,
    126                  0,
    127                  0,
    128                  SocketInfo::kTimerStateNoTimerPending)));
    129 
    130   // Differs only by remote port.
    131   EXPECT_FALSE(info.IsSameSocketAs(
    132       SocketInfo(SocketInfo::kConnectionStateEstablished,
    133                  ip_address1,
    134                  kPort1,
    135                  ip_address2,
    136                  kPort3,
    137                  0,
    138                  0,
    139                  SocketInfo::kTimerStateNoTimerPending)));
    140 
    141   // Only local address, local port, remote address, and remote port are
    142   // identical.
    143   EXPECT_TRUE(info.IsSameSocketAs(
    144       SocketInfo(SocketInfo::kConnectionStateClosing,
    145                  ip_address1,
    146                  kPort1,
    147                  ip_address2,
    148                  kPort2,
    149                  10,
    150                  20,
    151                  SocketInfo::kTimerStateRetransmitTimerPending)));
    152 }
    153 
    154 }  // namespace shill
    155