blob: 115a0da5411a88d3b18f4abaa7a0473a2415a69d [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"
Benedict Wongb9baf262017-12-03 15:43:08 -080035#include "Fwmark.h"
Lorenzo Colitti0f150552016-03-28 02:30:27 +090036#include "IptablesBaseTest.h"
Chenbo Fenga121e202018-03-19 11:51:54 -070037#include "bpf/BpfUtils.h"
Chenbo Fengd6104d12018-10-16 20:29:29 -070038#include "netdbpf/bpf_shared.h"
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +090039#include "tun_interface.h"
40
Bernie Innocentia5161a02019-01-30 22:40:53 +090041using ::testing::_;
Joel Scherpelz01cc5492017-06-16 10:45:14 +090042using ::testing::ByMove;
43using ::testing::Invoke;
44using ::testing::Return;
45using ::testing::StrictMock;
Joel Scherpelz01cc5492017-06-16 10:45:14 +090046
Lorenzo Colitti48f83002017-07-06 15:06:04 +090047using android::base::Join;
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +090048using android::base::StringPrintf;
49using android::net::TunInterface;
Joel Scherpelz01cc5492017-06-16 10:45:14 +090050using android::netdutils::UniqueFile;
Bernie Innocentia5161a02019-01-30 22:40:53 +090051using android::netdutils::status::ok;
Lorenzo Colitti86a47982016-03-18 17:52:25 +090052
Chenbo Feng44c0f442018-07-10 16:54:30 -070053const std::string ACCOUNT_RULES_WITHOUT_BPF =
54 "*filter\n"
Luke Huangae038f82018-11-05 11:17:31 +090055 "-A bw_INPUT -j bw_global_alert\n"
Chenbo Feng44c0f442018-07-10 16:54:30 -070056 "-A bw_INPUT -p esp -j RETURN\n"
57 "-A bw_INPUT -m mark --mark 0x100000/0x100000 -j RETURN\n"
58 "-A bw_INPUT -m owner --socket-exists\n"
59 "-A bw_INPUT -j MARK --or-mark 0x100000\n"
Luke Huangae038f82018-11-05 11:17:31 +090060 "-A bw_OUTPUT -j bw_global_alert\n"
Chenbo Feng44c0f442018-07-10 16:54:30 -070061 "-A bw_OUTPUT -o ipsec+ -j RETURN\n"
62 "-A bw_OUTPUT -m policy --pol ipsec --dir out -j RETURN\n"
Maciej Żenczykowskied20ec32020-05-28 03:22:28 +000063 "-A bw_OUTPUT -m owner --uid-owner clat -j RETURN\n"
Chenbo Feng44c0f442018-07-10 16:54:30 -070064 "-A bw_OUTPUT -m owner --socket-exists\n"
Maciej Żenczykowskidec83c72019-12-24 15:27:14 -080065 "-A bw_costly_shared -j bw_penalty_box\n"
Chenbo Feng44c0f442018-07-10 16:54:30 -070066 "\n"
Maciej Żenczykowskidec83c72019-12-24 15:27:14 -080067 "-A bw_penalty_box -j bw_happy_box\n"
68 "-A bw_happy_box -j bw_data_saver\n"
Chenbo Feng44c0f442018-07-10 16:54:30 -070069 "-A bw_data_saver -j RETURN\n"
Maciej Żenczykowskidec83c72019-12-24 15:27:14 -080070 "-I bw_happy_box -m owner --uid-owner 0-9999 -j RETURN\n"
Chenbo Feng44c0f442018-07-10 16:54:30 -070071 "COMMIT\n"
72 "*raw\n"
73 "-A bw_raw_PREROUTING -i ipsec+ -j RETURN\n"
74 "-A bw_raw_PREROUTING -m policy --pol ipsec --dir in -j RETURN\n"
75 "-A bw_raw_PREROUTING -m owner --socket-exists\n"
76 "COMMIT\n"
77 "*mangle\n"
78 "-A bw_mangle_POSTROUTING -o ipsec+ -j RETURN\n"
79 "-A bw_mangle_POSTROUTING -m policy --pol ipsec --dir out -j RETURN\n"
Chenbo Feng44c0f442018-07-10 16:54:30 -070080 "-A bw_mangle_POSTROUTING -j MARK --set-mark 0x0/0x100000\n"
Maciej Żenczykowskie408f882020-05-28 03:22:23 +000081 "-A bw_mangle_POSTROUTING -m owner --uid-owner clat -j RETURN\n"
Maciej Żenczykowski75d75902020-05-27 23:03:41 +000082 "-A bw_mangle_POSTROUTING -m owner --socket-exists\n"
Chenbo Feng44c0f442018-07-10 16:54:30 -070083 "COMMIT\n";
84
85const std::string ACCOUNT_RULES_WITH_BPF =
86 "*filter\n"
Luke Huangae038f82018-11-05 11:17:31 +090087 "-A bw_INPUT -j bw_global_alert\n"
Chenbo Feng44c0f442018-07-10 16:54:30 -070088 "-A bw_INPUT -p esp -j RETURN\n"
89 "-A bw_INPUT -m mark --mark 0x100000/0x100000 -j RETURN\n"
90 "\n"
91 "-A bw_INPUT -j MARK --or-mark 0x100000\n"
Luke Huangae038f82018-11-05 11:17:31 +090092 "-A bw_OUTPUT -j bw_global_alert\n"
Maciej Żenczykowskied20ec32020-05-28 03:22:28 +000093 "\n"
94 "\n"
95 "\n"
Chenbo Feng44c0f442018-07-10 16:54:30 -070096 "\n"
Maciej Żenczykowskidec83c72019-12-24 15:27:14 -080097 "-A bw_costly_shared -j bw_penalty_box\n" +
Chenbo Feng44c0f442018-07-10 16:54:30 -070098 StringPrintf("-I bw_penalty_box -m bpf --object-pinned %s -j REJECT\n",
99 XT_BPF_BLACKLIST_PROG_PATH) +
Maciej Żenczykowskidec83c72019-12-24 15:27:14 -0800100 "-A bw_penalty_box -j bw_happy_box\n"
101 "-A bw_happy_box -j bw_data_saver\n"
Chenbo Feng44c0f442018-07-10 16:54:30 -0700102 "-A bw_data_saver -j RETURN\n" +
103 StringPrintf("-I bw_happy_box -m bpf --object-pinned %s -j RETURN\n",
104 XT_BPF_WHITELIST_PROG_PATH) +
105 "COMMIT\n"
106 "*raw\n"
107 "-A bw_raw_PREROUTING -i ipsec+ -j RETURN\n"
108 "-A bw_raw_PREROUTING -m policy --pol ipsec --dir in -j RETURN\n" +
109 StringPrintf("-A bw_raw_PREROUTING -m bpf --object-pinned %s\n", XT_BPF_INGRESS_PROG_PATH) +
110 "COMMIT\n"
111 "*mangle\n"
112 "-A bw_mangle_POSTROUTING -o ipsec+ -j RETURN\n"
113 "-A bw_mangle_POSTROUTING -m policy --pol ipsec --dir out -j RETURN\n"
Maciej Żenczykowski75d75902020-05-27 23:03:41 +0000114 "-A bw_mangle_POSTROUTING -j MARK --set-mark 0x0/0x100000\n"
Maciej Żenczykowskie408f882020-05-28 03:22:23 +0000115 "-A bw_mangle_POSTROUTING -m owner --uid-owner clat -j RETURN\n" +
Chenbo Feng44c0f442018-07-10 16:54:30 -0700116 StringPrintf("-A bw_mangle_POSTROUTING -m bpf --object-pinned %s\n",
117 XT_BPF_EGRESS_PROG_PATH) +
118 "COMMIT\n";
119
Lorenzo Colitti0f150552016-03-28 02:30:27 +0900120class BandwidthControllerTest : public IptablesBaseTest {
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900121protected:
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900122 BandwidthControllerTest() {
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900123 BandwidthController::iptablesRestoreFunction = fakeExecIptablesRestoreWithOutput;
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900124 }
125 BandwidthController mBw;
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +0900126 TunInterface mTun;
127
128 void SetUp() {
129 ASSERT_EQ(0, mTun.init());
130 }
131
132 void TearDown() {
133 mTun.destroy();
134 }
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900135
Luke Huangae038f82018-11-05 11:17:31 +0900136 void expectSetupCommands(const std::string& expectedClean,
137 const std::string& expectedAccounting) {
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900138 std::string expectedList =
139 "*filter\n"
140 "-S\n"
141 "COMMIT\n";
142
143 std::string expectedFlush =
Luke Huangae038f82018-11-05 11:17:31 +0900144 "*filter\n"
145 ":bw_INPUT -\n"
146 ":bw_OUTPUT -\n"
147 ":bw_FORWARD -\n"
148 ":bw_happy_box -\n"
149 ":bw_penalty_box -\n"
150 ":bw_data_saver -\n"
151 ":bw_costly_shared -\n"
152 ":bw_global_alert -\n"
153 "COMMIT\n"
154 "*raw\n"
155 ":bw_raw_PREROUTING -\n"
156 "COMMIT\n"
157 "*mangle\n"
158 ":bw_mangle_POSTROUTING -\n"
159 "COMMIT\n";
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900160
161 ExpectedIptablesCommands expected = {{ V4, expectedList }};
162 if (expectedClean.size()) {
163 expected.push_back({ V4V6, expectedClean });
164 }
165 expected.push_back({ V4V6, expectedFlush });
166 if (expectedAccounting.size()) {
167 expected.push_back({ V4V6, expectedAccounting });
168 }
169
170 expectIptablesRestoreCommands(expected);
171 }
Lorenzo Colittie8b56e42017-04-26 15:16:03 +0900172
173 using IptOp = BandwidthController::IptOp;
174
Luke Huangae038f82018-11-05 11:17:31 +0900175 int runIptablesAlertCmd(IptOp a, const char* b, int64_t c) {
Lorenzo Colittie8b56e42017-04-26 15:16:03 +0900176 return mBw.runIptablesAlertCmd(a, b, c);
177 }
178
Bernie Innocenti7e25ec02018-07-02 19:32:17 +0900179 int setCostlyAlert(const std::string& a, int64_t b, int64_t* c) {
Lorenzo Colitti38078222017-07-06 17:27:23 +0900180 return mBw.setCostlyAlert(a, b, c);
181 }
182
Bernie Innocenti7e25ec02018-07-02 19:32:17 +0900183 int removeCostlyAlert(const std::string& a, int64_t* b) { return mBw.removeCostlyAlert(a, b); }
Lorenzo Colitti38078222017-07-06 17:27:23 +0900184
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900185 void expectUpdateQuota(uint64_t quota) {
186 uintptr_t dummy;
187 FILE* dummyFile = reinterpret_cast<FILE*>(&dummy);
188
189 EXPECT_CALL(mSyscalls, fopen(_, _)).WillOnce(Return(ByMove(UniqueFile(dummyFile))));
190 EXPECT_CALL(mSyscalls, vfprintf(dummyFile, _, _))
191 .WillOnce(Invoke([quota](FILE*, const std::string&, va_list ap) {
192 EXPECT_EQ(quota, va_arg(ap, uint64_t));
193 return 0;
194 }));
195 EXPECT_CALL(mSyscalls, fclose(dummyFile)).WillOnce(Return(ok));
196 }
197
Chenbo Feng44c0f442018-07-10 16:54:30 -0700198 void checkBandwithControl(bool useBpf) {
199 // Pretend no bw_costly_shared_<iface> rules already exist...
200 addIptablesRestoreOutput(
201 "-P OUTPUT ACCEPT\n"
202 "-N bw_costly_shared\n"
203 "-N unrelated\n");
204
205 // ... so none are flushed or deleted.
206 std::string expectedClean = "";
207
208 std::string expectedAccounting =
209 useBpf ? ACCOUNT_RULES_WITH_BPF : ACCOUNT_RULES_WITHOUT_BPF;
210 mBw.setBpfEnabled(useBpf);
Luke Huangf44a3c12018-09-07 12:10:12 +0800211 mBw.enableBandwidthControl();
Chenbo Feng44c0f442018-07-10 16:54:30 -0700212 expectSetupCommands(expectedClean, expectedAccounting);
213 }
214
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900215 StrictMock<android::netdutils::ScopedMockSyscalls> mSyscalls;
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900216};
217
Lorenzo Colittia0dc8a52016-03-26 22:42:07 +0900218TEST_F(BandwidthControllerTest, TestSetupIptablesHooks) {
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900219 // Pretend some bw_costly_shared_<iface> rules already exist...
220 addIptablesRestoreOutput(
221 "-P OUTPUT ACCEPT\n"
222 "-N bw_costly_rmnet_data0\n"
223 "-N bw_costly_shared\n"
224 "-N unrelated\n"
225 "-N bw_costly_rmnet_data7\n");
226
227 // ... and expect that they be flushed and deleted.
228 std::string expectedCleanCmds =
Lorenzo Colitti13debb82016-03-27 17:46:30 +0900229 "*filter\n"
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900230 ":bw_costly_rmnet_data0 -\n"
231 "-X bw_costly_rmnet_data0\n"
232 ":bw_costly_rmnet_data7 -\n"
233 "-X bw_costly_rmnet_data7\n"
234 "COMMIT\n";
235
236 mBw.setupIptablesHooks();
237 expectSetupCommands(expectedCleanCmds, "");
Lorenzo Colittia0dc8a52016-03-26 22:42:07 +0900238}
239
Benedict Wongb9baf262017-12-03 15:43:08 -0800240TEST_F(BandwidthControllerTest, TestCheckUidBillingMask) {
241 uint32_t uidBillingMask = Fwmark::getUidBillingMask();
242
243 // If mask is non-zero, and mask & mask-1 is equal to 0, then the mask is a power of two.
244 bool isPowerOfTwo = uidBillingMask && (uidBillingMask & (uidBillingMask - 1)) == 0;
245
246 // Must be exactly a power of two
247 EXPECT_TRUE(isPowerOfTwo);
248}
249
Chenbo Feng44c0f442018-07-10 16:54:30 -0700250TEST_F(BandwidthControllerTest, TestEnableBandwidthControlWithBpf) {
251 checkBandwithControl(true);
252}
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900253
Chenbo Feng44c0f442018-07-10 16:54:30 -0700254TEST_F(BandwidthControllerTest, TestEnableBandwidthControlWithoutBpf) {
255 checkBandwithControl(false);
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900256}
257
Lorenzo Colittia0dc8a52016-03-26 22:42:07 +0900258TEST_F(BandwidthControllerTest, TestDisableBandwidthControl) {
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900259 // Pretend some bw_costly_shared_<iface> rules already exist...
260 addIptablesRestoreOutput(
261 "-P OUTPUT ACCEPT\n"
262 "-N bw_costly_rmnet_data0\n"
263 "-N bw_costly_shared\n"
264 "-N unrelated\n"
265 "-N bw_costly_rmnet_data7\n");
266
267 // ... and expect that they be flushed.
268 std::string expectedCleanCmds =
Lorenzo Colitti13debb82016-03-27 17:46:30 +0900269 "*filter\n"
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900270 ":bw_costly_rmnet_data0 -\n"
271 ":bw_costly_rmnet_data7 -\n"
272 "COMMIT\n";
273
274 mBw.disableBandwidthControl();
275 expectSetupCommands(expectedCleanCmds, "");
Lorenzo Colittia0dc8a52016-03-26 22:42:07 +0900276}
277
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900278TEST_F(BandwidthControllerTest, TestEnableDataSaver) {
279 mBw.enableDataSaver(true);
Lorenzo Colittiaff28792017-09-26 17:46:18 +0900280 std::string expected4 =
Maciej Żenczykowskidec83c72019-12-24 15:27:14 -0800281 "*filter\n"
282 ":bw_data_saver -\n"
283 "-A bw_data_saver -j REJECT\n"
284 "COMMIT\n";
Lorenzo Colittiaff28792017-09-26 17:46:18 +0900285 std::string expected6 =
Maciej Żenczykowskidec83c72019-12-24 15:27:14 -0800286 "*filter\n"
287 ":bw_data_saver -\n"
288 "-A bw_data_saver -p icmpv6 --icmpv6-type packet-too-big -j RETURN\n"
289 "-A bw_data_saver -p icmpv6 --icmpv6-type router-solicitation -j RETURN\n"
290 "-A bw_data_saver -p icmpv6 --icmpv6-type router-advertisement -j RETURN\n"
291 "-A bw_data_saver -p icmpv6 --icmpv6-type neighbour-solicitation -j RETURN\n"
292 "-A bw_data_saver -p icmpv6 --icmpv6-type neighbour-advertisement -j RETURN\n"
293 "-A bw_data_saver -p icmpv6 --icmpv6-type redirect -j RETURN\n"
294 "-A bw_data_saver -j REJECT\n"
295 "COMMIT\n";
Lorenzo Colittiaff28792017-09-26 17:46:18 +0900296 expectIptablesRestoreCommands({
297 {V4, expected4},
298 {V6, expected6},
299 });
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900300
301 mBw.enableDataSaver(false);
Lorenzo Colittiaff28792017-09-26 17:46:18 +0900302 std::string expected = {
Maciej Żenczykowskidec83c72019-12-24 15:27:14 -0800303 "*filter\n"
304 ":bw_data_saver -\n"
305 "-A bw_data_saver -j RETURN\n"
306 "COMMIT\n"};
Lorenzo Colittiaff28792017-09-26 17:46:18 +0900307 expectIptablesRestoreCommands({
308 {V4, expected},
309 {V6, expected},
310 });
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900311}
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900312
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900313const std::vector<std::string> makeInterfaceQuotaCommands(const std::string& iface, int ruleIndex,
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +0900314 int64_t quota) {
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900315 const std::string chain = "bw_costly_" + iface;
316 const char* c_chain = chain.c_str();
317 const char* c_iface = iface.c_str();
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +0900318 std::vector<std::string> cmds = {
Maciej Żenczykowskidec83c72019-12-24 15:27:14 -0800319 "*filter",
320 StringPrintf(":%s -", c_chain),
321 StringPrintf("-A %s -j bw_penalty_box", c_chain),
322 StringPrintf("-I bw_INPUT %d -i %s -j %s", ruleIndex, c_iface, c_chain),
323 StringPrintf("-I bw_OUTPUT %d -o %s -j %s", ruleIndex, c_iface, c_chain),
324 StringPrintf("-A bw_FORWARD -i %s -j %s", c_iface, c_chain),
325 StringPrintf("-A bw_FORWARD -o %s -j %s", c_iface, c_chain),
326 StringPrintf("-A %s -m quota2 ! --quota %" PRIu64 " --name %s -j REJECT", c_chain,
327 quota, c_iface),
328 "COMMIT\n",
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +0900329 };
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900330 return {Join(cmds, "\n")};
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +0900331}
332
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900333const std::vector<std::string> removeInterfaceQuotaCommands(const std::string& iface) {
334 const std::string chain = "bw_costly_" + iface;
335 const char* c_chain = chain.c_str();
336 const char* c_iface = iface.c_str();
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +0900337 std::vector<std::string> cmds = {
Maciej Żenczykowskidec83c72019-12-24 15:27:14 -0800338 "*filter",
339 StringPrintf("-D bw_INPUT -i %s -j %s", c_iface, c_chain),
340 StringPrintf("-D bw_OUTPUT -o %s -j %s", c_iface, c_chain),
341 StringPrintf("-D bw_FORWARD -i %s -j %s", c_iface, c_chain),
342 StringPrintf("-D bw_FORWARD -o %s -j %s", c_iface, c_chain),
343 StringPrintf("-F %s", c_chain),
344 StringPrintf("-X %s", c_chain),
345 "COMMIT\n",
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +0900346 };
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900347 return {Join(cmds, "\n")};
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +0900348}
349
350TEST_F(BandwidthControllerTest, TestSetInterfaceQuota) {
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900351 constexpr uint64_t kOldQuota = 123456;
352 const std::string iface = mTun.name();
353 std::vector<std::string> expected = makeInterfaceQuotaCommands(iface, 1, kOldQuota);
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +0900354
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900355 EXPECT_EQ(0, mBw.setInterfaceQuota(iface, kOldQuota));
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900356 expectIptablesRestoreCommands(expected);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900357
358 constexpr uint64_t kNewQuota = kOldQuota + 1;
359 expected = {};
360 expectUpdateQuota(kNewQuota);
361 EXPECT_EQ(0, mBw.setInterfaceQuota(iface, kNewQuota));
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900362 expectIptablesRestoreCommands(expected);
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +0900363
364 expected = removeInterfaceQuotaCommands(iface);
365 EXPECT_EQ(0, mBw.removeInterfaceQuota(iface));
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900366 expectIptablesRestoreCommands(expected);
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +0900367}
Lorenzo Colittie8b56e42017-04-26 15:16:03 +0900368
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900369const std::vector<std::string> makeInterfaceSharedQuotaCommands(const std::string& iface,
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900370 int ruleIndex, int64_t quota,
371 bool insertQuota) {
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900372 const std::string chain = "bw_costly_shared";
373 const char* c_chain = chain.c_str();
374 const char* c_iface = iface.c_str();
375 std::vector<std::string> cmds = {
Maciej Żenczykowskidec83c72019-12-24 15:27:14 -0800376 "*filter",
377 StringPrintf("-I bw_INPUT %d -i %s -j %s", ruleIndex, c_iface, c_chain),
378 StringPrintf("-I bw_OUTPUT %d -o %s -j %s", ruleIndex, c_iface, c_chain),
379 StringPrintf("-A bw_FORWARD -i %s -j %s", c_iface, c_chain),
380 StringPrintf("-A bw_FORWARD -o %s -j %s", c_iface, c_chain),
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900381 };
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900382 if (insertQuota) {
Maciej Żenczykowskidec83c72019-12-24 15:27:14 -0800383 cmds.push_back(StringPrintf("-I %s -m quota2 ! --quota %" PRIu64 " --name shared -j REJECT",
384 c_chain, quota));
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900385 }
386 cmds.push_back("COMMIT\n");
387 return {Join(cmds, "\n")};
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900388}
389
390const std::vector<std::string> removeInterfaceSharedQuotaCommands(const std::string& iface,
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900391 int64_t quota, bool deleteQuota) {
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900392 const std::string chain = "bw_costly_shared";
393 const char* c_chain = chain.c_str();
394 const char* c_iface = iface.c_str();
395 std::vector<std::string> cmds = {
Maciej Żenczykowskidec83c72019-12-24 15:27:14 -0800396 "*filter",
397 StringPrintf("-D bw_INPUT -i %s -j %s", c_iface, c_chain),
398 StringPrintf("-D bw_OUTPUT -o %s -j %s", c_iface, c_chain),
399 StringPrintf("-D bw_FORWARD -i %s -j %s", c_iface, c_chain),
400 StringPrintf("-D bw_FORWARD -o %s -j %s", c_iface, c_chain),
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900401 };
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900402 if (deleteQuota) {
Maciej Żenczykowskidec83c72019-12-24 15:27:14 -0800403 cmds.push_back(StringPrintf("-D %s -m quota2 ! --quota %" PRIu64 " --name shared -j REJECT",
404 c_chain, quota));
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900405 }
406 cmds.push_back("COMMIT\n");
407 return {Join(cmds, "\n")};
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900408}
409
410TEST_F(BandwidthControllerTest, TestSetInterfaceSharedQuotaDuplicate) {
411 constexpr uint64_t kQuota = 123456;
412 const std::string iface = mTun.name();
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900413 std::vector<std::string> expected = makeInterfaceSharedQuotaCommands(iface, 1, 123456, true);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900414 EXPECT_EQ(0, mBw.setInterfaceSharedQuota(iface, kQuota));
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900415 expectIptablesRestoreCommands(expected);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900416
417 expected = {};
418 EXPECT_EQ(0, mBw.setInterfaceSharedQuota(iface, kQuota));
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900419 expectIptablesRestoreCommands(expected);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900420
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900421 expected = removeInterfaceSharedQuotaCommands(iface, kQuota, true);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900422 EXPECT_EQ(0, mBw.removeInterfaceSharedQuota(iface));
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900423 expectIptablesRestoreCommands(expected);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900424}
425
426TEST_F(BandwidthControllerTest, TestSetInterfaceSharedQuotaUpdate) {
427 constexpr uint64_t kOldQuota = 123456;
428 const std::string iface = mTun.name();
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900429 std::vector<std::string> expected = makeInterfaceSharedQuotaCommands(iface, 1, kOldQuota, true);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900430 EXPECT_EQ(0, mBw.setInterfaceSharedQuota(iface, kOldQuota));
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900431 expectIptablesRestoreCommands(expected);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900432
433 constexpr uint64_t kNewQuota = kOldQuota + 1;
434 expected = {};
435 expectUpdateQuota(kNewQuota);
436 EXPECT_EQ(0, mBw.setInterfaceSharedQuota(iface, kNewQuota));
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900437 expectIptablesRestoreCommands(expected);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900438
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900439 expected = removeInterfaceSharedQuotaCommands(iface, kNewQuota, true);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900440 EXPECT_EQ(0, mBw.removeInterfaceSharedQuota(iface));
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900441 expectIptablesRestoreCommands(expected);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900442}
443
444TEST_F(BandwidthControllerTest, TestSetInterfaceSharedQuotaTwoInterfaces) {
445 constexpr uint64_t kQuota = 123456;
446 const std::vector<std::string> ifaces{
447 {"a" + mTun.name()},
448 {"b" + mTun.name()},
449 };
450
451 for (const auto& iface : ifaces) {
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900452 // Quota rule is only added when the total number of
453 // interfaces transitions from 0 -> 1.
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900454 bool first = (iface == ifaces[0]);
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900455 auto expected = makeInterfaceSharedQuotaCommands(iface, 1, kQuota, first);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900456 EXPECT_EQ(0, mBw.setInterfaceSharedQuota(iface, kQuota));
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900457 expectIptablesRestoreCommands(expected);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900458 }
459
460 for (const auto& iface : ifaces) {
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900461 // Quota rule is only removed when the total number of
462 // interfaces transitions from 1 -> 0.
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900463 bool last = (iface == ifaces[1]);
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900464 auto expected = removeInterfaceSharedQuotaCommands(iface, kQuota, last);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900465 EXPECT_EQ(0, mBw.removeInterfaceSharedQuota(iface));
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900466 expectIptablesRestoreCommands(expected);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900467 }
468}
469
Lorenzo Colittie8b56e42017-04-26 15:16:03 +0900470TEST_F(BandwidthControllerTest, IptablesAlertCmd) {
471 std::vector<std::string> expected = {
Luke Huangae038f82018-11-05 11:17:31 +0900472 "*filter\n"
473 "-I bw_global_alert -m quota2 ! --quota 123456 --name MyWonderfulAlert\n"
474 "COMMIT\n"};
Lorenzo Colittie8b56e42017-04-26 15:16:03 +0900475 EXPECT_EQ(0, runIptablesAlertCmd(IptOp::IptOpInsert, "MyWonderfulAlert", 123456));
Lorenzo Colitti3c272702017-04-26 15:48:13 +0900476 expectIptablesRestoreCommands(expected);
Lorenzo Colittie8b56e42017-04-26 15:16:03 +0900477
478 expected = {
Luke Huangae038f82018-11-05 11:17:31 +0900479 "*filter\n"
480 "-D bw_global_alert -m quota2 ! --quota 123456 --name MyWonderfulAlert\n"
481 "COMMIT\n"};
Lorenzo Colittie8b56e42017-04-26 15:16:03 +0900482 EXPECT_EQ(0, runIptablesAlertCmd(IptOp::IptOpDelete, "MyWonderfulAlert", 123456));
Lorenzo Colitti3c272702017-04-26 15:48:13 +0900483 expectIptablesRestoreCommands(expected);
Lorenzo Colittie8b56e42017-04-26 15:16:03 +0900484}
485
Lorenzo Colitti38078222017-07-06 17:27:23 +0900486TEST_F(BandwidthControllerTest, CostlyAlert) {
487 const int64_t kQuota = 123456;
488 int64_t alertBytes = 0;
489
490 std::vector<std::string> expected = {
Lorenzo Colittie85ffe12017-07-06 17:25:37 +0900491 "*filter\n"
492 "-A bw_costly_shared -m quota2 ! --quota 123456 --name sharedAlert\n"
493 "COMMIT\n"
Lorenzo Colitti38078222017-07-06 17:27:23 +0900494 };
495 EXPECT_EQ(0, setCostlyAlert("shared", kQuota, &alertBytes));
496 EXPECT_EQ(kQuota, alertBytes);
Lorenzo Colittie85ffe12017-07-06 17:25:37 +0900497 expectIptablesRestoreCommands(expected);
Lorenzo Colitti38078222017-07-06 17:27:23 +0900498
499 expected = {};
500 expectUpdateQuota(kQuota);
501 EXPECT_EQ(0, setCostlyAlert("shared", kQuota + 1, &alertBytes));
502 EXPECT_EQ(kQuota + 1, alertBytes);
Lorenzo Colittie85ffe12017-07-06 17:25:37 +0900503 expectIptablesRestoreCommands(expected);
Lorenzo Colitti38078222017-07-06 17:27:23 +0900504
505 expected = {
Lorenzo Colittie85ffe12017-07-06 17:25:37 +0900506 "*filter\n"
Lorenzo Colitti38078222017-07-06 17:27:23 +0900507 "-D bw_costly_shared -m quota2 ! --quota 123457 --name sharedAlert\n"
Lorenzo Colittie85ffe12017-07-06 17:25:37 +0900508 "COMMIT\n"
Lorenzo Colitti38078222017-07-06 17:27:23 +0900509 };
510 EXPECT_EQ(0, removeCostlyAlert("shared", &alertBytes));
511 EXPECT_EQ(0, alertBytes);
Lorenzo Colittie85ffe12017-07-06 17:25:37 +0900512 expectIptablesRestoreCommands(expected);
Lorenzo Colitti38078222017-07-06 17:27:23 +0900513}
514
Lorenzo Colittif4dfa682017-04-28 11:09:07 +0900515TEST_F(BandwidthControllerTest, ManipulateSpecialApps) {
516 std::vector<const char *> appUids = { "1000", "1001", "10012" };
517
518 std::vector<std::string> expected = {
Maciej Żenczykowskidec83c72019-12-24 15:27:14 -0800519 "*filter\n"
520 "-I bw_happy_box -m owner --uid-owner 1000 -j RETURN\n"
521 "-I bw_happy_box -m owner --uid-owner 1001 -j RETURN\n"
522 "-I bw_happy_box -m owner --uid-owner 10012 -j RETURN\n"
523 "COMMIT\n"};
Lorenzo Colittif4dfa682017-04-28 11:09:07 +0900524 EXPECT_EQ(0, mBw.addNiceApps(appUids.size(), const_cast<char**>(&appUids[0])));
Lorenzo Colitti911bc4c2017-04-28 14:34:01 +0900525 expectIptablesRestoreCommands(expected);
Lorenzo Colittif4dfa682017-04-28 11:09:07 +0900526
527 expected = {
Maciej Żenczykowskidec83c72019-12-24 15:27:14 -0800528 "*filter\n"
529 "-D bw_penalty_box -m owner --uid-owner 1000 -j REJECT\n"
530 "-D bw_penalty_box -m owner --uid-owner 1001 -j REJECT\n"
531 "-D bw_penalty_box -m owner --uid-owner 10012 -j REJECT\n"
532 "COMMIT\n"};
Lorenzo Colittif4dfa682017-04-28 11:09:07 +0900533 EXPECT_EQ(0, mBw.removeNaughtyApps(appUids.size(), const_cast<char**>(&appUids[0])));
Lorenzo Colitti911bc4c2017-04-28 14:34:01 +0900534 expectIptablesRestoreCommands(expected);
Lorenzo Colittif4dfa682017-04-28 11:09:07 +0900535}