blob: dd466122b529400b948b07850bf5f4445d55b632 [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 Colittidf42ddd2017-02-28 01:20:13 +090045using android::base::StringPrintf;
46using android::net::TunInterface;
Joel Scherpelz01cc5492017-06-16 10:45:14 +090047using android::netdutils::status::ok;
48using android::netdutils::UniqueFile;
Lorenzo Colitti86a47982016-03-18 17:52:25 +090049
Lorenzo Colitti0f150552016-03-28 02:30:27 +090050class BandwidthControllerTest : public IptablesBaseTest {
Joel Scherpelz01cc5492017-06-16 10:45:14 +090051protected:
Lorenzo Colitti86a47982016-03-18 17:52:25 +090052 BandwidthControllerTest() {
53 BandwidthController::execFunction = fake_android_fork_exec;
54 BandwidthController::popenFunction = fake_popen;
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +090055 BandwidthController::iptablesRestoreFunction = fakeExecIptablesRestoreWithOutput;
Lorenzo Colitti86a47982016-03-18 17:52:25 +090056 }
57 BandwidthController mBw;
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +090058 TunInterface mTun;
59
60 void SetUp() {
61 ASSERT_EQ(0, mTun.init());
62 }
63
64 void TearDown() {
65 mTun.destroy();
66 }
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +090067
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +090068 void addIptablesRestoreOutput(std::string contents) {
69 sIptablesRestoreOutput.push_back(contents);
70 }
71
Lorenzo Colittice6748a2017-02-02 01:34:33 +090072 void addIptablesRestoreOutput(std::string contents1, std::string contents2) {
73 sIptablesRestoreOutput.push_back(contents1);
74 sIptablesRestoreOutput.push_back(contents2);
75 }
76
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +090077 void clearIptablesRestoreOutput() {
78 sIptablesRestoreOutput.clear();
79 }
80
81 void expectSetupCommands(const std::string& expectedClean, std::string expectedAccounting) {
82 std::string expectedList =
83 "*filter\n"
84 "-S\n"
85 "COMMIT\n";
86
87 std::string expectedFlush =
88 "*filter\n"
89 ":bw_INPUT -\n"
90 ":bw_OUTPUT -\n"
91 ":bw_FORWARD -\n"
92 ":bw_happy_box -\n"
93 ":bw_penalty_box -\n"
94 ":bw_data_saver -\n"
95 ":bw_costly_shared -\n"
96 "COMMIT\n"
97 "*raw\n"
98 ":bw_raw_PREROUTING -\n"
99 "COMMIT\n"
100 "*mangle\n"
101 ":bw_mangle_POSTROUTING -\n"
102 "COMMIT\n";
103
104 ExpectedIptablesCommands expected = {{ V4, expectedList }};
105 if (expectedClean.size()) {
106 expected.push_back({ V4V6, expectedClean });
107 }
108 expected.push_back({ V4V6, expectedFlush });
109 if (expectedAccounting.size()) {
110 expected.push_back({ V4V6, expectedAccounting });
111 }
112
113 expectIptablesRestoreCommands(expected);
114 }
Lorenzo Colittie8b56e42017-04-26 15:16:03 +0900115
116 using IptOp = BandwidthController::IptOp;
117
118 int runIptablesAlertCmd(IptOp a, const char *b, int64_t c) {
119 return mBw.runIptablesAlertCmd(a, b, c);
120 }
121
122 int runIptablesAlertFwdCmd(IptOp a, const char *b, int64_t c) {
123 return mBw.runIptablesAlertFwdCmd(a, b, c);
124 }
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900125
126 void expectUpdateQuota(uint64_t quota) {
127 uintptr_t dummy;
128 FILE* dummyFile = reinterpret_cast<FILE*>(&dummy);
129
130 EXPECT_CALL(mSyscalls, fopen(_, _)).WillOnce(Return(ByMove(UniqueFile(dummyFile))));
131 EXPECT_CALL(mSyscalls, vfprintf(dummyFile, _, _))
132 .WillOnce(Invoke([quota](FILE*, const std::string&, va_list ap) {
133 EXPECT_EQ(quota, va_arg(ap, uint64_t));
134 return 0;
135 }));
136 EXPECT_CALL(mSyscalls, fclose(dummyFile)).WillOnce(Return(ok));
137 }
138
139 StrictMock<android::netdutils::ScopedMockSyscalls> mSyscalls;
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900140};
141
Lorenzo Colittia0dc8a52016-03-26 22:42:07 +0900142TEST_F(BandwidthControllerTest, TestSetupIptablesHooks) {
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900143 // Pretend some bw_costly_shared_<iface> rules already exist...
144 addIptablesRestoreOutput(
145 "-P OUTPUT ACCEPT\n"
146 "-N bw_costly_rmnet_data0\n"
147 "-N bw_costly_shared\n"
148 "-N unrelated\n"
149 "-N bw_costly_rmnet_data7\n");
150
151 // ... and expect that they be flushed and deleted.
152 std::string expectedCleanCmds =
Lorenzo Colitti13debb82016-03-27 17:46:30 +0900153 "*filter\n"
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900154 ":bw_costly_rmnet_data0 -\n"
155 "-X bw_costly_rmnet_data0\n"
156 ":bw_costly_rmnet_data7 -\n"
157 "-X bw_costly_rmnet_data7\n"
158 "COMMIT\n";
159
160 mBw.setupIptablesHooks();
161 expectSetupCommands(expectedCleanCmds, "");
Lorenzo Colittia0dc8a52016-03-26 22:42:07 +0900162}
163
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900164TEST_F(BandwidthControllerTest, TestEnableBandwidthControl) {
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900165 // Pretend no bw_costly_shared_<iface> rules already exist...
166 addIptablesRestoreOutput(
167 "-P OUTPUT ACCEPT\n"
168 "-N bw_costly_shared\n"
169 "-N unrelated\n");
170
171 // ... so none are flushed or deleted.
172 std::string expectedClean = "";
173
174 std::string expectedAccounting =
Lorenzo Colitti13debb82016-03-27 17:46:30 +0900175 "*filter\n"
176 "-A bw_INPUT -m owner --socket-exists\n"
177 "-A bw_OUTPUT -m owner --socket-exists\n"
178 "-A bw_costly_shared --jump bw_penalty_box\n"
179 "-A bw_penalty_box --jump bw_happy_box\n"
180 "-A bw_happy_box --jump bw_data_saver\n"
181 "-A bw_data_saver -j RETURN\n"
182 "-I bw_happy_box -m owner --uid-owner 0-9999 --jump RETURN\n"
183 "COMMIT\n"
184 "*raw\n"
185 "-A bw_raw_PREROUTING -m owner --socket-exists\n"
186 "COMMIT\n"
187 "*mangle\n"
188 "-A bw_mangle_POSTROUTING -m owner --socket-exists\n"
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900189 "COMMIT\n";
Lorenzo Colitti13debb82016-03-27 17:46:30 +0900190
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900191 mBw.enableBandwidthControl(false);
192 expectSetupCommands(expectedClean, expectedAccounting);
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900193}
194
Lorenzo Colittia0dc8a52016-03-26 22:42:07 +0900195TEST_F(BandwidthControllerTest, TestDisableBandwidthControl) {
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900196 // Pretend some bw_costly_shared_<iface> rules already exist...
197 addIptablesRestoreOutput(
198 "-P OUTPUT ACCEPT\n"
199 "-N bw_costly_rmnet_data0\n"
200 "-N bw_costly_shared\n"
201 "-N unrelated\n"
202 "-N bw_costly_rmnet_data7\n");
203
204 // ... and expect that they be flushed.
205 std::string expectedCleanCmds =
Lorenzo Colitti13debb82016-03-27 17:46:30 +0900206 "*filter\n"
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900207 ":bw_costly_rmnet_data0 -\n"
208 ":bw_costly_rmnet_data7 -\n"
209 "COMMIT\n";
210
211 mBw.disableBandwidthControl();
212 expectSetupCommands(expectedCleanCmds, "");
Lorenzo Colittia0dc8a52016-03-26 22:42:07 +0900213}
214
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900215TEST_F(BandwidthControllerTest, TestEnableDataSaver) {
216 mBw.enableDataSaver(true);
217 std::vector<std::string> expected = {
Lorenzo Colitti911bc4c2017-04-28 14:34:01 +0900218 "*filter\n"
219 "-R bw_data_saver 1 --jump REJECT\n"
220 "COMMIT\n"
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900221 };
Lorenzo Colitti911bc4c2017-04-28 14:34:01 +0900222 expectIptablesRestoreCommands(expected);
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900223
224 mBw.enableDataSaver(false);
225 expected = {
Lorenzo Colitti911bc4c2017-04-28 14:34:01 +0900226 "*filter\n"
227 "-R bw_data_saver 1 --jump RETURN\n"
228 "COMMIT\n"
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900229 };
Lorenzo Colitti911bc4c2017-04-28 14:34:01 +0900230 expectIptablesRestoreCommands(expected);
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900231}
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900232
233std::string kIPv4TetherCounters = android::base::Join(std::vector<std::string> {
234 "Chain natctrl_tether_counters (4 references)",
235 " pkts bytes target prot opt in out source destination",
236 " 26 2373 RETURN all -- wlan0 rmnet0 0.0.0.0/0 0.0.0.0/0",
237 " 27 2002 RETURN all -- rmnet0 wlan0 0.0.0.0/0 0.0.0.0/0",
238 " 1040 107471 RETURN all -- bt-pan rmnet0 0.0.0.0/0 0.0.0.0/0",
239 " 1450 1708806 RETURN all -- rmnet0 bt-pan 0.0.0.0/0 0.0.0.0/0",
240}, '\n');
241
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900242std::string kIPv6TetherCounters = android::base::Join(std::vector<std::string> {
243 "Chain natctrl_tether_counters (2 references)",
244 " pkts bytes target prot opt in out source destination",
245 " 10000 10000000 RETURN all wlan0 rmnet0 ::/0 ::/0",
246 " 20000 20000000 RETURN all rmnet0 wlan0 ::/0 ::/0",
247}, '\n');
248
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900249std::string readSocketClientResponse(int fd) {
250 char buf[32768];
251 ssize_t bytesRead = read(fd, buf, sizeof(buf));
252 if (bytesRead < 0) {
253 return "";
254 }
255 for (int i = 0; i < bytesRead; i++) {
256 if (buf[i] == '\0') buf[i] = '\n';
257 }
258 return std::string(buf, bytesRead);
259}
260
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900261void expectNoSocketClientResponse(int fd) {
262 char buf[64];
263 EXPECT_EQ(-1, read(fd, buf, sizeof(buf)));
264}
265
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900266TEST_F(BandwidthControllerTest, TestGetTetherStats) {
267 int socketPair[2];
268 ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM, 0, socketPair));
269 ASSERT_EQ(0, fcntl(socketPair[0], F_SETFL, O_NONBLOCK | fcntl(socketPair[0], F_GETFL)));
270 ASSERT_EQ(0, fcntl(socketPair[1], F_SETFL, O_NONBLOCK | fcntl(socketPair[1], F_GETFL)));
271 SocketClient cli(socketPair[0], false);
272
273 std::string err;
274 BandwidthController::TetherStats filter;
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900275
276 // If no filter is specified, both IPv4 and IPv6 counters must have at least one interface pair.
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900277 addIptablesRestoreOutput(kIPv4TetherCounters);
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900278 ASSERT_EQ(-1, mBw.getTetherStats(&cli, filter, err));
279 expectNoSocketClientResponse(socketPair[1]);
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900280 clearIptablesRestoreOutput();
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900281
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900282 addIptablesRestoreOutput(kIPv6TetherCounters);
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900283 ASSERT_EQ(-1, mBw.getTetherStats(&cli, filter, err));
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900284 clearIptablesRestoreOutput();
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900285
286 // IPv4 and IPv6 counters are properly added together.
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900287 addIptablesRestoreOutput(kIPv4TetherCounters, kIPv6TetherCounters);
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900288 filter = BandwidthController::TetherStats();
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900289 std::string expected =
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900290 "114 wlan0 rmnet0 10002373 10026 20002002 20027\n"
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900291 "114 bt-pan rmnet0 107471 1040 1708806 1450\n"
292 "200 Tethering stats list completed\n";
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900293 ASSERT_EQ(0, mBw.getTetherStats(&cli, filter, err));
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900294 ASSERT_EQ(expected, readSocketClientResponse(socketPair[1]));
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900295 expectNoSocketClientResponse(socketPair[1]);
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900296 clearIptablesRestoreOutput();
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900297
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900298 // Test filtering.
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900299 addIptablesRestoreOutput(kIPv4TetherCounters, kIPv6TetherCounters);
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900300 filter = BandwidthController::TetherStats("bt-pan", "rmnet0", -1, -1, -1, -1);
301 expected = "221 bt-pan rmnet0 107471 1040 1708806 1450\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 Colittice6748a2017-02-02 01:34:33 +0900307 addIptablesRestoreOutput(kIPv4TetherCounters, kIPv6TetherCounters);
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900308 filter = BandwidthController::TetherStats("wlan0", "rmnet0", -1, -1, -1, -1);
309 expected = "221 wlan0 rmnet0 10002373 10026 20002002 20027\n";
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900310 ASSERT_EQ(0, mBw.getTetherStats(&cli, filter, err));
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900311 ASSERT_EQ(expected, readSocketClientResponse(socketPair[1]));
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900312 clearIptablesRestoreOutput();
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900313
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900314 // Select nonexistent interfaces.
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900315 addIptablesRestoreOutput(kIPv4TetherCounters, kIPv6TetherCounters);
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900316 filter = BandwidthController::TetherStats("rmnet0", "foo0", -1, -1, -1, -1);
317 expected = "200 Tethering stats list completed\n";
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900318 ASSERT_EQ(0, mBw.getTetherStats(&cli, filter, err));
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900319 ASSERT_EQ(expected, readSocketClientResponse(socketPair[1]));
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900320 clearIptablesRestoreOutput();
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900321
322 // No stats with a filter: no error.
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900323 addIptablesRestoreOutput("", "");
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900324 ASSERT_EQ(0, mBw.getTetherStats(&cli, filter, err));
325 ASSERT_EQ("200 Tethering stats list completed\n", readSocketClientResponse(socketPair[1]));
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900326 clearIptablesRestoreOutput();
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900327
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900328 addIptablesRestoreOutput("foo", "foo");
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900329 ASSERT_EQ(0, mBw.getTetherStats(&cli, filter, err));
330 ASSERT_EQ("200 Tethering stats list completed\n", readSocketClientResponse(socketPair[1]));
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900331 clearIptablesRestoreOutput();
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900332
333 // No stats and empty filter: error.
334 filter = BandwidthController::TetherStats();
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900335 addIptablesRestoreOutput("", kIPv6TetherCounters);
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900336 ASSERT_EQ(-1, mBw.getTetherStats(&cli, filter, err));
337 expectNoSocketClientResponse(socketPair[1]);
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900338 clearIptablesRestoreOutput();
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900339
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900340 addIptablesRestoreOutput(kIPv4TetherCounters, "");
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900341 ASSERT_EQ(-1, mBw.getTetherStats(&cli, filter, err));
342 expectNoSocketClientResponse(socketPair[1]);
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900343 clearIptablesRestoreOutput();
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900344
345 // Include only one pair of interfaces and things are fine.
346 std::vector<std::string> counterLines = android::base::Split(kIPv4TetherCounters, "\n");
347 std::vector<std::string> brokenCounterLines = counterLines;
348 counterLines.resize(4);
349 std::string counters = android::base::Join(counterLines, "\n") + "\n";
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900350 addIptablesRestoreOutput(counters, counters);
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900351 expected =
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900352 "114 wlan0 rmnet0 4746 52 4004 54\n"
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900353 "200 Tethering stats list completed\n";
354 ASSERT_EQ(0, mBw.getTetherStats(&cli, filter, err));
355 ASSERT_EQ(expected, readSocketClientResponse(socketPair[1]));
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900356 clearIptablesRestoreOutput();
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900357
358 // But if interfaces aren't paired, it's always an error.
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900359 err = "";
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900360 counterLines.resize(3);
361 counters = android::base::Join(counterLines, "\n") + "\n";
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900362 addIptablesRestoreOutput(counters, counters);
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900363 ASSERT_EQ(-1, mBw.getTetherStats(&cli, filter, err));
364 expectNoSocketClientResponse(socketPair[1]);
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900365 clearIptablesRestoreOutput();
366
367 // Token unit test of the fact that we return the stats in the error message which the caller
368 // ignores.
369 std::string expectedError = counters;
370 EXPECT_EQ(expectedError, err);
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900371
372 // popen() failing is always an error.
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900373 addIptablesRestoreOutput(kIPv4TetherCounters);
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900374 ASSERT_EQ(-1, mBw.getTetherStats(&cli, filter, err));
375 expectNoSocketClientResponse(socketPair[1]);
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900376 clearIptablesRestoreOutput();
377 addIptablesRestoreOutput(kIPv6TetherCounters);
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900378 ASSERT_EQ(-1, mBw.getTetherStats(&cli, filter, err));
379 expectNoSocketClientResponse(socketPair[1]);
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900380 clearIptablesRestoreOutput();
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900381}
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +0900382
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900383const std::vector<std::string> makeInterfaceQuotaCommands(const std::string& iface, int ruleIndex,
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +0900384 int64_t quota) {
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900385 const std::string chain = "bw_costly_" + iface;
386 const char* c_chain = chain.c_str();
387 const char* c_iface = iface.c_str();
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +0900388 std::vector<std::string> cmds = {
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900389 // StringPrintf(":%s -", c_chain),
390 StringPrintf("-F %s", c_chain),
391 StringPrintf("-N %s", c_chain),
392 StringPrintf("-A %s -j bw_penalty_box", c_chain),
393 StringPrintf("-D bw_INPUT -i %s --jump %s", c_iface, c_chain),
394 StringPrintf("-I bw_INPUT %d -i %s --jump %s", ruleIndex, c_iface, c_chain),
395 StringPrintf("-D bw_OUTPUT -o %s --jump %s", c_iface, c_chain),
396 StringPrintf("-I bw_OUTPUT %d -o %s --jump %s", ruleIndex, c_iface, c_chain),
397 StringPrintf("-D bw_FORWARD -o %s --jump %s", c_iface, c_chain),
398 StringPrintf("-A bw_FORWARD -o %s --jump %s", c_iface, c_chain),
399 StringPrintf("-A %s -m quota2 ! --quota %" PRIu64 " --name %s --jump REJECT", c_chain,
400 quota, c_iface),
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +0900401 };
402 return cmds;
403}
404
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900405const std::vector<std::string> removeInterfaceQuotaCommands(const std::string& iface) {
406 const std::string chain = "bw_costly_" + iface;
407 const char* c_chain = chain.c_str();
408 const char* c_iface = iface.c_str();
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +0900409 std::vector<std::string> cmds = {
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900410 StringPrintf("-D bw_INPUT -i %s --jump %s", c_iface, c_chain),
411 StringPrintf("-D bw_OUTPUT -o %s --jump %s", c_iface, c_chain),
412 StringPrintf("-D bw_FORWARD -o %s --jump %s", c_iface, c_chain),
413 StringPrintf("-F %s", c_chain),
414 StringPrintf("-X %s", c_chain),
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +0900415 };
416 return cmds;
417}
418
419TEST_F(BandwidthControllerTest, TestSetInterfaceQuota) {
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900420 constexpr uint64_t kOldQuota = 123456;
421 const std::string iface = mTun.name();
422 std::vector<std::string> expected = makeInterfaceQuotaCommands(iface, 1, kOldQuota);
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +0900423
424 // prepCostlyInterface assumes that exactly one of the "-F chain" and "-N chain" commands fails.
425 // So pretend that the first two commands (the IPv4 -F and the IPv6 -F) fail.
426 std::deque<int> returnValues(expected.size() * 2, 0);
427 returnValues[0] = 1;
428 returnValues[1] = 1;
429 setReturnValues(returnValues);
430
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900431 EXPECT_EQ(0, mBw.setInterfaceQuota(iface, kOldQuota));
432 expectIptablesCommands(expected);
433
434 constexpr uint64_t kNewQuota = kOldQuota + 1;
435 expected = {};
436 expectUpdateQuota(kNewQuota);
437 EXPECT_EQ(0, mBw.setInterfaceQuota(iface, kNewQuota));
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +0900438 expectIptablesCommands(expected);
439
440 expected = removeInterfaceQuotaCommands(iface);
441 EXPECT_EQ(0, mBw.removeInterfaceQuota(iface));
442 expectIptablesCommands(expected);
443}
Lorenzo Colittie8b56e42017-04-26 15:16:03 +0900444
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900445const std::vector<std::string> makeInterfaceSharedQuotaCommands(const std::string& iface,
446 int ruleIndex, int64_t quota) {
447 const std::string chain = "bw_costly_shared";
448 const char* c_chain = chain.c_str();
449 const char* c_iface = iface.c_str();
450 std::vector<std::string> cmds = {
451 StringPrintf("-D bw_INPUT -i %s --jump %s", c_iface, c_chain),
452 StringPrintf("-I bw_INPUT %d -i %s --jump %s", ruleIndex, c_iface, c_chain),
453 StringPrintf("-D bw_OUTPUT -o %s --jump %s", c_iface, c_chain),
454 StringPrintf("-I bw_OUTPUT %d -o %s --jump %s", ruleIndex, c_iface, c_chain),
455 StringPrintf("-D bw_FORWARD -o %s --jump %s", c_iface, c_chain),
456 StringPrintf("-A bw_FORWARD -o %s --jump %s", c_iface, c_chain),
457 StringPrintf("-I %s -m quota2 ! --quota %" PRIu64 " --name shared --jump REJECT", c_chain,
458 quota),
459 };
460 return cmds;
461}
462
463const std::vector<std::string> removeInterfaceSharedQuotaCommands(const std::string& iface,
464 int64_t quota) {
465 const std::string chain = "bw_costly_shared";
466 const char* c_chain = chain.c_str();
467 const char* c_iface = iface.c_str();
468 std::vector<std::string> cmds = {
469 StringPrintf("-D bw_INPUT -i %s --jump %s", c_iface, c_chain),
470 StringPrintf("-D bw_OUTPUT -o %s --jump %s", c_iface, c_chain),
471 StringPrintf("-D bw_FORWARD -o %s --jump %s", c_iface, c_chain),
472 StringPrintf("-D %s -m quota2 ! --quota %" PRIu64
473 " --name shared --jump REJECT", c_chain, quota),
474 };
475 return cmds;
476}
477
478TEST_F(BandwidthControllerTest, TestSetInterfaceSharedQuotaDuplicate) {
479 constexpr uint64_t kQuota = 123456;
480 const std::string iface = mTun.name();
481 std::vector<std::string> expected = makeInterfaceSharedQuotaCommands(iface, 1, 123456);
482 EXPECT_EQ(0, mBw.setInterfaceSharedQuota(iface, kQuota));
483 expectIptablesCommands(expected);
484
485 expected = {};
486 EXPECT_EQ(0, mBw.setInterfaceSharedQuota(iface, kQuota));
487 expectIptablesCommands(expected);
488
489 expected = removeInterfaceSharedQuotaCommands(iface, kQuota);
490 EXPECT_EQ(0, mBw.removeInterfaceSharedQuota(iface));
491 expectIptablesCommands(expected);
492}
493
494TEST_F(BandwidthControllerTest, TestSetInterfaceSharedQuotaUpdate) {
495 constexpr uint64_t kOldQuota = 123456;
496 const std::string iface = mTun.name();
497 std::vector<std::string> expected = makeInterfaceSharedQuotaCommands(iface, 1, kOldQuota);
498 EXPECT_EQ(0, mBw.setInterfaceSharedQuota(iface, kOldQuota));
499 expectIptablesCommands(expected);
500
501 constexpr uint64_t kNewQuota = kOldQuota + 1;
502 expected = {};
503 expectUpdateQuota(kNewQuota);
504 EXPECT_EQ(0, mBw.setInterfaceSharedQuota(iface, kNewQuota));
505 expectIptablesCommands(expected);
506
507 expected = removeInterfaceSharedQuotaCommands(iface, kNewQuota);
508 EXPECT_EQ(0, mBw.removeInterfaceSharedQuota(iface));
509 expectIptablesCommands(expected);
510}
511
512TEST_F(BandwidthControllerTest, TestSetInterfaceSharedQuotaTwoInterfaces) {
513 constexpr uint64_t kQuota = 123456;
514 const std::vector<std::string> ifaces{
515 {"a" + mTun.name()},
516 {"b" + mTun.name()},
517 };
518
519 for (const auto& iface : ifaces) {
520 bool first = (iface == ifaces[0]);
521 auto expected = makeInterfaceSharedQuotaCommands(iface, 1, kQuota);
522 if (!first) {
523 // Quota rule is only added when the total number of
524 // interfaces transitions from 0 -> 1.
525 expected.pop_back();
526 }
527 EXPECT_EQ(0, mBw.setInterfaceSharedQuota(iface, kQuota));
528 expectIptablesCommands(expected);
529 }
530
531 for (const auto& iface : ifaces) {
532 bool last = (iface == ifaces[1]);
533 auto expected = removeInterfaceSharedQuotaCommands(iface, kQuota);
534 if (!last) {
535 // Quota rule is only removed when the total number of
536 // interfaces transitions from 1 -> 0.
537 expected.pop_back();
538 }
539 EXPECT_EQ(0, mBw.removeInterfaceSharedQuota(iface));
540 expectIptablesCommands(expected);
541 }
542}
543
Lorenzo Colittie8b56e42017-04-26 15:16:03 +0900544TEST_F(BandwidthControllerTest, IptablesAlertCmd) {
545 std::vector<std::string> expected = {
Lorenzo Colitti3c272702017-04-26 15:48:13 +0900546 "*filter\n"
547 "-I bw_INPUT -m quota2 ! --quota 123456 --name MyWonderfulAlert\n"
548 "-I bw_OUTPUT -m quota2 ! --quota 123456 --name MyWonderfulAlert\n"
549 "COMMIT\n"
Lorenzo Colittie8b56e42017-04-26 15:16:03 +0900550 };
551 EXPECT_EQ(0, runIptablesAlertCmd(IptOp::IptOpInsert, "MyWonderfulAlert", 123456));
Lorenzo Colitti3c272702017-04-26 15:48:13 +0900552 expectIptablesRestoreCommands(expected);
Lorenzo Colittie8b56e42017-04-26 15:16:03 +0900553
554 expected = {
Lorenzo Colitti3c272702017-04-26 15:48:13 +0900555 "*filter\n"
556 "-D bw_INPUT -m quota2 ! --quota 123456 --name MyWonderfulAlert\n"
557 "-D bw_OUTPUT -m quota2 ! --quota 123456 --name MyWonderfulAlert\n"
558 "COMMIT\n"
Lorenzo Colittie8b56e42017-04-26 15:16:03 +0900559 };
560 EXPECT_EQ(0, runIptablesAlertCmd(IptOp::IptOpDelete, "MyWonderfulAlert", 123456));
Lorenzo Colitti3c272702017-04-26 15:48:13 +0900561 expectIptablesRestoreCommands(expected);
Lorenzo Colittie8b56e42017-04-26 15:16:03 +0900562}
563
564TEST_F(BandwidthControllerTest, IptablesAlertFwdCmd) {
565 std::vector<std::string> expected = {
Lorenzo Colitti3c272702017-04-26 15:48:13 +0900566 "*filter\n"
567 "-I bw_FORWARD -m quota2 ! --quota 123456 --name MyWonderfulAlert\n"
568 "COMMIT\n"
Lorenzo Colittie8b56e42017-04-26 15:16:03 +0900569 };
570 EXPECT_EQ(0, runIptablesAlertFwdCmd(IptOp::IptOpInsert, "MyWonderfulAlert", 123456));
Lorenzo Colitti3c272702017-04-26 15:48:13 +0900571 expectIptablesRestoreCommands(expected);
Lorenzo Colittie8b56e42017-04-26 15:16:03 +0900572
573 expected = {
Lorenzo Colitti3c272702017-04-26 15:48:13 +0900574 "*filter\n"
575 "-D bw_FORWARD -m quota2 ! --quota 123456 --name MyWonderfulAlert\n"
576 "COMMIT\n"
Lorenzo Colittie8b56e42017-04-26 15:16:03 +0900577 };
578 EXPECT_EQ(0, runIptablesAlertFwdCmd(IptOp::IptOpDelete, "MyWonderfulAlert", 123456));
Lorenzo Colitti3c272702017-04-26 15:48:13 +0900579 expectIptablesRestoreCommands(expected);
Lorenzo Colittie8b56e42017-04-26 15:16:03 +0900580}
Lorenzo Colittif4dfa682017-04-28 11:09:07 +0900581
582TEST_F(BandwidthControllerTest, ManipulateSpecialApps) {
583 std::vector<const char *> appUids = { "1000", "1001", "10012" };
584
585 std::vector<std::string> expected = {
Lorenzo Colitti911bc4c2017-04-28 14:34:01 +0900586 "*filter\n"
587 "-I bw_happy_box -m owner --uid-owner 1000 --jump RETURN\n"
588 "-I bw_happy_box -m owner --uid-owner 1001 --jump RETURN\n"
589 "-I bw_happy_box -m owner --uid-owner 10012 --jump RETURN\n"
590 "COMMIT\n"
Lorenzo Colittif4dfa682017-04-28 11:09:07 +0900591 };
592 EXPECT_EQ(0, mBw.addNiceApps(appUids.size(), const_cast<char**>(&appUids[0])));
Lorenzo Colitti911bc4c2017-04-28 14:34:01 +0900593 expectIptablesRestoreCommands(expected);
Lorenzo Colittif4dfa682017-04-28 11:09:07 +0900594
595 expected = {
Lorenzo Colitti911bc4c2017-04-28 14:34:01 +0900596 "*filter\n"
597 "-D bw_penalty_box -m owner --uid-owner 1000 --jump REJECT\n"
598 "-D bw_penalty_box -m owner --uid-owner 1001 --jump REJECT\n"
599 "-D bw_penalty_box -m owner --uid-owner 10012 --jump REJECT\n"
600 "COMMIT\n"
Lorenzo Colittif4dfa682017-04-28 11:09:07 +0900601 };
602 EXPECT_EQ(0, mBw.removeNaughtyApps(appUids.size(), const_cast<char**>(&appUids[0])));
Lorenzo Colitti911bc4c2017-04-28 14:34:01 +0900603 expectIptablesRestoreCommands(expected);
Lorenzo Colittif4dfa682017-04-28 11:09:07 +0900604}