Jason Glasgow | 82f9ab3 | 2012-04-04 14:27:19 -0400 | [diff] [blame] | 1 | // Copyright (c) 2012 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/modem.h" |
| 6 | |
Paul Stewart | 5ad1606 | 2013-02-21 18:10:48 -0800 | [diff] [blame] | 7 | #include <base/files/scoped_temp_dir.h> |
Jason Glasgow | 82f9ab3 | 2012-04-04 14:27:19 -0400 | [diff] [blame] | 8 | #include <gmock/gmock.h> |
| 9 | #include <gtest/gtest.h> |
Ben Chan | 5c853ef | 2012-10-05 00:05:37 -0700 | [diff] [blame] | 10 | #include <ModemManager/ModemManager.h> |
Jason Glasgow | 82f9ab3 | 2012-04-04 14:27:19 -0400 | [diff] [blame] | 11 | |
Ben Chan | 876efd3 | 2012-09-28 15:25:13 -0700 | [diff] [blame] | 12 | #include "shill/cellular_capability.h" |
Jason Glasgow | 82f9ab3 | 2012-04-04 14:27:19 -0400 | [diff] [blame] | 13 | #include "shill/dbus_property_matchers.h" |
| 14 | #include "shill/event_dispatcher.h" |
| 15 | #include "shill/manager.h" |
| 16 | #include "shill/mock_cellular.h" |
Jason Glasgow | 82f9ab3 | 2012-04-04 14:27:19 -0400 | [diff] [blame] | 17 | #include "shill/mock_dbus_properties_proxy.h" |
| 18 | #include "shill/mock_device_info.h" |
Prathmesh Prabhu | 27526f1 | 2013-03-25 19:42:18 -0700 | [diff] [blame] | 19 | #include "shill/mock_modem_info.h" |
Jason Glasgow | 82f9ab3 | 2012-04-04 14:27:19 -0400 | [diff] [blame] | 20 | #include "shill/mock_rtnl_handler.h" |
| 21 | #include "shill/proxy_factory.h" |
| 22 | #include "shill/rtnl_handler.h" |
| 23 | |
Albert Chaulk | 0e1cdea | 2013-02-27 15:32:55 -0800 | [diff] [blame] | 24 | using base::FilePath; |
Jason Glasgow | 82f9ab3 | 2012-04-04 14:27:19 -0400 | [diff] [blame] | 25 | using std::string; |
| 26 | using testing::_; |
| 27 | using testing::DoAll; |
| 28 | using testing::Return; |
| 29 | using testing::SetArgumentPointee; |
| 30 | using testing::Test; |
| 31 | |
| 32 | namespace shill { |
| 33 | |
| 34 | namespace { |
| 35 | |
| 36 | const int kTestInterfaceIndex = 5; |
| 37 | const char kLinkName[] = "usb0"; |
| 38 | const char kOwner[] = ":1.18"; |
Jason Glasgow | a585fc3 | 2012-06-06 11:04:09 -0400 | [diff] [blame] | 39 | const char kService[] = "org.chromium.ModemManager"; |
Jason Glasgow | 82f9ab3 | 2012-04-04 14:27:19 -0400 | [diff] [blame] | 40 | const char kPath[] = "/org/chromium/ModemManager/Gobi/0"; |
| 41 | const unsigned char kAddress[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05}; |
| 42 | const char kAddressAsString[] = "000102030405"; |
| 43 | |
| 44 | } // namespace |
| 45 | |
| 46 | class Modem1Test : public Test { |
| 47 | public: |
| 48 | Modem1Test() |
Prathmesh Prabhu | 27526f1 | 2013-03-25 19:42:18 -0700 | [diff] [blame] | 49 | : modem_info_(NULL, &dispatcher_, NULL, NULL, NULL), |
| 50 | device_info_(modem_info_.control_interface(), modem_info_.dispatcher(), |
| 51 | modem_info_.metrics(), modem_info_.manager()), |
Jason Glasgow | 82f9ab3 | 2012-04-04 14:27:19 -0400 | [diff] [blame] | 52 | proxy_(new MockDBusPropertiesProxy()), |
| 53 | proxy_factory_(this), |
| 54 | modem_( |
| 55 | new Modem1( |
| 56 | kOwner, |
Jason Glasgow | a585fc3 | 2012-06-06 11:04:09 -0400 | [diff] [blame] | 57 | kService, |
Jason Glasgow | 82f9ab3 | 2012-04-04 14:27:19 -0400 | [diff] [blame] | 58 | kPath, |
Prathmesh Prabhu | 27526f1 | 2013-03-25 19:42:18 -0700 | [diff] [blame] | 59 | &modem_info_)) {} |
Jason Glasgow | 82f9ab3 | 2012-04-04 14:27:19 -0400 | [diff] [blame] | 60 | virtual void SetUp(); |
| 61 | virtual void TearDown(); |
| 62 | |
| 63 | void ReplaceSingletons() { |
| 64 | modem_->rtnl_handler_ = &rtnl_handler_; |
| 65 | modem_->proxy_factory_ = &proxy_factory_; |
| 66 | } |
| 67 | |
| 68 | protected: |
| 69 | class TestProxyFactory : public ProxyFactory { |
| 70 | public: |
| 71 | explicit TestProxyFactory(Modem1Test *test) : test_(test) {} |
| 72 | |
| 73 | virtual DBusPropertiesProxyInterface *CreateDBusPropertiesProxy( |
Jason Glasgow | 82f9ab3 | 2012-04-04 14:27:19 -0400 | [diff] [blame] | 74 | const string &/*path*/, |
| 75 | const string &/*service*/) { |
| 76 | return test_->proxy_.release(); |
| 77 | } |
| 78 | |
| 79 | private: |
| 80 | Modem1Test *test_; |
| 81 | }; |
| 82 | |
Jason Glasgow | 82f9ab3 | 2012-04-04 14:27:19 -0400 | [diff] [blame] | 83 | EventDispatcher dispatcher_; |
Prathmesh Prabhu | 27526f1 | 2013-03-25 19:42:18 -0700 | [diff] [blame] | 84 | MockModemInfo modem_info_; |
| 85 | MockDeviceInfo device_info_; |
Jason Glasgow | 82f9ab3 | 2012-04-04 14:27:19 -0400 | [diff] [blame] | 86 | scoped_ptr<MockDBusPropertiesProxy> proxy_; |
| 87 | TestProxyFactory proxy_factory_; |
| 88 | scoped_ptr<Modem1> modem_; |
| 89 | MockRTNLHandler rtnl_handler_; |
| 90 | ByteString expected_address_; |
Paul Stewart | 5ad1606 | 2013-02-21 18:10:48 -0800 | [diff] [blame] | 91 | base::ScopedTempDir temp_dir_; |
Jason Glasgow | 82f9ab3 | 2012-04-04 14:27:19 -0400 | [diff] [blame] | 92 | string device_; |
| 93 | }; |
| 94 | |
| 95 | void Modem1Test::SetUp() { |
| 96 | ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 97 | modem_->netfiles_path_ = temp_dir_.path(); |
| 98 | device_ = temp_dir_.path().Append("devices").Append(kLinkName).value(); |
| 99 | FilePath device_dir = FilePath(device_).Append("1-2/3-4"); |
| 100 | ASSERT_TRUE(file_util::CreateDirectory(device_dir)); |
| 101 | FilePath symlink(temp_dir_.path().Append(kLinkName)); |
| 102 | ASSERT_TRUE(file_util::CreateSymbolicLink(device_dir, symlink)); |
| 103 | |
| 104 | EXPECT_EQ(kOwner, modem_->owner_); |
Jason Glasgow | a585fc3 | 2012-06-06 11:04:09 -0400 | [diff] [blame] | 105 | EXPECT_EQ(kService, modem_->service_); |
Jason Glasgow | 82f9ab3 | 2012-04-04 14:27:19 -0400 | [diff] [blame] | 106 | EXPECT_EQ(kPath, modem_->path_); |
| 107 | ReplaceSingletons(); |
| 108 | expected_address_ = ByteString(kAddress, arraysize(kAddress)); |
| 109 | |
| 110 | EXPECT_CALL(rtnl_handler_, GetInterfaceIndex(kLinkName)). |
| 111 | WillRepeatedly(Return(kTestInterfaceIndex)); |
| 112 | |
Prathmesh Prabhu | 27526f1 | 2013-03-25 19:42:18 -0700 | [diff] [blame] | 113 | EXPECT_CALL(*modem_info_.mock_manager(), device_info()) |
| 114 | .WillRepeatedly(Return(&device_info_)); |
| 115 | EXPECT_CALL(device_info_, GetMACAddress(kTestInterfaceIndex, _)). |
Jason Glasgow | 82f9ab3 | 2012-04-04 14:27:19 -0400 | [diff] [blame] | 116 | WillOnce(DoAll(SetArgumentPointee<1>(expected_address_), |
| 117 | Return(true))); |
| 118 | } |
| 119 | |
| 120 | void Modem1Test::TearDown() { |
| 121 | modem_.reset(); |
| 122 | } |
| 123 | |
| 124 | TEST_F(Modem1Test, CreateDeviceMM1) { |
Ben Chan | 876efd3 | 2012-09-28 15:25:13 -0700 | [diff] [blame] | 125 | DBusInterfaceToProperties properties; |
Jason Glasgow | 82f9ab3 | 2012-04-04 14:27:19 -0400 | [diff] [blame] | 126 | DBusPropertiesMap modem_properties; |
| 127 | DBus::Variant lock; |
| 128 | lock.writer().append_uint32(MM_MODEM_LOCK_NONE); |
| 129 | modem_properties[MM_MODEM_PROPERTY_UNLOCKREQUIRED] = lock; |
| 130 | DBus::Variant device_variant; |
| 131 | device_variant.writer().append_string(device_.c_str()); |
| 132 | modem_properties[MM_MODEM_PROPERTY_DEVICE] = device_variant; |
Ben Chan | 876efd3 | 2012-09-28 15:25:13 -0700 | [diff] [blame] | 133 | properties[MM_DBUS_INTERFACE_MODEM] = modem_properties; |
Jason Glasgow | 82f9ab3 | 2012-04-04 14:27:19 -0400 | [diff] [blame] | 134 | |
Ben Chan | 876efd3 | 2012-09-28 15:25:13 -0700 | [diff] [blame] | 135 | DBusPropertiesMap modem3gpp_properties; |
| 136 | DBus::Variant registration_state_variant; |
| 137 | registration_state_variant.writer().append_uint32( |
| 138 | MM_MODEM_3GPP_REGISTRATION_STATE_HOME); |
| 139 | modem3gpp_properties[MM_MODEM_MODEM3GPP_PROPERTY_REGISTRATIONSTATE] = |
| 140 | registration_state_variant; |
| 141 | properties[MM_DBUS_INTERFACE_MODEM_MODEM3GPP] = modem3gpp_properties; |
| 142 | |
| 143 | modem_->CreateDeviceMM1(properties); |
Jason Glasgow | 82f9ab3 | 2012-04-04 14:27:19 -0400 | [diff] [blame] | 144 | EXPECT_TRUE(modem_->device().get()); |
Ben Chan | 876efd3 | 2012-09-28 15:25:13 -0700 | [diff] [blame] | 145 | EXPECT_TRUE(modem_->device()->capability_->IsRegistered()); |
Jason Glasgow | 82f9ab3 | 2012-04-04 14:27:19 -0400 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | } // namespace shill |