blob: f4a02c2f834dc601c2ab562f1d6062d5c81e83ed [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 Chanf6120e92012-06-28 18:56:17 -070010#include <ModemManager/ModemManager-enums.h>
11#include <ModemManager/ModemManager-names.h>
Jason Glasgow82f9ab32012-04-04 14:27:19 -040012
13#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_,
64 static_cast<mobile_provider_db *>(NULL))) {}
65 virtual void SetUp();
66 virtual void TearDown();
67
68 void ReplaceSingletons() {
69 modem_->rtnl_handler_ = &rtnl_handler_;
70 modem_->proxy_factory_ = &proxy_factory_;
71 }
72
73 protected:
74 class TestProxyFactory : public ProxyFactory {
75 public:
76 explicit TestProxyFactory(Modem1Test *test) : test_(test) {}
77
78 virtual DBusPropertiesProxyInterface *CreateDBusPropertiesProxy(
Jason Glasgow82f9ab32012-04-04 14:27:19 -040079 const string &/*path*/,
80 const string &/*service*/) {
81 return test_->proxy_.release();
82 }
83
84 private:
85 Modem1Test *test_;
86 };
87
88 MockGLib glib_;
89 MockControl control_interface_;
90 EventDispatcher dispatcher_;
91 MockMetrics metrics_;
92 MockManager manager_;
93 MockDeviceInfo info_;
94 scoped_ptr<MockDBusPropertiesProxy> proxy_;
95 TestProxyFactory proxy_factory_;
96 scoped_ptr<Modem1> modem_;
97 MockRTNLHandler rtnl_handler_;
98 ByteString expected_address_;
99 ScopedTempDir temp_dir_;
100 string device_;
101};
102
103void Modem1Test::SetUp() {
104 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
105 modem_->netfiles_path_ = temp_dir_.path();
106 device_ = temp_dir_.path().Append("devices").Append(kLinkName).value();
107 FilePath device_dir = FilePath(device_).Append("1-2/3-4");
108 ASSERT_TRUE(file_util::CreateDirectory(device_dir));
109 FilePath symlink(temp_dir_.path().Append(kLinkName));
110 ASSERT_TRUE(file_util::CreateSymbolicLink(device_dir, symlink));
111
112 EXPECT_EQ(kOwner, modem_->owner_);
Jason Glasgowa585fc32012-06-06 11:04:09 -0400113 EXPECT_EQ(kService, modem_->service_);
Jason Glasgow82f9ab32012-04-04 14:27:19 -0400114 EXPECT_EQ(kPath, modem_->path_);
115 ReplaceSingletons();
116 expected_address_ = ByteString(kAddress, arraysize(kAddress));
117
118 EXPECT_CALL(rtnl_handler_, GetInterfaceIndex(kLinkName)).
119 WillRepeatedly(Return(kTestInterfaceIndex));
120
121 EXPECT_CALL(manager_, device_info()).WillRepeatedly(Return(&info_));
122 EXPECT_CALL(info_, GetMACAddress(kTestInterfaceIndex, _)).
123 WillOnce(DoAll(SetArgumentPointee<1>(expected_address_),
124 Return(true)));
125}
126
127void Modem1Test::TearDown() {
128 modem_.reset();
129}
130
131TEST_F(Modem1Test, CreateDeviceMM1) {
132 DBusInterfaceToProperties i_to_p;
133 DBusPropertiesMap modem_properties;
134 DBus::Variant lock;
135 lock.writer().append_uint32(MM_MODEM_LOCK_NONE);
136 modem_properties[MM_MODEM_PROPERTY_UNLOCKREQUIRED] = lock;
137 DBus::Variant device_variant;
138 device_variant.writer().append_string(device_.c_str());
139 modem_properties[MM_MODEM_PROPERTY_DEVICE] = device_variant;
140 i_to_p[MM_DBUS_INTERFACE_MODEM] = modem_properties;
141
142 modem_->CreateDeviceMM1(i_to_p);
143 EXPECT_TRUE(modem_->device().get());
144}
145
146} // namespace shill