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