blob: bc9cf8833d1a649b7d8c0b3288bfea21a8533e68 [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
Lorenzo Colitti89faa342016-02-26 11:38:47 +090019#include <cstdint>
Lorenzo Colittidedd2712016-03-22 12:36:29 +090020#include <cstdio>
21#include <cstdlib>
Lorenzo Colitti89faa342016-02-26 11:38:47 +090022#include <vector>
23
24#include <android-base/stringprintf.h>
Lorenzo Colittidedd2712016-03-22 12:36:29 +090025#include <android-base/strings.h>
Lorenzo Colitti89faa342016-02-26 11:38:47 +090026#include <gtest/gtest.h>
27#include <logwrap/logwrap.h>
28
29#include "NetdConstants.h"
30#include "android/net/INetd.h"
31#include "binder/IServiceManager.h"
32
33using namespace android;
34using namespace android::base;
35using namespace android::binder;
36using android::net::INetd;
37
38class BinderTest : public ::testing::Test {
39
40public:
41 BinderTest() {
42 sp<IServiceManager> sm = defaultServiceManager();
43 sp<IBinder> binder = sm->getService(String16("netd"));
44 if (binder != nullptr) {
45 mNetd = interface_cast<INetd>(binder);
46 }
47 }
48
49 void SetUp() {
50 ASSERT_NE(nullptr, mNetd.get());
51 }
52
53protected:
54 sp<INetd> mNetd;
55};
56
57
Lorenzo Colitti699aa992016-04-15 10:22:37 +090058class TimedOperation : public Stopwatch {
Lorenzo Colitti89faa342016-02-26 11:38:47 +090059public:
Lorenzo Colitti699aa992016-04-15 10:22:37 +090060 TimedOperation(std::string name): mName(name) {}
Lorenzo Colitti89faa342016-02-26 11:38:47 +090061 virtual ~TimedOperation() {
Lorenzo Colitti699aa992016-04-15 10:22:37 +090062 fprintf(stderr, " %s: %6.1f ms\n", mName.c_str(), timeTaken());
Lorenzo Colitti89faa342016-02-26 11:38:47 +090063 }
64
65private:
Lorenzo Colitti89faa342016-02-26 11:38:47 +090066 std::string mName;
67};
68
69TEST_F(BinderTest, TestIsAlive) {
70 TimedOperation t("isAlive RPC");
71 bool isAlive = false;
72 mNetd->isAlive(&isAlive);
73 ASSERT_TRUE(isAlive);
74}
75
76static int randomUid() {
77 return 100000 * arc4random_uniform(7) + 10000 + arc4random_uniform(5000);
78}
79
Lorenzo Colittidedd2712016-03-22 12:36:29 +090080static std::vector<std::string> listIptablesRule(const char *binary, const char *chainName) {
81 std::vector<std::string> lines;
82 FILE *f;
83
84 std::string command = StringPrintf("%s -n -L %s", binary, chainName);
85 if ((f = popen(command.c_str(), "r")) == nullptr) {
86 perror("popen");
87 return lines;
Lorenzo Colitti89faa342016-02-26 11:38:47 +090088 }
Lorenzo Colittidedd2712016-03-22 12:36:29 +090089
90 char *line = nullptr;
91 size_t linelen = 0;
92 while (getline(&line, &linelen, f) >= 0) {
93 lines.push_back(std::string(line, linelen));
94 free(line);
95 line = nullptr;
96 }
97
98 pclose(f);
99 return lines;
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900100}
101
Lorenzo Colittidedd2712016-03-22 12:36:29 +0900102static int iptablesRuleLineLength(const char *binary, const char *chainName) {
103 return listIptablesRule(binary, chainName).size();
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900104}
105
106
107TEST_F(BinderTest, TestFirewallReplaceUidChain) {
108 std::string chainName = StringPrintf("netd_binder_test_%u", arc4random_uniform(10000));
109 const int kNumUids = 500;
110 std::vector<int32_t> noUids(0);
111 std::vector<int32_t> uids(kNumUids);
112 for (int i = 0; i < kNumUids; i++) {
113 uids[i] = randomUid();
114 }
115
116 bool ret;
117 {
118 TimedOperation op(StringPrintf("Programming %d-UID whitelist chain", kNumUids));
119 mNetd->firewallReplaceUidChain(String16(chainName.c_str()), true, uids, &ret);
120 }
121 EXPECT_EQ(true, ret);
Lorenzo Colittidedd2712016-03-22 12:36:29 +0900122 EXPECT_EQ((int) uids.size() + 4, iptablesRuleLineLength(IPTABLES_PATH, chainName.c_str()));
123 EXPECT_EQ((int) uids.size() + 4, iptablesRuleLineLength(IP6TABLES_PATH, chainName.c_str()));
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900124 {
125 TimedOperation op("Clearing whitelist chain");
126 mNetd->firewallReplaceUidChain(String16(chainName.c_str()), false, noUids, &ret);
127 }
128 EXPECT_EQ(true, ret);
Lorenzo Colittidedd2712016-03-22 12:36:29 +0900129 EXPECT_EQ(2, iptablesRuleLineLength(IPTABLES_PATH, chainName.c_str()));
130 EXPECT_EQ(2, iptablesRuleLineLength(IP6TABLES_PATH, chainName.c_str()));
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900131
132 {
133 TimedOperation op(StringPrintf("Programming %d-UID blacklist chain", kNumUids));
134 mNetd->firewallReplaceUidChain(String16(chainName.c_str()), false, uids, &ret);
135 }
136 EXPECT_EQ(true, ret);
Lorenzo Colittidedd2712016-03-22 12:36:29 +0900137 EXPECT_EQ((int) uids.size() + 3, iptablesRuleLineLength(IPTABLES_PATH, chainName.c_str()));
138 EXPECT_EQ((int) uids.size() + 3, iptablesRuleLineLength(IP6TABLES_PATH, chainName.c_str()));
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900139
140 {
141 TimedOperation op("Clearing blacklist chain");
142 mNetd->firewallReplaceUidChain(String16(chainName.c_str()), false, noUids, &ret);
143 }
144 EXPECT_EQ(true, ret);
Lorenzo Colittidedd2712016-03-22 12:36:29 +0900145 EXPECT_EQ(2, iptablesRuleLineLength(IPTABLES_PATH, chainName.c_str()));
146 EXPECT_EQ(2, iptablesRuleLineLength(IP6TABLES_PATH, chainName.c_str()));
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900147
148 // Check that the call fails if iptables returns an error.
149 std::string veryLongStringName = "netd_binder_test_UnacceptablyLongIptablesChainName";
150 mNetd->firewallReplaceUidChain(String16(veryLongStringName.c_str()), true, noUids, &ret);
151 EXPECT_EQ(false, ret);
152}
Lorenzo Colittidedd2712016-03-22 12:36:29 +0900153
154static int bandwidthDataSaverEnabled(const char *binary) {
Lorenzo Colitti464eabe2016-03-25 13:38:19 +0900155 std::vector<std::string> lines = listIptablesRule(binary, "bw_data_saver");
Lorenzo Colittidedd2712016-03-22 12:36:29 +0900156
157 // Output looks like this:
158 //
Lorenzo Colitti464eabe2016-03-25 13:38:19 +0900159 // Chain bw_data_saver (1 references)
Lorenzo Colittidedd2712016-03-22 12:36:29 +0900160 // target prot opt source destination
Lorenzo Colittidedd2712016-03-22 12:36:29 +0900161 // RETURN all -- 0.0.0.0/0 0.0.0.0/0
Lorenzo Colitti464eabe2016-03-25 13:38:19 +0900162 EXPECT_EQ(3U, lines.size());
163 if (lines.size() != 3) return -1;
Lorenzo Colittidedd2712016-03-22 12:36:29 +0900164
Lorenzo Colitti464eabe2016-03-25 13:38:19 +0900165 EXPECT_TRUE(android::base::StartsWith(lines[2], "RETURN ") ||
166 android::base::StartsWith(lines[2], "REJECT "));
Lorenzo Colittidedd2712016-03-22 12:36:29 +0900167
Lorenzo Colitti464eabe2016-03-25 13:38:19 +0900168 return android::base::StartsWith(lines[2], "REJECT");
Lorenzo Colittidedd2712016-03-22 12:36:29 +0900169}
170
171bool enableDataSaver(sp<INetd>& netd, bool enable) {
172 TimedOperation op(enable ? " Enabling data saver" : "Disabling data saver");
173 bool ret;
174 netd->bandwidthEnableDataSaver(enable, &ret);
175 return ret;
176}
177
178int getDataSaverState() {
179 const int enabled4 = bandwidthDataSaverEnabled(IPTABLES_PATH);
180 const int enabled6 = bandwidthDataSaverEnabled(IP6TABLES_PATH);
181 EXPECT_EQ(enabled4, enabled6);
182 EXPECT_NE(-1, enabled4);
183 EXPECT_NE(-1, enabled6);
184 if (enabled4 != enabled6 || (enabled6 != 0 && enabled6 != 1)) {
185 return -1;
186 }
187 return enabled6;
188}
189
190TEST_F(BinderTest, TestBandwidthEnableDataSaver) {
191 const int wasEnabled = getDataSaverState();
192 ASSERT_NE(-1, wasEnabled);
193
194 if (wasEnabled) {
195 ASSERT_TRUE(enableDataSaver(mNetd, false));
196 EXPECT_EQ(0, getDataSaverState());
197 }
198
199 ASSERT_TRUE(enableDataSaver(mNetd, false));
200 EXPECT_EQ(0, getDataSaverState());
201
202 ASSERT_TRUE(enableDataSaver(mNetd, true));
203 EXPECT_EQ(1, getDataSaverState());
204
205 ASSERT_TRUE(enableDataSaver(mNetd, true));
206 EXPECT_EQ(1, getDataSaverState());
207
208 if (!wasEnabled) {
209 ASSERT_TRUE(enableDataSaver(mNetd, false));
210 EXPECT_EQ(0, getDataSaverState());
211 }
212}