Home | History | Annotate | Download | only in cellular
      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/cellular/modem.h"
     18 
     19 #include <base/files/scoped_temp_dir.h>
     20 #include <ModemManager/ModemManager.h>
     21 
     22 #include "shill/cellular/cellular_capability.h"
     23 #include "shill/cellular/mock_cellular.h"
     24 #include "shill/cellular/mock_modem_info.h"
     25 #include "shill/manager.h"
     26 #include "shill/mock_control.h"
     27 #include "shill/mock_dbus_properties_proxy.h"
     28 #include "shill/mock_device_info.h"
     29 #include "shill/net/mock_rtnl_handler.h"
     30 #include "shill/net/rtnl_handler.h"
     31 #include "shill/test_event_dispatcher.h"
     32 #include "shill/testing.h"
     33 
     34 using base::FilePath;
     35 using std::string;
     36 using std::vector;
     37 using testing::_;
     38 using testing::DoAll;
     39 using testing::Return;
     40 using testing::SetArgumentPointee;
     41 using testing::Test;
     42 
     43 namespace shill {
     44 
     45 namespace {
     46 
     47 const int kTestInterfaceIndex = 5;
     48 const char kLinkName[] = "usb0";
     49 const char kService[] = "org.chromium.ModemManager";
     50 const char kPath[] = "/org/chromium/ModemManager/Gobi/0";
     51 const unsigned char kAddress[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05};
     52 
     53 }  // namespace
     54 
     55 class Modem1Test : public Test {
     56  public:
     57   Modem1Test()
     58       : modem_info_(nullptr, &dispatcher_, nullptr, nullptr),
     59         device_info_(modem_info_.control_interface(), modem_info_.dispatcher(),
     60                      modem_info_.metrics(), modem_info_.manager()),
     61         proxy_(new MockDBusPropertiesProxy()),
     62         modem_(
     63             new Modem1(
     64                 kService,
     65                 kPath,
     66                 &modem_info_,
     67                 &control_interface_)) {}
     68   virtual void SetUp();
     69   virtual void TearDown();
     70 
     71   void ReplaceSingletons() {
     72     modem_->rtnl_handler_ = &rtnl_handler_;
     73   }
     74 
     75  protected:
     76   EventDispatcherForTest dispatcher_;
     77   MockModemInfo modem_info_;
     78   MockDeviceInfo device_info_;
     79   std::unique_ptr<MockDBusPropertiesProxy> proxy_;
     80   MockControl control_interface_;
     81   std::unique_ptr<Modem1> modem_;
     82   MockRTNLHandler rtnl_handler_;
     83   ByteString expected_address_;
     84 };
     85 
     86 void Modem1Test::SetUp() {
     87   EXPECT_EQ(kService, modem_->service_);
     88   EXPECT_EQ(kPath, modem_->path_);
     89   ReplaceSingletons();
     90   expected_address_ = ByteString(kAddress, arraysize(kAddress));
     91 
     92   EXPECT_CALL(rtnl_handler_, GetInterfaceIndex(kLinkName)).
     93       WillRepeatedly(Return(kTestInterfaceIndex));
     94 
     95   EXPECT_CALL(*modem_info_.mock_manager(), device_info())
     96       .WillRepeatedly(Return(&device_info_));
     97   EXPECT_CALL(device_info_, GetMACAddress(kTestInterfaceIndex, _)).
     98       WillOnce(DoAll(SetArgumentPointee<1>(expected_address_),
     99                      Return(true)));
    100 }
    101 
    102 void Modem1Test::TearDown() {
    103   modem_.reset();
    104 }
    105 
    106 TEST_F(Modem1Test, CreateDeviceMM1) {
    107   InterfaceToProperties properties;
    108 
    109   KeyValueStore modem_properties;
    110   modem_properties.SetUint(MM_MODEM_PROPERTY_UNLOCKREQUIRED,
    111                            MM_MODEM_LOCK_NONE);
    112   vector<std::tuple<string, uint32_t>> ports = {
    113       std::make_tuple(kLinkName, MM_MODEM_PORT_TYPE_NET) };
    114   modem_properties.Set(MM_MODEM_PROPERTY_PORTS, brillo::Any(ports));
    115   properties[MM_DBUS_INTERFACE_MODEM] = modem_properties;
    116 
    117   KeyValueStore modem3gpp_properties;
    118   modem3gpp_properties.SetUint(
    119       MM_MODEM_MODEM3GPP_PROPERTY_REGISTRATIONSTATE,
    120       MM_MODEM_3GPP_REGISTRATION_STATE_HOME);
    121   properties[MM_DBUS_INTERFACE_MODEM_MODEM3GPP] = modem3gpp_properties;
    122 
    123   EXPECT_CALL(control_interface_, CreateDBusPropertiesProxy(kPath, kService))
    124       .WillOnce(ReturnAndReleasePointee(&proxy_));
    125   modem_->CreateDeviceMM1(properties);
    126   EXPECT_TRUE(modem_->device().get());
    127   EXPECT_TRUE(modem_->device()->capability_->IsRegistered());
    128 }
    129 
    130 }  // namespace shill
    131