blob: defe875b370e14e7e0dc59f3688f6543a860b287 [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
58class TimedOperation {
59public:
60 TimedOperation(std::string name): mStart(std::chrono::steady_clock::now()), mName(name) {}
61 virtual ~TimedOperation() {
62 using ms = std::chrono::duration<float, std::ratio<1, 1000>>;
63 fprintf(stderr, " %s: %6.1f ms\n", mName.c_str(),
64 std::chrono::duration_cast<ms>(std::chrono::steady_clock::now() - mStart).count());
65 }
66
67private:
68 std::chrono::time_point<std::chrono::steady_clock> mStart;
69 std::string mName;
70};
71
72TEST_F(BinderTest, TestIsAlive) {
73 TimedOperation t("isAlive RPC");
74 bool isAlive = false;
75 mNetd->isAlive(&isAlive);
76 ASSERT_TRUE(isAlive);
77}
78
79static int randomUid() {
80 return 100000 * arc4random_uniform(7) + 10000 + arc4random_uniform(5000);
81}
82
Lorenzo Colittidedd2712016-03-22 12:36:29 +090083static std::vector<std::string> listIptablesRule(const char *binary, const char *chainName) {
84 std::vector<std::string> lines;
85 FILE *f;
86
87 std::string command = StringPrintf("%s -n -L %s", binary, chainName);
88 if ((f = popen(command.c_str(), "r")) == nullptr) {
89 perror("popen");
90 return lines;
Lorenzo Colitti89faa342016-02-26 11:38:47 +090091 }
Lorenzo Colittidedd2712016-03-22 12:36:29 +090092
93 char *line = nullptr;
94 size_t linelen = 0;
95 while (getline(&line, &linelen, f) >= 0) {
96 lines.push_back(std::string(line, linelen));
97 free(line);
98 line = nullptr;
99 }
100
101 pclose(f);
102 return lines;
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900103}
104
Lorenzo Colittidedd2712016-03-22 12:36:29 +0900105static int iptablesRuleLineLength(const char *binary, const char *chainName) {
106 return listIptablesRule(binary, chainName).size();
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900107}
108
109
110TEST_F(BinderTest, TestFirewallReplaceUidChain) {
111 std::string chainName = StringPrintf("netd_binder_test_%u", arc4random_uniform(10000));
112 const int kNumUids = 500;
113 std::vector<int32_t> noUids(0);
114 std::vector<int32_t> uids(kNumUids);
115 for (int i = 0; i < kNumUids; i++) {
116 uids[i] = randomUid();
117 }
118
119 bool ret;
120 {
121 TimedOperation op(StringPrintf("Programming %d-UID whitelist chain", kNumUids));
122 mNetd->firewallReplaceUidChain(String16(chainName.c_str()), true, uids, &ret);
123 }
124 EXPECT_EQ(true, ret);
Lorenzo Colittidedd2712016-03-22 12:36:29 +0900125 EXPECT_EQ((int) uids.size() + 4, iptablesRuleLineLength(IPTABLES_PATH, chainName.c_str()));
126 EXPECT_EQ((int) uids.size() + 4, iptablesRuleLineLength(IP6TABLES_PATH, chainName.c_str()));
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900127 {
128 TimedOperation op("Clearing whitelist chain");
129 mNetd->firewallReplaceUidChain(String16(chainName.c_str()), false, noUids, &ret);
130 }
131 EXPECT_EQ(true, ret);
Lorenzo Colittidedd2712016-03-22 12:36:29 +0900132 EXPECT_EQ(2, iptablesRuleLineLength(IPTABLES_PATH, chainName.c_str()));
133 EXPECT_EQ(2, iptablesRuleLineLength(IP6TABLES_PATH, chainName.c_str()));
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900134
135 {
136 TimedOperation op(StringPrintf("Programming %d-UID blacklist chain", kNumUids));
137 mNetd->firewallReplaceUidChain(String16(chainName.c_str()), false, uids, &ret);
138 }
139 EXPECT_EQ(true, ret);
Lorenzo Colittidedd2712016-03-22 12:36:29 +0900140 EXPECT_EQ((int) uids.size() + 3, iptablesRuleLineLength(IPTABLES_PATH, chainName.c_str()));
141 EXPECT_EQ((int) uids.size() + 3, iptablesRuleLineLength(IP6TABLES_PATH, chainName.c_str()));
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900142
143 {
144 TimedOperation op("Clearing blacklist chain");
145 mNetd->firewallReplaceUidChain(String16(chainName.c_str()), false, noUids, &ret);
146 }
147 EXPECT_EQ(true, ret);
Lorenzo Colittidedd2712016-03-22 12:36:29 +0900148 EXPECT_EQ(2, iptablesRuleLineLength(IPTABLES_PATH, chainName.c_str()));
149 EXPECT_EQ(2, iptablesRuleLineLength(IP6TABLES_PATH, chainName.c_str()));
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900150
151 // Check that the call fails if iptables returns an error.
152 std::string veryLongStringName = "netd_binder_test_UnacceptablyLongIptablesChainName";
153 mNetd->firewallReplaceUidChain(String16(veryLongStringName.c_str()), true, noUids, &ret);
154 EXPECT_EQ(false, ret);
155}
Lorenzo Colittidedd2712016-03-22 12:36:29 +0900156
157static int bandwidthDataSaverEnabled(const char *binary) {
Lorenzo Colitti464eabe2016-03-25 13:38:19 +0900158 std::vector<std::string> lines = listIptablesRule(binary, "bw_data_saver");
Lorenzo Colittidedd2712016-03-22 12:36:29 +0900159
160 // Output looks like this:
161 //
Lorenzo Colitti464eabe2016-03-25 13:38:19 +0900162 // Chain bw_data_saver (1 references)
Lorenzo Colittidedd2712016-03-22 12:36:29 +0900163 // target prot opt source destination
Lorenzo Colittidedd2712016-03-22 12:36:29 +0900164 // RETURN all -- 0.0.0.0/0 0.0.0.0/0
Lorenzo Colitti464eabe2016-03-25 13:38:19 +0900165 EXPECT_EQ(3U, lines.size());
166 if (lines.size() != 3) return -1;
Lorenzo Colittidedd2712016-03-22 12:36:29 +0900167
Lorenzo Colitti464eabe2016-03-25 13:38:19 +0900168 EXPECT_TRUE(android::base::StartsWith(lines[2], "RETURN ") ||
169 android::base::StartsWith(lines[2], "REJECT "));
Lorenzo Colittidedd2712016-03-22 12:36:29 +0900170
Lorenzo Colitti464eabe2016-03-25 13:38:19 +0900171 return android::base::StartsWith(lines[2], "REJECT");
Lorenzo Colittidedd2712016-03-22 12:36:29 +0900172}
173
174bool enableDataSaver(sp<INetd>& netd, bool enable) {
175 TimedOperation op(enable ? " Enabling data saver" : "Disabling data saver");
176 bool ret;
177 netd->bandwidthEnableDataSaver(enable, &ret);
178 return ret;
179}
180
181int getDataSaverState() {
182 const int enabled4 = bandwidthDataSaverEnabled(IPTABLES_PATH);
183 const int enabled6 = bandwidthDataSaverEnabled(IP6TABLES_PATH);
184 EXPECT_EQ(enabled4, enabled6);
185 EXPECT_NE(-1, enabled4);
186 EXPECT_NE(-1, enabled6);
187 if (enabled4 != enabled6 || (enabled6 != 0 && enabled6 != 1)) {
188 return -1;
189 }
190 return enabled6;
191}
192
193TEST_F(BinderTest, TestBandwidthEnableDataSaver) {
194 const int wasEnabled = getDataSaverState();
195 ASSERT_NE(-1, wasEnabled);
196
197 if (wasEnabled) {
198 ASSERT_TRUE(enableDataSaver(mNetd, false));
199 EXPECT_EQ(0, getDataSaverState());
200 }
201
202 ASSERT_TRUE(enableDataSaver(mNetd, false));
203 EXPECT_EQ(0, getDataSaverState());
204
205 ASSERT_TRUE(enableDataSaver(mNetd, true));
206 EXPECT_EQ(1, getDataSaverState());
207
208 ASSERT_TRUE(enableDataSaver(mNetd, true));
209 EXPECT_EQ(1, getDataSaverState());
210
211 if (!wasEnabled) {
212 ASSERT_TRUE(enableDataSaver(mNetd, false));
213 EXPECT_EQ(0, getDataSaverState());
214 }
215}