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 | bbeaf9a | 2016-07-08 18:24:26 +0900 | [diff] [blame] | 22 | #include <fcntl.h> |
| 23 | #include <unistd.h> |
| 24 | #include <sys/types.h> |
| 25 | #include <sys/socket.h> |
| 26 | |
Lorenzo Colitti | 86a4798 | 2016-03-18 17:52:25 +0900 | [diff] [blame] | 27 | #include <gtest/gtest.h> |
| 28 | |
Lorenzo Colitti | 13debb8 | 2016-03-27 17:46:30 +0900 | [diff] [blame] | 29 | #include <android-base/strings.h> |
Lorenzo Colitti | 56c4b1e | 2017-02-01 02:45:10 +0900 | [diff] [blame] | 30 | #include <android-base/stringprintf.h> |
Lorenzo Colitti | 13debb8 | 2016-03-27 17:46:30 +0900 | [diff] [blame] | 31 | |
Lorenzo Colitti | 86a4798 | 2016-03-18 17:52:25 +0900 | [diff] [blame] | 32 | #include "BandwidthController.h" |
Lorenzo Colitti | 0f15055 | 2016-03-28 02:30:27 +0900 | [diff] [blame] | 33 | #include "IptablesBaseTest.h" |
Lorenzo Colitti | 86a4798 | 2016-03-18 17:52:25 +0900 | [diff] [blame] | 34 | |
Lorenzo Colitti | 0f15055 | 2016-03-28 02:30:27 +0900 | [diff] [blame] | 35 | class BandwidthControllerTest : public IptablesBaseTest { |
Lorenzo Colitti | 86a4798 | 2016-03-18 17:52:25 +0900 | [diff] [blame] | 36 | public: |
| 37 | BandwidthControllerTest() { |
| 38 | BandwidthController::execFunction = fake_android_fork_exec; |
| 39 | BandwidthController::popenFunction = fake_popen; |
Lorenzo Colitti | 56c4b1e | 2017-02-01 02:45:10 +0900 | [diff] [blame] | 40 | BandwidthController::iptablesRestoreFunction = fakeExecIptablesRestoreWithOutput; |
Lorenzo Colitti | 86a4798 | 2016-03-18 17:52:25 +0900 | [diff] [blame] | 41 | } |
| 42 | BandwidthController mBw; |
Lorenzo Colitti | bbeaf9a | 2016-07-08 18:24:26 +0900 | [diff] [blame] | 43 | |
Lorenzo Colitti | 56c4b1e | 2017-02-01 02:45:10 +0900 | [diff] [blame] | 44 | void addIptablesRestoreOutput(std::string contents) { |
| 45 | sIptablesRestoreOutput.push_back(contents); |
| 46 | } |
| 47 | |
Lorenzo Colitti | ce6748a | 2017-02-02 01:34:33 +0900 | [diff] [blame^] | 48 | void addIptablesRestoreOutput(std::string contents1, std::string contents2) { |
| 49 | sIptablesRestoreOutput.push_back(contents1); |
| 50 | sIptablesRestoreOutput.push_back(contents2); |
| 51 | } |
| 52 | |
Lorenzo Colitti | 56c4b1e | 2017-02-01 02:45:10 +0900 | [diff] [blame] | 53 | void clearIptablesRestoreOutput() { |
| 54 | sIptablesRestoreOutput.clear(); |
| 55 | } |
| 56 | |
| 57 | void expectSetupCommands(const std::string& expectedClean, std::string expectedAccounting) { |
| 58 | std::string expectedList = |
| 59 | "*filter\n" |
| 60 | "-S\n" |
| 61 | "COMMIT\n"; |
| 62 | |
| 63 | std::string expectedFlush = |
| 64 | "*filter\n" |
| 65 | ":bw_INPUT -\n" |
| 66 | ":bw_OUTPUT -\n" |
| 67 | ":bw_FORWARD -\n" |
| 68 | ":bw_happy_box -\n" |
| 69 | ":bw_penalty_box -\n" |
| 70 | ":bw_data_saver -\n" |
| 71 | ":bw_costly_shared -\n" |
| 72 | "COMMIT\n" |
| 73 | "*raw\n" |
| 74 | ":bw_raw_PREROUTING -\n" |
| 75 | "COMMIT\n" |
| 76 | "*mangle\n" |
| 77 | ":bw_mangle_POSTROUTING -\n" |
| 78 | "COMMIT\n"; |
| 79 | |
| 80 | ExpectedIptablesCommands expected = {{ V4, expectedList }}; |
| 81 | if (expectedClean.size()) { |
| 82 | expected.push_back({ V4V6, expectedClean }); |
| 83 | } |
| 84 | expected.push_back({ V4V6, expectedFlush }); |
| 85 | if (expectedAccounting.size()) { |
| 86 | expected.push_back({ V4V6, expectedAccounting }); |
| 87 | } |
| 88 | |
| 89 | expectIptablesRestoreCommands(expected); |
| 90 | } |
Lorenzo Colitti | 86a4798 | 2016-03-18 17:52:25 +0900 | [diff] [blame] | 91 | }; |
| 92 | |
Lorenzo Colitti | a0dc8a5 | 2016-03-26 22:42:07 +0900 | [diff] [blame] | 93 | TEST_F(BandwidthControllerTest, TestSetupIptablesHooks) { |
Lorenzo Colitti | 56c4b1e | 2017-02-01 02:45:10 +0900 | [diff] [blame] | 94 | // Pretend some bw_costly_shared_<iface> rules already exist... |
| 95 | addIptablesRestoreOutput( |
| 96 | "-P OUTPUT ACCEPT\n" |
| 97 | "-N bw_costly_rmnet_data0\n" |
| 98 | "-N bw_costly_shared\n" |
| 99 | "-N unrelated\n" |
| 100 | "-N bw_costly_rmnet_data7\n"); |
| 101 | |
| 102 | // ... and expect that they be flushed and deleted. |
| 103 | std::string expectedCleanCmds = |
Lorenzo Colitti | 13debb8 | 2016-03-27 17:46:30 +0900 | [diff] [blame] | 104 | "*filter\n" |
Lorenzo Colitti | 56c4b1e | 2017-02-01 02:45:10 +0900 | [diff] [blame] | 105 | ":bw_costly_rmnet_data0 -\n" |
| 106 | "-X bw_costly_rmnet_data0\n" |
| 107 | ":bw_costly_rmnet_data7 -\n" |
| 108 | "-X bw_costly_rmnet_data7\n" |
| 109 | "COMMIT\n"; |
| 110 | |
| 111 | mBw.setupIptablesHooks(); |
| 112 | expectSetupCommands(expectedCleanCmds, ""); |
Lorenzo Colitti | a0dc8a5 | 2016-03-26 22:42:07 +0900 | [diff] [blame] | 113 | } |
| 114 | |
Lorenzo Colitti | 86a4798 | 2016-03-18 17:52:25 +0900 | [diff] [blame] | 115 | TEST_F(BandwidthControllerTest, TestEnableBandwidthControl) { |
Lorenzo Colitti | 56c4b1e | 2017-02-01 02:45:10 +0900 | [diff] [blame] | 116 | // Pretend no bw_costly_shared_<iface> rules already exist... |
| 117 | addIptablesRestoreOutput( |
| 118 | "-P OUTPUT ACCEPT\n" |
| 119 | "-N bw_costly_shared\n" |
| 120 | "-N unrelated\n"); |
| 121 | |
| 122 | // ... so none are flushed or deleted. |
| 123 | std::string expectedClean = ""; |
| 124 | |
| 125 | std::string expectedAccounting = |
Lorenzo Colitti | 13debb8 | 2016-03-27 17:46:30 +0900 | [diff] [blame] | 126 | "*filter\n" |
| 127 | "-A bw_INPUT -m owner --socket-exists\n" |
| 128 | "-A bw_OUTPUT -m owner --socket-exists\n" |
| 129 | "-A bw_costly_shared --jump bw_penalty_box\n" |
| 130 | "-A bw_penalty_box --jump bw_happy_box\n" |
| 131 | "-A bw_happy_box --jump bw_data_saver\n" |
| 132 | "-A bw_data_saver -j RETURN\n" |
| 133 | "-I bw_happy_box -m owner --uid-owner 0-9999 --jump RETURN\n" |
| 134 | "COMMIT\n" |
| 135 | "*raw\n" |
| 136 | "-A bw_raw_PREROUTING -m owner --socket-exists\n" |
| 137 | "COMMIT\n" |
| 138 | "*mangle\n" |
| 139 | "-A bw_mangle_POSTROUTING -m owner --socket-exists\n" |
Lorenzo Colitti | 56c4b1e | 2017-02-01 02:45:10 +0900 | [diff] [blame] | 140 | "COMMIT\n"; |
Lorenzo Colitti | 13debb8 | 2016-03-27 17:46:30 +0900 | [diff] [blame] | 141 | |
Lorenzo Colitti | 56c4b1e | 2017-02-01 02:45:10 +0900 | [diff] [blame] | 142 | mBw.enableBandwidthControl(false); |
| 143 | expectSetupCommands(expectedClean, expectedAccounting); |
Lorenzo Colitti | 86a4798 | 2016-03-18 17:52:25 +0900 | [diff] [blame] | 144 | } |
| 145 | |
Lorenzo Colitti | a0dc8a5 | 2016-03-26 22:42:07 +0900 | [diff] [blame] | 146 | TEST_F(BandwidthControllerTest, TestDisableBandwidthControl) { |
Lorenzo Colitti | 56c4b1e | 2017-02-01 02:45:10 +0900 | [diff] [blame] | 147 | // Pretend some bw_costly_shared_<iface> rules already exist... |
| 148 | addIptablesRestoreOutput( |
| 149 | "-P OUTPUT ACCEPT\n" |
| 150 | "-N bw_costly_rmnet_data0\n" |
| 151 | "-N bw_costly_shared\n" |
| 152 | "-N unrelated\n" |
| 153 | "-N bw_costly_rmnet_data7\n"); |
| 154 | |
| 155 | // ... and expect that they be flushed. |
| 156 | std::string expectedCleanCmds = |
Lorenzo Colitti | 13debb8 | 2016-03-27 17:46:30 +0900 | [diff] [blame] | 157 | "*filter\n" |
Lorenzo Colitti | 56c4b1e | 2017-02-01 02:45:10 +0900 | [diff] [blame] | 158 | ":bw_costly_rmnet_data0 -\n" |
| 159 | ":bw_costly_rmnet_data7 -\n" |
| 160 | "COMMIT\n"; |
| 161 | |
| 162 | mBw.disableBandwidthControl(); |
| 163 | expectSetupCommands(expectedCleanCmds, ""); |
Lorenzo Colitti | a0dc8a5 | 2016-03-26 22:42:07 +0900 | [diff] [blame] | 164 | } |
| 165 | |
Lorenzo Colitti | 86a4798 | 2016-03-18 17:52:25 +0900 | [diff] [blame] | 166 | TEST_F(BandwidthControllerTest, TestEnableDataSaver) { |
| 167 | mBw.enableDataSaver(true); |
| 168 | std::vector<std::string> expected = { |
Lorenzo Colitti | 464eabe | 2016-03-25 13:38:19 +0900 | [diff] [blame] | 169 | "-R bw_data_saver 1 --jump REJECT", |
Lorenzo Colitti | 86a4798 | 2016-03-18 17:52:25 +0900 | [diff] [blame] | 170 | }; |
| 171 | expectIptablesCommands(expected); |
| 172 | |
| 173 | mBw.enableDataSaver(false); |
| 174 | expected = { |
Lorenzo Colitti | 464eabe | 2016-03-25 13:38:19 +0900 | [diff] [blame] | 175 | "-R bw_data_saver 1 --jump RETURN", |
Lorenzo Colitti | 86a4798 | 2016-03-18 17:52:25 +0900 | [diff] [blame] | 176 | }; |
| 177 | expectIptablesCommands(expected); |
| 178 | } |
Lorenzo Colitti | bbeaf9a | 2016-07-08 18:24:26 +0900 | [diff] [blame] | 179 | |
| 180 | std::string kIPv4TetherCounters = android::base::Join(std::vector<std::string> { |
| 181 | "Chain natctrl_tether_counters (4 references)", |
| 182 | " pkts bytes target prot opt in out source destination", |
| 183 | " 26 2373 RETURN all -- wlan0 rmnet0 0.0.0.0/0 0.0.0.0/0", |
| 184 | " 27 2002 RETURN all -- rmnet0 wlan0 0.0.0.0/0 0.0.0.0/0", |
| 185 | " 1040 107471 RETURN all -- bt-pan rmnet0 0.0.0.0/0 0.0.0.0/0", |
| 186 | " 1450 1708806 RETURN all -- rmnet0 bt-pan 0.0.0.0/0 0.0.0.0/0", |
| 187 | }, '\n'); |
| 188 | |
Lorenzo Colitti | 26c9132 | 2016-07-11 11:36:25 +0900 | [diff] [blame] | 189 | std::string kIPv6TetherCounters = android::base::Join(std::vector<std::string> { |
| 190 | "Chain natctrl_tether_counters (2 references)", |
| 191 | " pkts bytes target prot opt in out source destination", |
| 192 | " 10000 10000000 RETURN all wlan0 rmnet0 ::/0 ::/0", |
| 193 | " 20000 20000000 RETURN all rmnet0 wlan0 ::/0 ::/0", |
| 194 | }, '\n'); |
| 195 | |
Lorenzo Colitti | bbeaf9a | 2016-07-08 18:24:26 +0900 | [diff] [blame] | 196 | std::string readSocketClientResponse(int fd) { |
| 197 | char buf[32768]; |
| 198 | ssize_t bytesRead = read(fd, buf, sizeof(buf)); |
| 199 | if (bytesRead < 0) { |
| 200 | return ""; |
| 201 | } |
| 202 | for (int i = 0; i < bytesRead; i++) { |
| 203 | if (buf[i] == '\0') buf[i] = '\n'; |
| 204 | } |
| 205 | return std::string(buf, bytesRead); |
| 206 | } |
| 207 | |
Lorenzo Colitti | 750e8fc | 2016-07-12 01:19:49 +0900 | [diff] [blame] | 208 | void expectNoSocketClientResponse(int fd) { |
| 209 | char buf[64]; |
| 210 | EXPECT_EQ(-1, read(fd, buf, sizeof(buf))); |
| 211 | } |
| 212 | |
Lorenzo Colitti | bbeaf9a | 2016-07-08 18:24:26 +0900 | [diff] [blame] | 213 | TEST_F(BandwidthControllerTest, TestGetTetherStats) { |
| 214 | int socketPair[2]; |
| 215 | ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM, 0, socketPair)); |
| 216 | ASSERT_EQ(0, fcntl(socketPair[0], F_SETFL, O_NONBLOCK | fcntl(socketPair[0], F_GETFL))); |
| 217 | ASSERT_EQ(0, fcntl(socketPair[1], F_SETFL, O_NONBLOCK | fcntl(socketPair[1], F_GETFL))); |
| 218 | SocketClient cli(socketPair[0], false); |
| 219 | |
| 220 | std::string err; |
| 221 | BandwidthController::TetherStats filter; |
Lorenzo Colitti | 26c9132 | 2016-07-11 11:36:25 +0900 | [diff] [blame] | 222 | |
| 223 | // 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^] | 224 | addIptablesRestoreOutput(kIPv4TetherCounters); |
Lorenzo Colitti | 26c9132 | 2016-07-11 11:36:25 +0900 | [diff] [blame] | 225 | ASSERT_EQ(-1, mBw.getTetherStats(&cli, filter, err)); |
| 226 | expectNoSocketClientResponse(socketPair[1]); |
Lorenzo Colitti | ce6748a | 2017-02-02 01:34:33 +0900 | [diff] [blame^] | 227 | clearIptablesRestoreOutput(); |
Lorenzo Colitti | 26c9132 | 2016-07-11 11:36:25 +0900 | [diff] [blame] | 228 | |
Lorenzo Colitti | ce6748a | 2017-02-02 01:34:33 +0900 | [diff] [blame^] | 229 | addIptablesRestoreOutput(kIPv6TetherCounters); |
Lorenzo Colitti | 26c9132 | 2016-07-11 11:36:25 +0900 | [diff] [blame] | 230 | ASSERT_EQ(-1, mBw.getTetherStats(&cli, filter, err)); |
Lorenzo Colitti | ce6748a | 2017-02-02 01:34:33 +0900 | [diff] [blame^] | 231 | clearIptablesRestoreOutput(); |
Lorenzo Colitti | 26c9132 | 2016-07-11 11:36:25 +0900 | [diff] [blame] | 232 | |
| 233 | // IPv4 and IPv6 counters are properly added together. |
Lorenzo Colitti | ce6748a | 2017-02-02 01:34:33 +0900 | [diff] [blame^] | 234 | addIptablesRestoreOutput(kIPv4TetherCounters, kIPv6TetherCounters); |
Lorenzo Colitti | 26c9132 | 2016-07-11 11:36:25 +0900 | [diff] [blame] | 235 | filter = BandwidthController::TetherStats(); |
Lorenzo Colitti | bbeaf9a | 2016-07-08 18:24:26 +0900 | [diff] [blame] | 236 | std::string expected = |
Lorenzo Colitti | 26c9132 | 2016-07-11 11:36:25 +0900 | [diff] [blame] | 237 | "114 wlan0 rmnet0 10002373 10026 20002002 20027\n" |
Lorenzo Colitti | bbeaf9a | 2016-07-08 18:24:26 +0900 | [diff] [blame] | 238 | "114 bt-pan rmnet0 107471 1040 1708806 1450\n" |
| 239 | "200 Tethering stats list completed\n"; |
Lorenzo Colitti | 750e8fc | 2016-07-12 01:19:49 +0900 | [diff] [blame] | 240 | ASSERT_EQ(0, mBw.getTetherStats(&cli, filter, err)); |
Lorenzo Colitti | bbeaf9a | 2016-07-08 18:24:26 +0900 | [diff] [blame] | 241 | ASSERT_EQ(expected, readSocketClientResponse(socketPair[1])); |
Lorenzo Colitti | 26c9132 | 2016-07-11 11:36:25 +0900 | [diff] [blame] | 242 | expectNoSocketClientResponse(socketPair[1]); |
Lorenzo Colitti | ce6748a | 2017-02-02 01:34:33 +0900 | [diff] [blame^] | 243 | clearIptablesRestoreOutput(); |
Lorenzo Colitti | bbeaf9a | 2016-07-08 18:24:26 +0900 | [diff] [blame] | 244 | |
Lorenzo Colitti | 26c9132 | 2016-07-11 11:36:25 +0900 | [diff] [blame] | 245 | // Test filtering. |
Lorenzo Colitti | ce6748a | 2017-02-02 01:34:33 +0900 | [diff] [blame^] | 246 | addIptablesRestoreOutput(kIPv4TetherCounters, kIPv6TetherCounters); |
Lorenzo Colitti | bbeaf9a | 2016-07-08 18:24:26 +0900 | [diff] [blame] | 247 | filter = BandwidthController::TetherStats("bt-pan", "rmnet0", -1, -1, -1, -1); |
| 248 | expected = "221 bt-pan rmnet0 107471 1040 1708806 1450\n"; |
Lorenzo Colitti | 750e8fc | 2016-07-12 01:19:49 +0900 | [diff] [blame] | 249 | ASSERT_EQ(0, mBw.getTetherStats(&cli, filter, err)); |
Lorenzo Colitti | bbeaf9a | 2016-07-08 18:24:26 +0900 | [diff] [blame] | 250 | ASSERT_EQ(expected, readSocketClientResponse(socketPair[1])); |
Lorenzo Colitti | 26c9132 | 2016-07-11 11:36:25 +0900 | [diff] [blame] | 251 | expectNoSocketClientResponse(socketPair[1]); |
Lorenzo Colitti | ce6748a | 2017-02-02 01:34:33 +0900 | [diff] [blame^] | 252 | clearIptablesRestoreOutput(); |
Lorenzo Colitti | bbeaf9a | 2016-07-08 18:24:26 +0900 | [diff] [blame] | 253 | |
Lorenzo Colitti | ce6748a | 2017-02-02 01:34:33 +0900 | [diff] [blame^] | 254 | addIptablesRestoreOutput(kIPv4TetherCounters, kIPv6TetherCounters); |
Lorenzo Colitti | 26c9132 | 2016-07-11 11:36:25 +0900 | [diff] [blame] | 255 | filter = BandwidthController::TetherStats("wlan0", "rmnet0", -1, -1, -1, -1); |
| 256 | expected = "221 wlan0 rmnet0 10002373 10026 20002002 20027\n"; |
Lorenzo Colitti | 750e8fc | 2016-07-12 01:19:49 +0900 | [diff] [blame] | 257 | ASSERT_EQ(0, mBw.getTetherStats(&cli, filter, err)); |
Lorenzo Colitti | bbeaf9a | 2016-07-08 18:24:26 +0900 | [diff] [blame] | 258 | ASSERT_EQ(expected, readSocketClientResponse(socketPair[1])); |
Lorenzo Colitti | ce6748a | 2017-02-02 01:34:33 +0900 | [diff] [blame^] | 259 | clearIptablesRestoreOutput(); |
Lorenzo Colitti | bbeaf9a | 2016-07-08 18:24:26 +0900 | [diff] [blame] | 260 | |
Lorenzo Colitti | 26c9132 | 2016-07-11 11:36:25 +0900 | [diff] [blame] | 261 | // Select nonexistent interfaces. |
Lorenzo Colitti | ce6748a | 2017-02-02 01:34:33 +0900 | [diff] [blame^] | 262 | addIptablesRestoreOutput(kIPv4TetherCounters, kIPv6TetherCounters); |
Lorenzo Colitti | bbeaf9a | 2016-07-08 18:24:26 +0900 | [diff] [blame] | 263 | filter = BandwidthController::TetherStats("rmnet0", "foo0", -1, -1, -1, -1); |
| 264 | expected = "200 Tethering stats list completed\n"; |
Lorenzo Colitti | 750e8fc | 2016-07-12 01:19:49 +0900 | [diff] [blame] | 265 | ASSERT_EQ(0, mBw.getTetherStats(&cli, filter, err)); |
Lorenzo Colitti | bbeaf9a | 2016-07-08 18:24:26 +0900 | [diff] [blame] | 266 | ASSERT_EQ(expected, readSocketClientResponse(socketPair[1])); |
Lorenzo Colitti | ce6748a | 2017-02-02 01:34:33 +0900 | [diff] [blame^] | 267 | clearIptablesRestoreOutput(); |
Lorenzo Colitti | 750e8fc | 2016-07-12 01:19:49 +0900 | [diff] [blame] | 268 | |
| 269 | // No stats with a filter: no error. |
Lorenzo Colitti | ce6748a | 2017-02-02 01:34:33 +0900 | [diff] [blame^] | 270 | addIptablesRestoreOutput("", ""); |
Lorenzo Colitti | 750e8fc | 2016-07-12 01:19:49 +0900 | [diff] [blame] | 271 | ASSERT_EQ(0, mBw.getTetherStats(&cli, filter, err)); |
| 272 | ASSERT_EQ("200 Tethering stats list completed\n", readSocketClientResponse(socketPair[1])); |
Lorenzo Colitti | ce6748a | 2017-02-02 01:34:33 +0900 | [diff] [blame^] | 273 | clearIptablesRestoreOutput(); |
Lorenzo Colitti | 26c9132 | 2016-07-11 11:36:25 +0900 | [diff] [blame] | 274 | |
Lorenzo Colitti | ce6748a | 2017-02-02 01:34:33 +0900 | [diff] [blame^] | 275 | addIptablesRestoreOutput("foo", "foo"); |
Lorenzo Colitti | 750e8fc | 2016-07-12 01:19:49 +0900 | [diff] [blame] | 276 | ASSERT_EQ(0, mBw.getTetherStats(&cli, filter, err)); |
| 277 | ASSERT_EQ("200 Tethering stats list completed\n", readSocketClientResponse(socketPair[1])); |
Lorenzo Colitti | ce6748a | 2017-02-02 01:34:33 +0900 | [diff] [blame^] | 278 | clearIptablesRestoreOutput(); |
Lorenzo Colitti | 750e8fc | 2016-07-12 01:19:49 +0900 | [diff] [blame] | 279 | |
| 280 | // No stats and empty filter: error. |
| 281 | filter = BandwidthController::TetherStats(); |
Lorenzo Colitti | ce6748a | 2017-02-02 01:34:33 +0900 | [diff] [blame^] | 282 | addIptablesRestoreOutput("", kIPv6TetherCounters); |
Lorenzo Colitti | 750e8fc | 2016-07-12 01:19:49 +0900 | [diff] [blame] | 283 | ASSERT_EQ(-1, mBw.getTetherStats(&cli, filter, err)); |
| 284 | expectNoSocketClientResponse(socketPair[1]); |
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 | |
Lorenzo Colitti | ce6748a | 2017-02-02 01:34:33 +0900 | [diff] [blame^] | 287 | addIptablesRestoreOutput(kIPv4TetherCounters, ""); |
Lorenzo Colitti | 26c9132 | 2016-07-11 11:36:25 +0900 | [diff] [blame] | 288 | ASSERT_EQ(-1, mBw.getTetherStats(&cli, filter, err)); |
| 289 | expectNoSocketClientResponse(socketPair[1]); |
Lorenzo Colitti | ce6748a | 2017-02-02 01:34:33 +0900 | [diff] [blame^] | 290 | clearIptablesRestoreOutput(); |
Lorenzo Colitti | 750e8fc | 2016-07-12 01:19:49 +0900 | [diff] [blame] | 291 | |
| 292 | // Include only one pair of interfaces and things are fine. |
| 293 | std::vector<std::string> counterLines = android::base::Split(kIPv4TetherCounters, "\n"); |
| 294 | std::vector<std::string> brokenCounterLines = counterLines; |
| 295 | counterLines.resize(4); |
| 296 | std::string counters = android::base::Join(counterLines, "\n") + "\n"; |
Lorenzo Colitti | ce6748a | 2017-02-02 01:34:33 +0900 | [diff] [blame^] | 297 | addIptablesRestoreOutput(counters, counters); |
Lorenzo Colitti | 750e8fc | 2016-07-12 01:19:49 +0900 | [diff] [blame] | 298 | expected = |
Lorenzo Colitti | 26c9132 | 2016-07-11 11:36:25 +0900 | [diff] [blame] | 299 | "114 wlan0 rmnet0 4746 52 4004 54\n" |
Lorenzo Colitti | 750e8fc | 2016-07-12 01:19:49 +0900 | [diff] [blame] | 300 | "200 Tethering stats list completed\n"; |
| 301 | ASSERT_EQ(0, mBw.getTetherStats(&cli, filter, err)); |
| 302 | ASSERT_EQ(expected, readSocketClientResponse(socketPair[1])); |
Lorenzo Colitti | ce6748a | 2017-02-02 01:34:33 +0900 | [diff] [blame^] | 303 | clearIptablesRestoreOutput(); |
Lorenzo Colitti | 750e8fc | 2016-07-12 01:19:49 +0900 | [diff] [blame] | 304 | |
| 305 | // But if interfaces aren't paired, it's always an error. |
Lorenzo Colitti | ce6748a | 2017-02-02 01:34:33 +0900 | [diff] [blame^] | 306 | err = ""; |
Lorenzo Colitti | 750e8fc | 2016-07-12 01:19:49 +0900 | [diff] [blame] | 307 | counterLines.resize(3); |
| 308 | counters = android::base::Join(counterLines, "\n") + "\n"; |
Lorenzo Colitti | ce6748a | 2017-02-02 01:34:33 +0900 | [diff] [blame^] | 309 | addIptablesRestoreOutput(counters, counters); |
Lorenzo Colitti | 750e8fc | 2016-07-12 01:19:49 +0900 | [diff] [blame] | 310 | ASSERT_EQ(-1, mBw.getTetherStats(&cli, filter, err)); |
| 311 | expectNoSocketClientResponse(socketPair[1]); |
Lorenzo Colitti | ce6748a | 2017-02-02 01:34:33 +0900 | [diff] [blame^] | 312 | clearIptablesRestoreOutput(); |
| 313 | |
| 314 | // Token unit test of the fact that we return the stats in the error message which the caller |
| 315 | // ignores. |
| 316 | std::string expectedError = counters; |
| 317 | EXPECT_EQ(expectedError, err); |
Lorenzo Colitti | 26c9132 | 2016-07-11 11:36:25 +0900 | [diff] [blame] | 318 | |
| 319 | // popen() failing is always an error. |
Lorenzo Colitti | ce6748a | 2017-02-02 01:34:33 +0900 | [diff] [blame^] | 320 | addIptablesRestoreOutput(kIPv4TetherCounters); |
Lorenzo Colitti | 26c9132 | 2016-07-11 11:36:25 +0900 | [diff] [blame] | 321 | ASSERT_EQ(-1, mBw.getTetherStats(&cli, filter, err)); |
| 322 | expectNoSocketClientResponse(socketPair[1]); |
Lorenzo Colitti | ce6748a | 2017-02-02 01:34:33 +0900 | [diff] [blame^] | 323 | clearIptablesRestoreOutput(); |
| 324 | addIptablesRestoreOutput(kIPv6TetherCounters); |
Lorenzo Colitti | 26c9132 | 2016-07-11 11:36:25 +0900 | [diff] [blame] | 325 | ASSERT_EQ(-1, mBw.getTetherStats(&cli, filter, err)); |
| 326 | expectNoSocketClientResponse(socketPair[1]); |
Lorenzo Colitti | ce6748a | 2017-02-02 01:34:33 +0900 | [diff] [blame^] | 327 | clearIptablesRestoreOutput(); |
Lorenzo Colitti | bbeaf9a | 2016-07-08 18:24:26 +0900 | [diff] [blame] | 328 | } |