blob: bcdb106b3ef0277d54869fc64a9f6663f3b3e119 [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 = {
130 "*filter",
Lorenzo Colitti4604b4a2017-08-24 19:21:50 +0900131 StringPrintf("-A tetherctrl_FORWARD -i %s -o %s -m state --state"
132 " ESTABLISHED,RELATED -g tetherctrl_counters", extIf, intIf),
133 StringPrintf("-A tetherctrl_FORWARD -i %s -o %s -m state --state INVALID -j DROP",
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900134 intIf, extIf),
Lorenzo Colitti4604b4a2017-08-24 19:21:50 +0900135 StringPrintf("-A tetherctrl_FORWARD -i %s -o %s -g tetherctrl_counters",
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900136 intIf, extIf),
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900137 };
138
139 std::vector<std::string> v6Cmds = {
140 "*filter",
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900141 };
142
Remi NGUYEN VAN70a269a2018-03-19 11:24:10 +0900143 if (withCounterChainRules) {
144 const std::vector<std::string> counterRules = {
145 StringPrintf("-A tetherctrl_counters -i %s -o %s -j RETURN", intIf, extIf),
146 StringPrintf("-A tetherctrl_counters -i %s -o %s -j RETURN", extIf, intIf),
147 };
148
149 appendAll(v4Cmds, counterRules);
150 appendAll(v6Cmds, counterRules);
151 }
152
153 appendAll(v4Cmds, {
154 "-D tetherctrl_FORWARD -j DROP",
155 "-A tetherctrl_FORWARD -j DROP",
156 "COMMIT\n",
157 });
158
159 v6Cmds.push_back("COMMIT\n");
160
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900161 return {
162 { V6, rpfilterCmd },
163 { V4, Join(v4Cmds, '\n') },
164 { V6, Join(v6Cmds, '\n') },
165 };
166 }
167
Remi NGUYEN VAN3b47c792018-03-20 14:44:12 +0900168 constexpr static const bool WITH_COUNTERS = true;
169 constexpr static const bool NO_COUNTERS = false;
170 constexpr static const bool WITH_IPV6 = true;
171 constexpr static const bool NO_IPV6 = false;
172 ExpectedIptablesCommands allNewNatCommands(
173 const char *intIf, const char *extIf, bool withCounterChainRules,
174 bool withIPv6Upstream) {
175
176 ExpectedIptablesCommands commands;
177 ExpectedIptablesCommands setupFirstIPv4Commands = firstIPv4UpstreamCommands(extIf);
178 ExpectedIptablesCommands startFirstNatCommands = startNatCommands(intIf, extIf,
179 withCounterChainRules);
180
181 appendAll(commands, setupFirstIPv4Commands);
182 if (withIPv6Upstream) {
183 ExpectedIptablesCommands setupFirstIPv6Commands = firstIPv6UpstreamCommands();
184 appendAll(commands, setupFirstIPv6Commands);
185 }
186 appendAll(commands, startFirstNatCommands);
187
188 return commands;
189 }
190
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900191 ExpectedIptablesCommands stopNatCommands(const char *intIf, const char *extIf) {
192 std::string rpfilterCmd = StringPrintf(
193 "*raw\n"
Lorenzo Colitti4604b4a2017-08-24 19:21:50 +0900194 "-D tetherctrl_raw_PREROUTING -i %s -m rpfilter --invert ! -s fe80::/64 -j DROP\n"
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900195 "COMMIT\n", intIf);
196
197 std::vector<std::string> v4Cmds = {
198 "*filter",
Lorenzo Colitti4604b4a2017-08-24 19:21:50 +0900199 StringPrintf("-D tetherctrl_FORWARD -i %s -o %s -m state --state"
200 " ESTABLISHED,RELATED -g tetherctrl_counters", extIf, intIf),
201 StringPrintf("-D tetherctrl_FORWARD -i %s -o %s -m state --state INVALID -j DROP",
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900202 intIf, extIf),
Lorenzo Colitti4604b4a2017-08-24 19:21:50 +0900203 StringPrintf("-D tetherctrl_FORWARD -i %s -o %s -g tetherctrl_counters",
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900204 intIf, extIf),
205 "COMMIT\n",
206 };
207
208 return {
209 { V6, rpfilterCmd },
210 { V4, Join(v4Cmds, '\n') },
211 };
212
213 }
214};
215
216TEST_F(TetherControllerTest, TestSetupIptablesHooks) {
217 mTetherCtrl.setupIptablesHooks();
218 expectIptablesRestoreCommands(SETUP_COMMANDS);
219}
220
221TEST_F(TetherControllerTest, TestSetDefaults) {
222 setDefaults();
223 expectIptablesRestoreCommands(FLUSH_COMMANDS);
224}
225
226TEST_F(TetherControllerTest, TestAddAndRemoveNat) {
Remi NGUYEN VAN70a269a2018-03-19 11:24:10 +0900227 // Start first NAT on first upstream interface. Expect the upstream and NAT rules to be created.
Remi NGUYEN VAN3b47c792018-03-20 14:44:12 +0900228 ExpectedIptablesCommands firstNat = allNewNatCommands(
229 "wlan0", "rmnet0", WITH_COUNTERS, WITH_IPV6);
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900230 mTetherCtrl.enableNat("wlan0", "rmnet0");
Remi NGUYEN VAN70a269a2018-03-19 11:24:10 +0900231 expectIptablesRestoreCommands(firstNat);
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900232
Remi NGUYEN VAN70a269a2018-03-19 11:24:10 +0900233 // Start second NAT on same upstream. Expect only the counter rules to be created.
Remi NGUYEN VAN3b47c792018-03-20 14:44:12 +0900234 ExpectedIptablesCommands startOtherNatOnSameUpstream = startNatCommands(
235 "usb0", "rmnet0", WITH_COUNTERS);
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900236 mTetherCtrl.enableNat("usb0", "rmnet0");
Remi NGUYEN VAN70a269a2018-03-19 11:24:10 +0900237 expectIptablesRestoreCommands(startOtherNatOnSameUpstream);
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900238
Remi NGUYEN VAN70a269a2018-03-19 11:24:10 +0900239 // Remove the first NAT.
240 ExpectedIptablesCommands stopFirstNat = stopNatCommands("wlan0", "rmnet0");
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900241 mTetherCtrl.disableNat("wlan0", "rmnet0");
Remi NGUYEN VAN70a269a2018-03-19 11:24:10 +0900242 expectIptablesRestoreCommands(stopFirstNat);
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900243
Remi NGUYEN VAN70a269a2018-03-19 11:24:10 +0900244 // Remove the last NAT. Expect rules to be cleared.
245 ExpectedIptablesCommands stopLastNat = stopNatCommands("usb0", "rmnet0");
246
247 appendAll(stopLastNat, FLUSH_COMMANDS);
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900248 mTetherCtrl.disableNat("usb0", "rmnet0");
Remi NGUYEN VAN70a269a2018-03-19 11:24:10 +0900249 expectIptablesRestoreCommands(stopLastNat);
250
Remi NGUYEN VAN3b47c792018-03-20 14:44:12 +0900251 // Re-add a NAT removed previously: tetherctrl_counters chain rules are not re-added
252 firstNat = allNewNatCommands("wlan0", "rmnet0", NO_COUNTERS, WITH_IPV6);
Remi NGUYEN VAN70a269a2018-03-19 11:24:10 +0900253 mTetherCtrl.enableNat("wlan0", "rmnet0");
254 expectIptablesRestoreCommands(firstNat);
255
256 // Remove it again. Expect rules to be cleared.
257 stopLastNat = stopNatCommands("wlan0", "rmnet0");
258 appendAll(stopLastNat, FLUSH_COMMANDS);
259 mTetherCtrl.disableNat("wlan0", "rmnet0");
260 expectIptablesRestoreCommands(stopLastNat);
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900261}
262
Remi NGUYEN VAN3b47c792018-03-20 14:44:12 +0900263TEST_F(TetherControllerTest, TestMultipleUpstreams) {
264 // Start first NAT on first upstream interface. Expect the upstream and NAT rules to be created.
265 ExpectedIptablesCommands firstNat = allNewNatCommands(
266 "wlan0", "rmnet0", WITH_COUNTERS, WITH_IPV6);
267 mTetherCtrl.enableNat("wlan0", "rmnet0");
268 expectIptablesRestoreCommands(firstNat);
269
270 // Start second NAT, on new upstream. Expect the upstream and NAT rules to be created for IPv4,
271 // but no counter rules for IPv6.
272 ExpectedIptablesCommands secondNat = allNewNatCommands(
273 "wlan0", "v4-rmnet0", WITH_COUNTERS, NO_IPV6);
274 mTetherCtrl.enableNat("wlan0", "v4-rmnet0");
275 expectIptablesRestoreCommands(secondNat);
276
277 // Pretend that the caller has forgotten that it set up the second NAT, and asks us to do so
278 // again. Expect that we take no action.
279 const ExpectedIptablesCommands NONE = {};
280 mTetherCtrl.enableNat("wlan0", "v4-rmnet0");
281 expectIptablesRestoreCommands(NONE);
282
283 // Remove the second NAT.
284 ExpectedIptablesCommands stopSecondNat = stopNatCommands("wlan0", "v4-rmnet0");
285 mTetherCtrl.disableNat("wlan0", "v4-rmnet0");
286 expectIptablesRestoreCommands(stopSecondNat);
287
288 // Remove the first NAT. Expect rules to be cleared.
289 ExpectedIptablesCommands stopFirstNat = stopNatCommands("wlan0", "rmnet0");
290 appendAll(stopFirstNat, FLUSH_COMMANDS);
291 mTetherCtrl.disableNat("wlan0", "rmnet0");
292 expectIptablesRestoreCommands(stopFirstNat);
293}
294
Lorenzo Colitti09353392017-08-24 14:20:32 +0900295std::string kTetherCounterHeaders = Join(std::vector<std::string> {
Lorenzo Colitti4604b4a2017-08-24 19:21:50 +0900296 "Chain tetherctrl_counters (4 references)",
Lorenzo Colitti09353392017-08-24 14:20:32 +0900297 " pkts bytes target prot opt in out source destination",
298}, '\n');
299
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900300std::string kIPv4TetherCounters = Join(std::vector<std::string> {
Lorenzo Colitti4604b4a2017-08-24 19:21:50 +0900301 "Chain tetherctrl_counters (4 references)",
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900302 " pkts bytes target prot opt in out source destination",
303 " 26 2373 RETURN all -- wlan0 rmnet0 0.0.0.0/0 0.0.0.0/0",
304 " 27 2002 RETURN all -- rmnet0 wlan0 0.0.0.0/0 0.0.0.0/0",
305 " 1040 107471 RETURN all -- bt-pan rmnet0 0.0.0.0/0 0.0.0.0/0",
306 " 1450 1708806 RETURN all -- rmnet0 bt-pan 0.0.0.0/0 0.0.0.0/0",
307}, '\n');
308
309std::string kIPv6TetherCounters = Join(std::vector<std::string> {
Lorenzo Colitti4604b4a2017-08-24 19:21:50 +0900310 "Chain tetherctrl_counters (2 references)",
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900311 " pkts bytes target prot opt in out source destination",
312 " 10000 10000000 RETURN all wlan0 rmnet0 ::/0 ::/0",
313 " 20000 20000000 RETURN all rmnet0 wlan0 ::/0 ::/0",
314}, '\n');
315
Lorenzo Colitti5192bf72017-09-04 13:30:59 +0900316void expectTetherStatsEqual(const TetherController::TetherStats& expected,
317 const TetherController::TetherStats& actual) {
318 EXPECT_EQ(expected.intIface, actual.intIface);
319 EXPECT_EQ(expected.extIface, actual.extIface);
320 EXPECT_EQ(expected.rxBytes, actual.rxBytes);
321 EXPECT_EQ(expected.txBytes, actual.txBytes);
322 EXPECT_EQ(expected.rxPackets, actual.rxPackets);
323 EXPECT_EQ(expected.txPackets, actual.txPackets);
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900324}
325
326TEST_F(TetherControllerTest, TestGetTetherStats) {
Lorenzo Colitti38fd1362017-09-15 11:40:01 +0900327 // Finding no headers is an error.
Lorenzo Colitti5192bf72017-09-04 13:30:59 +0900328 ASSERT_FALSE(isOk(mTetherCtrl.getTetherStats()));
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900329 clearIptablesRestoreOutput();
330
Lorenzo Colitti38fd1362017-09-15 11:40:01 +0900331 // Finding only v4 or only v6 headers is an error.
332 addIptablesRestoreOutput(kTetherCounterHeaders, "");
333 ASSERT_FALSE(isOk(mTetherCtrl.getTetherStats()));
334 clearIptablesRestoreOutput();
335
336 addIptablesRestoreOutput("", kTetherCounterHeaders);
337 ASSERT_FALSE(isOk(mTetherCtrl.getTetherStats()));
338 clearIptablesRestoreOutput();
339
340 // Finding headers but no stats is not an error.
341 addIptablesRestoreOutput(kTetherCounterHeaders, kTetherCounterHeaders);
342 StatusOr<TetherStatsList> result = mTetherCtrl.getTetherStats();
343 ASSERT_TRUE(isOk(result));
344 TetherStatsList actual = result.value();
345 ASSERT_EQ(0U, actual.size());
346 clearIptablesRestoreOutput();
347
348
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900349 addIptablesRestoreOutput(kIPv6TetherCounters);
Lorenzo Colitti5192bf72017-09-04 13:30:59 +0900350 ASSERT_FALSE(isOk(mTetherCtrl.getTetherStats()));
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900351 clearIptablesRestoreOutput();
352
353 // IPv4 and IPv6 counters are properly added together.
354 addIptablesRestoreOutput(kIPv4TetherCounters, kIPv6TetherCounters);
Lorenzo Colitti9a65ac62017-09-04 18:07:56 +0900355 TetherStats expected0("wlan0", "rmnet0", 20002002, 20027, 10002373, 10026);
356 TetherStats expected1("bt-pan", "rmnet0", 1708806, 1450, 107471, 1040);
Lorenzo Colitti38fd1362017-09-15 11:40:01 +0900357 result = mTetherCtrl.getTetherStats();
Lorenzo Colitti5192bf72017-09-04 13:30:59 +0900358 ASSERT_TRUE(isOk(result));
Lorenzo Colitti38fd1362017-09-15 11:40:01 +0900359 actual = result.value();
Lorenzo Colitti5192bf72017-09-04 13:30:59 +0900360 ASSERT_EQ(2U, actual.size());
361 expectTetherStatsEqual(expected0, result.value()[0]);
362 expectTetherStatsEqual(expected1, result.value()[1]);
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900363 clearIptablesRestoreOutput();
364
Lorenzo Colitti09353392017-08-24 14:20:32 +0900365 // No stats: error.
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900366 addIptablesRestoreOutput("", kIPv6TetherCounters);
Lorenzo Colitti5192bf72017-09-04 13:30:59 +0900367 ASSERT_FALSE(isOk(mTetherCtrl.getTetherStats()));
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900368 clearIptablesRestoreOutput();
369
370 addIptablesRestoreOutput(kIPv4TetherCounters, "");
Lorenzo Colitti5192bf72017-09-04 13:30:59 +0900371 ASSERT_FALSE(isOk(mTetherCtrl.getTetherStats()));
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900372 clearIptablesRestoreOutput();
373
374 // Include only one pair of interfaces and things are fine.
375 std::vector<std::string> counterLines = android::base::Split(kIPv4TetherCounters, "\n");
376 std::vector<std::string> brokenCounterLines = counterLines;
377 counterLines.resize(4);
378 std::string counters = Join(counterLines, "\n") + "\n";
379 addIptablesRestoreOutput(counters, counters);
Lorenzo Colitti9a65ac62017-09-04 18:07:56 +0900380 TetherStats expected1_0("wlan0", "rmnet0", 4004, 54, 4746, 52);
Lorenzo Colitti5192bf72017-09-04 13:30:59 +0900381 result = mTetherCtrl.getTetherStats();
382 ASSERT_TRUE(isOk(result));
383 actual = result.value();
384 ASSERT_EQ(1U, actual.size());
385 expectTetherStatsEqual(expected1_0, actual[0]);
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900386 clearIptablesRestoreOutput();
387
388 // But if interfaces aren't paired, it's always an error.
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900389 counterLines.resize(3);
390 counters = Join(counterLines, "\n") + "\n";
391 addIptablesRestoreOutput(counters, counters);
Lorenzo Colitti5192bf72017-09-04 13:30:59 +0900392 result = mTetherCtrl.getTetherStats();
393 ASSERT_FALSE(isOk(result));
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900394 clearIptablesRestoreOutput();
395
396 // Token unit test of the fact that we return the stats in the error message which the caller
397 // ignores.
398 std::string expectedError = counters;
Lorenzo Colitti5192bf72017-09-04 13:30:59 +0900399 std::string err = result.status().msg();
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900400 ASSERT_LE(expectedError.size(), err.size());
401 EXPECT_TRUE(std::equal(expectedError.rbegin(), expectedError.rend(), err.rbegin()));
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900402}
403
404} // namespace net
405} // namespace android