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