blob: 99263f3565404a0e3a4dd7e50cb3bc9c0cd69230 [file] [log] [blame]
Darin Petkove0a312e2011-07-20 13:45:28 -07001// Copyright (c) 2011 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
Darin Petkov6f9eaa32011-08-09 15:26:44 -07005#include <vector>
6
Darin Petkove0a312e2011-07-20 13:45:28 -07007#include <gtest/gtest.h>
8#include <mm/mm-modem.h>
9#include <net/if.h>
10#include <sys/ioctl.h>
11
12#include "shill/cellular.h"
13#include "shill/manager.h"
14#include "shill/mock_control.h"
15#include "shill/mock_dbus_properties_proxy.h"
16#include "shill/mock_glib.h"
17#include "shill/mock_sockets.h"
18#include "shill/modem.h"
19#include "shill/proxy_factory.h"
20#include "shill/rtnl_handler.h"
21#include "shill/shill_event.h"
22
23using std::string;
Darin Petkov6f9eaa32011-08-09 15:26:44 -070024using std::vector;
Darin Petkove0a312e2011-07-20 13:45:28 -070025using testing::_;
26using testing::DoAll;
27using testing::Return;
28using testing::StrictMock;
29using testing::Test;
30
31namespace shill {
32
33namespace {
34
35const int kTestInterfaceIndex = 5;
36
37ACTION(SetInterfaceIndex) {
38 if (arg2) {
39 reinterpret_cast<struct ifreq *>(arg2)->ifr_ifindex = kTestInterfaceIndex;
40 }
41}
42
43} // namespace
44
45class ModemTest : public Test {
46 public:
47 ModemTest()
48 : manager_(&control_interface_, &dispatcher_, &glib_),
Darin Petkov3b292332011-07-28 14:17:24 -070049 proxy_(new MockDBusPropertiesProxy()),
50 proxy_factory_(this),
Darin Petkove0a312e2011-07-20 13:45:28 -070051 modem_(kOwner, kPath, &control_interface_, &dispatcher_, &manager_) {}
52
53 virtual void SetUp();
54 virtual void TearDown();
55
56 void SetSockets(Sockets *sockets) {
57 RTNLHandler::GetInstance()->sockets_ = sockets;
58 }
59
60 protected:
61 class TestProxyFactory : public ProxyFactory {
62 public:
Darin Petkov3b292332011-07-28 14:17:24 -070063 TestProxyFactory(ModemTest *test) : test_(test) {}
Darin Petkove0a312e2011-07-20 13:45:28 -070064
65 virtual DBusPropertiesProxyInterface *CreateDBusPropertiesProxy(
Darin Petkovc5f56562011-08-06 16:40:05 -070066 DBusPropertiesProxyListener *listener,
Darin Petkove0a312e2011-07-20 13:45:28 -070067 const string &path,
68 const string &service) {
Darin Petkov3b292332011-07-28 14:17:24 -070069 return test_->proxy_.release();
Darin Petkove0a312e2011-07-20 13:45:28 -070070 }
71
72 private:
Darin Petkov3b292332011-07-28 14:17:24 -070073 ModemTest *test_;
Darin Petkove0a312e2011-07-20 13:45:28 -070074 };
75
76 static const char kOwner[];
77 static const char kPath[];
78
Darin Petkove0a312e2011-07-20 13:45:28 -070079 MockGLib glib_;
80 MockControl control_interface_;
81 EventDispatcher dispatcher_;
82 Manager manager_;
Darin Petkov3b292332011-07-28 14:17:24 -070083 scoped_ptr<MockDBusPropertiesProxy> proxy_;
Darin Petkove0a312e2011-07-20 13:45:28 -070084 TestProxyFactory proxy_factory_;
85 Modem modem_;
86 StrictMock<MockSockets> sockets_;
87};
88
89const char ModemTest::kOwner[] = ":1.18";
90const char ModemTest::kPath[] = "/org/chromium/ModemManager/Gobi/0";
91
92void ModemTest::SetUp() {
93 EXPECT_EQ(kOwner, modem_.owner_);
94 EXPECT_EQ(kPath, modem_.path_);
95 ProxyFactory::set_factory(&proxy_factory_);
96 SetSockets(&sockets_);
97}
98
99void ModemTest::TearDown() {
Darin Petkove0a312e2011-07-20 13:45:28 -0700100 ProxyFactory::set_factory(NULL);
101 SetSockets(NULL);
102}
103
Darin Petkove0a312e2011-07-20 13:45:28 -0700104TEST_F(ModemTest, Init) {
Darin Petkove0a312e2011-07-20 13:45:28 -0700105 DBusPropertiesMap props;
106 props[Modem::kPropertyIPMethod].writer().append_uint32(
107 MM_MODEM_IP_METHOD_DHCP);
108 props[Modem::kPropertyLinkName].writer().append_string("usb1");
Darin Petkov3b292332011-07-28 14:17:24 -0700109 EXPECT_CALL(*proxy_, GetAll(MM_MODEM_INTERFACE)).WillOnce(Return(props));
Darin Petkov67d8ecf2011-07-26 16:03:30 -0700110 EXPECT_TRUE(modem_.task_factory_.empty());
Darin Petkove0a312e2011-07-20 13:45:28 -0700111 modem_.Init();
Darin Petkov67d8ecf2011-07-26 16:03:30 -0700112 EXPECT_FALSE(modem_.task_factory_.empty());
113
114 EXPECT_CALL(sockets_, Socket(PF_INET, SOCK_DGRAM, 0)).WillOnce(Return(-1));
115 dispatcher_.DispatchPendingEvents();
Darin Petkove0a312e2011-07-20 13:45:28 -0700116}
117
118TEST_F(ModemTest, CreateCellularDevice) {
119 DBusPropertiesMap props;
120
121 modem_.CreateCellularDevice(props);
122 EXPECT_FALSE(modem_.device_.get());
123
124 props[Modem::kPropertyIPMethod].writer().append_uint32(
125 MM_MODEM_IP_METHOD_PPP);
126 modem_.CreateCellularDevice(props);
127 EXPECT_FALSE(modem_.device_.get());
128
129 props.erase(Modem::kPropertyIPMethod);
130 props[Modem::kPropertyIPMethod].writer().append_uint32(
131 MM_MODEM_IP_METHOD_DHCP);
132 modem_.CreateCellularDevice(props);
133 EXPECT_FALSE(modem_.device_.get());
134
135 static const char kLinkName[] = "usb0";
136 const int kTestSocket = 10;
137 props[Modem::kPropertyLinkName].writer().append_string(kLinkName);
138 EXPECT_CALL(sockets_, Socket(PF_INET, SOCK_DGRAM, 0))
Darin Petkove9d12e02011-07-27 15:09:37 -0700139 .Times(2)
140 .WillRepeatedly(Return(kTestSocket));
Darin Petkove0a312e2011-07-20 13:45:28 -0700141 EXPECT_CALL(sockets_, Ioctl(kTestSocket, SIOCGIFINDEX, _))
Darin Petkove9d12e02011-07-27 15:09:37 -0700142 .WillRepeatedly(DoAll(SetInterfaceIndex(), Return(0)));
Darin Petkove0a312e2011-07-20 13:45:28 -0700143 EXPECT_CALL(sockets_, Close(kTestSocket))
Darin Petkove9d12e02011-07-27 15:09:37 -0700144 .WillRepeatedly(Return(0));
Darin Petkove0a312e2011-07-20 13:45:28 -0700145 modem_.CreateCellularDevice(props);
Darin Petkove9d12e02011-07-27 15:09:37 -0700146 EXPECT_FALSE(modem_.device_.get());
147
148 props[Modem::kPropertyType].writer().append_uint32(MM_MODEM_TYPE_CDMA);
149 modem_.CreateCellularDevice(props);
150 ASSERT_TRUE(modem_.device_.get());
Darin Petkove0a312e2011-07-20 13:45:28 -0700151 EXPECT_EQ(kLinkName, modem_.device_->link_name());
152 EXPECT_EQ(kTestInterfaceIndex, modem_.device_->interface_index());
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700153
154 vector<DeviceRefPtr> devices;
155 manager_.FilterByTechnology(Device::kCellular, &devices);
156 EXPECT_EQ(1, devices.size());
157 EXPECT_TRUE(devices[0].get() == modem_.device_.get());
Darin Petkove0a312e2011-07-20 13:45:28 -0700158}
159
160} // namespace shill