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 | |
Lorenzo Colitti | 86a4798 | 2016-03-18 17:52:25 +0900 | [diff] [blame] | 33 | #include "BandwidthController.h" |
Lorenzo Colitti | 0f15055 | 2016-03-28 02:30:27 +0900 | [diff] [blame] | 34 | #include "IptablesBaseTest.h" |
Lorenzo Colitti | df42ddd | 2017-02-28 01:20:13 +0900 | [diff] [blame] | 35 | #include "tun_interface.h" |
| 36 | |
| 37 | using android::base::StringPrintf; |
| 38 | using android::net::TunInterface; |
Lorenzo Colitti | 86a4798 | 2016-03-18 17:52:25 +0900 | [diff] [blame] | 39 | |
Lorenzo Colitti | 0f15055 | 2016-03-28 02:30:27 +0900 | [diff] [blame] | 40 | class BandwidthControllerTest : public IptablesBaseTest { |
Lorenzo Colitti | 86a4798 | 2016-03-18 17:52:25 +0900 | [diff] [blame] | 41 | public: |
| 42 | BandwidthControllerTest() { |
| 43 | BandwidthController::execFunction = fake_android_fork_exec; |
| 44 | BandwidthController::popenFunction = fake_popen; |
Lorenzo Colitti | 56c4b1e | 2017-02-01 02:45:10 +0900 | [diff] [blame] | 45 | BandwidthController::iptablesRestoreFunction = fakeExecIptablesRestoreWithOutput; |
Lorenzo Colitti | 86a4798 | 2016-03-18 17:52:25 +0900 | [diff] [blame] | 46 | } |
| 47 | BandwidthController mBw; |
Lorenzo Colitti | df42ddd | 2017-02-28 01:20:13 +0900 | [diff] [blame] | 48 | TunInterface mTun; |
| 49 | |
| 50 | void SetUp() { |
| 51 | ASSERT_EQ(0, mTun.init()); |
| 52 | } |
| 53 | |
| 54 | void TearDown() { |
| 55 | mTun.destroy(); |
| 56 | } |
Lorenzo Colitti | bbeaf9a | 2016-07-08 18:24:26 +0900 | [diff] [blame] | 57 | |
Lorenzo Colitti | 56c4b1e | 2017-02-01 02:45:10 +0900 | [diff] [blame] | 58 | void addIptablesRestoreOutput(std::string contents) { |
| 59 | sIptablesRestoreOutput.push_back(contents); |
| 60 | } |
| 61 | |
Lorenzo Colitti | ce6748a | 2017-02-02 01:34:33 +0900 | [diff] [blame] | 62 | void addIptablesRestoreOutput(std::string contents1, std::string contents2) { |
| 63 | sIptablesRestoreOutput.push_back(contents1); |
| 64 | sIptablesRestoreOutput.push_back(contents2); |
| 65 | } |
| 66 | |
Lorenzo Colitti | 56c4b1e | 2017-02-01 02:45:10 +0900 | [diff] [blame] | 67 | void clearIptablesRestoreOutput() { |
| 68 | sIptablesRestoreOutput.clear(); |
| 69 | } |
| 70 | |
| 71 | void expectSetupCommands(const std::string& expectedClean, std::string expectedAccounting) { |
| 72 | std::string expectedList = |
| 73 | "*filter\n" |
| 74 | "-S\n" |
| 75 | "COMMIT\n"; |
| 76 | |
| 77 | std::string expectedFlush = |
| 78 | "*filter\n" |
| 79 | ":bw_INPUT -\n" |
| 80 | ":bw_OUTPUT -\n" |
| 81 | ":bw_FORWARD -\n" |
| 82 | ":bw_happy_box -\n" |
| 83 | ":bw_penalty_box -\n" |
| 84 | ":bw_data_saver -\n" |
| 85 | ":bw_costly_shared -\n" |
| 86 | "COMMIT\n" |
| 87 | "*raw\n" |
| 88 | ":bw_raw_PREROUTING -\n" |
| 89 | "COMMIT\n" |
| 90 | "*mangle\n" |
| 91 | ":bw_mangle_POSTROUTING -\n" |
| 92 | "COMMIT\n"; |
| 93 | |
| 94 | ExpectedIptablesCommands expected = {{ V4, expectedList }}; |
| 95 | if (expectedClean.size()) { |
| 96 | expected.push_back({ V4V6, expectedClean }); |
| 97 | } |
| 98 | expected.push_back({ V4V6, expectedFlush }); |
| 99 | if (expectedAccounting.size()) { |
| 100 | expected.push_back({ V4V6, expectedAccounting }); |
| 101 | } |
| 102 | |
| 103 | expectIptablesRestoreCommands(expected); |
| 104 | } |
Lorenzo Colitti | e8b56e4 | 2017-04-26 15:16:03 +0900 | [diff] [blame] | 105 | |
| 106 | using IptOp = BandwidthController::IptOp; |
| 107 | |
| 108 | int runIptablesAlertCmd(IptOp a, const char *b, int64_t c) { |
| 109 | return mBw.runIptablesAlertCmd(a, b, c); |
| 110 | } |
| 111 | |
| 112 | int runIptablesAlertFwdCmd(IptOp a, const char *b, int64_t c) { |
| 113 | return mBw.runIptablesAlertFwdCmd(a, b, c); |
| 114 | } |
Lorenzo Colitti | 86a4798 | 2016-03-18 17:52:25 +0900 | [diff] [blame] | 115 | }; |
| 116 | |
Lorenzo Colitti | a0dc8a5 | 2016-03-26 22:42:07 +0900 | [diff] [blame] | 117 | TEST_F(BandwidthControllerTest, TestSetupIptablesHooks) { |
Lorenzo Colitti | 56c4b1e | 2017-02-01 02:45:10 +0900 | [diff] [blame] | 118 | // Pretend some bw_costly_shared_<iface> rules already exist... |
| 119 | addIptablesRestoreOutput( |
| 120 | "-P OUTPUT ACCEPT\n" |
| 121 | "-N bw_costly_rmnet_data0\n" |
| 122 | "-N bw_costly_shared\n" |
| 123 | "-N unrelated\n" |
| 124 | "-N bw_costly_rmnet_data7\n"); |
| 125 | |
| 126 | // ... and expect that they be flushed and deleted. |
| 127 | std::string expectedCleanCmds = |
Lorenzo Colitti | 13debb8 | 2016-03-27 17:46:30 +0900 | [diff] [blame] | 128 | "*filter\n" |
Lorenzo Colitti | 56c4b1e | 2017-02-01 02:45:10 +0900 | [diff] [blame] | 129 | ":bw_costly_rmnet_data0 -\n" |
| 130 | "-X bw_costly_rmnet_data0\n" |
| 131 | ":bw_costly_rmnet_data7 -\n" |
| 132 | "-X bw_costly_rmnet_data7\n" |
| 133 | "COMMIT\n"; |
| 134 | |
| 135 | mBw.setupIptablesHooks(); |
| 136 | expectSetupCommands(expectedCleanCmds, ""); |
Lorenzo Colitti | a0dc8a5 | 2016-03-26 22:42:07 +0900 | [diff] [blame] | 137 | } |
| 138 | |
Lorenzo Colitti | 86a4798 | 2016-03-18 17:52:25 +0900 | [diff] [blame] | 139 | TEST_F(BandwidthControllerTest, TestEnableBandwidthControl) { |
Lorenzo Colitti | 56c4b1e | 2017-02-01 02:45:10 +0900 | [diff] [blame] | 140 | // Pretend no bw_costly_shared_<iface> rules already exist... |
| 141 | addIptablesRestoreOutput( |
| 142 | "-P OUTPUT ACCEPT\n" |
| 143 | "-N bw_costly_shared\n" |
| 144 | "-N unrelated\n"); |
| 145 | |
| 146 | // ... so none are flushed or deleted. |
| 147 | std::string expectedClean = ""; |
| 148 | |
| 149 | std::string expectedAccounting = |
Lorenzo Colitti | 13debb8 | 2016-03-27 17:46:30 +0900 | [diff] [blame] | 150 | "*filter\n" |
| 151 | "-A bw_INPUT -m owner --socket-exists\n" |
| 152 | "-A bw_OUTPUT -m owner --socket-exists\n" |
| 153 | "-A bw_costly_shared --jump bw_penalty_box\n" |
| 154 | "-A bw_penalty_box --jump bw_happy_box\n" |
| 155 | "-A bw_happy_box --jump bw_data_saver\n" |
| 156 | "-A bw_data_saver -j RETURN\n" |
| 157 | "-I bw_happy_box -m owner --uid-owner 0-9999 --jump RETURN\n" |
| 158 | "COMMIT\n" |
| 159 | "*raw\n" |
| 160 | "-A bw_raw_PREROUTING -m owner --socket-exists\n" |
| 161 | "COMMIT\n" |
| 162 | "*mangle\n" |
| 163 | "-A bw_mangle_POSTROUTING -m owner --socket-exists\n" |
Lorenzo Colitti | 56c4b1e | 2017-02-01 02:45:10 +0900 | [diff] [blame] | 164 | "COMMIT\n"; |
Lorenzo Colitti | 13debb8 | 2016-03-27 17:46:30 +0900 | [diff] [blame] | 165 | |
Lorenzo Colitti | 56c4b1e | 2017-02-01 02:45:10 +0900 | [diff] [blame] | 166 | mBw.enableBandwidthControl(false); |
| 167 | expectSetupCommands(expectedClean, expectedAccounting); |
Lorenzo Colitti | 86a4798 | 2016-03-18 17:52:25 +0900 | [diff] [blame] | 168 | } |
| 169 | |
Lorenzo Colitti | a0dc8a5 | 2016-03-26 22:42:07 +0900 | [diff] [blame] | 170 | TEST_F(BandwidthControllerTest, TestDisableBandwidthControl) { |
Lorenzo Colitti | 56c4b1e | 2017-02-01 02:45:10 +0900 | [diff] [blame] | 171 | // Pretend some bw_costly_shared_<iface> rules already exist... |
| 172 | addIptablesRestoreOutput( |
| 173 | "-P OUTPUT ACCEPT\n" |
| 174 | "-N bw_costly_rmnet_data0\n" |
| 175 | "-N bw_costly_shared\n" |
| 176 | "-N unrelated\n" |
| 177 | "-N bw_costly_rmnet_data7\n"); |
| 178 | |
| 179 | // ... and expect that they be flushed. |
| 180 | std::string expectedCleanCmds = |
Lorenzo Colitti | 13debb8 | 2016-03-27 17:46:30 +0900 | [diff] [blame] | 181 | "*filter\n" |
Lorenzo Colitti | 56c4b1e | 2017-02-01 02:45:10 +0900 | [diff] [blame] | 182 | ":bw_costly_rmnet_data0 -\n" |
| 183 | ":bw_costly_rmnet_data7 -\n" |
| 184 | "COMMIT\n"; |
| 185 | |
| 186 | mBw.disableBandwidthControl(); |
| 187 | expectSetupCommands(expectedCleanCmds, ""); |
Lorenzo Colitti | a0dc8a5 | 2016-03-26 22:42:07 +0900 | [diff] [blame] | 188 | } |
| 189 | |
Lorenzo Colitti | 86a4798 | 2016-03-18 17:52:25 +0900 | [diff] [blame] | 190 | TEST_F(BandwidthControllerTest, TestEnableDataSaver) { |
| 191 | mBw.enableDataSaver(true); |
| 192 | std::vector<std::string> expected = { |
Lorenzo Colitti | 464eabe | 2016-03-25 13:38:19 +0900 | [diff] [blame] | 193 | "-R bw_data_saver 1 --jump REJECT", |
Lorenzo Colitti | 86a4798 | 2016-03-18 17:52:25 +0900 | [diff] [blame] | 194 | }; |
| 195 | expectIptablesCommands(expected); |
| 196 | |
| 197 | mBw.enableDataSaver(false); |
| 198 | expected = { |
Lorenzo Colitti | 464eabe | 2016-03-25 13:38:19 +0900 | [diff] [blame] | 199 | "-R bw_data_saver 1 --jump RETURN", |
Lorenzo Colitti | 86a4798 | 2016-03-18 17:52:25 +0900 | [diff] [blame] | 200 | }; |
| 201 | expectIptablesCommands(expected); |
| 202 | } |
Lorenzo Colitti | bbeaf9a | 2016-07-08 18:24:26 +0900 | [diff] [blame] | 203 | |
| 204 | std::string kIPv4TetherCounters = android::base::Join(std::vector<std::string> { |
| 205 | "Chain natctrl_tether_counters (4 references)", |
| 206 | " pkts bytes target prot opt in out source destination", |
| 207 | " 26 2373 RETURN all -- wlan0 rmnet0 0.0.0.0/0 0.0.0.0/0", |
| 208 | " 27 2002 RETURN all -- rmnet0 wlan0 0.0.0.0/0 0.0.0.0/0", |
| 209 | " 1040 107471 RETURN all -- bt-pan rmnet0 0.0.0.0/0 0.0.0.0/0", |
| 210 | " 1450 1708806 RETURN all -- rmnet0 bt-pan 0.0.0.0/0 0.0.0.0/0", |
| 211 | }, '\n'); |
| 212 | |
Lorenzo Colitti | 26c9132 | 2016-07-11 11:36:25 +0900 | [diff] [blame] | 213 | std::string kIPv6TetherCounters = android::base::Join(std::vector<std::string> { |
| 214 | "Chain natctrl_tether_counters (2 references)", |
| 215 | " pkts bytes target prot opt in out source destination", |
| 216 | " 10000 10000000 RETURN all wlan0 rmnet0 ::/0 ::/0", |
| 217 | " 20000 20000000 RETURN all rmnet0 wlan0 ::/0 ::/0", |
| 218 | }, '\n'); |
| 219 | |
Lorenzo Colitti | bbeaf9a | 2016-07-08 18:24:26 +0900 | [diff] [blame] | 220 | std::string readSocketClientResponse(int fd) { |
| 221 | char buf[32768]; |
| 222 | ssize_t bytesRead = read(fd, buf, sizeof(buf)); |
| 223 | if (bytesRead < 0) { |
| 224 | return ""; |
| 225 | } |
| 226 | for (int i = 0; i < bytesRead; i++) { |
| 227 | if (buf[i] == '\0') buf[i] = '\n'; |
| 228 | } |
| 229 | return std::string(buf, bytesRead); |
| 230 | } |
| 231 | |
Lorenzo Colitti | 750e8fc | 2016-07-12 01:19:49 +0900 | [diff] [blame] | 232 | void expectNoSocketClientResponse(int fd) { |
| 233 | char buf[64]; |
| 234 | EXPECT_EQ(-1, read(fd, buf, sizeof(buf))); |
| 235 | } |
| 236 | |
Lorenzo Colitti | bbeaf9a | 2016-07-08 18:24:26 +0900 | [diff] [blame] | 237 | TEST_F(BandwidthControllerTest, TestGetTetherStats) { |
| 238 | int socketPair[2]; |
| 239 | ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM, 0, socketPair)); |
| 240 | ASSERT_EQ(0, fcntl(socketPair[0], F_SETFL, O_NONBLOCK | fcntl(socketPair[0], F_GETFL))); |
| 241 | ASSERT_EQ(0, fcntl(socketPair[1], F_SETFL, O_NONBLOCK | fcntl(socketPair[1], F_GETFL))); |
| 242 | SocketClient cli(socketPair[0], false); |
| 243 | |
| 244 | std::string err; |
| 245 | BandwidthController::TetherStats filter; |
Lorenzo Colitti | 26c9132 | 2016-07-11 11:36:25 +0900 | [diff] [blame] | 246 | |
| 247 | // 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] | 248 | addIptablesRestoreOutput(kIPv4TetherCounters); |
Lorenzo Colitti | 26c9132 | 2016-07-11 11:36:25 +0900 | [diff] [blame] | 249 | ASSERT_EQ(-1, mBw.getTetherStats(&cli, filter, err)); |
| 250 | expectNoSocketClientResponse(socketPair[1]); |
Lorenzo Colitti | ce6748a | 2017-02-02 01:34:33 +0900 | [diff] [blame] | 251 | clearIptablesRestoreOutput(); |
Lorenzo Colitti | 26c9132 | 2016-07-11 11:36:25 +0900 | [diff] [blame] | 252 | |
Lorenzo Colitti | ce6748a | 2017-02-02 01:34:33 +0900 | [diff] [blame] | 253 | addIptablesRestoreOutput(kIPv6TetherCounters); |
Lorenzo Colitti | 26c9132 | 2016-07-11 11:36:25 +0900 | [diff] [blame] | 254 | ASSERT_EQ(-1, mBw.getTetherStats(&cli, filter, err)); |
Lorenzo Colitti | ce6748a | 2017-02-02 01:34:33 +0900 | [diff] [blame] | 255 | clearIptablesRestoreOutput(); |
Lorenzo Colitti | 26c9132 | 2016-07-11 11:36:25 +0900 | [diff] [blame] | 256 | |
| 257 | // IPv4 and IPv6 counters are properly added together. |
Lorenzo Colitti | ce6748a | 2017-02-02 01:34:33 +0900 | [diff] [blame] | 258 | addIptablesRestoreOutput(kIPv4TetherCounters, kIPv6TetherCounters); |
Lorenzo Colitti | 26c9132 | 2016-07-11 11:36:25 +0900 | [diff] [blame] | 259 | filter = BandwidthController::TetherStats(); |
Lorenzo Colitti | bbeaf9a | 2016-07-08 18:24:26 +0900 | [diff] [blame] | 260 | std::string expected = |
Lorenzo Colitti | 26c9132 | 2016-07-11 11:36:25 +0900 | [diff] [blame] | 261 | "114 wlan0 rmnet0 10002373 10026 20002002 20027\n" |
Lorenzo Colitti | bbeaf9a | 2016-07-08 18:24:26 +0900 | [diff] [blame] | 262 | "114 bt-pan rmnet0 107471 1040 1708806 1450\n" |
| 263 | "200 Tethering stats list completed\n"; |
Lorenzo Colitti | 750e8fc | 2016-07-12 01:19:49 +0900 | [diff] [blame] | 264 | ASSERT_EQ(0, mBw.getTetherStats(&cli, filter, err)); |
Lorenzo Colitti | bbeaf9a | 2016-07-08 18:24:26 +0900 | [diff] [blame] | 265 | ASSERT_EQ(expected, readSocketClientResponse(socketPair[1])); |
Lorenzo Colitti | 26c9132 | 2016-07-11 11:36:25 +0900 | [diff] [blame] | 266 | expectNoSocketClientResponse(socketPair[1]); |
Lorenzo Colitti | ce6748a | 2017-02-02 01:34:33 +0900 | [diff] [blame] | 267 | clearIptablesRestoreOutput(); |
Lorenzo Colitti | bbeaf9a | 2016-07-08 18:24:26 +0900 | [diff] [blame] | 268 | |
Lorenzo Colitti | 26c9132 | 2016-07-11 11:36:25 +0900 | [diff] [blame] | 269 | // Test filtering. |
Lorenzo Colitti | ce6748a | 2017-02-02 01:34:33 +0900 | [diff] [blame] | 270 | addIptablesRestoreOutput(kIPv4TetherCounters, kIPv6TetherCounters); |
Lorenzo Colitti | bbeaf9a | 2016-07-08 18:24:26 +0900 | [diff] [blame] | 271 | filter = BandwidthController::TetherStats("bt-pan", "rmnet0", -1, -1, -1, -1); |
| 272 | expected = "221 bt-pan rmnet0 107471 1040 1708806 1450\n"; |
Lorenzo Colitti | 750e8fc | 2016-07-12 01:19:49 +0900 | [diff] [blame] | 273 | ASSERT_EQ(0, mBw.getTetherStats(&cli, filter, err)); |
Lorenzo Colitti | bbeaf9a | 2016-07-08 18:24:26 +0900 | [diff] [blame] | 274 | ASSERT_EQ(expected, readSocketClientResponse(socketPair[1])); |
Lorenzo Colitti | 26c9132 | 2016-07-11 11:36:25 +0900 | [diff] [blame] | 275 | expectNoSocketClientResponse(socketPair[1]); |
Lorenzo Colitti | ce6748a | 2017-02-02 01:34:33 +0900 | [diff] [blame] | 276 | clearIptablesRestoreOutput(); |
Lorenzo Colitti | bbeaf9a | 2016-07-08 18:24:26 +0900 | [diff] [blame] | 277 | |
Lorenzo Colitti | ce6748a | 2017-02-02 01:34:33 +0900 | [diff] [blame] | 278 | addIptablesRestoreOutput(kIPv4TetherCounters, kIPv6TetherCounters); |
Lorenzo Colitti | 26c9132 | 2016-07-11 11:36:25 +0900 | [diff] [blame] | 279 | filter = BandwidthController::TetherStats("wlan0", "rmnet0", -1, -1, -1, -1); |
| 280 | expected = "221 wlan0 rmnet0 10002373 10026 20002002 20027\n"; |
Lorenzo Colitti | 750e8fc | 2016-07-12 01:19:49 +0900 | [diff] [blame] | 281 | ASSERT_EQ(0, mBw.getTetherStats(&cli, filter, err)); |
Lorenzo Colitti | bbeaf9a | 2016-07-08 18:24:26 +0900 | [diff] [blame] | 282 | ASSERT_EQ(expected, readSocketClientResponse(socketPair[1])); |
Lorenzo Colitti | ce6748a | 2017-02-02 01:34:33 +0900 | [diff] [blame] | 283 | clearIptablesRestoreOutput(); |
Lorenzo Colitti | bbeaf9a | 2016-07-08 18:24:26 +0900 | [diff] [blame] | 284 | |
Lorenzo Colitti | 26c9132 | 2016-07-11 11:36:25 +0900 | [diff] [blame] | 285 | // Select nonexistent interfaces. |
Lorenzo Colitti | ce6748a | 2017-02-02 01:34:33 +0900 | [diff] [blame] | 286 | addIptablesRestoreOutput(kIPv4TetherCounters, kIPv6TetherCounters); |
Lorenzo Colitti | bbeaf9a | 2016-07-08 18:24:26 +0900 | [diff] [blame] | 287 | filter = BandwidthController::TetherStats("rmnet0", "foo0", -1, -1, -1, -1); |
| 288 | expected = "200 Tethering stats list completed\n"; |
Lorenzo Colitti | 750e8fc | 2016-07-12 01:19:49 +0900 | [diff] [blame] | 289 | ASSERT_EQ(0, mBw.getTetherStats(&cli, filter, err)); |
Lorenzo Colitti | bbeaf9a | 2016-07-08 18:24:26 +0900 | [diff] [blame] | 290 | ASSERT_EQ(expected, readSocketClientResponse(socketPair[1])); |
Lorenzo Colitti | ce6748a | 2017-02-02 01:34:33 +0900 | [diff] [blame] | 291 | clearIptablesRestoreOutput(); |
Lorenzo Colitti | 750e8fc | 2016-07-12 01:19:49 +0900 | [diff] [blame] | 292 | |
| 293 | // No stats with a filter: no error. |
Lorenzo Colitti | ce6748a | 2017-02-02 01:34:33 +0900 | [diff] [blame] | 294 | addIptablesRestoreOutput("", ""); |
Lorenzo Colitti | 750e8fc | 2016-07-12 01:19:49 +0900 | [diff] [blame] | 295 | ASSERT_EQ(0, mBw.getTetherStats(&cli, filter, err)); |
| 296 | ASSERT_EQ("200 Tethering stats list completed\n", readSocketClientResponse(socketPair[1])); |
Lorenzo Colitti | ce6748a | 2017-02-02 01:34:33 +0900 | [diff] [blame] | 297 | clearIptablesRestoreOutput(); |
Lorenzo Colitti | 26c9132 | 2016-07-11 11:36:25 +0900 | [diff] [blame] | 298 | |
Lorenzo Colitti | ce6748a | 2017-02-02 01:34:33 +0900 | [diff] [blame] | 299 | addIptablesRestoreOutput("foo", "foo"); |
Lorenzo Colitti | 750e8fc | 2016-07-12 01:19:49 +0900 | [diff] [blame] | 300 | ASSERT_EQ(0, mBw.getTetherStats(&cli, filter, err)); |
| 301 | ASSERT_EQ("200 Tethering stats list completed\n", readSocketClientResponse(socketPair[1])); |
Lorenzo Colitti | ce6748a | 2017-02-02 01:34:33 +0900 | [diff] [blame] | 302 | clearIptablesRestoreOutput(); |
Lorenzo Colitti | 750e8fc | 2016-07-12 01:19:49 +0900 | [diff] [blame] | 303 | |
| 304 | // No stats and empty filter: error. |
| 305 | filter = BandwidthController::TetherStats(); |
Lorenzo Colitti | ce6748a | 2017-02-02 01:34:33 +0900 | [diff] [blame] | 306 | addIptablesRestoreOutput("", kIPv6TetherCounters); |
Lorenzo Colitti | 750e8fc | 2016-07-12 01:19:49 +0900 | [diff] [blame] | 307 | ASSERT_EQ(-1, mBw.getTetherStats(&cli, filter, err)); |
| 308 | expectNoSocketClientResponse(socketPair[1]); |
Lorenzo Colitti | ce6748a | 2017-02-02 01:34:33 +0900 | [diff] [blame] | 309 | clearIptablesRestoreOutput(); |
Lorenzo Colitti | 26c9132 | 2016-07-11 11:36:25 +0900 | [diff] [blame] | 310 | |
Lorenzo Colitti | ce6748a | 2017-02-02 01:34:33 +0900 | [diff] [blame] | 311 | addIptablesRestoreOutput(kIPv4TetherCounters, ""); |
Lorenzo Colitti | 26c9132 | 2016-07-11 11:36:25 +0900 | [diff] [blame] | 312 | ASSERT_EQ(-1, mBw.getTetherStats(&cli, filter, err)); |
| 313 | expectNoSocketClientResponse(socketPair[1]); |
Lorenzo Colitti | ce6748a | 2017-02-02 01:34:33 +0900 | [diff] [blame] | 314 | clearIptablesRestoreOutput(); |
Lorenzo Colitti | 750e8fc | 2016-07-12 01:19:49 +0900 | [diff] [blame] | 315 | |
| 316 | // Include only one pair of interfaces and things are fine. |
| 317 | std::vector<std::string> counterLines = android::base::Split(kIPv4TetherCounters, "\n"); |
| 318 | std::vector<std::string> brokenCounterLines = counterLines; |
| 319 | counterLines.resize(4); |
| 320 | std::string counters = android::base::Join(counterLines, "\n") + "\n"; |
Lorenzo Colitti | ce6748a | 2017-02-02 01:34:33 +0900 | [diff] [blame] | 321 | addIptablesRestoreOutput(counters, counters); |
Lorenzo Colitti | 750e8fc | 2016-07-12 01:19:49 +0900 | [diff] [blame] | 322 | expected = |
Lorenzo Colitti | 26c9132 | 2016-07-11 11:36:25 +0900 | [diff] [blame] | 323 | "114 wlan0 rmnet0 4746 52 4004 54\n" |
Lorenzo Colitti | 750e8fc | 2016-07-12 01:19:49 +0900 | [diff] [blame] | 324 | "200 Tethering stats list completed\n"; |
| 325 | ASSERT_EQ(0, mBw.getTetherStats(&cli, filter, err)); |
| 326 | ASSERT_EQ(expected, readSocketClientResponse(socketPair[1])); |
Lorenzo Colitti | ce6748a | 2017-02-02 01:34:33 +0900 | [diff] [blame] | 327 | clearIptablesRestoreOutput(); |
Lorenzo Colitti | 750e8fc | 2016-07-12 01:19:49 +0900 | [diff] [blame] | 328 | |
| 329 | // But if interfaces aren't paired, it's always an error. |
Lorenzo Colitti | ce6748a | 2017-02-02 01:34:33 +0900 | [diff] [blame] | 330 | err = ""; |
Lorenzo Colitti | 750e8fc | 2016-07-12 01:19:49 +0900 | [diff] [blame] | 331 | counterLines.resize(3); |
| 332 | counters = android::base::Join(counterLines, "\n") + "\n"; |
Lorenzo Colitti | ce6748a | 2017-02-02 01:34:33 +0900 | [diff] [blame] | 333 | addIptablesRestoreOutput(counters, counters); |
Lorenzo Colitti | 750e8fc | 2016-07-12 01:19:49 +0900 | [diff] [blame] | 334 | ASSERT_EQ(-1, mBw.getTetherStats(&cli, filter, err)); |
| 335 | expectNoSocketClientResponse(socketPair[1]); |
Lorenzo Colitti | ce6748a | 2017-02-02 01:34:33 +0900 | [diff] [blame] | 336 | clearIptablesRestoreOutput(); |
| 337 | |
| 338 | // Token unit test of the fact that we return the stats in the error message which the caller |
| 339 | // ignores. |
| 340 | std::string expectedError = counters; |
| 341 | EXPECT_EQ(expectedError, err); |
Lorenzo Colitti | 26c9132 | 2016-07-11 11:36:25 +0900 | [diff] [blame] | 342 | |
| 343 | // popen() failing is always an error. |
Lorenzo Colitti | ce6748a | 2017-02-02 01:34:33 +0900 | [diff] [blame] | 344 | addIptablesRestoreOutput(kIPv4TetherCounters); |
Lorenzo Colitti | 26c9132 | 2016-07-11 11:36:25 +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(); |
| 348 | addIptablesRestoreOutput(kIPv6TetherCounters); |
Lorenzo Colitti | 26c9132 | 2016-07-11 11:36:25 +0900 | [diff] [blame] | 349 | ASSERT_EQ(-1, mBw.getTetherStats(&cli, filter, err)); |
| 350 | expectNoSocketClientResponse(socketPair[1]); |
Lorenzo Colitti | ce6748a | 2017-02-02 01:34:33 +0900 | [diff] [blame] | 351 | clearIptablesRestoreOutput(); |
Lorenzo Colitti | bbeaf9a | 2016-07-08 18:24:26 +0900 | [diff] [blame] | 352 | } |
Lorenzo Colitti | df42ddd | 2017-02-28 01:20:13 +0900 | [diff] [blame] | 353 | |
| 354 | const std::vector<std::string> makeInterfaceQuotaCommands(const char *iface, int ruleIndex, |
| 355 | int64_t quota) { |
| 356 | std::vector<std::string> cmds = { |
| 357 | StringPrintf("-F bw_costly_%s", iface), |
| 358 | StringPrintf("-N bw_costly_%s", iface), |
| 359 | StringPrintf("-A bw_costly_%s -j bw_penalty_box", iface), |
| 360 | StringPrintf("-D bw_INPUT -i %s --jump bw_costly_%s", iface, iface), |
| 361 | StringPrintf("-I bw_INPUT %d -i %s --jump bw_costly_%s", ruleIndex, iface, iface), |
| 362 | StringPrintf("-D bw_OUTPUT -o %s --jump bw_costly_%s", iface, iface), |
| 363 | StringPrintf("-I bw_OUTPUT %d -o %s --jump bw_costly_%s", ruleIndex, iface, iface), |
| 364 | StringPrintf("-D bw_FORWARD -o %s --jump bw_costly_%s", iface, iface), |
| 365 | StringPrintf("-A bw_FORWARD -o %s --jump bw_costly_%s", iface, iface), |
| 366 | StringPrintf("-A bw_costly_%s -m quota2 ! --quota %" PRIu64 " --name %s --jump REJECT", |
| 367 | iface, quota, iface), |
| 368 | }; |
| 369 | return cmds; |
| 370 | } |
| 371 | |
| 372 | const std::vector<std::string> removeInterfaceQuotaCommands(const char *iface) { |
| 373 | std::vector<std::string> cmds = { |
| 374 | StringPrintf("-D bw_INPUT -i %s --jump bw_costly_%s", iface, iface), |
| 375 | StringPrintf("-D bw_OUTPUT -o %s --jump bw_costly_%s", iface, iface), |
| 376 | StringPrintf("-D bw_FORWARD -o %s --jump bw_costly_%s", iface, iface), |
| 377 | StringPrintf("-F bw_costly_%s", iface), |
| 378 | StringPrintf("-X bw_costly_%s", iface), |
| 379 | }; |
| 380 | return cmds; |
| 381 | } |
| 382 | |
| 383 | TEST_F(BandwidthControllerTest, TestSetInterfaceQuota) { |
| 384 | const char *iface = mTun.name().c_str(); |
| 385 | std::vector<std::string> expected = makeInterfaceQuotaCommands(iface, 1, 123456); |
| 386 | |
| 387 | // prepCostlyInterface assumes that exactly one of the "-F chain" and "-N chain" commands fails. |
| 388 | // So pretend that the first two commands (the IPv4 -F and the IPv6 -F) fail. |
| 389 | std::deque<int> returnValues(expected.size() * 2, 0); |
| 390 | returnValues[0] = 1; |
| 391 | returnValues[1] = 1; |
| 392 | setReturnValues(returnValues); |
| 393 | |
| 394 | EXPECT_EQ(0, mBw.setInterfaceQuota(iface, 123456)); |
| 395 | expectIptablesCommands(expected); |
| 396 | |
| 397 | expected = removeInterfaceQuotaCommands(iface); |
| 398 | EXPECT_EQ(0, mBw.removeInterfaceQuota(iface)); |
| 399 | expectIptablesCommands(expected); |
| 400 | } |
Lorenzo Colitti | e8b56e4 | 2017-04-26 15:16:03 +0900 | [diff] [blame] | 401 | |
| 402 | TEST_F(BandwidthControllerTest, IptablesAlertCmd) { |
| 403 | std::vector<std::string> expected = { |
Lorenzo Colitti | 3c27270 | 2017-04-26 15:48:13 +0900 | [diff] [blame] | 404 | "*filter\n" |
| 405 | "-I bw_INPUT -m quota2 ! --quota 123456 --name MyWonderfulAlert\n" |
| 406 | "-I bw_OUTPUT -m quota2 ! --quota 123456 --name MyWonderfulAlert\n" |
| 407 | "COMMIT\n" |
Lorenzo Colitti | e8b56e4 | 2017-04-26 15:16:03 +0900 | [diff] [blame] | 408 | }; |
| 409 | EXPECT_EQ(0, runIptablesAlertCmd(IptOp::IptOpInsert, "MyWonderfulAlert", 123456)); |
Lorenzo Colitti | 3c27270 | 2017-04-26 15:48:13 +0900 | [diff] [blame] | 410 | expectIptablesRestoreCommands(expected); |
Lorenzo Colitti | e8b56e4 | 2017-04-26 15:16:03 +0900 | [diff] [blame] | 411 | |
| 412 | expected = { |
Lorenzo Colitti | 3c27270 | 2017-04-26 15:48:13 +0900 | [diff] [blame] | 413 | "*filter\n" |
| 414 | "-D bw_INPUT -m quota2 ! --quota 123456 --name MyWonderfulAlert\n" |
| 415 | "-D bw_OUTPUT -m quota2 ! --quota 123456 --name MyWonderfulAlert\n" |
| 416 | "COMMIT\n" |
Lorenzo Colitti | e8b56e4 | 2017-04-26 15:16:03 +0900 | [diff] [blame] | 417 | }; |
| 418 | EXPECT_EQ(0, runIptablesAlertCmd(IptOp::IptOpDelete, "MyWonderfulAlert", 123456)); |
Lorenzo Colitti | 3c27270 | 2017-04-26 15:48:13 +0900 | [diff] [blame] | 419 | expectIptablesRestoreCommands(expected); |
Lorenzo Colitti | e8b56e4 | 2017-04-26 15:16:03 +0900 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | TEST_F(BandwidthControllerTest, IptablesAlertFwdCmd) { |
| 423 | std::vector<std::string> expected = { |
Lorenzo Colitti | 3c27270 | 2017-04-26 15:48:13 +0900 | [diff] [blame] | 424 | "*filter\n" |
| 425 | "-I bw_FORWARD -m quota2 ! --quota 123456 --name MyWonderfulAlert\n" |
| 426 | "COMMIT\n" |
Lorenzo Colitti | e8b56e4 | 2017-04-26 15:16:03 +0900 | [diff] [blame] | 427 | }; |
| 428 | EXPECT_EQ(0, runIptablesAlertFwdCmd(IptOp::IptOpInsert, "MyWonderfulAlert", 123456)); |
Lorenzo Colitti | 3c27270 | 2017-04-26 15:48:13 +0900 | [diff] [blame] | 429 | expectIptablesRestoreCommands(expected); |
Lorenzo Colitti | e8b56e4 | 2017-04-26 15:16:03 +0900 | [diff] [blame] | 430 | |
| 431 | expected = { |
Lorenzo Colitti | 3c27270 | 2017-04-26 15:48:13 +0900 | [diff] [blame] | 432 | "*filter\n" |
| 433 | "-D bw_FORWARD -m quota2 ! --quota 123456 --name MyWonderfulAlert\n" |
| 434 | "COMMIT\n" |
Lorenzo Colitti | e8b56e4 | 2017-04-26 15:16:03 +0900 | [diff] [blame] | 435 | }; |
| 436 | EXPECT_EQ(0, runIptablesAlertFwdCmd(IptOp::IptOpDelete, "MyWonderfulAlert", 123456)); |
Lorenzo Colitti | 3c27270 | 2017-04-26 15:48:13 +0900 | [diff] [blame] | 437 | expectIptablesRestoreCommands(expected); |
Lorenzo Colitti | e8b56e4 | 2017-04-26 15:16:03 +0900 | [diff] [blame] | 438 | } |
Lorenzo Colitti | f4dfa68 | 2017-04-28 11:09:07 +0900 | [diff] [blame^] | 439 | |
| 440 | TEST_F(BandwidthControllerTest, ManipulateSpecialApps) { |
| 441 | std::vector<const char *> appUids = { "1000", "1001", "10012" }; |
| 442 | |
| 443 | std::vector<std::string> expected = { |
| 444 | "-I bw_happy_box -m owner --uid-owner 1000 --jump RETURN", |
| 445 | "-I bw_happy_box -m owner --uid-owner 1001 --jump RETURN", |
| 446 | "-I bw_happy_box -m owner --uid-owner 10012 --jump RETURN", |
| 447 | }; |
| 448 | EXPECT_EQ(0, mBw.addNiceApps(appUids.size(), const_cast<char**>(&appUids[0]))); |
| 449 | expectIptablesCommands(expected); |
| 450 | |
| 451 | expected = { |
| 452 | "-D bw_penalty_box -m owner --uid-owner 1000 --jump REJECT", |
| 453 | "-D bw_penalty_box -m owner --uid-owner 1001 --jump REJECT", |
| 454 | "-D bw_penalty_box -m owner --uid-owner 10012 --jump REJECT", |
| 455 | }; |
| 456 | EXPECT_EQ(0, mBw.removeNaughtyApps(appUids.size(), const_cast<char**>(&appUids[0]))); |
| 457 | expectIptablesCommands(expected); |
| 458 | } |