Lorenzo Colitti | 0f15055 | 2016-03-28 02:30:27 +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 | * IptablesBaseTest.cpp - utility class for tests that use iptables |
| 17 | */ |
| 18 | |
Lorenzo Colitti | bbeaf9a | 2016-07-08 18:24:26 +0900 | [diff] [blame] | 19 | #include <deque> |
Lorenzo Colitti | 0f15055 | 2016-03-28 02:30:27 +0900 | [diff] [blame] | 20 | #include <string> |
| 21 | #include <vector> |
| 22 | |
| 23 | #include <gtest/gtest.h> |
| 24 | |
Lorenzo Colitti | bbeaf9a | 2016-07-08 18:24:26 +0900 | [diff] [blame] | 25 | #include <android-base/stringprintf.h> |
| 26 | |
Lorenzo Colitti | 0f15055 | 2016-03-28 02:30:27 +0900 | [diff] [blame] | 27 | #include "IptablesBaseTest.h" |
| 28 | #include "NetdConstants.h" |
| 29 | |
Lorenzo Colitti | bbeaf9a | 2016-07-08 18:24:26 +0900 | [diff] [blame] | 30 | #define LOG_TAG "IptablesBaseTest" |
| 31 | #include <cutils/log.h> |
| 32 | |
Lorenzo Colitti | c1306ea | 2017-03-27 05:52:31 +0900 | [diff] [blame] | 33 | using android::base::StringPrintf; |
| 34 | |
Lorenzo Colitti | 0f15055 | 2016-03-28 02:30:27 +0900 | [diff] [blame] | 35 | IptablesBaseTest::IptablesBaseTest() { |
| 36 | sCmds.clear(); |
| 37 | sRestoreCmds.clear(); |
Lorenzo Colitti | 849a11c | 2017-02-27 23:01:16 +0900 | [diff] [blame] | 38 | sReturnValues.clear(); |
Lorenzo Colitti | 0f15055 | 2016-03-28 02:30:27 +0900 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | int IptablesBaseTest::fake_android_fork_exec(int argc, char* argv[], int *status, bool, bool) { |
| 42 | std::string cmd = argv[0]; |
| 43 | for (int i = 1; i < argc; i++) { |
Lorenzo Colitti | 8e1cee9 | 2016-07-09 14:24:08 +0900 | [diff] [blame] | 44 | if (argv[i] == NULL) break; // NatController likes to pass in invalid argc values. |
Lorenzo Colitti | 0f15055 | 2016-03-28 02:30:27 +0900 | [diff] [blame] | 45 | cmd += " "; |
| 46 | cmd += argv[i]; |
| 47 | } |
| 48 | sCmds.push_back(cmd); |
Lorenzo Colitti | 849a11c | 2017-02-27 23:01:16 +0900 | [diff] [blame] | 49 | |
| 50 | int ret; |
| 51 | if (sReturnValues.size()) { |
| 52 | ret = sReturnValues.front(); |
| 53 | sReturnValues.pop_front(); |
| 54 | } else { |
| 55 | ret = 0; |
Lorenzo Colitti | 8e1cee9 | 2016-07-09 14:24:08 +0900 | [diff] [blame] | 56 | } |
Lorenzo Colitti | 849a11c | 2017-02-27 23:01:16 +0900 | [diff] [blame] | 57 | |
| 58 | if (status) { |
| 59 | *status = ret; |
| 60 | } |
| 61 | return ret; |
Lorenzo Colitti | 0f15055 | 2016-03-28 02:30:27 +0900 | [diff] [blame] | 62 | } |
| 63 | |
Lorenzo Colitti | 9028d91 | 2016-03-28 02:34:54 +0900 | [diff] [blame] | 64 | int IptablesBaseTest::fakeExecIptables(IptablesTarget target, ...) { |
| 65 | std::string cmd = " -w"; |
| 66 | va_list args; |
| 67 | va_start(args, target); |
| 68 | const char *arg; |
| 69 | do { |
| 70 | arg = va_arg(args, const char *); |
| 71 | if (arg != nullptr) { |
| 72 | cmd += " "; |
| 73 | cmd += arg; |
| 74 | } |
| 75 | } while (arg); |
Mikhail Lappo | f7f2dc0 | 2017-03-23 21:58:45 +0100 | [diff] [blame] | 76 | va_end(args); |
Lorenzo Colitti | 9028d91 | 2016-03-28 02:34:54 +0900 | [diff] [blame] | 77 | |
| 78 | if (target == V4 || target == V4V6) { |
| 79 | sCmds.push_back(IPTABLES_PATH + cmd); |
| 80 | } |
| 81 | if (target == V6 || target == V4V6) { |
| 82 | sCmds.push_back(IP6TABLES_PATH + cmd); |
| 83 | } |
| 84 | |
| 85 | return 0; |
| 86 | } |
| 87 | |
Lorenzo Colitti | bbeaf9a | 2016-07-08 18:24:26 +0900 | [diff] [blame] | 88 | FILE *IptablesBaseTest::fake_popen(const char * /* cmd */, const char *type) { |
| 89 | if (sPopenContents.empty() || strcmp(type, "r") != 0) { |
| 90 | return NULL; |
| 91 | } |
| 92 | |
Lorenzo Colitti | c1306ea | 2017-03-27 05:52:31 +0900 | [diff] [blame] | 93 | std::string realCmd = StringPrintf("echo '%s'", sPopenContents.front().c_str()); |
Lorenzo Colitti | bbeaf9a | 2016-07-08 18:24:26 +0900 | [diff] [blame] | 94 | sPopenContents.pop_front(); |
| 95 | return popen(realCmd.c_str(), "r"); |
| 96 | } |
| 97 | |
Lorenzo Colitti | cd28377 | 2017-01-31 19:00:49 +0900 | [diff] [blame] | 98 | int IptablesBaseTest::fakeExecIptablesRestoreWithOutput(IptablesTarget target, |
| 99 | const std::string& commands, |
| 100 | std::string *output) { |
Lorenzo Colitti | e60c0a5 | 2016-03-29 00:53:45 +0900 | [diff] [blame] | 101 | sRestoreCmds.push_back({ target, commands }); |
Lorenzo Colitti | cd28377 | 2017-01-31 19:00:49 +0900 | [diff] [blame] | 102 | if (output != nullptr) { |
| 103 | *output = sIptablesRestoreOutput.size() ? sIptablesRestoreOutput.front().c_str() : ""; |
| 104 | } |
| 105 | if (sIptablesRestoreOutput.size()) { |
| 106 | sIptablesRestoreOutput.pop_front(); |
| 107 | } |
Lorenzo Colitti | 0f15055 | 2016-03-28 02:30:27 +0900 | [diff] [blame] | 108 | return 0; |
| 109 | } |
| 110 | |
Lorenzo Colitti | cd28377 | 2017-01-31 19:00:49 +0900 | [diff] [blame] | 111 | int IptablesBaseTest::fakeExecIptablesRestore(IptablesTarget target, const std::string& commands) { |
| 112 | return fakeExecIptablesRestoreWithOutput(target, commands, nullptr); |
| 113 | } |
| 114 | |
Lorenzo Colitti | c1306ea | 2017-03-27 05:52:31 +0900 | [diff] [blame] | 115 | int IptablesBaseTest::fakeExecIptablesRestoreCommand(IptablesTarget target, |
| 116 | const std::string& table, |
| 117 | const std::string& command, |
| 118 | std::string *output) { |
| 119 | std::string fullCmd = StringPrintf("-t %s %s", table.c_str(), command.c_str()); |
| 120 | return fakeExecIptablesRestoreWithOutput(target, fullCmd, output); |
| 121 | } |
| 122 | |
Lorenzo Colitti | 9028d91 | 2016-03-28 02:34:54 +0900 | [diff] [blame] | 123 | int IptablesBaseTest::expectIptablesCommand(IptablesTarget target, int pos, |
| 124 | const std::string& cmd) { |
Lorenzo Colitti | 54ecf16 | 2016-05-13 16:57:15 +0900 | [diff] [blame] | 125 | |
| 126 | if ((unsigned) pos >= sCmds.size()) { |
| 127 | ADD_FAILURE() << "Expected too many iptables commands, want command " |
| 128 | << pos + 1 << "/" << sCmds.size(); |
| 129 | return -1; |
| 130 | } |
| 131 | |
Lorenzo Colitti | 9028d91 | 2016-03-28 02:34:54 +0900 | [diff] [blame] | 132 | if (target == V4 || target == V4V6) { |
| 133 | EXPECT_EQ("/system/bin/iptables -w " + cmd, sCmds[pos++]); |
| 134 | } |
| 135 | if (target == V6 || target == V4V6) { |
| 136 | EXPECT_EQ("/system/bin/ip6tables -w " + cmd, sCmds[pos++]); |
| 137 | } |
Lorenzo Colitti | 54ecf16 | 2016-05-13 16:57:15 +0900 | [diff] [blame] | 138 | |
Lorenzo Colitti | 9028d91 | 2016-03-28 02:34:54 +0900 | [diff] [blame] | 139 | return target == V4V6 ? 2 : 1; |
| 140 | } |
Lorenzo Colitti | 0f15055 | 2016-03-28 02:30:27 +0900 | [diff] [blame] | 141 | |
Lorenzo Colitti | 9028d91 | 2016-03-28 02:34:54 +0900 | [diff] [blame] | 142 | void IptablesBaseTest::expectIptablesCommands(const std::vector<std::string>& expectedCmds) { |
| 143 | ExpectedIptablesCommands expected; |
Lorenzo Colitti | eb7eb3e | 2017-08-11 01:20:04 +0900 | [diff] [blame^] | 144 | for (const auto& cmd : expectedCmds) { |
Lorenzo Colitti | 9028d91 | 2016-03-28 02:34:54 +0900 | [diff] [blame] | 145 | expected.push_back({ V4V6, cmd }); |
| 146 | } |
| 147 | expectIptablesCommands(expected); |
| 148 | } |
| 149 | |
| 150 | void IptablesBaseTest::expectIptablesCommands(const ExpectedIptablesCommands& expectedCmds) { |
| 151 | size_t pos = 0; |
Lorenzo Colitti | 0f15055 | 2016-03-28 02:30:27 +0900 | [diff] [blame] | 152 | for (size_t i = 0; i < expectedCmds.size(); i ++) { |
Lorenzo Colitti | eb7eb3e | 2017-08-11 01:20:04 +0900 | [diff] [blame^] | 153 | const auto& target = expectedCmds[i].first; |
| 154 | const auto& cmd = expectedCmds[i].second; |
Lorenzo Colitti | 54ecf16 | 2016-05-13 16:57:15 +0900 | [diff] [blame] | 155 | int numConsumed = expectIptablesCommand(target, pos, cmd); |
| 156 | if (numConsumed < 0) { |
| 157 | // Read past the end of the array. |
| 158 | break; |
| 159 | } |
| 160 | pos += numConsumed; |
Lorenzo Colitti | 0f15055 | 2016-03-28 02:30:27 +0900 | [diff] [blame] | 161 | } |
| 162 | |
Lorenzo Colitti | 9028d91 | 2016-03-28 02:34:54 +0900 | [diff] [blame] | 163 | EXPECT_EQ(pos, sCmds.size()); |
Lorenzo Colitti | 0f15055 | 2016-03-28 02:30:27 +0900 | [diff] [blame] | 164 | sCmds.clear(); |
| 165 | } |
| 166 | |
Lorenzo Colitti | 8e1cee9 | 2016-07-09 14:24:08 +0900 | [diff] [blame] | 167 | void IptablesBaseTest::expectIptablesCommands( |
| 168 | const std::vector<ExpectedIptablesCommands>& snippets) { |
| 169 | ExpectedIptablesCommands expected; |
| 170 | for (const auto& snippet: snippets) { |
| 171 | expected.insert(expected.end(), snippet.begin(), snippet.end()); |
| 172 | } |
| 173 | expectIptablesCommands(expected); |
| 174 | } |
| 175 | |
Lorenzo Colitti | 0f15055 | 2016-03-28 02:30:27 +0900 | [diff] [blame] | 176 | void IptablesBaseTest::expectIptablesRestoreCommands(const std::vector<std::string>& expectedCmds) { |
Lorenzo Colitti | e60c0a5 | 2016-03-29 00:53:45 +0900 | [diff] [blame] | 177 | ExpectedIptablesCommands expected; |
Lorenzo Colitti | eb7eb3e | 2017-08-11 01:20:04 +0900 | [diff] [blame^] | 178 | for (const auto& cmd : expectedCmds) { |
Lorenzo Colitti | e60c0a5 | 2016-03-29 00:53:45 +0900 | [diff] [blame] | 179 | expected.push_back({ V4V6, cmd }); |
| 180 | } |
| 181 | expectIptablesRestoreCommands(expected); |
| 182 | } |
| 183 | |
| 184 | void IptablesBaseTest::expectIptablesRestoreCommands(const ExpectedIptablesCommands& expectedCmds) { |
Lorenzo Colitti | 0f15055 | 2016-03-28 02:30:27 +0900 | [diff] [blame] | 185 | EXPECT_EQ(expectedCmds.size(), sRestoreCmds.size()); |
Lorenzo Colitti | e60c0a5 | 2016-03-29 00:53:45 +0900 | [diff] [blame] | 186 | for (size_t i = 0; i < expectedCmds.size(); i++) { |
| 187 | EXPECT_EQ(expectedCmds[i], sRestoreCmds[i]) << |
| 188 | "iptables-restore command " << i << " differs"; |
| 189 | } |
Lorenzo Colitti | 0f15055 | 2016-03-28 02:30:27 +0900 | [diff] [blame] | 190 | sRestoreCmds.clear(); |
| 191 | } |
| 192 | |
Lorenzo Colitti | 849a11c | 2017-02-27 23:01:16 +0900 | [diff] [blame] | 193 | void IptablesBaseTest::setReturnValues(const std::deque<int>& returnValues) { |
| 194 | sReturnValues = returnValues; |
| 195 | } |
| 196 | |
Lorenzo Colitti | 0f15055 | 2016-03-28 02:30:27 +0900 | [diff] [blame] | 197 | std::vector<std::string> IptablesBaseTest::sCmds = {}; |
Lorenzo Colitti | e60c0a5 | 2016-03-29 00:53:45 +0900 | [diff] [blame] | 198 | IptablesBaseTest::ExpectedIptablesCommands IptablesBaseTest::sRestoreCmds = {}; |
Lorenzo Colitti | bbeaf9a | 2016-07-08 18:24:26 +0900 | [diff] [blame] | 199 | std::deque<std::string> IptablesBaseTest::sPopenContents = {}; |
Lorenzo Colitti | cd28377 | 2017-01-31 19:00:49 +0900 | [diff] [blame] | 200 | std::deque<std::string> IptablesBaseTest::sIptablesRestoreOutput = {}; |
Lorenzo Colitti | 849a11c | 2017-02-27 23:01:16 +0900 | [diff] [blame] | 201 | std::deque<int> IptablesBaseTest::sReturnValues = {}; |