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