blob: f879fe674ccbfd96be149b98565f1e0f61b07168 [file] [log] [blame]
Lorenzo Colitti0f150552016-03-28 02:30:27 +09001/*
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 Colittibbeaf9a2016-07-08 18:24:26 +090019#include <deque>
Lorenzo Colitti0f150552016-03-28 02:30:27 +090020#include <string>
21#include <vector>
22
23#include <gtest/gtest.h>
24
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +090025#include <android-base/stringprintf.h>
26
Lorenzo Colitti0f150552016-03-28 02:30:27 +090027#include "IptablesBaseTest.h"
28#include "NetdConstants.h"
29
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +090030#define LOG_TAG "IptablesBaseTest"
31#include <cutils/log.h>
32
Lorenzo Colitti0f150552016-03-28 02:30:27 +090033IptablesBaseTest::IptablesBaseTest() {
34 sCmds.clear();
35 sRestoreCmds.clear();
Lorenzo Colitti849a11c2017-02-27 23:01:16 +090036 sReturnValues.clear();
Lorenzo Colitti0f150552016-03-28 02:30:27 +090037}
38
39int 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 Colitti8e1cee92016-07-09 14:24:08 +090042 if (argv[i] == NULL) break; // NatController likes to pass in invalid argc values.
Lorenzo Colitti0f150552016-03-28 02:30:27 +090043 cmd += " ";
44 cmd += argv[i];
45 }
46 sCmds.push_back(cmd);
Lorenzo Colitti849a11c2017-02-27 23:01:16 +090047
48 int ret;
49 if (sReturnValues.size()) {
50 ret = sReturnValues.front();
51 sReturnValues.pop_front();
52 } else {
53 ret = 0;
Lorenzo Colitti8e1cee92016-07-09 14:24:08 +090054 }
Lorenzo Colitti849a11c2017-02-27 23:01:16 +090055
56 if (status) {
57 *status = ret;
58 }
59 return ret;
Lorenzo Colitti0f150552016-03-28 02:30:27 +090060}
61
Lorenzo Colitti9028d912016-03-28 02:34:54 +090062int 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 Colittibbeaf9a2016-07-08 18:24:26 +090085FILE *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 Colitticd283772017-01-31 19:00:49 +090095int IptablesBaseTest::fakeExecIptablesRestoreWithOutput(IptablesTarget target,
96 const std::string& commands,
97 std::string *output) {
Lorenzo Colittie60c0a52016-03-29 00:53:45 +090098 sRestoreCmds.push_back({ target, commands });
Lorenzo Colitticd283772017-01-31 19:00:49 +090099 if (output != nullptr) {
100 *output = sIptablesRestoreOutput.size() ? sIptablesRestoreOutput.front().c_str() : "";
101 }
102 if (sIptablesRestoreOutput.size()) {
103 sIptablesRestoreOutput.pop_front();
104 }
Lorenzo Colitti0f150552016-03-28 02:30:27 +0900105 return 0;
106}
107
Lorenzo Colitticd283772017-01-31 19:00:49 +0900108int IptablesBaseTest::fakeExecIptablesRestore(IptablesTarget target, const std::string& commands) {
109 return fakeExecIptablesRestoreWithOutput(target, commands, nullptr);
110}
111
Lorenzo Colitti9028d912016-03-28 02:34:54 +0900112int IptablesBaseTest::expectIptablesCommand(IptablesTarget target, int pos,
113 const std::string& cmd) {
Lorenzo Colitti54ecf162016-05-13 16:57:15 +0900114
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 Colitti9028d912016-03-28 02:34:54 +0900121 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 Colitti54ecf162016-05-13 16:57:15 +0900127
Lorenzo Colitti9028d912016-03-28 02:34:54 +0900128 return target == V4V6 ? 2 : 1;
129}
Lorenzo Colitti0f150552016-03-28 02:30:27 +0900130
Lorenzo Colitti9028d912016-03-28 02:34:54 +0900131void 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
139void IptablesBaseTest::expectIptablesCommands(const ExpectedIptablesCommands& expectedCmds) {
140 size_t pos = 0;
Lorenzo Colitti0f150552016-03-28 02:30:27 +0900141 for (size_t i = 0; i < expectedCmds.size(); i ++) {
Lorenzo Colitti9028d912016-03-28 02:34:54 +0900142 auto target = expectedCmds[i].first;
143 auto cmd = expectedCmds[i].second;
Lorenzo Colitti54ecf162016-05-13 16:57:15 +0900144 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 Colitti0f150552016-03-28 02:30:27 +0900150 }
151
Lorenzo Colitti9028d912016-03-28 02:34:54 +0900152 EXPECT_EQ(pos, sCmds.size());
Lorenzo Colitti0f150552016-03-28 02:30:27 +0900153 sCmds.clear();
154}
155
Lorenzo Colitti8e1cee92016-07-09 14:24:08 +0900156void 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 Colitti0f150552016-03-28 02:30:27 +0900165void IptablesBaseTest::expectIptablesRestoreCommands(const std::vector<std::string>& expectedCmds) {
Lorenzo Colittie60c0a52016-03-29 00:53:45 +0900166 ExpectedIptablesCommands expected;
167 for (auto cmd : expectedCmds) {
168 expected.push_back({ V4V6, cmd });
169 }
170 expectIptablesRestoreCommands(expected);
171}
172
173void IptablesBaseTest::expectIptablesRestoreCommands(const ExpectedIptablesCommands& expectedCmds) {
Lorenzo Colitti0f150552016-03-28 02:30:27 +0900174 EXPECT_EQ(expectedCmds.size(), sRestoreCmds.size());
Lorenzo Colittie60c0a52016-03-29 00:53:45 +0900175 for (size_t i = 0; i < expectedCmds.size(); i++) {
176 EXPECT_EQ(expectedCmds[i], sRestoreCmds[i]) <<
177 "iptables-restore command " << i << " differs";
178 }
Lorenzo Colitti0f150552016-03-28 02:30:27 +0900179 sRestoreCmds.clear();
180}
181
Lorenzo Colitti849a11c2017-02-27 23:01:16 +0900182void IptablesBaseTest::setReturnValues(const std::deque<int>& returnValues) {
183 sReturnValues = returnValues;
184}
185
Lorenzo Colitti0f150552016-03-28 02:30:27 +0900186std::vector<std::string> IptablesBaseTest::sCmds = {};
Lorenzo Colittie60c0a52016-03-29 00:53:45 +0900187IptablesBaseTest::ExpectedIptablesCommands IptablesBaseTest::sRestoreCmds = {};
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900188std::deque<std::string> IptablesBaseTest::sPopenContents = {};
Lorenzo Colitticd283772017-01-31 19:00:49 +0900189std::deque<std::string> IptablesBaseTest::sIptablesRestoreOutput = {};
Lorenzo Colitti849a11c2017-02-27 23:01:16 +0900190std::deque<int> IptablesBaseTest::sReturnValues = {};