blob: c9cf13c103f87f99a2de75ec0884e229a1902cfb [file] [log] [blame]
Darin Petkov41c0e0a2012-01-09 16:38:53 +01001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Darin Petkove0a312e2011-07-20 13:45:28 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Jason Glasgow82f9ab32012-04-04 14:27:19 -04005#include "shill/modem.h"
6
Darin Petkov6f9eaa32011-08-09 15:26:44 -07007#include <vector>
8
Chris Masone626719f2011-08-18 16:58:48 -07009#include <gmock/gmock.h>
Darin Petkove0a312e2011-07-20 13:45:28 -070010#include <gtest/gtest.h>
11#include <mm/mm-modem.h>
12#include <net/if.h>
13#include <sys/ioctl.h>
14
15#include "shill/cellular.h"
Darin Petkov721ac932011-11-16 15:43:09 +010016#include "shill/cellular_capability_gsm.h"
David Rochbergfa1d31d2012-03-20 10:38:07 -040017#include "shill/dbus_property_matchers.h"
Paul Stewart26b327e2011-10-19 11:38:09 -070018#include "shill/event_dispatcher.h"
Darin Petkove0a312e2011-07-20 13:45:28 -070019#include "shill/manager.h"
David Rochbergfa1d31d2012-03-20 10:38:07 -040020#include "shill/mock_cellular.h"
Darin Petkove0a312e2011-07-20 13:45:28 -070021#include "shill/mock_control.h"
22#include "shill/mock_dbus_properties_proxy.h"
David Rochbergfa1d31d2012-03-20 10:38:07 -040023#include "shill/mock_device_info.h"
Darin Petkove0a312e2011-07-20 13:45:28 -070024#include "shill/mock_glib.h"
Chris Masone626719f2011-08-18 16:58:48 -070025#include "shill/mock_manager.h"
Thieu Le3426c8f2012-01-11 17:35:11 -080026#include "shill/mock_metrics.h"
David Rochbergfa1d31d2012-03-20 10:38:07 -040027#include "shill/mock_modem.h"
28#include "shill/mock_rtnl_handler.h"
Darin Petkove0a312e2011-07-20 13:45:28 -070029#include "shill/proxy_factory.h"
30#include "shill/rtnl_handler.h"
Darin Petkove0a312e2011-07-20 13:45:28 -070031
32using std::string;
Darin Petkov6f9eaa32011-08-09 15:26:44 -070033using std::vector;
Darin Petkove0a312e2011-07-20 13:45:28 -070034using testing::_;
35using testing::DoAll;
36using testing::Return;
Chris Masone626719f2011-08-18 16:58:48 -070037using testing::SetArgumentPointee;
David Rochbergfa1d31d2012-03-20 10:38:07 -040038using testing::StrEq;
Darin Petkove0a312e2011-07-20 13:45:28 -070039using testing::StrictMock;
40using testing::Test;
41
42namespace shill {
43
44namespace {
45
46const int kTestInterfaceIndex = 5;
David Rochbergfa1d31d2012-03-20 10:38:07 -040047const char kLinkName[] = "usb0";
48const char kOwner[] = ":1.18";
49const char kPath[] = "/org/chromium/ModemManager/Gobi/0";
50const unsigned char kAddress[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05};
51const char kAddressAsString[] = "000102030405";
Darin Petkove0a312e2011-07-20 13:45:28 -070052
53ACTION(SetInterfaceIndex) {
54 if (arg2) {
55 reinterpret_cast<struct ifreq *>(arg2)->ifr_ifindex = kTestInterfaceIndex;
56 }
57}
58
59} // namespace
60
61class ModemTest : public Test {
62 public:
63 ModemTest()
Thieu Le3426c8f2012-01-11 17:35:11 -080064 : manager_(&control_interface_, &dispatcher_, &metrics_, &glib_),
Jason Glasgowe9089492012-02-23 17:57:37 -050065 info_(&control_interface_, &dispatcher_, &metrics_, &manager_),
Darin Petkov3b292332011-07-28 14:17:24 -070066 proxy_(new MockDBusPropertiesProxy()),
67 proxy_factory_(this),
David Rochbergfa1d31d2012-03-20 10:38:07 -040068 modem_(
69 new StrictModem(
70 kOwner,
71 kPath,
72 &control_interface_,
73 &dispatcher_,
74 &metrics_,
75 &manager_,
76 static_cast<mobile_provider_db *>(NULL))) {}
Darin Petkove0a312e2011-07-20 13:45:28 -070077 virtual void SetUp();
78 virtual void TearDown();
79
David Rochbergfa1d31d2012-03-20 10:38:07 -040080 void ReplaceSingletons() {
81 modem_->rtnl_handler_ = &rtnl_handler_;
Darin Petkove0a312e2011-07-20 13:45:28 -070082 }
83
84 protected:
85 class TestProxyFactory : public ProxyFactory {
86 public:
Paul Stewart7355ce12011-09-02 10:47:01 -070087 explicit TestProxyFactory(ModemTest *test) : test_(test) {}
Darin Petkove0a312e2011-07-20 13:45:28 -070088
89 virtual DBusPropertiesProxyInterface *CreateDBusPropertiesProxy(
mukesh agrawal1830fa12011-09-26 14:31:40 -070090 const string &/*path*/,
91 const string &/*service*/) {
Darin Petkov3b292332011-07-28 14:17:24 -070092 return test_->proxy_.release();
Darin Petkove0a312e2011-07-20 13:45:28 -070093 }
94
95 private:
Darin Petkov3b292332011-07-28 14:17:24 -070096 ModemTest *test_;
Darin Petkove0a312e2011-07-20 13:45:28 -070097 };
98
Darin Petkov721ac932011-11-16 15:43:09 +010099 CellularCapabilityGSM *GetCapabilityGSM() {
100 return dynamic_cast<CellularCapabilityGSM *>(
Jason Glasgowe9089492012-02-23 17:57:37 -0500101 modem_->device_->capability_.get());
Darin Petkov721ac932011-11-16 15:43:09 +0100102 }
103
Darin Petkove0a312e2011-07-20 13:45:28 -0700104 MockGLib glib_;
105 MockControl control_interface_;
106 EventDispatcher dispatcher_;
Thieu Le3426c8f2012-01-11 17:35:11 -0800107 MockMetrics metrics_;
Chris Masone626719f2011-08-18 16:58:48 -0700108 MockManager manager_;
Jason Glasgowe9089492012-02-23 17:57:37 -0500109 MockDeviceInfo info_;
Darin Petkov3b292332011-07-28 14:17:24 -0700110 scoped_ptr<MockDBusPropertiesProxy> proxy_;
Darin Petkove0a312e2011-07-20 13:45:28 -0700111 TestProxyFactory proxy_factory_;
David Rochbergfa1d31d2012-03-20 10:38:07 -0400112 scoped_ptr<StrictModem> modem_;
113 MockRTNLHandler rtnl_handler_;
114 ByteString expected_address_;
Darin Petkove0a312e2011-07-20 13:45:28 -0700115};
116
Darin Petkove0a312e2011-07-20 13:45:28 -0700117void ModemTest::SetUp() {
Jason Glasgowe9089492012-02-23 17:57:37 -0500118 EXPECT_EQ(kOwner, modem_->owner_);
119 EXPECT_EQ(kPath, modem_->path_);
David Rochbergfa1d31d2012-03-20 10:38:07 -0400120 ReplaceSingletons();
121 expected_address_ = ByteString(kAddress, arraysize(kAddress));
122
123 EXPECT_CALL(rtnl_handler_, GetInterfaceIndex(kLinkName)).
124 WillRepeatedly(Return(kTestInterfaceIndex));
125
David Rochbergfa1d31d2012-03-20 10:38:07 -0400126 EXPECT_CALL(manager_, device_info()).WillRepeatedly(Return(&info_));
Darin Petkove0a312e2011-07-20 13:45:28 -0700127}
128
129void ModemTest::TearDown() {
Jason Glasgowe9089492012-02-23 17:57:37 -0500130 modem_.reset();
Darin Petkove0a312e2011-07-20 13:45:28 -0700131}
132
David Rochbergfa1d31d2012-03-20 10:38:07 -0400133TEST_F(ModemTest, PendingDevicePropertiesAndCreate) {
134 static const char kSentinel[] = "sentinel";
135 static const uint32 kSentinelValue = 17;
Darin Petkove0a312e2011-07-20 13:45:28 -0700136
David Rochbergfa1d31d2012-03-20 10:38:07 -0400137 DBusPropertiesMap properties;
138 properties[kSentinel].writer().append_uint32(kSentinelValue);
139
140 EXPECT_CALL(*modem_, GetLinkName(_, _)).WillRepeatedly(DoAll(
141 SetArgumentPointee<1>(string(kLinkName)),
142 Return(true)));
143 EXPECT_CALL(rtnl_handler_, GetInterfaceIndex(StrEq(kLinkName))).
144 WillRepeatedly(Return(kTestInterfaceIndex));
145
146 // The first time we call CreateDeviceFromModemProperties,
147 // GetMACAddress will fail.
148 EXPECT_CALL(info_, GetMACAddress(kTestInterfaceIndex, _)).
149 WillOnce(Return(false));
Jason Glasgow4c0724a2012-04-17 15:47:40 -0400150 EXPECT_CALL(*modem_, GetModemInterface()).
151 WillRepeatedly(Return(MM_MODEM_INTERFACE));
David Rochbergfa1d31d2012-03-20 10:38:07 -0400152 modem_->CreateDeviceFromModemProperties(properties);
Jason Glasgowe9089492012-02-23 17:57:37 -0500153 EXPECT_FALSE(modem_->device_.get());
Darin Petkove0a312e2011-07-20 13:45:28 -0700154
David Rochbergfa1d31d2012-03-20 10:38:07 -0400155 // On the second time, we allow GetMACAddress to succeed. Now we
156 // expect a device to be built
157 EXPECT_CALL(info_, GetMACAddress(kTestInterfaceIndex, _)).
158 WillOnce(DoAll(SetArgumentPointee<1>(expected_address_),
159 Return(true)));
160
161 // modem will take ownership
162 MockCellular* cellular = new MockCellular(
163 &control_interface_,
164 &dispatcher_,
165 &metrics_,
166 &manager_,
167 kLinkName,
168 kAddressAsString,
169 kTestInterfaceIndex,
170 Cellular::kTypeGSM,
171 kOwner,
172 kPath,
173 static_cast<mobile_provider_db *>(NULL));
174
175 EXPECT_CALL(*modem_,
176 ConstructCellular(StrEq(kLinkName),
177 StrEq(kAddressAsString),
178 kTestInterfaceIndex)).
179 WillOnce(Return(cellular));
180
Jason Glasgow4c0724a2012-04-17 15:47:40 -0400181 EXPECT_CALL(*cellular, OnDBusPropertiesChanged(
182 _,
183 HasDBusPropertyWithValueU32(kSentinel, kSentinelValue),
184 _));
David Rochbergfa1d31d2012-03-20 10:38:07 -0400185 EXPECT_CALL(info_, RegisterDevice(_));
186 EXPECT_CALL(info_, DeregisterDevice(_));
187 modem_->OnDeviceInfoAvailable(kLinkName);
188
189 EXPECT_TRUE(modem_->device_.get());
190}
191
192TEST_F(ModemTest, EarlyDeviceProperties) {
193 // OnDeviceInfoAvailable called before
194 // CreateDeviceFromModemProperties: Do nothing
195 modem_->OnDeviceInfoAvailable(kLinkName);
196 EXPECT_FALSE(modem_->device_.get());
197}
198
199TEST_F(ModemTest, CreateDeviceEarlyFailures) {
200 DBusPropertiesMap properties;
201
202 EXPECT_CALL(*modem_, ConstructCellular(_, _, _)).Times(0);
203
204 // No link name: no device created
205 EXPECT_CALL(*modem_, GetLinkName(_, _)).WillOnce(Return(false));
206 modem_->CreateDeviceFromModemProperties(properties);
Jason Glasgowe9089492012-02-23 17:57:37 -0500207 EXPECT_FALSE(modem_->device_.get());
Darin Petkove0a312e2011-07-20 13:45:28 -0700208
David Rochbergfa1d31d2012-03-20 10:38:07 -0400209 // Link name, but no ifindex: no device created
210 EXPECT_CALL(*modem_, GetLinkName(_, _)).WillOnce(DoAll(
211 SetArgumentPointee<1>(string(kLinkName)),
212 Return(true)));
213 EXPECT_CALL(rtnl_handler_, GetInterfaceIndex(StrEq(kLinkName))).WillOnce(
214 Return(-1));
215 modem_->CreateDeviceFromModemProperties(properties);
Jason Glasgowe9089492012-02-23 17:57:37 -0500216 EXPECT_FALSE(modem_->device_.get());
David Rochbergfa1d31d2012-03-20 10:38:07 -0400217}
Darin Petkove0a312e2011-07-20 13:45:28 -0700218
David Rochbergfa1d31d2012-03-20 10:38:07 -0400219TEST_F(ModemTest, RejectPPPModem) {
220 // TODO(rochberg): Port this to ModemClassic
Darin Petkove0a312e2011-07-20 13:45:28 -0700221}
222
223} // namespace shill