blob: 7ccdc0c11f8551a8d895deec70357034553872b4 [file] [log] [blame]
Lorenzo Colittia93126d2017-08-24 13:28:19 +09001/*
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 Colitti5192bf72017-09-04 13:30:59 +090031#include <netdutils/StatusOr.h>
Lorenzo Colittia93126d2017-08-24 13:28:19 +090032
33#include "TetherController.h"
34#include "IptablesBaseTest.h"
35
36using android::base::Join;
37using android::base::StringPrintf;
Lorenzo Colitti5192bf72017-09-04 13:30:59 +090038using android::netdutils::StatusOr;
39using TetherStats = android::net::TetherController::TetherStats;
40using TetherStatsList = android::net::TetherController::TetherStatsList;
Lorenzo Colittia93126d2017-08-24 13:28:19 +090041
42namespace android {
43namespace net {
44
45class TetherControllerTest : public IptablesBaseTest {
46public:
47 TetherControllerTest() {
48 TetherController::iptablesRestoreFunction = fakeExecIptablesRestoreWithOutput;
49 }
50
51protected:
52 TetherController mTetherCtrl;
53
54 int setDefaults() {
55 return mTetherCtrl.setDefaults();
56 }
57
58 const ExpectedIptablesCommands FLUSH_COMMANDS = {
59 { V4, "*filter\n"
Lorenzo Colitti4604b4a2017-08-24 19:21:50 +090060 ":tetherctrl_FORWARD -\n"
61 "-A tetherctrl_FORWARD -j DROP\n"
Lorenzo Colittia93126d2017-08-24 13:28:19 +090062 "COMMIT\n"
63 "*nat\n"
Lorenzo Colitti4604b4a2017-08-24 19:21:50 +090064 ":tetherctrl_nat_POSTROUTING -\n"
Lorenzo Colittia93126d2017-08-24 13:28:19 +090065 "COMMIT\n" },
66 { V6, "*filter\n"
Lorenzo Colitti4604b4a2017-08-24 19:21:50 +090067 ":tetherctrl_FORWARD -\n"
Lorenzo Colittia93126d2017-08-24 13:28:19 +090068 "COMMIT\n"
69 "*raw\n"
Lorenzo Colitti4604b4a2017-08-24 19:21:50 +090070 ":tetherctrl_raw_PREROUTING -\n"
Lorenzo Colittia93126d2017-08-24 13:28:19 +090071 "COMMIT\n" },
72 };
73
74 const ExpectedIptablesCommands SETUP_COMMANDS = {
75 { V4, "*filter\n"
Lorenzo Colitti4604b4a2017-08-24 19:21:50 +090076 ":tetherctrl_FORWARD -\n"
77 "-A tetherctrl_FORWARD -j DROP\n"
Lorenzo Colittia93126d2017-08-24 13:28:19 +090078 "COMMIT\n"
79 "*nat\n"
Lorenzo Colitti4604b4a2017-08-24 19:21:50 +090080 ":tetherctrl_nat_POSTROUTING -\n"
Lorenzo Colittia93126d2017-08-24 13:28:19 +090081 "COMMIT\n" },
82 { V6, "*filter\n"
Lorenzo Colitti4604b4a2017-08-24 19:21:50 +090083 ":tetherctrl_FORWARD -\n"
Lorenzo Colittia93126d2017-08-24 13:28:19 +090084 "COMMIT\n"
85 "*raw\n"
Lorenzo Colitti4604b4a2017-08-24 19:21:50 +090086 ":tetherctrl_raw_PREROUTING -\n"
Lorenzo Colittia93126d2017-08-24 13:28:19 +090087 "COMMIT\n" },
88 { V4, "*mangle\n"
Lorenzo Colitti4604b4a2017-08-24 19:21:50 +090089 "-A tetherctrl_mangle_FORWARD -p tcp --tcp-flags SYN SYN "
Lorenzo Colittia93126d2017-08-24 13:28:19 +090090 "-j TCPMSS --clamp-mss-to-pmtu\n"
91 "COMMIT\n" },
92 { V4V6, "*filter\n"
Lorenzo Colitti4604b4a2017-08-24 19:21:50 +090093 ":tetherctrl_counters -\n"
Lorenzo Colittia93126d2017-08-24 13:28:19 +090094 "COMMIT\n" },
95 };
96
Remi NGUYEN VAN70a269a2018-03-19 11:24:10 +090097 ExpectedIptablesCommands firstIPv4UpstreamCommands(const char *extIf) {
Lorenzo Colittia93126d2017-08-24 13:28:19 +090098 std::string v4Cmd = StringPrintf(
99 "*nat\n"
Lorenzo Colitti4604b4a2017-08-24 19:21:50 +0900100 "-A tetherctrl_nat_POSTROUTING -o %s -j MASQUERADE\n"
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900101 "COMMIT\n", extIf);
Remi NGUYEN VAN70a269a2018-03-19 11:24:10 +0900102 return {
103 { V4, v4Cmd },
104 };
105 }
106
107 ExpectedIptablesCommands firstIPv6UpstreamCommands() {
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900108 std::string v6Cmd =
109 "*filter\n"
Lorenzo Colitti4604b4a2017-08-24 19:21:50 +0900110 "-A tetherctrl_FORWARD -g tetherctrl_counters\n"
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900111 "COMMIT\n";
112 return {
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900113 { V6, v6Cmd },
114 };
115 }
116
Remi NGUYEN VAN70a269a2018-03-19 11:24:10 +0900117 template<typename T>
118 void appendAll(std::vector<T>& cmds, const std::vector<T>& appendCmds) {
Remi NGUYEN VAN3b47c792018-03-20 14:44:12 +0900119 cmds.insert(cmds.end(), appendCmds.begin(), appendCmds.end());
Remi NGUYEN VAN70a269a2018-03-19 11:24:10 +0900120 }
121
122 ExpectedIptablesCommands startNatCommands(const char *intIf, const char *extIf,
123 bool withCounterChainRules) {
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900124 std::string rpfilterCmd = StringPrintf(
125 "*raw\n"
Lorenzo Colitti4604b4a2017-08-24 19:21:50 +0900126 "-A tetherctrl_raw_PREROUTING -i %s -m rpfilter --invert ! -s fe80::/64 -j DROP\n"
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900127 "COMMIT\n", intIf);
128
129 std::vector<std::string> v4Cmds = {
hiroaki.yokoyama04f2cb52018-06-13 18:47:30 +0900130 "*raw",
131 StringPrintf(
132 "-A tetherctrl_raw_PREROUTING -p tcp --dport 21 -i %s -j CT --helper ftp",
133 intIf),
134 StringPrintf("-A tetherctrl_raw_PREROUTING -p tcp --dport 1723 -i %s -j CT "
135 "--helper pptp",
136 intIf),
137 "COMMIT",
138 "*filter",
139 StringPrintf("-A tetherctrl_FORWARD -i %s -o %s -m state --state"
140 " ESTABLISHED,RELATED -g tetherctrl_counters",
141 extIf, intIf),
142 StringPrintf("-A tetherctrl_FORWARD -i %s -o %s -m state --state INVALID -j DROP",
143 intIf, extIf),
144 StringPrintf("-A tetherctrl_FORWARD -i %s -o %s -g tetherctrl_counters", intIf,
145 extIf),
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900146 };
147
148 std::vector<std::string> v6Cmds = {
149 "*filter",
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900150 };
151
Remi NGUYEN VAN70a269a2018-03-19 11:24:10 +0900152 if (withCounterChainRules) {
153 const std::vector<std::string> counterRules = {
154 StringPrintf("-A tetherctrl_counters -i %s -o %s -j RETURN", intIf, extIf),
155 StringPrintf("-A tetherctrl_counters -i %s -o %s -j RETURN", extIf, intIf),
156 };
157
158 appendAll(v4Cmds, counterRules);
159 appendAll(v6Cmds, counterRules);
160 }
161
162 appendAll(v4Cmds, {
163 "-D tetherctrl_FORWARD -j DROP",
164 "-A tetherctrl_FORWARD -j DROP",
165 "COMMIT\n",
166 });
167
168 v6Cmds.push_back("COMMIT\n");
169
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900170 return {
171 { V6, rpfilterCmd },
172 { V4, Join(v4Cmds, '\n') },
173 { V6, Join(v6Cmds, '\n') },
174 };
175 }
176
Remi NGUYEN VAN3b47c792018-03-20 14:44:12 +0900177 constexpr static const bool WITH_COUNTERS = true;
178 constexpr static const bool NO_COUNTERS = false;
179 constexpr static const bool WITH_IPV6 = true;
180 constexpr static const bool NO_IPV6 = false;
181 ExpectedIptablesCommands allNewNatCommands(
182 const char *intIf, const char *extIf, bool withCounterChainRules,
183 bool withIPv6Upstream) {
184
185 ExpectedIptablesCommands commands;
186 ExpectedIptablesCommands setupFirstIPv4Commands = firstIPv4UpstreamCommands(extIf);
187 ExpectedIptablesCommands startFirstNatCommands = startNatCommands(intIf, extIf,
188 withCounterChainRules);
189
190 appendAll(commands, setupFirstIPv4Commands);
191 if (withIPv6Upstream) {
192 ExpectedIptablesCommands setupFirstIPv6Commands = firstIPv6UpstreamCommands();
193 appendAll(commands, setupFirstIPv6Commands);
194 }
195 appendAll(commands, startFirstNatCommands);
196
197 return commands;
198 }
199
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900200 ExpectedIptablesCommands stopNatCommands(const char *intIf, const char *extIf) {
201 std::string rpfilterCmd = StringPrintf(
202 "*raw\n"
Lorenzo Colitti4604b4a2017-08-24 19:21:50 +0900203 "-D tetherctrl_raw_PREROUTING -i %s -m rpfilter --invert ! -s fe80::/64 -j DROP\n"
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900204 "COMMIT\n", intIf);
205
206 std::vector<std::string> v4Cmds = {
hiroaki.yokoyama04f2cb52018-06-13 18:47:30 +0900207 "*raw",
208 StringPrintf(
209 "-D tetherctrl_raw_PREROUTING -p tcp --dport 21 -i %s -j CT --helper ftp",
210 intIf),
211 StringPrintf("-D tetherctrl_raw_PREROUTING -p tcp --dport 1723 -i %s -j CT "
212 "--helper pptp",
213 intIf),
214 "COMMIT",
215 "*filter",
216 StringPrintf("-D tetherctrl_FORWARD -i %s -o %s -m state --state"
217 " ESTABLISHED,RELATED -g tetherctrl_counters",
218 extIf, intIf),
219 StringPrintf("-D tetherctrl_FORWARD -i %s -o %s -m state --state INVALID -j DROP",
220 intIf, extIf),
221 StringPrintf("-D tetherctrl_FORWARD -i %s -o %s -g tetherctrl_counters", intIf,
222 extIf),
223 "COMMIT\n",
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900224 };
225
226 return {
227 { V6, rpfilterCmd },
228 { V4, Join(v4Cmds, '\n') },
229 };
230
231 }
232};
233
234TEST_F(TetherControllerTest, TestSetupIptablesHooks) {
235 mTetherCtrl.setupIptablesHooks();
236 expectIptablesRestoreCommands(SETUP_COMMANDS);
237}
238
239TEST_F(TetherControllerTest, TestSetDefaults) {
240 setDefaults();
241 expectIptablesRestoreCommands(FLUSH_COMMANDS);
242}
243
244TEST_F(TetherControllerTest, TestAddAndRemoveNat) {
Remi NGUYEN VAN70a269a2018-03-19 11:24:10 +0900245 // Start first NAT on first upstream interface. Expect the upstream and NAT rules to be created.
Remi NGUYEN VAN3b47c792018-03-20 14:44:12 +0900246 ExpectedIptablesCommands firstNat = allNewNatCommands(
247 "wlan0", "rmnet0", WITH_COUNTERS, WITH_IPV6);
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900248 mTetherCtrl.enableNat("wlan0", "rmnet0");
Remi NGUYEN VAN70a269a2018-03-19 11:24:10 +0900249 expectIptablesRestoreCommands(firstNat);
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900250
Remi NGUYEN VAN70a269a2018-03-19 11:24:10 +0900251 // Start second NAT on same upstream. Expect only the counter rules to be created.
Remi NGUYEN VAN3b47c792018-03-20 14:44:12 +0900252 ExpectedIptablesCommands startOtherNatOnSameUpstream = startNatCommands(
253 "usb0", "rmnet0", WITH_COUNTERS);
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900254 mTetherCtrl.enableNat("usb0", "rmnet0");
Remi NGUYEN VAN70a269a2018-03-19 11:24:10 +0900255 expectIptablesRestoreCommands(startOtherNatOnSameUpstream);
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900256
Remi NGUYEN VAN70a269a2018-03-19 11:24:10 +0900257 // Remove the first NAT.
258 ExpectedIptablesCommands stopFirstNat = stopNatCommands("wlan0", "rmnet0");
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900259 mTetherCtrl.disableNat("wlan0", "rmnet0");
Remi NGUYEN VAN70a269a2018-03-19 11:24:10 +0900260 expectIptablesRestoreCommands(stopFirstNat);
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900261
Remi NGUYEN VAN70a269a2018-03-19 11:24:10 +0900262 // Remove the last NAT. Expect rules to be cleared.
263 ExpectedIptablesCommands stopLastNat = stopNatCommands("usb0", "rmnet0");
264
265 appendAll(stopLastNat, FLUSH_COMMANDS);
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900266 mTetherCtrl.disableNat("usb0", "rmnet0");
Remi NGUYEN VAN70a269a2018-03-19 11:24:10 +0900267 expectIptablesRestoreCommands(stopLastNat);
268
Remi NGUYEN VAN3b47c792018-03-20 14:44:12 +0900269 // Re-add a NAT removed previously: tetherctrl_counters chain rules are not re-added
270 firstNat = allNewNatCommands("wlan0", "rmnet0", NO_COUNTERS, WITH_IPV6);
Remi NGUYEN VAN70a269a2018-03-19 11:24:10 +0900271 mTetherCtrl.enableNat("wlan0", "rmnet0");
272 expectIptablesRestoreCommands(firstNat);
273
274 // Remove it again. Expect rules to be cleared.
275 stopLastNat = stopNatCommands("wlan0", "rmnet0");
276 appendAll(stopLastNat, FLUSH_COMMANDS);
277 mTetherCtrl.disableNat("wlan0", "rmnet0");
278 expectIptablesRestoreCommands(stopLastNat);
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900279}
280
Remi NGUYEN VAN3b47c792018-03-20 14:44:12 +0900281TEST_F(TetherControllerTest, TestMultipleUpstreams) {
282 // Start first NAT on first upstream interface. Expect the upstream and NAT rules to be created.
283 ExpectedIptablesCommands firstNat = allNewNatCommands(
284 "wlan0", "rmnet0", WITH_COUNTERS, WITH_IPV6);
285 mTetherCtrl.enableNat("wlan0", "rmnet0");
286 expectIptablesRestoreCommands(firstNat);
287
288 // Start second NAT, on new upstream. Expect the upstream and NAT rules to be created for IPv4,
289 // but no counter rules for IPv6.
290 ExpectedIptablesCommands secondNat = allNewNatCommands(
291 "wlan0", "v4-rmnet0", WITH_COUNTERS, NO_IPV6);
292 mTetherCtrl.enableNat("wlan0", "v4-rmnet0");
293 expectIptablesRestoreCommands(secondNat);
294
295 // Pretend that the caller has forgotten that it set up the second NAT, and asks us to do so
296 // again. Expect that we take no action.
297 const ExpectedIptablesCommands NONE = {};
298 mTetherCtrl.enableNat("wlan0", "v4-rmnet0");
299 expectIptablesRestoreCommands(NONE);
300
301 // Remove the second NAT.
302 ExpectedIptablesCommands stopSecondNat = stopNatCommands("wlan0", "v4-rmnet0");
303 mTetherCtrl.disableNat("wlan0", "v4-rmnet0");
304 expectIptablesRestoreCommands(stopSecondNat);
305
306 // Remove the first NAT. Expect rules to be cleared.
307 ExpectedIptablesCommands stopFirstNat = stopNatCommands("wlan0", "rmnet0");
308 appendAll(stopFirstNat, FLUSH_COMMANDS);
309 mTetherCtrl.disableNat("wlan0", "rmnet0");
310 expectIptablesRestoreCommands(stopFirstNat);
311}
312
Lorenzo Colitti09353392017-08-24 14:20:32 +0900313std::string kTetherCounterHeaders = Join(std::vector<std::string> {
Lorenzo Colitti4604b4a2017-08-24 19:21:50 +0900314 "Chain tetherctrl_counters (4 references)",
Lorenzo Colitti09353392017-08-24 14:20:32 +0900315 " pkts bytes target prot opt in out source destination",
316}, '\n');
317
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900318std::string kIPv4TetherCounters = Join(std::vector<std::string> {
Lorenzo Colitti4604b4a2017-08-24 19:21:50 +0900319 "Chain tetherctrl_counters (4 references)",
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900320 " pkts bytes target prot opt in out source destination",
321 " 26 2373 RETURN all -- wlan0 rmnet0 0.0.0.0/0 0.0.0.0/0",
322 " 27 2002 RETURN all -- rmnet0 wlan0 0.0.0.0/0 0.0.0.0/0",
323 " 1040 107471 RETURN all -- bt-pan rmnet0 0.0.0.0/0 0.0.0.0/0",
324 " 1450 1708806 RETURN all -- rmnet0 bt-pan 0.0.0.0/0 0.0.0.0/0",
325}, '\n');
326
327std::string kIPv6TetherCounters = Join(std::vector<std::string> {
Lorenzo Colitti4604b4a2017-08-24 19:21:50 +0900328 "Chain tetherctrl_counters (2 references)",
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900329 " pkts bytes target prot opt in out source destination",
330 " 10000 10000000 RETURN all wlan0 rmnet0 ::/0 ::/0",
331 " 20000 20000000 RETURN all rmnet0 wlan0 ::/0 ::/0",
332}, '\n');
333
Lorenzo Colitti5192bf72017-09-04 13:30:59 +0900334void expectTetherStatsEqual(const TetherController::TetherStats& expected,
335 const TetherController::TetherStats& actual) {
336 EXPECT_EQ(expected.intIface, actual.intIface);
337 EXPECT_EQ(expected.extIface, actual.extIface);
338 EXPECT_EQ(expected.rxBytes, actual.rxBytes);
339 EXPECT_EQ(expected.txBytes, actual.txBytes);
340 EXPECT_EQ(expected.rxPackets, actual.rxPackets);
341 EXPECT_EQ(expected.txPackets, actual.txPackets);
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900342}
343
344TEST_F(TetherControllerTest, TestGetTetherStats) {
Lorenzo Colitti38fd1362017-09-15 11:40:01 +0900345 // Finding no headers is an error.
Lorenzo Colitti5192bf72017-09-04 13:30:59 +0900346 ASSERT_FALSE(isOk(mTetherCtrl.getTetherStats()));
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900347 clearIptablesRestoreOutput();
348
Lorenzo Colitti38fd1362017-09-15 11:40:01 +0900349 // Finding only v4 or only v6 headers is an error.
350 addIptablesRestoreOutput(kTetherCounterHeaders, "");
351 ASSERT_FALSE(isOk(mTetherCtrl.getTetherStats()));
352 clearIptablesRestoreOutput();
353
354 addIptablesRestoreOutput("", kTetherCounterHeaders);
355 ASSERT_FALSE(isOk(mTetherCtrl.getTetherStats()));
356 clearIptablesRestoreOutput();
357
358 // Finding headers but no stats is not an error.
359 addIptablesRestoreOutput(kTetherCounterHeaders, kTetherCounterHeaders);
360 StatusOr<TetherStatsList> result = mTetherCtrl.getTetherStats();
361 ASSERT_TRUE(isOk(result));
362 TetherStatsList actual = result.value();
363 ASSERT_EQ(0U, actual.size());
364 clearIptablesRestoreOutput();
365
366
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900367 addIptablesRestoreOutput(kIPv6TetherCounters);
Lorenzo Colitti5192bf72017-09-04 13:30:59 +0900368 ASSERT_FALSE(isOk(mTetherCtrl.getTetherStats()));
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900369 clearIptablesRestoreOutput();
370
371 // IPv4 and IPv6 counters are properly added together.
372 addIptablesRestoreOutput(kIPv4TetherCounters, kIPv6TetherCounters);
Lorenzo Colitti9a65ac62017-09-04 18:07:56 +0900373 TetherStats expected0("wlan0", "rmnet0", 20002002, 20027, 10002373, 10026);
374 TetherStats expected1("bt-pan", "rmnet0", 1708806, 1450, 107471, 1040);
Lorenzo Colitti38fd1362017-09-15 11:40:01 +0900375 result = mTetherCtrl.getTetherStats();
Lorenzo Colitti5192bf72017-09-04 13:30:59 +0900376 ASSERT_TRUE(isOk(result));
Lorenzo Colitti38fd1362017-09-15 11:40:01 +0900377 actual = result.value();
Lorenzo Colitti5192bf72017-09-04 13:30:59 +0900378 ASSERT_EQ(2U, actual.size());
379 expectTetherStatsEqual(expected0, result.value()[0]);
380 expectTetherStatsEqual(expected1, result.value()[1]);
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900381 clearIptablesRestoreOutput();
382
Lorenzo Colitti09353392017-08-24 14:20:32 +0900383 // No stats: error.
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900384 addIptablesRestoreOutput("", kIPv6TetherCounters);
Lorenzo Colitti5192bf72017-09-04 13:30:59 +0900385 ASSERT_FALSE(isOk(mTetherCtrl.getTetherStats()));
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900386 clearIptablesRestoreOutput();
387
388 addIptablesRestoreOutput(kIPv4TetherCounters, "");
Lorenzo Colitti5192bf72017-09-04 13:30:59 +0900389 ASSERT_FALSE(isOk(mTetherCtrl.getTetherStats()));
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900390 clearIptablesRestoreOutput();
391
392 // Include only one pair of interfaces and things are fine.
393 std::vector<std::string> counterLines = android::base::Split(kIPv4TetherCounters, "\n");
394 std::vector<std::string> brokenCounterLines = counterLines;
395 counterLines.resize(4);
396 std::string counters = Join(counterLines, "\n") + "\n";
397 addIptablesRestoreOutput(counters, counters);
Lorenzo Colitti9a65ac62017-09-04 18:07:56 +0900398 TetherStats expected1_0("wlan0", "rmnet0", 4004, 54, 4746, 52);
Lorenzo Colitti5192bf72017-09-04 13:30:59 +0900399 result = mTetherCtrl.getTetherStats();
400 ASSERT_TRUE(isOk(result));
401 actual = result.value();
402 ASSERT_EQ(1U, actual.size());
403 expectTetherStatsEqual(expected1_0, actual[0]);
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900404 clearIptablesRestoreOutput();
405
406 // But if interfaces aren't paired, it's always an error.
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900407 counterLines.resize(3);
408 counters = Join(counterLines, "\n") + "\n";
409 addIptablesRestoreOutput(counters, counters);
Lorenzo Colitti5192bf72017-09-04 13:30:59 +0900410 result = mTetherCtrl.getTetherStats();
411 ASSERT_FALSE(isOk(result));
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900412 clearIptablesRestoreOutput();
413
414 // Token unit test of the fact that we return the stats in the error message which the caller
415 // ignores.
416 std::string expectedError = counters;
Lorenzo Colitti5192bf72017-09-04 13:30:59 +0900417 std::string err = result.status().msg();
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900418 ASSERT_LE(expectedError.size(), err.size());
419 EXPECT_TRUE(std::equal(expectedError.rbegin(), expectedError.rend(), err.rbegin()));
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900420}
421
422} // namespace net
423} // namespace android