blob: 1d8c32f948ae0b00a182dafb7493049a0a105252 [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>
Ben Chan5c853ef2012-10-05 00:05:37 -07008#include <ModemManager/ModemManager.h>
Jason Glasgow82f9ab32012-04-04 14:27:19 -04009
Ben Chan876efd32012-09-28 15:25:13 -070010#include "shill/cellular_capability.h"
Jason Glasgow82f9ab32012-04-04 14:27:19 -040011#include "shill/dbus_property_matchers.h"
12#include "shill/event_dispatcher.h"
13#include "shill/manager.h"
14#include "shill/mock_cellular.h"
Jason Glasgow82f9ab32012-04-04 14:27:19 -040015#include "shill/mock_dbus_properties_proxy.h"
16#include "shill/mock_device_info.h"
Prathmesh Prabhu27526f12013-03-25 19:42:18 -070017#include "shill/mock_modem_info.h"
Ben Chan1e2ba232014-01-27 16:35:45 -080018#include "shill/mock_proxy_factory.h"
Jason Glasgow82f9ab32012-04-04 14:27:19 -040019#include "shill/mock_rtnl_handler.h"
Jason Glasgow82f9ab32012-04-04 14:27:19 -040020#include "shill/rtnl_handler.h"
Ben Chan1e2ba232014-01-27 16:35:45 -080021#include "shill/testing.h"
Jason Glasgow82f9ab32012-04-04 14:27:19 -040022
Albert Chaulk0e1cdea2013-02-27 15:32:55 -080023using base::FilePath;
Jason Glasgow82f9ab32012-04-04 14:27:19 -040024using std::string;
Prathmesh Prabhu5c1f29c2013-09-10 18:14:09 -070025using std::vector;
Jason Glasgow82f9ab32012-04-04 14:27:19 -040026using 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};
Jason Glasgow82f9ab32012-04-04 14:27:19 -040042
43} // namespace
44
45class Modem1Test : public Test {
46 public:
47 Modem1Test()
Ben Chanea18c6c2014-09-30 13:08:26 -070048 : modem_info_(nullptr, &dispatcher_, nullptr, nullptr, nullptr),
Prathmesh Prabhu27526f12013-03-25 19:42:18 -070049 device_info_(modem_info_.control_interface(), modem_info_.dispatcher(),
50 modem_info_.metrics(), modem_info_.manager()),
Jason Glasgow82f9ab32012-04-04 14:27:19 -040051 proxy_(new MockDBusPropertiesProxy()),
Jason Glasgow82f9ab32012-04-04 14:27:19 -040052 modem_(
53 new Modem1(
54 kOwner,
Jason Glasgowa585fc32012-06-06 11:04:09 -040055 kService,
Jason Glasgow82f9ab32012-04-04 14:27:19 -040056 kPath,
Prathmesh Prabhu27526f12013-03-25 19:42:18 -070057 &modem_info_)) {}
Jason Glasgow82f9ab32012-04-04 14:27:19 -040058 virtual void SetUp();
59 virtual void TearDown();
60
61 void ReplaceSingletons() {
62 modem_->rtnl_handler_ = &rtnl_handler_;
63 modem_->proxy_factory_ = &proxy_factory_;
64 }
65
66 protected:
Jason Glasgow82f9ab32012-04-04 14:27:19 -040067 EventDispatcher dispatcher_;
Prathmesh Prabhu27526f12013-03-25 19:42:18 -070068 MockModemInfo modem_info_;
69 MockDeviceInfo device_info_;
Ben Chanc20ed132014-10-16 12:25:03 -070070 std::unique_ptr<MockDBusPropertiesProxy> proxy_;
Ben Chan1e2ba232014-01-27 16:35:45 -080071 MockProxyFactory proxy_factory_;
Ben Chanc20ed132014-10-16 12:25:03 -070072 std::unique_ptr<Modem1> modem_;
Jason Glasgow82f9ab32012-04-04 14:27:19 -040073 MockRTNLHandler rtnl_handler_;
74 ByteString expected_address_;
Jason Glasgow82f9ab32012-04-04 14:27:19 -040075};
76
77void Modem1Test::SetUp() {
Jason Glasgow82f9ab32012-04-04 14:27:19 -040078 EXPECT_EQ(kOwner, modem_->owner_);
Jason Glasgowa585fc32012-06-06 11:04:09 -040079 EXPECT_EQ(kService, modem_->service_);
Jason Glasgow82f9ab32012-04-04 14:27:19 -040080 EXPECT_EQ(kPath, modem_->path_);
81 ReplaceSingletons();
82 expected_address_ = ByteString(kAddress, arraysize(kAddress));
83
84 EXPECT_CALL(rtnl_handler_, GetInterfaceIndex(kLinkName)).
85 WillRepeatedly(Return(kTestInterfaceIndex));
86
Prathmesh Prabhu27526f12013-03-25 19:42:18 -070087 EXPECT_CALL(*modem_info_.mock_manager(), device_info())
88 .WillRepeatedly(Return(&device_info_));
89 EXPECT_CALL(device_info_, GetMACAddress(kTestInterfaceIndex, _)).
Jason Glasgow82f9ab32012-04-04 14:27:19 -040090 WillOnce(DoAll(SetArgumentPointee<1>(expected_address_),
91 Return(true)));
92}
93
94void Modem1Test::TearDown() {
95 modem_.reset();
96}
97
98TEST_F(Modem1Test, CreateDeviceMM1) {
Ben Chan876efd32012-09-28 15:25:13 -070099 DBusInterfaceToProperties properties;
Jason Glasgow82f9ab32012-04-04 14:27:19 -0400100 DBusPropertiesMap modem_properties;
101 DBus::Variant lock;
102 lock.writer().append_uint32(MM_MODEM_LOCK_NONE);
103 modem_properties[MM_MODEM_PROPERTY_UNLOCKREQUIRED] = lock;
Prathmesh Prabhu5c1f29c2013-09-10 18:14:09 -0700104
105 DBus::Variant ports_variant;
106 DBus::MessageIter ports_message_iter = ports_variant.writer();
107 DBus::MessageIter ports_array_iter = ports_message_iter.new_array("(su)");
108 DBus::MessageIter port_struct_iter = ports_array_iter.new_struct();
109 port_struct_iter.append_string(kLinkName);
110 port_struct_iter.append_uint32(MM_MODEM_PORT_TYPE_NET);
111 ports_array_iter.close_container(port_struct_iter);
112 ports_message_iter.close_container(ports_array_iter);
113 modem_properties[MM_MODEM_PROPERTY_PORTS] = ports_variant;
114
Ben Chan876efd32012-09-28 15:25:13 -0700115 properties[MM_DBUS_INTERFACE_MODEM] = modem_properties;
Jason Glasgow82f9ab32012-04-04 14:27:19 -0400116
Ben Chan876efd32012-09-28 15:25:13 -0700117 DBusPropertiesMap modem3gpp_properties;
118 DBus::Variant registration_state_variant;
119 registration_state_variant.writer().append_uint32(
120 MM_MODEM_3GPP_REGISTRATION_STATE_HOME);
121 modem3gpp_properties[MM_MODEM_MODEM3GPP_PROPERTY_REGISTRATIONSTATE] =
122 registration_state_variant;
123 properties[MM_DBUS_INTERFACE_MODEM_MODEM3GPP] = modem3gpp_properties;
124
Ben Chan1e2ba232014-01-27 16:35:45 -0800125 EXPECT_CALL(proxy_factory_, CreateDBusPropertiesProxy(kPath, kOwner))
126 .WillOnce(ReturnAndReleasePointee(&proxy_));
Ben Chan876efd32012-09-28 15:25:13 -0700127 modem_->CreateDeviceMM1(properties);
Jason Glasgow82f9ab32012-04-04 14:27:19 -0400128 EXPECT_TRUE(modem_->device().get());
Ben Chan876efd32012-09-28 15:25:13 -0700129 EXPECT_TRUE(modem_->device()->capability_->IsRegistered());
Jason Glasgow82f9ab32012-04-04 14:27:19 -0400130}
131
132} // namespace shill