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