blob: 487c958859e0264ce0af73c644a4cc353e5473b7 [file] [log] [blame]
Jason Glasgow82f9ab32012-04-04 14:27:19 -04001// 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 Stewart5ad16062013-02-21 18:10:48 -08007#include <base/files/scoped_temp_dir.h>
Jason Glasgow82f9ab32012-04-04 14:27:19 -04008#include <gmock/gmock.h>
9#include <gtest/gtest.h>
Ben Chan5c853ef2012-10-05 00:05:37 -070010#include <ModemManager/ModemManager.h>
Jason Glasgow82f9ab32012-04-04 14:27:19 -040011
Ben Chan876efd32012-09-28 15:25:13 -070012#include "shill/cellular_capability.h"
Jason Glasgow82f9ab32012-04-04 14:27:19 -040013#include "shill/dbus_property_matchers.h"
14#include "shill/event_dispatcher.h"
15#include "shill/manager.h"
16#include "shill/mock_cellular.h"
Jason Glasgow82f9ab32012-04-04 14:27:19 -040017#include "shill/mock_dbus_properties_proxy.h"
18#include "shill/mock_device_info.h"
Prathmesh Prabhu27526f12013-03-25 19:42:18 -070019#include "shill/mock_modem_info.h"
Jason Glasgow82f9ab32012-04-04 14:27:19 -040020#include "shill/mock_rtnl_handler.h"
21#include "shill/proxy_factory.h"
22#include "shill/rtnl_handler.h"
23
Albert Chaulk0e1cdea2013-02-27 15:32:55 -080024using base::FilePath;
Jason Glasgow82f9ab32012-04-04 14:27:19 -040025using std::string;
26using testing::_;
27using testing::DoAll;
28using testing::Return;
29using testing::SetArgumentPointee;
30using testing::Test;
31
32namespace shill {
33
34namespace {
35
36const int kTestInterfaceIndex = 5;
37const char kLinkName[] = "usb0";
38const char kOwner[] = ":1.18";
Jason Glasgowa585fc32012-06-06 11:04:09 -040039const char kService[] = "org.chromium.ModemManager";
Jason Glasgow82f9ab32012-04-04 14:27:19 -040040const char kPath[] = "/org/chromium/ModemManager/Gobi/0";
41const unsigned char kAddress[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05};
42const char kAddressAsString[] = "000102030405";
43
44} // namespace
45
46class Modem1Test : public Test {
47 public:
48 Modem1Test()
Prathmesh Prabhu27526f12013-03-25 19:42:18 -070049 : 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 Glasgow82f9ab32012-04-04 14:27:19 -040052 proxy_(new MockDBusPropertiesProxy()),
53 proxy_factory_(this),
54 modem_(
55 new Modem1(
56 kOwner,
Jason Glasgowa585fc32012-06-06 11:04:09 -040057 kService,
Jason Glasgow82f9ab32012-04-04 14:27:19 -040058 kPath,
Prathmesh Prabhu27526f12013-03-25 19:42:18 -070059 &modem_info_)) {}
Jason Glasgow82f9ab32012-04-04 14:27:19 -040060 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 Glasgow82f9ab32012-04-04 14:27:19 -040074 const string &/*path*/,
75 const string &/*service*/) {
76 return test_->proxy_.release();
77 }
78
79 private:
80 Modem1Test *test_;
81 };
82
Jason Glasgow82f9ab32012-04-04 14:27:19 -040083 EventDispatcher dispatcher_;
Prathmesh Prabhu27526f12013-03-25 19:42:18 -070084 MockModemInfo modem_info_;
85 MockDeviceInfo device_info_;
Jason Glasgow82f9ab32012-04-04 14:27:19 -040086 scoped_ptr<MockDBusPropertiesProxy> proxy_;
87 TestProxyFactory proxy_factory_;
88 scoped_ptr<Modem1> modem_;
89 MockRTNLHandler rtnl_handler_;
90 ByteString expected_address_;
Paul Stewart5ad16062013-02-21 18:10:48 -080091 base::ScopedTempDir temp_dir_;
Jason Glasgow82f9ab32012-04-04 14:27:19 -040092 string device_;
93};
94
95void 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 Glasgowa585fc32012-06-06 11:04:09 -0400105 EXPECT_EQ(kService, modem_->service_);
Jason Glasgow82f9ab32012-04-04 14:27:19 -0400106 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 Prabhu27526f12013-03-25 19:42:18 -0700113 EXPECT_CALL(*modem_info_.mock_manager(), device_info())
114 .WillRepeatedly(Return(&device_info_));
115 EXPECT_CALL(device_info_, GetMACAddress(kTestInterfaceIndex, _)).
Jason Glasgow82f9ab32012-04-04 14:27:19 -0400116 WillOnce(DoAll(SetArgumentPointee<1>(expected_address_),
117 Return(true)));
118}
119
120void Modem1Test::TearDown() {
121 modem_.reset();
122}
123
124TEST_F(Modem1Test, CreateDeviceMM1) {
Ben Chan876efd32012-09-28 15:25:13 -0700125 DBusInterfaceToProperties properties;
Jason Glasgow82f9ab32012-04-04 14:27:19 -0400126 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 Chan876efd32012-09-28 15:25:13 -0700133 properties[MM_DBUS_INTERFACE_MODEM] = modem_properties;
Jason Glasgow82f9ab32012-04-04 14:27:19 -0400134
Ben Chan876efd32012-09-28 15:25:13 -0700135 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 Glasgow82f9ab32012-04-04 14:27:19 -0400144 EXPECT_TRUE(modem_->device().get());
Ben Chan876efd32012-09-28 15:25:13 -0700145 EXPECT_TRUE(modem_->device()->capability_->IsRegistered());
Jason Glasgow82f9ab32012-04-04 14:27:19 -0400146}
147
148} // namespace shill