blob: b88842112d0dea0f838b63364e65e675bb25a7b1 [file] [log] [blame]
Lorenzo Colitti89faa342016-02-26 11:38:47 +09001/*
2 * Copyright 2016 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 * binder_test.cpp - unit tests for netd binder RPCs.
17 */
18
Robin Leeb8087362016-03-30 18:43:08 +010019#include <cerrno>
20#include <cinttypes>
Lorenzo Colitti89faa342016-02-26 11:38:47 +090021#include <cstdint>
Lorenzo Colittidedd2712016-03-22 12:36:29 +090022#include <cstdio>
23#include <cstdlib>
Lorenzo Colitti563d98b2016-04-24 13:13:14 +090024#include <set>
Lorenzo Colitti89faa342016-02-26 11:38:47 +090025#include <vector>
26
Lorenzo Colitti755faa92016-07-27 22:10:49 +090027#include <fcntl.h>
Erik Klinecc4f2732016-08-03 11:24:27 +090028#include <ifaddrs.h>
Lorenzo Colitti755faa92016-07-27 22:10:49 +090029#include <netdb.h>
Lorenzo Colitti563d98b2016-04-24 13:13:14 +090030#include <sys/socket.h>
Lorenzo Colitti755faa92016-07-27 22:10:49 +090031#include <sys/types.h>
Lorenzo Colitti563d98b2016-04-24 13:13:14 +090032#include <netinet/in.h>
Lorenzo Colitti755faa92016-07-27 22:10:49 +090033#include <linux/if.h>
34#include <linux/if_tun.h>
Ben Schwartze7601812017-04-28 16:38:29 -040035#include <openssl/base64.h>
Lorenzo Colitti563d98b2016-04-24 13:13:14 +090036
Erik Klinecc4f2732016-08-03 11:24:27 +090037#include <android-base/macros.h>
Lorenzo Colitti89faa342016-02-26 11:38:47 +090038#include <android-base/stringprintf.h>
Lorenzo Colittidedd2712016-03-22 12:36:29 +090039#include <android-base/strings.h>
Chenbo Feng837ddfc2018-05-08 13:45:08 -070040#include <bpf/BpfUtils.h>
Robin Leeb8087362016-03-30 18:43:08 +010041#include <cutils/multiuser.h>
Lorenzo Colitti89faa342016-02-26 11:38:47 +090042#include <gtest/gtest.h>
43#include <logwrap/logwrap.h>
Lorenzo Colitti755faa92016-07-27 22:10:49 +090044#include <netutils/ifc.h>
Lorenzo Colitti89faa342016-02-26 11:38:47 +090045
Nathan Harold21299f72018-03-16 20:13:03 -070046#include "InterfaceController.h"
Lorenzo Colitti89faa342016-02-26 11:38:47 +090047#include "NetdConstants.h"
Robin Lee7e05cc92016-09-21 16:31:33 +090048#include "Stopwatch.h"
Nathan Harold21299f72018-03-16 20:13:03 -070049#include "XfrmController.h"
Lorenzo Colitti1e299c62017-02-27 17:16:10 +090050#include "tun_interface.h"
Lorenzo Colitti89faa342016-02-26 11:38:47 +090051#include "android/net/INetd.h"
Robin Leeb8087362016-03-30 18:43:08 +010052#include "android/net/UidRange.h"
Lorenzo Colitti89faa342016-02-26 11:38:47 +090053#include "binder/IServiceManager.h"
Nathan Harold21299f72018-03-16 20:13:03 -070054#include "netdutils/Syscalls.h"
Lorenzo Colitti89faa342016-02-26 11:38:47 +090055
Lorenzo Colitti5c68b9c2017-08-10 18:50:10 +090056#define IP_PATH "/system/bin/ip"
57#define IP6TABLES_PATH "/system/bin/ip6tables"
58#define IPTABLES_PATH "/system/bin/iptables"
Lorenzo Colitti755faa92016-07-27 22:10:49 +090059#define TUN_DEV "/dev/tun"
Luke Huang0051a622018-07-23 20:30:16 +080060#define RAW_TABLE "raw"
61#define MANGLE_TABLE "mangle"
Lorenzo Colitti755faa92016-07-27 22:10:49 +090062
Bernie Innocenti1bcc25a2018-08-10 17:15:00 +090063namespace binder = android::binder;
64namespace netdutils = android::netdutils;
65
66using android::IBinder;
67using android::IServiceManager;
68using android::sp;
69using android::String16;
70using android::String8;
71using android::base::Join;
Lorenzo Colittiaff28792017-09-26 17:46:18 +090072using android::base::StartsWith;
Bernie Innocenti1bcc25a2018-08-10 17:15:00 +090073using android::base::StringPrintf;
Chenbo Feng837ddfc2018-05-08 13:45:08 -070074using android::bpf::hasBpfSupport;
Lorenzo Colitti89faa342016-02-26 11:38:47 +090075using android::net::INetd;
Lorenzo Colitti1e299c62017-02-27 17:16:10 +090076using android::net::TunInterface;
Robin Leeb8087362016-03-30 18:43:08 +010077using android::net::UidRange;
Nathan Harold21299f72018-03-16 20:13:03 -070078using android::net::XfrmController;
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +090079using android::os::PersistableBundle;
Robin Leeb8087362016-03-30 18:43:08 +010080
Bernie Innocenti1bcc25a2018-08-10 17:15:00 +090081#define SKIP_IF_BPF_SUPPORTED \
82 do { \
83 if (hasBpfSupport()) return; \
84 } while (0)
Chenbo Feng837ddfc2018-05-08 13:45:08 -070085
Robin Leeb8087362016-03-30 18:43:08 +010086static const char* IP_RULE_V4 = "-4";
87static const char* IP_RULE_V6 = "-6";
Lorenzo Colittid33e96d2016-12-15 23:59:01 +090088static const int TEST_NETID1 = 65501;
89static const int TEST_NETID2 = 65502;
90constexpr int BASE_UID = AID_USER_OFFSET * 5;
Lorenzo Colitti89faa342016-02-26 11:38:47 +090091
Benedict Wongb2daefb2017-12-06 22:05:46 -080092static const std::string NO_SOCKET_ALLOW_RULE("! owner UID match 0-4294967294");
93static const std::string ESP_ALLOW_RULE("esp");
94
Lorenzo Colitti89faa342016-02-26 11:38:47 +090095class BinderTest : public ::testing::Test {
Bernie Innocenti1bcc25a2018-08-10 17:15:00 +090096 public:
Lorenzo Colitti89faa342016-02-26 11:38:47 +090097 BinderTest() {
Bernie Innocenti1bcc25a2018-08-10 17:15:00 +090098 sp<IServiceManager> sm = android::defaultServiceManager();
Lorenzo Colitti89faa342016-02-26 11:38:47 +090099 sp<IBinder> binder = sm->getService(String16("netd"));
100 if (binder != nullptr) {
Bernie Innocenti1bcc25a2018-08-10 17:15:00 +0900101 mNetd = android::interface_cast<INetd>(binder);
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900102 }
103 }
104
Lorenzo Colitti755faa92016-07-27 22:10:49 +0900105 void SetUp() override {
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900106 ASSERT_NE(nullptr, mNetd.get());
107 }
108
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900109 void TearDown() override {
110 mNetd->networkDestroy(TEST_NETID1);
111 mNetd->networkDestroy(TEST_NETID2);
112 }
113
Nathan Harold21299f72018-03-16 20:13:03 -0700114 bool allocateIpSecResources(bool expectOk, int32_t *spi);
115
Lorenzo Colitti755faa92016-07-27 22:10:49 +0900116 // Static because setting up the tun interface takes about 40ms.
117 static void SetUpTestCase() {
Lorenzo Colitti1e299c62017-02-27 17:16:10 +0900118 ASSERT_EQ(0, sTun.init());
119 ASSERT_LE(sTun.name().size(), static_cast<size_t>(IFNAMSIZ));
Lorenzo Colitti755faa92016-07-27 22:10:49 +0900120 }
121
122 static void TearDownTestCase() {
123 // Closing the socket removes the interface and IP addresses.
Lorenzo Colitti1e299c62017-02-27 17:16:10 +0900124 sTun.destroy();
Lorenzo Colitti755faa92016-07-27 22:10:49 +0900125 }
126
127 static void fakeRemoteSocketPair(int *clientSocket, int *serverSocket, int *acceptedSocket);
Lorenzo Colitti755faa92016-07-27 22:10:49 +0900128
Bernie Innocenti1bcc25a2018-08-10 17:15:00 +0900129 protected:
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900130 sp<INetd> mNetd;
Lorenzo Colitti1e299c62017-02-27 17:16:10 +0900131 static TunInterface sTun;
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900132};
133
Lorenzo Colitti1e299c62017-02-27 17:16:10 +0900134TunInterface BinderTest::sTun;
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900135
Lorenzo Colitti699aa992016-04-15 10:22:37 +0900136class TimedOperation : public Stopwatch {
Bernie Innocenti1bcc25a2018-08-10 17:15:00 +0900137 public:
Chih-Hung Hsieh18051052016-05-06 10:36:13 -0700138 explicit TimedOperation(const std::string &name): mName(name) {}
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900139 virtual ~TimedOperation() {
Lorenzo Colitti699aa992016-04-15 10:22:37 +0900140 fprintf(stderr, " %s: %6.1f ms\n", mName.c_str(), timeTaken());
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900141 }
142
Bernie Innocenti1bcc25a2018-08-10 17:15:00 +0900143 private:
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900144 std::string mName;
145};
146
Bernie Innocenti1bcc25a2018-08-10 17:15:00 +0900147TEST_F(BinderTest, IsAlive) {
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900148 TimedOperation t("isAlive RPC");
149 bool isAlive = false;
150 mNetd->isAlive(&isAlive);
151 ASSERT_TRUE(isAlive);
152}
153
154static int randomUid() {
155 return 100000 * arc4random_uniform(7) + 10000 + arc4random_uniform(5000);
156}
157
Robin Leeb8087362016-03-30 18:43:08 +0100158static std::vector<std::string> runCommand(const std::string& command) {
Lorenzo Colittidedd2712016-03-22 12:36:29 +0900159 std::vector<std::string> lines;
Bernie Innocentif6918262018-06-11 17:37:35 +0900160 FILE *f = popen(command.c_str(), "r"); // NOLINT(cert-env33-c)
161 if (f == nullptr) {
Lorenzo Colittidedd2712016-03-22 12:36:29 +0900162 perror("popen");
163 return lines;
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900164 }
Lorenzo Colittidedd2712016-03-22 12:36:29 +0900165
166 char *line = nullptr;
Robin Leeb8087362016-03-30 18:43:08 +0100167 size_t bufsize = 0;
168 ssize_t linelen = 0;
169 while ((linelen = getline(&line, &bufsize, f)) >= 0) {
Lorenzo Colittidedd2712016-03-22 12:36:29 +0900170 lines.push_back(std::string(line, linelen));
171 free(line);
172 line = nullptr;
173 }
174
175 pclose(f);
176 return lines;
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900177}
178
Robin Leeb8087362016-03-30 18:43:08 +0100179static std::vector<std::string> listIpRules(const char *ipVersion) {
180 std::string command = StringPrintf("%s %s rule list", IP_PATH, ipVersion);
181 return runCommand(command);
182}
183
184static std::vector<std::string> listIptablesRule(const char *binary, const char *chainName) {
Lorenzo Colitti80545772016-06-09 14:20:08 +0900185 std::string command = StringPrintf("%s -w -n -L %s", binary, chainName);
Robin Leeb8087362016-03-30 18:43:08 +0100186 return runCommand(command);
187}
188
Lorenzo Colittidedd2712016-03-22 12:36:29 +0900189static int iptablesRuleLineLength(const char *binary, const char *chainName) {
190 return listIptablesRule(binary, chainName).size();
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900191}
192
Benedict Wongb2daefb2017-12-06 22:05:46 -0800193static bool iptablesRuleExists(const char *binary,
194 const char *chainName,
Bernie Innocentif6918262018-06-11 17:37:35 +0900195 const std::string& expectedRule) {
Benedict Wongb2daefb2017-12-06 22:05:46 -0800196 std::vector<std::string> rules = listIptablesRule(binary, chainName);
Bernie Innocentif6918262018-06-11 17:37:35 +0900197 for (const auto& rule : rules) {
Benedict Wongb2daefb2017-12-06 22:05:46 -0800198 if(rule.find(expectedRule) != std::string::npos) {
199 return true;
200 }
201 }
202 return false;
203}
204
205static bool iptablesNoSocketAllowRuleExists(const char *chainName){
206 return iptablesRuleExists(IPTABLES_PATH, chainName, NO_SOCKET_ALLOW_RULE) &&
207 iptablesRuleExists(IP6TABLES_PATH, chainName, NO_SOCKET_ALLOW_RULE);
208}
209
210static bool iptablesEspAllowRuleExists(const char *chainName){
211 return iptablesRuleExists(IPTABLES_PATH, chainName, ESP_ALLOW_RULE) &&
212 iptablesRuleExists(IP6TABLES_PATH, chainName, ESP_ALLOW_RULE);
213}
214
Bernie Innocenti1bcc25a2018-08-10 17:15:00 +0900215TEST_F(BinderTest, FirewallReplaceUidChain) {
Chenbo Feng837ddfc2018-05-08 13:45:08 -0700216 SKIP_IF_BPF_SUPPORTED;
217
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900218 std::string chainName = StringPrintf("netd_binder_test_%u", arc4random_uniform(10000));
219 const int kNumUids = 500;
220 std::vector<int32_t> noUids(0);
221 std::vector<int32_t> uids(kNumUids);
222 for (int i = 0; i < kNumUids; i++) {
223 uids[i] = randomUid();
224 }
225
226 bool ret;
227 {
228 TimedOperation op(StringPrintf("Programming %d-UID whitelist chain", kNumUids));
Erik Klinef52d4522018-03-14 15:01:46 +0900229 mNetd->firewallReplaceUidChain(chainName, true, uids, &ret);
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900230 }
231 EXPECT_EQ(true, ret);
Benedict Wongb2daefb2017-12-06 22:05:46 -0800232 EXPECT_EQ((int) uids.size() + 9, iptablesRuleLineLength(IPTABLES_PATH, chainName.c_str()));
233 EXPECT_EQ((int) uids.size() + 15, iptablesRuleLineLength(IP6TABLES_PATH, chainName.c_str()));
234 EXPECT_EQ(true, iptablesNoSocketAllowRuleExists(chainName.c_str()));
235 EXPECT_EQ(true, iptablesEspAllowRuleExists(chainName.c_str()));
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900236 {
237 TimedOperation op("Clearing whitelist chain");
Erik Klinef52d4522018-03-14 15:01:46 +0900238 mNetd->firewallReplaceUidChain(chainName, false, noUids, &ret);
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900239 }
240 EXPECT_EQ(true, ret);
Lorenzo Colitti50b198a2017-03-30 02:50:09 +0900241 EXPECT_EQ(5, iptablesRuleLineLength(IPTABLES_PATH, chainName.c_str()));
242 EXPECT_EQ(5, iptablesRuleLineLength(IP6TABLES_PATH, chainName.c_str()));
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900243
244 {
245 TimedOperation op(StringPrintf("Programming %d-UID blacklist chain", kNumUids));
Erik Klinef52d4522018-03-14 15:01:46 +0900246 mNetd->firewallReplaceUidChain(chainName, false, uids, &ret);
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900247 }
248 EXPECT_EQ(true, ret);
Lorenzo Colitti50b198a2017-03-30 02:50:09 +0900249 EXPECT_EQ((int) uids.size() + 5, iptablesRuleLineLength(IPTABLES_PATH, chainName.c_str()));
250 EXPECT_EQ((int) uids.size() + 5, iptablesRuleLineLength(IP6TABLES_PATH, chainName.c_str()));
Benedict Wongb2daefb2017-12-06 22:05:46 -0800251 EXPECT_EQ(false, iptablesNoSocketAllowRuleExists(chainName.c_str()));
252 EXPECT_EQ(false, iptablesEspAllowRuleExists(chainName.c_str()));
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900253
254 {
255 TimedOperation op("Clearing blacklist chain");
Erik Klinef52d4522018-03-14 15:01:46 +0900256 mNetd->firewallReplaceUidChain(chainName, false, noUids, &ret);
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900257 }
258 EXPECT_EQ(true, ret);
Lorenzo Colitti50b198a2017-03-30 02:50:09 +0900259 EXPECT_EQ(5, iptablesRuleLineLength(IPTABLES_PATH, chainName.c_str()));
260 EXPECT_EQ(5, iptablesRuleLineLength(IP6TABLES_PATH, chainName.c_str()));
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900261
262 // Check that the call fails if iptables returns an error.
263 std::string veryLongStringName = "netd_binder_test_UnacceptablyLongIptablesChainName";
Erik Klinef52d4522018-03-14 15:01:46 +0900264 mNetd->firewallReplaceUidChain(veryLongStringName, true, noUids, &ret);
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900265 EXPECT_EQ(false, ret);
266}
Lorenzo Colittidedd2712016-03-22 12:36:29 +0900267
Bernie Innocenti1bcc25a2018-08-10 17:15:00 +0900268TEST_F(BinderTest, VirtualTunnelInterface) {
George Burgess IVc4a6d272018-05-21 14:41:57 -0700269 const struct TestData {
270 const std::string family;
271 const std::string deviceName;
272 const std::string localAddress;
273 const std::string remoteAddress;
manojboopathi8707f232018-01-02 14:45:47 -0800274 int32_t iKey;
275 int32_t oKey;
276 } kTestData[] = {
Nathan Harold21299f72018-03-16 20:13:03 -0700277 {"IPV4", "test_vti", "127.0.0.1", "8.8.8.8", 0x1234 + 53, 0x1234 + 53},
278 {"IPV6", "test_vti6", "::1", "2001:4860:4860::8888", 0x1234 + 50, 0x1234 + 50},
manojboopathi8707f232018-01-02 14:45:47 -0800279 };
280
281 for (unsigned int i = 0; i < arraysize(kTestData); i++) {
Nathan Harold21299f72018-03-16 20:13:03 -0700282 const auto& td = kTestData[i];
manojboopathi8707f232018-01-02 14:45:47 -0800283
284 binder::Status status;
285
286 // Create Virtual Tunnel Interface.
Nathan Harold21299f72018-03-16 20:13:03 -0700287 status = mNetd->addVirtualTunnelInterface(td.deviceName, td.localAddress, td.remoteAddress,
288 td.iKey, td.oKey);
manojboopathi8707f232018-01-02 14:45:47 -0800289 EXPECT_TRUE(status.isOk()) << td.family << status.exceptionMessage();
290
291 // Update Virtual Tunnel Interface.
292 status = mNetd->updateVirtualTunnelInterface(td.deviceName, td.localAddress,
293 td.remoteAddress, td.iKey, td.oKey);
294 EXPECT_TRUE(status.isOk()) << td.family << status.exceptionMessage();
295
296 // Remove Virtual Tunnel Interface.
297 status = mNetd->removeVirtualTunnelInterface(td.deviceName);
298 EXPECT_TRUE(status.isOk()) << td.family << status.exceptionMessage();
299 }
300}
301
Nathan Harold2deff322018-05-10 14:03:48 -0700302// IPsec tests are not run in 32 bit mode; both 32-bit kernels and
303// mismatched ABIs (64-bit kernel with 32-bit userspace) are unsupported.
304#if INTPTR_MAX != INT32_MAX
Nathan Harold21299f72018-03-16 20:13:03 -0700305#define RETURN_FALSE_IF_NEQ(_expect_, _ret_) \
306 do { if ((_expect_) != (_ret_)) return false; } while(false)
307bool BinderTest::allocateIpSecResources(bool expectOk, int32_t *spi) {
308 netdutils::Status status = XfrmController::ipSecAllocateSpi(0, "::", "::1", 123, spi);
309 SCOPED_TRACE(status);
310 RETURN_FALSE_IF_NEQ(status.ok(), expectOk);
311
312 // Add a policy
313 status = XfrmController::ipSecAddSecurityPolicy(0, 0, "::", "::1", 123, 0, 0);
314 SCOPED_TRACE(status);
315 RETURN_FALSE_IF_NEQ(status.ok(), expectOk);
316
317 // Add an ipsec interface
318 status = netdutils::statusFromErrno(
319 XfrmController::addVirtualTunnelInterface(
320 "ipsec_test", "::", "::1", 0xF00D, 0xD00D, false),
321 "addVirtualTunnelInterface");
322 return (status.ok() == expectOk);
323}
324
Bernie Innocenti1bcc25a2018-08-10 17:15:00 +0900325TEST_F(BinderTest, XfrmControllerInit) {
Nathan Harold21299f72018-03-16 20:13:03 -0700326 netdutils::Status status;
327 status = XfrmController::Init();
328 SCOPED_TRACE(status);
Nathan Harold2deff322018-05-10 14:03:48 -0700329
330 // Older devices or devices with mismatched Kernel/User ABI cannot support the IPsec
331 // feature.
332 if (status.code() == EOPNOTSUPP) return;
333
Nathan Harold21299f72018-03-16 20:13:03 -0700334 ASSERT_TRUE(status.ok());
335
336 int32_t spi = 0;
337
338 ASSERT_TRUE(allocateIpSecResources(true, &spi));
339 ASSERT_TRUE(allocateIpSecResources(false, &spi));
340
341 status = XfrmController::Init();
Nathan Harold39ad6622018-04-25 12:56:56 -0700342 ASSERT_TRUE(status.ok());
Nathan Harold21299f72018-03-16 20:13:03 -0700343 ASSERT_TRUE(allocateIpSecResources(true, &spi));
344
345 // Clean up
346 status = XfrmController::ipSecDeleteSecurityAssociation(0, "::", "::1", 123, spi, 0);
347 SCOPED_TRACE(status);
348 ASSERT_TRUE(status.ok());
349
350 status = XfrmController::ipSecDeleteSecurityPolicy(0, 0, "::", "::1", 0, 0);
351 SCOPED_TRACE(status);
352 ASSERT_TRUE(status.ok());
353
354 // Remove Virtual Tunnel Interface.
355 status = netdutils::statusFromErrno(
356 XfrmController::removeVirtualTunnelInterface("ipsec_test"),
357 "removeVirtualTunnelInterface");
358
359 ASSERT_TRUE(status.ok());
360}
Nathan Harold2deff322018-05-10 14:03:48 -0700361#endif
Nathan Harold21299f72018-03-16 20:13:03 -0700362
Lorenzo Colittidedd2712016-03-22 12:36:29 +0900363static int bandwidthDataSaverEnabled(const char *binary) {
Lorenzo Colitti464eabe2016-03-25 13:38:19 +0900364 std::vector<std::string> lines = listIptablesRule(binary, "bw_data_saver");
Lorenzo Colittidedd2712016-03-22 12:36:29 +0900365
366 // Output looks like this:
367 //
Lorenzo Colitti464eabe2016-03-25 13:38:19 +0900368 // Chain bw_data_saver (1 references)
Lorenzo Colittidedd2712016-03-22 12:36:29 +0900369 // target prot opt source destination
Lorenzo Colittidedd2712016-03-22 12:36:29 +0900370 // RETURN all -- 0.0.0.0/0 0.0.0.0/0
Lorenzo Colittiaff28792017-09-26 17:46:18 +0900371 //
372 // or:
373 //
374 // Chain bw_data_saver (1 references)
375 // target prot opt source destination
376 // ... possibly connectivity critical packet rules here ...
377 // REJECT all -- ::/0 ::/0
Lorenzo Colittidedd2712016-03-22 12:36:29 +0900378
Lorenzo Colittiaff28792017-09-26 17:46:18 +0900379 EXPECT_GE(lines.size(), 3U);
Lorenzo Colittidedd2712016-03-22 12:36:29 +0900380
Lorenzo Colittiaff28792017-09-26 17:46:18 +0900381 if (lines.size() == 3 && StartsWith(lines[2], "RETURN ")) {
382 // Data saver disabled.
383 return 0;
384 }
385
386 size_t minSize = (std::string(binary) == IPTABLES_PATH) ? 3 : 9;
387
388 if (lines.size() >= minSize && StartsWith(lines[lines.size() -1], "REJECT ")) {
389 // Data saver enabled.
390 return 1;
391 }
392
393 return -1;
Lorenzo Colittidedd2712016-03-22 12:36:29 +0900394}
395
396bool enableDataSaver(sp<INetd>& netd, bool enable) {
397 TimedOperation op(enable ? " Enabling data saver" : "Disabling data saver");
398 bool ret;
399 netd->bandwidthEnableDataSaver(enable, &ret);
400 return ret;
401}
402
403int getDataSaverState() {
404 const int enabled4 = bandwidthDataSaverEnabled(IPTABLES_PATH);
405 const int enabled6 = bandwidthDataSaverEnabled(IP6TABLES_PATH);
406 EXPECT_EQ(enabled4, enabled6);
407 EXPECT_NE(-1, enabled4);
408 EXPECT_NE(-1, enabled6);
409 if (enabled4 != enabled6 || (enabled6 != 0 && enabled6 != 1)) {
410 return -1;
411 }
412 return enabled6;
413}
414
Bernie Innocenti1bcc25a2018-08-10 17:15:00 +0900415TEST_F(BinderTest, BandwidthEnableDataSaver) {
Lorenzo Colittidedd2712016-03-22 12:36:29 +0900416 const int wasEnabled = getDataSaverState();
417 ASSERT_NE(-1, wasEnabled);
418
419 if (wasEnabled) {
420 ASSERT_TRUE(enableDataSaver(mNetd, false));
421 EXPECT_EQ(0, getDataSaverState());
422 }
423
424 ASSERT_TRUE(enableDataSaver(mNetd, false));
425 EXPECT_EQ(0, getDataSaverState());
426
427 ASSERT_TRUE(enableDataSaver(mNetd, true));
428 EXPECT_EQ(1, getDataSaverState());
429
430 ASSERT_TRUE(enableDataSaver(mNetd, true));
431 EXPECT_EQ(1, getDataSaverState());
432
433 if (!wasEnabled) {
434 ASSERT_TRUE(enableDataSaver(mNetd, false));
435 EXPECT_EQ(0, getDataSaverState());
436 }
437}
Robin Leeb8087362016-03-30 18:43:08 +0100438
439static bool ipRuleExistsForRange(const uint32_t priority, const UidRange& range,
440 const std::string& action, const char* ipVersion) {
441 // Output looks like this:
Robin Lee6c84ef62016-05-03 13:17:58 +0100442 // "12500:\tfrom all fwmark 0x0/0x20000 iif lo uidrange 1000-2000 prohibit"
Robin Leeb8087362016-03-30 18:43:08 +0100443 std::vector<std::string> rules = listIpRules(ipVersion);
444
445 std::string prefix = StringPrintf("%" PRIu32 ":", priority);
446 std::string suffix = StringPrintf(" iif lo uidrange %d-%d %s\n",
447 range.getStart(), range.getStop(), action.c_str());
Bernie Innocentif6918262018-06-11 17:37:35 +0900448 for (const auto& line : rules) {
Elliott Hughes2f445082017-12-20 12:39:35 -0800449 if (android::base::StartsWith(line, prefix) && android::base::EndsWith(line, suffix)) {
Robin Leeb8087362016-03-30 18:43:08 +0100450 return true;
451 }
452 }
453 return false;
454}
455
456static bool ipRuleExistsForRange(const uint32_t priority, const UidRange& range,
457 const std::string& action) {
458 bool existsIp4 = ipRuleExistsForRange(priority, range, action, IP_RULE_V4);
459 bool existsIp6 = ipRuleExistsForRange(priority, range, action, IP_RULE_V6);
460 EXPECT_EQ(existsIp4, existsIp6);
461 return existsIp4;
462}
463
Bernie Innocenti1bcc25a2018-08-10 17:15:00 +0900464TEST_F(BinderTest, NetworkInterfaces) {
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900465 EXPECT_TRUE(mNetd->networkCreatePhysical(TEST_NETID1, "").isOk());
466 EXPECT_EQ(EEXIST, mNetd->networkCreatePhysical(TEST_NETID1, "").serviceSpecificErrorCode());
467 EXPECT_EQ(EEXIST, mNetd->networkCreateVpn(TEST_NETID1, false, true).serviceSpecificErrorCode());
468 EXPECT_TRUE(mNetd->networkCreateVpn(TEST_NETID2, false, true).isOk());
469
470 EXPECT_TRUE(mNetd->networkAddInterface(TEST_NETID1, sTun.name()).isOk());
471 EXPECT_EQ(EBUSY,
472 mNetd->networkAddInterface(TEST_NETID2, sTun.name()).serviceSpecificErrorCode());
473
474 EXPECT_TRUE(mNetd->networkDestroy(TEST_NETID1).isOk());
475 EXPECT_TRUE(mNetd->networkAddInterface(TEST_NETID2, sTun.name()).isOk());
476 EXPECT_TRUE(mNetd->networkDestroy(TEST_NETID2).isOk());
477}
478
Bernie Innocenti1bcc25a2018-08-10 17:15:00 +0900479TEST_F(BinderTest, NetworkUidRules) {
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900480 const uint32_t RULE_PRIORITY_SECURE_VPN = 12000;
481
482 EXPECT_TRUE(mNetd->networkCreateVpn(TEST_NETID1, false, true).isOk());
483 EXPECT_EQ(EEXIST, mNetd->networkCreateVpn(TEST_NETID1, false, true).serviceSpecificErrorCode());
484 EXPECT_TRUE(mNetd->networkAddInterface(TEST_NETID1, sTun.name()).isOk());
485
486 std::vector<UidRange> uidRanges = {
487 {BASE_UID + 8005, BASE_UID + 8012},
488 {BASE_UID + 8090, BASE_UID + 8099}
489 };
490 UidRange otherRange(BASE_UID + 8190, BASE_UID + 8299);
491 std::string suffix = StringPrintf("lookup %s ", sTun.name().c_str());
492
493 EXPECT_TRUE(mNetd->networkAddUidRanges(TEST_NETID1, uidRanges).isOk());
494
495 EXPECT_TRUE(ipRuleExistsForRange(RULE_PRIORITY_SECURE_VPN, uidRanges[0], suffix));
496 EXPECT_FALSE(ipRuleExistsForRange(RULE_PRIORITY_SECURE_VPN, otherRange, suffix));
497 EXPECT_TRUE(mNetd->networkRemoveUidRanges(TEST_NETID1, uidRanges).isOk());
498 EXPECT_FALSE(ipRuleExistsForRange(RULE_PRIORITY_SECURE_VPN, uidRanges[0], suffix));
499
500 EXPECT_TRUE(mNetd->networkAddUidRanges(TEST_NETID1, uidRanges).isOk());
501 EXPECT_TRUE(ipRuleExistsForRange(RULE_PRIORITY_SECURE_VPN, uidRanges[1], suffix));
502 EXPECT_TRUE(mNetd->networkDestroy(TEST_NETID1).isOk());
503 EXPECT_FALSE(ipRuleExistsForRange(RULE_PRIORITY_SECURE_VPN, uidRanges[1], suffix));
504
505 EXPECT_EQ(ENONET, mNetd->networkDestroy(TEST_NETID1).serviceSpecificErrorCode());
506}
507
Bernie Innocenti1bcc25a2018-08-10 17:15:00 +0900508TEST_F(BinderTest, NetworkRejectNonSecureVpn) {
Robin Lee6c84ef62016-05-03 13:17:58 +0100509 constexpr uint32_t RULE_PRIORITY = 12500;
Robin Leeb8087362016-03-30 18:43:08 +0100510
Robin Leeb8087362016-03-30 18:43:08 +0100511 std::vector<UidRange> uidRanges = {
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900512 {BASE_UID + 150, BASE_UID + 224},
513 {BASE_UID + 226, BASE_UID + 300}
Robin Leeb8087362016-03-30 18:43:08 +0100514 };
515
516 const std::vector<std::string> initialRulesV4 = listIpRules(IP_RULE_V4);
517 const std::vector<std::string> initialRulesV6 = listIpRules(IP_RULE_V6);
518
519 // Create two valid rules.
520 ASSERT_TRUE(mNetd->networkRejectNonSecureVpn(true, uidRanges).isOk());
521 EXPECT_EQ(initialRulesV4.size() + 2, listIpRules(IP_RULE_V4).size());
522 EXPECT_EQ(initialRulesV6.size() + 2, listIpRules(IP_RULE_V6).size());
523 for (auto const& range : uidRanges) {
524 EXPECT_TRUE(ipRuleExistsForRange(RULE_PRIORITY, range, "prohibit"));
525 }
526
527 // Remove the rules.
528 ASSERT_TRUE(mNetd->networkRejectNonSecureVpn(false, uidRanges).isOk());
529 EXPECT_EQ(initialRulesV4.size(), listIpRules(IP_RULE_V4).size());
530 EXPECT_EQ(initialRulesV6.size(), listIpRules(IP_RULE_V6).size());
531 for (auto const& range : uidRanges) {
532 EXPECT_FALSE(ipRuleExistsForRange(RULE_PRIORITY, range, "prohibit"));
533 }
534
535 // Fail to remove the rules a second time after they are already deleted.
536 binder::Status status = mNetd->networkRejectNonSecureVpn(false, uidRanges);
537 ASSERT_EQ(binder::Status::EX_SERVICE_SPECIFIC, status.exceptionCode());
538 EXPECT_EQ(ENOENT, status.serviceSpecificErrorCode());
539
540 // All rules should be the same as before.
541 EXPECT_EQ(initialRulesV4, listIpRules(IP_RULE_V4));
542 EXPECT_EQ(initialRulesV6, listIpRules(IP_RULE_V6));
543}
Lorenzo Colitti563d98b2016-04-24 13:13:14 +0900544
Lorenzo Colitti755faa92016-07-27 22:10:49 +0900545// Create a socket pair that isLoopbackSocket won't think is local.
546void BinderTest::fakeRemoteSocketPair(int *clientSocket, int *serverSocket, int *acceptedSocket) {
Bernie Innocentif6918262018-06-11 17:37:35 +0900547 *serverSocket = socket(AF_INET6, SOCK_STREAM | SOCK_CLOEXEC, 0);
Lorenzo Colitti1e299c62017-02-27 17:16:10 +0900548 struct sockaddr_in6 server6 = { .sin6_family = AF_INET6, .sin6_addr = sTun.dstAddr() };
Lorenzo Colitti563d98b2016-04-24 13:13:14 +0900549 ASSERT_EQ(0, bind(*serverSocket, (struct sockaddr *) &server6, sizeof(server6)));
550
551 socklen_t addrlen = sizeof(server6);
552 ASSERT_EQ(0, getsockname(*serverSocket, (struct sockaddr *) &server6, &addrlen));
553 ASSERT_EQ(0, listen(*serverSocket, 10));
554
Bernie Innocentif6918262018-06-11 17:37:35 +0900555 *clientSocket = socket(AF_INET6, SOCK_STREAM | SOCK_CLOEXEC, 0);
Lorenzo Colitti1e299c62017-02-27 17:16:10 +0900556 struct sockaddr_in6 client6 = { .sin6_family = AF_INET6, .sin6_addr = sTun.srcAddr() };
Lorenzo Colitti755faa92016-07-27 22:10:49 +0900557 ASSERT_EQ(0, bind(*clientSocket, (struct sockaddr *) &client6, sizeof(client6)));
Lorenzo Colitti563d98b2016-04-24 13:13:14 +0900558 ASSERT_EQ(0, connect(*clientSocket, (struct sockaddr *) &server6, sizeof(server6)));
559 ASSERT_EQ(0, getsockname(*clientSocket, (struct sockaddr *) &client6, &addrlen));
560
Bernie Innocentif6918262018-06-11 17:37:35 +0900561 *acceptedSocket = accept4(*serverSocket, (struct sockaddr *) &server6, &addrlen, SOCK_CLOEXEC);
Lorenzo Colitti563d98b2016-04-24 13:13:14 +0900562 ASSERT_NE(-1, *acceptedSocket);
563
564 ASSERT_EQ(0, memcmp(&client6, &server6, sizeof(client6)));
565}
566
567void checkSocketpairOpen(int clientSocket, int acceptedSocket) {
568 char buf[4096];
569 EXPECT_EQ(4, write(clientSocket, "foo", sizeof("foo")));
570 EXPECT_EQ(4, read(acceptedSocket, buf, sizeof(buf)));
571 EXPECT_EQ(0, memcmp(buf, "foo", sizeof("foo")));
572}
573
574void checkSocketpairClosed(int clientSocket, int acceptedSocket) {
575 // Check that the client socket was closed with ECONNABORTED.
576 int ret = write(clientSocket, "foo", sizeof("foo"));
577 int err = errno;
578 EXPECT_EQ(-1, ret);
579 EXPECT_EQ(ECONNABORTED, err);
580
581 // Check that it sent a RST to the server.
582 ret = write(acceptedSocket, "foo", sizeof("foo"));
583 err = errno;
584 EXPECT_EQ(-1, ret);
585 EXPECT_EQ(ECONNRESET, err);
586}
587
Bernie Innocenti1bcc25a2018-08-10 17:15:00 +0900588TEST_F(BinderTest, SocketDestroy) {
Lorenzo Colitti563d98b2016-04-24 13:13:14 +0900589 int clientSocket, serverSocket, acceptedSocket;
Lorenzo Colitti755faa92016-07-27 22:10:49 +0900590 ASSERT_NO_FATAL_FAILURE(fakeRemoteSocketPair(&clientSocket, &serverSocket, &acceptedSocket));
Lorenzo Colitti563d98b2016-04-24 13:13:14 +0900591
592 // Pick a random UID in the system UID range.
593 constexpr int baseUid = AID_APP - 2000;
594 static_assert(baseUid > 0, "Not enough UIDs? Please fix this test.");
595 int uid = baseUid + 500 + arc4random_uniform(1000);
596 EXPECT_EQ(0, fchown(clientSocket, uid, -1));
597
598 // UID ranges that don't contain uid.
599 std::vector<UidRange> uidRanges = {
600 {baseUid + 42, baseUid + 449},
601 {baseUid + 1536, AID_APP - 4},
602 {baseUid + 498, uid - 1},
603 {uid + 1, baseUid + 1520},
604 };
605 // A skip list that doesn't contain UID.
606 std::vector<int32_t> skipUids { baseUid + 123, baseUid + 1600 };
607
608 // Close sockets. Our test socket should be intact.
609 EXPECT_TRUE(mNetd->socketDestroy(uidRanges, skipUids).isOk());
610 checkSocketpairOpen(clientSocket, acceptedSocket);
611
612 // UID ranges that do contain uid.
613 uidRanges = {
614 {baseUid + 42, baseUid + 449},
615 {baseUid + 1536, AID_APP - 4},
616 {baseUid + 498, baseUid + 1520},
617 };
618 // Add uid to the skip list.
619 skipUids.push_back(uid);
620
621 // Close sockets. Our test socket should still be intact because it's in the skip list.
622 EXPECT_TRUE(mNetd->socketDestroy(uidRanges, skipUids).isOk());
623 checkSocketpairOpen(clientSocket, acceptedSocket);
624
625 // Now remove uid from skipUids, and close sockets. Our test socket should have been closed.
626 skipUids.resize(skipUids.size() - 1);
627 EXPECT_TRUE(mNetd->socketDestroy(uidRanges, skipUids).isOk());
628 checkSocketpairClosed(clientSocket, acceptedSocket);
629
630 close(clientSocket);
631 close(serverSocket);
632 close(acceptedSocket);
633}
Erik Klinecc4f2732016-08-03 11:24:27 +0900634
635namespace {
636
637int netmaskToPrefixLength(const uint8_t *buf, size_t buflen) {
638 if (buf == nullptr) return -1;
639
640 int prefixLength = 0;
641 bool endOfContiguousBits = false;
642 for (unsigned int i = 0; i < buflen; i++) {
643 const uint8_t value = buf[i];
644
645 // Bad bit sequence: check for a contiguous set of bits from the high
646 // end by verifying that the inverted value + 1 is a power of 2
647 // (power of 2 iff. (v & (v - 1)) == 0).
648 const uint8_t inverse = ~value + 1;
649 if ((inverse & (inverse - 1)) != 0) return -1;
650
651 prefixLength += (value == 0) ? 0 : CHAR_BIT - ffs(value) + 1;
652
653 // Bogus netmask.
654 if (endOfContiguousBits && value != 0) return -1;
655
656 if (value != 0xff) endOfContiguousBits = true;
657 }
658
659 return prefixLength;
660}
661
662template<typename T>
663int netmaskToPrefixLength(const T *p) {
664 return netmaskToPrefixLength(reinterpret_cast<const uint8_t*>(p), sizeof(T));
665}
666
667
668static bool interfaceHasAddress(
669 const std::string &ifname, const char *addrString, int prefixLength) {
670 struct addrinfo *addrinfoList = nullptr;
671 ScopedAddrinfo addrinfoCleanup(addrinfoList);
672
673 const struct addrinfo hints = {
674 .ai_flags = AI_NUMERICHOST,
675 .ai_family = AF_UNSPEC,
676 .ai_socktype = SOCK_DGRAM,
677 };
678 if (getaddrinfo(addrString, nullptr, &hints, &addrinfoList) != 0 ||
679 addrinfoList == nullptr || addrinfoList->ai_addr == nullptr) {
680 return false;
681 }
682
683 struct ifaddrs *ifaddrsList = nullptr;
684 ScopedIfaddrs ifaddrsCleanup(ifaddrsList);
685
686 if (getifaddrs(&ifaddrsList) != 0) {
687 return false;
688 }
689
690 for (struct ifaddrs *addr = ifaddrsList; addr != nullptr; addr = addr->ifa_next) {
691 if (std::string(addr->ifa_name) != ifname ||
692 addr->ifa_addr == nullptr ||
693 addr->ifa_addr->sa_family != addrinfoList->ai_addr->sa_family) {
694 continue;
695 }
696
697 switch (addr->ifa_addr->sa_family) {
698 case AF_INET: {
699 auto *addr4 = reinterpret_cast<const struct sockaddr_in*>(addr->ifa_addr);
700 auto *want = reinterpret_cast<const struct sockaddr_in*>(addrinfoList->ai_addr);
701 if (memcmp(&addr4->sin_addr, &want->sin_addr, sizeof(want->sin_addr)) != 0) {
702 continue;
703 }
704
705 if (prefixLength < 0) return true; // not checking prefix lengths
706
707 if (addr->ifa_netmask == nullptr) return false;
708 auto *nm = reinterpret_cast<const struct sockaddr_in*>(addr->ifa_netmask);
709 EXPECT_EQ(prefixLength, netmaskToPrefixLength(&nm->sin_addr));
710 return (prefixLength == netmaskToPrefixLength(&nm->sin_addr));
711 }
712 case AF_INET6: {
713 auto *addr6 = reinterpret_cast<const struct sockaddr_in6*>(addr->ifa_addr);
714 auto *want = reinterpret_cast<const struct sockaddr_in6*>(addrinfoList->ai_addr);
715 if (memcmp(&addr6->sin6_addr, &want->sin6_addr, sizeof(want->sin6_addr)) != 0) {
716 continue;
717 }
718
719 if (prefixLength < 0) return true; // not checking prefix lengths
720
721 if (addr->ifa_netmask == nullptr) return false;
722 auto *nm = reinterpret_cast<const struct sockaddr_in6*>(addr->ifa_netmask);
723 EXPECT_EQ(prefixLength, netmaskToPrefixLength(&nm->sin6_addr));
724 return (prefixLength == netmaskToPrefixLength(&nm->sin6_addr));
725 }
726 default:
727 // Cannot happen because we have already screened for matching
728 // address families at the top of each iteration.
729 continue;
730 }
731 }
732
733 return false;
734}
735
736} // namespace
737
Bernie Innocenti1bcc25a2018-08-10 17:15:00 +0900738TEST_F(BinderTest, InterfaceAddRemoveAddress) {
Erik Klinecc4f2732016-08-03 11:24:27 +0900739 static const struct TestData {
740 const char *addrString;
741 const int prefixLength;
742 const bool expectSuccess;
743 } kTestData[] = {
744 { "192.0.2.1", 24, true },
745 { "192.0.2.2", 25, true },
746 { "192.0.2.3", 32, true },
747 { "192.0.2.4", 33, false },
748 { "192.not.an.ip", 24, false },
749 { "2001:db8::1", 64, true },
750 { "2001:db8::2", 65, true },
751 { "2001:db8::3", 128, true },
752 { "2001:db8::4", 129, false },
753 { "foo:bar::bad", 64, false },
754 };
755
756 for (unsigned int i = 0; i < arraysize(kTestData); i++) {
757 const auto &td = kTestData[i];
758
759 // [1.a] Add the address.
760 binder::Status status = mNetd->interfaceAddAddress(
Lorenzo Colitti1e299c62017-02-27 17:16:10 +0900761 sTun.name(), td.addrString, td.prefixLength);
Erik Klinecc4f2732016-08-03 11:24:27 +0900762 if (td.expectSuccess) {
763 EXPECT_TRUE(status.isOk()) << status.exceptionMessage();
764 } else {
765 ASSERT_EQ(binder::Status::EX_SERVICE_SPECIFIC, status.exceptionCode());
766 ASSERT_NE(0, status.serviceSpecificErrorCode());
767 }
768
769 // [1.b] Verify the addition meets the expectation.
770 if (td.expectSuccess) {
Lorenzo Colitti1e299c62017-02-27 17:16:10 +0900771 EXPECT_TRUE(interfaceHasAddress(sTun.name(), td.addrString, td.prefixLength));
Erik Klinecc4f2732016-08-03 11:24:27 +0900772 } else {
Lorenzo Colitti1e299c62017-02-27 17:16:10 +0900773 EXPECT_FALSE(interfaceHasAddress(sTun.name(), td.addrString, -1));
Erik Klinecc4f2732016-08-03 11:24:27 +0900774 }
775
776 // [2.a] Try to remove the address. If it was not previously added, removing it fails.
Lorenzo Colitti1e299c62017-02-27 17:16:10 +0900777 status = mNetd->interfaceDelAddress(sTun.name(), td.addrString, td.prefixLength);
Erik Klinecc4f2732016-08-03 11:24:27 +0900778 if (td.expectSuccess) {
779 EXPECT_TRUE(status.isOk()) << status.exceptionMessage();
780 } else {
781 ASSERT_EQ(binder::Status::EX_SERVICE_SPECIFIC, status.exceptionCode());
782 ASSERT_NE(0, status.serviceSpecificErrorCode());
783 }
784
785 // [2.b] No matter what, the address should not be present.
Lorenzo Colitti1e299c62017-02-27 17:16:10 +0900786 EXPECT_FALSE(interfaceHasAddress(sTun.name(), td.addrString, -1));
Erik Klinecc4f2732016-08-03 11:24:27 +0900787 }
788}
Erik Kline55b06f82016-07-04 09:57:18 +0900789
Bernie Innocenti1bcc25a2018-08-10 17:15:00 +0900790TEST_F(BinderTest, SetProcSysNet) {
Erik Kline55b06f82016-07-04 09:57:18 +0900791 static const struct TestData {
792 const int family;
793 const int which;
794 const char *ifname;
795 const char *parameter;
796 const char *value;
797 const int expectedReturnCode;
798 } kTestData[] = {
Lorenzo Colitti1e299c62017-02-27 17:16:10 +0900799 { INetd::IPV4, INetd::CONF, sTun.name().c_str(), "arp_ignore", "1", 0 },
800 { -1, INetd::CONF, sTun.name().c_str(), "arp_ignore", "1", EAFNOSUPPORT },
801 { INetd::IPV4, -1, sTun.name().c_str(), "arp_ignore", "1", EINVAL },
Erik Kline55b06f82016-07-04 09:57:18 +0900802 { INetd::IPV4, INetd::CONF, "..", "conf/lo/arp_ignore", "1", EINVAL },
803 { INetd::IPV4, INetd::CONF, ".", "lo/arp_ignore", "1", EINVAL },
Lorenzo Colitti1e299c62017-02-27 17:16:10 +0900804 { INetd::IPV4, INetd::CONF, sTun.name().c_str(), "../all/arp_ignore", "1", EINVAL },
805 { INetd::IPV6, INetd::NEIGH, sTun.name().c_str(), "ucast_solicit", "7", 0 },
Erik Kline55b06f82016-07-04 09:57:18 +0900806 };
807
808 for (unsigned int i = 0; i < arraysize(kTestData); i++) {
809 const auto &td = kTestData[i];
810
811 const binder::Status status = mNetd->setProcSysNet(
812 td.family, td.which, td.ifname, td.parameter,
813 td.value);
814
815 if (td.expectedReturnCode == 0) {
816 SCOPED_TRACE(String8::format("test case %d should have passed", i));
817 EXPECT_EQ(0, status.exceptionCode());
818 EXPECT_EQ(0, status.serviceSpecificErrorCode());
819 } else {
820 SCOPED_TRACE(String8::format("test case %d should have failed", i));
821 EXPECT_EQ(binder::Status::EX_SERVICE_SPECIFIC, status.exceptionCode());
822 EXPECT_EQ(td.expectedReturnCode, status.serviceSpecificErrorCode());
823 }
824 }
825}
Ben Schwartze7601812017-04-28 16:38:29 -0400826
827static std::string base64Encode(const std::vector<uint8_t>& input) {
828 size_t out_len;
829 EXPECT_EQ(1, EVP_EncodedLength(&out_len, input.size()));
830 // out_len includes the trailing NULL.
831 uint8_t output_bytes[out_len];
832 EXPECT_EQ(out_len - 1, EVP_EncodeBlock(output_bytes, input.data(), input.size()));
833 return std::string(reinterpret_cast<char*>(output_bytes));
834}
835
Bernie Innocenti1bcc25a2018-08-10 17:15:00 +0900836TEST_F(BinderTest, SetResolverConfiguration_Tls) {
Erik Klinea1476fb2018-03-04 21:01:56 +0900837 const std::vector<std::string> LOCALLY_ASSIGNED_DNS{"8.8.8.8", "2001:4860:4860::8888"};
Ben Schwartze7601812017-04-28 16:38:29 -0400838 std::vector<uint8_t> fp(SHA256_SIZE);
Ben Schwartz4204ecf2017-10-02 12:35:48 -0400839 std::vector<uint8_t> short_fp(1);
840 std::vector<uint8_t> long_fp(SHA256_SIZE + 1);
841 std::vector<std::string> test_domains;
842 std::vector<int> test_params = { 300, 25, 8, 8 };
843 unsigned test_netid = 0;
Ben Schwartze7601812017-04-28 16:38:29 -0400844 static const struct TestData {
Ben Schwartz4204ecf2017-10-02 12:35:48 -0400845 const std::vector<std::string> servers;
846 const std::string tlsName;
847 const std::vector<std::vector<uint8_t>> tlsFingerprints;
Ben Schwartze7601812017-04-28 16:38:29 -0400848 const int expectedReturnCode;
Erik Klinea1476fb2018-03-04 21:01:56 +0900849 } kTlsTestData[] = {
Ben Schwartz4204ecf2017-10-02 12:35:48 -0400850 { {"192.0.2.1"}, "", {}, 0 },
851 { {"2001:db8::2"}, "host.name", {}, 0 },
852 { {"192.0.2.3"}, "@@@@", { fp }, 0 },
853 { {"2001:db8::4"}, "", { fp }, 0 },
Erik Klinea1476fb2018-03-04 21:01:56 +0900854 { {}, "", {}, 0 },
Ben Schwartz4204ecf2017-10-02 12:35:48 -0400855 { {""}, "", {}, EINVAL },
Erik Klinea1476fb2018-03-04 21:01:56 +0900856 { {"192.0.*.5"}, "", {}, EINVAL },
Ben Schwartz4204ecf2017-10-02 12:35:48 -0400857 { {"2001:dg8::6"}, "", {}, EINVAL },
858 { {"2001:db8::c"}, "", { short_fp }, EINVAL },
859 { {"192.0.2.12"}, "", { long_fp }, EINVAL },
860 { {"2001:db8::e"}, "", { fp, fp, fp }, 0 },
861 { {"192.0.2.14"}, "", { fp, short_fp }, EINVAL },
Ben Schwartze7601812017-04-28 16:38:29 -0400862 };
863
Erik Klinea1476fb2018-03-04 21:01:56 +0900864 for (unsigned int i = 0; i < arraysize(kTlsTestData); i++) {
865 const auto &td = kTlsTestData[i];
Ben Schwartze7601812017-04-28 16:38:29 -0400866
867 std::vector<std::string> fingerprints;
Ben Schwartz4204ecf2017-10-02 12:35:48 -0400868 for (const auto& fingerprint : td.tlsFingerprints) {
Ben Schwartze7601812017-04-28 16:38:29 -0400869 fingerprints.push_back(base64Encode(fingerprint));
870 }
Ben Schwartz4204ecf2017-10-02 12:35:48 -0400871 binder::Status status = mNetd->setResolverConfiguration(
Erik Klinea1476fb2018-03-04 21:01:56 +0900872 test_netid, LOCALLY_ASSIGNED_DNS, test_domains, test_params,
873 td.tlsName, td.servers, fingerprints);
Ben Schwartze7601812017-04-28 16:38:29 -0400874
Ben Schwartz4204ecf2017-10-02 12:35:48 -0400875 if (td.expectedReturnCode == 0) {
Ben Schwartze7601812017-04-28 16:38:29 -0400876 SCOPED_TRACE(String8::format("test case %d should have passed", i));
877 SCOPED_TRACE(status.toString8());
878 EXPECT_EQ(0, status.exceptionCode());
879 } else {
880 SCOPED_TRACE(String8::format("test case %d should have failed", i));
881 EXPECT_EQ(binder::Status::EX_SERVICE_SPECIFIC, status.exceptionCode());
Ben Schwartz4204ecf2017-10-02 12:35:48 -0400882 EXPECT_EQ(td.expectedReturnCode, status.serviceSpecificErrorCode());
Ben Schwartze7601812017-04-28 16:38:29 -0400883 }
Ben Schwartze7601812017-04-28 16:38:29 -0400884 }
Ben Schwartz4204ecf2017-10-02 12:35:48 -0400885 // Ensure TLS is disabled before the start of the next test.
886 mNetd->setResolverConfiguration(
Erik Klinea1476fb2018-03-04 21:01:56 +0900887 test_netid, kTlsTestData[0].servers, test_domains, test_params,
888 "", {}, {});
Ben Schwartze7601812017-04-28 16:38:29 -0400889}
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900890
Bernie Innocenti1bcc25a2018-08-10 17:15:00 +0900891namespace {
892
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900893void expectNoTestCounterRules() {
894 for (const auto& binary : { IPTABLES_PATH, IP6TABLES_PATH }) {
895 std::string command = StringPrintf("%s -w -nvL tetherctrl_counters", binary);
896 std::string allRules = Join(runCommand(command), "\n");
897 EXPECT_EQ(std::string::npos, allRules.find("netdtest_"));
898 }
899}
900
Bernie Innocentif6918262018-06-11 17:37:35 +0900901void addTetherCounterValues(const char* path, const std::string& if1, const std::string& if2,
902 int byte, int pkt) {
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900903 runCommand(StringPrintf("%s -w -A tetherctrl_counters -i %s -o %s -j RETURN -c %d %d",
904 path, if1.c_str(), if2.c_str(), pkt, byte));
905}
906
Bernie Innocentif6918262018-06-11 17:37:35 +0900907void delTetherCounterValues(const char* path, const std::string& if1, const std::string& if2) {
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900908 runCommand(StringPrintf("%s -w -D tetherctrl_counters -i %s -o %s -j RETURN",
909 path, if1.c_str(), if2.c_str()));
910 runCommand(StringPrintf("%s -w -D tetherctrl_counters -i %s -o %s -j RETURN",
911 path, if2.c_str(), if1.c_str()));
912}
913
Bernie Innocenti1bcc25a2018-08-10 17:15:00 +0900914} // namespace
915
916TEST_F(BinderTest, TetherGetStats) {
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900917 expectNoTestCounterRules();
918
919 // TODO: fold this into more comprehensive tests once we have binder RPCs for enabling and
920 // disabling tethering. We don't check the return value because these commands will fail if
921 // tethering is already enabled.
922 runCommand(StringPrintf("%s -w -N tetherctrl_counters", IPTABLES_PATH));
923 runCommand(StringPrintf("%s -w -N tetherctrl_counters", IP6TABLES_PATH));
924
925 std::string intIface1 = StringPrintf("netdtest_%u", arc4random_uniform(10000));
926 std::string intIface2 = StringPrintf("netdtest_%u", arc4random_uniform(10000));
927 std::string intIface3 = StringPrintf("netdtest_%u", arc4random_uniform(10000));
928 std::string extIface1 = StringPrintf("netdtest_%u", arc4random_uniform(10000));
929 std::string extIface2 = StringPrintf("netdtest_%u", arc4random_uniform(10000));
930
931 addTetherCounterValues(IPTABLES_PATH, intIface1, extIface1, 123, 111);
932 addTetherCounterValues(IP6TABLES_PATH, intIface1, extIface1, 456, 10);
933 addTetherCounterValues(IPTABLES_PATH, extIface1, intIface1, 321, 222);
934 addTetherCounterValues(IP6TABLES_PATH, extIface1, intIface1, 654, 20);
935 // RX is from external to internal, and TX is from internal to external.
936 // So rxBytes is 321 + 654 = 975, txBytes is 123 + 456 = 579, etc.
937 std::vector<int64_t> expected1 = { 975, 242, 579, 121 };
938
939 addTetherCounterValues(IPTABLES_PATH, intIface2, extIface2, 1000, 333);
940 addTetherCounterValues(IP6TABLES_PATH, intIface2, extIface2, 3000, 30);
941
942 addTetherCounterValues(IPTABLES_PATH, extIface2, intIface2, 2000, 444);
943 addTetherCounterValues(IP6TABLES_PATH, extIface2, intIface2, 4000, 40);
944
945 addTetherCounterValues(IP6TABLES_PATH, intIface3, extIface2, 1000, 25);
946 addTetherCounterValues(IP6TABLES_PATH, extIface2, intIface3, 2000, 35);
947 std::vector<int64_t> expected2 = { 8000, 519, 5000, 388 };
948
949 PersistableBundle stats;
950 binder::Status status = mNetd->tetherGetStats(&stats);
951 EXPECT_TRUE(status.isOk()) << "Getting tethering stats failed: " << status;
952
953 std::vector<int64_t> actual1;
954 EXPECT_TRUE(stats.getLongVector(String16(extIface1.c_str()), &actual1));
955 EXPECT_EQ(expected1, actual1);
956
957 std::vector<int64_t> actual2;
958 EXPECT_TRUE(stats.getLongVector(String16(extIface2.c_str()), &actual2));
959 EXPECT_EQ(expected2, actual2);
960
961 for (const auto& path : { IPTABLES_PATH, IP6TABLES_PATH }) {
962 delTetherCounterValues(path, intIface1, extIface1);
963 delTetherCounterValues(path, intIface2, extIface2);
964 if (path == IP6TABLES_PATH) {
965 delTetherCounterValues(path, intIface3, extIface2);
966 }
967 }
968
969 expectNoTestCounterRules();
970}
Bernie Innocenti1bcc25a2018-08-10 17:15:00 +0900971
Luke Huang0051a622018-07-23 20:30:16 +0800972namespace {
973
974constexpr char chainName_LOCAL_RAW_PREROUTING[] = "idletimer_raw_PREROUTING";
975constexpr char chainName_MANGLE_POSTROUTING[] = "idletimer_mangle_POSTROUTING";
976
977static std::vector<std::string> listIptablesRuleByTable(const char* binary, const char* table,
978 const char* chainName) {
979 std::string command = StringPrintf("%s -t %s -w -n -v -L %s", binary, table, chainName);
980 return runCommand(command);
981}
982
983bool iptablesIdleTimerInterfcaeRuleExists(const char* binary, const char* chainName,
984 const std::string& expectedInterface,
985 const std::string& expectedRule, const char* table) {
986 std::vector<std::string> rules = listIptablesRuleByTable(binary, table, chainName);
987 for (const auto& rule : rules) {
988 if (rule.find(expectedInterface) != std::string::npos) {
989 if (rule.find(expectedRule) != std::string::npos) {
990 return true;
991 }
992 }
993 }
994 return false;
995}
996
997void expectIdletimerInterfaceRuleExists(const std::string& ifname, int timeout,
998 const std::string& classLable) {
999 std::string IdletimerRule =
1000 StringPrintf("timeout:%u label:%s send_nl_msg:1", timeout, classLable.c_str());
1001 for (const auto& binary : {IPTABLES_PATH, IP6TABLES_PATH}) {
1002 EXPECT_TRUE(iptablesIdleTimerInterfcaeRuleExists(binary, chainName_LOCAL_RAW_PREROUTING,
1003 ifname, IdletimerRule, RAW_TABLE));
1004 EXPECT_TRUE(iptablesIdleTimerInterfcaeRuleExists(binary, chainName_MANGLE_POSTROUTING,
1005 ifname, IdletimerRule, MANGLE_TABLE));
1006 }
1007}
1008
1009void expectIdletimerInterfaceRuleNotExists(const std::string& ifname, int timeout,
1010 const std::string& classLable) {
1011 std::string IdletimerRule =
1012 StringPrintf("timeout:%u label:%s send_nl_msg:1", timeout, classLable.c_str());
1013 for (const auto& binary : {IPTABLES_PATH, IP6TABLES_PATH}) {
1014 EXPECT_FALSE(iptablesIdleTimerInterfcaeRuleExists(binary, chainName_LOCAL_RAW_PREROUTING,
1015 ifname, IdletimerRule, RAW_TABLE));
1016 EXPECT_FALSE(iptablesIdleTimerInterfcaeRuleExists(binary, chainName_MANGLE_POSTROUTING,
1017 ifname, IdletimerRule, MANGLE_TABLE));
1018 }
1019}
1020
Bernie Innocenti1bcc25a2018-08-10 17:15:00 +09001021} // namespace
1022
1023TEST_F(BinderTest, IdletimerAddRemoveInterface) {
Luke Huang0051a622018-07-23 20:30:16 +08001024 // TODO: We will get error in if expectIdletimerInterfaceRuleNotExists if there are the same
1025 // rule in the table. Because we only check the result after calling remove function. We might
1026 // check the actual rule which is removed by our function (maybe compare the results between
1027 // calling function before and after)
1028 binder::Status status;
1029 const struct TestData {
1030 const std::string ifname;
1031 int32_t timeout;
1032 const std::string classLabel;
1033 } idleTestData[] = {
1034 {"wlan0", 1234, "happyday"},
1035 {"rmnet_data0", 4567, "friday"},
1036 };
1037 for (const auto& td : idleTestData) {
1038 status = mNetd->idletimerAddInterface(td.ifname, td.timeout, td.classLabel);
1039 EXPECT_TRUE(status.isOk()) << status.exceptionMessage();
1040 expectIdletimerInterfaceRuleExists(td.ifname, td.timeout, td.classLabel);
1041
1042 status = mNetd->idletimerRemoveInterface(td.ifname, td.timeout, td.classLabel);
1043 EXPECT_TRUE(status.isOk()) << status.exceptionMessage();
1044 expectIdletimerInterfaceRuleNotExists(td.ifname, td.timeout, td.classLabel);
1045 }
1046}
1047
Luke Huanga67dd562018-07-17 19:58:25 +08001048namespace {
1049
1050constexpr char STRICT_OUTPUT[] = "st_OUTPUT";
1051constexpr char STRICT_CLEAR_CAUGHT[] = "st_clear_caught";
1052
1053void expectStrictSetUidAccept(const int uid) {
1054 std::string uidRule = StringPrintf("owner UID match %u", uid);
1055 std::string perUidChain = StringPrintf("st_clear_caught_%u", uid);
1056 for (const auto& binary : {IPTABLES_PATH, IP6TABLES_PATH}) {
1057 EXPECT_FALSE(iptablesRuleExists(binary, STRICT_OUTPUT, uidRule.c_str()));
1058 EXPECT_FALSE(iptablesRuleExists(binary, STRICT_CLEAR_CAUGHT, uidRule.c_str()));
1059 EXPECT_EQ(0, iptablesRuleLineLength(binary, perUidChain.c_str()));
1060 }
1061}
1062
1063void expectStrictSetUidLog(const int uid) {
1064 static const char logRule[] = "st_penalty_log all";
1065 std::string uidRule = StringPrintf("owner UID match %u", uid);
1066 std::string perUidChain = StringPrintf("st_clear_caught_%u", uid);
1067 for (const auto& binary : {IPTABLES_PATH, IP6TABLES_PATH}) {
1068 EXPECT_TRUE(iptablesRuleExists(binary, STRICT_OUTPUT, uidRule.c_str()));
1069 EXPECT_TRUE(iptablesRuleExists(binary, STRICT_CLEAR_CAUGHT, uidRule.c_str()));
1070 EXPECT_TRUE(iptablesRuleExists(binary, perUidChain.c_str(), logRule));
1071 }
1072}
1073
1074void expectStrictSetUidReject(const int uid) {
1075 static const char rejectRule[] = "st_penalty_reject all";
1076 std::string uidRule = StringPrintf("owner UID match %u", uid);
1077 std::string perUidChain = StringPrintf("st_clear_caught_%u", uid);
1078 for (const auto& binary : {IPTABLES_PATH, IP6TABLES_PATH}) {
1079 EXPECT_TRUE(iptablesRuleExists(binary, STRICT_OUTPUT, uidRule.c_str()));
1080 EXPECT_TRUE(iptablesRuleExists(binary, STRICT_CLEAR_CAUGHT, uidRule.c_str()));
1081 EXPECT_TRUE(iptablesRuleExists(binary, perUidChain.c_str(), rejectRule));
1082 }
1083}
1084
Bernie Innocenti1bcc25a2018-08-10 17:15:00 +09001085} // namespace
1086
1087TEST_F(BinderTest, StrictSetUidCleartextPenalty) {
Luke Huanga67dd562018-07-17 19:58:25 +08001088 binder::Status status;
1089 int32_t uid = randomUid();
1090
1091 // setUidCleartextPenalty Policy:Log with randomUid
1092 status = mNetd->strictUidCleartextPenalty(uid, INetd::PENALTY_POLICY_LOG);
1093 EXPECT_TRUE(status.isOk()) << status.exceptionMessage();
1094 expectStrictSetUidLog(uid);
1095
1096 // setUidCleartextPenalty Policy:Accept with randomUid
1097 status = mNetd->strictUidCleartextPenalty(uid, INetd::PENALTY_POLICY_ACCEPT);
1098 expectStrictSetUidAccept(uid);
1099
1100 // setUidCleartextPenalty Policy:Reject with randomUid
1101 status = mNetd->strictUidCleartextPenalty(uid, INetd::PENALTY_POLICY_REJECT);
1102 EXPECT_TRUE(status.isOk()) << status.exceptionMessage();
1103 expectStrictSetUidReject(uid);
1104
1105 // setUidCleartextPenalty Policy:Accept with randomUid
1106 status = mNetd->strictUidCleartextPenalty(uid, INetd::PENALTY_POLICY_ACCEPT);
1107 expectStrictSetUidAccept(uid);
1108
1109 // test wrong policy
1110 int32_t wrongPolicy = -123;
1111 status = mNetd->strictUidCleartextPenalty(uid, wrongPolicy);
1112 EXPECT_EQ(EINVAL, status.serviceSpecificErrorCode());
1113}
1114
Bernie Innocenti1bcc25a2018-08-10 17:15:00 +09001115namespace {
Luke Huang6d301232018-08-01 14:05:18 +08001116
1117static bool processExists(const std::string& processName) {
1118 std::string cmd = StringPrintf("ps -A | grep '%s'", processName.c_str());
1119 return (runCommand(cmd.c_str()).size()) ? true : false;
1120}
1121
Bernie Innocenti1bcc25a2018-08-10 17:15:00 +09001122} // namespace
1123
1124TEST_F(BinderTest, ClatdStartStop) {
Luke Huang6d301232018-08-01 14:05:18 +08001125 binder::Status status;
1126 // use dummy0 for test since it is set ready
1127 static const char testIf[] = "dummy0";
1128 const std::string clatdName = StringPrintf("clatd-%s", testIf);
1129
1130 status = mNetd->clatdStart(testIf);
1131 EXPECT_TRUE(status.isOk()) << status.exceptionMessage();
1132 EXPECT_TRUE(processExists(clatdName));
1133
1134 mNetd->clatdStop(testIf);
1135 EXPECT_TRUE(status.isOk()) << status.exceptionMessage();
1136 EXPECT_FALSE(processExists(clatdName));
1137}
Luke Huang457d4702018-08-16 15:39:15 +08001138
1139namespace {
1140
1141bool getIpfwdV4Enable() {
1142 static const char ipv4IpfwdCmd[] = "cat /proc/sys/net/ipv4/ip_forward";
1143 std::vector<std::string> result = runCommand(ipv4IpfwdCmd);
1144 EXPECT_TRUE(!result.empty());
1145 int v4Enable = std::stoi(result[0]);
1146 return v4Enable;
1147}
1148
1149bool getIpfwdV6Enable() {
1150 static const char ipv6IpfwdCmd[] = "cat proc/sys/net/ipv6/conf/all/forwarding";
1151 std::vector<std::string> result = runCommand(ipv6IpfwdCmd);
1152 EXPECT_TRUE(!result.empty());
1153 int v6Enable = std::stoi(result[0]);
1154 return v6Enable;
1155}
1156
1157void expectIpfwdEnable(bool enable) {
1158 int enableIPv4 = getIpfwdV4Enable();
1159 int enableIPv6 = getIpfwdV6Enable();
1160 EXPECT_EQ(enable, enableIPv4);
1161 EXPECT_EQ(enable, enableIPv6);
1162}
1163
1164bool ipRuleIpfwdExists(const char* ipVersion, const std::string ipfwdRule) {
1165 std::vector<std::string> rules = listIpRules(ipVersion);
1166 for (const auto& rule : rules) {
1167 if (rule.find(ipfwdRule) != std::string::npos) {
1168 return true;
1169 }
1170 }
1171 return false;
1172}
1173
1174void expectIpfwdRuleExists(const char* fromIf, const char* toIf) {
1175 std::string ipfwdRule = StringPrintf("18000:\tfrom all iif %s lookup %s ", fromIf, toIf);
1176
1177 for (const auto& ipVersion : {IP_RULE_V4, IP_RULE_V6}) {
1178 EXPECT_TRUE(ipRuleIpfwdExists(ipVersion, ipfwdRule));
1179 }
1180}
1181
1182void expectIpfwdRuleNotExists(const char* fromIf, const char* toIf) {
1183 std::string ipfwdRule = StringPrintf("18000:\tfrom all iif %s lookup %s ", fromIf, toIf);
1184
1185 for (const auto& ipVersion : {IP_RULE_V4, IP_RULE_V6}) {
1186 EXPECT_FALSE(ipRuleIpfwdExists(ipVersion, ipfwdRule));
1187 }
1188}
1189
1190} // namespace
1191
1192TEST_F(BinderTest, TestIpfwdEnableDisableStatusForwarding) {
1193 // Netd default enable Ipfwd with requester NetdHwService
1194 const std::string defaultRequester = "NetdHwService";
1195
1196 binder::Status status = mNetd->ipfwdDisableForwarding(defaultRequester);
1197 EXPECT_TRUE(status.isOk()) << status.exceptionMessage();
1198 expectIpfwdEnable(false);
1199
1200 bool ipfwdEnabled;
1201 status = mNetd->ipfwdEnabled(&ipfwdEnabled);
1202 EXPECT_TRUE(status.isOk()) << status.exceptionMessage();
1203 EXPECT_FALSE(ipfwdEnabled);
1204
1205 status = mNetd->ipfwdEnableForwarding(defaultRequester);
1206 EXPECT_TRUE(status.isOk()) << status.exceptionMessage();
1207 expectIpfwdEnable(true);
1208
1209 status = mNetd->ipfwdEnabled(&ipfwdEnabled);
1210 EXPECT_TRUE(status.isOk()) << status.exceptionMessage();
1211 EXPECT_TRUE(ipfwdEnabled);
1212}
1213
1214TEST_F(BinderTest, TestIpfwdAddRemoveInterfaceForward) {
1215 static const char testFromIf[] = "dummy0";
1216 static const char testToIf[] = "dummy0";
1217
1218 binder::Status status = mNetd->ipfwdAddInterfaceForward(testFromIf, testToIf);
1219 EXPECT_TRUE(status.isOk()) << status.exceptionMessage();
1220 expectIpfwdRuleExists(testFromIf, testToIf);
1221
1222 status = mNetd->ipfwdRemoveInterfaceForward(testFromIf, testToIf);
1223 EXPECT_TRUE(status.isOk()) << status.exceptionMessage();
1224 expectIpfwdRuleNotExists(testFromIf, testToIf);
Bernie Innocenti1bcc25a2018-08-10 17:15:00 +09001225}