Lorenzo Colitti | 86a4798 | 2016-03-18 17:52:25 +0900 | [diff] [blame] | 1 | /* |
| 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 Colitti | 86a4798 | 2016-03-18 17:52:25 +0900 | [diff] [blame] | 21 | |
Lorenzo Colitti | df42ddd | 2017-02-28 01:20:13 +0900 | [diff] [blame] | 22 | #include <inttypes.h> |
Lorenzo Colitti | bbeaf9a | 2016-07-08 18:24:26 +0900 | [diff] [blame] | 23 | #include <fcntl.h> |
| 24 | #include <unistd.h> |
| 25 | #include <sys/types.h> |
| 26 | #include <sys/socket.h> |
| 27 | |
Lorenzo Colitti | 86a4798 | 2016-03-18 17:52:25 +0900 | [diff] [blame] | 28 | #include <gtest/gtest.h> |
| 29 | |
Lorenzo Colitti | 13debb8 | 2016-03-27 17:46:30 +0900 | [diff] [blame] | 30 | #include <android-base/strings.h> |
Lorenzo Colitti | 56c4b1e | 2017-02-01 02:45:10 +0900 | [diff] [blame] | 31 | #include <android-base/stringprintf.h> |
Lorenzo Colitti | 13debb8 | 2016-03-27 17:46:30 +0900 | [diff] [blame] | 32 | |
Joel Scherpelz | 01cc549 | 2017-06-16 10:45:14 +0900 | [diff] [blame] | 33 | #include <netdutils/MockSyscalls.h> |
Lorenzo Colitti | 86a4798 | 2016-03-18 17:52:25 +0900 | [diff] [blame] | 34 | #include "BandwidthController.h" |
Benedict Wong | b9baf26 | 2017-12-03 15:43:08 -0800 | [diff] [blame] | 35 | #include "Fwmark.h" |
Lorenzo Colitti | 0f15055 | 2016-03-28 02:30:27 +0900 | [diff] [blame] | 36 | #include "IptablesBaseTest.h" |
Chenbo Feng | a121e20 | 2018-03-19 11:51:54 -0700 | [diff] [blame] | 37 | #include "bpf/BpfUtils.h" |
Chenbo Feng | d6104d1 | 2018-10-16 20:29:29 -0700 | [diff] [blame^] | 38 | #include "netdbpf/bpf_shared.h" |
Lorenzo Colitti | df42ddd | 2017-02-28 01:20:13 +0900 | [diff] [blame] | 39 | #include "tun_interface.h" |
| 40 | |
Joel Scherpelz | 01cc549 | 2017-06-16 10:45:14 +0900 | [diff] [blame] | 41 | using ::testing::ByMove; |
| 42 | using ::testing::Invoke; |
| 43 | using ::testing::Return; |
| 44 | using ::testing::StrictMock; |
| 45 | using ::testing::Test; |
| 46 | using ::testing::_; |
| 47 | |
Lorenzo Colitti | 48f8300 | 2017-07-06 15:06:04 +0900 | [diff] [blame] | 48 | using android::base::Join; |
Lorenzo Colitti | df42ddd | 2017-02-28 01:20:13 +0900 | [diff] [blame] | 49 | using android::base::StringPrintf; |
| 50 | using android::net::TunInterface; |
Joel Scherpelz | 01cc549 | 2017-06-16 10:45:14 +0900 | [diff] [blame] | 51 | using android::netdutils::status::ok; |
| 52 | using android::netdutils::UniqueFile; |
Lorenzo Colitti | 86a4798 | 2016-03-18 17:52:25 +0900 | [diff] [blame] | 53 | |
Chenbo Feng | 44c0f44 | 2018-07-10 16:54:30 -0700 | [diff] [blame] | 54 | const std::string ACCOUNT_RULES_WITHOUT_BPF = |
| 55 | "*filter\n" |
| 56 | "-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" |
| 60 | "-A bw_OUTPUT -o ipsec+ -j RETURN\n" |
| 61 | "-A bw_OUTPUT -m policy --pol ipsec --dir out -j RETURN\n" |
| 62 | "-A bw_OUTPUT -m owner --socket-exists\n" |
| 63 | "-A bw_costly_shared --jump bw_penalty_box\n" |
| 64 | "\n" |
| 65 | "-A bw_penalty_box --jump bw_happy_box\n" |
| 66 | "-A bw_happy_box --jump bw_data_saver\n" |
| 67 | "-A bw_data_saver -j RETURN\n" |
| 68 | "-I bw_happy_box -m owner --uid-owner 0-9999 --jump RETURN\n" |
| 69 | "COMMIT\n" |
| 70 | "*raw\n" |
| 71 | "-A bw_raw_PREROUTING -i ipsec+ -j RETURN\n" |
| 72 | "-A bw_raw_PREROUTING -m policy --pol ipsec --dir in -j RETURN\n" |
| 73 | "-A bw_raw_PREROUTING -m owner --socket-exists\n" |
| 74 | "COMMIT\n" |
| 75 | "*mangle\n" |
| 76 | "-A bw_mangle_POSTROUTING -o ipsec+ -j RETURN\n" |
| 77 | "-A bw_mangle_POSTROUTING -m policy --pol ipsec --dir out -j RETURN\n" |
| 78 | "-A bw_mangle_POSTROUTING -m owner --socket-exists\n" |
| 79 | "-A bw_mangle_POSTROUTING -j MARK --set-mark 0x0/0x100000\n" |
| 80 | "\n" |
| 81 | "COMMIT\n"; |
| 82 | |
| 83 | const std::string ACCOUNT_RULES_WITH_BPF = |
| 84 | "*filter\n" |
| 85 | "-A bw_INPUT -p esp -j RETURN\n" |
| 86 | "-A bw_INPUT -m mark --mark 0x100000/0x100000 -j RETURN\n" |
| 87 | "\n" |
| 88 | "-A bw_INPUT -j MARK --or-mark 0x100000\n" |
| 89 | "-A bw_OUTPUT -o ipsec+ -j RETURN\n" |
| 90 | "-A bw_OUTPUT -m policy --pol ipsec --dir out -j RETURN\n" |
| 91 | "\n" |
| 92 | "-A bw_costly_shared --jump bw_penalty_box\n" + |
| 93 | StringPrintf("-I bw_penalty_box -m bpf --object-pinned %s -j REJECT\n", |
| 94 | XT_BPF_BLACKLIST_PROG_PATH) + |
| 95 | "-A bw_penalty_box --jump bw_happy_box\n" |
| 96 | "-A bw_happy_box --jump bw_data_saver\n" |
| 97 | "-A bw_data_saver -j RETURN\n" + |
| 98 | StringPrintf("-I bw_happy_box -m bpf --object-pinned %s -j RETURN\n", |
| 99 | XT_BPF_WHITELIST_PROG_PATH) + |
| 100 | "COMMIT\n" |
| 101 | "*raw\n" |
| 102 | "-A bw_raw_PREROUTING -i ipsec+ -j RETURN\n" |
| 103 | "-A bw_raw_PREROUTING -m policy --pol ipsec --dir in -j RETURN\n" + |
| 104 | StringPrintf("-A bw_raw_PREROUTING -m bpf --object-pinned %s\n", XT_BPF_INGRESS_PROG_PATH) + |
| 105 | "COMMIT\n" |
| 106 | "*mangle\n" |
| 107 | "-A bw_mangle_POSTROUTING -o ipsec+ -j RETURN\n" |
| 108 | "-A bw_mangle_POSTROUTING -m policy --pol ipsec --dir out -j RETURN\n" |
| 109 | "\n" |
| 110 | "-A bw_mangle_POSTROUTING -j MARK --set-mark 0x0/0x100000\n" + |
| 111 | StringPrintf("-A bw_mangle_POSTROUTING -m bpf --object-pinned %s\n", |
| 112 | XT_BPF_EGRESS_PROG_PATH) + |
| 113 | "COMMIT\n"; |
| 114 | |
Lorenzo Colitti | 0f15055 | 2016-03-28 02:30:27 +0900 | [diff] [blame] | 115 | class BandwidthControllerTest : public IptablesBaseTest { |
Joel Scherpelz | 01cc549 | 2017-06-16 10:45:14 +0900 | [diff] [blame] | 116 | protected: |
Lorenzo Colitti | 86a4798 | 2016-03-18 17:52:25 +0900 | [diff] [blame] | 117 | BandwidthControllerTest() { |
Lorenzo Colitti | 56c4b1e | 2017-02-01 02:45:10 +0900 | [diff] [blame] | 118 | BandwidthController::iptablesRestoreFunction = fakeExecIptablesRestoreWithOutput; |
Lorenzo Colitti | 86a4798 | 2016-03-18 17:52:25 +0900 | [diff] [blame] | 119 | } |
| 120 | BandwidthController mBw; |
Lorenzo Colitti | df42ddd | 2017-02-28 01:20:13 +0900 | [diff] [blame] | 121 | TunInterface mTun; |
| 122 | |
| 123 | void SetUp() { |
| 124 | ASSERT_EQ(0, mTun.init()); |
| 125 | } |
| 126 | |
| 127 | void TearDown() { |
| 128 | mTun.destroy(); |
| 129 | } |
Lorenzo Colitti | bbeaf9a | 2016-07-08 18:24:26 +0900 | [diff] [blame] | 130 | |
Lorenzo Colitti | 56c4b1e | 2017-02-01 02:45:10 +0900 | [diff] [blame] | 131 | void expectSetupCommands(const std::string& expectedClean, std::string expectedAccounting) { |
| 132 | std::string expectedList = |
| 133 | "*filter\n" |
| 134 | "-S\n" |
| 135 | "COMMIT\n"; |
| 136 | |
| 137 | std::string expectedFlush = |
| 138 | "*filter\n" |
| 139 | ":bw_INPUT -\n" |
| 140 | ":bw_OUTPUT -\n" |
| 141 | ":bw_FORWARD -\n" |
| 142 | ":bw_happy_box -\n" |
| 143 | ":bw_penalty_box -\n" |
| 144 | ":bw_data_saver -\n" |
| 145 | ":bw_costly_shared -\n" |
| 146 | "COMMIT\n" |
| 147 | "*raw\n" |
| 148 | ":bw_raw_PREROUTING -\n" |
| 149 | "COMMIT\n" |
| 150 | "*mangle\n" |
| 151 | ":bw_mangle_POSTROUTING -\n" |
| 152 | "COMMIT\n"; |
| 153 | |
| 154 | ExpectedIptablesCommands expected = {{ V4, expectedList }}; |
| 155 | if (expectedClean.size()) { |
| 156 | expected.push_back({ V4V6, expectedClean }); |
| 157 | } |
| 158 | expected.push_back({ V4V6, expectedFlush }); |
| 159 | if (expectedAccounting.size()) { |
| 160 | expected.push_back({ V4V6, expectedAccounting }); |
| 161 | } |
| 162 | |
| 163 | expectIptablesRestoreCommands(expected); |
| 164 | } |
Lorenzo Colitti | e8b56e4 | 2017-04-26 15:16:03 +0900 | [diff] [blame] | 165 | |
| 166 | using IptOp = BandwidthController::IptOp; |
| 167 | |
| 168 | int runIptablesAlertCmd(IptOp a, const char *b, int64_t c) { |
| 169 | return mBw.runIptablesAlertCmd(a, b, c); |
| 170 | } |
| 171 | |
| 172 | int runIptablesAlertFwdCmd(IptOp a, const char *b, int64_t c) { |
| 173 | return mBw.runIptablesAlertFwdCmd(a, b, c); |
| 174 | } |
Joel Scherpelz | 01cc549 | 2017-06-16 10:45:14 +0900 | [diff] [blame] | 175 | |
Bernie Innocenti | 7e25ec0 | 2018-07-02 19:32:17 +0900 | [diff] [blame] | 176 | int setCostlyAlert(const std::string& a, int64_t b, int64_t* c) { |
Lorenzo Colitti | 3807822 | 2017-07-06 17:27:23 +0900 | [diff] [blame] | 177 | return mBw.setCostlyAlert(a, b, c); |
| 178 | } |
| 179 | |
Bernie Innocenti | 7e25ec0 | 2018-07-02 19:32:17 +0900 | [diff] [blame] | 180 | int removeCostlyAlert(const std::string& a, int64_t* b) { return mBw.removeCostlyAlert(a, b); } |
Lorenzo Colitti | 3807822 | 2017-07-06 17:27:23 +0900 | [diff] [blame] | 181 | |
Joel Scherpelz | 01cc549 | 2017-06-16 10:45:14 +0900 | [diff] [blame] | 182 | void expectUpdateQuota(uint64_t quota) { |
| 183 | uintptr_t dummy; |
| 184 | FILE* dummyFile = reinterpret_cast<FILE*>(&dummy); |
| 185 | |
| 186 | EXPECT_CALL(mSyscalls, fopen(_, _)).WillOnce(Return(ByMove(UniqueFile(dummyFile)))); |
| 187 | EXPECT_CALL(mSyscalls, vfprintf(dummyFile, _, _)) |
| 188 | .WillOnce(Invoke([quota](FILE*, const std::string&, va_list ap) { |
| 189 | EXPECT_EQ(quota, va_arg(ap, uint64_t)); |
| 190 | return 0; |
| 191 | })); |
| 192 | EXPECT_CALL(mSyscalls, fclose(dummyFile)).WillOnce(Return(ok)); |
| 193 | } |
| 194 | |
Chenbo Feng | 44c0f44 | 2018-07-10 16:54:30 -0700 | [diff] [blame] | 195 | void checkBandwithControl(bool useBpf) { |
| 196 | // Pretend no bw_costly_shared_<iface> rules already exist... |
| 197 | addIptablesRestoreOutput( |
| 198 | "-P OUTPUT ACCEPT\n" |
| 199 | "-N bw_costly_shared\n" |
| 200 | "-N unrelated\n"); |
| 201 | |
| 202 | // ... so none are flushed or deleted. |
| 203 | std::string expectedClean = ""; |
| 204 | |
| 205 | std::string expectedAccounting = |
| 206 | useBpf ? ACCOUNT_RULES_WITH_BPF : ACCOUNT_RULES_WITHOUT_BPF; |
| 207 | mBw.setBpfEnabled(useBpf); |
Luke Huang | f44a3c1 | 2018-09-07 12:10:12 +0800 | [diff] [blame] | 208 | mBw.enableBandwidthControl(); |
Chenbo Feng | 44c0f44 | 2018-07-10 16:54:30 -0700 | [diff] [blame] | 209 | expectSetupCommands(expectedClean, expectedAccounting); |
| 210 | } |
| 211 | |
Joel Scherpelz | 01cc549 | 2017-06-16 10:45:14 +0900 | [diff] [blame] | 212 | StrictMock<android::netdutils::ScopedMockSyscalls> mSyscalls; |
Lorenzo Colitti | 86a4798 | 2016-03-18 17:52:25 +0900 | [diff] [blame] | 213 | }; |
| 214 | |
Lorenzo Colitti | a0dc8a5 | 2016-03-26 22:42:07 +0900 | [diff] [blame] | 215 | TEST_F(BandwidthControllerTest, TestSetupIptablesHooks) { |
Lorenzo Colitti | 56c4b1e | 2017-02-01 02:45:10 +0900 | [diff] [blame] | 216 | // Pretend some bw_costly_shared_<iface> rules already exist... |
| 217 | addIptablesRestoreOutput( |
| 218 | "-P OUTPUT ACCEPT\n" |
| 219 | "-N bw_costly_rmnet_data0\n" |
| 220 | "-N bw_costly_shared\n" |
| 221 | "-N unrelated\n" |
| 222 | "-N bw_costly_rmnet_data7\n"); |
| 223 | |
| 224 | // ... and expect that they be flushed and deleted. |
| 225 | std::string expectedCleanCmds = |
Lorenzo Colitti | 13debb8 | 2016-03-27 17:46:30 +0900 | [diff] [blame] | 226 | "*filter\n" |
Lorenzo Colitti | 56c4b1e | 2017-02-01 02:45:10 +0900 | [diff] [blame] | 227 | ":bw_costly_rmnet_data0 -\n" |
| 228 | "-X bw_costly_rmnet_data0\n" |
| 229 | ":bw_costly_rmnet_data7 -\n" |
| 230 | "-X bw_costly_rmnet_data7\n" |
| 231 | "COMMIT\n"; |
| 232 | |
| 233 | mBw.setupIptablesHooks(); |
| 234 | expectSetupCommands(expectedCleanCmds, ""); |
Lorenzo Colitti | a0dc8a5 | 2016-03-26 22:42:07 +0900 | [diff] [blame] | 235 | } |
| 236 | |
Benedict Wong | b9baf26 | 2017-12-03 15:43:08 -0800 | [diff] [blame] | 237 | TEST_F(BandwidthControllerTest, TestCheckUidBillingMask) { |
| 238 | uint32_t uidBillingMask = Fwmark::getUidBillingMask(); |
| 239 | |
| 240 | // If mask is non-zero, and mask & mask-1 is equal to 0, then the mask is a power of two. |
| 241 | bool isPowerOfTwo = uidBillingMask && (uidBillingMask & (uidBillingMask - 1)) == 0; |
| 242 | |
| 243 | // Must be exactly a power of two |
| 244 | EXPECT_TRUE(isPowerOfTwo); |
| 245 | } |
| 246 | |
Chenbo Feng | 44c0f44 | 2018-07-10 16:54:30 -0700 | [diff] [blame] | 247 | TEST_F(BandwidthControllerTest, TestEnableBandwidthControlWithBpf) { |
| 248 | checkBandwithControl(true); |
| 249 | } |
Lorenzo Colitti | 56c4b1e | 2017-02-01 02:45:10 +0900 | [diff] [blame] | 250 | |
Chenbo Feng | 44c0f44 | 2018-07-10 16:54:30 -0700 | [diff] [blame] | 251 | TEST_F(BandwidthControllerTest, TestEnableBandwidthControlWithoutBpf) { |
| 252 | checkBandwithControl(false); |
Lorenzo Colitti | 86a4798 | 2016-03-18 17:52:25 +0900 | [diff] [blame] | 253 | } |
| 254 | |
Lorenzo Colitti | a0dc8a5 | 2016-03-26 22:42:07 +0900 | [diff] [blame] | 255 | TEST_F(BandwidthControllerTest, TestDisableBandwidthControl) { |
Lorenzo Colitti | 56c4b1e | 2017-02-01 02:45:10 +0900 | [diff] [blame] | 256 | // Pretend some bw_costly_shared_<iface> rules already exist... |
| 257 | addIptablesRestoreOutput( |
| 258 | "-P OUTPUT ACCEPT\n" |
| 259 | "-N bw_costly_rmnet_data0\n" |
| 260 | "-N bw_costly_shared\n" |
| 261 | "-N unrelated\n" |
| 262 | "-N bw_costly_rmnet_data7\n"); |
| 263 | |
| 264 | // ... and expect that they be flushed. |
| 265 | std::string expectedCleanCmds = |
Lorenzo Colitti | 13debb8 | 2016-03-27 17:46:30 +0900 | [diff] [blame] | 266 | "*filter\n" |
Lorenzo Colitti | 56c4b1e | 2017-02-01 02:45:10 +0900 | [diff] [blame] | 267 | ":bw_costly_rmnet_data0 -\n" |
| 268 | ":bw_costly_rmnet_data7 -\n" |
| 269 | "COMMIT\n"; |
| 270 | |
| 271 | mBw.disableBandwidthControl(); |
| 272 | expectSetupCommands(expectedCleanCmds, ""); |
Lorenzo Colitti | a0dc8a5 | 2016-03-26 22:42:07 +0900 | [diff] [blame] | 273 | } |
| 274 | |
Lorenzo Colitti | 86a4798 | 2016-03-18 17:52:25 +0900 | [diff] [blame] | 275 | TEST_F(BandwidthControllerTest, TestEnableDataSaver) { |
| 276 | mBw.enableDataSaver(true); |
Lorenzo Colitti | aff2879 | 2017-09-26 17:46:18 +0900 | [diff] [blame] | 277 | std::string expected4 = |
Lorenzo Colitti | 911bc4c | 2017-04-28 14:34:01 +0900 | [diff] [blame] | 278 | "*filter\n" |
Lorenzo Colitti | aff2879 | 2017-09-26 17:46:18 +0900 | [diff] [blame] | 279 | ":bw_data_saver -\n" |
| 280 | "-A bw_data_saver --jump REJECT\n" |
| 281 | "COMMIT\n"; |
| 282 | std::string expected6 = |
| 283 | "*filter\n" |
| 284 | ":bw_data_saver -\n" |
| 285 | "-A bw_data_saver -p icmpv6 --icmpv6-type packet-too-big -j RETURN\n" |
| 286 | "-A bw_data_saver -p icmpv6 --icmpv6-type router-solicitation -j RETURN\n" |
| 287 | "-A bw_data_saver -p icmpv6 --icmpv6-type router-advertisement -j RETURN\n" |
| 288 | "-A bw_data_saver -p icmpv6 --icmpv6-type neighbour-solicitation -j RETURN\n" |
| 289 | "-A bw_data_saver -p icmpv6 --icmpv6-type neighbour-advertisement -j RETURN\n" |
| 290 | "-A bw_data_saver -p icmpv6 --icmpv6-type redirect -j RETURN\n" |
| 291 | "-A bw_data_saver --jump REJECT\n" |
| 292 | "COMMIT\n"; |
| 293 | expectIptablesRestoreCommands({ |
| 294 | {V4, expected4}, |
| 295 | {V6, expected6}, |
| 296 | }); |
Lorenzo Colitti | 86a4798 | 2016-03-18 17:52:25 +0900 | [diff] [blame] | 297 | |
| 298 | mBw.enableDataSaver(false); |
Lorenzo Colitti | aff2879 | 2017-09-26 17:46:18 +0900 | [diff] [blame] | 299 | std::string expected = { |
Lorenzo Colitti | 911bc4c | 2017-04-28 14:34:01 +0900 | [diff] [blame] | 300 | "*filter\n" |
Lorenzo Colitti | aff2879 | 2017-09-26 17:46:18 +0900 | [diff] [blame] | 301 | ":bw_data_saver -\n" |
| 302 | "-A bw_data_saver --jump RETURN\n" |
Lorenzo Colitti | 911bc4c | 2017-04-28 14:34:01 +0900 | [diff] [blame] | 303 | "COMMIT\n" |
Lorenzo Colitti | 86a4798 | 2016-03-18 17:52:25 +0900 | [diff] [blame] | 304 | }; |
Lorenzo Colitti | aff2879 | 2017-09-26 17:46:18 +0900 | [diff] [blame] | 305 | expectIptablesRestoreCommands({ |
| 306 | {V4, expected}, |
| 307 | {V6, expected}, |
| 308 | }); |
Lorenzo Colitti | 86a4798 | 2016-03-18 17:52:25 +0900 | [diff] [blame] | 309 | } |
Lorenzo Colitti | bbeaf9a | 2016-07-08 18:24:26 +0900 | [diff] [blame] | 310 | |
Joel Scherpelz | 01cc549 | 2017-06-16 10:45:14 +0900 | [diff] [blame] | 311 | const std::vector<std::string> makeInterfaceQuotaCommands(const std::string& iface, int ruleIndex, |
Lorenzo Colitti | df42ddd | 2017-02-28 01:20:13 +0900 | [diff] [blame] | 312 | int64_t quota) { |
Joel Scherpelz | 01cc549 | 2017-06-16 10:45:14 +0900 | [diff] [blame] | 313 | const std::string chain = "bw_costly_" + iface; |
| 314 | const char* c_chain = chain.c_str(); |
| 315 | const char* c_iface = iface.c_str(); |
Lorenzo Colitti | df42ddd | 2017-02-28 01:20:13 +0900 | [diff] [blame] | 316 | std::vector<std::string> cmds = { |
Lorenzo Colitti | 48f8300 | 2017-07-06 15:06:04 +0900 | [diff] [blame] | 317 | "*filter", |
| 318 | StringPrintf(":%s -", c_chain), |
Joel Scherpelz | 01cc549 | 2017-06-16 10:45:14 +0900 | [diff] [blame] | 319 | StringPrintf("-A %s -j bw_penalty_box", c_chain), |
Joel Scherpelz | 01cc549 | 2017-06-16 10:45:14 +0900 | [diff] [blame] | 320 | StringPrintf("-I bw_INPUT %d -i %s --jump %s", ruleIndex, c_iface, c_chain), |
Joel Scherpelz | 01cc549 | 2017-06-16 10:45:14 +0900 | [diff] [blame] | 321 | StringPrintf("-I bw_OUTPUT %d -o %s --jump %s", ruleIndex, c_iface, c_chain), |
Erik Kline | 51eb324 | 2017-09-20 18:30:47 +0900 | [diff] [blame] | 322 | StringPrintf("-A bw_FORWARD -i %s --jump %s", c_iface, c_chain), |
Joel Scherpelz | 01cc549 | 2017-06-16 10:45:14 +0900 | [diff] [blame] | 323 | StringPrintf("-A bw_FORWARD -o %s --jump %s", c_iface, c_chain), |
| 324 | StringPrintf("-A %s -m quota2 ! --quota %" PRIu64 " --name %s --jump REJECT", c_chain, |
| 325 | quota, c_iface), |
Lorenzo Colitti | 48f8300 | 2017-07-06 15:06:04 +0900 | [diff] [blame] | 326 | "COMMIT\n", |
Lorenzo Colitti | df42ddd | 2017-02-28 01:20:13 +0900 | [diff] [blame] | 327 | }; |
Lorenzo Colitti | 48f8300 | 2017-07-06 15:06:04 +0900 | [diff] [blame] | 328 | return {Join(cmds, "\n")}; |
Lorenzo Colitti | df42ddd | 2017-02-28 01:20:13 +0900 | [diff] [blame] | 329 | } |
| 330 | |
Joel Scherpelz | 01cc549 | 2017-06-16 10:45:14 +0900 | [diff] [blame] | 331 | const 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 Colitti | df42ddd | 2017-02-28 01:20:13 +0900 | [diff] [blame] | 335 | std::vector<std::string> cmds = { |
Lorenzo Colitti | 48f8300 | 2017-07-06 15:06:04 +0900 | [diff] [blame] | 336 | "*filter", |
Joel Scherpelz | 01cc549 | 2017-06-16 10:45:14 +0900 | [diff] [blame] | 337 | StringPrintf("-D bw_INPUT -i %s --jump %s", c_iface, c_chain), |
| 338 | StringPrintf("-D bw_OUTPUT -o %s --jump %s", c_iface, c_chain), |
Erik Kline | 51eb324 | 2017-09-20 18:30:47 +0900 | [diff] [blame] | 339 | StringPrintf("-D bw_FORWARD -i %s --jump %s", c_iface, c_chain), |
Joel Scherpelz | 01cc549 | 2017-06-16 10:45:14 +0900 | [diff] [blame] | 340 | StringPrintf("-D bw_FORWARD -o %s --jump %s", c_iface, c_chain), |
| 341 | StringPrintf("-F %s", c_chain), |
| 342 | StringPrintf("-X %s", c_chain), |
Lorenzo Colitti | 48f8300 | 2017-07-06 15:06:04 +0900 | [diff] [blame] | 343 | "COMMIT\n", |
Lorenzo Colitti | df42ddd | 2017-02-28 01:20:13 +0900 | [diff] [blame] | 344 | }; |
Lorenzo Colitti | 48f8300 | 2017-07-06 15:06:04 +0900 | [diff] [blame] | 345 | return {Join(cmds, "\n")}; |
Lorenzo Colitti | df42ddd | 2017-02-28 01:20:13 +0900 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | TEST_F(BandwidthControllerTest, TestSetInterfaceQuota) { |
Joel Scherpelz | 01cc549 | 2017-06-16 10:45:14 +0900 | [diff] [blame] | 349 | constexpr uint64_t kOldQuota = 123456; |
| 350 | const std::string iface = mTun.name(); |
| 351 | std::vector<std::string> expected = makeInterfaceQuotaCommands(iface, 1, kOldQuota); |
Lorenzo Colitti | df42ddd | 2017-02-28 01:20:13 +0900 | [diff] [blame] | 352 | |
Joel Scherpelz | 01cc549 | 2017-06-16 10:45:14 +0900 | [diff] [blame] | 353 | EXPECT_EQ(0, mBw.setInterfaceQuota(iface, kOldQuota)); |
Lorenzo Colitti | 48f8300 | 2017-07-06 15:06:04 +0900 | [diff] [blame] | 354 | expectIptablesRestoreCommands(expected); |
Joel Scherpelz | 01cc549 | 2017-06-16 10:45:14 +0900 | [diff] [blame] | 355 | |
| 356 | constexpr uint64_t kNewQuota = kOldQuota + 1; |
| 357 | expected = {}; |
| 358 | expectUpdateQuota(kNewQuota); |
| 359 | EXPECT_EQ(0, mBw.setInterfaceQuota(iface, kNewQuota)); |
Lorenzo Colitti | 48f8300 | 2017-07-06 15:06:04 +0900 | [diff] [blame] | 360 | expectIptablesRestoreCommands(expected); |
Lorenzo Colitti | df42ddd | 2017-02-28 01:20:13 +0900 | [diff] [blame] | 361 | |
| 362 | expected = removeInterfaceQuotaCommands(iface); |
| 363 | EXPECT_EQ(0, mBw.removeInterfaceQuota(iface)); |
Lorenzo Colitti | 48f8300 | 2017-07-06 15:06:04 +0900 | [diff] [blame] | 364 | expectIptablesRestoreCommands(expected); |
Lorenzo Colitti | df42ddd | 2017-02-28 01:20:13 +0900 | [diff] [blame] | 365 | } |
Lorenzo Colitti | e8b56e4 | 2017-04-26 15:16:03 +0900 | [diff] [blame] | 366 | |
Joel Scherpelz | 01cc549 | 2017-06-16 10:45:14 +0900 | [diff] [blame] | 367 | const std::vector<std::string> makeInterfaceSharedQuotaCommands(const std::string& iface, |
Lorenzo Colitti | 48f8300 | 2017-07-06 15:06:04 +0900 | [diff] [blame] | 368 | int ruleIndex, int64_t quota, |
| 369 | bool insertQuota) { |
Joel Scherpelz | 01cc549 | 2017-06-16 10:45:14 +0900 | [diff] [blame] | 370 | 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 = { |
Lorenzo Colitti | 48f8300 | 2017-07-06 15:06:04 +0900 | [diff] [blame] | 374 | "*filter", |
Joel Scherpelz | 01cc549 | 2017-06-16 10:45:14 +0900 | [diff] [blame] | 375 | StringPrintf("-I bw_INPUT %d -i %s --jump %s", ruleIndex, c_iface, c_chain), |
Joel Scherpelz | 01cc549 | 2017-06-16 10:45:14 +0900 | [diff] [blame] | 376 | StringPrintf("-I bw_OUTPUT %d -o %s --jump %s", ruleIndex, c_iface, c_chain), |
Erik Kline | 51eb324 | 2017-09-20 18:30:47 +0900 | [diff] [blame] | 377 | StringPrintf("-A bw_FORWARD -i %s --jump %s", c_iface, c_chain), |
Joel Scherpelz | 01cc549 | 2017-06-16 10:45:14 +0900 | [diff] [blame] | 378 | StringPrintf("-A bw_FORWARD -o %s --jump %s", c_iface, c_chain), |
Joel Scherpelz | 01cc549 | 2017-06-16 10:45:14 +0900 | [diff] [blame] | 379 | }; |
Lorenzo Colitti | 48f8300 | 2017-07-06 15:06:04 +0900 | [diff] [blame] | 380 | if (insertQuota) { |
| 381 | cmds.push_back(StringPrintf( |
| 382 | "-I %s -m quota2 ! --quota %" PRIu64 " --name shared --jump REJECT", c_chain, quota)); |
| 383 | } |
| 384 | cmds.push_back("COMMIT\n"); |
| 385 | return {Join(cmds, "\n")}; |
Joel Scherpelz | 01cc549 | 2017-06-16 10:45:14 +0900 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | const std::vector<std::string> removeInterfaceSharedQuotaCommands(const std::string& iface, |
Lorenzo Colitti | 48f8300 | 2017-07-06 15:06:04 +0900 | [diff] [blame] | 389 | int64_t quota, bool deleteQuota) { |
Joel Scherpelz | 01cc549 | 2017-06-16 10:45:14 +0900 | [diff] [blame] | 390 | 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 = { |
Lorenzo Colitti | 48f8300 | 2017-07-06 15:06:04 +0900 | [diff] [blame] | 394 | "*filter", |
Joel Scherpelz | 01cc549 | 2017-06-16 10:45:14 +0900 | [diff] [blame] | 395 | StringPrintf("-D bw_INPUT -i %s --jump %s", c_iface, c_chain), |
| 396 | StringPrintf("-D bw_OUTPUT -o %s --jump %s", c_iface, c_chain), |
Erik Kline | 51eb324 | 2017-09-20 18:30:47 +0900 | [diff] [blame] | 397 | StringPrintf("-D bw_FORWARD -i %s --jump %s", c_iface, c_chain), |
Joel Scherpelz | 01cc549 | 2017-06-16 10:45:14 +0900 | [diff] [blame] | 398 | StringPrintf("-D bw_FORWARD -o %s --jump %s", c_iface, c_chain), |
Joel Scherpelz | 01cc549 | 2017-06-16 10:45:14 +0900 | [diff] [blame] | 399 | }; |
Lorenzo Colitti | 48f8300 | 2017-07-06 15:06:04 +0900 | [diff] [blame] | 400 | if (deleteQuota) { |
| 401 | cmds.push_back(StringPrintf( |
| 402 | "-D %s -m quota2 ! --quota %" PRIu64 " --name shared --jump REJECT", c_chain, quota)); |
| 403 | } |
| 404 | cmds.push_back("COMMIT\n"); |
| 405 | return {Join(cmds, "\n")}; |
Joel Scherpelz | 01cc549 | 2017-06-16 10:45:14 +0900 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | TEST_F(BandwidthControllerTest, TestSetInterfaceSharedQuotaDuplicate) { |
| 409 | constexpr uint64_t kQuota = 123456; |
| 410 | const std::string iface = mTun.name(); |
Lorenzo Colitti | 48f8300 | 2017-07-06 15:06:04 +0900 | [diff] [blame] | 411 | std::vector<std::string> expected = makeInterfaceSharedQuotaCommands(iface, 1, 123456, true); |
Joel Scherpelz | 01cc549 | 2017-06-16 10:45:14 +0900 | [diff] [blame] | 412 | EXPECT_EQ(0, mBw.setInterfaceSharedQuota(iface, kQuota)); |
Lorenzo Colitti | 48f8300 | 2017-07-06 15:06:04 +0900 | [diff] [blame] | 413 | expectIptablesRestoreCommands(expected); |
Joel Scherpelz | 01cc549 | 2017-06-16 10:45:14 +0900 | [diff] [blame] | 414 | |
| 415 | expected = {}; |
| 416 | EXPECT_EQ(0, mBw.setInterfaceSharedQuota(iface, kQuota)); |
Lorenzo Colitti | 48f8300 | 2017-07-06 15:06:04 +0900 | [diff] [blame] | 417 | expectIptablesRestoreCommands(expected); |
Joel Scherpelz | 01cc549 | 2017-06-16 10:45:14 +0900 | [diff] [blame] | 418 | |
Lorenzo Colitti | 48f8300 | 2017-07-06 15:06:04 +0900 | [diff] [blame] | 419 | expected = removeInterfaceSharedQuotaCommands(iface, kQuota, true); |
Joel Scherpelz | 01cc549 | 2017-06-16 10:45:14 +0900 | [diff] [blame] | 420 | EXPECT_EQ(0, mBw.removeInterfaceSharedQuota(iface)); |
Lorenzo Colitti | 48f8300 | 2017-07-06 15:06:04 +0900 | [diff] [blame] | 421 | expectIptablesRestoreCommands(expected); |
Joel Scherpelz | 01cc549 | 2017-06-16 10:45:14 +0900 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | TEST_F(BandwidthControllerTest, TestSetInterfaceSharedQuotaUpdate) { |
| 425 | constexpr uint64_t kOldQuota = 123456; |
| 426 | const std::string iface = mTun.name(); |
Lorenzo Colitti | 48f8300 | 2017-07-06 15:06:04 +0900 | [diff] [blame] | 427 | std::vector<std::string> expected = makeInterfaceSharedQuotaCommands(iface, 1, kOldQuota, true); |
Joel Scherpelz | 01cc549 | 2017-06-16 10:45:14 +0900 | [diff] [blame] | 428 | EXPECT_EQ(0, mBw.setInterfaceSharedQuota(iface, kOldQuota)); |
Lorenzo Colitti | 48f8300 | 2017-07-06 15:06:04 +0900 | [diff] [blame] | 429 | expectIptablesRestoreCommands(expected); |
Joel Scherpelz | 01cc549 | 2017-06-16 10:45:14 +0900 | [diff] [blame] | 430 | |
| 431 | constexpr uint64_t kNewQuota = kOldQuota + 1; |
| 432 | expected = {}; |
| 433 | expectUpdateQuota(kNewQuota); |
| 434 | EXPECT_EQ(0, mBw.setInterfaceSharedQuota(iface, kNewQuota)); |
Lorenzo Colitti | 48f8300 | 2017-07-06 15:06:04 +0900 | [diff] [blame] | 435 | expectIptablesRestoreCommands(expected); |
Joel Scherpelz | 01cc549 | 2017-06-16 10:45:14 +0900 | [diff] [blame] | 436 | |
Lorenzo Colitti | 48f8300 | 2017-07-06 15:06:04 +0900 | [diff] [blame] | 437 | expected = removeInterfaceSharedQuotaCommands(iface, kNewQuota, true); |
Joel Scherpelz | 01cc549 | 2017-06-16 10:45:14 +0900 | [diff] [blame] | 438 | EXPECT_EQ(0, mBw.removeInterfaceSharedQuota(iface)); |
Lorenzo Colitti | 48f8300 | 2017-07-06 15:06:04 +0900 | [diff] [blame] | 439 | expectIptablesRestoreCommands(expected); |
Joel Scherpelz | 01cc549 | 2017-06-16 10:45:14 +0900 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | TEST_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 Colitti | 48f8300 | 2017-07-06 15:06:04 +0900 | [diff] [blame] | 450 | // Quota rule is only added when the total number of |
| 451 | // interfaces transitions from 0 -> 1. |
Joel Scherpelz | 01cc549 | 2017-06-16 10:45:14 +0900 | [diff] [blame] | 452 | bool first = (iface == ifaces[0]); |
Lorenzo Colitti | 48f8300 | 2017-07-06 15:06:04 +0900 | [diff] [blame] | 453 | auto expected = makeInterfaceSharedQuotaCommands(iface, 1, kQuota, first); |
Joel Scherpelz | 01cc549 | 2017-06-16 10:45:14 +0900 | [diff] [blame] | 454 | EXPECT_EQ(0, mBw.setInterfaceSharedQuota(iface, kQuota)); |
Lorenzo Colitti | 48f8300 | 2017-07-06 15:06:04 +0900 | [diff] [blame] | 455 | expectIptablesRestoreCommands(expected); |
Joel Scherpelz | 01cc549 | 2017-06-16 10:45:14 +0900 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | for (const auto& iface : ifaces) { |
Lorenzo Colitti | 48f8300 | 2017-07-06 15:06:04 +0900 | [diff] [blame] | 459 | // Quota rule is only removed when the total number of |
| 460 | // interfaces transitions from 1 -> 0. |
Joel Scherpelz | 01cc549 | 2017-06-16 10:45:14 +0900 | [diff] [blame] | 461 | bool last = (iface == ifaces[1]); |
Lorenzo Colitti | 48f8300 | 2017-07-06 15:06:04 +0900 | [diff] [blame] | 462 | auto expected = removeInterfaceSharedQuotaCommands(iface, kQuota, last); |
Joel Scherpelz | 01cc549 | 2017-06-16 10:45:14 +0900 | [diff] [blame] | 463 | EXPECT_EQ(0, mBw.removeInterfaceSharedQuota(iface)); |
Lorenzo Colitti | 48f8300 | 2017-07-06 15:06:04 +0900 | [diff] [blame] | 464 | expectIptablesRestoreCommands(expected); |
Joel Scherpelz | 01cc549 | 2017-06-16 10:45:14 +0900 | [diff] [blame] | 465 | } |
| 466 | } |
| 467 | |
Lorenzo Colitti | e8b56e4 | 2017-04-26 15:16:03 +0900 | [diff] [blame] | 468 | TEST_F(BandwidthControllerTest, IptablesAlertCmd) { |
| 469 | std::vector<std::string> expected = { |
Lorenzo Colitti | 3c27270 | 2017-04-26 15:48:13 +0900 | [diff] [blame] | 470 | "*filter\n" |
| 471 | "-I bw_INPUT -m quota2 ! --quota 123456 --name MyWonderfulAlert\n" |
| 472 | "-I bw_OUTPUT -m quota2 ! --quota 123456 --name MyWonderfulAlert\n" |
| 473 | "COMMIT\n" |
Lorenzo Colitti | e8b56e4 | 2017-04-26 15:16:03 +0900 | [diff] [blame] | 474 | }; |
| 475 | EXPECT_EQ(0, runIptablesAlertCmd(IptOp::IptOpInsert, "MyWonderfulAlert", 123456)); |
Lorenzo Colitti | 3c27270 | 2017-04-26 15:48:13 +0900 | [diff] [blame] | 476 | expectIptablesRestoreCommands(expected); |
Lorenzo Colitti | e8b56e4 | 2017-04-26 15:16:03 +0900 | [diff] [blame] | 477 | |
| 478 | expected = { |
Lorenzo Colitti | 3c27270 | 2017-04-26 15:48:13 +0900 | [diff] [blame] | 479 | "*filter\n" |
| 480 | "-D bw_INPUT -m quota2 ! --quota 123456 --name MyWonderfulAlert\n" |
| 481 | "-D bw_OUTPUT -m quota2 ! --quota 123456 --name MyWonderfulAlert\n" |
| 482 | "COMMIT\n" |
Lorenzo Colitti | e8b56e4 | 2017-04-26 15:16:03 +0900 | [diff] [blame] | 483 | }; |
| 484 | EXPECT_EQ(0, runIptablesAlertCmd(IptOp::IptOpDelete, "MyWonderfulAlert", 123456)); |
Lorenzo Colitti | 3c27270 | 2017-04-26 15:48:13 +0900 | [diff] [blame] | 485 | expectIptablesRestoreCommands(expected); |
Lorenzo Colitti | e8b56e4 | 2017-04-26 15:16:03 +0900 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | TEST_F(BandwidthControllerTest, IptablesAlertFwdCmd) { |
| 489 | std::vector<std::string> expected = { |
Lorenzo Colitti | 3c27270 | 2017-04-26 15:48:13 +0900 | [diff] [blame] | 490 | "*filter\n" |
| 491 | "-I bw_FORWARD -m quota2 ! --quota 123456 --name MyWonderfulAlert\n" |
| 492 | "COMMIT\n" |
Lorenzo Colitti | e8b56e4 | 2017-04-26 15:16:03 +0900 | [diff] [blame] | 493 | }; |
| 494 | EXPECT_EQ(0, runIptablesAlertFwdCmd(IptOp::IptOpInsert, "MyWonderfulAlert", 123456)); |
Lorenzo Colitti | 3c27270 | 2017-04-26 15:48:13 +0900 | [diff] [blame] | 495 | expectIptablesRestoreCommands(expected); |
Lorenzo Colitti | e8b56e4 | 2017-04-26 15:16:03 +0900 | [diff] [blame] | 496 | |
| 497 | expected = { |
Lorenzo Colitti | 3c27270 | 2017-04-26 15:48:13 +0900 | [diff] [blame] | 498 | "*filter\n" |
| 499 | "-D bw_FORWARD -m quota2 ! --quota 123456 --name MyWonderfulAlert\n" |
| 500 | "COMMIT\n" |
Lorenzo Colitti | e8b56e4 | 2017-04-26 15:16:03 +0900 | [diff] [blame] | 501 | }; |
| 502 | EXPECT_EQ(0, runIptablesAlertFwdCmd(IptOp::IptOpDelete, "MyWonderfulAlert", 123456)); |
Lorenzo Colitti | 3c27270 | 2017-04-26 15:48:13 +0900 | [diff] [blame] | 503 | expectIptablesRestoreCommands(expected); |
Lorenzo Colitti | e8b56e4 | 2017-04-26 15:16:03 +0900 | [diff] [blame] | 504 | } |
Lorenzo Colitti | f4dfa68 | 2017-04-28 11:09:07 +0900 | [diff] [blame] | 505 | |
Lorenzo Colitti | 3807822 | 2017-07-06 17:27:23 +0900 | [diff] [blame] | 506 | TEST_F(BandwidthControllerTest, CostlyAlert) { |
| 507 | const int64_t kQuota = 123456; |
| 508 | int64_t alertBytes = 0; |
| 509 | |
| 510 | std::vector<std::string> expected = { |
Lorenzo Colitti | e85ffe1 | 2017-07-06 17:25:37 +0900 | [diff] [blame] | 511 | "*filter\n" |
| 512 | "-A bw_costly_shared -m quota2 ! --quota 123456 --name sharedAlert\n" |
| 513 | "COMMIT\n" |
Lorenzo Colitti | 3807822 | 2017-07-06 17:27:23 +0900 | [diff] [blame] | 514 | }; |
| 515 | EXPECT_EQ(0, setCostlyAlert("shared", kQuota, &alertBytes)); |
| 516 | EXPECT_EQ(kQuota, alertBytes); |
Lorenzo Colitti | e85ffe1 | 2017-07-06 17:25:37 +0900 | [diff] [blame] | 517 | expectIptablesRestoreCommands(expected); |
Lorenzo Colitti | 3807822 | 2017-07-06 17:27:23 +0900 | [diff] [blame] | 518 | |
| 519 | expected = {}; |
| 520 | expectUpdateQuota(kQuota); |
| 521 | EXPECT_EQ(0, setCostlyAlert("shared", kQuota + 1, &alertBytes)); |
| 522 | EXPECT_EQ(kQuota + 1, alertBytes); |
Lorenzo Colitti | e85ffe1 | 2017-07-06 17:25:37 +0900 | [diff] [blame] | 523 | expectIptablesRestoreCommands(expected); |
Lorenzo Colitti | 3807822 | 2017-07-06 17:27:23 +0900 | [diff] [blame] | 524 | |
| 525 | expected = { |
Lorenzo Colitti | e85ffe1 | 2017-07-06 17:25:37 +0900 | [diff] [blame] | 526 | "*filter\n" |
Lorenzo Colitti | 3807822 | 2017-07-06 17:27:23 +0900 | [diff] [blame] | 527 | "-D bw_costly_shared -m quota2 ! --quota 123457 --name sharedAlert\n" |
Lorenzo Colitti | e85ffe1 | 2017-07-06 17:25:37 +0900 | [diff] [blame] | 528 | "COMMIT\n" |
Lorenzo Colitti | 3807822 | 2017-07-06 17:27:23 +0900 | [diff] [blame] | 529 | }; |
| 530 | EXPECT_EQ(0, removeCostlyAlert("shared", &alertBytes)); |
| 531 | EXPECT_EQ(0, alertBytes); |
Lorenzo Colitti | e85ffe1 | 2017-07-06 17:25:37 +0900 | [diff] [blame] | 532 | expectIptablesRestoreCommands(expected); |
Lorenzo Colitti | 3807822 | 2017-07-06 17:27:23 +0900 | [diff] [blame] | 533 | } |
| 534 | |
Lorenzo Colitti | f4dfa68 | 2017-04-28 11:09:07 +0900 | [diff] [blame] | 535 | TEST_F(BandwidthControllerTest, ManipulateSpecialApps) { |
| 536 | std::vector<const char *> appUids = { "1000", "1001", "10012" }; |
| 537 | |
| 538 | std::vector<std::string> expected = { |
Lorenzo Colitti | 911bc4c | 2017-04-28 14:34:01 +0900 | [diff] [blame] | 539 | "*filter\n" |
| 540 | "-I bw_happy_box -m owner --uid-owner 1000 --jump RETURN\n" |
| 541 | "-I bw_happy_box -m owner --uid-owner 1001 --jump RETURN\n" |
| 542 | "-I bw_happy_box -m owner --uid-owner 10012 --jump RETURN\n" |
| 543 | "COMMIT\n" |
Lorenzo Colitti | f4dfa68 | 2017-04-28 11:09:07 +0900 | [diff] [blame] | 544 | }; |
| 545 | EXPECT_EQ(0, mBw.addNiceApps(appUids.size(), const_cast<char**>(&appUids[0]))); |
Lorenzo Colitti | 911bc4c | 2017-04-28 14:34:01 +0900 | [diff] [blame] | 546 | expectIptablesRestoreCommands(expected); |
Lorenzo Colitti | f4dfa68 | 2017-04-28 11:09:07 +0900 | [diff] [blame] | 547 | |
| 548 | expected = { |
Lorenzo Colitti | 911bc4c | 2017-04-28 14:34:01 +0900 | [diff] [blame] | 549 | "*filter\n" |
| 550 | "-D bw_penalty_box -m owner --uid-owner 1000 --jump REJECT\n" |
| 551 | "-D bw_penalty_box -m owner --uid-owner 1001 --jump REJECT\n" |
| 552 | "-D bw_penalty_box -m owner --uid-owner 10012 --jump REJECT\n" |
| 553 | "COMMIT\n" |
Lorenzo Colitti | f4dfa68 | 2017-04-28 11:09:07 +0900 | [diff] [blame] | 554 | }; |
| 555 | EXPECT_EQ(0, mBw.removeNaughtyApps(appUids.size(), const_cast<char**>(&appUids[0]))); |
Lorenzo Colitti | 911bc4c | 2017-04-28 14:34:01 +0900 | [diff] [blame] | 556 | expectIptablesRestoreCommands(expected); |
Lorenzo Colitti | f4dfa68 | 2017-04-28 11:09:07 +0900 | [diff] [blame] | 557 | } |