blob: e6c7b47ab7f00c8f989df49d196b1bdb7b59302a [file] [log] [blame]
Lorenzo Colitti8e1cee92016-07-09 14:24:08 +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 * NatControllerTest.cpp - unit tests for NatController.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>
31
32#include "NatController.h"
33#include "IptablesBaseTest.h"
34
35using android::base::StringPrintf;
36
37class NatControllerTest : public IptablesBaseTest {
38public:
39 NatControllerTest() {
40 NatController::execFunction = fake_android_fork_exec;
41 }
42
43protected:
44 NatController mNatCtrl;
45
46 int setDefaults() {
47 return mNatCtrl.setDefaults();
48 }
49
50 const ExpectedIptablesCommands FLUSH_COMMANDS = {
Lorenzo Colitti05cfd252016-07-10 23:15:46 +090051 { V4V6, "-F natctrl_FORWARD" },
52 { V4, "-A natctrl_FORWARD -j DROP" },
53 { V4, "-t nat -F natctrl_nat_POSTROUTING" },
Lorenzo Colitti8e1cee92016-07-09 14:24:08 +090054 };
55
56 const ExpectedIptablesCommands SETUP_COMMANDS = {
Lorenzo Colitti05cfd252016-07-10 23:15:46 +090057 { V4V6, "-F natctrl_FORWARD" },
58 { V4, "-A natctrl_FORWARD -j DROP" },
59 { V4, "-t nat -F natctrl_nat_POSTROUTING" },
60 { V4V6, "-F natctrl_tether_counters" },
61 { V4V6, "-X natctrl_tether_counters" },
62 { V4V6, "-N natctrl_tether_counters" },
63 { V4, "-t mangle -A natctrl_mangle_FORWARD -p tcp --tcp-flags SYN SYN "
64 "-j TCPMSS --clamp-mss-to-pmtu" },
Lorenzo Colitti8e1cee92016-07-09 14:24:08 +090065 };
66
67 const ExpectedIptablesCommands TWIDDLE_COMMANDS = {
68 { V4, "-D natctrl_FORWARD -j DROP" },
69 { V4, "-A natctrl_FORWARD -j DROP" },
70 };
71
Lorenzo Colitti05cfd252016-07-10 23:15:46 +090072 ExpectedIptablesCommands firstNatCommands(const char *extIf) {
Lorenzo Colitti8e1cee92016-07-09 14:24:08 +090073 return {
74 { V4, StringPrintf("-t nat -A natctrl_nat_POSTROUTING -o %s -j MASQUERADE", extIf) },
Lorenzo Colitti05cfd252016-07-10 23:15:46 +090075 { V6, "-A natctrl_FORWARD -g natctrl_tether_counters" },
Lorenzo Colitti8e1cee92016-07-09 14:24:08 +090076 };
77 }
78
79 ExpectedIptablesCommands startNatCommands(const char *intIf, const char *extIf) {
80 return {
Lorenzo Colitti05cfd252016-07-10 23:15:46 +090081 { V4, StringPrintf("-A natctrl_FORWARD -i %s -o %s -m state --state"
82 " ESTABLISHED,RELATED -g natctrl_tether_counters", extIf, intIf) },
83 { V4, StringPrintf("-A natctrl_FORWARD -i %s -o %s -m state --state INVALID -j DROP",
84 intIf, extIf) },
85 { V4, StringPrintf("-A natctrl_FORWARD -i %s -o %s -g natctrl_tether_counters",
86 intIf, extIf) },
87 { V4V6, StringPrintf("-A natctrl_tether_counters -i %s -o %s -j RETURN",
88 intIf, extIf) },
89 { V4V6, StringPrintf("-A natctrl_tether_counters -i %s -o %s -j RETURN",
90 extIf, intIf) },
Lorenzo Colitti8e1cee92016-07-09 14:24:08 +090091 };
92 }
93
94 ExpectedIptablesCommands stopNatCommands(const char *intIf, const char *extIf) {
95 return {
96 { V4, StringPrintf("-D natctrl_FORWARD -i %s -o %s -m state --state"
97 " ESTABLISHED,RELATED -g natctrl_tether_counters", extIf, intIf) },
98 { V4, StringPrintf("-D natctrl_FORWARD -i %s -o %s -m state --state INVALID -j DROP",
99 intIf, extIf) },
100 { V4, StringPrintf("-D natctrl_FORWARD -i %s -o %s -g natctrl_tether_counters",
101 intIf, extIf) },
102 };
103 }
104};
105
106TEST_F(NatControllerTest, TestSetupIptablesHooks) {
107 mNatCtrl.setupIptablesHooks();
108 expectIptablesCommands(SETUP_COMMANDS);
109}
110
111TEST_F(NatControllerTest, TestSetDefaults) {
112 setDefaults();
113 expectIptablesCommands(FLUSH_COMMANDS);
114}
115
116TEST_F(NatControllerTest, TestAddAndRemoveNat) {
117
118 std::vector<ExpectedIptablesCommands> startFirstNat = {
Lorenzo Colitti05cfd252016-07-10 23:15:46 +0900119 firstNatCommands("rmnet0"),
Lorenzo Colitti8e1cee92016-07-09 14:24:08 +0900120 startNatCommands("wlan0", "rmnet0"),
121 TWIDDLE_COMMANDS,
122 };
123 mNatCtrl.enableNat("wlan0", "rmnet0");
124 expectIptablesCommands(startFirstNat);
125
126 std::vector<ExpectedIptablesCommands> startOtherNat = {
127 startNatCommands("usb0", "rmnet0"),
128 TWIDDLE_COMMANDS,
129 };
130 mNatCtrl.enableNat("usb0", "rmnet0");
131 expectIptablesCommands(startOtherNat);
132
133 ExpectedIptablesCommands stopOtherNat = stopNatCommands("wlan0", "rmnet0");
134 mNatCtrl.disableNat("wlan0", "rmnet0");
135 expectIptablesCommands(stopOtherNat);
136
137 std::vector<ExpectedIptablesCommands> stopLastNat = {
138 stopNatCommands("usb0", "rmnet0"),
139 FLUSH_COMMANDS,
140 };
141 mNatCtrl.disableNat("usb0", "rmnet0");
142 expectIptablesCommands(stopLastNat);
143}