blob: b50370a741b2985cb929a5f80aa394f8d9775b46 [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(
77 DBusPropertiesProxyDelegate */*delegate*/,
78 const string &/*path*/,
79 const string &/*service*/) {
80 return test_->proxy_.release();
81 }
82
83 private:
84 Modem1Test *test_;
85 };
86
87 MockGLib glib_;
88 MockControl control_interface_;
89 EventDispatcher dispatcher_;
90 MockMetrics metrics_;
91 MockManager manager_;
92 MockDeviceInfo info_;
93 scoped_ptr<MockDBusPropertiesProxy> proxy_;
94 TestProxyFactory proxy_factory_;
95 scoped_ptr<Modem1> modem_;
96 MockRTNLHandler rtnl_handler_;
97 ByteString expected_address_;
98 ScopedTempDir temp_dir_;
99 string device_;
100};
101
102void Modem1Test::SetUp() {
103 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
104 modem_->netfiles_path_ = temp_dir_.path();
105 device_ = temp_dir_.path().Append("devices").Append(kLinkName).value();
106 FilePath device_dir = FilePath(device_).Append("1-2/3-4");
107 ASSERT_TRUE(file_util::CreateDirectory(device_dir));
108 FilePath symlink(temp_dir_.path().Append(kLinkName));
109 ASSERT_TRUE(file_util::CreateSymbolicLink(device_dir, symlink));
110
111 EXPECT_EQ(kOwner, modem_->owner_);
112 EXPECT_EQ(kPath, modem_->path_);
113 ReplaceSingletons();
114 expected_address_ = ByteString(kAddress, arraysize(kAddress));
115
116 EXPECT_CALL(rtnl_handler_, GetInterfaceIndex(kLinkName)).
117 WillRepeatedly(Return(kTestInterfaceIndex));
118
119 EXPECT_CALL(manager_, device_info()).WillRepeatedly(Return(&info_));
120 EXPECT_CALL(info_, GetMACAddress(kTestInterfaceIndex, _)).
121 WillOnce(DoAll(SetArgumentPointee<1>(expected_address_),
122 Return(true)));
123}
124
125void Modem1Test::TearDown() {
126 modem_.reset();
127}
128
129TEST_F(Modem1Test, CreateDeviceMM1) {
130 DBusInterfaceToProperties i_to_p;
131 DBusPropertiesMap modem_properties;
132 DBus::Variant lock;
133 lock.writer().append_uint32(MM_MODEM_LOCK_NONE);
134 modem_properties[MM_MODEM_PROPERTY_UNLOCKREQUIRED] = lock;
135 DBus::Variant device_variant;
136 device_variant.writer().append_string(device_.c_str());
137 modem_properties[MM_MODEM_PROPERTY_DEVICE] = device_variant;
138 i_to_p[MM_DBUS_INTERFACE_MODEM] = modem_properties;
139
140 modem_->CreateDeviceMM1(i_to_p);
141 EXPECT_TRUE(modem_->device().get());
142}
143
144} // namespace shill