blob: 78d507f20b90332610746f2cc1182f591ae7e7c2 [file] [log] [blame]
Joel Scherpelzde937962017-06-01 13:20:21 +09001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Chenbo Feng7e974052018-02-28 22:57:21 -080017#include <ifaddrs.h>
18#include <net/if.h>
19#include <sys/types.h>
20
Joel Scherpelzde937962017-06-01 13:20:21 +090021#include <gmock/gmock.h>
22#include <gtest/gtest.h>
23
24#include <netdutils/MockSyscalls.h>
25
26#include "InterfaceController.h"
27
28using testing::ByMove;
29using testing::Invoke;
30using testing::Return;
31using testing::StrictMock;
32using testing::_;
33
34namespace android {
35namespace net {
36namespace {
37
38using netdutils::Fd;
39using netdutils::ScopedMockSyscalls;
40using netdutils::Slice;
41using netdutils::Status;
42using netdutils::StatusOr;
43using netdutils::UniqueFd;
44using netdutils::makeSlice;
45using netdutils::status::ok;
46using netdutils::statusFromErrno;
47
48constexpr Fd kDevRandomFd(777);
49constexpr Fd kStableSecretFd(9999);
50const char kDevRandomPath[] = "/dev/random";
51const char kTestIface[] = "wlan5";
52const char kStableSecretProperty[] = "persist.netd.stable_secret";
53const char kStableSecretPath[] = "/proc/sys/net/ipv6/conf/wlan5/stable_secret";
54const char kTestIPv6Address[] = "\x20\x01\x0d\xb8\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x10";
55const char kTestIPv6AddressString[] = "2001:db8:506:708:90a:b0c:d0e:f10";
56
57// getProperty() and setProperty() are forwarded to this mock
58class MockProperties {
59 public:
60 MOCK_CONST_METHOD2(get, std::string(const std::string& key, const std::string& dflt));
61 MOCK_CONST_METHOD2(set, Status(const std::string& key, const std::string& val));
62};
63
64} // namespace
65
66class StablePrivacyTest : public testing::Test {
67 protected:
68 void expectOpenFile(const std::string& path, const Fd fd, int err) {
69 if (err == 0) {
70 EXPECT_CALL(mSyscalls, open(path, _, _)).WillOnce(Return(ByMove(UniqueFd(fd))));
71 EXPECT_CALL(mSyscalls, close(fd)).WillOnce(Return(ok));
72 } else {
73 EXPECT_CALL(mSyscalls, open(path, _, _))
74 .WillOnce(Return(ByMove(statusFromErrno(err, "open() failed"))));
75 }
76 }
77
78 void expectReadFromDevRandom(const std::string& data) {
79 expectOpenFile(kDevRandomPath, kDevRandomFd, 0);
80 EXPECT_CALL(mSyscalls, read(kDevRandomFd, _)).WillOnce(Invoke([data](Fd, const Slice buf) {
81 EXPECT_EQ(data.size(), buf.size());
82 return take(buf, copy(buf, makeSlice(data)));
83 }));
84 }
85
86 void expectGetPropertyDefault(const std::string& key) {
87 EXPECT_CALL(mProperties, get(key, _))
88 .WillOnce(Invoke([](const std::string&, const std::string& dflt) { return dflt; }));
89 }
90
91 void expectGetProperty(const std::string& key, const std::string& val) {
92 EXPECT_CALL(mProperties, get(key, _))
93 .WillOnce(Invoke([val](const std::string&, const std::string&) { return val; }));
94 }
95
96 void expectSetProperty(const std::string& key, const std::string& val, Status status) {
Bernie Innocenti7e25ec02018-07-02 19:32:17 +090097 EXPECT_CALL(mProperties, set(key, val)).WillOnce(Return(std::move(status)));
Joel Scherpelzde937962017-06-01 13:20:21 +090098 }
99
100 void expectWriteToFile(const Fd fd, const std::string& val, int err) {
101 EXPECT_CALL(mSyscalls, write(fd, _))
102 .WillOnce(Invoke([val, err](Fd, const Slice buf) -> StatusOr<size_t> {
103 EXPECT_EQ(val, toString(buf));
104 if (err) {
105 return statusFromErrno(err, "write() failed");
106 }
107 return val.size();
108 }));
109 }
110
111 Status enableStablePrivacyAddresses(const std::string& iface) {
112 return InterfaceController::enableStablePrivacyAddresses(iface, mGet, mSet);
113 }
114
115 StrictMock<ScopedMockSyscalls> mSyscalls;
116 StrictMock<MockProperties> mProperties;
117
118 const std::function<std::string(const std::string&, const std::string&)> mGet =
119 [this](const std::string& key, const std::string& dflt) {
120 return mProperties.get(key, dflt);
121 };
122 const std::function<Status(const std::string&, const std::string&)> mSet =
123 [this](const std::string& key, const std::string& val) {
124 return mProperties.set(key, val);
125 };
126};
127
128TEST_F(StablePrivacyTest, PropertyOpenEnoent) {
129 expectOpenFile(kStableSecretPath, kStableSecretFd, ENOENT);
130 EXPECT_NE(ok, enableStablePrivacyAddresses(kTestIface));
131}
132
133TEST_F(StablePrivacyTest, PropertyOpenEaccess) {
134 expectOpenFile(kStableSecretPath, kStableSecretFd, EACCES);
135 EXPECT_NE(ok, enableStablePrivacyAddresses(kTestIface));
136}
137
138TEST_F(StablePrivacyTest, FirstBootWriteOkSetPropertyOk) {
139 expectOpenFile(kStableSecretPath, kStableSecretFd, 0);
140 expectGetPropertyDefault(kStableSecretProperty);
141 expectReadFromDevRandom(kTestIPv6Address);
142 expectWriteToFile(kStableSecretFd, kTestIPv6AddressString, 0);
143 expectSetProperty(kStableSecretProperty, kTestIPv6AddressString, ok);
144 EXPECT_EQ(ok, enableStablePrivacyAddresses(kTestIface));
145}
146
147TEST_F(StablePrivacyTest, FirstBootWriteOkSetPropertyFail) {
148 const auto kError = statusFromErrno(EINVAL, "");
149 expectOpenFile(kStableSecretPath, kStableSecretFd, 0);
150 expectGetPropertyDefault(kStableSecretProperty);
151 expectReadFromDevRandom(kTestIPv6Address);
152 expectWriteToFile(kStableSecretFd, kTestIPv6AddressString, 0);
153 expectSetProperty(kStableSecretProperty, kTestIPv6AddressString, kError);
154 EXPECT_EQ(kError, enableStablePrivacyAddresses(kTestIface));
155}
156
157TEST_F(StablePrivacyTest, FirstBootWriteFail) {
158 expectOpenFile(kStableSecretPath, kStableSecretFd, 0);
159 expectGetPropertyDefault(kStableSecretProperty);
160 expectReadFromDevRandom(kTestIPv6Address);
161 expectWriteToFile(kStableSecretFd, kTestIPv6AddressString, ENOSPC);
162 EXPECT_NE(ok, enableStablePrivacyAddresses(kTestIface));
163}
164
165TEST_F(StablePrivacyTest, ExistingPropertyWriteOk) {
166 expectOpenFile(kStableSecretPath, kStableSecretFd, 0);
167 expectGetProperty(kStableSecretProperty, kTestIPv6AddressString);
168 expectWriteToFile(kStableSecretFd, kTestIPv6AddressString, 0);
169 EXPECT_EQ(ok, enableStablePrivacyAddresses(kTestIface));
170}
171
172TEST_F(StablePrivacyTest, ExistingPropertyWriteFail) {
173 expectOpenFile(kStableSecretPath, kStableSecretFd, 0);
174 expectGetProperty(kStableSecretProperty, kTestIPv6AddressString);
175 expectWriteToFile(kStableSecretFd, kTestIPv6AddressString, EACCES);
176 EXPECT_NE(ok, enableStablePrivacyAddresses(kTestIface));
177}
178
Chenbo Feng7e974052018-02-28 22:57:21 -0800179class GetIfaceListTest : public testing::Test {};
180
Nathan Harold172f8e42018-04-19 13:10:19 -0700181TEST_F(GetIfaceListTest, IfaceNames) {
182 StatusOr<std::vector<std::string>> ifaceNames = InterfaceController::getIfaceNames();
183 EXPECT_EQ(ok, ifaceNames.status());
184 struct ifaddrs *ifaddr, *ifa;
185 EXPECT_EQ(0, getifaddrs(&ifaddr));
Yi Kongbdfd57e2018-07-25 13:26:10 -0700186 for (ifa = ifaddr; ifa != nullptr; ifa = ifa->ifa_next) {
Nathan Harold172f8e42018-04-19 13:10:19 -0700187 const auto val = std::find(
188 ifaceNames.value().begin(), ifaceNames.value().end(), ifa->ifa_name);
189 EXPECT_NE(ifaceNames.value().end(), val);
190 }
191 freeifaddrs(ifaddr);
192}
193
Chenbo Feng7e974052018-02-28 22:57:21 -0800194TEST_F(GetIfaceListTest, IfaceExist) {
195 StatusOr<std::map<std::string, uint32_t>> ifaceMap = InterfaceController::getIfaceList();
196 EXPECT_EQ(ok, ifaceMap.status());
197 struct ifaddrs *ifaddr, *ifa;
198 EXPECT_EQ(0, getifaddrs(&ifaddr));
Yi Kongbdfd57e2018-07-25 13:26:10 -0700199 for (ifa = ifaddr; ifa != nullptr; ifa = ifa->ifa_next) {
Chenbo Feng7e974052018-02-28 22:57:21 -0800200 uint32_t ifaceIndex = if_nametoindex(ifa->ifa_name);
201 const auto ifacePair = ifaceMap.value().find(ifa->ifa_name);
202 EXPECT_NE(ifaceMap.value().end(), ifacePair);
203 EXPECT_EQ(ifaceIndex, ifacePair->second);
204 }
205 freeifaddrs(ifaddr);
206}
207
Joel Scherpelzde937962017-06-01 13:20:21 +0900208} // namespace net
209} // namespace android