blob: ba101fd15a911656caf6e8e38e4a02b84dd3f072 [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) {
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 Colittia93126d2017-08-24 13:28:19 +0900126 std::string rpfilterCmd = StringPrintf(
127 "*raw\n"
Lorenzo Colitti4604b4a2017-08-24 19:21:50 +0900128 "-A tetherctrl_raw_PREROUTING -i %s -m rpfilter --invert ! -s fe80::/64 -j DROP\n"
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900129 "COMMIT\n", intIf);
130
131 std::vector<std::string> v4Cmds = {
132 "*filter",
Lorenzo Colitti4604b4a2017-08-24 19:21:50 +0900133 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 Colittia93126d2017-08-24 13:28:19 +0900136 intIf, extIf),
Lorenzo Colitti4604b4a2017-08-24 19:21:50 +0900137 StringPrintf("-A tetherctrl_FORWARD -i %s -o %s -g tetherctrl_counters",
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900138 intIf, extIf),
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900139 };
140
141 std::vector<std::string> v6Cmds = {
142 "*filter",
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900143 };
144
Remi NGUYEN VAN70a269a2018-03-19 11:24:10 +0900145 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 Colittia93126d2017-08-24 13:28:19 +0900163 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 Colitti4604b4a2017-08-24 19:21:50 +0900173 "-D tetherctrl_raw_PREROUTING -i %s -m rpfilter --invert ! -s fe80::/64 -j DROP\n"
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900174 "COMMIT\n", intIf);
175
176 std::vector<std::string> v4Cmds = {
177 "*filter",
Lorenzo Colitti4604b4a2017-08-24 19:21:50 +0900178 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 Colittia93126d2017-08-24 13:28:19 +0900181 intIf, extIf),
Lorenzo Colitti4604b4a2017-08-24 19:21:50 +0900182 StringPrintf("-D tetherctrl_FORWARD -i %s -o %s -g tetherctrl_counters",
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900183 intIf, extIf),
184 "COMMIT\n",
185 };
186
187 return {
188 { V6, rpfilterCmd },
189 { V4, Join(v4Cmds, '\n') },
190 };
191
192 }
193};
194
195TEST_F(TetherControllerTest, TestSetupIptablesHooks) {
196 mTetherCtrl.setupIptablesHooks();
197 expectIptablesRestoreCommands(SETUP_COMMANDS);
198}
199
200TEST_F(TetherControllerTest, TestSetDefaults) {
201 setDefaults();
202 expectIptablesRestoreCommands(FLUSH_COMMANDS);
203}
204
205TEST_F(TetherControllerTest, TestAddAndRemoveNat) {
Remi NGUYEN VAN70a269a2018-03-19 11:24:10 +0900206 // 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 Colittia93126d2017-08-24 13:28:19 +0900214 mTetherCtrl.enableNat("wlan0", "rmnet0");
Remi NGUYEN VAN70a269a2018-03-19 11:24:10 +0900215 expectIptablesRestoreCommands(firstNat);
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900216
Remi NGUYEN VAN70a269a2018-03-19 11:24:10 +0900217 // Start second NAT on same upstream. Expect only the counter rules to be created.
218 ExpectedIptablesCommands startOtherNatOnSameUpstream = startNatCommands("usb0", "rmnet0", true);
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900219 mTetherCtrl.enableNat("usb0", "rmnet0");
Remi NGUYEN VAN70a269a2018-03-19 11:24:10 +0900220 expectIptablesRestoreCommands(startOtherNatOnSameUpstream);
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900221
Remi NGUYEN VAN70a269a2018-03-19 11:24:10 +0900222 // Remove the first NAT.
223 ExpectedIptablesCommands stopFirstNat = stopNatCommands("wlan0", "rmnet0");
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900224 mTetherCtrl.disableNat("wlan0", "rmnet0");
Remi NGUYEN VAN70a269a2018-03-19 11:24:10 +0900225 expectIptablesRestoreCommands(stopFirstNat);
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900226
Remi NGUYEN VAN70a269a2018-03-19 11:24:10 +0900227 // Remove the last NAT. Expect rules to be cleared.
228 ExpectedIptablesCommands stopLastNat = stopNatCommands("usb0", "rmnet0");
229
230 appendAll(stopLastNat, FLUSH_COMMANDS);
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900231 mTetherCtrl.disableNat("usb0", "rmnet0");
Remi NGUYEN VAN70a269a2018-03-19 11:24:10 +0900232 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 Colittia93126d2017-08-24 13:28:19 +0900249}
250
Lorenzo Colitti09353392017-08-24 14:20:32 +0900251std::string kTetherCounterHeaders = Join(std::vector<std::string> {
Lorenzo Colitti4604b4a2017-08-24 19:21:50 +0900252 "Chain tetherctrl_counters (4 references)",
Lorenzo Colitti09353392017-08-24 14:20:32 +0900253 " pkts bytes target prot opt in out source destination",
254}, '\n');
255
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900256std::string kIPv4TetherCounters = Join(std::vector<std::string> {
Lorenzo Colitti4604b4a2017-08-24 19:21:50 +0900257 "Chain tetherctrl_counters (4 references)",
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900258 " 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
265std::string kIPv6TetherCounters = Join(std::vector<std::string> {
Lorenzo Colitti4604b4a2017-08-24 19:21:50 +0900266 "Chain tetherctrl_counters (2 references)",
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900267 " 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 Colitti5192bf72017-09-04 13:30:59 +0900272void 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 Colittia93126d2017-08-24 13:28:19 +0900280}
281
282TEST_F(TetherControllerTest, TestGetTetherStats) {
Lorenzo Colitti38fd1362017-09-15 11:40:01 +0900283 // Finding no headers is an error.
Lorenzo Colitti5192bf72017-09-04 13:30:59 +0900284 ASSERT_FALSE(isOk(mTetherCtrl.getTetherStats()));
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900285 clearIptablesRestoreOutput();
286
Lorenzo Colitti38fd1362017-09-15 11:40:01 +0900287 // 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 Colittia93126d2017-08-24 13:28:19 +0900305 addIptablesRestoreOutput(kIPv6TetherCounters);
Lorenzo Colitti5192bf72017-09-04 13:30:59 +0900306 ASSERT_FALSE(isOk(mTetherCtrl.getTetherStats()));
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900307 clearIptablesRestoreOutput();
308
309 // IPv4 and IPv6 counters are properly added together.
310 addIptablesRestoreOutput(kIPv4TetherCounters, kIPv6TetherCounters);
Lorenzo Colitti9a65ac62017-09-04 18:07:56 +0900311 TetherStats expected0("wlan0", "rmnet0", 20002002, 20027, 10002373, 10026);
312 TetherStats expected1("bt-pan", "rmnet0", 1708806, 1450, 107471, 1040);
Lorenzo Colitti38fd1362017-09-15 11:40:01 +0900313 result = mTetherCtrl.getTetherStats();
Lorenzo Colitti5192bf72017-09-04 13:30:59 +0900314 ASSERT_TRUE(isOk(result));
Lorenzo Colitti38fd1362017-09-15 11:40:01 +0900315 actual = result.value();
Lorenzo Colitti5192bf72017-09-04 13:30:59 +0900316 ASSERT_EQ(2U, actual.size());
317 expectTetherStatsEqual(expected0, result.value()[0]);
318 expectTetherStatsEqual(expected1, result.value()[1]);
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900319 clearIptablesRestoreOutput();
320
Lorenzo Colitti09353392017-08-24 14:20:32 +0900321 // No stats: error.
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900322 addIptablesRestoreOutput("", kIPv6TetherCounters);
Lorenzo Colitti5192bf72017-09-04 13:30:59 +0900323 ASSERT_FALSE(isOk(mTetherCtrl.getTetherStats()));
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900324 clearIptablesRestoreOutput();
325
326 addIptablesRestoreOutput(kIPv4TetherCounters, "");
Lorenzo Colitti5192bf72017-09-04 13:30:59 +0900327 ASSERT_FALSE(isOk(mTetherCtrl.getTetherStats()));
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900328 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 Colitti9a65ac62017-09-04 18:07:56 +0900336 TetherStats expected1_0("wlan0", "rmnet0", 4004, 54, 4746, 52);
Lorenzo Colitti5192bf72017-09-04 13:30:59 +0900337 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 Colittia93126d2017-08-24 13:28:19 +0900342 clearIptablesRestoreOutput();
343
344 // But if interfaces aren't paired, it's always an error.
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900345 counterLines.resize(3);
346 counters = Join(counterLines, "\n") + "\n";
347 addIptablesRestoreOutput(counters, counters);
Lorenzo Colitti5192bf72017-09-04 13:30:59 +0900348 result = mTetherCtrl.getTetherStats();
349 ASSERT_FALSE(isOk(result));
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900350 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 Colitti5192bf72017-09-04 13:30:59 +0900355 std::string err = result.status().msg();
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900356 ASSERT_LE(expectedError.size(), err.size());
357 EXPECT_TRUE(std::equal(expectedError.rbegin(), expectedError.rend(), err.rbegin()));
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900358}
359
360} // namespace net
361} // namespace android