blob: f6a429831bf46e6251090a777a823f9d18fa9c9f [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>
10#include <mm/ModemManager-enums.h>
11#include <mm/ModemManager-names.h>
12
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";
41const char kPath[] = "/org/chromium/ModemManager/Gobi/0";
42const unsigned char kAddress[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05};
43const char kAddressAsString[] = "000102030405";
44
45} // namespace
46
47class Modem1Test : public Test {
48 public:
49 Modem1Test()
50 : manager_(&control_interface_, &dispatcher_, &metrics_, &glib_),
51 info_(&control_interface_, &dispatcher_, &metrics_, &manager_),
52 proxy_(new MockDBusPropertiesProxy()),
53 proxy_factory_(this),
54 modem_(
55 new Modem1(
56 kOwner,
57 kPath,
58 &control_interface_,
59 &dispatcher_,
60 &metrics_,
61 &manager_,
62 static_cast<mobile_provider_db *>(NULL))) {}
63 virtual void SetUp();
64 virtual void TearDown();
65
66 void ReplaceSingletons() {
67 modem_->rtnl_handler_ = &rtnl_handler_;
68 modem_->proxy_factory_ = &proxy_factory_;
69 }
70
71 protected:
72 class TestProxyFactory : public ProxyFactory {
73 public:
74 explicit TestProxyFactory(Modem1Test *test) : test_(test) {}
75
76 virtual DBusPropertiesProxyInterface *CreateDBusPropertiesProxy(
Jason Glasgow82f9ab32012-04-04 14:27:19 -040077 const string &/*path*/,
78 const string &/*service*/) {
79 return test_->proxy_.release();
80 }
81
82 private:
83 Modem1Test *test_;
84 };
85
86 MockGLib glib_;
87 MockControl control_interface_;
88 EventDispatcher dispatcher_;
89 MockMetrics metrics_;
90 MockManager manager_;
91 MockDeviceInfo info_;
92 scoped_ptr<MockDBusPropertiesProxy> proxy_;
93 TestProxyFactory proxy_factory_;
94 scoped_ptr<Modem1> modem_;
95 MockRTNLHandler rtnl_handler_;
96 ByteString expected_address_;
97 ScopedTempDir temp_dir_;
98 string device_;
99};
100
101void Modem1Test::SetUp() {
102 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
103 modem_->netfiles_path_ = temp_dir_.path();
104 device_ = temp_dir_.path().Append("devices").Append(kLinkName).value();
105 FilePath device_dir = FilePath(device_).Append("1-2/3-4");
106 ASSERT_TRUE(file_util::CreateDirectory(device_dir));
107 FilePath symlink(temp_dir_.path().Append(kLinkName));
108 ASSERT_TRUE(file_util::CreateSymbolicLink(device_dir, symlink));
109
110 EXPECT_EQ(kOwner, modem_->owner_);
111 EXPECT_EQ(kPath, modem_->path_);
112 ReplaceSingletons();
113 expected_address_ = ByteString(kAddress, arraysize(kAddress));
114
115 EXPECT_CALL(rtnl_handler_, GetInterfaceIndex(kLinkName)).
116 WillRepeatedly(Return(kTestInterfaceIndex));
117
118 EXPECT_CALL(manager_, device_info()).WillRepeatedly(Return(&info_));
119 EXPECT_CALL(info_, GetMACAddress(kTestInterfaceIndex, _)).
120 WillOnce(DoAll(SetArgumentPointee<1>(expected_address_),
121 Return(true)));
122}
123
124void Modem1Test::TearDown() {
125 modem_.reset();
126}
127
128TEST_F(Modem1Test, CreateDeviceMM1) {
129 DBusInterfaceToProperties i_to_p;
130 DBusPropertiesMap modem_properties;
131 DBus::Variant lock;
132 lock.writer().append_uint32(MM_MODEM_LOCK_NONE);
133 modem_properties[MM_MODEM_PROPERTY_UNLOCKREQUIRED] = lock;
134 DBus::Variant device_variant;
135 device_variant.writer().append_string(device_.c_str());
136 modem_properties[MM_MODEM_PROPERTY_DEVICE] = device_variant;
137 i_to_p[MM_DBUS_INTERFACE_MODEM] = modem_properties;
138
139 modem_->CreateDeviceMM1(i_to_p);
140 EXPECT_TRUE(modem_->device().get());
141}
142
143} // namespace shill