blob: c5b95ee9c0744739adb7f82f55335f31e804ad93 [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
7#include <base/scoped_temp_dir.h>
8#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"
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
27using std::string;
28using testing::_;
29using testing::DoAll;
30using testing::Return;
31using testing::SetArgumentPointee;
32using testing::Test;
33
34namespace shill {
35
36namespace {
37
38const int kTestInterfaceIndex = 5;
39const char kLinkName[] = "usb0";
40const char kOwner[] = ":1.18";
Jason Glasgowa585fc32012-06-06 11:04:09 -040041const char kService[] = "org.chromium.ModemManager";
Jason Glasgow82f9ab32012-04-04 14:27:19 -040042const char kPath[] = "/org/chromium/ModemManager/Gobi/0";
43const unsigned char kAddress[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05};
44const char kAddressAsString[] = "000102030405";
45
46} // namespace
47
48class 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 Glasgowa585fc32012-06-06 11:04:09 -040058 kService,
Jason Glasgow82f9ab32012-04-04 14:27:19 -040059 kPath,
60 &control_interface_,
61 &dispatcher_,
62 &metrics_,
63 &manager_,
Ben Chan62028b22012-11-05 11:20:02 -080064 static_cast<CellularOperatorInfo *>(NULL),
Jason Glasgow82f9ab32012-04-04 14:27:19 -040065 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 Glasgow82f9ab32012-04-04 14:27:19 -040080 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
104void 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 Glasgowa585fc32012-06-06 11:04:09 -0400114 EXPECT_EQ(kService, modem_->service_);
Jason Glasgow82f9ab32012-04-04 14:27:19 -0400115 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
128void Modem1Test::TearDown() {
129 modem_.reset();
130}
131
132TEST_F(Modem1Test, CreateDeviceMM1) {
Ben Chan876efd32012-09-28 15:25:13 -0700133 DBusInterfaceToProperties properties;
Jason Glasgow82f9ab32012-04-04 14:27:19 -0400134 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 Chan876efd32012-09-28 15:25:13 -0700141 properties[MM_DBUS_INTERFACE_MODEM] = modem_properties;
Jason Glasgow82f9ab32012-04-04 14:27:19 -0400142
Ben Chan876efd32012-09-28 15:25:13 -0700143 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 Glasgow82f9ab32012-04-04 14:27:19 -0400152 EXPECT_TRUE(modem_->device().get());
Ben Chan876efd32012-09-28 15:25:13 -0700153 EXPECT_TRUE(modem_->device()->capability_->IsRegistered());
Jason Glasgow82f9ab32012-04-04 14:27:19 -0400154}
155
156} // namespace shill