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