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" |
Logan Chien | 3f46148 | 2018-04-23 14:31:32 +0800 | [diff] [blame] | 31 | #include <log/log.h> |
Lorenzo Colitti | bbeaf9a | 2016-07-08 18:24:26 +0900 | [diff] [blame] | 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++) { |
Yi Kong | bdfd57e | 2018-07-25 13:26:10 -0700 | [diff] [blame^] | 44 | if (argv[i] == nullptr) break; |
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 | bbeaf9a | 2016-07-08 18:24:26 +0900 | [diff] [blame] | 64 | FILE *IptablesBaseTest::fake_popen(const char * /* cmd */, const char *type) { |
| 65 | if (sPopenContents.empty() || strcmp(type, "r") != 0) { |
Yi Kong | bdfd57e | 2018-07-25 13:26:10 -0700 | [diff] [blame^] | 66 | return nullptr; |
Lorenzo Colitti | bbeaf9a | 2016-07-08 18:24:26 +0900 | [diff] [blame] | 67 | } |
| 68 | |
Lorenzo Colitti | c1306ea | 2017-03-27 05:52:31 +0900 | [diff] [blame] | 69 | std::string realCmd = StringPrintf("echo '%s'", sPopenContents.front().c_str()); |
Lorenzo Colitti | bbeaf9a | 2016-07-08 18:24:26 +0900 | [diff] [blame] | 70 | sPopenContents.pop_front(); |
Bernie Innocenti | 15bb55c | 2018-06-03 16:19:51 +0900 | [diff] [blame] | 71 | return popen(realCmd.c_str(), "r"); // NOLINT(cert-env33-c) |
Lorenzo Colitti | bbeaf9a | 2016-07-08 18:24:26 +0900 | [diff] [blame] | 72 | } |
| 73 | |
Lorenzo Colitti | cd28377 | 2017-01-31 19:00:49 +0900 | [diff] [blame] | 74 | int IptablesBaseTest::fakeExecIptablesRestoreWithOutput(IptablesTarget target, |
| 75 | const std::string& commands, |
| 76 | std::string *output) { |
Lorenzo Colitti | e60c0a5 | 2016-03-29 00:53:45 +0900 | [diff] [blame] | 77 | sRestoreCmds.push_back({ target, commands }); |
Lorenzo Colitti | cd28377 | 2017-01-31 19:00:49 +0900 | [diff] [blame] | 78 | if (output != nullptr) { |
| 79 | *output = sIptablesRestoreOutput.size() ? sIptablesRestoreOutput.front().c_str() : ""; |
| 80 | } |
| 81 | if (sIptablesRestoreOutput.size()) { |
| 82 | sIptablesRestoreOutput.pop_front(); |
| 83 | } |
Lorenzo Colitti | 0f15055 | 2016-03-28 02:30:27 +0900 | [diff] [blame] | 84 | return 0; |
| 85 | } |
| 86 | |
Lorenzo Colitti | cd28377 | 2017-01-31 19:00:49 +0900 | [diff] [blame] | 87 | int IptablesBaseTest::fakeExecIptablesRestore(IptablesTarget target, const std::string& commands) { |
| 88 | return fakeExecIptablesRestoreWithOutput(target, commands, nullptr); |
| 89 | } |
| 90 | |
Lorenzo Colitti | c1306ea | 2017-03-27 05:52:31 +0900 | [diff] [blame] | 91 | int IptablesBaseTest::fakeExecIptablesRestoreCommand(IptablesTarget target, |
| 92 | const std::string& table, |
| 93 | const std::string& command, |
| 94 | std::string *output) { |
| 95 | std::string fullCmd = StringPrintf("-t %s %s", table.c_str(), command.c_str()); |
| 96 | return fakeExecIptablesRestoreWithOutput(target, fullCmd, output); |
| 97 | } |
| 98 | |
Lorenzo Colitti | 0f15055 | 2016-03-28 02:30:27 +0900 | [diff] [blame] | 99 | void IptablesBaseTest::expectIptablesRestoreCommands(const std::vector<std::string>& expectedCmds) { |
Lorenzo Colitti | e60c0a5 | 2016-03-29 00:53:45 +0900 | [diff] [blame] | 100 | ExpectedIptablesCommands expected; |
Lorenzo Colitti | eb7eb3e | 2017-08-11 01:20:04 +0900 | [diff] [blame] | 101 | for (const auto& cmd : expectedCmds) { |
Lorenzo Colitti | e60c0a5 | 2016-03-29 00:53:45 +0900 | [diff] [blame] | 102 | expected.push_back({ V4V6, cmd }); |
| 103 | } |
| 104 | expectIptablesRestoreCommands(expected); |
| 105 | } |
| 106 | |
| 107 | void IptablesBaseTest::expectIptablesRestoreCommands(const ExpectedIptablesCommands& expectedCmds) { |
Lorenzo Colitti | 0f15055 | 2016-03-28 02:30:27 +0900 | [diff] [blame] | 108 | EXPECT_EQ(expectedCmds.size(), sRestoreCmds.size()); |
Lorenzo Colitti | e60c0a5 | 2016-03-29 00:53:45 +0900 | [diff] [blame] | 109 | for (size_t i = 0; i < expectedCmds.size(); i++) { |
| 110 | EXPECT_EQ(expectedCmds[i], sRestoreCmds[i]) << |
| 111 | "iptables-restore command " << i << " differs"; |
| 112 | } |
Lorenzo Colitti | 0f15055 | 2016-03-28 02:30:27 +0900 | [diff] [blame] | 113 | sRestoreCmds.clear(); |
| 114 | } |
| 115 | |
| 116 | std::vector<std::string> IptablesBaseTest::sCmds = {}; |
Lorenzo Colitti | e60c0a5 | 2016-03-29 00:53:45 +0900 | [diff] [blame] | 117 | IptablesBaseTest::ExpectedIptablesCommands IptablesBaseTest::sRestoreCmds = {}; |
Lorenzo Colitti | bbeaf9a | 2016-07-08 18:24:26 +0900 | [diff] [blame] | 118 | std::deque<std::string> IptablesBaseTest::sPopenContents = {}; |
Lorenzo Colitti | cd28377 | 2017-01-31 19:00:49 +0900 | [diff] [blame] | 119 | std::deque<std::string> IptablesBaseTest::sIptablesRestoreOutput = {}; |
Lorenzo Colitti | 849a11c | 2017-02-27 23:01:16 +0900 | [diff] [blame] | 120 | std::deque<int> IptablesBaseTest::sReturnValues = {}; |