blob: d635daf016fbc267425a781b31fc0fa01e9e0a63 [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
Lorenzo Colitti0f150552016-03-28 02:30:27 +090053class BandwidthControllerTest : public IptablesBaseTest {
Joel Scherpelz01cc5492017-06-16 10:45:14 +090054protected:
Lorenzo Colitti86a47982016-03-18 17:52:25 +090055 BandwidthControllerTest() {
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +090056 BandwidthController::iptablesRestoreFunction = fakeExecIptablesRestoreWithOutput;
Lorenzo Colitti86a47982016-03-18 17:52:25 +090057 }
58 BandwidthController mBw;
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +090059 TunInterface mTun;
60
61 void SetUp() {
62 ASSERT_EQ(0, mTun.init());
63 }
64
65 void TearDown() {
66 mTun.destroy();
67 }
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +090068
Luke Huangae038f82018-11-05 11:17:31 +090069 void expectSetupCommands(const std::string& expectedClean,
70 const std::string& expectedAccounting) {
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +090071 std::string expectedList =
72 "*filter\n"
73 "-S\n"
74 "COMMIT\n";
75
76 std::string expectedFlush =
Luke Huangae038f82018-11-05 11:17:31 +090077 "*filter\n"
78 ":bw_INPUT -\n"
79 ":bw_OUTPUT -\n"
80 ":bw_FORWARD -\n"
81 ":bw_happy_box -\n"
82 ":bw_penalty_box -\n"
83 ":bw_data_saver -\n"
84 ":bw_costly_shared -\n"
85 ":bw_global_alert -\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";
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +090093
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
Luke Huangae038f82018-11-05 11:17:31 +0900108 int runIptablesAlertCmd(IptOp a, const char* b, int64_t c) {
Lorenzo Colittie8b56e42017-04-26 15:16:03 +0900109 return mBw.runIptablesAlertCmd(a, b, c);
110 }
111
Bernie Innocenti7e25ec02018-07-02 19:32:17 +0900112 int setCostlyAlert(const std::string& a, int64_t b, int64_t* c) {
Lorenzo Colitti38078222017-07-06 17:27:23 +0900113 return mBw.setCostlyAlert(a, b, c);
114 }
115
Bernie Innocenti7e25ec02018-07-02 19:32:17 +0900116 int removeCostlyAlert(const std::string& a, int64_t* b) { return mBw.removeCostlyAlert(a, b); }
Lorenzo Colitti38078222017-07-06 17:27:23 +0900117
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900118 void expectUpdateQuota(uint64_t quota) {
119 uintptr_t dummy;
120 FILE* dummyFile = reinterpret_cast<FILE*>(&dummy);
121
122 EXPECT_CALL(mSyscalls, fopen(_, _)).WillOnce(Return(ByMove(UniqueFile(dummyFile))));
123 EXPECT_CALL(mSyscalls, vfprintf(dummyFile, _, _))
124 .WillOnce(Invoke([quota](FILE*, const std::string&, va_list ap) {
125 EXPECT_EQ(quota, va_arg(ap, uint64_t));
126 return 0;
127 }));
128 EXPECT_CALL(mSyscalls, fclose(dummyFile)).WillOnce(Return(ok));
129 }
130
131 StrictMock<android::netdutils::ScopedMockSyscalls> mSyscalls;
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900132};
133
Lorenzo Colittia0dc8a52016-03-26 22:42:07 +0900134TEST_F(BandwidthControllerTest, TestSetupIptablesHooks) {
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900135 // Pretend some bw_costly_shared_<iface> rules already exist...
136 addIptablesRestoreOutput(
137 "-P OUTPUT ACCEPT\n"
138 "-N bw_costly_rmnet_data0\n"
139 "-N bw_costly_shared\n"
140 "-N unrelated\n"
141 "-N bw_costly_rmnet_data7\n");
142
143 // ... and expect that they be flushed and deleted.
144 std::string expectedCleanCmds =
Lorenzo Colitti13debb82016-03-27 17:46:30 +0900145 "*filter\n"
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900146 ":bw_costly_rmnet_data0 -\n"
147 "-X bw_costly_rmnet_data0\n"
148 ":bw_costly_rmnet_data7 -\n"
149 "-X bw_costly_rmnet_data7\n"
150 "COMMIT\n";
151
152 mBw.setupIptablesHooks();
153 expectSetupCommands(expectedCleanCmds, "");
Lorenzo Colittia0dc8a52016-03-26 22:42:07 +0900154}
155
Benedict Wongb9baf262017-12-03 15:43:08 -0800156TEST_F(BandwidthControllerTest, TestCheckUidBillingMask) {
157 uint32_t uidBillingMask = Fwmark::getUidBillingMask();
158
159 // If mask is non-zero, and mask & mask-1 is equal to 0, then the mask is a power of two.
160 bool isPowerOfTwo = uidBillingMask && (uidBillingMask & (uidBillingMask - 1)) == 0;
161
162 // Must be exactly a power of two
163 EXPECT_TRUE(isPowerOfTwo);
164}
165
Patrick Rohr03e3f7b2020-12-29 16:09:33 +0100166TEST_F(BandwidthControllerTest, TestEnableBandwidthControl) {
167 // Pretend no bw_costly_shared_<iface> rules already exist...
168 addIptablesRestoreOutput(
169 "-P OUTPUT ACCEPT\n"
170 "-N bw_costly_shared\n"
171 "-N unrelated\n");
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900172
Patrick Rohr03e3f7b2020-12-29 16:09:33 +0100173 // ... so none are flushed or deleted.
174 // clang-format off
175 static const std::string expectedClean = "";
176 static const std::string expectedAccounting =
177 "*filter\n"
178 "-A bw_INPUT -j bw_global_alert\n"
179 "-A bw_INPUT -p esp -j RETURN\n"
180 "-A bw_INPUT -m mark --mark 0x100000/0x100000 -j RETURN\n"
181 "-A bw_INPUT -j MARK --or-mark 0x100000\n"
182 "-A bw_OUTPUT -j bw_global_alert\n"
183 "-A bw_costly_shared -j bw_penalty_box\n"
184 "-I bw_penalty_box -m bpf --object-pinned " XT_BPF_DENYLIST_PROG_PATH " -j REJECT\n"
185 "-A bw_penalty_box -j bw_happy_box\n"
186 "-A bw_happy_box -j bw_data_saver\n"
187 "-A bw_data_saver -j RETURN\n"
188 "-I bw_happy_box -m bpf --object-pinned " XT_BPF_ALLOWLIST_PROG_PATH " -j RETURN\n"
189 "COMMIT\n"
190 "*raw\n"
191 "-A bw_raw_PREROUTING -i ipsec+ -j RETURN\n"
192 "-A bw_raw_PREROUTING -m policy --pol ipsec --dir in -j RETURN\n"
193 "-A bw_raw_PREROUTING -m bpf --object-pinned " XT_BPF_INGRESS_PROG_PATH "\n"
194 "COMMIT\n"
195 "*mangle\n"
196 "-A bw_mangle_POSTROUTING -o ipsec+ -j RETURN\n"
197 "-A bw_mangle_POSTROUTING -m policy --pol ipsec --dir out -j RETURN\n"
198 "-A bw_mangle_POSTROUTING -j MARK --set-mark 0x0/0x100000\n"
199 "-A bw_mangle_POSTROUTING -m owner --uid-owner clat -j RETURN\n"
200 "-A bw_mangle_POSTROUTING -m bpf --object-pinned " XT_BPF_EGRESS_PROG_PATH "\n"
201 "COMMIT\n";
202 // clang-format on
203
204 mBw.enableBandwidthControl();
205 expectSetupCommands(expectedClean, expectedAccounting);
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900206}
207
Lorenzo Colittia0dc8a52016-03-26 22:42:07 +0900208TEST_F(BandwidthControllerTest, TestDisableBandwidthControl) {
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900209 // Pretend some bw_costly_shared_<iface> rules already exist...
210 addIptablesRestoreOutput(
211 "-P OUTPUT ACCEPT\n"
212 "-N bw_costly_rmnet_data0\n"
213 "-N bw_costly_shared\n"
214 "-N unrelated\n"
215 "-N bw_costly_rmnet_data7\n");
216
217 // ... and expect that they be flushed.
218 std::string expectedCleanCmds =
Lorenzo Colitti13debb82016-03-27 17:46:30 +0900219 "*filter\n"
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900220 ":bw_costly_rmnet_data0 -\n"
221 ":bw_costly_rmnet_data7 -\n"
222 "COMMIT\n";
223
224 mBw.disableBandwidthControl();
225 expectSetupCommands(expectedCleanCmds, "");
Lorenzo Colittia0dc8a52016-03-26 22:42:07 +0900226}
227
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900228TEST_F(BandwidthControllerTest, TestEnableDataSaver) {
229 mBw.enableDataSaver(true);
Lorenzo Colittiaff28792017-09-26 17:46:18 +0900230 std::string expected4 =
Maciej Żenczykowskidec83c72019-12-24 15:27:14 -0800231 "*filter\n"
232 ":bw_data_saver -\n"
233 "-A bw_data_saver -j REJECT\n"
234 "COMMIT\n";
Lorenzo Colittiaff28792017-09-26 17:46:18 +0900235 std::string expected6 =
Maciej Żenczykowskidec83c72019-12-24 15:27:14 -0800236 "*filter\n"
237 ":bw_data_saver -\n"
238 "-A bw_data_saver -p icmpv6 --icmpv6-type packet-too-big -j RETURN\n"
239 "-A bw_data_saver -p icmpv6 --icmpv6-type router-solicitation -j RETURN\n"
240 "-A bw_data_saver -p icmpv6 --icmpv6-type router-advertisement -j RETURN\n"
241 "-A bw_data_saver -p icmpv6 --icmpv6-type neighbour-solicitation -j RETURN\n"
242 "-A bw_data_saver -p icmpv6 --icmpv6-type neighbour-advertisement -j RETURN\n"
243 "-A bw_data_saver -p icmpv6 --icmpv6-type redirect -j RETURN\n"
244 "-A bw_data_saver -j REJECT\n"
245 "COMMIT\n";
Lorenzo Colittiaff28792017-09-26 17:46:18 +0900246 expectIptablesRestoreCommands({
247 {V4, expected4},
248 {V6, expected6},
249 });
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900250
251 mBw.enableDataSaver(false);
Lorenzo Colittiaff28792017-09-26 17:46:18 +0900252 std::string expected = {
Maciej Żenczykowskidec83c72019-12-24 15:27:14 -0800253 "*filter\n"
254 ":bw_data_saver -\n"
255 "-A bw_data_saver -j RETURN\n"
256 "COMMIT\n"};
Lorenzo Colittiaff28792017-09-26 17:46:18 +0900257 expectIptablesRestoreCommands({
258 {V4, expected},
259 {V6, expected},
260 });
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900261}
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900262
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900263const std::vector<std::string> makeInterfaceQuotaCommands(const std::string& iface, int ruleIndex,
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +0900264 int64_t quota) {
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900265 const std::string chain = "bw_costly_" + iface;
266 const char* c_chain = chain.c_str();
267 const char* c_iface = iface.c_str();
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +0900268 std::vector<std::string> cmds = {
Maciej Żenczykowskidec83c72019-12-24 15:27:14 -0800269 "*filter",
270 StringPrintf(":%s -", c_chain),
271 StringPrintf("-A %s -j bw_penalty_box", c_chain),
272 StringPrintf("-I bw_INPUT %d -i %s -j %s", ruleIndex, c_iface, c_chain),
273 StringPrintf("-I bw_OUTPUT %d -o %s -j %s", ruleIndex, c_iface, c_chain),
274 StringPrintf("-A bw_FORWARD -i %s -j %s", c_iface, c_chain),
275 StringPrintf("-A bw_FORWARD -o %s -j %s", c_iface, c_chain),
276 StringPrintf("-A %s -m quota2 ! --quota %" PRIu64 " --name %s -j REJECT", c_chain,
277 quota, c_iface),
278 "COMMIT\n",
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +0900279 };
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900280 return {Join(cmds, "\n")};
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +0900281}
282
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900283const std::vector<std::string> removeInterfaceQuotaCommands(const std::string& iface) {
284 const std::string chain = "bw_costly_" + iface;
285 const char* c_chain = chain.c_str();
286 const char* c_iface = iface.c_str();
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +0900287 std::vector<std::string> cmds = {
Maciej Żenczykowskidec83c72019-12-24 15:27:14 -0800288 "*filter",
289 StringPrintf("-D bw_INPUT -i %s -j %s", c_iface, c_chain),
290 StringPrintf("-D bw_OUTPUT -o %s -j %s", c_iface, c_chain),
291 StringPrintf("-D bw_FORWARD -i %s -j %s", c_iface, c_chain),
292 StringPrintf("-D bw_FORWARD -o %s -j %s", c_iface, c_chain),
293 StringPrintf("-F %s", c_chain),
294 StringPrintf("-X %s", c_chain),
295 "COMMIT\n",
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +0900296 };
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900297 return {Join(cmds, "\n")};
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +0900298}
299
300TEST_F(BandwidthControllerTest, TestSetInterfaceQuota) {
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900301 constexpr uint64_t kOldQuota = 123456;
302 const std::string iface = mTun.name();
303 std::vector<std::string> expected = makeInterfaceQuotaCommands(iface, 1, kOldQuota);
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +0900304
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900305 EXPECT_EQ(0, mBw.setInterfaceQuota(iface, kOldQuota));
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900306 expectIptablesRestoreCommands(expected);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900307
308 constexpr uint64_t kNewQuota = kOldQuota + 1;
309 expected = {};
310 expectUpdateQuota(kNewQuota);
311 EXPECT_EQ(0, mBw.setInterfaceQuota(iface, kNewQuota));
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900312 expectIptablesRestoreCommands(expected);
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +0900313
314 expected = removeInterfaceQuotaCommands(iface);
315 EXPECT_EQ(0, mBw.removeInterfaceQuota(iface));
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900316 expectIptablesRestoreCommands(expected);
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +0900317}
Lorenzo Colittie8b56e42017-04-26 15:16:03 +0900318
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900319const std::vector<std::string> makeInterfaceSharedQuotaCommands(const std::string& iface,
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900320 int ruleIndex, int64_t quota,
321 bool insertQuota) {
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900322 const std::string chain = "bw_costly_shared";
323 const char* c_chain = chain.c_str();
324 const char* c_iface = iface.c_str();
325 std::vector<std::string> cmds = {
Maciej Żenczykowskidec83c72019-12-24 15:27:14 -0800326 "*filter",
327 StringPrintf("-I bw_INPUT %d -i %s -j %s", ruleIndex, c_iface, c_chain),
328 StringPrintf("-I bw_OUTPUT %d -o %s -j %s", ruleIndex, c_iface, c_chain),
329 StringPrintf("-A bw_FORWARD -i %s -j %s", c_iface, c_chain),
330 StringPrintf("-A bw_FORWARD -o %s -j %s", c_iface, c_chain),
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900331 };
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900332 if (insertQuota) {
Maciej Żenczykowskidec83c72019-12-24 15:27:14 -0800333 cmds.push_back(StringPrintf("-I %s -m quota2 ! --quota %" PRIu64 " --name shared -j REJECT",
334 c_chain, quota));
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900335 }
336 cmds.push_back("COMMIT\n");
337 return {Join(cmds, "\n")};
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900338}
339
340const std::vector<std::string> removeInterfaceSharedQuotaCommands(const std::string& iface,
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900341 int64_t quota, bool deleteQuota) {
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900342 const std::string chain = "bw_costly_shared";
343 const char* c_chain = chain.c_str();
344 const char* c_iface = iface.c_str();
345 std::vector<std::string> cmds = {
Maciej Żenczykowskidec83c72019-12-24 15:27:14 -0800346 "*filter",
347 StringPrintf("-D bw_INPUT -i %s -j %s", c_iface, c_chain),
348 StringPrintf("-D bw_OUTPUT -o %s -j %s", c_iface, c_chain),
349 StringPrintf("-D bw_FORWARD -i %s -j %s", c_iface, c_chain),
350 StringPrintf("-D bw_FORWARD -o %s -j %s", c_iface, c_chain),
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900351 };
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900352 if (deleteQuota) {
Maciej Żenczykowskidec83c72019-12-24 15:27:14 -0800353 cmds.push_back(StringPrintf("-D %s -m quota2 ! --quota %" PRIu64 " --name shared -j REJECT",
354 c_chain, quota));
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900355 }
356 cmds.push_back("COMMIT\n");
357 return {Join(cmds, "\n")};
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900358}
359
360TEST_F(BandwidthControllerTest, TestSetInterfaceSharedQuotaDuplicate) {
361 constexpr uint64_t kQuota = 123456;
362 const std::string iface = mTun.name();
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900363 std::vector<std::string> expected = makeInterfaceSharedQuotaCommands(iface, 1, 123456, true);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900364 EXPECT_EQ(0, mBw.setInterfaceSharedQuota(iface, kQuota));
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900365 expectIptablesRestoreCommands(expected);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900366
367 expected = {};
368 EXPECT_EQ(0, mBw.setInterfaceSharedQuota(iface, kQuota));
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900369 expectIptablesRestoreCommands(expected);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900370
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900371 expected = removeInterfaceSharedQuotaCommands(iface, kQuota, true);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900372 EXPECT_EQ(0, mBw.removeInterfaceSharedQuota(iface));
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900373 expectIptablesRestoreCommands(expected);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900374}
375
376TEST_F(BandwidthControllerTest, TestSetInterfaceSharedQuotaUpdate) {
377 constexpr uint64_t kOldQuota = 123456;
378 const std::string iface = mTun.name();
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900379 std::vector<std::string> expected = makeInterfaceSharedQuotaCommands(iface, 1, kOldQuota, true);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900380 EXPECT_EQ(0, mBw.setInterfaceSharedQuota(iface, kOldQuota));
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900381 expectIptablesRestoreCommands(expected);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900382
383 constexpr uint64_t kNewQuota = kOldQuota + 1;
384 expected = {};
385 expectUpdateQuota(kNewQuota);
386 EXPECT_EQ(0, mBw.setInterfaceSharedQuota(iface, kNewQuota));
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900387 expectIptablesRestoreCommands(expected);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900388
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900389 expected = removeInterfaceSharedQuotaCommands(iface, kNewQuota, true);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900390 EXPECT_EQ(0, mBw.removeInterfaceSharedQuota(iface));
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900391 expectIptablesRestoreCommands(expected);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900392}
393
394TEST_F(BandwidthControllerTest, TestSetInterfaceSharedQuotaTwoInterfaces) {
395 constexpr uint64_t kQuota = 123456;
396 const std::vector<std::string> ifaces{
397 {"a" + mTun.name()},
398 {"b" + mTun.name()},
399 };
400
401 for (const auto& iface : ifaces) {
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900402 // Quota rule is only added when the total number of
403 // interfaces transitions from 0 -> 1.
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900404 bool first = (iface == ifaces[0]);
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900405 auto expected = makeInterfaceSharedQuotaCommands(iface, 1, kQuota, first);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900406 EXPECT_EQ(0, mBw.setInterfaceSharedQuota(iface, kQuota));
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900407 expectIptablesRestoreCommands(expected);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900408 }
409
410 for (const auto& iface : ifaces) {
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900411 // Quota rule is only removed when the total number of
412 // interfaces transitions from 1 -> 0.
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900413 bool last = (iface == ifaces[1]);
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900414 auto expected = removeInterfaceSharedQuotaCommands(iface, kQuota, last);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900415 EXPECT_EQ(0, mBw.removeInterfaceSharedQuota(iface));
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900416 expectIptablesRestoreCommands(expected);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900417 }
418}
419
Lorenzo Colittie8b56e42017-04-26 15:16:03 +0900420TEST_F(BandwidthControllerTest, IptablesAlertCmd) {
421 std::vector<std::string> expected = {
Luke Huangae038f82018-11-05 11:17:31 +0900422 "*filter\n"
423 "-I bw_global_alert -m quota2 ! --quota 123456 --name MyWonderfulAlert\n"
424 "COMMIT\n"};
Lorenzo Colittie8b56e42017-04-26 15:16:03 +0900425 EXPECT_EQ(0, runIptablesAlertCmd(IptOp::IptOpInsert, "MyWonderfulAlert", 123456));
Lorenzo Colitti3c272702017-04-26 15:48:13 +0900426 expectIptablesRestoreCommands(expected);
Lorenzo Colittie8b56e42017-04-26 15:16:03 +0900427
428 expected = {
Luke Huangae038f82018-11-05 11:17:31 +0900429 "*filter\n"
430 "-D bw_global_alert -m quota2 ! --quota 123456 --name MyWonderfulAlert\n"
431 "COMMIT\n"};
Lorenzo Colittie8b56e42017-04-26 15:16:03 +0900432 EXPECT_EQ(0, runIptablesAlertCmd(IptOp::IptOpDelete, "MyWonderfulAlert", 123456));
Lorenzo Colitti3c272702017-04-26 15:48:13 +0900433 expectIptablesRestoreCommands(expected);
Lorenzo Colittie8b56e42017-04-26 15:16:03 +0900434}
435
Lorenzo Colitti38078222017-07-06 17:27:23 +0900436TEST_F(BandwidthControllerTest, CostlyAlert) {
437 const int64_t kQuota = 123456;
438 int64_t alertBytes = 0;
439
440 std::vector<std::string> expected = {
Lorenzo Colittie85ffe12017-07-06 17:25:37 +0900441 "*filter\n"
442 "-A bw_costly_shared -m quota2 ! --quota 123456 --name sharedAlert\n"
443 "COMMIT\n"
Lorenzo Colitti38078222017-07-06 17:27:23 +0900444 };
445 EXPECT_EQ(0, setCostlyAlert("shared", kQuota, &alertBytes));
446 EXPECT_EQ(kQuota, alertBytes);
Lorenzo Colittie85ffe12017-07-06 17:25:37 +0900447 expectIptablesRestoreCommands(expected);
Lorenzo Colitti38078222017-07-06 17:27:23 +0900448
449 expected = {};
450 expectUpdateQuota(kQuota);
451 EXPECT_EQ(0, setCostlyAlert("shared", kQuota + 1, &alertBytes));
452 EXPECT_EQ(kQuota + 1, alertBytes);
Lorenzo Colittie85ffe12017-07-06 17:25:37 +0900453 expectIptablesRestoreCommands(expected);
Lorenzo Colitti38078222017-07-06 17:27:23 +0900454
455 expected = {
Lorenzo Colittie85ffe12017-07-06 17:25:37 +0900456 "*filter\n"
Lorenzo Colitti38078222017-07-06 17:27:23 +0900457 "-D bw_costly_shared -m quota2 ! --quota 123457 --name sharedAlert\n"
Lorenzo Colittie85ffe12017-07-06 17:25:37 +0900458 "COMMIT\n"
Lorenzo Colitti38078222017-07-06 17:27:23 +0900459 };
460 EXPECT_EQ(0, removeCostlyAlert("shared", &alertBytes));
461 EXPECT_EQ(0, alertBytes);
Lorenzo Colittie85ffe12017-07-06 17:25:37 +0900462 expectIptablesRestoreCommands(expected);
Lorenzo Colitti38078222017-07-06 17:27:23 +0900463}
464