blob: c6b21d8f31f461ac5f37aa6a49696e88d559513b [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
Lorenzo Colitti86a47982016-03-18 17:52:25 +090033#include "BandwidthController.h"
Lorenzo Colitti0f150552016-03-28 02:30:27 +090034#include "IptablesBaseTest.h"
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +090035#include "tun_interface.h"
36
37using android::base::StringPrintf;
38using android::net::TunInterface;
Lorenzo Colitti86a47982016-03-18 17:52:25 +090039
Lorenzo Colitti0f150552016-03-28 02:30:27 +090040class BandwidthControllerTest : public IptablesBaseTest {
Lorenzo Colitti86a47982016-03-18 17:52:25 +090041public:
42 BandwidthControllerTest() {
43 BandwidthController::execFunction = fake_android_fork_exec;
44 BandwidthController::popenFunction = fake_popen;
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +090045 BandwidthController::iptablesRestoreFunction = fakeExecIptablesRestoreWithOutput;
Lorenzo Colitti86a47982016-03-18 17:52:25 +090046 }
47 BandwidthController mBw;
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +090048 TunInterface mTun;
49
50 void SetUp() {
51 ASSERT_EQ(0, mTun.init());
52 }
53
54 void TearDown() {
55 mTun.destroy();
56 }
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +090057
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +090058 void addIptablesRestoreOutput(std::string contents) {
59 sIptablesRestoreOutput.push_back(contents);
60 }
61
Lorenzo Colittice6748a2017-02-02 01:34:33 +090062 void addIptablesRestoreOutput(std::string contents1, std::string contents2) {
63 sIptablesRestoreOutput.push_back(contents1);
64 sIptablesRestoreOutput.push_back(contents2);
65 }
66
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +090067 void clearIptablesRestoreOutput() {
68 sIptablesRestoreOutput.clear();
69 }
70
71 void expectSetupCommands(const std::string& expectedClean, std::string expectedAccounting) {
72 std::string expectedList =
73 "*filter\n"
74 "-S\n"
75 "COMMIT\n";
76
77 std::string expectedFlush =
78 "*filter\n"
79 ":bw_INPUT -\n"
80 ":bw_OUTPUT -\n"
81 ":bw_FORWARD -\n"
82 ":bw_happy_box -\n"
83 ":bw_penalty_box -\n"
84 ":bw_data_saver -\n"
85 ":bw_costly_shared -\n"
86 "COMMIT\n"
87 "*raw\n"
88 ":bw_raw_PREROUTING -\n"
89 "COMMIT\n"
90 "*mangle\n"
91 ":bw_mangle_POSTROUTING -\n"
92 "COMMIT\n";
93
94 ExpectedIptablesCommands expected = {{ V4, expectedList }};
95 if (expectedClean.size()) {
96 expected.push_back({ V4V6, expectedClean });
97 }
98 expected.push_back({ V4V6, expectedFlush });
99 if (expectedAccounting.size()) {
100 expected.push_back({ V4V6, expectedAccounting });
101 }
102
103 expectIptablesRestoreCommands(expected);
104 }
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900105};
106
Lorenzo Colittia0dc8a52016-03-26 22:42:07 +0900107TEST_F(BandwidthControllerTest, TestSetupIptablesHooks) {
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900108 // Pretend some bw_costly_shared_<iface> rules already exist...
109 addIptablesRestoreOutput(
110 "-P OUTPUT ACCEPT\n"
111 "-N bw_costly_rmnet_data0\n"
112 "-N bw_costly_shared\n"
113 "-N unrelated\n"
114 "-N bw_costly_rmnet_data7\n");
115
116 // ... and expect that they be flushed and deleted.
117 std::string expectedCleanCmds =
Lorenzo Colitti13debb82016-03-27 17:46:30 +0900118 "*filter\n"
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900119 ":bw_costly_rmnet_data0 -\n"
120 "-X bw_costly_rmnet_data0\n"
121 ":bw_costly_rmnet_data7 -\n"
122 "-X bw_costly_rmnet_data7\n"
123 "COMMIT\n";
124
125 mBw.setupIptablesHooks();
126 expectSetupCommands(expectedCleanCmds, "");
Lorenzo Colittia0dc8a52016-03-26 22:42:07 +0900127}
128
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900129TEST_F(BandwidthControllerTest, TestEnableBandwidthControl) {
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900130 // Pretend no bw_costly_shared_<iface> rules already exist...
131 addIptablesRestoreOutput(
132 "-P OUTPUT ACCEPT\n"
133 "-N bw_costly_shared\n"
134 "-N unrelated\n");
135
136 // ... so none are flushed or deleted.
137 std::string expectedClean = "";
138
139 std::string expectedAccounting =
Lorenzo Colitti13debb82016-03-27 17:46:30 +0900140 "*filter\n"
141 "-A bw_INPUT -m owner --socket-exists\n"
142 "-A bw_OUTPUT -m owner --socket-exists\n"
143 "-A bw_costly_shared --jump bw_penalty_box\n"
144 "-A bw_penalty_box --jump bw_happy_box\n"
145 "-A bw_happy_box --jump bw_data_saver\n"
146 "-A bw_data_saver -j RETURN\n"
147 "-I bw_happy_box -m owner --uid-owner 0-9999 --jump RETURN\n"
148 "COMMIT\n"
149 "*raw\n"
150 "-A bw_raw_PREROUTING -m owner --socket-exists\n"
151 "COMMIT\n"
152 "*mangle\n"
153 "-A bw_mangle_POSTROUTING -m owner --socket-exists\n"
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900154 "COMMIT\n";
Lorenzo Colitti13debb82016-03-27 17:46:30 +0900155
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900156 mBw.enableBandwidthControl(false);
157 expectSetupCommands(expectedClean, expectedAccounting);
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900158}
159
Lorenzo Colittia0dc8a52016-03-26 22:42:07 +0900160TEST_F(BandwidthControllerTest, TestDisableBandwidthControl) {
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900161 // Pretend some bw_costly_shared_<iface> rules already exist...
162 addIptablesRestoreOutput(
163 "-P OUTPUT ACCEPT\n"
164 "-N bw_costly_rmnet_data0\n"
165 "-N bw_costly_shared\n"
166 "-N unrelated\n"
167 "-N bw_costly_rmnet_data7\n");
168
169 // ... and expect that they be flushed.
170 std::string expectedCleanCmds =
Lorenzo Colitti13debb82016-03-27 17:46:30 +0900171 "*filter\n"
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900172 ":bw_costly_rmnet_data0 -\n"
173 ":bw_costly_rmnet_data7 -\n"
174 "COMMIT\n";
175
176 mBw.disableBandwidthControl();
177 expectSetupCommands(expectedCleanCmds, "");
Lorenzo Colittia0dc8a52016-03-26 22:42:07 +0900178}
179
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900180TEST_F(BandwidthControllerTest, TestEnableDataSaver) {
181 mBw.enableDataSaver(true);
182 std::vector<std::string> expected = {
Lorenzo Colitti464eabe2016-03-25 13:38:19 +0900183 "-R bw_data_saver 1 --jump REJECT",
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900184 };
185 expectIptablesCommands(expected);
186
187 mBw.enableDataSaver(false);
188 expected = {
Lorenzo Colitti464eabe2016-03-25 13:38:19 +0900189 "-R bw_data_saver 1 --jump RETURN",
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900190 };
191 expectIptablesCommands(expected);
192}
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900193
194std::string kIPv4TetherCounters = android::base::Join(std::vector<std::string> {
195 "Chain natctrl_tether_counters (4 references)",
196 " pkts bytes target prot opt in out source destination",
197 " 26 2373 RETURN all -- wlan0 rmnet0 0.0.0.0/0 0.0.0.0/0",
198 " 27 2002 RETURN all -- rmnet0 wlan0 0.0.0.0/0 0.0.0.0/0",
199 " 1040 107471 RETURN all -- bt-pan rmnet0 0.0.0.0/0 0.0.0.0/0",
200 " 1450 1708806 RETURN all -- rmnet0 bt-pan 0.0.0.0/0 0.0.0.0/0",
201}, '\n');
202
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900203std::string kIPv6TetherCounters = android::base::Join(std::vector<std::string> {
204 "Chain natctrl_tether_counters (2 references)",
205 " pkts bytes target prot opt in out source destination",
206 " 10000 10000000 RETURN all wlan0 rmnet0 ::/0 ::/0",
207 " 20000 20000000 RETURN all rmnet0 wlan0 ::/0 ::/0",
208}, '\n');
209
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900210std::string readSocketClientResponse(int fd) {
211 char buf[32768];
212 ssize_t bytesRead = read(fd, buf, sizeof(buf));
213 if (bytesRead < 0) {
214 return "";
215 }
216 for (int i = 0; i < bytesRead; i++) {
217 if (buf[i] == '\0') buf[i] = '\n';
218 }
219 return std::string(buf, bytesRead);
220}
221
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900222void expectNoSocketClientResponse(int fd) {
223 char buf[64];
224 EXPECT_EQ(-1, read(fd, buf, sizeof(buf)));
225}
226
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900227TEST_F(BandwidthControllerTest, TestGetTetherStats) {
228 int socketPair[2];
229 ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM, 0, socketPair));
230 ASSERT_EQ(0, fcntl(socketPair[0], F_SETFL, O_NONBLOCK | fcntl(socketPair[0], F_GETFL)));
231 ASSERT_EQ(0, fcntl(socketPair[1], F_SETFL, O_NONBLOCK | fcntl(socketPair[1], F_GETFL)));
232 SocketClient cli(socketPair[0], false);
233
234 std::string err;
235 BandwidthController::TetherStats filter;
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900236
237 // If no filter is specified, both IPv4 and IPv6 counters must have at least one interface pair.
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900238 addIptablesRestoreOutput(kIPv4TetherCounters);
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900239 ASSERT_EQ(-1, mBw.getTetherStats(&cli, filter, err));
240 expectNoSocketClientResponse(socketPair[1]);
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900241 clearIptablesRestoreOutput();
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900242
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900243 addIptablesRestoreOutput(kIPv6TetherCounters);
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900244 ASSERT_EQ(-1, mBw.getTetherStats(&cli, filter, err));
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900245 clearIptablesRestoreOutput();
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900246
247 // IPv4 and IPv6 counters are properly added together.
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900248 addIptablesRestoreOutput(kIPv4TetherCounters, kIPv6TetherCounters);
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900249 filter = BandwidthController::TetherStats();
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900250 std::string expected =
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900251 "114 wlan0 rmnet0 10002373 10026 20002002 20027\n"
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900252 "114 bt-pan rmnet0 107471 1040 1708806 1450\n"
253 "200 Tethering stats list completed\n";
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900254 ASSERT_EQ(0, mBw.getTetherStats(&cli, filter, err));
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900255 ASSERT_EQ(expected, readSocketClientResponse(socketPair[1]));
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900256 expectNoSocketClientResponse(socketPair[1]);
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900257 clearIptablesRestoreOutput();
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900258
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900259 // Test filtering.
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900260 addIptablesRestoreOutput(kIPv4TetherCounters, kIPv6TetherCounters);
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900261 filter = BandwidthController::TetherStats("bt-pan", "rmnet0", -1, -1, -1, -1);
262 expected = "221 bt-pan rmnet0 107471 1040 1708806 1450\n";
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900263 ASSERT_EQ(0, mBw.getTetherStats(&cli, filter, err));
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900264 ASSERT_EQ(expected, readSocketClientResponse(socketPair[1]));
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900265 expectNoSocketClientResponse(socketPair[1]);
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900266 clearIptablesRestoreOutput();
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900267
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900268 addIptablesRestoreOutput(kIPv4TetherCounters, kIPv6TetherCounters);
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900269 filter = BandwidthController::TetherStats("wlan0", "rmnet0", -1, -1, -1, -1);
270 expected = "221 wlan0 rmnet0 10002373 10026 20002002 20027\n";
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900271 ASSERT_EQ(0, mBw.getTetherStats(&cli, filter, err));
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900272 ASSERT_EQ(expected, readSocketClientResponse(socketPair[1]));
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900273 clearIptablesRestoreOutput();
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900274
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900275 // Select nonexistent interfaces.
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900276 addIptablesRestoreOutput(kIPv4TetherCounters, kIPv6TetherCounters);
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900277 filter = BandwidthController::TetherStats("rmnet0", "foo0", -1, -1, -1, -1);
278 expected = "200 Tethering stats list completed\n";
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900279 ASSERT_EQ(0, mBw.getTetherStats(&cli, filter, err));
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900280 ASSERT_EQ(expected, readSocketClientResponse(socketPair[1]));
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900281 clearIptablesRestoreOutput();
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900282
283 // No stats with a filter: no error.
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900284 addIptablesRestoreOutput("", "");
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900285 ASSERT_EQ(0, mBw.getTetherStats(&cli, filter, err));
286 ASSERT_EQ("200 Tethering stats list completed\n", readSocketClientResponse(socketPair[1]));
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900287 clearIptablesRestoreOutput();
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900288
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900289 addIptablesRestoreOutput("foo", "foo");
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900290 ASSERT_EQ(0, mBw.getTetherStats(&cli, filter, err));
291 ASSERT_EQ("200 Tethering stats list completed\n", readSocketClientResponse(socketPair[1]));
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900292 clearIptablesRestoreOutput();
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900293
294 // No stats and empty filter: error.
295 filter = BandwidthController::TetherStats();
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900296 addIptablesRestoreOutput("", kIPv6TetherCounters);
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900297 ASSERT_EQ(-1, mBw.getTetherStats(&cli, filter, err));
298 expectNoSocketClientResponse(socketPair[1]);
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900299 clearIptablesRestoreOutput();
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900300
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900301 addIptablesRestoreOutput(kIPv4TetherCounters, "");
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900302 ASSERT_EQ(-1, mBw.getTetherStats(&cli, filter, err));
303 expectNoSocketClientResponse(socketPair[1]);
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900304 clearIptablesRestoreOutput();
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900305
306 // Include only one pair of interfaces and things are fine.
307 std::vector<std::string> counterLines = android::base::Split(kIPv4TetherCounters, "\n");
308 std::vector<std::string> brokenCounterLines = counterLines;
309 counterLines.resize(4);
310 std::string counters = android::base::Join(counterLines, "\n") + "\n";
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900311 addIptablesRestoreOutput(counters, counters);
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900312 expected =
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900313 "114 wlan0 rmnet0 4746 52 4004 54\n"
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900314 "200 Tethering stats list completed\n";
315 ASSERT_EQ(0, mBw.getTetherStats(&cli, filter, err));
316 ASSERT_EQ(expected, readSocketClientResponse(socketPair[1]));
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900317 clearIptablesRestoreOutput();
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900318
319 // But if interfaces aren't paired, it's always an error.
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900320 err = "";
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900321 counterLines.resize(3);
322 counters = android::base::Join(counterLines, "\n") + "\n";
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900323 addIptablesRestoreOutput(counters, counters);
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900324 ASSERT_EQ(-1, mBw.getTetherStats(&cli, filter, err));
325 expectNoSocketClientResponse(socketPair[1]);
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900326 clearIptablesRestoreOutput();
327
328 // Token unit test of the fact that we return the stats in the error message which the caller
329 // ignores.
330 std::string expectedError = counters;
331 EXPECT_EQ(expectedError, err);
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900332
333 // popen() failing is always an error.
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900334 addIptablesRestoreOutput(kIPv4TetherCounters);
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900335 ASSERT_EQ(-1, mBw.getTetherStats(&cli, filter, err));
336 expectNoSocketClientResponse(socketPair[1]);
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900337 clearIptablesRestoreOutput();
338 addIptablesRestoreOutput(kIPv6TetherCounters);
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900339 ASSERT_EQ(-1, mBw.getTetherStats(&cli, filter, err));
340 expectNoSocketClientResponse(socketPair[1]);
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900341 clearIptablesRestoreOutput();
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900342}
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +0900343
344const std::vector<std::string> makeInterfaceQuotaCommands(const char *iface, int ruleIndex,
345 int64_t quota) {
346 std::vector<std::string> cmds = {
347 StringPrintf("-F bw_costly_%s", iface),
348 StringPrintf("-N bw_costly_%s", iface),
349 StringPrintf("-A bw_costly_%s -j bw_penalty_box", iface),
350 StringPrintf("-D bw_INPUT -i %s --jump bw_costly_%s", iface, iface),
351 StringPrintf("-I bw_INPUT %d -i %s --jump bw_costly_%s", ruleIndex, iface, iface),
352 StringPrintf("-D bw_OUTPUT -o %s --jump bw_costly_%s", iface, iface),
353 StringPrintf("-I bw_OUTPUT %d -o %s --jump bw_costly_%s", ruleIndex, iface, iface),
354 StringPrintf("-D bw_FORWARD -o %s --jump bw_costly_%s", iface, iface),
355 StringPrintf("-A bw_FORWARD -o %s --jump bw_costly_%s", iface, iface),
356 StringPrintf("-A bw_costly_%s -m quota2 ! --quota %" PRIu64 " --name %s --jump REJECT",
357 iface, quota, iface),
358 };
359 return cmds;
360}
361
362const std::vector<std::string> removeInterfaceQuotaCommands(const char *iface) {
363 std::vector<std::string> cmds = {
364 StringPrintf("-D bw_INPUT -i %s --jump bw_costly_%s", iface, iface),
365 StringPrintf("-D bw_OUTPUT -o %s --jump bw_costly_%s", iface, iface),
366 StringPrintf("-D bw_FORWARD -o %s --jump bw_costly_%s", iface, iface),
367 StringPrintf("-F bw_costly_%s", iface),
368 StringPrintf("-X bw_costly_%s", iface),
369 };
370 return cmds;
371}
372
373TEST_F(BandwidthControllerTest, TestSetInterfaceQuota) {
374 const char *iface = mTun.name().c_str();
375 std::vector<std::string> expected = makeInterfaceQuotaCommands(iface, 1, 123456);
376
377 // prepCostlyInterface assumes that exactly one of the "-F chain" and "-N chain" commands fails.
378 // So pretend that the first two commands (the IPv4 -F and the IPv6 -F) fail.
379 std::deque<int> returnValues(expected.size() * 2, 0);
380 returnValues[0] = 1;
381 returnValues[1] = 1;
382 setReturnValues(returnValues);
383
384 EXPECT_EQ(0, mBw.setInterfaceQuota(iface, 123456));
385 expectIptablesCommands(expected);
386
387 expected = removeInterfaceQuotaCommands(iface);
388 EXPECT_EQ(0, mBw.removeInterfaceQuota(iface));
389 expectIptablesCommands(expected);
390}