Lorenzo Colitti | 8e1cee9 | 2016-07-09 14:24:08 +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 | * 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 | |
| 35 | using android::base::StringPrintf; |
| 36 | |
| 37 | class NatControllerTest : public IptablesBaseTest { |
| 38 | public: |
| 39 | NatControllerTest() { |
| 40 | NatController::execFunction = fake_android_fork_exec; |
| 41 | } |
| 42 | |
| 43 | protected: |
| 44 | NatController mNatCtrl; |
| 45 | |
| 46 | int setDefaults() { |
| 47 | return mNatCtrl.setDefaults(); |
| 48 | } |
| 49 | |
| 50 | const ExpectedIptablesCommands FLUSH_COMMANDS = { |
| 51 | { V4, "-F natctrl_FORWARD" }, |
| 52 | { V4, "-A natctrl_FORWARD -j DROP" }, |
| 53 | { V4, "-t nat -F natctrl_nat_POSTROUTING" }, |
| 54 | }; |
| 55 | |
| 56 | const ExpectedIptablesCommands SETUP_COMMANDS = { |
| 57 | { V4, "-F natctrl_FORWARD" }, |
| 58 | { V4, "-A natctrl_FORWARD -j DROP" }, |
| 59 | { V4, "-t nat -F natctrl_nat_POSTROUTING" }, |
| 60 | { V4, "-F natctrl_tether_counters" }, |
| 61 | { V4, "-X natctrl_tether_counters" }, |
| 62 | { V4, "-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" }, |
| 65 | }; |
| 66 | |
| 67 | const ExpectedIptablesCommands TWIDDLE_COMMANDS = { |
| 68 | { V4, "-D natctrl_FORWARD -j DROP" }, |
| 69 | { V4, "-A natctrl_FORWARD -j DROP" }, |
| 70 | }; |
| 71 | |
| 72 | ExpectedIptablesCommands enableMasqueradeCommand(const char *extIf) { |
| 73 | return { |
| 74 | { V4, StringPrintf("-t nat -A natctrl_nat_POSTROUTING -o %s -j MASQUERADE", extIf) }, |
| 75 | }; |
| 76 | } |
| 77 | |
| 78 | ExpectedIptablesCommands startNatCommands(const char *intIf, const char *extIf) { |
| 79 | return { |
| 80 | { V4, StringPrintf("-A natctrl_FORWARD -i %s -o %s -m state --state" |
| 81 | " ESTABLISHED,RELATED -g natctrl_tether_counters", extIf, intIf) }, |
| 82 | { V4, StringPrintf("-A natctrl_FORWARD -i %s -o %s -m state --state INVALID -j DROP", |
| 83 | intIf, extIf) }, |
| 84 | { V4, StringPrintf("-A natctrl_FORWARD -i %s -o %s -g natctrl_tether_counters", |
| 85 | intIf, extIf) }, |
| 86 | { V4, StringPrintf("-A natctrl_tether_counters -i %s -o %s -j RETURN", intIf, extIf) }, |
| 87 | { V4, StringPrintf("-A natctrl_tether_counters -i %s -o %s -j RETURN", extIf, intIf) }, |
| 88 | }; |
| 89 | } |
| 90 | |
| 91 | ExpectedIptablesCommands stopNatCommands(const char *intIf, const char *extIf) { |
| 92 | return { |
| 93 | { V4, StringPrintf("-D natctrl_FORWARD -i %s -o %s -m state --state" |
| 94 | " ESTABLISHED,RELATED -g natctrl_tether_counters", extIf, intIf) }, |
| 95 | { V4, StringPrintf("-D natctrl_FORWARD -i %s -o %s -m state --state INVALID -j DROP", |
| 96 | intIf, extIf) }, |
| 97 | { V4, StringPrintf("-D natctrl_FORWARD -i %s -o %s -g natctrl_tether_counters", |
| 98 | intIf, extIf) }, |
| 99 | }; |
| 100 | } |
| 101 | }; |
| 102 | |
| 103 | TEST_F(NatControllerTest, TestSetupIptablesHooks) { |
| 104 | mNatCtrl.setupIptablesHooks(); |
| 105 | expectIptablesCommands(SETUP_COMMANDS); |
| 106 | } |
| 107 | |
| 108 | TEST_F(NatControllerTest, TestSetDefaults) { |
| 109 | setDefaults(); |
| 110 | expectIptablesCommands(FLUSH_COMMANDS); |
| 111 | } |
| 112 | |
| 113 | TEST_F(NatControllerTest, TestAddAndRemoveNat) { |
| 114 | |
| 115 | std::vector<ExpectedIptablesCommands> startFirstNat = { |
| 116 | enableMasqueradeCommand("rmnet0"), |
| 117 | startNatCommands("wlan0", "rmnet0"), |
| 118 | TWIDDLE_COMMANDS, |
| 119 | }; |
| 120 | mNatCtrl.enableNat("wlan0", "rmnet0"); |
| 121 | expectIptablesCommands(startFirstNat); |
| 122 | |
| 123 | std::vector<ExpectedIptablesCommands> startOtherNat = { |
| 124 | startNatCommands("usb0", "rmnet0"), |
| 125 | TWIDDLE_COMMANDS, |
| 126 | }; |
| 127 | mNatCtrl.enableNat("usb0", "rmnet0"); |
| 128 | expectIptablesCommands(startOtherNat); |
| 129 | |
| 130 | ExpectedIptablesCommands stopOtherNat = stopNatCommands("wlan0", "rmnet0"); |
| 131 | mNatCtrl.disableNat("wlan0", "rmnet0"); |
| 132 | expectIptablesCommands(stopOtherNat); |
| 133 | |
| 134 | std::vector<ExpectedIptablesCommands> stopLastNat = { |
| 135 | stopNatCommands("usb0", "rmnet0"), |
| 136 | FLUSH_COMMANDS, |
| 137 | }; |
| 138 | mNatCtrl.disableNat("usb0", "rmnet0"); |
| 139 | expectIptablesCommands(stopLastNat); |
| 140 | } |