blob: ae743a988e52124c4cd515bdfb28819a80e71036 [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) {
158 std::vector<std::string> lines = listIptablesRule(binary, "bw_costly_shared");
159
160 // Output looks like this:
161 //
162 // Chain bw_costly_shared (0 references)
163 // target prot opt source destination
164 // bw_penalty_box all -- 0.0.0.0/0 0.0.0.0/0
165 // bw_happy_box all -- 0.0.0.0/0 0.0.0.0/0
166 // RETURN all -- 0.0.0.0/0 0.0.0.0/0
167 EXPECT_EQ(5U, lines.size());
168 if (lines.size() != 5) return -1;
169
170 EXPECT_TRUE(android::base::StartsWith(lines[2], "bw_penalty_box "));
171 EXPECT_TRUE(android::base::StartsWith(lines[3], "bw_happy_box "));
172 EXPECT_TRUE(android::base::StartsWith(lines[4], "RETURN ") ||
173 android::base::StartsWith(lines[4], "REJECT "));
174
175 return android::base::StartsWith(lines[4], "REJECT");
176}
177
178bool enableDataSaver(sp<INetd>& netd, bool enable) {
179 TimedOperation op(enable ? " Enabling data saver" : "Disabling data saver");
180 bool ret;
181 netd->bandwidthEnableDataSaver(enable, &ret);
182 return ret;
183}
184
185int getDataSaverState() {
186 const int enabled4 = bandwidthDataSaverEnabled(IPTABLES_PATH);
187 const int enabled6 = bandwidthDataSaverEnabled(IP6TABLES_PATH);
188 EXPECT_EQ(enabled4, enabled6);
189 EXPECT_NE(-1, enabled4);
190 EXPECT_NE(-1, enabled6);
191 if (enabled4 != enabled6 || (enabled6 != 0 && enabled6 != 1)) {
192 return -1;
193 }
194 return enabled6;
195}
196
197TEST_F(BinderTest, TestBandwidthEnableDataSaver) {
198 const int wasEnabled = getDataSaverState();
199 ASSERT_NE(-1, wasEnabled);
200
201 if (wasEnabled) {
202 ASSERT_TRUE(enableDataSaver(mNetd, false));
203 EXPECT_EQ(0, getDataSaverState());
204 }
205
206 ASSERT_TRUE(enableDataSaver(mNetd, false));
207 EXPECT_EQ(0, getDataSaverState());
208
209 ASSERT_TRUE(enableDataSaver(mNetd, true));
210 EXPECT_EQ(1, getDataSaverState());
211
212 ASSERT_TRUE(enableDataSaver(mNetd, true));
213 EXPECT_EQ(1, getDataSaverState());
214
215 if (!wasEnabled) {
216 ASSERT_TRUE(enableDataSaver(mNetd, false));
217 EXPECT_EQ(0, getDataSaverState());
218 }
219}