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_, |
| 64 | static_cast<mobile_provider_db *>(NULL))) {} |
| 65 | virtual void SetUp(); |
| 66 | virtual void TearDown(); |
| 67 | |
| 68 | void ReplaceSingletons() { |
| 69 | modem_->rtnl_handler_ = &rtnl_handler_; |
| 70 | modem_->proxy_factory_ = &proxy_factory_; |
| 71 | } |
| 72 | |
| 73 | protected: |
| 74 | class TestProxyFactory : public ProxyFactory { |
| 75 | public: |
| 76 | explicit TestProxyFactory(Modem1Test *test) : test_(test) {} |
| 77 | |
| 78 | virtual DBusPropertiesProxyInterface *CreateDBusPropertiesProxy( |
Jason Glasgow | 82f9ab3 | 2012-04-04 14:27:19 -0400 | [diff] [blame] | 79 | const string &/*path*/, |
| 80 | const string &/*service*/) { |
| 81 | return test_->proxy_.release(); |
| 82 | } |
| 83 | |
| 84 | private: |
| 85 | Modem1Test *test_; |
| 86 | }; |
| 87 | |
| 88 | MockGLib glib_; |
| 89 | MockControl control_interface_; |
| 90 | EventDispatcher dispatcher_; |
| 91 | MockMetrics metrics_; |
| 92 | MockManager manager_; |
| 93 | MockDeviceInfo info_; |
| 94 | scoped_ptr<MockDBusPropertiesProxy> proxy_; |
| 95 | TestProxyFactory proxy_factory_; |
| 96 | scoped_ptr<Modem1> modem_; |
| 97 | MockRTNLHandler rtnl_handler_; |
| 98 | ByteString expected_address_; |
| 99 | ScopedTempDir temp_dir_; |
| 100 | string device_; |
| 101 | }; |
| 102 | |
| 103 | void Modem1Test::SetUp() { |
| 104 | ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 105 | modem_->netfiles_path_ = temp_dir_.path(); |
| 106 | device_ = temp_dir_.path().Append("devices").Append(kLinkName).value(); |
| 107 | FilePath device_dir = FilePath(device_).Append("1-2/3-4"); |
| 108 | ASSERT_TRUE(file_util::CreateDirectory(device_dir)); |
| 109 | FilePath symlink(temp_dir_.path().Append(kLinkName)); |
| 110 | ASSERT_TRUE(file_util::CreateSymbolicLink(device_dir, symlink)); |
| 111 | |
| 112 | EXPECT_EQ(kOwner, modem_->owner_); |
Jason Glasgow | a585fc3 | 2012-06-06 11:04:09 -0400 | [diff] [blame] | 113 | EXPECT_EQ(kService, modem_->service_); |
Jason Glasgow | 82f9ab3 | 2012-04-04 14:27:19 -0400 | [diff] [blame] | 114 | EXPECT_EQ(kPath, modem_->path_); |
| 115 | ReplaceSingletons(); |
| 116 | expected_address_ = ByteString(kAddress, arraysize(kAddress)); |
| 117 | |
| 118 | EXPECT_CALL(rtnl_handler_, GetInterfaceIndex(kLinkName)). |
| 119 | WillRepeatedly(Return(kTestInterfaceIndex)); |
| 120 | |
| 121 | EXPECT_CALL(manager_, device_info()).WillRepeatedly(Return(&info_)); |
| 122 | EXPECT_CALL(info_, GetMACAddress(kTestInterfaceIndex, _)). |
| 123 | WillOnce(DoAll(SetArgumentPointee<1>(expected_address_), |
| 124 | Return(true))); |
| 125 | } |
| 126 | |
| 127 | void Modem1Test::TearDown() { |
| 128 | modem_.reset(); |
| 129 | } |
| 130 | |
| 131 | TEST_F(Modem1Test, CreateDeviceMM1) { |
Ben Chan | 876efd3 | 2012-09-28 15:25:13 -0700 | [diff] [blame] | 132 | DBusInterfaceToProperties properties; |
Jason Glasgow | 82f9ab3 | 2012-04-04 14:27:19 -0400 | [diff] [blame] | 133 | DBusPropertiesMap modem_properties; |
| 134 | DBus::Variant lock; |
| 135 | lock.writer().append_uint32(MM_MODEM_LOCK_NONE); |
| 136 | modem_properties[MM_MODEM_PROPERTY_UNLOCKREQUIRED] = lock; |
| 137 | DBus::Variant device_variant; |
| 138 | device_variant.writer().append_string(device_.c_str()); |
| 139 | modem_properties[MM_MODEM_PROPERTY_DEVICE] = device_variant; |
Ben Chan | 876efd3 | 2012-09-28 15:25:13 -0700 | [diff] [blame] | 140 | properties[MM_DBUS_INTERFACE_MODEM] = modem_properties; |
Jason Glasgow | 82f9ab3 | 2012-04-04 14:27:19 -0400 | [diff] [blame] | 141 | |
Ben Chan | 876efd3 | 2012-09-28 15:25:13 -0700 | [diff] [blame] | 142 | DBusPropertiesMap modem3gpp_properties; |
| 143 | DBus::Variant registration_state_variant; |
| 144 | registration_state_variant.writer().append_uint32( |
| 145 | MM_MODEM_3GPP_REGISTRATION_STATE_HOME); |
| 146 | modem3gpp_properties[MM_MODEM_MODEM3GPP_PROPERTY_REGISTRATIONSTATE] = |
| 147 | registration_state_variant; |
| 148 | properties[MM_DBUS_INTERFACE_MODEM_MODEM3GPP] = modem3gpp_properties; |
| 149 | |
| 150 | modem_->CreateDeviceMM1(properties); |
Jason Glasgow | 82f9ab3 | 2012-04-04 14:27:19 -0400 | [diff] [blame] | 151 | EXPECT_TRUE(modem_->device().get()); |
Ben Chan | 876efd3 | 2012-09-28 15:25:13 -0700 | [diff] [blame] | 152 | EXPECT_TRUE(modem_->device()->capability_->IsRegistered()); |
Jason Glasgow | 82f9ab3 | 2012-04-04 14:27:19 -0400 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | } // namespace shill |