blob: 4371d00c5ef8e0559e1577fc21f93541832b3718 [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
127 void expectUpdateQuota(uint64_t quota) {
128 uintptr_t dummy;
129 FILE* dummyFile = reinterpret_cast<FILE*>(&dummy);
130
131 EXPECT_CALL(mSyscalls, fopen(_, _)).WillOnce(Return(ByMove(UniqueFile(dummyFile))));
132 EXPECT_CALL(mSyscalls, vfprintf(dummyFile, _, _))
133 .WillOnce(Invoke([quota](FILE*, const std::string&, va_list ap) {
134 EXPECT_EQ(quota, va_arg(ap, uint64_t));
135 return 0;
136 }));
137 EXPECT_CALL(mSyscalls, fclose(dummyFile)).WillOnce(Return(ok));
138 }
139
140 StrictMock<android::netdutils::ScopedMockSyscalls> mSyscalls;
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900141};
142
Lorenzo Colittia0dc8a52016-03-26 22:42:07 +0900143TEST_F(BandwidthControllerTest, TestSetupIptablesHooks) {
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900144 // Pretend some bw_costly_shared_<iface> rules already exist...
145 addIptablesRestoreOutput(
146 "-P OUTPUT ACCEPT\n"
147 "-N bw_costly_rmnet_data0\n"
148 "-N bw_costly_shared\n"
149 "-N unrelated\n"
150 "-N bw_costly_rmnet_data7\n");
151
152 // ... and expect that they be flushed and deleted.
153 std::string expectedCleanCmds =
Lorenzo Colitti13debb82016-03-27 17:46:30 +0900154 "*filter\n"
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900155 ":bw_costly_rmnet_data0 -\n"
156 "-X bw_costly_rmnet_data0\n"
157 ":bw_costly_rmnet_data7 -\n"
158 "-X bw_costly_rmnet_data7\n"
159 "COMMIT\n";
160
161 mBw.setupIptablesHooks();
162 expectSetupCommands(expectedCleanCmds, "");
Lorenzo Colittia0dc8a52016-03-26 22:42:07 +0900163}
164
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900165TEST_F(BandwidthControllerTest, TestEnableBandwidthControl) {
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900166 // Pretend no bw_costly_shared_<iface> rules already exist...
167 addIptablesRestoreOutput(
168 "-P OUTPUT ACCEPT\n"
169 "-N bw_costly_shared\n"
170 "-N unrelated\n");
171
172 // ... so none are flushed or deleted.
173 std::string expectedClean = "";
174
175 std::string expectedAccounting =
Lorenzo Colitti13debb82016-03-27 17:46:30 +0900176 "*filter\n"
177 "-A bw_INPUT -m owner --socket-exists\n"
178 "-A bw_OUTPUT -m owner --socket-exists\n"
179 "-A bw_costly_shared --jump bw_penalty_box\n"
180 "-A bw_penalty_box --jump bw_happy_box\n"
181 "-A bw_happy_box --jump bw_data_saver\n"
182 "-A bw_data_saver -j RETURN\n"
183 "-I bw_happy_box -m owner --uid-owner 0-9999 --jump RETURN\n"
184 "COMMIT\n"
185 "*raw\n"
186 "-A bw_raw_PREROUTING -m owner --socket-exists\n"
187 "COMMIT\n"
188 "*mangle\n"
189 "-A bw_mangle_POSTROUTING -m owner --socket-exists\n"
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900190 "COMMIT\n";
Lorenzo Colitti13debb82016-03-27 17:46:30 +0900191
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900192 mBw.enableBandwidthControl(false);
193 expectSetupCommands(expectedClean, expectedAccounting);
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900194}
195
Lorenzo Colittia0dc8a52016-03-26 22:42:07 +0900196TEST_F(BandwidthControllerTest, TestDisableBandwidthControl) {
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900197 // Pretend some bw_costly_shared_<iface> rules already exist...
198 addIptablesRestoreOutput(
199 "-P OUTPUT ACCEPT\n"
200 "-N bw_costly_rmnet_data0\n"
201 "-N bw_costly_shared\n"
202 "-N unrelated\n"
203 "-N bw_costly_rmnet_data7\n");
204
205 // ... and expect that they be flushed.
206 std::string expectedCleanCmds =
Lorenzo Colitti13debb82016-03-27 17:46:30 +0900207 "*filter\n"
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900208 ":bw_costly_rmnet_data0 -\n"
209 ":bw_costly_rmnet_data7 -\n"
210 "COMMIT\n";
211
212 mBw.disableBandwidthControl();
213 expectSetupCommands(expectedCleanCmds, "");
Lorenzo Colittia0dc8a52016-03-26 22:42:07 +0900214}
215
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900216TEST_F(BandwidthControllerTest, TestEnableDataSaver) {
217 mBw.enableDataSaver(true);
218 std::vector<std::string> expected = {
Lorenzo Colitti911bc4c2017-04-28 14:34:01 +0900219 "*filter\n"
220 "-R bw_data_saver 1 --jump REJECT\n"
221 "COMMIT\n"
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900222 };
Lorenzo Colitti911bc4c2017-04-28 14:34:01 +0900223 expectIptablesRestoreCommands(expected);
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900224
225 mBw.enableDataSaver(false);
226 expected = {
Lorenzo Colitti911bc4c2017-04-28 14:34:01 +0900227 "*filter\n"
228 "-R bw_data_saver 1 --jump RETURN\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}
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900233
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900234std::string kIPv4TetherCounters = Join(std::vector<std::string> {
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900235 "Chain natctrl_tether_counters (4 references)",
236 " pkts bytes target prot opt in out source destination",
237 " 26 2373 RETURN all -- wlan0 rmnet0 0.0.0.0/0 0.0.0.0/0",
238 " 27 2002 RETURN all -- rmnet0 wlan0 0.0.0.0/0 0.0.0.0/0",
239 " 1040 107471 RETURN all -- bt-pan rmnet0 0.0.0.0/0 0.0.0.0/0",
240 " 1450 1708806 RETURN all -- rmnet0 bt-pan 0.0.0.0/0 0.0.0.0/0",
241}, '\n');
242
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900243std::string kIPv6TetherCounters = Join(std::vector<std::string> {
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900244 "Chain natctrl_tether_counters (2 references)",
245 " pkts bytes target prot opt in out source destination",
246 " 10000 10000000 RETURN all wlan0 rmnet0 ::/0 ::/0",
247 " 20000 20000000 RETURN all rmnet0 wlan0 ::/0 ::/0",
248}, '\n');
249
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900250std::string readSocketClientResponse(int fd) {
251 char buf[32768];
252 ssize_t bytesRead = read(fd, buf, sizeof(buf));
253 if (bytesRead < 0) {
254 return "";
255 }
256 for (int i = 0; i < bytesRead; i++) {
257 if (buf[i] == '\0') buf[i] = '\n';
258 }
259 return std::string(buf, bytesRead);
260}
261
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900262void expectNoSocketClientResponse(int fd) {
263 char buf[64];
264 EXPECT_EQ(-1, read(fd, buf, sizeof(buf)));
265}
266
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900267TEST_F(BandwidthControllerTest, TestGetTetherStats) {
268 int socketPair[2];
269 ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM, 0, socketPair));
270 ASSERT_EQ(0, fcntl(socketPair[0], F_SETFL, O_NONBLOCK | fcntl(socketPair[0], F_GETFL)));
271 ASSERT_EQ(0, fcntl(socketPair[1], F_SETFL, O_NONBLOCK | fcntl(socketPair[1], F_GETFL)));
272 SocketClient cli(socketPair[0], false);
273
274 std::string err;
275 BandwidthController::TetherStats filter;
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900276
277 // If no filter is specified, both IPv4 and IPv6 counters must have at least one interface pair.
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900278 addIptablesRestoreOutput(kIPv4TetherCounters);
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900279 ASSERT_EQ(-1, mBw.getTetherStats(&cli, filter, err));
280 expectNoSocketClientResponse(socketPair[1]);
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900281 clearIptablesRestoreOutput();
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900282
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900283 addIptablesRestoreOutput(kIPv6TetherCounters);
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900284 ASSERT_EQ(-1, mBw.getTetherStats(&cli, filter, err));
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900285 clearIptablesRestoreOutput();
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900286
287 // IPv4 and IPv6 counters are properly added together.
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900288 addIptablesRestoreOutput(kIPv4TetherCounters, kIPv6TetherCounters);
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900289 filter = BandwidthController::TetherStats();
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900290 std::string expected =
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900291 "114 wlan0 rmnet0 10002373 10026 20002002 20027\n"
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900292 "114 bt-pan rmnet0 107471 1040 1708806 1450\n"
293 "200 Tethering stats list completed\n";
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900294 ASSERT_EQ(0, mBw.getTetherStats(&cli, filter, err));
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900295 ASSERT_EQ(expected, readSocketClientResponse(socketPair[1]));
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900296 expectNoSocketClientResponse(socketPair[1]);
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900297 clearIptablesRestoreOutput();
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900298
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900299 // Test filtering.
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900300 addIptablesRestoreOutput(kIPv4TetherCounters, kIPv6TetherCounters);
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900301 filter = BandwidthController::TetherStats("bt-pan", "rmnet0", -1, -1, -1, -1);
302 expected = "221 bt-pan rmnet0 107471 1040 1708806 1450\n";
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900303 ASSERT_EQ(0, mBw.getTetherStats(&cli, filter, err));
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900304 ASSERT_EQ(expected, readSocketClientResponse(socketPair[1]));
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900305 expectNoSocketClientResponse(socketPair[1]);
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900306 clearIptablesRestoreOutput();
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900307
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900308 addIptablesRestoreOutput(kIPv4TetherCounters, kIPv6TetherCounters);
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900309 filter = BandwidthController::TetherStats("wlan0", "rmnet0", -1, -1, -1, -1);
310 expected = "221 wlan0 rmnet0 10002373 10026 20002002 20027\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 Colittice6748a2017-02-02 01:34:33 +0900313 clearIptablesRestoreOutput();
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900314
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900315 // Select nonexistent interfaces.
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900316 addIptablesRestoreOutput(kIPv4TetherCounters, kIPv6TetherCounters);
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900317 filter = BandwidthController::TetherStats("rmnet0", "foo0", -1, -1, -1, -1);
318 expected = "200 Tethering stats list completed\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 Colitti750e8fc2016-07-12 01:19:49 +0900322
323 // No stats with a filter: no error.
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900324 addIptablesRestoreOutput("", "");
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900325 ASSERT_EQ(0, mBw.getTetherStats(&cli, filter, err));
326 ASSERT_EQ("200 Tethering stats list completed\n", readSocketClientResponse(socketPair[1]));
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900327 clearIptablesRestoreOutput();
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900328
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900329 addIptablesRestoreOutput("foo", "foo");
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900330 ASSERT_EQ(0, mBw.getTetherStats(&cli, filter, err));
331 ASSERT_EQ("200 Tethering stats list completed\n", readSocketClientResponse(socketPair[1]));
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900332 clearIptablesRestoreOutput();
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900333
334 // No stats and empty filter: error.
335 filter = BandwidthController::TetherStats();
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900336 addIptablesRestoreOutput("", kIPv6TetherCounters);
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900337 ASSERT_EQ(-1, mBw.getTetherStats(&cli, filter, err));
338 expectNoSocketClientResponse(socketPair[1]);
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900339 clearIptablesRestoreOutput();
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900340
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900341 addIptablesRestoreOutput(kIPv4TetherCounters, "");
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900342 ASSERT_EQ(-1, mBw.getTetherStats(&cli, filter, err));
343 expectNoSocketClientResponse(socketPair[1]);
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900344 clearIptablesRestoreOutput();
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900345
346 // Include only one pair of interfaces and things are fine.
347 std::vector<std::string> counterLines = android::base::Split(kIPv4TetherCounters, "\n");
348 std::vector<std::string> brokenCounterLines = counterLines;
349 counterLines.resize(4);
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900350 std::string counters = Join(counterLines, "\n") + "\n";
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900351 addIptablesRestoreOutput(counters, counters);
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900352 expected =
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900353 "114 wlan0 rmnet0 4746 52 4004 54\n"
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900354 "200 Tethering stats list completed\n";
355 ASSERT_EQ(0, mBw.getTetherStats(&cli, filter, err));
356 ASSERT_EQ(expected, readSocketClientResponse(socketPair[1]));
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900357 clearIptablesRestoreOutput();
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900358
359 // But if interfaces aren't paired, it's always an error.
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900360 err = "";
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900361 counterLines.resize(3);
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900362 counters = Join(counterLines, "\n") + "\n";
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900363 addIptablesRestoreOutput(counters, counters);
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900364 ASSERT_EQ(-1, mBw.getTetherStats(&cli, filter, err));
365 expectNoSocketClientResponse(socketPair[1]);
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900366 clearIptablesRestoreOutput();
367
368 // Token unit test of the fact that we return the stats in the error message which the caller
369 // ignores.
370 std::string expectedError = counters;
371 EXPECT_EQ(expectedError, err);
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900372
373 // popen() failing is always an error.
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900374 addIptablesRestoreOutput(kIPv4TetherCounters);
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900375 ASSERT_EQ(-1, mBw.getTetherStats(&cli, filter, err));
376 expectNoSocketClientResponse(socketPair[1]);
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900377 clearIptablesRestoreOutput();
378 addIptablesRestoreOutput(kIPv6TetherCounters);
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900379 ASSERT_EQ(-1, mBw.getTetherStats(&cli, filter, err));
380 expectNoSocketClientResponse(socketPair[1]);
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900381 clearIptablesRestoreOutput();
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900382}
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +0900383
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900384const std::vector<std::string> makeInterfaceQuotaCommands(const std::string& iface, int ruleIndex,
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +0900385 int64_t quota) {
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900386 const std::string chain = "bw_costly_" + iface;
387 const char* c_chain = chain.c_str();
388 const char* c_iface = iface.c_str();
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +0900389 std::vector<std::string> cmds = {
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900390 "*filter",
391 StringPrintf(":%s -", c_chain),
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900392 StringPrintf("-A %s -j bw_penalty_box", c_chain),
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900393 StringPrintf("-I bw_INPUT %d -i %s --jump %s", ruleIndex, c_iface, c_chain),
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900394 StringPrintf("-I bw_OUTPUT %d -o %s --jump %s", ruleIndex, c_iface, c_chain),
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900395 StringPrintf("-A bw_FORWARD -o %s --jump %s", c_iface, c_chain),
396 StringPrintf("-A %s -m quota2 ! --quota %" PRIu64 " --name %s --jump REJECT", c_chain,
397 quota, c_iface),
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900398 "COMMIT\n",
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +0900399 };
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900400 return {Join(cmds, "\n")};
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +0900401}
402
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900403const std::vector<std::string> removeInterfaceQuotaCommands(const std::string& iface) {
404 const std::string chain = "bw_costly_" + iface;
405 const char* c_chain = chain.c_str();
406 const char* c_iface = iface.c_str();
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +0900407 std::vector<std::string> cmds = {
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900408 "*filter",
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900409 StringPrintf("-D bw_INPUT -i %s --jump %s", c_iface, c_chain),
410 StringPrintf("-D bw_OUTPUT -o %s --jump %s", c_iface, c_chain),
411 StringPrintf("-D bw_FORWARD -o %s --jump %s", c_iface, c_chain),
412 StringPrintf("-F %s", c_chain),
413 StringPrintf("-X %s", c_chain),
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900414 "COMMIT\n",
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +0900415 };
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900416 return {Join(cmds, "\n")};
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +0900417}
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
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900424 EXPECT_EQ(0, mBw.setInterfaceQuota(iface, kOldQuota));
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900425 expectIptablesRestoreCommands(expected);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900426
427 constexpr uint64_t kNewQuota = kOldQuota + 1;
428 expected = {};
429 expectUpdateQuota(kNewQuota);
430 EXPECT_EQ(0, mBw.setInterfaceQuota(iface, kNewQuota));
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900431 expectIptablesRestoreCommands(expected);
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +0900432
433 expected = removeInterfaceQuotaCommands(iface);
434 EXPECT_EQ(0, mBw.removeInterfaceQuota(iface));
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900435 expectIptablesRestoreCommands(expected);
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +0900436}
Lorenzo Colittie8b56e42017-04-26 15:16:03 +0900437
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900438const std::vector<std::string> makeInterfaceSharedQuotaCommands(const std::string& iface,
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900439 int ruleIndex, int64_t quota,
440 bool insertQuota) {
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900441 const std::string chain = "bw_costly_shared";
442 const char* c_chain = chain.c_str();
443 const char* c_iface = iface.c_str();
444 std::vector<std::string> cmds = {
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900445 "*filter",
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900446 StringPrintf("-I bw_INPUT %d -i %s --jump %s", ruleIndex, c_iface, c_chain),
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900447 StringPrintf("-I bw_OUTPUT %d -o %s --jump %s", ruleIndex, c_iface, c_chain),
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900448 StringPrintf("-A bw_FORWARD -o %s --jump %s", c_iface, c_chain),
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900449 };
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900450 if (insertQuota) {
451 cmds.push_back(StringPrintf(
452 "-I %s -m quota2 ! --quota %" PRIu64 " --name shared --jump REJECT", c_chain, quota));
453 }
454 cmds.push_back("COMMIT\n");
455 return {Join(cmds, "\n")};
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900456}
457
458const std::vector<std::string> removeInterfaceSharedQuotaCommands(const std::string& iface,
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900459 int64_t quota, bool deleteQuota) {
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900460 const std::string chain = "bw_costly_shared";
461 const char* c_chain = chain.c_str();
462 const char* c_iface = iface.c_str();
463 std::vector<std::string> cmds = {
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900464 "*filter",
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900465 StringPrintf("-D bw_INPUT -i %s --jump %s", c_iface, c_chain),
466 StringPrintf("-D bw_OUTPUT -o %s --jump %s", c_iface, c_chain),
467 StringPrintf("-D bw_FORWARD -o %s --jump %s", c_iface, c_chain),
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900468 };
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900469 if (deleteQuota) {
470 cmds.push_back(StringPrintf(
471 "-D %s -m quota2 ! --quota %" PRIu64 " --name shared --jump REJECT", c_chain, quota));
472 }
473 cmds.push_back("COMMIT\n");
474 return {Join(cmds, "\n")};
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900475}
476
477TEST_F(BandwidthControllerTest, TestSetInterfaceSharedQuotaDuplicate) {
478 constexpr uint64_t kQuota = 123456;
479 const std::string iface = mTun.name();
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900480 std::vector<std::string> expected = makeInterfaceSharedQuotaCommands(iface, 1, 123456, true);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900481 EXPECT_EQ(0, mBw.setInterfaceSharedQuota(iface, kQuota));
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900482 expectIptablesRestoreCommands(expected);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900483
484 expected = {};
485 EXPECT_EQ(0, mBw.setInterfaceSharedQuota(iface, kQuota));
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900486 expectIptablesRestoreCommands(expected);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900487
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900488 expected = removeInterfaceSharedQuotaCommands(iface, kQuota, true);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900489 EXPECT_EQ(0, mBw.removeInterfaceSharedQuota(iface));
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900490 expectIptablesRestoreCommands(expected);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900491}
492
493TEST_F(BandwidthControllerTest, TestSetInterfaceSharedQuotaUpdate) {
494 constexpr uint64_t kOldQuota = 123456;
495 const std::string iface = mTun.name();
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900496 std::vector<std::string> expected = makeInterfaceSharedQuotaCommands(iface, 1, kOldQuota, true);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900497 EXPECT_EQ(0, mBw.setInterfaceSharedQuota(iface, kOldQuota));
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900498 expectIptablesRestoreCommands(expected);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900499
500 constexpr uint64_t kNewQuota = kOldQuota + 1;
501 expected = {};
502 expectUpdateQuota(kNewQuota);
503 EXPECT_EQ(0, mBw.setInterfaceSharedQuota(iface, kNewQuota));
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900504 expectIptablesRestoreCommands(expected);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900505
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900506 expected = removeInterfaceSharedQuotaCommands(iface, kNewQuota, true);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900507 EXPECT_EQ(0, mBw.removeInterfaceSharedQuota(iface));
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900508 expectIptablesRestoreCommands(expected);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900509}
510
511TEST_F(BandwidthControllerTest, TestSetInterfaceSharedQuotaTwoInterfaces) {
512 constexpr uint64_t kQuota = 123456;
513 const std::vector<std::string> ifaces{
514 {"a" + mTun.name()},
515 {"b" + mTun.name()},
516 };
517
518 for (const auto& iface : ifaces) {
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900519 // Quota rule is only added when the total number of
520 // interfaces transitions from 0 -> 1.
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900521 bool first = (iface == ifaces[0]);
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900522 auto expected = makeInterfaceSharedQuotaCommands(iface, 1, kQuota, first);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900523 EXPECT_EQ(0, mBw.setInterfaceSharedQuota(iface, kQuota));
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900524 expectIptablesRestoreCommands(expected);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900525 }
526
527 for (const auto& iface : ifaces) {
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900528 // Quota rule is only removed when the total number of
529 // interfaces transitions from 1 -> 0.
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900530 bool last = (iface == ifaces[1]);
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900531 auto expected = removeInterfaceSharedQuotaCommands(iface, kQuota, last);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900532 EXPECT_EQ(0, mBw.removeInterfaceSharedQuota(iface));
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900533 expectIptablesRestoreCommands(expected);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900534 }
535}
536
Lorenzo Colittie8b56e42017-04-26 15:16:03 +0900537TEST_F(BandwidthControllerTest, IptablesAlertCmd) {
538 std::vector<std::string> expected = {
Lorenzo Colitti3c272702017-04-26 15:48:13 +0900539 "*filter\n"
540 "-I bw_INPUT -m quota2 ! --quota 123456 --name MyWonderfulAlert\n"
541 "-I bw_OUTPUT -m quota2 ! --quota 123456 --name MyWonderfulAlert\n"
542 "COMMIT\n"
Lorenzo Colittie8b56e42017-04-26 15:16:03 +0900543 };
544 EXPECT_EQ(0, runIptablesAlertCmd(IptOp::IptOpInsert, "MyWonderfulAlert", 123456));
Lorenzo Colitti3c272702017-04-26 15:48:13 +0900545 expectIptablesRestoreCommands(expected);
Lorenzo Colittie8b56e42017-04-26 15:16:03 +0900546
547 expected = {
Lorenzo Colitti3c272702017-04-26 15:48:13 +0900548 "*filter\n"
549 "-D bw_INPUT -m quota2 ! --quota 123456 --name MyWonderfulAlert\n"
550 "-D bw_OUTPUT -m quota2 ! --quota 123456 --name MyWonderfulAlert\n"
551 "COMMIT\n"
Lorenzo Colittie8b56e42017-04-26 15:16:03 +0900552 };
553 EXPECT_EQ(0, runIptablesAlertCmd(IptOp::IptOpDelete, "MyWonderfulAlert", 123456));
Lorenzo Colitti3c272702017-04-26 15:48:13 +0900554 expectIptablesRestoreCommands(expected);
Lorenzo Colittie8b56e42017-04-26 15:16:03 +0900555}
556
557TEST_F(BandwidthControllerTest, IptablesAlertFwdCmd) {
558 std::vector<std::string> expected = {
Lorenzo Colitti3c272702017-04-26 15:48:13 +0900559 "*filter\n"
560 "-I bw_FORWARD -m quota2 ! --quota 123456 --name MyWonderfulAlert\n"
561 "COMMIT\n"
Lorenzo Colittie8b56e42017-04-26 15:16:03 +0900562 };
563 EXPECT_EQ(0, runIptablesAlertFwdCmd(IptOp::IptOpInsert, "MyWonderfulAlert", 123456));
Lorenzo Colitti3c272702017-04-26 15:48:13 +0900564 expectIptablesRestoreCommands(expected);
Lorenzo Colittie8b56e42017-04-26 15:16:03 +0900565
566 expected = {
Lorenzo Colitti3c272702017-04-26 15:48:13 +0900567 "*filter\n"
568 "-D 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::IptOpDelete, "MyWonderfulAlert", 123456));
Lorenzo Colitti3c272702017-04-26 15:48:13 +0900572 expectIptablesRestoreCommands(expected);
Lorenzo Colittie8b56e42017-04-26 15:16:03 +0900573}
Lorenzo Colittif4dfa682017-04-28 11:09:07 +0900574
575TEST_F(BandwidthControllerTest, ManipulateSpecialApps) {
576 std::vector<const char *> appUids = { "1000", "1001", "10012" };
577
578 std::vector<std::string> expected = {
Lorenzo Colitti911bc4c2017-04-28 14:34:01 +0900579 "*filter\n"
580 "-I bw_happy_box -m owner --uid-owner 1000 --jump RETURN\n"
581 "-I bw_happy_box -m owner --uid-owner 1001 --jump RETURN\n"
582 "-I bw_happy_box -m owner --uid-owner 10012 --jump RETURN\n"
583 "COMMIT\n"
Lorenzo Colittif4dfa682017-04-28 11:09:07 +0900584 };
585 EXPECT_EQ(0, mBw.addNiceApps(appUids.size(), const_cast<char**>(&appUids[0])));
Lorenzo Colitti911bc4c2017-04-28 14:34:01 +0900586 expectIptablesRestoreCommands(expected);
Lorenzo Colittif4dfa682017-04-28 11:09:07 +0900587
588 expected = {
Lorenzo Colitti911bc4c2017-04-28 14:34:01 +0900589 "*filter\n"
590 "-D bw_penalty_box -m owner --uid-owner 1000 --jump REJECT\n"
591 "-D bw_penalty_box -m owner --uid-owner 1001 --jump REJECT\n"
592 "-D bw_penalty_box -m owner --uid-owner 10012 --jump REJECT\n"
593 "COMMIT\n"
Lorenzo Colittif4dfa682017-04-28 11:09:07 +0900594 };
595 EXPECT_EQ(0, mBw.removeNaughtyApps(appUids.size(), const_cast<char**>(&appUids[0])));
Lorenzo Colitti911bc4c2017-04-28 14:34:01 +0900596 expectIptablesRestoreCommands(expected);
Lorenzo Colittif4dfa682017-04-28 11:09:07 +0900597}