blob: 0966b7ff85a954ac20cca39561008a15b6d3c538 [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"
63 "-A bw_OUTPUT -m owner --socket-exists\n"
Maciej Żenczykowskidec83c72019-12-24 15:27:14 -080064 "-A bw_costly_shared -j bw_penalty_box\n"
Chenbo Feng44c0f442018-07-10 16:54:30 -070065 "\n"
Maciej Żenczykowskidec83c72019-12-24 15:27:14 -080066 "-A bw_penalty_box -j bw_happy_box\n"
67 "-A bw_happy_box -j bw_data_saver\n"
Chenbo Feng44c0f442018-07-10 16:54:30 -070068 "-A bw_data_saver -j RETURN\n"
Maciej Żenczykowskidec83c72019-12-24 15:27:14 -080069 "-I bw_happy_box -m owner --uid-owner 0-9999 -j RETURN\n"
Chenbo Feng44c0f442018-07-10 16:54:30 -070070 "COMMIT\n"
71 "*raw\n"
72 "-A bw_raw_PREROUTING -i ipsec+ -j RETURN\n"
73 "-A bw_raw_PREROUTING -m policy --pol ipsec --dir in -j RETURN\n"
74 "-A bw_raw_PREROUTING -m owner --socket-exists\n"
75 "COMMIT\n"
76 "*mangle\n"
77 "-A bw_mangle_POSTROUTING -o ipsec+ -j RETURN\n"
78 "-A bw_mangle_POSTROUTING -m policy --pol ipsec --dir out -j RETURN\n"
79 "-A bw_mangle_POSTROUTING -m owner --socket-exists\n"
80 "-A bw_mangle_POSTROUTING -j MARK --set-mark 0x0/0x100000\n"
81 "\n"
82 "COMMIT\n";
83
84const std::string ACCOUNT_RULES_WITH_BPF =
85 "*filter\n"
Luke Huangae038f82018-11-05 11:17:31 +090086 "-A bw_INPUT -j bw_global_alert\n"
Chenbo Feng44c0f442018-07-10 16:54:30 -070087 "-A bw_INPUT -p esp -j RETURN\n"
88 "-A bw_INPUT -m mark --mark 0x100000/0x100000 -j RETURN\n"
89 "\n"
90 "-A bw_INPUT -j MARK --or-mark 0x100000\n"
Luke Huangae038f82018-11-05 11:17:31 +090091 "-A bw_OUTPUT -j bw_global_alert\n"
Chenbo Feng44c0f442018-07-10 16:54:30 -070092 "-A bw_OUTPUT -o ipsec+ -j RETURN\n"
93 "-A bw_OUTPUT -m policy --pol ipsec --dir out -j RETURN\n"
94 "\n"
Maciej Żenczykowskidec83c72019-12-24 15:27:14 -080095 "-A bw_costly_shared -j bw_penalty_box\n" +
Chenbo Feng44c0f442018-07-10 16:54:30 -070096 StringPrintf("-I bw_penalty_box -m bpf --object-pinned %s -j REJECT\n",
97 XT_BPF_BLACKLIST_PROG_PATH) +
Maciej Żenczykowskidec83c72019-12-24 15:27:14 -080098 "-A bw_penalty_box -j bw_happy_box\n"
99 "-A bw_happy_box -j bw_data_saver\n"
Chenbo Feng44c0f442018-07-10 16:54:30 -0700100 "-A bw_data_saver -j RETURN\n" +
101 StringPrintf("-I bw_happy_box -m bpf --object-pinned %s -j RETURN\n",
102 XT_BPF_WHITELIST_PROG_PATH) +
103 "COMMIT\n"
104 "*raw\n"
105 "-A bw_raw_PREROUTING -i ipsec+ -j RETURN\n"
106 "-A bw_raw_PREROUTING -m policy --pol ipsec --dir in -j RETURN\n" +
107 StringPrintf("-A bw_raw_PREROUTING -m bpf --object-pinned %s\n", XT_BPF_INGRESS_PROG_PATH) +
108 "COMMIT\n"
109 "*mangle\n"
110 "-A bw_mangle_POSTROUTING -o ipsec+ -j RETURN\n"
111 "-A bw_mangle_POSTROUTING -m policy --pol ipsec --dir out -j RETURN\n"
112 "\n"
113 "-A bw_mangle_POSTROUTING -j MARK --set-mark 0x0/0x100000\n" +
114 StringPrintf("-A bw_mangle_POSTROUTING -m bpf --object-pinned %s\n",
115 XT_BPF_EGRESS_PROG_PATH) +
116 "COMMIT\n";
117
Lorenzo Colitti0f150552016-03-28 02:30:27 +0900118class BandwidthControllerTest : public IptablesBaseTest {
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900119protected:
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900120 BandwidthControllerTest() {
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900121 BandwidthController::iptablesRestoreFunction = fakeExecIptablesRestoreWithOutput;
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900122 }
123 BandwidthController mBw;
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +0900124 TunInterface mTun;
125
126 void SetUp() {
127 ASSERT_EQ(0, mTun.init());
128 }
129
130 void TearDown() {
131 mTun.destroy();
132 }
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900133
Luke Huangae038f82018-11-05 11:17:31 +0900134 void expectSetupCommands(const std::string& expectedClean,
135 const std::string& expectedAccounting) {
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900136 std::string expectedList =
137 "*filter\n"
138 "-S\n"
139 "COMMIT\n";
140
141 std::string expectedFlush =
Luke Huangae038f82018-11-05 11:17:31 +0900142 "*filter\n"
143 ":bw_INPUT -\n"
144 ":bw_OUTPUT -\n"
145 ":bw_FORWARD -\n"
146 ":bw_happy_box -\n"
147 ":bw_penalty_box -\n"
148 ":bw_data_saver -\n"
149 ":bw_costly_shared -\n"
150 ":bw_global_alert -\n"
151 "COMMIT\n"
152 "*raw\n"
153 ":bw_raw_PREROUTING -\n"
154 "COMMIT\n"
155 "*mangle\n"
156 ":bw_mangle_POSTROUTING -\n"
157 "COMMIT\n";
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900158
159 ExpectedIptablesCommands expected = {{ V4, expectedList }};
160 if (expectedClean.size()) {
161 expected.push_back({ V4V6, expectedClean });
162 }
163 expected.push_back({ V4V6, expectedFlush });
164 if (expectedAccounting.size()) {
165 expected.push_back({ V4V6, expectedAccounting });
166 }
167
168 expectIptablesRestoreCommands(expected);
169 }
Lorenzo Colittie8b56e42017-04-26 15:16:03 +0900170
171 using IptOp = BandwidthController::IptOp;
172
Luke Huangae038f82018-11-05 11:17:31 +0900173 int runIptablesAlertCmd(IptOp a, const char* b, int64_t c) {
Lorenzo Colittie8b56e42017-04-26 15:16:03 +0900174 return mBw.runIptablesAlertCmd(a, b, c);
175 }
176
Bernie Innocenti7e25ec02018-07-02 19:32:17 +0900177 int setCostlyAlert(const std::string& a, int64_t b, int64_t* c) {
Lorenzo Colitti38078222017-07-06 17:27:23 +0900178 return mBw.setCostlyAlert(a, b, c);
179 }
180
Bernie Innocenti7e25ec02018-07-02 19:32:17 +0900181 int removeCostlyAlert(const std::string& a, int64_t* b) { return mBw.removeCostlyAlert(a, b); }
Lorenzo Colitti38078222017-07-06 17:27:23 +0900182
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900183 void expectUpdateQuota(uint64_t quota) {
184 uintptr_t dummy;
185 FILE* dummyFile = reinterpret_cast<FILE*>(&dummy);
186
187 EXPECT_CALL(mSyscalls, fopen(_, _)).WillOnce(Return(ByMove(UniqueFile(dummyFile))));
188 EXPECT_CALL(mSyscalls, vfprintf(dummyFile, _, _))
189 .WillOnce(Invoke([quota](FILE*, const std::string&, va_list ap) {
190 EXPECT_EQ(quota, va_arg(ap, uint64_t));
191 return 0;
192 }));
193 EXPECT_CALL(mSyscalls, fclose(dummyFile)).WillOnce(Return(ok));
194 }
195
Chenbo Feng44c0f442018-07-10 16:54:30 -0700196 void checkBandwithControl(bool useBpf) {
197 // Pretend no bw_costly_shared_<iface> rules already exist...
198 addIptablesRestoreOutput(
199 "-P OUTPUT ACCEPT\n"
200 "-N bw_costly_shared\n"
201 "-N unrelated\n");
202
203 // ... so none are flushed or deleted.
204 std::string expectedClean = "";
205
206 std::string expectedAccounting =
207 useBpf ? ACCOUNT_RULES_WITH_BPF : ACCOUNT_RULES_WITHOUT_BPF;
208 mBw.setBpfEnabled(useBpf);
Luke Huangf44a3c12018-09-07 12:10:12 +0800209 mBw.enableBandwidthControl();
Chenbo Feng44c0f442018-07-10 16:54:30 -0700210 expectSetupCommands(expectedClean, expectedAccounting);
211 }
212
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900213 StrictMock<android::netdutils::ScopedMockSyscalls> mSyscalls;
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900214};
215
Lorenzo Colittia0dc8a52016-03-26 22:42:07 +0900216TEST_F(BandwidthControllerTest, TestSetupIptablesHooks) {
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900217 // Pretend some bw_costly_shared_<iface> rules already exist...
218 addIptablesRestoreOutput(
219 "-P OUTPUT ACCEPT\n"
220 "-N bw_costly_rmnet_data0\n"
221 "-N bw_costly_shared\n"
222 "-N unrelated\n"
223 "-N bw_costly_rmnet_data7\n");
224
225 // ... and expect that they be flushed and deleted.
226 std::string expectedCleanCmds =
Lorenzo Colitti13debb82016-03-27 17:46:30 +0900227 "*filter\n"
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900228 ":bw_costly_rmnet_data0 -\n"
229 "-X bw_costly_rmnet_data0\n"
230 ":bw_costly_rmnet_data7 -\n"
231 "-X bw_costly_rmnet_data7\n"
232 "COMMIT\n";
233
234 mBw.setupIptablesHooks();
235 expectSetupCommands(expectedCleanCmds, "");
Lorenzo Colittia0dc8a52016-03-26 22:42:07 +0900236}
237
Benedict Wongb9baf262017-12-03 15:43:08 -0800238TEST_F(BandwidthControllerTest, TestCheckUidBillingMask) {
239 uint32_t uidBillingMask = Fwmark::getUidBillingMask();
240
241 // If mask is non-zero, and mask & mask-1 is equal to 0, then the mask is a power of two.
242 bool isPowerOfTwo = uidBillingMask && (uidBillingMask & (uidBillingMask - 1)) == 0;
243
244 // Must be exactly a power of two
245 EXPECT_TRUE(isPowerOfTwo);
246}
247
Chenbo Feng44c0f442018-07-10 16:54:30 -0700248TEST_F(BandwidthControllerTest, TestEnableBandwidthControlWithBpf) {
249 checkBandwithControl(true);
250}
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900251
Chenbo Feng44c0f442018-07-10 16:54:30 -0700252TEST_F(BandwidthControllerTest, TestEnableBandwidthControlWithoutBpf) {
253 checkBandwithControl(false);
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900254}
255
Lorenzo Colittia0dc8a52016-03-26 22:42:07 +0900256TEST_F(BandwidthControllerTest, TestDisableBandwidthControl) {
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900257 // Pretend some bw_costly_shared_<iface> rules already exist...
258 addIptablesRestoreOutput(
259 "-P OUTPUT ACCEPT\n"
260 "-N bw_costly_rmnet_data0\n"
261 "-N bw_costly_shared\n"
262 "-N unrelated\n"
263 "-N bw_costly_rmnet_data7\n");
264
265 // ... and expect that they be flushed.
266 std::string expectedCleanCmds =
Lorenzo Colitti13debb82016-03-27 17:46:30 +0900267 "*filter\n"
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900268 ":bw_costly_rmnet_data0 -\n"
269 ":bw_costly_rmnet_data7 -\n"
270 "COMMIT\n";
271
272 mBw.disableBandwidthControl();
273 expectSetupCommands(expectedCleanCmds, "");
Lorenzo Colittia0dc8a52016-03-26 22:42:07 +0900274}
275
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900276TEST_F(BandwidthControllerTest, TestEnableDataSaver) {
277 mBw.enableDataSaver(true);
Lorenzo Colittiaff28792017-09-26 17:46:18 +0900278 std::string expected4 =
Maciej Żenczykowskidec83c72019-12-24 15:27:14 -0800279 "*filter\n"
280 ":bw_data_saver -\n"
281 "-A bw_data_saver -j REJECT\n"
282 "COMMIT\n";
Lorenzo Colittiaff28792017-09-26 17:46:18 +0900283 std::string expected6 =
Maciej Żenczykowskidec83c72019-12-24 15:27:14 -0800284 "*filter\n"
285 ":bw_data_saver -\n"
286 "-A bw_data_saver -p icmpv6 --icmpv6-type packet-too-big -j RETURN\n"
287 "-A bw_data_saver -p icmpv6 --icmpv6-type router-solicitation -j RETURN\n"
288 "-A bw_data_saver -p icmpv6 --icmpv6-type router-advertisement -j RETURN\n"
289 "-A bw_data_saver -p icmpv6 --icmpv6-type neighbour-solicitation -j RETURN\n"
290 "-A bw_data_saver -p icmpv6 --icmpv6-type neighbour-advertisement -j RETURN\n"
291 "-A bw_data_saver -p icmpv6 --icmpv6-type redirect -j RETURN\n"
292 "-A bw_data_saver -j REJECT\n"
293 "COMMIT\n";
Lorenzo Colittiaff28792017-09-26 17:46:18 +0900294 expectIptablesRestoreCommands({
295 {V4, expected4},
296 {V6, expected6},
297 });
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900298
299 mBw.enableDataSaver(false);
Lorenzo Colittiaff28792017-09-26 17:46:18 +0900300 std::string expected = {
Maciej Żenczykowskidec83c72019-12-24 15:27:14 -0800301 "*filter\n"
302 ":bw_data_saver -\n"
303 "-A bw_data_saver -j RETURN\n"
304 "COMMIT\n"};
Lorenzo Colittiaff28792017-09-26 17:46:18 +0900305 expectIptablesRestoreCommands({
306 {V4, expected},
307 {V6, expected},
308 });
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900309}
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900310
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900311const std::vector<std::string> makeInterfaceQuotaCommands(const std::string& iface, int ruleIndex,
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +0900312 int64_t quota) {
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900313 const std::string chain = "bw_costly_" + iface;
314 const char* c_chain = chain.c_str();
315 const char* c_iface = iface.c_str();
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +0900316 std::vector<std::string> cmds = {
Maciej Żenczykowskidec83c72019-12-24 15:27:14 -0800317 "*filter",
318 StringPrintf(":%s -", c_chain),
319 StringPrintf("-A %s -j bw_penalty_box", c_chain),
320 StringPrintf("-I bw_INPUT %d -i %s -j %s", ruleIndex, c_iface, c_chain),
321 StringPrintf("-I bw_OUTPUT %d -o %s -j %s", ruleIndex, c_iface, c_chain),
322 StringPrintf("-A bw_FORWARD -i %s -j %s", c_iface, c_chain),
323 StringPrintf("-A bw_FORWARD -o %s -j %s", c_iface, c_chain),
324 StringPrintf("-A %s -m quota2 ! --quota %" PRIu64 " --name %s -j REJECT", c_chain,
325 quota, c_iface),
326 "COMMIT\n",
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +0900327 };
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900328 return {Join(cmds, "\n")};
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +0900329}
330
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900331const std::vector<std::string> removeInterfaceQuotaCommands(const std::string& iface) {
332 const std::string chain = "bw_costly_" + iface;
333 const char* c_chain = chain.c_str();
334 const char* c_iface = iface.c_str();
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +0900335 std::vector<std::string> cmds = {
Maciej Żenczykowskidec83c72019-12-24 15:27:14 -0800336 "*filter",
337 StringPrintf("-D bw_INPUT -i %s -j %s", c_iface, c_chain),
338 StringPrintf("-D bw_OUTPUT -o %s -j %s", c_iface, c_chain),
339 StringPrintf("-D bw_FORWARD -i %s -j %s", c_iface, c_chain),
340 StringPrintf("-D bw_FORWARD -o %s -j %s", c_iface, c_chain),
341 StringPrintf("-F %s", c_chain),
342 StringPrintf("-X %s", c_chain),
343 "COMMIT\n",
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +0900344 };
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900345 return {Join(cmds, "\n")};
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +0900346}
347
348TEST_F(BandwidthControllerTest, TestSetInterfaceQuota) {
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900349 constexpr uint64_t kOldQuota = 123456;
350 const std::string iface = mTun.name();
351 std::vector<std::string> expected = makeInterfaceQuotaCommands(iface, 1, kOldQuota);
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +0900352
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900353 EXPECT_EQ(0, mBw.setInterfaceQuota(iface, kOldQuota));
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900354 expectIptablesRestoreCommands(expected);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900355
356 constexpr uint64_t kNewQuota = kOldQuota + 1;
357 expected = {};
358 expectUpdateQuota(kNewQuota);
359 EXPECT_EQ(0, mBw.setInterfaceQuota(iface, kNewQuota));
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900360 expectIptablesRestoreCommands(expected);
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +0900361
362 expected = removeInterfaceQuotaCommands(iface);
363 EXPECT_EQ(0, mBw.removeInterfaceQuota(iface));
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900364 expectIptablesRestoreCommands(expected);
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +0900365}
Lorenzo Colittie8b56e42017-04-26 15:16:03 +0900366
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900367const std::vector<std::string> makeInterfaceSharedQuotaCommands(const std::string& iface,
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900368 int ruleIndex, int64_t quota,
369 bool insertQuota) {
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900370 const std::string chain = "bw_costly_shared";
371 const char* c_chain = chain.c_str();
372 const char* c_iface = iface.c_str();
373 std::vector<std::string> cmds = {
Maciej Żenczykowskidec83c72019-12-24 15:27:14 -0800374 "*filter",
375 StringPrintf("-I bw_INPUT %d -i %s -j %s", ruleIndex, c_iface, c_chain),
376 StringPrintf("-I bw_OUTPUT %d -o %s -j %s", ruleIndex, c_iface, c_chain),
377 StringPrintf("-A bw_FORWARD -i %s -j %s", c_iface, c_chain),
378 StringPrintf("-A bw_FORWARD -o %s -j %s", c_iface, c_chain),
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900379 };
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900380 if (insertQuota) {
Maciej Żenczykowskidec83c72019-12-24 15:27:14 -0800381 cmds.push_back(StringPrintf("-I %s -m quota2 ! --quota %" PRIu64 " --name shared -j REJECT",
382 c_chain, quota));
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900383 }
384 cmds.push_back("COMMIT\n");
385 return {Join(cmds, "\n")};
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900386}
387
388const std::vector<std::string> removeInterfaceSharedQuotaCommands(const std::string& iface,
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900389 int64_t quota, bool deleteQuota) {
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900390 const std::string chain = "bw_costly_shared";
391 const char* c_chain = chain.c_str();
392 const char* c_iface = iface.c_str();
393 std::vector<std::string> cmds = {
Maciej Żenczykowskidec83c72019-12-24 15:27:14 -0800394 "*filter",
395 StringPrintf("-D bw_INPUT -i %s -j %s", c_iface, c_chain),
396 StringPrintf("-D bw_OUTPUT -o %s -j %s", c_iface, c_chain),
397 StringPrintf("-D bw_FORWARD -i %s -j %s", c_iface, c_chain),
398 StringPrintf("-D bw_FORWARD -o %s -j %s", c_iface, c_chain),
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900399 };
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900400 if (deleteQuota) {
Maciej Żenczykowskidec83c72019-12-24 15:27:14 -0800401 cmds.push_back(StringPrintf("-D %s -m quota2 ! --quota %" PRIu64 " --name shared -j REJECT",
402 c_chain, quota));
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900403 }
404 cmds.push_back("COMMIT\n");
405 return {Join(cmds, "\n")};
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900406}
407
408TEST_F(BandwidthControllerTest, TestSetInterfaceSharedQuotaDuplicate) {
409 constexpr uint64_t kQuota = 123456;
410 const std::string iface = mTun.name();
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900411 std::vector<std::string> expected = makeInterfaceSharedQuotaCommands(iface, 1, 123456, true);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900412 EXPECT_EQ(0, mBw.setInterfaceSharedQuota(iface, kQuota));
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900413 expectIptablesRestoreCommands(expected);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900414
415 expected = {};
416 EXPECT_EQ(0, mBw.setInterfaceSharedQuota(iface, kQuota));
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900417 expectIptablesRestoreCommands(expected);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900418
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900419 expected = removeInterfaceSharedQuotaCommands(iface, kQuota, true);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900420 EXPECT_EQ(0, mBw.removeInterfaceSharedQuota(iface));
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900421 expectIptablesRestoreCommands(expected);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900422}
423
424TEST_F(BandwidthControllerTest, TestSetInterfaceSharedQuotaUpdate) {
425 constexpr uint64_t kOldQuota = 123456;
426 const std::string iface = mTun.name();
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900427 std::vector<std::string> expected = makeInterfaceSharedQuotaCommands(iface, 1, kOldQuota, true);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900428 EXPECT_EQ(0, mBw.setInterfaceSharedQuota(iface, kOldQuota));
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900429 expectIptablesRestoreCommands(expected);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900430
431 constexpr uint64_t kNewQuota = kOldQuota + 1;
432 expected = {};
433 expectUpdateQuota(kNewQuota);
434 EXPECT_EQ(0, mBw.setInterfaceSharedQuota(iface, kNewQuota));
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900435 expectIptablesRestoreCommands(expected);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900436
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900437 expected = removeInterfaceSharedQuotaCommands(iface, kNewQuota, true);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900438 EXPECT_EQ(0, mBw.removeInterfaceSharedQuota(iface));
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900439 expectIptablesRestoreCommands(expected);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900440}
441
442TEST_F(BandwidthControllerTest, TestSetInterfaceSharedQuotaTwoInterfaces) {
443 constexpr uint64_t kQuota = 123456;
444 const std::vector<std::string> ifaces{
445 {"a" + mTun.name()},
446 {"b" + mTun.name()},
447 };
448
449 for (const auto& iface : ifaces) {
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900450 // Quota rule is only added when the total number of
451 // interfaces transitions from 0 -> 1.
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900452 bool first = (iface == ifaces[0]);
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900453 auto expected = makeInterfaceSharedQuotaCommands(iface, 1, kQuota, first);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900454 EXPECT_EQ(0, mBw.setInterfaceSharedQuota(iface, kQuota));
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900455 expectIptablesRestoreCommands(expected);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900456 }
457
458 for (const auto& iface : ifaces) {
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900459 // Quota rule is only removed when the total number of
460 // interfaces transitions from 1 -> 0.
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900461 bool last = (iface == ifaces[1]);
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900462 auto expected = removeInterfaceSharedQuotaCommands(iface, kQuota, last);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900463 EXPECT_EQ(0, mBw.removeInterfaceSharedQuota(iface));
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900464 expectIptablesRestoreCommands(expected);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900465 }
466}
467
Lorenzo Colittie8b56e42017-04-26 15:16:03 +0900468TEST_F(BandwidthControllerTest, IptablesAlertCmd) {
469 std::vector<std::string> expected = {
Luke Huangae038f82018-11-05 11:17:31 +0900470 "*filter\n"
471 "-I bw_global_alert -m quota2 ! --quota 123456 --name MyWonderfulAlert\n"
472 "COMMIT\n"};
Lorenzo Colittie8b56e42017-04-26 15:16:03 +0900473 EXPECT_EQ(0, runIptablesAlertCmd(IptOp::IptOpInsert, "MyWonderfulAlert", 123456));
Lorenzo Colitti3c272702017-04-26 15:48:13 +0900474 expectIptablesRestoreCommands(expected);
Lorenzo Colittie8b56e42017-04-26 15:16:03 +0900475
476 expected = {
Luke Huangae038f82018-11-05 11:17:31 +0900477 "*filter\n"
478 "-D bw_global_alert -m quota2 ! --quota 123456 --name MyWonderfulAlert\n"
479 "COMMIT\n"};
Lorenzo Colittie8b56e42017-04-26 15:16:03 +0900480 EXPECT_EQ(0, runIptablesAlertCmd(IptOp::IptOpDelete, "MyWonderfulAlert", 123456));
Lorenzo Colitti3c272702017-04-26 15:48:13 +0900481 expectIptablesRestoreCommands(expected);
Lorenzo Colittie8b56e42017-04-26 15:16:03 +0900482}
483
Lorenzo Colitti38078222017-07-06 17:27:23 +0900484TEST_F(BandwidthControllerTest, CostlyAlert) {
485 const int64_t kQuota = 123456;
486 int64_t alertBytes = 0;
487
488 std::vector<std::string> expected = {
Lorenzo Colittie85ffe12017-07-06 17:25:37 +0900489 "*filter\n"
490 "-A bw_costly_shared -m quota2 ! --quota 123456 --name sharedAlert\n"
491 "COMMIT\n"
Lorenzo Colitti38078222017-07-06 17:27:23 +0900492 };
493 EXPECT_EQ(0, setCostlyAlert("shared", kQuota, &alertBytes));
494 EXPECT_EQ(kQuota, alertBytes);
Lorenzo Colittie85ffe12017-07-06 17:25:37 +0900495 expectIptablesRestoreCommands(expected);
Lorenzo Colitti38078222017-07-06 17:27:23 +0900496
497 expected = {};
498 expectUpdateQuota(kQuota);
499 EXPECT_EQ(0, setCostlyAlert("shared", kQuota + 1, &alertBytes));
500 EXPECT_EQ(kQuota + 1, alertBytes);
Lorenzo Colittie85ffe12017-07-06 17:25:37 +0900501 expectIptablesRestoreCommands(expected);
Lorenzo Colitti38078222017-07-06 17:27:23 +0900502
503 expected = {
Lorenzo Colittie85ffe12017-07-06 17:25:37 +0900504 "*filter\n"
Lorenzo Colitti38078222017-07-06 17:27:23 +0900505 "-D bw_costly_shared -m quota2 ! --quota 123457 --name sharedAlert\n"
Lorenzo Colittie85ffe12017-07-06 17:25:37 +0900506 "COMMIT\n"
Lorenzo Colitti38078222017-07-06 17:27:23 +0900507 };
508 EXPECT_EQ(0, removeCostlyAlert("shared", &alertBytes));
509 EXPECT_EQ(0, alertBytes);
Lorenzo Colittie85ffe12017-07-06 17:25:37 +0900510 expectIptablesRestoreCommands(expected);
Lorenzo Colitti38078222017-07-06 17:27:23 +0900511}
512
Lorenzo Colittif4dfa682017-04-28 11:09:07 +0900513TEST_F(BandwidthControllerTest, ManipulateSpecialApps) {
514 std::vector<const char *> appUids = { "1000", "1001", "10012" };
515
516 std::vector<std::string> expected = {
Maciej Żenczykowskidec83c72019-12-24 15:27:14 -0800517 "*filter\n"
518 "-I bw_happy_box -m owner --uid-owner 1000 -j RETURN\n"
519 "-I bw_happy_box -m owner --uid-owner 1001 -j RETURN\n"
520 "-I bw_happy_box -m owner --uid-owner 10012 -j RETURN\n"
521 "COMMIT\n"};
Lorenzo Colittif4dfa682017-04-28 11:09:07 +0900522 EXPECT_EQ(0, mBw.addNiceApps(appUids.size(), const_cast<char**>(&appUids[0])));
Lorenzo Colitti911bc4c2017-04-28 14:34:01 +0900523 expectIptablesRestoreCommands(expected);
Lorenzo Colittif4dfa682017-04-28 11:09:07 +0900524
525 expected = {
Maciej Żenczykowskidec83c72019-12-24 15:27:14 -0800526 "*filter\n"
527 "-D bw_penalty_box -m owner --uid-owner 1000 -j REJECT\n"
528 "-D bw_penalty_box -m owner --uid-owner 1001 -j REJECT\n"
529 "-D bw_penalty_box -m owner --uid-owner 10012 -j REJECT\n"
530 "COMMIT\n"};
Lorenzo Colittif4dfa682017-04-28 11:09:07 +0900531 EXPECT_EQ(0, mBw.removeNaughtyApps(appUids.size(), const_cast<char**>(&appUids[0])));
Lorenzo Colitti911bc4c2017-04-28 14:34:01 +0900532 expectIptablesRestoreCommands(expected);
Lorenzo Colittif4dfa682017-04-28 11:09:07 +0900533}