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