blob: 1886ad6797f6ccd3d0a9446fea883bad5f602749 [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("-N %s", c_chain),
Lorenzo Colitti2782b6b2017-07-06 15:32:05 +0900390 StringPrintf("-F %s", c_chain),
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900391 StringPrintf("-A %s -j bw_penalty_box", c_chain),
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900392 StringPrintf("-I bw_INPUT %d -i %s --jump %s", ruleIndex, c_iface, c_chain),
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900393 StringPrintf("-I bw_OUTPUT %d -o %s --jump %s", ruleIndex, c_iface, c_chain),
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900394 StringPrintf("-A bw_FORWARD -o %s --jump %s", c_iface, c_chain),
395 StringPrintf("-A %s -m quota2 ! --quota %" PRIu64 " --name %s --jump REJECT", c_chain,
396 quota, c_iface),
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +0900397 };
398 return cmds;
399}
400
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900401const std::vector<std::string> removeInterfaceQuotaCommands(const std::string& iface) {
402 const std::string chain = "bw_costly_" + iface;
403 const char* c_chain = chain.c_str();
404 const char* c_iface = iface.c_str();
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +0900405 std::vector<std::string> cmds = {
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900406 StringPrintf("-D bw_INPUT -i %s --jump %s", c_iface, c_chain),
407 StringPrintf("-D bw_OUTPUT -o %s --jump %s", c_iface, c_chain),
408 StringPrintf("-D bw_FORWARD -o %s --jump %s", c_iface, c_chain),
409 StringPrintf("-F %s", c_chain),
410 StringPrintf("-X %s", c_chain),
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +0900411 };
412 return cmds;
413}
414
415TEST_F(BandwidthControllerTest, TestSetInterfaceQuota) {
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900416 constexpr uint64_t kOldQuota = 123456;
417 const std::string iface = mTun.name();
418 std::vector<std::string> expected = makeInterfaceQuotaCommands(iface, 1, kOldQuota);
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +0900419
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900420 EXPECT_EQ(0, mBw.setInterfaceQuota(iface, kOldQuota));
421 expectIptablesCommands(expected);
422
423 constexpr uint64_t kNewQuota = kOldQuota + 1;
424 expected = {};
425 expectUpdateQuota(kNewQuota);
426 EXPECT_EQ(0, mBw.setInterfaceQuota(iface, kNewQuota));
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +0900427 expectIptablesCommands(expected);
428
429 expected = removeInterfaceQuotaCommands(iface);
430 EXPECT_EQ(0, mBw.removeInterfaceQuota(iface));
431 expectIptablesCommands(expected);
432}
Lorenzo Colittie8b56e42017-04-26 15:16:03 +0900433
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900434const std::vector<std::string> makeInterfaceSharedQuotaCommands(const std::string& iface,
435 int ruleIndex, int64_t quota) {
436 const std::string chain = "bw_costly_shared";
437 const char* c_chain = chain.c_str();
438 const char* c_iface = iface.c_str();
439 std::vector<std::string> cmds = {
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900440 StringPrintf("-I bw_INPUT %d -i %s --jump %s", ruleIndex, c_iface, c_chain),
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900441 StringPrintf("-I bw_OUTPUT %d -o %s --jump %s", ruleIndex, c_iface, c_chain),
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900442 StringPrintf("-A bw_FORWARD -o %s --jump %s", c_iface, c_chain),
443 StringPrintf("-I %s -m quota2 ! --quota %" PRIu64 " --name shared --jump REJECT", c_chain,
444 quota),
445 };
446 return cmds;
447}
448
449const std::vector<std::string> removeInterfaceSharedQuotaCommands(const std::string& iface,
450 int64_t quota) {
451 const std::string chain = "bw_costly_shared";
452 const char* c_chain = chain.c_str();
453 const char* c_iface = iface.c_str();
454 std::vector<std::string> cmds = {
455 StringPrintf("-D bw_INPUT -i %s --jump %s", c_iface, c_chain),
456 StringPrintf("-D bw_OUTPUT -o %s --jump %s", c_iface, c_chain),
457 StringPrintf("-D bw_FORWARD -o %s --jump %s", c_iface, c_chain),
458 StringPrintf("-D %s -m quota2 ! --quota %" PRIu64
459 " --name shared --jump REJECT", c_chain, quota),
460 };
461 return cmds;
462}
463
464TEST_F(BandwidthControllerTest, TestSetInterfaceSharedQuotaDuplicate) {
465 constexpr uint64_t kQuota = 123456;
466 const std::string iface = mTun.name();
467 std::vector<std::string> expected = makeInterfaceSharedQuotaCommands(iface, 1, 123456);
468 EXPECT_EQ(0, mBw.setInterfaceSharedQuota(iface, kQuota));
469 expectIptablesCommands(expected);
470
471 expected = {};
472 EXPECT_EQ(0, mBw.setInterfaceSharedQuota(iface, kQuota));
473 expectIptablesCommands(expected);
474
475 expected = removeInterfaceSharedQuotaCommands(iface, kQuota);
476 EXPECT_EQ(0, mBw.removeInterfaceSharedQuota(iface));
477 expectIptablesCommands(expected);
478}
479
480TEST_F(BandwidthControllerTest, TestSetInterfaceSharedQuotaUpdate) {
481 constexpr uint64_t kOldQuota = 123456;
482 const std::string iface = mTun.name();
483 std::vector<std::string> expected = makeInterfaceSharedQuotaCommands(iface, 1, kOldQuota);
484 EXPECT_EQ(0, mBw.setInterfaceSharedQuota(iface, kOldQuota));
485 expectIptablesCommands(expected);
486
487 constexpr uint64_t kNewQuota = kOldQuota + 1;
488 expected = {};
489 expectUpdateQuota(kNewQuota);
490 EXPECT_EQ(0, mBw.setInterfaceSharedQuota(iface, kNewQuota));
491 expectIptablesCommands(expected);
492
493 expected = removeInterfaceSharedQuotaCommands(iface, kNewQuota);
494 EXPECT_EQ(0, mBw.removeInterfaceSharedQuota(iface));
495 expectIptablesCommands(expected);
496}
497
498TEST_F(BandwidthControllerTest, TestSetInterfaceSharedQuotaTwoInterfaces) {
499 constexpr uint64_t kQuota = 123456;
500 const std::vector<std::string> ifaces{
501 {"a" + mTun.name()},
502 {"b" + mTun.name()},
503 };
504
505 for (const auto& iface : ifaces) {
506 bool first = (iface == ifaces[0]);
507 auto expected = makeInterfaceSharedQuotaCommands(iface, 1, kQuota);
508 if (!first) {
509 // Quota rule is only added when the total number of
510 // interfaces transitions from 0 -> 1.
511 expected.pop_back();
512 }
513 EXPECT_EQ(0, mBw.setInterfaceSharedQuota(iface, kQuota));
514 expectIptablesCommands(expected);
515 }
516
517 for (const auto& iface : ifaces) {
518 bool last = (iface == ifaces[1]);
519 auto expected = removeInterfaceSharedQuotaCommands(iface, kQuota);
520 if (!last) {
521 // Quota rule is only removed when the total number of
522 // interfaces transitions from 1 -> 0.
523 expected.pop_back();
524 }
525 EXPECT_EQ(0, mBw.removeInterfaceSharedQuota(iface));
526 expectIptablesCommands(expected);
527 }
528}
529
Lorenzo Colittie8b56e42017-04-26 15:16:03 +0900530TEST_F(BandwidthControllerTest, IptablesAlertCmd) {
531 std::vector<std::string> expected = {
Lorenzo Colitti3c272702017-04-26 15:48:13 +0900532 "*filter\n"
533 "-I bw_INPUT -m quota2 ! --quota 123456 --name MyWonderfulAlert\n"
534 "-I bw_OUTPUT -m quota2 ! --quota 123456 --name MyWonderfulAlert\n"
535 "COMMIT\n"
Lorenzo Colittie8b56e42017-04-26 15:16:03 +0900536 };
537 EXPECT_EQ(0, runIptablesAlertCmd(IptOp::IptOpInsert, "MyWonderfulAlert", 123456));
Lorenzo Colitti3c272702017-04-26 15:48:13 +0900538 expectIptablesRestoreCommands(expected);
Lorenzo Colittie8b56e42017-04-26 15:16:03 +0900539
540 expected = {
Lorenzo Colitti3c272702017-04-26 15:48:13 +0900541 "*filter\n"
542 "-D bw_INPUT -m quota2 ! --quota 123456 --name MyWonderfulAlert\n"
543 "-D bw_OUTPUT -m quota2 ! --quota 123456 --name MyWonderfulAlert\n"
544 "COMMIT\n"
Lorenzo Colittie8b56e42017-04-26 15:16:03 +0900545 };
546 EXPECT_EQ(0, runIptablesAlertCmd(IptOp::IptOpDelete, "MyWonderfulAlert", 123456));
Lorenzo Colitti3c272702017-04-26 15:48:13 +0900547 expectIptablesRestoreCommands(expected);
Lorenzo Colittie8b56e42017-04-26 15:16:03 +0900548}
549
550TEST_F(BandwidthControllerTest, IptablesAlertFwdCmd) {
551 std::vector<std::string> expected = {
Lorenzo Colitti3c272702017-04-26 15:48:13 +0900552 "*filter\n"
553 "-I bw_FORWARD -m quota2 ! --quota 123456 --name MyWonderfulAlert\n"
554 "COMMIT\n"
Lorenzo Colittie8b56e42017-04-26 15:16:03 +0900555 };
556 EXPECT_EQ(0, runIptablesAlertFwdCmd(IptOp::IptOpInsert, "MyWonderfulAlert", 123456));
Lorenzo Colitti3c272702017-04-26 15:48:13 +0900557 expectIptablesRestoreCommands(expected);
Lorenzo Colittie8b56e42017-04-26 15:16:03 +0900558
559 expected = {
Lorenzo Colitti3c272702017-04-26 15:48:13 +0900560 "*filter\n"
561 "-D bw_FORWARD -m quota2 ! --quota 123456 --name MyWonderfulAlert\n"
562 "COMMIT\n"
Lorenzo Colittie8b56e42017-04-26 15:16:03 +0900563 };
564 EXPECT_EQ(0, runIptablesAlertFwdCmd(IptOp::IptOpDelete, "MyWonderfulAlert", 123456));
Lorenzo Colitti3c272702017-04-26 15:48:13 +0900565 expectIptablesRestoreCommands(expected);
Lorenzo Colittie8b56e42017-04-26 15:16:03 +0900566}
Lorenzo Colittif4dfa682017-04-28 11:09:07 +0900567
568TEST_F(BandwidthControllerTest, ManipulateSpecialApps) {
569 std::vector<const char *> appUids = { "1000", "1001", "10012" };
570
571 std::vector<std::string> expected = {
Lorenzo Colitti911bc4c2017-04-28 14:34:01 +0900572 "*filter\n"
573 "-I bw_happy_box -m owner --uid-owner 1000 --jump RETURN\n"
574 "-I bw_happy_box -m owner --uid-owner 1001 --jump RETURN\n"
575 "-I bw_happy_box -m owner --uid-owner 10012 --jump RETURN\n"
576 "COMMIT\n"
Lorenzo Colittif4dfa682017-04-28 11:09:07 +0900577 };
578 EXPECT_EQ(0, mBw.addNiceApps(appUids.size(), const_cast<char**>(&appUids[0])));
Lorenzo Colitti911bc4c2017-04-28 14:34:01 +0900579 expectIptablesRestoreCommands(expected);
Lorenzo Colittif4dfa682017-04-28 11:09:07 +0900580
581 expected = {
Lorenzo Colitti911bc4c2017-04-28 14:34:01 +0900582 "*filter\n"
583 "-D bw_penalty_box -m owner --uid-owner 1000 --jump REJECT\n"
584 "-D bw_penalty_box -m owner --uid-owner 1001 --jump REJECT\n"
585 "-D bw_penalty_box -m owner --uid-owner 10012 --jump REJECT\n"
586 "COMMIT\n"
Lorenzo Colittif4dfa682017-04-28 11:09:07 +0900587 };
588 EXPECT_EQ(0, mBw.removeNaughtyApps(appUids.size(), const_cast<char**>(&appUids[0])));
Lorenzo Colitti911bc4c2017-04-28 14:34:01 +0900589 expectIptablesRestoreCommands(expected);
Lorenzo Colittif4dfa682017-04-28 11:09:07 +0900590}