blob: e62f340db2aff02d9ef4d1da10da0fc739740832 [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"
Lorenzo Colitti0f150552016-03-28 02:30:27 +090035#include "IptablesBaseTest.h"
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +090036#include "tun_interface.h"
37
Joel Scherpelz01cc5492017-06-16 10:45:14 +090038using ::testing::ByMove;
39using ::testing::Invoke;
40using ::testing::Return;
41using ::testing::StrictMock;
42using ::testing::Test;
43using ::testing::_;
44
Lorenzo Colitti48f83002017-07-06 15:06:04 +090045using android::base::Join;
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +090046using android::base::StringPrintf;
47using android::net::TunInterface;
Joel Scherpelz01cc5492017-06-16 10:45:14 +090048using android::netdutils::status::ok;
49using android::netdutils::UniqueFile;
Lorenzo Colitti86a47982016-03-18 17:52:25 +090050
Lorenzo Colitti0f150552016-03-28 02:30:27 +090051class BandwidthControllerTest : public IptablesBaseTest {
Joel Scherpelz01cc5492017-06-16 10:45:14 +090052protected:
Lorenzo Colitti86a47982016-03-18 17:52:25 +090053 BandwidthControllerTest() {
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +090054 BandwidthController::iptablesRestoreFunction = fakeExecIptablesRestoreWithOutput;
Lorenzo Colitti86a47982016-03-18 17:52:25 +090055 }
56 BandwidthController mBw;
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +090057 TunInterface mTun;
58
59 void SetUp() {
60 ASSERT_EQ(0, mTun.init());
61 }
62
63 void TearDown() {
64 mTun.destroy();
65 }
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +090066
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +090067 void expectSetupCommands(const std::string& expectedClean, std::string expectedAccounting) {
68 std::string expectedList =
69 "*filter\n"
70 "-S\n"
71 "COMMIT\n";
72
73 std::string expectedFlush =
74 "*filter\n"
75 ":bw_INPUT -\n"
76 ":bw_OUTPUT -\n"
77 ":bw_FORWARD -\n"
78 ":bw_happy_box -\n"
79 ":bw_penalty_box -\n"
80 ":bw_data_saver -\n"
81 ":bw_costly_shared -\n"
82 "COMMIT\n"
83 "*raw\n"
84 ":bw_raw_PREROUTING -\n"
85 "COMMIT\n"
86 "*mangle\n"
87 ":bw_mangle_POSTROUTING -\n"
88 "COMMIT\n";
89
90 ExpectedIptablesCommands expected = {{ V4, expectedList }};
91 if (expectedClean.size()) {
92 expected.push_back({ V4V6, expectedClean });
93 }
94 expected.push_back({ V4V6, expectedFlush });
95 if (expectedAccounting.size()) {
96 expected.push_back({ V4V6, expectedAccounting });
97 }
98
99 expectIptablesRestoreCommands(expected);
100 }
Lorenzo Colittie8b56e42017-04-26 15:16:03 +0900101
102 using IptOp = BandwidthController::IptOp;
103
104 int runIptablesAlertCmd(IptOp a, const char *b, int64_t c) {
105 return mBw.runIptablesAlertCmd(a, b, c);
106 }
107
108 int runIptablesAlertFwdCmd(IptOp a, const char *b, int64_t c) {
109 return mBw.runIptablesAlertFwdCmd(a, b, c);
110 }
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900111
Lorenzo Colitti38078222017-07-06 17:27:23 +0900112 int setCostlyAlert(const std::string a, int64_t b, int64_t *c) {
113 return mBw.setCostlyAlert(a, b, c);
114 }
115
116 int removeCostlyAlert(const std::string a, int64_t *b) {
117 return mBw.removeCostlyAlert(a, b);
118 }
119
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900120 void expectUpdateQuota(uint64_t quota) {
121 uintptr_t dummy;
122 FILE* dummyFile = reinterpret_cast<FILE*>(&dummy);
123
124 EXPECT_CALL(mSyscalls, fopen(_, _)).WillOnce(Return(ByMove(UniqueFile(dummyFile))));
125 EXPECT_CALL(mSyscalls, vfprintf(dummyFile, _, _))
126 .WillOnce(Invoke([quota](FILE*, const std::string&, va_list ap) {
127 EXPECT_EQ(quota, va_arg(ap, uint64_t));
128 return 0;
129 }));
130 EXPECT_CALL(mSyscalls, fclose(dummyFile)).WillOnce(Return(ok));
131 }
132
133 StrictMock<android::netdutils::ScopedMockSyscalls> mSyscalls;
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900134};
135
Lorenzo Colittia0dc8a52016-03-26 22:42:07 +0900136TEST_F(BandwidthControllerTest, TestSetupIptablesHooks) {
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900137 // Pretend some bw_costly_shared_<iface> rules already exist...
138 addIptablesRestoreOutput(
139 "-P OUTPUT ACCEPT\n"
140 "-N bw_costly_rmnet_data0\n"
141 "-N bw_costly_shared\n"
142 "-N unrelated\n"
143 "-N bw_costly_rmnet_data7\n");
144
145 // ... and expect that they be flushed and deleted.
146 std::string expectedCleanCmds =
Lorenzo Colitti13debb82016-03-27 17:46:30 +0900147 "*filter\n"
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900148 ":bw_costly_rmnet_data0 -\n"
149 "-X bw_costly_rmnet_data0\n"
150 ":bw_costly_rmnet_data7 -\n"
151 "-X bw_costly_rmnet_data7\n"
152 "COMMIT\n";
153
154 mBw.setupIptablesHooks();
155 expectSetupCommands(expectedCleanCmds, "");
Lorenzo Colittia0dc8a52016-03-26 22:42:07 +0900156}
157
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900158TEST_F(BandwidthControllerTest, TestEnableBandwidthControl) {
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900159 // Pretend no bw_costly_shared_<iface> rules already exist...
160 addIptablesRestoreOutput(
161 "-P OUTPUT ACCEPT\n"
162 "-N bw_costly_shared\n"
163 "-N unrelated\n");
164
165 // ... so none are flushed or deleted.
166 std::string expectedClean = "";
167
168 std::string expectedAccounting =
Lorenzo Colitti13debb82016-03-27 17:46:30 +0900169 "*filter\n"
170 "-A bw_INPUT -m owner --socket-exists\n"
171 "-A bw_OUTPUT -m owner --socket-exists\n"
172 "-A bw_costly_shared --jump bw_penalty_box\n"
173 "-A bw_penalty_box --jump bw_happy_box\n"
174 "-A bw_happy_box --jump bw_data_saver\n"
175 "-A bw_data_saver -j RETURN\n"
176 "-I bw_happy_box -m owner --uid-owner 0-9999 --jump RETURN\n"
177 "COMMIT\n"
178 "*raw\n"
179 "-A bw_raw_PREROUTING -m owner --socket-exists\n"
180 "COMMIT\n"
181 "*mangle\n"
182 "-A bw_mangle_POSTROUTING -m owner --socket-exists\n"
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900183 "COMMIT\n";
Lorenzo Colitti13debb82016-03-27 17:46:30 +0900184
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900185 mBw.enableBandwidthControl(false);
186 expectSetupCommands(expectedClean, expectedAccounting);
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900187}
188
Lorenzo Colittia0dc8a52016-03-26 22:42:07 +0900189TEST_F(BandwidthControllerTest, TestDisableBandwidthControl) {
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900190 // Pretend some bw_costly_shared_<iface> rules already exist...
191 addIptablesRestoreOutput(
192 "-P OUTPUT ACCEPT\n"
193 "-N bw_costly_rmnet_data0\n"
194 "-N bw_costly_shared\n"
195 "-N unrelated\n"
196 "-N bw_costly_rmnet_data7\n");
197
198 // ... and expect that they be flushed.
199 std::string expectedCleanCmds =
Lorenzo Colitti13debb82016-03-27 17:46:30 +0900200 "*filter\n"
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900201 ":bw_costly_rmnet_data0 -\n"
202 ":bw_costly_rmnet_data7 -\n"
203 "COMMIT\n";
204
205 mBw.disableBandwidthControl();
206 expectSetupCommands(expectedCleanCmds, "");
Lorenzo Colittia0dc8a52016-03-26 22:42:07 +0900207}
208
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900209TEST_F(BandwidthControllerTest, TestEnableDataSaver) {
210 mBw.enableDataSaver(true);
211 std::vector<std::string> expected = {
Lorenzo Colitti911bc4c2017-04-28 14:34:01 +0900212 "*filter\n"
213 "-R bw_data_saver 1 --jump REJECT\n"
214 "COMMIT\n"
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900215 };
Lorenzo Colitti911bc4c2017-04-28 14:34:01 +0900216 expectIptablesRestoreCommands(expected);
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900217
218 mBw.enableDataSaver(false);
219 expected = {
Lorenzo Colitti911bc4c2017-04-28 14:34:01 +0900220 "*filter\n"
221 "-R bw_data_saver 1 --jump RETURN\n"
222 "COMMIT\n"
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900223 };
Lorenzo Colitti911bc4c2017-04-28 14:34:01 +0900224 expectIptablesRestoreCommands(expected);
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900225}
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900226
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900227const std::vector<std::string> makeInterfaceQuotaCommands(const std::string& iface, int ruleIndex,
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +0900228 int64_t quota) {
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900229 const std::string chain = "bw_costly_" + iface;
230 const char* c_chain = chain.c_str();
231 const char* c_iface = iface.c_str();
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +0900232 std::vector<std::string> cmds = {
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900233 "*filter",
234 StringPrintf(":%s -", c_chain),
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900235 StringPrintf("-A %s -j bw_penalty_box", c_chain),
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900236 StringPrintf("-I bw_INPUT %d -i %s --jump %s", ruleIndex, c_iface, c_chain),
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900237 StringPrintf("-I bw_OUTPUT %d -o %s --jump %s", ruleIndex, c_iface, c_chain),
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900238 StringPrintf("-A bw_FORWARD -o %s --jump %s", c_iface, c_chain),
239 StringPrintf("-A %s -m quota2 ! --quota %" PRIu64 " --name %s --jump REJECT", c_chain,
240 quota, c_iface),
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900241 "COMMIT\n",
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +0900242 };
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900243 return {Join(cmds, "\n")};
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +0900244}
245
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900246const std::vector<std::string> removeInterfaceQuotaCommands(const std::string& iface) {
247 const std::string chain = "bw_costly_" + iface;
248 const char* c_chain = chain.c_str();
249 const char* c_iface = iface.c_str();
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +0900250 std::vector<std::string> cmds = {
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900251 "*filter",
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900252 StringPrintf("-D bw_INPUT -i %s --jump %s", c_iface, c_chain),
253 StringPrintf("-D bw_OUTPUT -o %s --jump %s", c_iface, c_chain),
254 StringPrintf("-D bw_FORWARD -o %s --jump %s", c_iface, c_chain),
255 StringPrintf("-F %s", c_chain),
256 StringPrintf("-X %s", c_chain),
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900257 "COMMIT\n",
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +0900258 };
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900259 return {Join(cmds, "\n")};
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +0900260}
261
262TEST_F(BandwidthControllerTest, TestSetInterfaceQuota) {
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900263 constexpr uint64_t kOldQuota = 123456;
264 const std::string iface = mTun.name();
265 std::vector<std::string> expected = makeInterfaceQuotaCommands(iface, 1, kOldQuota);
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +0900266
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900267 EXPECT_EQ(0, mBw.setInterfaceQuota(iface, kOldQuota));
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900268 expectIptablesRestoreCommands(expected);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900269
270 constexpr uint64_t kNewQuota = kOldQuota + 1;
271 expected = {};
272 expectUpdateQuota(kNewQuota);
273 EXPECT_EQ(0, mBw.setInterfaceQuota(iface, kNewQuota));
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900274 expectIptablesRestoreCommands(expected);
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +0900275
276 expected = removeInterfaceQuotaCommands(iface);
277 EXPECT_EQ(0, mBw.removeInterfaceQuota(iface));
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900278 expectIptablesRestoreCommands(expected);
Lorenzo Colittidf42ddd2017-02-28 01:20:13 +0900279}
Lorenzo Colittie8b56e42017-04-26 15:16:03 +0900280
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900281const std::vector<std::string> makeInterfaceSharedQuotaCommands(const std::string& iface,
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900282 int ruleIndex, int64_t quota,
283 bool insertQuota) {
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900284 const std::string chain = "bw_costly_shared";
285 const char* c_chain = chain.c_str();
286 const char* c_iface = iface.c_str();
287 std::vector<std::string> cmds = {
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900288 "*filter",
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900289 StringPrintf("-I bw_INPUT %d -i %s --jump %s", ruleIndex, c_iface, c_chain),
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900290 StringPrintf("-I bw_OUTPUT %d -o %s --jump %s", ruleIndex, c_iface, c_chain),
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900291 StringPrintf("-A bw_FORWARD -o %s --jump %s", c_iface, c_chain),
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900292 };
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900293 if (insertQuota) {
294 cmds.push_back(StringPrintf(
295 "-I %s -m quota2 ! --quota %" PRIu64 " --name shared --jump REJECT", c_chain, quota));
296 }
297 cmds.push_back("COMMIT\n");
298 return {Join(cmds, "\n")};
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900299}
300
301const std::vector<std::string> removeInterfaceSharedQuotaCommands(const std::string& iface,
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900302 int64_t quota, bool deleteQuota) {
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900303 const std::string chain = "bw_costly_shared";
304 const char* c_chain = chain.c_str();
305 const char* c_iface = iface.c_str();
306 std::vector<std::string> cmds = {
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900307 "*filter",
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900308 StringPrintf("-D bw_INPUT -i %s --jump %s", c_iface, c_chain),
309 StringPrintf("-D bw_OUTPUT -o %s --jump %s", c_iface, c_chain),
310 StringPrintf("-D bw_FORWARD -o %s --jump %s", c_iface, c_chain),
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900311 };
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900312 if (deleteQuota) {
313 cmds.push_back(StringPrintf(
314 "-D %s -m quota2 ! --quota %" PRIu64 " --name shared --jump REJECT", c_chain, quota));
315 }
316 cmds.push_back("COMMIT\n");
317 return {Join(cmds, "\n")};
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900318}
319
320TEST_F(BandwidthControllerTest, TestSetInterfaceSharedQuotaDuplicate) {
321 constexpr uint64_t kQuota = 123456;
322 const std::string iface = mTun.name();
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900323 std::vector<std::string> expected = makeInterfaceSharedQuotaCommands(iface, 1, 123456, true);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900324 EXPECT_EQ(0, mBw.setInterfaceSharedQuota(iface, kQuota));
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900325 expectIptablesRestoreCommands(expected);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900326
327 expected = {};
328 EXPECT_EQ(0, mBw.setInterfaceSharedQuota(iface, kQuota));
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900329 expectIptablesRestoreCommands(expected);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900330
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900331 expected = removeInterfaceSharedQuotaCommands(iface, kQuota, true);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900332 EXPECT_EQ(0, mBw.removeInterfaceSharedQuota(iface));
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900333 expectIptablesRestoreCommands(expected);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900334}
335
336TEST_F(BandwidthControllerTest, TestSetInterfaceSharedQuotaUpdate) {
337 constexpr uint64_t kOldQuota = 123456;
338 const std::string iface = mTun.name();
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900339 std::vector<std::string> expected = makeInterfaceSharedQuotaCommands(iface, 1, kOldQuota, true);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900340 EXPECT_EQ(0, mBw.setInterfaceSharedQuota(iface, kOldQuota));
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900341 expectIptablesRestoreCommands(expected);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900342
343 constexpr uint64_t kNewQuota = kOldQuota + 1;
344 expected = {};
345 expectUpdateQuota(kNewQuota);
346 EXPECT_EQ(0, mBw.setInterfaceSharedQuota(iface, kNewQuota));
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900347 expectIptablesRestoreCommands(expected);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900348
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900349 expected = removeInterfaceSharedQuotaCommands(iface, kNewQuota, true);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900350 EXPECT_EQ(0, mBw.removeInterfaceSharedQuota(iface));
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900351 expectIptablesRestoreCommands(expected);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900352}
353
354TEST_F(BandwidthControllerTest, TestSetInterfaceSharedQuotaTwoInterfaces) {
355 constexpr uint64_t kQuota = 123456;
356 const std::vector<std::string> ifaces{
357 {"a" + mTun.name()},
358 {"b" + mTun.name()},
359 };
360
361 for (const auto& iface : ifaces) {
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900362 // Quota rule is only added when the total number of
363 // interfaces transitions from 0 -> 1.
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900364 bool first = (iface == ifaces[0]);
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900365 auto expected = makeInterfaceSharedQuotaCommands(iface, 1, kQuota, first);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900366 EXPECT_EQ(0, mBw.setInterfaceSharedQuota(iface, kQuota));
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900367 expectIptablesRestoreCommands(expected);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900368 }
369
370 for (const auto& iface : ifaces) {
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900371 // Quota rule is only removed when the total number of
372 // interfaces transitions from 1 -> 0.
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900373 bool last = (iface == ifaces[1]);
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900374 auto expected = removeInterfaceSharedQuotaCommands(iface, kQuota, last);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900375 EXPECT_EQ(0, mBw.removeInterfaceSharedQuota(iface));
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900376 expectIptablesRestoreCommands(expected);
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900377 }
378}
379
Lorenzo Colittie8b56e42017-04-26 15:16:03 +0900380TEST_F(BandwidthControllerTest, IptablesAlertCmd) {
381 std::vector<std::string> expected = {
Lorenzo Colitti3c272702017-04-26 15:48:13 +0900382 "*filter\n"
383 "-I bw_INPUT -m quota2 ! --quota 123456 --name MyWonderfulAlert\n"
384 "-I bw_OUTPUT -m quota2 ! --quota 123456 --name MyWonderfulAlert\n"
385 "COMMIT\n"
Lorenzo Colittie8b56e42017-04-26 15:16:03 +0900386 };
387 EXPECT_EQ(0, runIptablesAlertCmd(IptOp::IptOpInsert, "MyWonderfulAlert", 123456));
Lorenzo Colitti3c272702017-04-26 15:48:13 +0900388 expectIptablesRestoreCommands(expected);
Lorenzo Colittie8b56e42017-04-26 15:16:03 +0900389
390 expected = {
Lorenzo Colitti3c272702017-04-26 15:48:13 +0900391 "*filter\n"
392 "-D bw_INPUT -m quota2 ! --quota 123456 --name MyWonderfulAlert\n"
393 "-D bw_OUTPUT -m quota2 ! --quota 123456 --name MyWonderfulAlert\n"
394 "COMMIT\n"
Lorenzo Colittie8b56e42017-04-26 15:16:03 +0900395 };
396 EXPECT_EQ(0, runIptablesAlertCmd(IptOp::IptOpDelete, "MyWonderfulAlert", 123456));
Lorenzo Colitti3c272702017-04-26 15:48:13 +0900397 expectIptablesRestoreCommands(expected);
Lorenzo Colittie8b56e42017-04-26 15:16:03 +0900398}
399
400TEST_F(BandwidthControllerTest, IptablesAlertFwdCmd) {
401 std::vector<std::string> expected = {
Lorenzo Colitti3c272702017-04-26 15:48:13 +0900402 "*filter\n"
403 "-I bw_FORWARD -m quota2 ! --quota 123456 --name MyWonderfulAlert\n"
404 "COMMIT\n"
Lorenzo Colittie8b56e42017-04-26 15:16:03 +0900405 };
406 EXPECT_EQ(0, runIptablesAlertFwdCmd(IptOp::IptOpInsert, "MyWonderfulAlert", 123456));
Lorenzo Colitti3c272702017-04-26 15:48:13 +0900407 expectIptablesRestoreCommands(expected);
Lorenzo Colittie8b56e42017-04-26 15:16:03 +0900408
409 expected = {
Lorenzo Colitti3c272702017-04-26 15:48:13 +0900410 "*filter\n"
411 "-D bw_FORWARD -m quota2 ! --quota 123456 --name MyWonderfulAlert\n"
412 "COMMIT\n"
Lorenzo Colittie8b56e42017-04-26 15:16:03 +0900413 };
414 EXPECT_EQ(0, runIptablesAlertFwdCmd(IptOp::IptOpDelete, "MyWonderfulAlert", 123456));
Lorenzo Colitti3c272702017-04-26 15:48:13 +0900415 expectIptablesRestoreCommands(expected);
Lorenzo Colittie8b56e42017-04-26 15:16:03 +0900416}
Lorenzo Colittif4dfa682017-04-28 11:09:07 +0900417
Lorenzo Colitti38078222017-07-06 17:27:23 +0900418TEST_F(BandwidthControllerTest, CostlyAlert) {
419 const int64_t kQuota = 123456;
420 int64_t alertBytes = 0;
421
422 std::vector<std::string> expected = {
Lorenzo Colittie85ffe12017-07-06 17:25:37 +0900423 "*filter\n"
424 "-A bw_costly_shared -m quota2 ! --quota 123456 --name sharedAlert\n"
425 "COMMIT\n"
Lorenzo Colitti38078222017-07-06 17:27:23 +0900426 };
427 EXPECT_EQ(0, setCostlyAlert("shared", kQuota, &alertBytes));
428 EXPECT_EQ(kQuota, alertBytes);
Lorenzo Colittie85ffe12017-07-06 17:25:37 +0900429 expectIptablesRestoreCommands(expected);
Lorenzo Colitti38078222017-07-06 17:27:23 +0900430
431 expected = {};
432 expectUpdateQuota(kQuota);
433 EXPECT_EQ(0, setCostlyAlert("shared", kQuota + 1, &alertBytes));
434 EXPECT_EQ(kQuota + 1, alertBytes);
Lorenzo Colittie85ffe12017-07-06 17:25:37 +0900435 expectIptablesRestoreCommands(expected);
Lorenzo Colitti38078222017-07-06 17:27:23 +0900436
437 expected = {
Lorenzo Colittie85ffe12017-07-06 17:25:37 +0900438 "*filter\n"
Lorenzo Colitti38078222017-07-06 17:27:23 +0900439 "-D bw_costly_shared -m quota2 ! --quota 123457 --name sharedAlert\n"
Lorenzo Colittie85ffe12017-07-06 17:25:37 +0900440 "COMMIT\n"
Lorenzo Colitti38078222017-07-06 17:27:23 +0900441 };
442 EXPECT_EQ(0, removeCostlyAlert("shared", &alertBytes));
443 EXPECT_EQ(0, alertBytes);
Lorenzo Colittie85ffe12017-07-06 17:25:37 +0900444 expectIptablesRestoreCommands(expected);
Lorenzo Colitti38078222017-07-06 17:27:23 +0900445}
446
Lorenzo Colittif4dfa682017-04-28 11:09:07 +0900447TEST_F(BandwidthControllerTest, ManipulateSpecialApps) {
448 std::vector<const char *> appUids = { "1000", "1001", "10012" };
449
450 std::vector<std::string> expected = {
Lorenzo Colitti911bc4c2017-04-28 14:34:01 +0900451 "*filter\n"
452 "-I bw_happy_box -m owner --uid-owner 1000 --jump RETURN\n"
453 "-I bw_happy_box -m owner --uid-owner 1001 --jump RETURN\n"
454 "-I bw_happy_box -m owner --uid-owner 10012 --jump RETURN\n"
455 "COMMIT\n"
Lorenzo Colittif4dfa682017-04-28 11:09:07 +0900456 };
457 EXPECT_EQ(0, mBw.addNiceApps(appUids.size(), const_cast<char**>(&appUids[0])));
Lorenzo Colitti911bc4c2017-04-28 14:34:01 +0900458 expectIptablesRestoreCommands(expected);
Lorenzo Colittif4dfa682017-04-28 11:09:07 +0900459
460 expected = {
Lorenzo Colitti911bc4c2017-04-28 14:34:01 +0900461 "*filter\n"
462 "-D bw_penalty_box -m owner --uid-owner 1000 --jump REJECT\n"
463 "-D bw_penalty_box -m owner --uid-owner 1001 --jump REJECT\n"
464 "-D bw_penalty_box -m owner --uid-owner 10012 --jump REJECT\n"
465 "COMMIT\n"
Lorenzo Colittif4dfa682017-04-28 11:09:07 +0900466 };
467 EXPECT_EQ(0, mBw.removeNaughtyApps(appUids.size(), const_cast<char**>(&appUids[0])));
Lorenzo Colitti911bc4c2017-04-28 14:34:01 +0900468 expectIptablesRestoreCommands(expected);
Lorenzo Colittif4dfa682017-04-28 11:09:07 +0900469}