Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +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 | * TetherControllerTest.cpp - unit tests for TetherController.cpp |
| 17 | */ |
| 18 | |
| 19 | #include <string> |
| 20 | #include <vector> |
| 21 | |
| 22 | #include <fcntl.h> |
| 23 | #include <unistd.h> |
| 24 | #include <sys/types.h> |
| 25 | #include <sys/socket.h> |
| 26 | |
| 27 | #include <gtest/gtest.h> |
| 28 | |
| 29 | #include <android-base/stringprintf.h> |
| 30 | #include <android-base/strings.h> |
Lorenzo Colitti | 5192bf7 | 2017-09-04 13:30:59 +0900 | [diff] [blame] | 31 | #include <netdutils/StatusOr.h> |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 32 | |
| 33 | #include "TetherController.h" |
| 34 | #include "IptablesBaseTest.h" |
| 35 | |
| 36 | using android::base::Join; |
| 37 | using android::base::StringPrintf; |
Lorenzo Colitti | 5192bf7 | 2017-09-04 13:30:59 +0900 | [diff] [blame] | 38 | using android::netdutils::StatusOr; |
| 39 | using TetherStats = android::net::TetherController::TetherStats; |
| 40 | using TetherStatsList = android::net::TetherController::TetherStatsList; |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 41 | |
| 42 | namespace android { |
| 43 | namespace net { |
| 44 | |
| 45 | class TetherControllerTest : public IptablesBaseTest { |
| 46 | public: |
| 47 | TetherControllerTest() { |
| 48 | TetherController::iptablesRestoreFunction = fakeExecIptablesRestoreWithOutput; |
| 49 | } |
| 50 | |
| 51 | protected: |
| 52 | TetherController mTetherCtrl; |
| 53 | |
| 54 | int setDefaults() { |
| 55 | return mTetherCtrl.setDefaults(); |
| 56 | } |
| 57 | |
| 58 | const ExpectedIptablesCommands FLUSH_COMMANDS = { |
| 59 | { V4, "*filter\n" |
Lorenzo Colitti | 4604b4a | 2017-08-24 19:21:50 +0900 | [diff] [blame] | 60 | ":tetherctrl_FORWARD -\n" |
| 61 | "-A tetherctrl_FORWARD -j DROP\n" |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 62 | "COMMIT\n" |
| 63 | "*nat\n" |
Lorenzo Colitti | 4604b4a | 2017-08-24 19:21:50 +0900 | [diff] [blame] | 64 | ":tetherctrl_nat_POSTROUTING -\n" |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 65 | "COMMIT\n" }, |
| 66 | { V6, "*filter\n" |
Lorenzo Colitti | 4604b4a | 2017-08-24 19:21:50 +0900 | [diff] [blame] | 67 | ":tetherctrl_FORWARD -\n" |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 68 | "COMMIT\n" |
| 69 | "*raw\n" |
Lorenzo Colitti | 4604b4a | 2017-08-24 19:21:50 +0900 | [diff] [blame] | 70 | ":tetherctrl_raw_PREROUTING -\n" |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 71 | "COMMIT\n" }, |
| 72 | }; |
| 73 | |
| 74 | const ExpectedIptablesCommands SETUP_COMMANDS = { |
| 75 | { V4, "*filter\n" |
Lorenzo Colitti | 4604b4a | 2017-08-24 19:21:50 +0900 | [diff] [blame] | 76 | ":tetherctrl_FORWARD -\n" |
| 77 | "-A tetherctrl_FORWARD -j DROP\n" |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 78 | "COMMIT\n" |
| 79 | "*nat\n" |
Lorenzo Colitti | 4604b4a | 2017-08-24 19:21:50 +0900 | [diff] [blame] | 80 | ":tetherctrl_nat_POSTROUTING -\n" |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 81 | "COMMIT\n" }, |
| 82 | { V6, "*filter\n" |
Lorenzo Colitti | 4604b4a | 2017-08-24 19:21:50 +0900 | [diff] [blame] | 83 | ":tetherctrl_FORWARD -\n" |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 84 | "COMMIT\n" |
| 85 | "*raw\n" |
Lorenzo Colitti | 4604b4a | 2017-08-24 19:21:50 +0900 | [diff] [blame] | 86 | ":tetherctrl_raw_PREROUTING -\n" |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 87 | "COMMIT\n" }, |
| 88 | { V4, "*mangle\n" |
Lorenzo Colitti | 4604b4a | 2017-08-24 19:21:50 +0900 | [diff] [blame] | 89 | "-A tetherctrl_mangle_FORWARD -p tcp --tcp-flags SYN SYN " |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 90 | "-j TCPMSS --clamp-mss-to-pmtu\n" |
| 91 | "COMMIT\n" }, |
| 92 | { V4V6, "*filter\n" |
Lorenzo Colitti | 4604b4a | 2017-08-24 19:21:50 +0900 | [diff] [blame] | 93 | ":tetherctrl_counters -\n" |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 94 | "COMMIT\n" }, |
| 95 | }; |
| 96 | |
Remi NGUYEN VAN | 70a269a | 2018-03-19 11:24:10 +0900 | [diff] [blame] | 97 | ExpectedIptablesCommands firstIPv4UpstreamCommands(const char *extIf) { |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 98 | std::string v4Cmd = StringPrintf( |
| 99 | "*nat\n" |
Lorenzo Colitti | 4604b4a | 2017-08-24 19:21:50 +0900 | [diff] [blame] | 100 | "-A tetherctrl_nat_POSTROUTING -o %s -j MASQUERADE\n" |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 101 | "COMMIT\n", extIf); |
Remi NGUYEN VAN | 70a269a | 2018-03-19 11:24:10 +0900 | [diff] [blame] | 102 | return { |
| 103 | { V4, v4Cmd }, |
| 104 | }; |
| 105 | } |
| 106 | |
| 107 | ExpectedIptablesCommands firstIPv6UpstreamCommands() { |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 108 | std::string v6Cmd = |
| 109 | "*filter\n" |
Lorenzo Colitti | 4604b4a | 2017-08-24 19:21:50 +0900 | [diff] [blame] | 110 | "-A tetherctrl_FORWARD -g tetherctrl_counters\n" |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 111 | "COMMIT\n"; |
| 112 | return { |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 113 | { V6, v6Cmd }, |
| 114 | }; |
| 115 | } |
| 116 | |
Remi NGUYEN VAN | 70a269a | 2018-03-19 11:24:10 +0900 | [diff] [blame] | 117 | template<typename T> |
| 118 | void appendAll(std::vector<T>& cmds, const std::vector<T>& appendCmds) { |
| 119 | for (auto& cmd : appendCmds) { |
| 120 | cmds.push_back(cmd); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | ExpectedIptablesCommands startNatCommands(const char *intIf, const char *extIf, |
| 125 | bool withCounterChainRules) { |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 126 | std::string rpfilterCmd = StringPrintf( |
| 127 | "*raw\n" |
Lorenzo Colitti | 4604b4a | 2017-08-24 19:21:50 +0900 | [diff] [blame] | 128 | "-A tetherctrl_raw_PREROUTING -i %s -m rpfilter --invert ! -s fe80::/64 -j DROP\n" |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 129 | "COMMIT\n", intIf); |
| 130 | |
| 131 | std::vector<std::string> v4Cmds = { |
| 132 | "*filter", |
Lorenzo Colitti | 4604b4a | 2017-08-24 19:21:50 +0900 | [diff] [blame] | 133 | StringPrintf("-A tetherctrl_FORWARD -i %s -o %s -m state --state" |
| 134 | " ESTABLISHED,RELATED -g tetherctrl_counters", extIf, intIf), |
| 135 | StringPrintf("-A tetherctrl_FORWARD -i %s -o %s -m state --state INVALID -j DROP", |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 136 | intIf, extIf), |
Lorenzo Colitti | 4604b4a | 2017-08-24 19:21:50 +0900 | [diff] [blame] | 137 | StringPrintf("-A tetherctrl_FORWARD -i %s -o %s -g tetherctrl_counters", |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 138 | intIf, extIf), |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 139 | }; |
| 140 | |
| 141 | std::vector<std::string> v6Cmds = { |
| 142 | "*filter", |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 143 | }; |
| 144 | |
Remi NGUYEN VAN | 70a269a | 2018-03-19 11:24:10 +0900 | [diff] [blame] | 145 | if (withCounterChainRules) { |
| 146 | const std::vector<std::string> counterRules = { |
| 147 | StringPrintf("-A tetherctrl_counters -i %s -o %s -j RETURN", intIf, extIf), |
| 148 | StringPrintf("-A tetherctrl_counters -i %s -o %s -j RETURN", extIf, intIf), |
| 149 | }; |
| 150 | |
| 151 | appendAll(v4Cmds, counterRules); |
| 152 | appendAll(v6Cmds, counterRules); |
| 153 | } |
| 154 | |
| 155 | appendAll(v4Cmds, { |
| 156 | "-D tetherctrl_FORWARD -j DROP", |
| 157 | "-A tetherctrl_FORWARD -j DROP", |
| 158 | "COMMIT\n", |
| 159 | }); |
| 160 | |
| 161 | v6Cmds.push_back("COMMIT\n"); |
| 162 | |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 163 | return { |
| 164 | { V6, rpfilterCmd }, |
| 165 | { V4, Join(v4Cmds, '\n') }, |
| 166 | { V6, Join(v6Cmds, '\n') }, |
| 167 | }; |
| 168 | } |
| 169 | |
| 170 | ExpectedIptablesCommands stopNatCommands(const char *intIf, const char *extIf) { |
| 171 | std::string rpfilterCmd = StringPrintf( |
| 172 | "*raw\n" |
Lorenzo Colitti | 4604b4a | 2017-08-24 19:21:50 +0900 | [diff] [blame] | 173 | "-D tetherctrl_raw_PREROUTING -i %s -m rpfilter --invert ! -s fe80::/64 -j DROP\n" |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 174 | "COMMIT\n", intIf); |
| 175 | |
| 176 | std::vector<std::string> v4Cmds = { |
| 177 | "*filter", |
Lorenzo Colitti | 4604b4a | 2017-08-24 19:21:50 +0900 | [diff] [blame] | 178 | StringPrintf("-D tetherctrl_FORWARD -i %s -o %s -m state --state" |
| 179 | " ESTABLISHED,RELATED -g tetherctrl_counters", extIf, intIf), |
| 180 | StringPrintf("-D tetherctrl_FORWARD -i %s -o %s -m state --state INVALID -j DROP", |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 181 | intIf, extIf), |
Lorenzo Colitti | 4604b4a | 2017-08-24 19:21:50 +0900 | [diff] [blame] | 182 | StringPrintf("-D tetherctrl_FORWARD -i %s -o %s -g tetherctrl_counters", |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 183 | intIf, extIf), |
| 184 | "COMMIT\n", |
| 185 | }; |
| 186 | |
| 187 | return { |
| 188 | { V6, rpfilterCmd }, |
| 189 | { V4, Join(v4Cmds, '\n') }, |
| 190 | }; |
| 191 | |
| 192 | } |
| 193 | }; |
| 194 | |
| 195 | TEST_F(TetherControllerTest, TestSetupIptablesHooks) { |
| 196 | mTetherCtrl.setupIptablesHooks(); |
| 197 | expectIptablesRestoreCommands(SETUP_COMMANDS); |
| 198 | } |
| 199 | |
| 200 | TEST_F(TetherControllerTest, TestSetDefaults) { |
| 201 | setDefaults(); |
| 202 | expectIptablesRestoreCommands(FLUSH_COMMANDS); |
| 203 | } |
| 204 | |
| 205 | TEST_F(TetherControllerTest, TestAddAndRemoveNat) { |
Remi NGUYEN VAN | 70a269a | 2018-03-19 11:24:10 +0900 | [diff] [blame] | 206 | // Start first NAT on first upstream interface. Expect the upstream and NAT rules to be created. |
| 207 | ExpectedIptablesCommands firstNat; |
| 208 | ExpectedIptablesCommands setupFirstIPv4Commands = firstIPv4UpstreamCommands("rmnet0"); |
| 209 | ExpectedIptablesCommands setupFirstIPv6Commands = firstIPv6UpstreamCommands(); |
| 210 | ExpectedIptablesCommands startFirstNatCommands = startNatCommands("wlan0", "rmnet0", true); |
| 211 | appendAll(firstNat, setupFirstIPv4Commands); |
| 212 | appendAll(firstNat, setupFirstIPv6Commands); |
| 213 | appendAll(firstNat, startFirstNatCommands); |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 214 | mTetherCtrl.enableNat("wlan0", "rmnet0"); |
Remi NGUYEN VAN | 70a269a | 2018-03-19 11:24:10 +0900 | [diff] [blame] | 215 | expectIptablesRestoreCommands(firstNat); |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 216 | |
Remi NGUYEN VAN | 70a269a | 2018-03-19 11:24:10 +0900 | [diff] [blame] | 217 | // Start second NAT on same upstream. Expect only the counter rules to be created. |
| 218 | ExpectedIptablesCommands startOtherNatOnSameUpstream = startNatCommands("usb0", "rmnet0", true); |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 219 | mTetherCtrl.enableNat("usb0", "rmnet0"); |
Remi NGUYEN VAN | 70a269a | 2018-03-19 11:24:10 +0900 | [diff] [blame] | 220 | expectIptablesRestoreCommands(startOtherNatOnSameUpstream); |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 221 | |
Remi NGUYEN VAN | 70a269a | 2018-03-19 11:24:10 +0900 | [diff] [blame] | 222 | // Remove the first NAT. |
| 223 | ExpectedIptablesCommands stopFirstNat = stopNatCommands("wlan0", "rmnet0"); |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 224 | mTetherCtrl.disableNat("wlan0", "rmnet0"); |
Remi NGUYEN VAN | 70a269a | 2018-03-19 11:24:10 +0900 | [diff] [blame] | 225 | expectIptablesRestoreCommands(stopFirstNat); |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 226 | |
Remi NGUYEN VAN | 70a269a | 2018-03-19 11:24:10 +0900 | [diff] [blame] | 227 | // Remove the last NAT. Expect rules to be cleared. |
| 228 | ExpectedIptablesCommands stopLastNat = stopNatCommands("usb0", "rmnet0"); |
| 229 | |
| 230 | appendAll(stopLastNat, FLUSH_COMMANDS); |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 231 | mTetherCtrl.disableNat("usb0", "rmnet0"); |
Remi NGUYEN VAN | 70a269a | 2018-03-19 11:24:10 +0900 | [diff] [blame] | 232 | expectIptablesRestoreCommands(stopLastNat); |
| 233 | |
| 234 | // Re-add a NAT removed previously |
| 235 | firstNat = {}; |
| 236 | // tetherctrl_counters chain rules are not re-added |
| 237 | startFirstNatCommands = startNatCommands("wlan0", "rmnet0", false); |
| 238 | appendAll(firstNat, setupFirstIPv4Commands); |
| 239 | appendAll(firstNat, setupFirstIPv6Commands); |
| 240 | appendAll(firstNat, startFirstNatCommands); |
| 241 | mTetherCtrl.enableNat("wlan0", "rmnet0"); |
| 242 | expectIptablesRestoreCommands(firstNat); |
| 243 | |
| 244 | // Remove it again. Expect rules to be cleared. |
| 245 | stopLastNat = stopNatCommands("wlan0", "rmnet0"); |
| 246 | appendAll(stopLastNat, FLUSH_COMMANDS); |
| 247 | mTetherCtrl.disableNat("wlan0", "rmnet0"); |
| 248 | expectIptablesRestoreCommands(stopLastNat); |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 249 | } |
| 250 | |
Lorenzo Colitti | 0935339 | 2017-08-24 14:20:32 +0900 | [diff] [blame] | 251 | std::string kTetherCounterHeaders = Join(std::vector<std::string> { |
Lorenzo Colitti | 4604b4a | 2017-08-24 19:21:50 +0900 | [diff] [blame] | 252 | "Chain tetherctrl_counters (4 references)", |
Lorenzo Colitti | 0935339 | 2017-08-24 14:20:32 +0900 | [diff] [blame] | 253 | " pkts bytes target prot opt in out source destination", |
| 254 | }, '\n'); |
| 255 | |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 256 | std::string kIPv4TetherCounters = Join(std::vector<std::string> { |
Lorenzo Colitti | 4604b4a | 2017-08-24 19:21:50 +0900 | [diff] [blame] | 257 | "Chain tetherctrl_counters (4 references)", |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 258 | " pkts bytes target prot opt in out source destination", |
| 259 | " 26 2373 RETURN all -- wlan0 rmnet0 0.0.0.0/0 0.0.0.0/0", |
| 260 | " 27 2002 RETURN all -- rmnet0 wlan0 0.0.0.0/0 0.0.0.0/0", |
| 261 | " 1040 107471 RETURN all -- bt-pan rmnet0 0.0.0.0/0 0.0.0.0/0", |
| 262 | " 1450 1708806 RETURN all -- rmnet0 bt-pan 0.0.0.0/0 0.0.0.0/0", |
| 263 | }, '\n'); |
| 264 | |
| 265 | std::string kIPv6TetherCounters = Join(std::vector<std::string> { |
Lorenzo Colitti | 4604b4a | 2017-08-24 19:21:50 +0900 | [diff] [blame] | 266 | "Chain tetherctrl_counters (2 references)", |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 267 | " pkts bytes target prot opt in out source destination", |
| 268 | " 10000 10000000 RETURN all wlan0 rmnet0 ::/0 ::/0", |
| 269 | " 20000 20000000 RETURN all rmnet0 wlan0 ::/0 ::/0", |
| 270 | }, '\n'); |
| 271 | |
Lorenzo Colitti | 5192bf7 | 2017-09-04 13:30:59 +0900 | [diff] [blame] | 272 | void expectTetherStatsEqual(const TetherController::TetherStats& expected, |
| 273 | const TetherController::TetherStats& actual) { |
| 274 | EXPECT_EQ(expected.intIface, actual.intIface); |
| 275 | EXPECT_EQ(expected.extIface, actual.extIface); |
| 276 | EXPECT_EQ(expected.rxBytes, actual.rxBytes); |
| 277 | EXPECT_EQ(expected.txBytes, actual.txBytes); |
| 278 | EXPECT_EQ(expected.rxPackets, actual.rxPackets); |
| 279 | EXPECT_EQ(expected.txPackets, actual.txPackets); |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | TEST_F(TetherControllerTest, TestGetTetherStats) { |
Lorenzo Colitti | 38fd136 | 2017-09-15 11:40:01 +0900 | [diff] [blame] | 283 | // Finding no headers is an error. |
Lorenzo Colitti | 5192bf7 | 2017-09-04 13:30:59 +0900 | [diff] [blame] | 284 | ASSERT_FALSE(isOk(mTetherCtrl.getTetherStats())); |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 285 | clearIptablesRestoreOutput(); |
| 286 | |
Lorenzo Colitti | 38fd136 | 2017-09-15 11:40:01 +0900 | [diff] [blame] | 287 | // Finding only v4 or only v6 headers is an error. |
| 288 | addIptablesRestoreOutput(kTetherCounterHeaders, ""); |
| 289 | ASSERT_FALSE(isOk(mTetherCtrl.getTetherStats())); |
| 290 | clearIptablesRestoreOutput(); |
| 291 | |
| 292 | addIptablesRestoreOutput("", kTetherCounterHeaders); |
| 293 | ASSERT_FALSE(isOk(mTetherCtrl.getTetherStats())); |
| 294 | clearIptablesRestoreOutput(); |
| 295 | |
| 296 | // Finding headers but no stats is not an error. |
| 297 | addIptablesRestoreOutput(kTetherCounterHeaders, kTetherCounterHeaders); |
| 298 | StatusOr<TetherStatsList> result = mTetherCtrl.getTetherStats(); |
| 299 | ASSERT_TRUE(isOk(result)); |
| 300 | TetherStatsList actual = result.value(); |
| 301 | ASSERT_EQ(0U, actual.size()); |
| 302 | clearIptablesRestoreOutput(); |
| 303 | |
| 304 | |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 305 | addIptablesRestoreOutput(kIPv6TetherCounters); |
Lorenzo Colitti | 5192bf7 | 2017-09-04 13:30:59 +0900 | [diff] [blame] | 306 | ASSERT_FALSE(isOk(mTetherCtrl.getTetherStats())); |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 307 | clearIptablesRestoreOutput(); |
| 308 | |
| 309 | // IPv4 and IPv6 counters are properly added together. |
| 310 | addIptablesRestoreOutput(kIPv4TetherCounters, kIPv6TetherCounters); |
Lorenzo Colitti | 9a65ac6 | 2017-09-04 18:07:56 +0900 | [diff] [blame] | 311 | TetherStats expected0("wlan0", "rmnet0", 20002002, 20027, 10002373, 10026); |
| 312 | TetherStats expected1("bt-pan", "rmnet0", 1708806, 1450, 107471, 1040); |
Lorenzo Colitti | 38fd136 | 2017-09-15 11:40:01 +0900 | [diff] [blame] | 313 | result = mTetherCtrl.getTetherStats(); |
Lorenzo Colitti | 5192bf7 | 2017-09-04 13:30:59 +0900 | [diff] [blame] | 314 | ASSERT_TRUE(isOk(result)); |
Lorenzo Colitti | 38fd136 | 2017-09-15 11:40:01 +0900 | [diff] [blame] | 315 | actual = result.value(); |
Lorenzo Colitti | 5192bf7 | 2017-09-04 13:30:59 +0900 | [diff] [blame] | 316 | ASSERT_EQ(2U, actual.size()); |
| 317 | expectTetherStatsEqual(expected0, result.value()[0]); |
| 318 | expectTetherStatsEqual(expected1, result.value()[1]); |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 319 | clearIptablesRestoreOutput(); |
| 320 | |
Lorenzo Colitti | 0935339 | 2017-08-24 14:20:32 +0900 | [diff] [blame] | 321 | // No stats: error. |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 322 | addIptablesRestoreOutput("", kIPv6TetherCounters); |
Lorenzo Colitti | 5192bf7 | 2017-09-04 13:30:59 +0900 | [diff] [blame] | 323 | ASSERT_FALSE(isOk(mTetherCtrl.getTetherStats())); |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 324 | clearIptablesRestoreOutput(); |
| 325 | |
| 326 | addIptablesRestoreOutput(kIPv4TetherCounters, ""); |
Lorenzo Colitti | 5192bf7 | 2017-09-04 13:30:59 +0900 | [diff] [blame] | 327 | ASSERT_FALSE(isOk(mTetherCtrl.getTetherStats())); |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 328 | clearIptablesRestoreOutput(); |
| 329 | |
| 330 | // Include only one pair of interfaces and things are fine. |
| 331 | std::vector<std::string> counterLines = android::base::Split(kIPv4TetherCounters, "\n"); |
| 332 | std::vector<std::string> brokenCounterLines = counterLines; |
| 333 | counterLines.resize(4); |
| 334 | std::string counters = Join(counterLines, "\n") + "\n"; |
| 335 | addIptablesRestoreOutput(counters, counters); |
Lorenzo Colitti | 9a65ac6 | 2017-09-04 18:07:56 +0900 | [diff] [blame] | 336 | TetherStats expected1_0("wlan0", "rmnet0", 4004, 54, 4746, 52); |
Lorenzo Colitti | 5192bf7 | 2017-09-04 13:30:59 +0900 | [diff] [blame] | 337 | result = mTetherCtrl.getTetherStats(); |
| 338 | ASSERT_TRUE(isOk(result)); |
| 339 | actual = result.value(); |
| 340 | ASSERT_EQ(1U, actual.size()); |
| 341 | expectTetherStatsEqual(expected1_0, actual[0]); |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 342 | clearIptablesRestoreOutput(); |
| 343 | |
| 344 | // But if interfaces aren't paired, it's always an error. |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 345 | counterLines.resize(3); |
| 346 | counters = Join(counterLines, "\n") + "\n"; |
| 347 | addIptablesRestoreOutput(counters, counters); |
Lorenzo Colitti | 5192bf7 | 2017-09-04 13:30:59 +0900 | [diff] [blame] | 348 | result = mTetherCtrl.getTetherStats(); |
| 349 | ASSERT_FALSE(isOk(result)); |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 350 | clearIptablesRestoreOutput(); |
| 351 | |
| 352 | // Token unit test of the fact that we return the stats in the error message which the caller |
| 353 | // ignores. |
| 354 | std::string expectedError = counters; |
Lorenzo Colitti | 5192bf7 | 2017-09-04 13:30:59 +0900 | [diff] [blame] | 355 | std::string err = result.status().msg(); |
Lorenzo Colitti | 9a8a9ff | 2017-01-31 19:06:59 +0900 | [diff] [blame] | 356 | ASSERT_LE(expectedError.size(), err.size()); |
| 357 | EXPECT_TRUE(std::equal(expectedError.rbegin(), expectedError.rend(), err.rbegin())); |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | } // namespace net |
| 361 | } // namespace android |