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() { |
| 54 | BandwidthController::execFunction = fake_android_fork_exec; |
| 55 | BandwidthController::popenFunction = fake_popen; |
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 | |
Lorenzo Colitti | 56c4b1e | 2017-02-01 02:45:10 +0900 | [diff] [blame] | 69 | void addIptablesRestoreOutput(std::string contents) { |
| 70 | sIptablesRestoreOutput.push_back(contents); |
| 71 | } |
| 72 | |
Lorenzo Colitti | ce6748a | 2017-02-02 01:34:33 +0900 | [diff] [blame] | 73 | void addIptablesRestoreOutput(std::string contents1, std::string contents2) { |
| 74 | sIptablesRestoreOutput.push_back(contents1); |
| 75 | sIptablesRestoreOutput.push_back(contents2); |
| 76 | } |
| 77 | |
Lorenzo Colitti | 56c4b1e | 2017-02-01 02:45:10 +0900 | [diff] [blame] | 78 | void clearIptablesRestoreOutput() { |
| 79 | sIptablesRestoreOutput.clear(); |
| 80 | } |
| 81 | |
| 82 | void expectSetupCommands(const std::string& expectedClean, std::string expectedAccounting) { |
| 83 | std::string expectedList = |
| 84 | "*filter\n" |
| 85 | "-S\n" |
| 86 | "COMMIT\n"; |
| 87 | |
| 88 | std::string expectedFlush = |
| 89 | "*filter\n" |
| 90 | ":bw_INPUT -\n" |
| 91 | ":bw_OUTPUT -\n" |
| 92 | ":bw_FORWARD -\n" |
| 93 | ":bw_happy_box -\n" |
| 94 | ":bw_penalty_box -\n" |
| 95 | ":bw_data_saver -\n" |
| 96 | ":bw_costly_shared -\n" |
| 97 | "COMMIT\n" |
| 98 | "*raw\n" |
| 99 | ":bw_raw_PREROUTING -\n" |
| 100 | "COMMIT\n" |
| 101 | "*mangle\n" |
| 102 | ":bw_mangle_POSTROUTING -\n" |
| 103 | "COMMIT\n"; |
| 104 | |
| 105 | ExpectedIptablesCommands expected = {{ V4, expectedList }}; |
| 106 | if (expectedClean.size()) { |
| 107 | expected.push_back({ V4V6, expectedClean }); |
| 108 | } |
| 109 | expected.push_back({ V4V6, expectedFlush }); |
| 110 | if (expectedAccounting.size()) { |
| 111 | expected.push_back({ V4V6, expectedAccounting }); |
| 112 | } |
| 113 | |
| 114 | expectIptablesRestoreCommands(expected); |
| 115 | } |
Lorenzo Colitti | e8b56e4 | 2017-04-26 15:16:03 +0900 | [diff] [blame] | 116 | |
| 117 | using IptOp = BandwidthController::IptOp; |
| 118 | |
| 119 | int runIptablesAlertCmd(IptOp a, const char *b, int64_t c) { |
| 120 | return mBw.runIptablesAlertCmd(a, b, c); |
| 121 | } |
| 122 | |
| 123 | int runIptablesAlertFwdCmd(IptOp a, const char *b, int64_t c) { |
| 124 | return mBw.runIptablesAlertFwdCmd(a, b, c); |
| 125 | } |
Joel Scherpelz | 01cc549 | 2017-06-16 10:45:14 +0900 | [diff] [blame] | 126 | |
Lorenzo Colitti | 3807822 | 2017-07-06 17:27:23 +0900 | [diff] [blame^] | 127 | int setCostlyAlert(const std::string a, int64_t b, int64_t *c) { |
| 128 | return mBw.setCostlyAlert(a, b, c); |
| 129 | } |
| 130 | |
| 131 | int removeCostlyAlert(const std::string a, int64_t *b) { |
| 132 | return mBw.removeCostlyAlert(a, b); |
| 133 | } |
| 134 | |
Joel Scherpelz | 01cc549 | 2017-06-16 10:45:14 +0900 | [diff] [blame] | 135 | void expectUpdateQuota(uint64_t quota) { |
| 136 | uintptr_t dummy; |
| 137 | FILE* dummyFile = reinterpret_cast<FILE*>(&dummy); |
| 138 | |
| 139 | EXPECT_CALL(mSyscalls, fopen(_, _)).WillOnce(Return(ByMove(UniqueFile(dummyFile)))); |
| 140 | EXPECT_CALL(mSyscalls, vfprintf(dummyFile, _, _)) |
| 141 | .WillOnce(Invoke([quota](FILE*, const std::string&, va_list ap) { |
| 142 | EXPECT_EQ(quota, va_arg(ap, uint64_t)); |
| 143 | return 0; |
| 144 | })); |
| 145 | EXPECT_CALL(mSyscalls, fclose(dummyFile)).WillOnce(Return(ok)); |
| 146 | } |
| 147 | |
| 148 | StrictMock<android::netdutils::ScopedMockSyscalls> mSyscalls; |
Lorenzo Colitti | 86a4798 | 2016-03-18 17:52:25 +0900 | [diff] [blame] | 149 | }; |
| 150 | |
Lorenzo Colitti | a0dc8a5 | 2016-03-26 22:42:07 +0900 | [diff] [blame] | 151 | TEST_F(BandwidthControllerTest, TestSetupIptablesHooks) { |
Lorenzo Colitti | 56c4b1e | 2017-02-01 02:45:10 +0900 | [diff] [blame] | 152 | // Pretend some bw_costly_shared_<iface> rules already exist... |
| 153 | addIptablesRestoreOutput( |
| 154 | "-P OUTPUT ACCEPT\n" |
| 155 | "-N bw_costly_rmnet_data0\n" |
| 156 | "-N bw_costly_shared\n" |
| 157 | "-N unrelated\n" |
| 158 | "-N bw_costly_rmnet_data7\n"); |
| 159 | |
| 160 | // ... and expect that they be flushed and deleted. |
| 161 | std::string expectedCleanCmds = |
Lorenzo Colitti | 13debb8 | 2016-03-27 17:46:30 +0900 | [diff] [blame] | 162 | "*filter\n" |
Lorenzo Colitti | 56c4b1e | 2017-02-01 02:45:10 +0900 | [diff] [blame] | 163 | ":bw_costly_rmnet_data0 -\n" |
| 164 | "-X bw_costly_rmnet_data0\n" |
| 165 | ":bw_costly_rmnet_data7 -\n" |
| 166 | "-X bw_costly_rmnet_data7\n" |
| 167 | "COMMIT\n"; |
| 168 | |
| 169 | mBw.setupIptablesHooks(); |
| 170 | expectSetupCommands(expectedCleanCmds, ""); |
Lorenzo Colitti | a0dc8a5 | 2016-03-26 22:42:07 +0900 | [diff] [blame] | 171 | } |
| 172 | |
Lorenzo Colitti | 86a4798 | 2016-03-18 17:52:25 +0900 | [diff] [blame] | 173 | TEST_F(BandwidthControllerTest, TestEnableBandwidthControl) { |
Lorenzo Colitti | 56c4b1e | 2017-02-01 02:45:10 +0900 | [diff] [blame] | 174 | // Pretend no bw_costly_shared_<iface> rules already exist... |
| 175 | addIptablesRestoreOutput( |
| 176 | "-P OUTPUT ACCEPT\n" |
| 177 | "-N bw_costly_shared\n" |
| 178 | "-N unrelated\n"); |
| 179 | |
| 180 | // ... so none are flushed or deleted. |
| 181 | std::string expectedClean = ""; |
| 182 | |
| 183 | std::string expectedAccounting = |
Lorenzo Colitti | 13debb8 | 2016-03-27 17:46:30 +0900 | [diff] [blame] | 184 | "*filter\n" |
| 185 | "-A bw_INPUT -m owner --socket-exists\n" |
| 186 | "-A bw_OUTPUT -m owner --socket-exists\n" |
| 187 | "-A bw_costly_shared --jump bw_penalty_box\n" |
| 188 | "-A bw_penalty_box --jump bw_happy_box\n" |
| 189 | "-A bw_happy_box --jump bw_data_saver\n" |
| 190 | "-A bw_data_saver -j RETURN\n" |
| 191 | "-I bw_happy_box -m owner --uid-owner 0-9999 --jump RETURN\n" |
| 192 | "COMMIT\n" |
| 193 | "*raw\n" |
| 194 | "-A bw_raw_PREROUTING -m owner --socket-exists\n" |
| 195 | "COMMIT\n" |
| 196 | "*mangle\n" |
| 197 | "-A bw_mangle_POSTROUTING -m owner --socket-exists\n" |
Lorenzo Colitti | 56c4b1e | 2017-02-01 02:45:10 +0900 | [diff] [blame] | 198 | "COMMIT\n"; |
Lorenzo Colitti | 13debb8 | 2016-03-27 17:46:30 +0900 | [diff] [blame] | 199 | |
Lorenzo Colitti | 56c4b1e | 2017-02-01 02:45:10 +0900 | [diff] [blame] | 200 | mBw.enableBandwidthControl(false); |
| 201 | expectSetupCommands(expectedClean, expectedAccounting); |
Lorenzo Colitti | 86a4798 | 2016-03-18 17:52:25 +0900 | [diff] [blame] | 202 | } |
| 203 | |
Lorenzo Colitti | a0dc8a5 | 2016-03-26 22:42:07 +0900 | [diff] [blame] | 204 | TEST_F(BandwidthControllerTest, TestDisableBandwidthControl) { |
Lorenzo Colitti | 56c4b1e | 2017-02-01 02:45:10 +0900 | [diff] [blame] | 205 | // Pretend some bw_costly_shared_<iface> rules already exist... |
| 206 | addIptablesRestoreOutput( |
| 207 | "-P OUTPUT ACCEPT\n" |
| 208 | "-N bw_costly_rmnet_data0\n" |
| 209 | "-N bw_costly_shared\n" |
| 210 | "-N unrelated\n" |
| 211 | "-N bw_costly_rmnet_data7\n"); |
| 212 | |
| 213 | // ... and expect that they be flushed. |
| 214 | std::string expectedCleanCmds = |
Lorenzo Colitti | 13debb8 | 2016-03-27 17:46:30 +0900 | [diff] [blame] | 215 | "*filter\n" |
Lorenzo Colitti | 56c4b1e | 2017-02-01 02:45:10 +0900 | [diff] [blame] | 216 | ":bw_costly_rmnet_data0 -\n" |
| 217 | ":bw_costly_rmnet_data7 -\n" |
| 218 | "COMMIT\n"; |
| 219 | |
| 220 | mBw.disableBandwidthControl(); |
| 221 | expectSetupCommands(expectedCleanCmds, ""); |
Lorenzo Colitti | a0dc8a5 | 2016-03-26 22:42:07 +0900 | [diff] [blame] | 222 | } |
| 223 | |
Lorenzo Colitti | 86a4798 | 2016-03-18 17:52:25 +0900 | [diff] [blame] | 224 | TEST_F(BandwidthControllerTest, TestEnableDataSaver) { |
| 225 | mBw.enableDataSaver(true); |
| 226 | std::vector<std::string> expected = { |
Lorenzo Colitti | 911bc4c | 2017-04-28 14:34:01 +0900 | [diff] [blame] | 227 | "*filter\n" |
| 228 | "-R bw_data_saver 1 --jump REJECT\n" |
| 229 | "COMMIT\n" |
Lorenzo Colitti | 86a4798 | 2016-03-18 17:52:25 +0900 | [diff] [blame] | 230 | }; |
Lorenzo Colitti | 911bc4c | 2017-04-28 14:34:01 +0900 | [diff] [blame] | 231 | expectIptablesRestoreCommands(expected); |
Lorenzo Colitti | 86a4798 | 2016-03-18 17:52:25 +0900 | [diff] [blame] | 232 | |
| 233 | mBw.enableDataSaver(false); |
| 234 | expected = { |
Lorenzo Colitti | 911bc4c | 2017-04-28 14:34:01 +0900 | [diff] [blame] | 235 | "*filter\n" |
| 236 | "-R bw_data_saver 1 --jump RETURN\n" |
| 237 | "COMMIT\n" |
Lorenzo Colitti | 86a4798 | 2016-03-18 17:52:25 +0900 | [diff] [blame] | 238 | }; |
Lorenzo Colitti | 911bc4c | 2017-04-28 14:34:01 +0900 | [diff] [blame] | 239 | expectIptablesRestoreCommands(expected); |
Lorenzo Colitti | 86a4798 | 2016-03-18 17:52:25 +0900 | [diff] [blame] | 240 | } |
Lorenzo Colitti | bbeaf9a | 2016-07-08 18:24:26 +0900 | [diff] [blame] | 241 | |
Lorenzo Colitti | 48f8300 | 2017-07-06 15:06:04 +0900 | [diff] [blame] | 242 | std::string kIPv4TetherCounters = Join(std::vector<std::string> { |
Lorenzo Colitti | bbeaf9a | 2016-07-08 18:24:26 +0900 | [diff] [blame] | 243 | "Chain natctrl_tether_counters (4 references)", |
| 244 | " pkts bytes target prot opt in out source destination", |
| 245 | " 26 2373 RETURN all -- wlan0 rmnet0 0.0.0.0/0 0.0.0.0/0", |
| 246 | " 27 2002 RETURN all -- rmnet0 wlan0 0.0.0.0/0 0.0.0.0/0", |
| 247 | " 1040 107471 RETURN all -- bt-pan rmnet0 0.0.0.0/0 0.0.0.0/0", |
| 248 | " 1450 1708806 RETURN all -- rmnet0 bt-pan 0.0.0.0/0 0.0.0.0/0", |
| 249 | }, '\n'); |
| 250 | |
Lorenzo Colitti | 48f8300 | 2017-07-06 15:06:04 +0900 | [diff] [blame] | 251 | std::string kIPv6TetherCounters = Join(std::vector<std::string> { |
Lorenzo Colitti | 26c9132 | 2016-07-11 11:36:25 +0900 | [diff] [blame] | 252 | "Chain natctrl_tether_counters (2 references)", |
| 253 | " pkts bytes target prot opt in out source destination", |
| 254 | " 10000 10000000 RETURN all wlan0 rmnet0 ::/0 ::/0", |
| 255 | " 20000 20000000 RETURN all rmnet0 wlan0 ::/0 ::/0", |
| 256 | }, '\n'); |
| 257 | |
Lorenzo Colitti | bbeaf9a | 2016-07-08 18:24:26 +0900 | [diff] [blame] | 258 | std::string readSocketClientResponse(int fd) { |
| 259 | char buf[32768]; |
| 260 | ssize_t bytesRead = read(fd, buf, sizeof(buf)); |
| 261 | if (bytesRead < 0) { |
| 262 | return ""; |
| 263 | } |
| 264 | for (int i = 0; i < bytesRead; i++) { |
| 265 | if (buf[i] == '\0') buf[i] = '\n'; |
| 266 | } |
| 267 | return std::string(buf, bytesRead); |
| 268 | } |
| 269 | |
Lorenzo Colitti | 750e8fc | 2016-07-12 01:19:49 +0900 | [diff] [blame] | 270 | void expectNoSocketClientResponse(int fd) { |
| 271 | char buf[64]; |
| 272 | EXPECT_EQ(-1, read(fd, buf, sizeof(buf))); |
| 273 | } |
| 274 | |
Lorenzo Colitti | bbeaf9a | 2016-07-08 18:24:26 +0900 | [diff] [blame] | 275 | TEST_F(BandwidthControllerTest, TestGetTetherStats) { |
| 276 | int socketPair[2]; |
| 277 | ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM, 0, socketPair)); |
| 278 | ASSERT_EQ(0, fcntl(socketPair[0], F_SETFL, O_NONBLOCK | fcntl(socketPair[0], F_GETFL))); |
| 279 | ASSERT_EQ(0, fcntl(socketPair[1], F_SETFL, O_NONBLOCK | fcntl(socketPair[1], F_GETFL))); |
| 280 | SocketClient cli(socketPair[0], false); |
| 281 | |
| 282 | std::string err; |
| 283 | BandwidthController::TetherStats filter; |
Lorenzo Colitti | 26c9132 | 2016-07-11 11:36:25 +0900 | [diff] [blame] | 284 | |
| 285 | // If no filter is specified, both IPv4 and IPv6 counters must have at least one interface pair. |
Lorenzo Colitti | ce6748a | 2017-02-02 01:34:33 +0900 | [diff] [blame] | 286 | addIptablesRestoreOutput(kIPv4TetherCounters); |
Lorenzo Colitti | 26c9132 | 2016-07-11 11:36:25 +0900 | [diff] [blame] | 287 | ASSERT_EQ(-1, mBw.getTetherStats(&cli, filter, err)); |
| 288 | expectNoSocketClientResponse(socketPair[1]); |
Lorenzo Colitti | ce6748a | 2017-02-02 01:34:33 +0900 | [diff] [blame] | 289 | clearIptablesRestoreOutput(); |
Lorenzo Colitti | 26c9132 | 2016-07-11 11:36:25 +0900 | [diff] [blame] | 290 | |
Lorenzo Colitti | ce6748a | 2017-02-02 01:34:33 +0900 | [diff] [blame] | 291 | addIptablesRestoreOutput(kIPv6TetherCounters); |
Lorenzo Colitti | 26c9132 | 2016-07-11 11:36:25 +0900 | [diff] [blame] | 292 | ASSERT_EQ(-1, mBw.getTetherStats(&cli, filter, err)); |
Lorenzo Colitti | ce6748a | 2017-02-02 01:34:33 +0900 | [diff] [blame] | 293 | clearIptablesRestoreOutput(); |
Lorenzo Colitti | 26c9132 | 2016-07-11 11:36:25 +0900 | [diff] [blame] | 294 | |
| 295 | // IPv4 and IPv6 counters are properly added together. |
Lorenzo Colitti | ce6748a | 2017-02-02 01:34:33 +0900 | [diff] [blame] | 296 | addIptablesRestoreOutput(kIPv4TetherCounters, kIPv6TetherCounters); |
Lorenzo Colitti | 26c9132 | 2016-07-11 11:36:25 +0900 | [diff] [blame] | 297 | filter = BandwidthController::TetherStats(); |
Lorenzo Colitti | bbeaf9a | 2016-07-08 18:24:26 +0900 | [diff] [blame] | 298 | std::string expected = |
Lorenzo Colitti | 26c9132 | 2016-07-11 11:36:25 +0900 | [diff] [blame] | 299 | "114 wlan0 rmnet0 10002373 10026 20002002 20027\n" |
Lorenzo Colitti | bbeaf9a | 2016-07-08 18:24:26 +0900 | [diff] [blame] | 300 | "114 bt-pan rmnet0 107471 1040 1708806 1450\n" |
| 301 | "200 Tethering stats list completed\n"; |
Lorenzo Colitti | 750e8fc | 2016-07-12 01:19:49 +0900 | [diff] [blame] | 302 | ASSERT_EQ(0, mBw.getTetherStats(&cli, filter, err)); |
Lorenzo Colitti | bbeaf9a | 2016-07-08 18:24:26 +0900 | [diff] [blame] | 303 | ASSERT_EQ(expected, readSocketClientResponse(socketPair[1])); |
Lorenzo Colitti | 26c9132 | 2016-07-11 11:36:25 +0900 | [diff] [blame] | 304 | expectNoSocketClientResponse(socketPair[1]); |
Lorenzo Colitti | ce6748a | 2017-02-02 01:34:33 +0900 | [diff] [blame] | 305 | clearIptablesRestoreOutput(); |
Lorenzo Colitti | bbeaf9a | 2016-07-08 18:24:26 +0900 | [diff] [blame] | 306 | |
Lorenzo Colitti | 26c9132 | 2016-07-11 11:36:25 +0900 | [diff] [blame] | 307 | // Test filtering. |
Lorenzo Colitti | ce6748a | 2017-02-02 01:34:33 +0900 | [diff] [blame] | 308 | addIptablesRestoreOutput(kIPv4TetherCounters, kIPv6TetherCounters); |
Lorenzo Colitti | bbeaf9a | 2016-07-08 18:24:26 +0900 | [diff] [blame] | 309 | filter = BandwidthController::TetherStats("bt-pan", "rmnet0", -1, -1, -1, -1); |
| 310 | expected = "221 bt-pan rmnet0 107471 1040 1708806 1450\n"; |
Lorenzo Colitti | 750e8fc | 2016-07-12 01:19:49 +0900 | [diff] [blame] | 311 | ASSERT_EQ(0, mBw.getTetherStats(&cli, filter, err)); |
Lorenzo Colitti | bbeaf9a | 2016-07-08 18:24:26 +0900 | [diff] [blame] | 312 | ASSERT_EQ(expected, readSocketClientResponse(socketPair[1])); |
Lorenzo Colitti | 26c9132 | 2016-07-11 11:36:25 +0900 | [diff] [blame] | 313 | expectNoSocketClientResponse(socketPair[1]); |
Lorenzo Colitti | ce6748a | 2017-02-02 01:34:33 +0900 | [diff] [blame] | 314 | clearIptablesRestoreOutput(); |
Lorenzo Colitti | bbeaf9a | 2016-07-08 18:24:26 +0900 | [diff] [blame] | 315 | |
Lorenzo Colitti | ce6748a | 2017-02-02 01:34:33 +0900 | [diff] [blame] | 316 | addIptablesRestoreOutput(kIPv4TetherCounters, kIPv6TetherCounters); |
Lorenzo Colitti | 26c9132 | 2016-07-11 11:36:25 +0900 | [diff] [blame] | 317 | filter = BandwidthController::TetherStats("wlan0", "rmnet0", -1, -1, -1, -1); |
| 318 | expected = "221 wlan0 rmnet0 10002373 10026 20002002 20027\n"; |
Lorenzo Colitti | 750e8fc | 2016-07-12 01:19:49 +0900 | [diff] [blame] | 319 | ASSERT_EQ(0, mBw.getTetherStats(&cli, filter, err)); |
Lorenzo Colitti | bbeaf9a | 2016-07-08 18:24:26 +0900 | [diff] [blame] | 320 | ASSERT_EQ(expected, readSocketClientResponse(socketPair[1])); |
Lorenzo Colitti | ce6748a | 2017-02-02 01:34:33 +0900 | [diff] [blame] | 321 | clearIptablesRestoreOutput(); |
Lorenzo Colitti | bbeaf9a | 2016-07-08 18:24:26 +0900 | [diff] [blame] | 322 | |
Lorenzo Colitti | 26c9132 | 2016-07-11 11:36:25 +0900 | [diff] [blame] | 323 | // Select nonexistent interfaces. |
Lorenzo Colitti | ce6748a | 2017-02-02 01:34:33 +0900 | [diff] [blame] | 324 | addIptablesRestoreOutput(kIPv4TetherCounters, kIPv6TetherCounters); |
Lorenzo Colitti | bbeaf9a | 2016-07-08 18:24:26 +0900 | [diff] [blame] | 325 | filter = BandwidthController::TetherStats("rmnet0", "foo0", -1, -1, -1, -1); |
| 326 | expected = "200 Tethering stats list completed\n"; |
Lorenzo Colitti | 750e8fc | 2016-07-12 01:19:49 +0900 | [diff] [blame] | 327 | ASSERT_EQ(0, mBw.getTetherStats(&cli, filter, err)); |
Lorenzo Colitti | bbeaf9a | 2016-07-08 18:24:26 +0900 | [diff] [blame] | 328 | ASSERT_EQ(expected, readSocketClientResponse(socketPair[1])); |
Lorenzo Colitti | ce6748a | 2017-02-02 01:34:33 +0900 | [diff] [blame] | 329 | clearIptablesRestoreOutput(); |
Lorenzo Colitti | 750e8fc | 2016-07-12 01:19:49 +0900 | [diff] [blame] | 330 | |
| 331 | // No stats with a filter: no error. |
Lorenzo Colitti | ce6748a | 2017-02-02 01:34:33 +0900 | [diff] [blame] | 332 | addIptablesRestoreOutput("", ""); |
Lorenzo Colitti | 750e8fc | 2016-07-12 01:19:49 +0900 | [diff] [blame] | 333 | ASSERT_EQ(0, mBw.getTetherStats(&cli, filter, err)); |
| 334 | ASSERT_EQ("200 Tethering stats list completed\n", readSocketClientResponse(socketPair[1])); |
Lorenzo Colitti | ce6748a | 2017-02-02 01:34:33 +0900 | [diff] [blame] | 335 | clearIptablesRestoreOutput(); |
Lorenzo Colitti | 26c9132 | 2016-07-11 11:36:25 +0900 | [diff] [blame] | 336 | |
Lorenzo Colitti | ce6748a | 2017-02-02 01:34:33 +0900 | [diff] [blame] | 337 | addIptablesRestoreOutput("foo", "foo"); |
Lorenzo Colitti | 750e8fc | 2016-07-12 01:19:49 +0900 | [diff] [blame] | 338 | ASSERT_EQ(0, mBw.getTetherStats(&cli, filter, err)); |
| 339 | ASSERT_EQ("200 Tethering stats list completed\n", readSocketClientResponse(socketPair[1])); |
Lorenzo Colitti | ce6748a | 2017-02-02 01:34:33 +0900 | [diff] [blame] | 340 | clearIptablesRestoreOutput(); |
Lorenzo Colitti | 750e8fc | 2016-07-12 01:19:49 +0900 | [diff] [blame] | 341 | |
| 342 | // No stats and empty filter: error. |
| 343 | filter = BandwidthController::TetherStats(); |
Lorenzo Colitti | ce6748a | 2017-02-02 01:34:33 +0900 | [diff] [blame] | 344 | addIptablesRestoreOutput("", kIPv6TetherCounters); |
Lorenzo Colitti | 750e8fc | 2016-07-12 01:19:49 +0900 | [diff] [blame] | 345 | ASSERT_EQ(-1, mBw.getTetherStats(&cli, filter, err)); |
| 346 | expectNoSocketClientResponse(socketPair[1]); |
Lorenzo Colitti | ce6748a | 2017-02-02 01:34:33 +0900 | [diff] [blame] | 347 | clearIptablesRestoreOutput(); |
Lorenzo Colitti | 26c9132 | 2016-07-11 11:36:25 +0900 | [diff] [blame] | 348 | |
Lorenzo Colitti | ce6748a | 2017-02-02 01:34:33 +0900 | [diff] [blame] | 349 | addIptablesRestoreOutput(kIPv4TetherCounters, ""); |
Lorenzo Colitti | 26c9132 | 2016-07-11 11:36:25 +0900 | [diff] [blame] | 350 | ASSERT_EQ(-1, mBw.getTetherStats(&cli, filter, err)); |
| 351 | expectNoSocketClientResponse(socketPair[1]); |
Lorenzo Colitti | ce6748a | 2017-02-02 01:34:33 +0900 | [diff] [blame] | 352 | clearIptablesRestoreOutput(); |
Lorenzo Colitti | 750e8fc | 2016-07-12 01:19:49 +0900 | [diff] [blame] | 353 | |
| 354 | // Include only one pair of interfaces and things are fine. |
| 355 | std::vector<std::string> counterLines = android::base::Split(kIPv4TetherCounters, "\n"); |
| 356 | std::vector<std::string> brokenCounterLines = counterLines; |
| 357 | counterLines.resize(4); |
Lorenzo Colitti | 48f8300 | 2017-07-06 15:06:04 +0900 | [diff] [blame] | 358 | std::string counters = Join(counterLines, "\n") + "\n"; |
Lorenzo Colitti | ce6748a | 2017-02-02 01:34:33 +0900 | [diff] [blame] | 359 | addIptablesRestoreOutput(counters, counters); |
Lorenzo Colitti | 750e8fc | 2016-07-12 01:19:49 +0900 | [diff] [blame] | 360 | expected = |
Lorenzo Colitti | 26c9132 | 2016-07-11 11:36:25 +0900 | [diff] [blame] | 361 | "114 wlan0 rmnet0 4746 52 4004 54\n" |
Lorenzo Colitti | 750e8fc | 2016-07-12 01:19:49 +0900 | [diff] [blame] | 362 | "200 Tethering stats list completed\n"; |
| 363 | ASSERT_EQ(0, mBw.getTetherStats(&cli, filter, err)); |
| 364 | ASSERT_EQ(expected, readSocketClientResponse(socketPair[1])); |
Lorenzo Colitti | ce6748a | 2017-02-02 01:34:33 +0900 | [diff] [blame] | 365 | clearIptablesRestoreOutput(); |
Lorenzo Colitti | 750e8fc | 2016-07-12 01:19:49 +0900 | [diff] [blame] | 366 | |
| 367 | // But if interfaces aren't paired, it's always an error. |
Lorenzo Colitti | ce6748a | 2017-02-02 01:34:33 +0900 | [diff] [blame] | 368 | err = ""; |
Lorenzo Colitti | 750e8fc | 2016-07-12 01:19:49 +0900 | [diff] [blame] | 369 | counterLines.resize(3); |
Lorenzo Colitti | 48f8300 | 2017-07-06 15:06:04 +0900 | [diff] [blame] | 370 | counters = Join(counterLines, "\n") + "\n"; |
Lorenzo Colitti | ce6748a | 2017-02-02 01:34:33 +0900 | [diff] [blame] | 371 | addIptablesRestoreOutput(counters, counters); |
Lorenzo Colitti | 750e8fc | 2016-07-12 01:19:49 +0900 | [diff] [blame] | 372 | ASSERT_EQ(-1, mBw.getTetherStats(&cli, filter, err)); |
| 373 | expectNoSocketClientResponse(socketPair[1]); |
Lorenzo Colitti | ce6748a | 2017-02-02 01:34:33 +0900 | [diff] [blame] | 374 | clearIptablesRestoreOutput(); |
| 375 | |
| 376 | // Token unit test of the fact that we return the stats in the error message which the caller |
| 377 | // ignores. |
| 378 | std::string expectedError = counters; |
| 379 | EXPECT_EQ(expectedError, err); |
Lorenzo Colitti | 26c9132 | 2016-07-11 11:36:25 +0900 | [diff] [blame] | 380 | |
| 381 | // popen() failing is always an error. |
Lorenzo Colitti | ce6748a | 2017-02-02 01:34:33 +0900 | [diff] [blame] | 382 | addIptablesRestoreOutput(kIPv4TetherCounters); |
Lorenzo Colitti | 26c9132 | 2016-07-11 11:36:25 +0900 | [diff] [blame] | 383 | ASSERT_EQ(-1, mBw.getTetherStats(&cli, filter, err)); |
| 384 | expectNoSocketClientResponse(socketPair[1]); |
Lorenzo Colitti | ce6748a | 2017-02-02 01:34:33 +0900 | [diff] [blame] | 385 | clearIptablesRestoreOutput(); |
| 386 | addIptablesRestoreOutput(kIPv6TetherCounters); |
Lorenzo Colitti | 26c9132 | 2016-07-11 11:36:25 +0900 | [diff] [blame] | 387 | ASSERT_EQ(-1, mBw.getTetherStats(&cli, filter, err)); |
| 388 | expectNoSocketClientResponse(socketPair[1]); |
Lorenzo Colitti | ce6748a | 2017-02-02 01:34:33 +0900 | [diff] [blame] | 389 | clearIptablesRestoreOutput(); |
Lorenzo Colitti | bbeaf9a | 2016-07-08 18:24:26 +0900 | [diff] [blame] | 390 | } |
Lorenzo Colitti | df42ddd | 2017-02-28 01:20:13 +0900 | [diff] [blame] | 391 | |
Joel Scherpelz | 01cc549 | 2017-06-16 10:45:14 +0900 | [diff] [blame] | 392 | const std::vector<std::string> makeInterfaceQuotaCommands(const std::string& iface, int ruleIndex, |
Lorenzo Colitti | df42ddd | 2017-02-28 01:20:13 +0900 | [diff] [blame] | 393 | int64_t quota) { |
Joel Scherpelz | 01cc549 | 2017-06-16 10:45:14 +0900 | [diff] [blame] | 394 | const std::string chain = "bw_costly_" + iface; |
| 395 | const char* c_chain = chain.c_str(); |
| 396 | const char* c_iface = iface.c_str(); |
Lorenzo Colitti | df42ddd | 2017-02-28 01:20:13 +0900 | [diff] [blame] | 397 | std::vector<std::string> cmds = { |
Lorenzo Colitti | 48f8300 | 2017-07-06 15:06:04 +0900 | [diff] [blame] | 398 | "*filter", |
| 399 | StringPrintf(":%s -", c_chain), |
Joel Scherpelz | 01cc549 | 2017-06-16 10:45:14 +0900 | [diff] [blame] | 400 | StringPrintf("-A %s -j bw_penalty_box", c_chain), |
Joel Scherpelz | 01cc549 | 2017-06-16 10:45:14 +0900 | [diff] [blame] | 401 | 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] | 402 | 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] | 403 | StringPrintf("-A bw_FORWARD -o %s --jump %s", c_iface, c_chain), |
| 404 | StringPrintf("-A %s -m quota2 ! --quota %" PRIu64 " --name %s --jump REJECT", c_chain, |
| 405 | quota, c_iface), |
Lorenzo Colitti | 48f8300 | 2017-07-06 15:06:04 +0900 | [diff] [blame] | 406 | "COMMIT\n", |
Lorenzo Colitti | df42ddd | 2017-02-28 01:20:13 +0900 | [diff] [blame] | 407 | }; |
Lorenzo Colitti | 48f8300 | 2017-07-06 15:06:04 +0900 | [diff] [blame] | 408 | return {Join(cmds, "\n")}; |
Lorenzo Colitti | df42ddd | 2017-02-28 01:20:13 +0900 | [diff] [blame] | 409 | } |
| 410 | |
Joel Scherpelz | 01cc549 | 2017-06-16 10:45:14 +0900 | [diff] [blame] | 411 | const std::vector<std::string> removeInterfaceQuotaCommands(const std::string& iface) { |
| 412 | const std::string chain = "bw_costly_" + iface; |
| 413 | const char* c_chain = chain.c_str(); |
| 414 | const char* c_iface = iface.c_str(); |
Lorenzo Colitti | df42ddd | 2017-02-28 01:20:13 +0900 | [diff] [blame] | 415 | std::vector<std::string> cmds = { |
Lorenzo Colitti | 48f8300 | 2017-07-06 15:06:04 +0900 | [diff] [blame] | 416 | "*filter", |
Joel Scherpelz | 01cc549 | 2017-06-16 10:45:14 +0900 | [diff] [blame] | 417 | StringPrintf("-D bw_INPUT -i %s --jump %s", c_iface, c_chain), |
| 418 | StringPrintf("-D bw_OUTPUT -o %s --jump %s", c_iface, c_chain), |
| 419 | StringPrintf("-D bw_FORWARD -o %s --jump %s", c_iface, c_chain), |
| 420 | StringPrintf("-F %s", c_chain), |
| 421 | StringPrintf("-X %s", c_chain), |
Lorenzo Colitti | 48f8300 | 2017-07-06 15:06:04 +0900 | [diff] [blame] | 422 | "COMMIT\n", |
Lorenzo Colitti | df42ddd | 2017-02-28 01:20:13 +0900 | [diff] [blame] | 423 | }; |
Lorenzo Colitti | 48f8300 | 2017-07-06 15:06:04 +0900 | [diff] [blame] | 424 | return {Join(cmds, "\n")}; |
Lorenzo Colitti | df42ddd | 2017-02-28 01:20:13 +0900 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | TEST_F(BandwidthControllerTest, TestSetInterfaceQuota) { |
Joel Scherpelz | 01cc549 | 2017-06-16 10:45:14 +0900 | [diff] [blame] | 428 | constexpr uint64_t kOldQuota = 123456; |
| 429 | const std::string iface = mTun.name(); |
| 430 | std::vector<std::string> expected = makeInterfaceQuotaCommands(iface, 1, kOldQuota); |
Lorenzo Colitti | df42ddd | 2017-02-28 01:20:13 +0900 | [diff] [blame] | 431 | |
Joel Scherpelz | 01cc549 | 2017-06-16 10:45:14 +0900 | [diff] [blame] | 432 | EXPECT_EQ(0, mBw.setInterfaceQuota(iface, kOldQuota)); |
Lorenzo Colitti | 48f8300 | 2017-07-06 15:06:04 +0900 | [diff] [blame] | 433 | expectIptablesRestoreCommands(expected); |
Joel Scherpelz | 01cc549 | 2017-06-16 10:45:14 +0900 | [diff] [blame] | 434 | |
| 435 | constexpr uint64_t kNewQuota = kOldQuota + 1; |
| 436 | expected = {}; |
| 437 | expectUpdateQuota(kNewQuota); |
| 438 | EXPECT_EQ(0, mBw.setInterfaceQuota(iface, kNewQuota)); |
Lorenzo Colitti | 48f8300 | 2017-07-06 15:06:04 +0900 | [diff] [blame] | 439 | expectIptablesRestoreCommands(expected); |
Lorenzo Colitti | df42ddd | 2017-02-28 01:20:13 +0900 | [diff] [blame] | 440 | |
| 441 | expected = removeInterfaceQuotaCommands(iface); |
| 442 | EXPECT_EQ(0, mBw.removeInterfaceQuota(iface)); |
Lorenzo Colitti | 48f8300 | 2017-07-06 15:06:04 +0900 | [diff] [blame] | 443 | expectIptablesRestoreCommands(expected); |
Lorenzo Colitti | df42ddd | 2017-02-28 01:20:13 +0900 | [diff] [blame] | 444 | } |
Lorenzo Colitti | e8b56e4 | 2017-04-26 15:16:03 +0900 | [diff] [blame] | 445 | |
Joel Scherpelz | 01cc549 | 2017-06-16 10:45:14 +0900 | [diff] [blame] | 446 | const std::vector<std::string> makeInterfaceSharedQuotaCommands(const std::string& iface, |
Lorenzo Colitti | 48f8300 | 2017-07-06 15:06:04 +0900 | [diff] [blame] | 447 | int ruleIndex, int64_t quota, |
| 448 | bool insertQuota) { |
Joel Scherpelz | 01cc549 | 2017-06-16 10:45:14 +0900 | [diff] [blame] | 449 | const std::string chain = "bw_costly_shared"; |
| 450 | const char* c_chain = chain.c_str(); |
| 451 | const char* c_iface = iface.c_str(); |
| 452 | std::vector<std::string> cmds = { |
Lorenzo Colitti | 48f8300 | 2017-07-06 15:06:04 +0900 | [diff] [blame] | 453 | "*filter", |
Joel Scherpelz | 01cc549 | 2017-06-16 10:45:14 +0900 | [diff] [blame] | 454 | 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] | 455 | 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] | 456 | StringPrintf("-A bw_FORWARD -o %s --jump %s", c_iface, c_chain), |
Joel Scherpelz | 01cc549 | 2017-06-16 10:45:14 +0900 | [diff] [blame] | 457 | }; |
Lorenzo Colitti | 48f8300 | 2017-07-06 15:06:04 +0900 | [diff] [blame] | 458 | if (insertQuota) { |
| 459 | cmds.push_back(StringPrintf( |
| 460 | "-I %s -m quota2 ! --quota %" PRIu64 " --name shared --jump REJECT", c_chain, quota)); |
| 461 | } |
| 462 | cmds.push_back("COMMIT\n"); |
| 463 | return {Join(cmds, "\n")}; |
Joel Scherpelz | 01cc549 | 2017-06-16 10:45:14 +0900 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | const std::vector<std::string> removeInterfaceSharedQuotaCommands(const std::string& iface, |
Lorenzo Colitti | 48f8300 | 2017-07-06 15:06:04 +0900 | [diff] [blame] | 467 | int64_t quota, bool deleteQuota) { |
Joel Scherpelz | 01cc549 | 2017-06-16 10:45:14 +0900 | [diff] [blame] | 468 | const std::string chain = "bw_costly_shared"; |
| 469 | const char* c_chain = chain.c_str(); |
| 470 | const char* c_iface = iface.c_str(); |
| 471 | std::vector<std::string> cmds = { |
Lorenzo Colitti | 48f8300 | 2017-07-06 15:06:04 +0900 | [diff] [blame] | 472 | "*filter", |
Joel Scherpelz | 01cc549 | 2017-06-16 10:45:14 +0900 | [diff] [blame] | 473 | StringPrintf("-D bw_INPUT -i %s --jump %s", c_iface, c_chain), |
| 474 | StringPrintf("-D bw_OUTPUT -o %s --jump %s", c_iface, c_chain), |
| 475 | StringPrintf("-D bw_FORWARD -o %s --jump %s", c_iface, c_chain), |
Joel Scherpelz | 01cc549 | 2017-06-16 10:45:14 +0900 | [diff] [blame] | 476 | }; |
Lorenzo Colitti | 48f8300 | 2017-07-06 15:06:04 +0900 | [diff] [blame] | 477 | if (deleteQuota) { |
| 478 | cmds.push_back(StringPrintf( |
| 479 | "-D %s -m quota2 ! --quota %" PRIu64 " --name shared --jump REJECT", c_chain, quota)); |
| 480 | } |
| 481 | cmds.push_back("COMMIT\n"); |
| 482 | return {Join(cmds, "\n")}; |
Joel Scherpelz | 01cc549 | 2017-06-16 10:45:14 +0900 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | TEST_F(BandwidthControllerTest, TestSetInterfaceSharedQuotaDuplicate) { |
| 486 | constexpr uint64_t kQuota = 123456; |
| 487 | const std::string iface = mTun.name(); |
Lorenzo Colitti | 48f8300 | 2017-07-06 15:06:04 +0900 | [diff] [blame] | 488 | std::vector<std::string> expected = makeInterfaceSharedQuotaCommands(iface, 1, 123456, true); |
Joel Scherpelz | 01cc549 | 2017-06-16 10:45:14 +0900 | [diff] [blame] | 489 | EXPECT_EQ(0, mBw.setInterfaceSharedQuota(iface, kQuota)); |
Lorenzo Colitti | 48f8300 | 2017-07-06 15:06:04 +0900 | [diff] [blame] | 490 | expectIptablesRestoreCommands(expected); |
Joel Scherpelz | 01cc549 | 2017-06-16 10:45:14 +0900 | [diff] [blame] | 491 | |
| 492 | expected = {}; |
| 493 | EXPECT_EQ(0, mBw.setInterfaceSharedQuota(iface, kQuota)); |
Lorenzo Colitti | 48f8300 | 2017-07-06 15:06:04 +0900 | [diff] [blame] | 494 | expectIptablesRestoreCommands(expected); |
Joel Scherpelz | 01cc549 | 2017-06-16 10:45:14 +0900 | [diff] [blame] | 495 | |
Lorenzo Colitti | 48f8300 | 2017-07-06 15:06:04 +0900 | [diff] [blame] | 496 | expected = removeInterfaceSharedQuotaCommands(iface, kQuota, true); |
Joel Scherpelz | 01cc549 | 2017-06-16 10:45:14 +0900 | [diff] [blame] | 497 | EXPECT_EQ(0, mBw.removeInterfaceSharedQuota(iface)); |
Lorenzo Colitti | 48f8300 | 2017-07-06 15:06:04 +0900 | [diff] [blame] | 498 | expectIptablesRestoreCommands(expected); |
Joel Scherpelz | 01cc549 | 2017-06-16 10:45:14 +0900 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | TEST_F(BandwidthControllerTest, TestSetInterfaceSharedQuotaUpdate) { |
| 502 | constexpr uint64_t kOldQuota = 123456; |
| 503 | const std::string iface = mTun.name(); |
Lorenzo Colitti | 48f8300 | 2017-07-06 15:06:04 +0900 | [diff] [blame] | 504 | std::vector<std::string> expected = makeInterfaceSharedQuotaCommands(iface, 1, kOldQuota, true); |
Joel Scherpelz | 01cc549 | 2017-06-16 10:45:14 +0900 | [diff] [blame] | 505 | EXPECT_EQ(0, mBw.setInterfaceSharedQuota(iface, kOldQuota)); |
Lorenzo Colitti | 48f8300 | 2017-07-06 15:06:04 +0900 | [diff] [blame] | 506 | expectIptablesRestoreCommands(expected); |
Joel Scherpelz | 01cc549 | 2017-06-16 10:45:14 +0900 | [diff] [blame] | 507 | |
| 508 | constexpr uint64_t kNewQuota = kOldQuota + 1; |
| 509 | expected = {}; |
| 510 | expectUpdateQuota(kNewQuota); |
| 511 | EXPECT_EQ(0, mBw.setInterfaceSharedQuota(iface, kNewQuota)); |
Lorenzo Colitti | 48f8300 | 2017-07-06 15:06:04 +0900 | [diff] [blame] | 512 | expectIptablesRestoreCommands(expected); |
Joel Scherpelz | 01cc549 | 2017-06-16 10:45:14 +0900 | [diff] [blame] | 513 | |
Lorenzo Colitti | 48f8300 | 2017-07-06 15:06:04 +0900 | [diff] [blame] | 514 | expected = removeInterfaceSharedQuotaCommands(iface, kNewQuota, true); |
Joel Scherpelz | 01cc549 | 2017-06-16 10:45:14 +0900 | [diff] [blame] | 515 | EXPECT_EQ(0, mBw.removeInterfaceSharedQuota(iface)); |
Lorenzo Colitti | 48f8300 | 2017-07-06 15:06:04 +0900 | [diff] [blame] | 516 | expectIptablesRestoreCommands(expected); |
Joel Scherpelz | 01cc549 | 2017-06-16 10:45:14 +0900 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | TEST_F(BandwidthControllerTest, TestSetInterfaceSharedQuotaTwoInterfaces) { |
| 520 | constexpr uint64_t kQuota = 123456; |
| 521 | const std::vector<std::string> ifaces{ |
| 522 | {"a" + mTun.name()}, |
| 523 | {"b" + mTun.name()}, |
| 524 | }; |
| 525 | |
| 526 | for (const auto& iface : ifaces) { |
Lorenzo Colitti | 48f8300 | 2017-07-06 15:06:04 +0900 | [diff] [blame] | 527 | // Quota rule is only added when the total number of |
| 528 | // interfaces transitions from 0 -> 1. |
Joel Scherpelz | 01cc549 | 2017-06-16 10:45:14 +0900 | [diff] [blame] | 529 | bool first = (iface == ifaces[0]); |
Lorenzo Colitti | 48f8300 | 2017-07-06 15:06:04 +0900 | [diff] [blame] | 530 | auto expected = makeInterfaceSharedQuotaCommands(iface, 1, kQuota, first); |
Joel Scherpelz | 01cc549 | 2017-06-16 10:45:14 +0900 | [diff] [blame] | 531 | EXPECT_EQ(0, mBw.setInterfaceSharedQuota(iface, kQuota)); |
Lorenzo Colitti | 48f8300 | 2017-07-06 15:06:04 +0900 | [diff] [blame] | 532 | expectIptablesRestoreCommands(expected); |
Joel Scherpelz | 01cc549 | 2017-06-16 10:45:14 +0900 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | for (const auto& iface : ifaces) { |
Lorenzo Colitti | 48f8300 | 2017-07-06 15:06:04 +0900 | [diff] [blame] | 536 | // Quota rule is only removed when the total number of |
| 537 | // interfaces transitions from 1 -> 0. |
Joel Scherpelz | 01cc549 | 2017-06-16 10:45:14 +0900 | [diff] [blame] | 538 | bool last = (iface == ifaces[1]); |
Lorenzo Colitti | 48f8300 | 2017-07-06 15:06:04 +0900 | [diff] [blame] | 539 | auto expected = removeInterfaceSharedQuotaCommands(iface, kQuota, last); |
Joel Scherpelz | 01cc549 | 2017-06-16 10:45:14 +0900 | [diff] [blame] | 540 | EXPECT_EQ(0, mBw.removeInterfaceSharedQuota(iface)); |
Lorenzo Colitti | 48f8300 | 2017-07-06 15:06:04 +0900 | [diff] [blame] | 541 | expectIptablesRestoreCommands(expected); |
Joel Scherpelz | 01cc549 | 2017-06-16 10:45:14 +0900 | [diff] [blame] | 542 | } |
| 543 | } |
| 544 | |
Lorenzo Colitti | e8b56e4 | 2017-04-26 15:16:03 +0900 | [diff] [blame] | 545 | TEST_F(BandwidthControllerTest, IptablesAlertCmd) { |
| 546 | std::vector<std::string> expected = { |
Lorenzo Colitti | 3c27270 | 2017-04-26 15:48:13 +0900 | [diff] [blame] | 547 | "*filter\n" |
| 548 | "-I bw_INPUT -m quota2 ! --quota 123456 --name MyWonderfulAlert\n" |
| 549 | "-I bw_OUTPUT -m quota2 ! --quota 123456 --name MyWonderfulAlert\n" |
| 550 | "COMMIT\n" |
Lorenzo Colitti | e8b56e4 | 2017-04-26 15:16:03 +0900 | [diff] [blame] | 551 | }; |
| 552 | EXPECT_EQ(0, runIptablesAlertCmd(IptOp::IptOpInsert, "MyWonderfulAlert", 123456)); |
Lorenzo Colitti | 3c27270 | 2017-04-26 15:48:13 +0900 | [diff] [blame] | 553 | expectIptablesRestoreCommands(expected); |
Lorenzo Colitti | e8b56e4 | 2017-04-26 15:16:03 +0900 | [diff] [blame] | 554 | |
| 555 | expected = { |
Lorenzo Colitti | 3c27270 | 2017-04-26 15:48:13 +0900 | [diff] [blame] | 556 | "*filter\n" |
| 557 | "-D bw_INPUT -m quota2 ! --quota 123456 --name MyWonderfulAlert\n" |
| 558 | "-D bw_OUTPUT -m quota2 ! --quota 123456 --name MyWonderfulAlert\n" |
| 559 | "COMMIT\n" |
Lorenzo Colitti | e8b56e4 | 2017-04-26 15:16:03 +0900 | [diff] [blame] | 560 | }; |
| 561 | EXPECT_EQ(0, runIptablesAlertCmd(IptOp::IptOpDelete, "MyWonderfulAlert", 123456)); |
Lorenzo Colitti | 3c27270 | 2017-04-26 15:48:13 +0900 | [diff] [blame] | 562 | expectIptablesRestoreCommands(expected); |
Lorenzo Colitti | e8b56e4 | 2017-04-26 15:16:03 +0900 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | TEST_F(BandwidthControllerTest, IptablesAlertFwdCmd) { |
| 566 | std::vector<std::string> expected = { |
Lorenzo Colitti | 3c27270 | 2017-04-26 15:48:13 +0900 | [diff] [blame] | 567 | "*filter\n" |
| 568 | "-I bw_FORWARD -m quota2 ! --quota 123456 --name MyWonderfulAlert\n" |
| 569 | "COMMIT\n" |
Lorenzo Colitti | e8b56e4 | 2017-04-26 15:16:03 +0900 | [diff] [blame] | 570 | }; |
| 571 | EXPECT_EQ(0, runIptablesAlertFwdCmd(IptOp::IptOpInsert, "MyWonderfulAlert", 123456)); |
Lorenzo Colitti | 3c27270 | 2017-04-26 15:48:13 +0900 | [diff] [blame] | 572 | expectIptablesRestoreCommands(expected); |
Lorenzo Colitti | e8b56e4 | 2017-04-26 15:16:03 +0900 | [diff] [blame] | 573 | |
| 574 | expected = { |
Lorenzo Colitti | 3c27270 | 2017-04-26 15:48:13 +0900 | [diff] [blame] | 575 | "*filter\n" |
| 576 | "-D bw_FORWARD -m quota2 ! --quota 123456 --name MyWonderfulAlert\n" |
| 577 | "COMMIT\n" |
Lorenzo Colitti | e8b56e4 | 2017-04-26 15:16:03 +0900 | [diff] [blame] | 578 | }; |
| 579 | EXPECT_EQ(0, runIptablesAlertFwdCmd(IptOp::IptOpDelete, "MyWonderfulAlert", 123456)); |
Lorenzo Colitti | 3c27270 | 2017-04-26 15:48:13 +0900 | [diff] [blame] | 580 | expectIptablesRestoreCommands(expected); |
Lorenzo Colitti | e8b56e4 | 2017-04-26 15:16:03 +0900 | [diff] [blame] | 581 | } |
Lorenzo Colitti | f4dfa68 | 2017-04-28 11:09:07 +0900 | [diff] [blame] | 582 | |
Lorenzo Colitti | 3807822 | 2017-07-06 17:27:23 +0900 | [diff] [blame^] | 583 | TEST_F(BandwidthControllerTest, CostlyAlert) { |
| 584 | const int64_t kQuota = 123456; |
| 585 | int64_t alertBytes = 0; |
| 586 | |
| 587 | std::vector<std::string> expected = { |
| 588 | "-A bw_costly_shared -m quota2 ! --quota 123456 --name sharedAlert\n", |
| 589 | }; |
| 590 | EXPECT_EQ(0, setCostlyAlert("shared", kQuota, &alertBytes)); |
| 591 | EXPECT_EQ(kQuota, alertBytes); |
| 592 | expectIptablesCommands(expected); |
| 593 | |
| 594 | expected = {}; |
| 595 | expectUpdateQuota(kQuota); |
| 596 | EXPECT_EQ(0, setCostlyAlert("shared", kQuota + 1, &alertBytes)); |
| 597 | EXPECT_EQ(kQuota + 1, alertBytes); |
| 598 | expectIptablesCommands(expected); |
| 599 | |
| 600 | expected = { |
| 601 | "-D bw_costly_shared -m quota2 ! --quota 123457 --name sharedAlert\n" |
| 602 | }; |
| 603 | EXPECT_EQ(0, removeCostlyAlert("shared", &alertBytes)); |
| 604 | EXPECT_EQ(0, alertBytes); |
| 605 | expectIptablesCommands(expected); |
| 606 | } |
| 607 | |
Lorenzo Colitti | f4dfa68 | 2017-04-28 11:09:07 +0900 | [diff] [blame] | 608 | TEST_F(BandwidthControllerTest, ManipulateSpecialApps) { |
| 609 | std::vector<const char *> appUids = { "1000", "1001", "10012" }; |
| 610 | |
| 611 | std::vector<std::string> expected = { |
Lorenzo Colitti | 911bc4c | 2017-04-28 14:34:01 +0900 | [diff] [blame] | 612 | "*filter\n" |
| 613 | "-I bw_happy_box -m owner --uid-owner 1000 --jump RETURN\n" |
| 614 | "-I bw_happy_box -m owner --uid-owner 1001 --jump RETURN\n" |
| 615 | "-I bw_happy_box -m owner --uid-owner 10012 --jump RETURN\n" |
| 616 | "COMMIT\n" |
Lorenzo Colitti | f4dfa68 | 2017-04-28 11:09:07 +0900 | [diff] [blame] | 617 | }; |
| 618 | EXPECT_EQ(0, mBw.addNiceApps(appUids.size(), const_cast<char**>(&appUids[0]))); |
Lorenzo Colitti | 911bc4c | 2017-04-28 14:34:01 +0900 | [diff] [blame] | 619 | expectIptablesRestoreCommands(expected); |
Lorenzo Colitti | f4dfa68 | 2017-04-28 11:09:07 +0900 | [diff] [blame] | 620 | |
| 621 | expected = { |
Lorenzo Colitti | 911bc4c | 2017-04-28 14:34:01 +0900 | [diff] [blame] | 622 | "*filter\n" |
| 623 | "-D bw_penalty_box -m owner --uid-owner 1000 --jump REJECT\n" |
| 624 | "-D bw_penalty_box -m owner --uid-owner 1001 --jump REJECT\n" |
| 625 | "-D bw_penalty_box -m owner --uid-owner 10012 --jump REJECT\n" |
| 626 | "COMMIT\n" |
Lorenzo Colitti | f4dfa68 | 2017-04-28 11:09:07 +0900 | [diff] [blame] | 627 | }; |
| 628 | EXPECT_EQ(0, mBw.removeNaughtyApps(appUids.size(), const_cast<char**>(&appUids[0]))); |
Lorenzo Colitti | 911bc4c | 2017-04-28 14:34:01 +0900 | [diff] [blame] | 629 | expectIptablesRestoreCommands(expected); |
Lorenzo Colitti | f4dfa68 | 2017-04-28 11:09:07 +0900 | [diff] [blame] | 630 | } |