Home | History | Annotate | Download | only in ethernet
      1 //
      2 // Copyright (C) 2012 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/ethernet/ethernet_service.h"
     18 
     19 #include <gmock/gmock.h>
     20 #include <gtest/gtest.h>
     21 
     22 #include "shill/ethernet/mock_ethernet.h"
     23 #include "shill/mock_adaptors.h"
     24 #include "shill/mock_manager.h"
     25 #include "shill/mock_store.h"
     26 #include "shill/property_store_unittest.h"
     27 #include "shill/refptr_types.h"
     28 #include "shill/service_property_change_test.h"
     29 
     30 using ::testing::_;
     31 using ::testing::NiceMock;
     32 using ::testing::Return;
     33 
     34 namespace shill {
     35 
     36 class EthernetServiceTest : public PropertyStoreTest {
     37  public:
     38   EthernetServiceTest()
     39       : mock_manager_(control_interface(), dispatcher(), metrics()),
     40         ethernet_(
     41             new NiceMock<MockEthernet>(control_interface(),
     42                                        dispatcher(),
     43                                        metrics(),
     44                                        &mock_manager_,
     45                                        "ethernet",
     46                                        fake_mac,
     47                                        0)),
     48         service_(
     49             new EthernetService(control_interface(),
     50                                 dispatcher(),
     51                                 metrics(),
     52                                 &mock_manager_,
     53                                 ethernet_->weak_ptr_factory_.GetWeakPtr())) {}
     54   ~EthernetServiceTest() override {}
     55 
     56  protected:
     57   static const char fake_mac[];
     58 
     59   bool GetAutoConnect() {
     60     return service_->GetAutoConnect(nullptr);
     61   }
     62 
     63   bool SetAutoConnect(const bool connect, Error* error) {
     64     return service_->SetAutoConnectFull(connect, error);
     65   }
     66 
     67   ServiceMockAdaptor* GetAdaptor() {
     68     return static_cast<ServiceMockAdaptor*>(service_->adaptor());
     69   }
     70 
     71   MockManager mock_manager_;
     72   scoped_refptr<MockEthernet> ethernet_;
     73   EthernetServiceRefPtr service_;
     74 };
     75 
     76 // static
     77 const char EthernetServiceTest::fake_mac[] = "AaBBcCDDeeFF";
     78 
     79 TEST_F(EthernetServiceTest, AutoConnect) {
     80   EXPECT_TRUE(service_->IsAutoConnectByDefault());
     81   EXPECT_TRUE(GetAutoConnect());
     82   {
     83     Error error;
     84     SetAutoConnect(false, &error);
     85     EXPECT_FALSE(error.IsSuccess());
     86   }
     87   EXPECT_TRUE(GetAutoConnect());
     88   {
     89     Error error;
     90     SetAutoConnect(true, &error);
     91     EXPECT_TRUE(error.IsSuccess());
     92   }
     93   EXPECT_TRUE(GetAutoConnect());
     94 }
     95 
     96 TEST_F(EthernetServiceTest, ConnectDisconnectDelegation) {
     97   EXPECT_CALL(*ethernet_, link_up()).WillRepeatedly(Return(true));
     98   EXPECT_CALL(*ethernet_, ConnectTo(service_.get()));
     99   service_->AutoConnect();
    100   EXPECT_CALL(*ethernet_, DisconnectFrom(service_.get()));
    101   Error error;
    102   service_->Disconnect(&error, "in test");
    103 }
    104 
    105 TEST_F(EthernetServiceTest, PropertyChanges) {
    106   TestCommonPropertyChanges(service_, GetAdaptor());
    107 }
    108 
    109 // Custom property setters should return false, and make no changes, if
    110 // the new value is the same as the old value.
    111 TEST_F(EthernetServiceTest, CustomSetterNoopChange) {
    112   TestCustomSetterNoopChange(service_, &mock_manager_);
    113 }
    114 
    115 TEST_F(EthernetServiceTest, LoadAutoConnect) {
    116   // Make sure when we try to load an Ethernet service, it sets AutoConnect
    117   // to be true even if the property is not found.
    118   NiceMock<MockStore> mock_store;
    119   EXPECT_CALL(mock_store, ContainsGroup(_)).WillRepeatedly(Return(true));
    120   EXPECT_CALL(mock_store, GetBool(_, _, _)).WillRepeatedly(Return(false));
    121   EXPECT_TRUE(service_->Load(&mock_store));
    122   EXPECT_TRUE(GetAutoConnect());
    123 }
    124 
    125 TEST_F(EthernetServiceTest, GetTethering) {
    126   EXPECT_CALL(*ethernet_, IsConnectedViaTether())
    127       .WillOnce(Return(true))
    128       .WillOnce(Return(false));
    129   EXPECT_EQ(kTetheringConfirmedState, service_->GetTethering(nullptr));
    130   EXPECT_EQ(kTetheringNotDetectedState, service_->GetTethering(nullptr));
    131 }
    132 
    133 TEST_F(EthernetServiceTest, IsVisible) {
    134   EXPECT_CALL(*ethernet_, link_up())
    135       .WillOnce(Return(false))
    136       .WillOnce(Return(true));
    137   EXPECT_FALSE(service_->IsVisible());
    138   EXPECT_TRUE(service_->IsVisible());
    139 }
    140 
    141 TEST_F(EthernetServiceTest, IsAutoConnectable) {
    142   EXPECT_CALL(*ethernet_, link_up())
    143       .WillOnce(Return(false))
    144       .WillOnce(Return(true));
    145   const char* reason;
    146   EXPECT_FALSE(service_->IsAutoConnectable(&reason));
    147   EXPECT_STREQ("no carrier", reason);
    148   EXPECT_TRUE(service_->IsAutoConnectable(nullptr));
    149 }
    150 
    151 }  // namespace shill
    152