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