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