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