blob: 938cea0f0ddf0ef50acef83af5263aecb8b4eef8 [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"
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +090038#include "tun_interface.h"
39
Joel Scherpelz01cc5492017-06-16 10:45:14 +090040using ::testing::ByMove;
41using ::testing::Invoke;
42using ::testing::Return;
43using ::testing::StrictMock;
44using ::testing::Test;
45using ::testing::_;
46
Lorenzo Colitti48f83002017-07-06 15:06:04 +090047using android::base::Join;
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +090048using android::base::StringPrintf;
Chenbo Fenga121e202018-03-19 11:51:54 -070049using android::bpf::XT_BPF_EGRESS_PROG_PATH;
50using android::bpf::XT_BPF_INGRESS_PROG_PATH;
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +090051using android::net::TunInterface;
Joel Scherpelz01cc5492017-06-16 10:45:14 +090052using android::netdutils::status::ok;
53using android::netdutils::UniqueFile;
Lorenzo Colitti86a47982016-03-18 17:52:25 +090054
Lorenzo Colitti0f150552016-03-28 02:30:27 +090055class BandwidthControllerTest : public IptablesBaseTest {
Joel Scherpelz01cc5492017-06-16 10:45:14 +090056protected:
Lorenzo Colitti86a47982016-03-18 17:52:25 +090057 BandwidthControllerTest() {
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +090058 BandwidthController::iptablesRestoreFunction = fakeExecIptablesRestoreWithOutput;
Lorenzo Colitti86a47982016-03-18 17:52:25 +090059 }
60 BandwidthController mBw;
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +090061 TunInterface mTun;
62
63 void SetUp() {
64 ASSERT_EQ(0, mTun.init());
65 }
66
67 void TearDown() {
68 mTun.destroy();
69 }
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +090070
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +090071 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 Colittie8b56e42017-04-26 15:16:03 +0900105
106 using IptOp = BandwidthController::IptOp;
107
108 int runIptablesAlertCmd(IptOp a, const char *b, int64_t c) {
109 return mBw.runIptablesAlertCmd(a, b, c);
110 }
111
112 int runIptablesAlertFwdCmd(IptOp a, const char *b, int64_t c) {
113 return mBw.runIptablesAlertFwdCmd(a, b, c);
114 }
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900115
Lorenzo Colitti38078222017-07-06 17:27:23 +0900116 int setCostlyAlert(const std::string a, int64_t b, int64_t *c) {
117 return mBw.setCostlyAlert(a, b, c);
118 }
119
120 int removeCostlyAlert(const std::string a, int64_t *b) {
121 return mBw.removeCostlyAlert(a, b);
122 }
123
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900124 void expectUpdateQuota(uint64_t quota) {
125 uintptr_t dummy;
126 FILE* dummyFile = reinterpret_cast<FILE*>(&dummy);
127
128 EXPECT_CALL(mSyscalls, fopen(_, _)).WillOnce(Return(ByMove(UniqueFile(dummyFile))));
129 EXPECT_CALL(mSyscalls, vfprintf(dummyFile, _, _))
130 .WillOnce(Invoke([quota](FILE*, const std::string&, va_list ap) {
131 EXPECT_EQ(quota, va_arg(ap, uint64_t));
132 return 0;
133 }));
134 EXPECT_CALL(mSyscalls, fclose(dummyFile)).WillOnce(Return(ok));
135 }
136
137 StrictMock<android::netdutils::ScopedMockSyscalls> mSyscalls;
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900138};
139
Lorenzo Colittia0dc8a52016-03-26 22:42:07 +0900140TEST_F(BandwidthControllerTest, TestSetupIptablesHooks) {
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900141 // Pretend some bw_costly_shared_<iface> rules already exist...
142 addIptablesRestoreOutput(
143 "-P OUTPUT ACCEPT\n"
144 "-N bw_costly_rmnet_data0\n"
145 "-N bw_costly_shared\n"
146 "-N unrelated\n"
147 "-N bw_costly_rmnet_data7\n");
148
149 // ... and expect that they be flushed and deleted.
150 std::string expectedCleanCmds =
Lorenzo Colitti13debb82016-03-27 17:46:30 +0900151 "*filter\n"
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900152 ":bw_costly_rmnet_data0 -\n"
153 "-X bw_costly_rmnet_data0\n"
154 ":bw_costly_rmnet_data7 -\n"
155 "-X bw_costly_rmnet_data7\n"
156 "COMMIT\n";
157
158 mBw.setupIptablesHooks();
159 expectSetupCommands(expectedCleanCmds, "");
Lorenzo Colittia0dc8a52016-03-26 22:42:07 +0900160}
161
Benedict Wongb9baf262017-12-03 15:43:08 -0800162TEST_F(BandwidthControllerTest, TestCheckUidBillingMask) {
163 uint32_t uidBillingMask = Fwmark::getUidBillingMask();
164
165 // If mask is non-zero, and mask & mask-1 is equal to 0, then the mask is a power of two.
166 bool isPowerOfTwo = uidBillingMask && (uidBillingMask & (uidBillingMask - 1)) == 0;
167
168 // Must be exactly a power of two
169 EXPECT_TRUE(isPowerOfTwo);
170}
171
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900172TEST_F(BandwidthControllerTest, TestEnableBandwidthControl) {
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900173 // Pretend no bw_costly_shared_<iface> rules already exist...
174 addIptablesRestoreOutput(
175 "-P OUTPUT ACCEPT\n"
176 "-N bw_costly_shared\n"
177 "-N unrelated\n");
178
179 // ... so none are flushed or deleted.
180 std::string expectedClean = "";
181
Benedict Wongb9baf262017-12-03 15:43:08 -0800182 uint32_t uidBillingMask = Fwmark::getUidBillingMask();
Chenbo Fenga121e202018-03-19 11:51:54 -0700183 bool useBpf = BandwidthController::getBpfStatsStatus();
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900184 std::string expectedAccounting =
Lorenzo Colitti13debb82016-03-27 17:46:30 +0900185 "*filter\n"
Benedict Wongb9baf262017-12-03 15:43:08 -0800186 "-A bw_INPUT -p esp -j RETURN\n" +
187 StringPrintf("-A bw_INPUT -m mark --mark 0x%x/0x%x -j RETURN\n",
188 uidBillingMask, uidBillingMask) +
189 "-A bw_INPUT -m owner --socket-exists\n" +
190 StringPrintf("-A bw_INPUT -j MARK --or-mark 0x%x\n", uidBillingMask) +
191 "-A bw_OUTPUT -o " IPSEC_IFACE_PREFIX "+ -j RETURN\n"
192 "-A bw_OUTPUT -m policy --pol ipsec --dir out -j RETURN\n"
Lorenzo Colitti13debb82016-03-27 17:46:30 +0900193 "-A bw_OUTPUT -m owner --socket-exists\n"
194 "-A bw_costly_shared --jump bw_penalty_box\n"
195 "-A bw_penalty_box --jump bw_happy_box\n"
196 "-A bw_happy_box --jump bw_data_saver\n"
197 "-A bw_data_saver -j RETURN\n"
198 "-I bw_happy_box -m owner --uid-owner 0-9999 --jump RETURN\n"
199 "COMMIT\n"
200 "*raw\n"
Benedict Wongb9baf262017-12-03 15:43:08 -0800201 "-A bw_raw_PREROUTING -i " IPSEC_IFACE_PREFIX "+ -j RETURN\n"
202 "-A bw_raw_PREROUTING -m policy --pol ipsec --dir in -j RETURN\n"
Chenbo Fenga121e202018-03-19 11:51:54 -0700203 "-A bw_raw_PREROUTING -m owner --socket-exists\n";
204 if (useBpf) {
205 expectedAccounting += StringPrintf("-A bw_raw_PREROUTING -m bpf --object-pinned %s\n",
206 XT_BPF_INGRESS_PROG_PATH);
207 } else {
208 expectedAccounting += "\n";
209 }
210 expectedAccounting +=
Lorenzo Colitti13debb82016-03-27 17:46:30 +0900211 "COMMIT\n"
212 "*mangle\n"
Benedict Wongb9baf262017-12-03 15:43:08 -0800213 "-A bw_mangle_POSTROUTING -o " IPSEC_IFACE_PREFIX "+ -j RETURN\n"
214 "-A bw_mangle_POSTROUTING -m policy --pol ipsec --dir out -j RETURN\n"
215 "-A bw_mangle_POSTROUTING -m owner --socket-exists\n" +
Chenbo Fenga121e202018-03-19 11:51:54 -0700216 StringPrintf("-A bw_mangle_POSTROUTING -j MARK --set-mark 0x0/0x%x\n", uidBillingMask);
217 if (useBpf) {
218 expectedAccounting += StringPrintf("-A bw_mangle_POSTROUTING -m bpf --object-pinned %s\n",
219 XT_BPF_EGRESS_PROG_PATH);
220 } else {
221 expectedAccounting += "\n";
222 }
223 expectedAccounting += "COMMIT\n";
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900224 mBw.enableBandwidthControl(false);
225 expectSetupCommands(expectedClean, expectedAccounting);
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900226}
227
Lorenzo Colittia0dc8a52016-03-26 22:42:07 +0900228TEST_F(BandwidthControllerTest, TestDisableBandwidthControl) {
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900229 // Pretend some bw_costly_shared_<iface> rules already exist...
230 addIptablesRestoreOutput(
231 "-P OUTPUT ACCEPT\n"
232 "-N bw_costly_rmnet_data0\n"
233 "-N bw_costly_shared\n"
234 "-N unrelated\n"
235 "-N bw_costly_rmnet_data7\n");
236
237 // ... and expect that they be flushed.
238 std::string expectedCleanCmds =
Lorenzo Colitti13debb82016-03-27 17:46:30 +0900239 "*filter\n"
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900240 ":bw_costly_rmnet_data0 -\n"
241 ":bw_costly_rmnet_data7 -\n"
242 "COMMIT\n";
243
244 mBw.disableBandwidthControl();
245 expectSetupCommands(expectedCleanCmds, "");
Lorenzo Colittia0dc8a52016-03-26 22:42:07 +0900246}
247
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900248TEST_F(BandwidthControllerTest, TestEnableDataSaver) {
249 mBw.enableDataSaver(true);
Lorenzo Colittiaff28792017-09-26 17:46:18 +0900250 std::string expected4 =
Lorenzo Colitti911bc4c2017-04-28 14:34:01 +0900251 "*filter\n"
Lorenzo Colittiaff28792017-09-26 17:46:18 +0900252 ":bw_data_saver -\n"
253 "-A bw_data_saver --jump REJECT\n"
254 "COMMIT\n";
255 std::string expected6 =
256 "*filter\n"
257 ":bw_data_saver -\n"
258 "-A bw_data_saver -p icmpv6 --icmpv6-type packet-too-big -j RETURN\n"
259 "-A bw_data_saver -p icmpv6 --icmpv6-type router-solicitation -j RETURN\n"
260 "-A bw_data_saver -p icmpv6 --icmpv6-type router-advertisement -j RETURN\n"
261 "-A bw_data_saver -p icmpv6 --icmpv6-type neighbour-solicitation -j RETURN\n"
262 "-A bw_data_saver -p icmpv6 --icmpv6-type neighbour-advertisement -j RETURN\n"
263 "-A bw_data_saver -p icmpv6 --icmpv6-type redirect -j RETURN\n"
264 "-A bw_data_saver --jump REJECT\n"
265 "COMMIT\n";
266 expectIptablesRestoreCommands({
267 {V4, expected4},
268 {V6, expected6},
269 });
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900270
271 mBw.enableDataSaver(false);
Lorenzo Colittiaff28792017-09-26 17:46:18 +0900272 std::string expected = {
Lorenzo Colitti911bc4c2017-04-28 14:34:01 +0900273 "*filter\n"
Lorenzo Colittiaff28792017-09-26 17:46:18 +0900274 ":bw_data_saver -\n"
275 "-A bw_data_saver --jump RETURN\n"
Lorenzo Colitti911bc4c2017-04-28 14:34:01 +0900276 "COMMIT\n"
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900277 };
Lorenzo Colittiaff28792017-09-26 17:46:18 +0900278 expectIptablesRestoreCommands({
279 {V4, expected},
280 {V6, expected},
281 });
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900282}
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900283
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900284const std::vector<std::string> makeInterfaceQuotaCommands(const std::string& iface, int ruleIndex,
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +0900285 int64_t quota) {
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900286 const std::string chain = "bw_costly_" + iface;
287 const char* c_chain = chain.c_str();
288 const char* c_iface = iface.c_str();
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +0900289 std::vector<std::string> cmds = {
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900290 "*filter",
291 StringPrintf(":%s -", c_chain),
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900292 StringPrintf("-A %s -j bw_penalty_box", c_chain),
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900293 StringPrintf("-I bw_INPUT %d -i %s --jump %s", ruleIndex, c_iface, c_chain),
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900294 StringPrintf("-I bw_OUTPUT %d -o %s --jump %s", ruleIndex, c_iface, c_chain),
Erik Kline51eb3242017-09-20 18:30:47 +0900295 StringPrintf("-A bw_FORWARD -i %s --jump %s", c_iface, c_chain),
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900296 StringPrintf("-A bw_FORWARD -o %s --jump %s", c_iface, c_chain),
297 StringPrintf("-A %s -m quota2 ! --quota %" PRIu64 " --name %s --jump REJECT", c_chain,
298 quota, c_iface),
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900299 "COMMIT\n",
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +0900300 };
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900301 return {Join(cmds, "\n")};
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +0900302}
303
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900304const std::vector<std::string> removeInterfaceQuotaCommands(const std::string& iface) {
305 const std::string chain = "bw_costly_" + iface;
306 const char* c_chain = chain.c_str();
307 const char* c_iface = iface.c_str();
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +0900308 std::vector<std::string> cmds = {
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900309 "*filter",
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900310 StringPrintf("-D bw_INPUT -i %s --jump %s", c_iface, c_chain),
311 StringPrintf("-D bw_OUTPUT -o %s --jump %s", c_iface, c_chain),
Erik Kline51eb3242017-09-20 18:30:47 +0900312 StringPrintf("-D bw_FORWARD -i %s --jump %s", c_iface, c_chain),
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900313 StringPrintf("-D bw_FORWARD -o %s --jump %s", c_iface, c_chain),
314 StringPrintf("-F %s", c_chain),
315 StringPrintf("-X %s", c_chain),
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900316 "COMMIT\n",
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +0900317 };
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900318 return {Join(cmds, "\n")};
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +0900319}
320
321TEST_F(BandwidthControllerTest, TestSetInterfaceQuota) {
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900322 constexpr uint64_t kOldQuota = 123456;
323 const std::string iface = mTun.name();
324 std::vector<std::string> expected = makeInterfaceQuotaCommands(iface, 1, kOldQuota);
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +0900325
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900326 EXPECT_EQ(0, mBw.setInterfaceQuota(iface, kOldQuota));
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900327 expectIptablesRestoreCommands(expected);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900328
329 constexpr uint64_t kNewQuota = kOldQuota + 1;
330 expected = {};
331 expectUpdateQuota(kNewQuota);
332 EXPECT_EQ(0, mBw.setInterfaceQuota(iface, kNewQuota));
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900333 expectIptablesRestoreCommands(expected);
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +0900334
335 expected = removeInterfaceQuotaCommands(iface);
336 EXPECT_EQ(0, mBw.removeInterfaceQuota(iface));
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900337 expectIptablesRestoreCommands(expected);
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +0900338}
Lorenzo Colittie8b56e42017-04-26 15:16:03 +0900339
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900340const std::vector<std::string> makeInterfaceSharedQuotaCommands(const std::string& iface,
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900341 int ruleIndex, int64_t quota,
342 bool insertQuota) {
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900343 const std::string chain = "bw_costly_shared";
344 const char* c_chain = chain.c_str();
345 const char* c_iface = iface.c_str();
346 std::vector<std::string> cmds = {
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900347 "*filter",
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900348 StringPrintf("-I bw_INPUT %d -i %s --jump %s", ruleIndex, c_iface, c_chain),
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900349 StringPrintf("-I bw_OUTPUT %d -o %s --jump %s", ruleIndex, c_iface, c_chain),
Erik Kline51eb3242017-09-20 18:30:47 +0900350 StringPrintf("-A bw_FORWARD -i %s --jump %s", c_iface, c_chain),
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900351 StringPrintf("-A bw_FORWARD -o %s --jump %s", c_iface, c_chain),
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900352 };
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900353 if (insertQuota) {
354 cmds.push_back(StringPrintf(
355 "-I %s -m quota2 ! --quota %" PRIu64 " --name shared --jump REJECT", c_chain, quota));
356 }
357 cmds.push_back("COMMIT\n");
358 return {Join(cmds, "\n")};
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900359}
360
361const std::vector<std::string> removeInterfaceSharedQuotaCommands(const std::string& iface,
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900362 int64_t quota, bool deleteQuota) {
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900363 const std::string chain = "bw_costly_shared";
364 const char* c_chain = chain.c_str();
365 const char* c_iface = iface.c_str();
366 std::vector<std::string> cmds = {
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900367 "*filter",
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900368 StringPrintf("-D bw_INPUT -i %s --jump %s", c_iface, c_chain),
369 StringPrintf("-D bw_OUTPUT -o %s --jump %s", c_iface, c_chain),
Erik Kline51eb3242017-09-20 18:30:47 +0900370 StringPrintf("-D bw_FORWARD -i %s --jump %s", c_iface, c_chain),
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900371 StringPrintf("-D bw_FORWARD -o %s --jump %s", c_iface, c_chain),
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900372 };
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900373 if (deleteQuota) {
374 cmds.push_back(StringPrintf(
375 "-D %s -m quota2 ! --quota %" PRIu64 " --name shared --jump REJECT", c_chain, quota));
376 }
377 cmds.push_back("COMMIT\n");
378 return {Join(cmds, "\n")};
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900379}
380
381TEST_F(BandwidthControllerTest, TestSetInterfaceSharedQuotaDuplicate) {
382 constexpr uint64_t kQuota = 123456;
383 const std::string iface = mTun.name();
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900384 std::vector<std::string> expected = makeInterfaceSharedQuotaCommands(iface, 1, 123456, true);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900385 EXPECT_EQ(0, mBw.setInterfaceSharedQuota(iface, kQuota));
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900386 expectIptablesRestoreCommands(expected);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900387
388 expected = {};
389 EXPECT_EQ(0, mBw.setInterfaceSharedQuota(iface, kQuota));
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900390 expectIptablesRestoreCommands(expected);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900391
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900392 expected = removeInterfaceSharedQuotaCommands(iface, kQuota, true);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900393 EXPECT_EQ(0, mBw.removeInterfaceSharedQuota(iface));
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900394 expectIptablesRestoreCommands(expected);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900395}
396
397TEST_F(BandwidthControllerTest, TestSetInterfaceSharedQuotaUpdate) {
398 constexpr uint64_t kOldQuota = 123456;
399 const std::string iface = mTun.name();
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900400 std::vector<std::string> expected = makeInterfaceSharedQuotaCommands(iface, 1, kOldQuota, true);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900401 EXPECT_EQ(0, mBw.setInterfaceSharedQuota(iface, kOldQuota));
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900402 expectIptablesRestoreCommands(expected);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900403
404 constexpr uint64_t kNewQuota = kOldQuota + 1;
405 expected = {};
406 expectUpdateQuota(kNewQuota);
407 EXPECT_EQ(0, mBw.setInterfaceSharedQuota(iface, kNewQuota));
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900408 expectIptablesRestoreCommands(expected);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900409
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900410 expected = removeInterfaceSharedQuotaCommands(iface, kNewQuota, true);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900411 EXPECT_EQ(0, mBw.removeInterfaceSharedQuota(iface));
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900412 expectIptablesRestoreCommands(expected);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900413}
414
415TEST_F(BandwidthControllerTest, TestSetInterfaceSharedQuotaTwoInterfaces) {
416 constexpr uint64_t kQuota = 123456;
417 const std::vector<std::string> ifaces{
418 {"a" + mTun.name()},
419 {"b" + mTun.name()},
420 };
421
422 for (const auto& iface : ifaces) {
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900423 // Quota rule is only added when the total number of
424 // interfaces transitions from 0 -> 1.
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900425 bool first = (iface == ifaces[0]);
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900426 auto expected = makeInterfaceSharedQuotaCommands(iface, 1, kQuota, first);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900427 EXPECT_EQ(0, mBw.setInterfaceSharedQuota(iface, kQuota));
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900428 expectIptablesRestoreCommands(expected);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900429 }
430
431 for (const auto& iface : ifaces) {
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900432 // Quota rule is only removed when the total number of
433 // interfaces transitions from 1 -> 0.
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900434 bool last = (iface == ifaces[1]);
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900435 auto expected = removeInterfaceSharedQuotaCommands(iface, kQuota, last);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900436 EXPECT_EQ(0, mBw.removeInterfaceSharedQuota(iface));
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900437 expectIptablesRestoreCommands(expected);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900438 }
439}
440
Lorenzo Colittie8b56e42017-04-26 15:16:03 +0900441TEST_F(BandwidthControllerTest, IptablesAlertCmd) {
442 std::vector<std::string> expected = {
Lorenzo Colitti3c272702017-04-26 15:48:13 +0900443 "*filter\n"
444 "-I bw_INPUT -m quota2 ! --quota 123456 --name MyWonderfulAlert\n"
445 "-I bw_OUTPUT -m quota2 ! --quota 123456 --name MyWonderfulAlert\n"
446 "COMMIT\n"
Lorenzo Colittie8b56e42017-04-26 15:16:03 +0900447 };
448 EXPECT_EQ(0, runIptablesAlertCmd(IptOp::IptOpInsert, "MyWonderfulAlert", 123456));
Lorenzo Colitti3c272702017-04-26 15:48:13 +0900449 expectIptablesRestoreCommands(expected);
Lorenzo Colittie8b56e42017-04-26 15:16:03 +0900450
451 expected = {
Lorenzo Colitti3c272702017-04-26 15:48:13 +0900452 "*filter\n"
453 "-D bw_INPUT -m quota2 ! --quota 123456 --name MyWonderfulAlert\n"
454 "-D bw_OUTPUT -m quota2 ! --quota 123456 --name MyWonderfulAlert\n"
455 "COMMIT\n"
Lorenzo Colittie8b56e42017-04-26 15:16:03 +0900456 };
457 EXPECT_EQ(0, runIptablesAlertCmd(IptOp::IptOpDelete, "MyWonderfulAlert", 123456));
Lorenzo Colitti3c272702017-04-26 15:48:13 +0900458 expectIptablesRestoreCommands(expected);
Lorenzo Colittie8b56e42017-04-26 15:16:03 +0900459}
460
461TEST_F(BandwidthControllerTest, IptablesAlertFwdCmd) {
462 std::vector<std::string> expected = {
Lorenzo Colitti3c272702017-04-26 15:48:13 +0900463 "*filter\n"
464 "-I bw_FORWARD -m quota2 ! --quota 123456 --name MyWonderfulAlert\n"
465 "COMMIT\n"
Lorenzo Colittie8b56e42017-04-26 15:16:03 +0900466 };
467 EXPECT_EQ(0, runIptablesAlertFwdCmd(IptOp::IptOpInsert, "MyWonderfulAlert", 123456));
Lorenzo Colitti3c272702017-04-26 15:48:13 +0900468 expectIptablesRestoreCommands(expected);
Lorenzo Colittie8b56e42017-04-26 15:16:03 +0900469
470 expected = {
Lorenzo Colitti3c272702017-04-26 15:48:13 +0900471 "*filter\n"
472 "-D bw_FORWARD -m quota2 ! --quota 123456 --name MyWonderfulAlert\n"
473 "COMMIT\n"
Lorenzo Colittie8b56e42017-04-26 15:16:03 +0900474 };
475 EXPECT_EQ(0, runIptablesAlertFwdCmd(IptOp::IptOpDelete, "MyWonderfulAlert", 123456));
Lorenzo Colitti3c272702017-04-26 15:48:13 +0900476 expectIptablesRestoreCommands(expected);
Lorenzo Colittie8b56e42017-04-26 15:16:03 +0900477}
Lorenzo Colittif4dfa682017-04-28 11:09:07 +0900478
Lorenzo Colitti38078222017-07-06 17:27:23 +0900479TEST_F(BandwidthControllerTest, CostlyAlert) {
480 const int64_t kQuota = 123456;
481 int64_t alertBytes = 0;
482
483 std::vector<std::string> expected = {
Lorenzo Colittie85ffe12017-07-06 17:25:37 +0900484 "*filter\n"
485 "-A bw_costly_shared -m quota2 ! --quota 123456 --name sharedAlert\n"
486 "COMMIT\n"
Lorenzo Colitti38078222017-07-06 17:27:23 +0900487 };
488 EXPECT_EQ(0, setCostlyAlert("shared", kQuota, &alertBytes));
489 EXPECT_EQ(kQuota, alertBytes);
Lorenzo Colittie85ffe12017-07-06 17:25:37 +0900490 expectIptablesRestoreCommands(expected);
Lorenzo Colitti38078222017-07-06 17:27:23 +0900491
492 expected = {};
493 expectUpdateQuota(kQuota);
494 EXPECT_EQ(0, setCostlyAlert("shared", kQuota + 1, &alertBytes));
495 EXPECT_EQ(kQuota + 1, alertBytes);
Lorenzo Colittie85ffe12017-07-06 17:25:37 +0900496 expectIptablesRestoreCommands(expected);
Lorenzo Colitti38078222017-07-06 17:27:23 +0900497
498 expected = {
Lorenzo Colittie85ffe12017-07-06 17:25:37 +0900499 "*filter\n"
Lorenzo Colitti38078222017-07-06 17:27:23 +0900500 "-D bw_costly_shared -m quota2 ! --quota 123457 --name sharedAlert\n"
Lorenzo Colittie85ffe12017-07-06 17:25:37 +0900501 "COMMIT\n"
Lorenzo Colitti38078222017-07-06 17:27:23 +0900502 };
503 EXPECT_EQ(0, removeCostlyAlert("shared", &alertBytes));
504 EXPECT_EQ(0, alertBytes);
Lorenzo Colittie85ffe12017-07-06 17:25:37 +0900505 expectIptablesRestoreCommands(expected);
Lorenzo Colitti38078222017-07-06 17:27:23 +0900506}
507
Lorenzo Colittif4dfa682017-04-28 11:09:07 +0900508TEST_F(BandwidthControllerTest, ManipulateSpecialApps) {
509 std::vector<const char *> appUids = { "1000", "1001", "10012" };
510
511 std::vector<std::string> expected = {
Lorenzo Colitti911bc4c2017-04-28 14:34:01 +0900512 "*filter\n"
513 "-I bw_happy_box -m owner --uid-owner 1000 --jump RETURN\n"
514 "-I bw_happy_box -m owner --uid-owner 1001 --jump RETURN\n"
515 "-I bw_happy_box -m owner --uid-owner 10012 --jump RETURN\n"
516 "COMMIT\n"
Lorenzo Colittif4dfa682017-04-28 11:09:07 +0900517 };
518 EXPECT_EQ(0, mBw.addNiceApps(appUids.size(), const_cast<char**>(&appUids[0])));
Lorenzo Colitti911bc4c2017-04-28 14:34:01 +0900519 expectIptablesRestoreCommands(expected);
Lorenzo Colittif4dfa682017-04-28 11:09:07 +0900520
521 expected = {
Lorenzo Colitti911bc4c2017-04-28 14:34:01 +0900522 "*filter\n"
523 "-D bw_penalty_box -m owner --uid-owner 1000 --jump REJECT\n"
524 "-D bw_penalty_box -m owner --uid-owner 1001 --jump REJECT\n"
525 "-D bw_penalty_box -m owner --uid-owner 10012 --jump REJECT\n"
526 "COMMIT\n"
Lorenzo Colittif4dfa682017-04-28 11:09:07 +0900527 };
528 EXPECT_EQ(0, mBw.removeNaughtyApps(appUids.size(), const_cast<char**>(&appUids[0])));
Lorenzo Colitti911bc4c2017-04-28 14:34:01 +0900529 expectIptablesRestoreCommands(expected);
Lorenzo Colittif4dfa682017-04-28 11:09:07 +0900530}