blob: becfe49be63f18b80b8491ec1a2f4fef9c9ce015 [file] [log] [blame]
Lorenzo Colitti86a47982016-03-18 17:52:25 +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 * BandwidthControllerTest.cpp - unit tests for BandwidthController.cpp
17 */
18
19#include <string>
20#include <vector>
Lorenzo Colitti86a47982016-03-18 17:52:25 +090021
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +090022#include <inttypes.h>
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +090023#include <fcntl.h>
24#include <unistd.h>
25#include <sys/types.h>
26#include <sys/socket.h>
27
Lorenzo Colitti86a47982016-03-18 17:52:25 +090028#include <gtest/gtest.h>
29
Lorenzo Colitti13debb82016-03-27 17:46:30 +090030#include <android-base/strings.h>
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +090031#include <android-base/stringprintf.h>
Lorenzo Colitti13debb82016-03-27 17:46:30 +090032
Joel Scherpelz01cc5492017-06-16 10:45:14 +090033#include <netdutils/MockSyscalls.h>
Lorenzo Colitti86a47982016-03-18 17:52:25 +090034#include "BandwidthController.h"
Lorenzo Colitti0f150552016-03-28 02:30:27 +090035#include "IptablesBaseTest.h"
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +090036#include "tun_interface.h"
37
Joel Scherpelz01cc5492017-06-16 10:45:14 +090038using ::testing::ByMove;
39using ::testing::Invoke;
40using ::testing::Return;
41using ::testing::StrictMock;
42using ::testing::Test;
43using ::testing::_;
44
Lorenzo Colitti48f83002017-07-06 15:06:04 +090045using android::base::Join;
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +090046using android::base::StringPrintf;
47using android::net::TunInterface;
Joel Scherpelz01cc5492017-06-16 10:45:14 +090048using android::netdutils::status::ok;
49using android::netdutils::UniqueFile;
Lorenzo Colitti86a47982016-03-18 17:52:25 +090050
Lorenzo Colitti0f150552016-03-28 02:30:27 +090051class BandwidthControllerTest : public IptablesBaseTest {
Joel Scherpelz01cc5492017-06-16 10:45:14 +090052protected:
Lorenzo Colitti86a47982016-03-18 17:52:25 +090053 BandwidthControllerTest() {
54 BandwidthController::execFunction = fake_android_fork_exec;
55 BandwidthController::popenFunction = fake_popen;
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +090056 BandwidthController::iptablesRestoreFunction = fakeExecIptablesRestoreWithOutput;
Lorenzo Colitti86a47982016-03-18 17:52:25 +090057 }
58 BandwidthController mBw;
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +090059 TunInterface mTun;
60
61 void SetUp() {
62 ASSERT_EQ(0, mTun.init());
63 }
64
65 void TearDown() {
66 mTun.destroy();
67 }
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +090068
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +090069 void addIptablesRestoreOutput(std::string contents) {
70 sIptablesRestoreOutput.push_back(contents);
71 }
72
Lorenzo Colittice6748a2017-02-02 01:34:33 +090073 void addIptablesRestoreOutput(std::string contents1, std::string contents2) {
74 sIptablesRestoreOutput.push_back(contents1);
75 sIptablesRestoreOutput.push_back(contents2);
76 }
77
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +090078 void clearIptablesRestoreOutput() {
79 sIptablesRestoreOutput.clear();
80 }
81
82 void expectSetupCommands(const std::string& expectedClean, std::string expectedAccounting) {
83 std::string expectedList =
84 "*filter\n"
85 "-S\n"
86 "COMMIT\n";
87
88 std::string expectedFlush =
89 "*filter\n"
90 ":bw_INPUT -\n"
91 ":bw_OUTPUT -\n"
92 ":bw_FORWARD -\n"
93 ":bw_happy_box -\n"
94 ":bw_penalty_box -\n"
95 ":bw_data_saver -\n"
96 ":bw_costly_shared -\n"
97 "COMMIT\n"
98 "*raw\n"
99 ":bw_raw_PREROUTING -\n"
100 "COMMIT\n"
101 "*mangle\n"
102 ":bw_mangle_POSTROUTING -\n"
103 "COMMIT\n";
104
105 ExpectedIptablesCommands expected = {{ V4, expectedList }};
106 if (expectedClean.size()) {
107 expected.push_back({ V4V6, expectedClean });
108 }
109 expected.push_back({ V4V6, expectedFlush });
110 if (expectedAccounting.size()) {
111 expected.push_back({ V4V6, expectedAccounting });
112 }
113
114 expectIptablesRestoreCommands(expected);
115 }
Lorenzo Colittie8b56e42017-04-26 15:16:03 +0900116
117 using IptOp = BandwidthController::IptOp;
118
119 int runIptablesAlertCmd(IptOp a, const char *b, int64_t c) {
120 return mBw.runIptablesAlertCmd(a, b, c);
121 }
122
123 int runIptablesAlertFwdCmd(IptOp a, const char *b, int64_t c) {
124 return mBw.runIptablesAlertFwdCmd(a, b, c);
125 }
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900126
Lorenzo Colitti38078222017-07-06 17:27:23 +0900127 int setCostlyAlert(const std::string a, int64_t b, int64_t *c) {
128 return mBw.setCostlyAlert(a, b, c);
129 }
130
131 int removeCostlyAlert(const std::string a, int64_t *b) {
132 return mBw.removeCostlyAlert(a, b);
133 }
134
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900135 void expectUpdateQuota(uint64_t quota) {
136 uintptr_t dummy;
137 FILE* dummyFile = reinterpret_cast<FILE*>(&dummy);
138
139 EXPECT_CALL(mSyscalls, fopen(_, _)).WillOnce(Return(ByMove(UniqueFile(dummyFile))));
140 EXPECT_CALL(mSyscalls, vfprintf(dummyFile, _, _))
141 .WillOnce(Invoke([quota](FILE*, const std::string&, va_list ap) {
142 EXPECT_EQ(quota, va_arg(ap, uint64_t));
143 return 0;
144 }));
145 EXPECT_CALL(mSyscalls, fclose(dummyFile)).WillOnce(Return(ok));
146 }
147
148 StrictMock<android::netdutils::ScopedMockSyscalls> mSyscalls;
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900149};
150
Lorenzo Colittia0dc8a52016-03-26 22:42:07 +0900151TEST_F(BandwidthControllerTest, TestSetupIptablesHooks) {
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900152 // Pretend some bw_costly_shared_<iface> rules already exist...
153 addIptablesRestoreOutput(
154 "-P OUTPUT ACCEPT\n"
155 "-N bw_costly_rmnet_data0\n"
156 "-N bw_costly_shared\n"
157 "-N unrelated\n"
158 "-N bw_costly_rmnet_data7\n");
159
160 // ... and expect that they be flushed and deleted.
161 std::string expectedCleanCmds =
Lorenzo Colitti13debb82016-03-27 17:46:30 +0900162 "*filter\n"
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900163 ":bw_costly_rmnet_data0 -\n"
164 "-X bw_costly_rmnet_data0\n"
165 ":bw_costly_rmnet_data7 -\n"
166 "-X bw_costly_rmnet_data7\n"
167 "COMMIT\n";
168
169 mBw.setupIptablesHooks();
170 expectSetupCommands(expectedCleanCmds, "");
Lorenzo Colittia0dc8a52016-03-26 22:42:07 +0900171}
172
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900173TEST_F(BandwidthControllerTest, TestEnableBandwidthControl) {
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900174 // Pretend no bw_costly_shared_<iface> rules already exist...
175 addIptablesRestoreOutput(
176 "-P OUTPUT ACCEPT\n"
177 "-N bw_costly_shared\n"
178 "-N unrelated\n");
179
180 // ... so none are flushed or deleted.
181 std::string expectedClean = "";
182
183 std::string expectedAccounting =
Lorenzo Colitti13debb82016-03-27 17:46:30 +0900184 "*filter\n"
185 "-A bw_INPUT -m owner --socket-exists\n"
186 "-A bw_OUTPUT -m owner --socket-exists\n"
187 "-A bw_costly_shared --jump bw_penalty_box\n"
188 "-A bw_penalty_box --jump bw_happy_box\n"
189 "-A bw_happy_box --jump bw_data_saver\n"
190 "-A bw_data_saver -j RETURN\n"
191 "-I bw_happy_box -m owner --uid-owner 0-9999 --jump RETURN\n"
192 "COMMIT\n"
193 "*raw\n"
194 "-A bw_raw_PREROUTING -m owner --socket-exists\n"
195 "COMMIT\n"
196 "*mangle\n"
197 "-A bw_mangle_POSTROUTING -m owner --socket-exists\n"
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900198 "COMMIT\n";
Lorenzo Colitti13debb82016-03-27 17:46:30 +0900199
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900200 mBw.enableBandwidthControl(false);
201 expectSetupCommands(expectedClean, expectedAccounting);
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900202}
203
Lorenzo Colittia0dc8a52016-03-26 22:42:07 +0900204TEST_F(BandwidthControllerTest, TestDisableBandwidthControl) {
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900205 // Pretend some bw_costly_shared_<iface> rules already exist...
206 addIptablesRestoreOutput(
207 "-P OUTPUT ACCEPT\n"
208 "-N bw_costly_rmnet_data0\n"
209 "-N bw_costly_shared\n"
210 "-N unrelated\n"
211 "-N bw_costly_rmnet_data7\n");
212
213 // ... and expect that they be flushed.
214 std::string expectedCleanCmds =
Lorenzo Colitti13debb82016-03-27 17:46:30 +0900215 "*filter\n"
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900216 ":bw_costly_rmnet_data0 -\n"
217 ":bw_costly_rmnet_data7 -\n"
218 "COMMIT\n";
219
220 mBw.disableBandwidthControl();
221 expectSetupCommands(expectedCleanCmds, "");
Lorenzo Colittia0dc8a52016-03-26 22:42:07 +0900222}
223
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900224TEST_F(BandwidthControllerTest, TestEnableDataSaver) {
225 mBw.enableDataSaver(true);
226 std::vector<std::string> expected = {
Lorenzo Colitti911bc4c2017-04-28 14:34:01 +0900227 "*filter\n"
228 "-R bw_data_saver 1 --jump REJECT\n"
229 "COMMIT\n"
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900230 };
Lorenzo Colitti911bc4c2017-04-28 14:34:01 +0900231 expectIptablesRestoreCommands(expected);
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900232
233 mBw.enableDataSaver(false);
234 expected = {
Lorenzo Colitti911bc4c2017-04-28 14:34:01 +0900235 "*filter\n"
236 "-R bw_data_saver 1 --jump RETURN\n"
237 "COMMIT\n"
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900238 };
Lorenzo Colitti911bc4c2017-04-28 14:34:01 +0900239 expectIptablesRestoreCommands(expected);
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900240}
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900241
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900242std::string kIPv4TetherCounters = Join(std::vector<std::string> {
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900243 "Chain natctrl_tether_counters (4 references)",
244 " pkts bytes target prot opt in out source destination",
245 " 26 2373 RETURN all -- wlan0 rmnet0 0.0.0.0/0 0.0.0.0/0",
246 " 27 2002 RETURN all -- rmnet0 wlan0 0.0.0.0/0 0.0.0.0/0",
247 " 1040 107471 RETURN all -- bt-pan rmnet0 0.0.0.0/0 0.0.0.0/0",
248 " 1450 1708806 RETURN all -- rmnet0 bt-pan 0.0.0.0/0 0.0.0.0/0",
249}, '\n');
250
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900251std::string kIPv6TetherCounters = Join(std::vector<std::string> {
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900252 "Chain natctrl_tether_counters (2 references)",
253 " pkts bytes target prot opt in out source destination",
254 " 10000 10000000 RETURN all wlan0 rmnet0 ::/0 ::/0",
255 " 20000 20000000 RETURN all rmnet0 wlan0 ::/0 ::/0",
256}, '\n');
257
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900258std::string readSocketClientResponse(int fd) {
259 char buf[32768];
260 ssize_t bytesRead = read(fd, buf, sizeof(buf));
261 if (bytesRead < 0) {
262 return "";
263 }
264 for (int i = 0; i < bytesRead; i++) {
265 if (buf[i] == '\0') buf[i] = '\n';
266 }
267 return std::string(buf, bytesRead);
268}
269
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900270void expectNoSocketClientResponse(int fd) {
271 char buf[64];
272 EXPECT_EQ(-1, read(fd, buf, sizeof(buf)));
273}
274
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900275TEST_F(BandwidthControllerTest, TestGetTetherStats) {
276 int socketPair[2];
277 ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM, 0, socketPair));
278 ASSERT_EQ(0, fcntl(socketPair[0], F_SETFL, O_NONBLOCK | fcntl(socketPair[0], F_GETFL)));
279 ASSERT_EQ(0, fcntl(socketPair[1], F_SETFL, O_NONBLOCK | fcntl(socketPair[1], F_GETFL)));
280 SocketClient cli(socketPair[0], false);
281
282 std::string err;
283 BandwidthController::TetherStats filter;
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900284
285 // If no filter is specified, both IPv4 and IPv6 counters must have at least one interface pair.
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900286 addIptablesRestoreOutput(kIPv4TetherCounters);
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900287 ASSERT_EQ(-1, mBw.getTetherStats(&cli, filter, err));
288 expectNoSocketClientResponse(socketPair[1]);
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900289 clearIptablesRestoreOutput();
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900290
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900291 addIptablesRestoreOutput(kIPv6TetherCounters);
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900292 ASSERT_EQ(-1, mBw.getTetherStats(&cli, filter, err));
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900293 clearIptablesRestoreOutput();
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900294
295 // IPv4 and IPv6 counters are properly added together.
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900296 addIptablesRestoreOutput(kIPv4TetherCounters, kIPv6TetherCounters);
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900297 filter = BandwidthController::TetherStats();
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900298 std::string expected =
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900299 "114 wlan0 rmnet0 10002373 10026 20002002 20027\n"
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900300 "114 bt-pan rmnet0 107471 1040 1708806 1450\n"
301 "200 Tethering stats list completed\n";
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900302 ASSERT_EQ(0, mBw.getTetherStats(&cli, filter, err));
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900303 ASSERT_EQ(expected, readSocketClientResponse(socketPair[1]));
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900304 expectNoSocketClientResponse(socketPair[1]);
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900305 clearIptablesRestoreOutput();
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900306
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900307 // Test filtering.
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900308 addIptablesRestoreOutput(kIPv4TetherCounters, kIPv6TetherCounters);
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900309 filter = BandwidthController::TetherStats("bt-pan", "rmnet0", -1, -1, -1, -1);
310 expected = "221 bt-pan rmnet0 107471 1040 1708806 1450\n";
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900311 ASSERT_EQ(0, mBw.getTetherStats(&cli, filter, err));
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900312 ASSERT_EQ(expected, readSocketClientResponse(socketPair[1]));
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900313 expectNoSocketClientResponse(socketPair[1]);
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900314 clearIptablesRestoreOutput();
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900315
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900316 addIptablesRestoreOutput(kIPv4TetherCounters, kIPv6TetherCounters);
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900317 filter = BandwidthController::TetherStats("wlan0", "rmnet0", -1, -1, -1, -1);
318 expected = "221 wlan0 rmnet0 10002373 10026 20002002 20027\n";
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900319 ASSERT_EQ(0, mBw.getTetherStats(&cli, filter, err));
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900320 ASSERT_EQ(expected, readSocketClientResponse(socketPair[1]));
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900321 clearIptablesRestoreOutput();
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900322
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900323 // Select nonexistent interfaces.
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900324 addIptablesRestoreOutput(kIPv4TetherCounters, kIPv6TetherCounters);
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900325 filter = BandwidthController::TetherStats("rmnet0", "foo0", -1, -1, -1, -1);
326 expected = "200 Tethering stats list completed\n";
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900327 ASSERT_EQ(0, mBw.getTetherStats(&cli, filter, err));
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900328 ASSERT_EQ(expected, readSocketClientResponse(socketPair[1]));
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900329 clearIptablesRestoreOutput();
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900330
331 // No stats with a filter: no error.
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900332 addIptablesRestoreOutput("", "");
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900333 ASSERT_EQ(0, mBw.getTetherStats(&cli, filter, err));
334 ASSERT_EQ("200 Tethering stats list completed\n", readSocketClientResponse(socketPair[1]));
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900335 clearIptablesRestoreOutput();
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900336
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900337 addIptablesRestoreOutput("foo", "foo");
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900338 ASSERT_EQ(0, mBw.getTetherStats(&cli, filter, err));
339 ASSERT_EQ("200 Tethering stats list completed\n", readSocketClientResponse(socketPair[1]));
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900340 clearIptablesRestoreOutput();
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900341
342 // No stats and empty filter: error.
343 filter = BandwidthController::TetherStats();
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900344 addIptablesRestoreOutput("", kIPv6TetherCounters);
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900345 ASSERT_EQ(-1, mBw.getTetherStats(&cli, filter, err));
346 expectNoSocketClientResponse(socketPair[1]);
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900347 clearIptablesRestoreOutput();
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900348
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900349 addIptablesRestoreOutput(kIPv4TetherCounters, "");
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900350 ASSERT_EQ(-1, mBw.getTetherStats(&cli, filter, err));
351 expectNoSocketClientResponse(socketPair[1]);
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900352 clearIptablesRestoreOutput();
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900353
354 // Include only one pair of interfaces and things are fine.
355 std::vector<std::string> counterLines = android::base::Split(kIPv4TetherCounters, "\n");
356 std::vector<std::string> brokenCounterLines = counterLines;
357 counterLines.resize(4);
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900358 std::string counters = Join(counterLines, "\n") + "\n";
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900359 addIptablesRestoreOutput(counters, counters);
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900360 expected =
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900361 "114 wlan0 rmnet0 4746 52 4004 54\n"
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900362 "200 Tethering stats list completed\n";
363 ASSERT_EQ(0, mBw.getTetherStats(&cli, filter, err));
364 ASSERT_EQ(expected, readSocketClientResponse(socketPair[1]));
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900365 clearIptablesRestoreOutput();
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900366
367 // But if interfaces aren't paired, it's always an error.
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900368 err = "";
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900369 counterLines.resize(3);
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900370 counters = Join(counterLines, "\n") + "\n";
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900371 addIptablesRestoreOutput(counters, counters);
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900372 ASSERT_EQ(-1, mBw.getTetherStats(&cli, filter, err));
373 expectNoSocketClientResponse(socketPair[1]);
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900374 clearIptablesRestoreOutput();
375
376 // Token unit test of the fact that we return the stats in the error message which the caller
377 // ignores.
378 std::string expectedError = counters;
379 EXPECT_EQ(expectedError, err);
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900380
381 // popen() failing is always an error.
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900382 addIptablesRestoreOutput(kIPv4TetherCounters);
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900383 ASSERT_EQ(-1, mBw.getTetherStats(&cli, filter, err));
384 expectNoSocketClientResponse(socketPair[1]);
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900385 clearIptablesRestoreOutput();
386 addIptablesRestoreOutput(kIPv6TetherCounters);
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900387 ASSERT_EQ(-1, mBw.getTetherStats(&cli, filter, err));
388 expectNoSocketClientResponse(socketPair[1]);
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900389 clearIptablesRestoreOutput();
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900390}
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +0900391
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900392const std::vector<std::string> makeInterfaceQuotaCommands(const std::string& iface, int ruleIndex,
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +0900393 int64_t quota) {
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900394 const std::string chain = "bw_costly_" + iface;
395 const char* c_chain = chain.c_str();
396 const char* c_iface = iface.c_str();
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +0900397 std::vector<std::string> cmds = {
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900398 "*filter",
399 StringPrintf(":%s -", c_chain),
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900400 StringPrintf("-A %s -j bw_penalty_box", c_chain),
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900401 StringPrintf("-I bw_INPUT %d -i %s --jump %s", ruleIndex, c_iface, c_chain),
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900402 StringPrintf("-I bw_OUTPUT %d -o %s --jump %s", ruleIndex, c_iface, c_chain),
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900403 StringPrintf("-A bw_FORWARD -o %s --jump %s", c_iface, c_chain),
404 StringPrintf("-A %s -m quota2 ! --quota %" PRIu64 " --name %s --jump REJECT", c_chain,
405 quota, c_iface),
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900406 "COMMIT\n",
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +0900407 };
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900408 return {Join(cmds, "\n")};
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +0900409}
410
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900411const std::vector<std::string> removeInterfaceQuotaCommands(const std::string& iface) {
412 const std::string chain = "bw_costly_" + iface;
413 const char* c_chain = chain.c_str();
414 const char* c_iface = iface.c_str();
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +0900415 std::vector<std::string> cmds = {
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900416 "*filter",
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900417 StringPrintf("-D bw_INPUT -i %s --jump %s", c_iface, c_chain),
418 StringPrintf("-D bw_OUTPUT -o %s --jump %s", c_iface, c_chain),
419 StringPrintf("-D bw_FORWARD -o %s --jump %s", c_iface, c_chain),
420 StringPrintf("-F %s", c_chain),
421 StringPrintf("-X %s", c_chain),
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900422 "COMMIT\n",
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +0900423 };
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900424 return {Join(cmds, "\n")};
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +0900425}
426
427TEST_F(BandwidthControllerTest, TestSetInterfaceQuota) {
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900428 constexpr uint64_t kOldQuota = 123456;
429 const std::string iface = mTun.name();
430 std::vector<std::string> expected = makeInterfaceQuotaCommands(iface, 1, kOldQuota);
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +0900431
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900432 EXPECT_EQ(0, mBw.setInterfaceQuota(iface, kOldQuota));
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900433 expectIptablesRestoreCommands(expected);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900434
435 constexpr uint64_t kNewQuota = kOldQuota + 1;
436 expected = {};
437 expectUpdateQuota(kNewQuota);
438 EXPECT_EQ(0, mBw.setInterfaceQuota(iface, kNewQuota));
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900439 expectIptablesRestoreCommands(expected);
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +0900440
441 expected = removeInterfaceQuotaCommands(iface);
442 EXPECT_EQ(0, mBw.removeInterfaceQuota(iface));
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900443 expectIptablesRestoreCommands(expected);
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +0900444}
Lorenzo Colittie8b56e42017-04-26 15:16:03 +0900445
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900446const std::vector<std::string> makeInterfaceSharedQuotaCommands(const std::string& iface,
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900447 int ruleIndex, int64_t quota,
448 bool insertQuota) {
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900449 const std::string chain = "bw_costly_shared";
450 const char* c_chain = chain.c_str();
451 const char* c_iface = iface.c_str();
452 std::vector<std::string> cmds = {
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900453 "*filter",
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900454 StringPrintf("-I bw_INPUT %d -i %s --jump %s", ruleIndex, c_iface, c_chain),
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900455 StringPrintf("-I bw_OUTPUT %d -o %s --jump %s", ruleIndex, c_iface, c_chain),
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900456 StringPrintf("-A bw_FORWARD -o %s --jump %s", c_iface, c_chain),
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900457 };
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900458 if (insertQuota) {
459 cmds.push_back(StringPrintf(
460 "-I %s -m quota2 ! --quota %" PRIu64 " --name shared --jump REJECT", c_chain, quota));
461 }
462 cmds.push_back("COMMIT\n");
463 return {Join(cmds, "\n")};
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900464}
465
466const std::vector<std::string> removeInterfaceSharedQuotaCommands(const std::string& iface,
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900467 int64_t quota, bool deleteQuota) {
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900468 const std::string chain = "bw_costly_shared";
469 const char* c_chain = chain.c_str();
470 const char* c_iface = iface.c_str();
471 std::vector<std::string> cmds = {
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900472 "*filter",
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900473 StringPrintf("-D bw_INPUT -i %s --jump %s", c_iface, c_chain),
474 StringPrintf("-D bw_OUTPUT -o %s --jump %s", c_iface, c_chain),
475 StringPrintf("-D bw_FORWARD -o %s --jump %s", c_iface, c_chain),
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900476 };
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900477 if (deleteQuota) {
478 cmds.push_back(StringPrintf(
479 "-D %s -m quota2 ! --quota %" PRIu64 " --name shared --jump REJECT", c_chain, quota));
480 }
481 cmds.push_back("COMMIT\n");
482 return {Join(cmds, "\n")};
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900483}
484
485TEST_F(BandwidthControllerTest, TestSetInterfaceSharedQuotaDuplicate) {
486 constexpr uint64_t kQuota = 123456;
487 const std::string iface = mTun.name();
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900488 std::vector<std::string> expected = makeInterfaceSharedQuotaCommands(iface, 1, 123456, true);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900489 EXPECT_EQ(0, mBw.setInterfaceSharedQuota(iface, kQuota));
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900490 expectIptablesRestoreCommands(expected);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900491
492 expected = {};
493 EXPECT_EQ(0, mBw.setInterfaceSharedQuota(iface, kQuota));
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900494 expectIptablesRestoreCommands(expected);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900495
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900496 expected = removeInterfaceSharedQuotaCommands(iface, kQuota, true);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900497 EXPECT_EQ(0, mBw.removeInterfaceSharedQuota(iface));
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900498 expectIptablesRestoreCommands(expected);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900499}
500
501TEST_F(BandwidthControllerTest, TestSetInterfaceSharedQuotaUpdate) {
502 constexpr uint64_t kOldQuota = 123456;
503 const std::string iface = mTun.name();
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900504 std::vector<std::string> expected = makeInterfaceSharedQuotaCommands(iface, 1, kOldQuota, true);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900505 EXPECT_EQ(0, mBw.setInterfaceSharedQuota(iface, kOldQuota));
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900506 expectIptablesRestoreCommands(expected);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900507
508 constexpr uint64_t kNewQuota = kOldQuota + 1;
509 expected = {};
510 expectUpdateQuota(kNewQuota);
511 EXPECT_EQ(0, mBw.setInterfaceSharedQuota(iface, kNewQuota));
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900512 expectIptablesRestoreCommands(expected);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900513
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900514 expected = removeInterfaceSharedQuotaCommands(iface, kNewQuota, true);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900515 EXPECT_EQ(0, mBw.removeInterfaceSharedQuota(iface));
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900516 expectIptablesRestoreCommands(expected);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900517}
518
519TEST_F(BandwidthControllerTest, TestSetInterfaceSharedQuotaTwoInterfaces) {
520 constexpr uint64_t kQuota = 123456;
521 const std::vector<std::string> ifaces{
522 {"a" + mTun.name()},
523 {"b" + mTun.name()},
524 };
525
526 for (const auto& iface : ifaces) {
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900527 // Quota rule is only added when the total number of
528 // interfaces transitions from 0 -> 1.
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900529 bool first = (iface == ifaces[0]);
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900530 auto expected = makeInterfaceSharedQuotaCommands(iface, 1, kQuota, first);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900531 EXPECT_EQ(0, mBw.setInterfaceSharedQuota(iface, kQuota));
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900532 expectIptablesRestoreCommands(expected);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900533 }
534
535 for (const auto& iface : ifaces) {
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900536 // Quota rule is only removed when the total number of
537 // interfaces transitions from 1 -> 0.
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900538 bool last = (iface == ifaces[1]);
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900539 auto expected = removeInterfaceSharedQuotaCommands(iface, kQuota, last);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900540 EXPECT_EQ(0, mBw.removeInterfaceSharedQuota(iface));
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900541 expectIptablesRestoreCommands(expected);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900542 }
543}
544
Lorenzo Colittie8b56e42017-04-26 15:16:03 +0900545TEST_F(BandwidthControllerTest, IptablesAlertCmd) {
546 std::vector<std::string> expected = {
Lorenzo Colitti3c272702017-04-26 15:48:13 +0900547 "*filter\n"
548 "-I bw_INPUT -m quota2 ! --quota 123456 --name MyWonderfulAlert\n"
549 "-I bw_OUTPUT -m quota2 ! --quota 123456 --name MyWonderfulAlert\n"
550 "COMMIT\n"
Lorenzo Colittie8b56e42017-04-26 15:16:03 +0900551 };
552 EXPECT_EQ(0, runIptablesAlertCmd(IptOp::IptOpInsert, "MyWonderfulAlert", 123456));
Lorenzo Colitti3c272702017-04-26 15:48:13 +0900553 expectIptablesRestoreCommands(expected);
Lorenzo Colittie8b56e42017-04-26 15:16:03 +0900554
555 expected = {
Lorenzo Colitti3c272702017-04-26 15:48:13 +0900556 "*filter\n"
557 "-D bw_INPUT -m quota2 ! --quota 123456 --name MyWonderfulAlert\n"
558 "-D bw_OUTPUT -m quota2 ! --quota 123456 --name MyWonderfulAlert\n"
559 "COMMIT\n"
Lorenzo Colittie8b56e42017-04-26 15:16:03 +0900560 };
561 EXPECT_EQ(0, runIptablesAlertCmd(IptOp::IptOpDelete, "MyWonderfulAlert", 123456));
Lorenzo Colitti3c272702017-04-26 15:48:13 +0900562 expectIptablesRestoreCommands(expected);
Lorenzo Colittie8b56e42017-04-26 15:16:03 +0900563}
564
565TEST_F(BandwidthControllerTest, IptablesAlertFwdCmd) {
566 std::vector<std::string> expected = {
Lorenzo Colitti3c272702017-04-26 15:48:13 +0900567 "*filter\n"
568 "-I bw_FORWARD -m quota2 ! --quota 123456 --name MyWonderfulAlert\n"
569 "COMMIT\n"
Lorenzo Colittie8b56e42017-04-26 15:16:03 +0900570 };
571 EXPECT_EQ(0, runIptablesAlertFwdCmd(IptOp::IptOpInsert, "MyWonderfulAlert", 123456));
Lorenzo Colitti3c272702017-04-26 15:48:13 +0900572 expectIptablesRestoreCommands(expected);
Lorenzo Colittie8b56e42017-04-26 15:16:03 +0900573
574 expected = {
Lorenzo Colitti3c272702017-04-26 15:48:13 +0900575 "*filter\n"
576 "-D bw_FORWARD -m quota2 ! --quota 123456 --name MyWonderfulAlert\n"
577 "COMMIT\n"
Lorenzo Colittie8b56e42017-04-26 15:16:03 +0900578 };
579 EXPECT_EQ(0, runIptablesAlertFwdCmd(IptOp::IptOpDelete, "MyWonderfulAlert", 123456));
Lorenzo Colitti3c272702017-04-26 15:48:13 +0900580 expectIptablesRestoreCommands(expected);
Lorenzo Colittie8b56e42017-04-26 15:16:03 +0900581}
Lorenzo Colittif4dfa682017-04-28 11:09:07 +0900582
Lorenzo Colitti38078222017-07-06 17:27:23 +0900583TEST_F(BandwidthControllerTest, CostlyAlert) {
584 const int64_t kQuota = 123456;
585 int64_t alertBytes = 0;
586
587 std::vector<std::string> expected = {
588 "-A bw_costly_shared -m quota2 ! --quota 123456 --name sharedAlert\n",
589 };
590 EXPECT_EQ(0, setCostlyAlert("shared", kQuota, &alertBytes));
591 EXPECT_EQ(kQuota, alertBytes);
592 expectIptablesCommands(expected);
593
594 expected = {};
595 expectUpdateQuota(kQuota);
596 EXPECT_EQ(0, setCostlyAlert("shared", kQuota + 1, &alertBytes));
597 EXPECT_EQ(kQuota + 1, alertBytes);
598 expectIptablesCommands(expected);
599
600 expected = {
601 "-D bw_costly_shared -m quota2 ! --quota 123457 --name sharedAlert\n"
602 };
603 EXPECT_EQ(0, removeCostlyAlert("shared", &alertBytes));
604 EXPECT_EQ(0, alertBytes);
605 expectIptablesCommands(expected);
606}
607
Lorenzo Colittif4dfa682017-04-28 11:09:07 +0900608TEST_F(BandwidthControllerTest, ManipulateSpecialApps) {
609 std::vector<const char *> appUids = { "1000", "1001", "10012" };
610
611 std::vector<std::string> expected = {
Lorenzo Colitti911bc4c2017-04-28 14:34:01 +0900612 "*filter\n"
613 "-I bw_happy_box -m owner --uid-owner 1000 --jump RETURN\n"
614 "-I bw_happy_box -m owner --uid-owner 1001 --jump RETURN\n"
615 "-I bw_happy_box -m owner --uid-owner 10012 --jump RETURN\n"
616 "COMMIT\n"
Lorenzo Colittif4dfa682017-04-28 11:09:07 +0900617 };
618 EXPECT_EQ(0, mBw.addNiceApps(appUids.size(), const_cast<char**>(&appUids[0])));
Lorenzo Colitti911bc4c2017-04-28 14:34:01 +0900619 expectIptablesRestoreCommands(expected);
Lorenzo Colittif4dfa682017-04-28 11:09:07 +0900620
621 expected = {
Lorenzo Colitti911bc4c2017-04-28 14:34:01 +0900622 "*filter\n"
623 "-D bw_penalty_box -m owner --uid-owner 1000 --jump REJECT\n"
624 "-D bw_penalty_box -m owner --uid-owner 1001 --jump REJECT\n"
625 "-D bw_penalty_box -m owner --uid-owner 10012 --jump REJECT\n"
626 "COMMIT\n"
Lorenzo Colittif4dfa682017-04-28 11:09:07 +0900627 };
628 EXPECT_EQ(0, mBw.removeNaughtyApps(appUids.size(), const_cast<char**>(&appUids[0])));
Lorenzo Colitti911bc4c2017-04-28 14:34:01 +0900629 expectIptablesRestoreCommands(expected);
Lorenzo Colittif4dfa682017-04-28 11:09:07 +0900630}