blob: 3179149d5944ad5e5caeac25bee12f4545b6047e [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);
Mikhail Lappof7f2dc02017-03-23 21:58:45 +010074 va_end(args);
Lorenzo Colitti9028d912016-03-28 02:34:54 +090075
76 if (target == V4 || target == V4V6) {
77 sCmds.push_back(IPTABLES_PATH + cmd);
78 }
79 if (target == V6 || target == V4V6) {
80 sCmds.push_back(IP6TABLES_PATH + cmd);
81 }
82
83 return 0;
84}
85
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +090086FILE *IptablesBaseTest::fake_popen(const char * /* cmd */, const char *type) {
87 if (sPopenContents.empty() || strcmp(type, "r") != 0) {
88 return NULL;
89 }
90
91 std::string realCmd = android::base::StringPrintf("echo '%s'", sPopenContents.front().c_str());
92 sPopenContents.pop_front();
93 return popen(realCmd.c_str(), "r");
94}
95
Lorenzo Colitticd283772017-01-31 19:00:49 +090096int IptablesBaseTest::fakeExecIptablesRestoreWithOutput(IptablesTarget target,
97 const std::string& commands,
98 std::string *output) {
Lorenzo Colittie60c0a52016-03-29 00:53:45 +090099 sRestoreCmds.push_back({ target, commands });
Lorenzo Colitticd283772017-01-31 19:00:49 +0900100 if (output != nullptr) {
101 *output = sIptablesRestoreOutput.size() ? sIptablesRestoreOutput.front().c_str() : "";
102 }
103 if (sIptablesRestoreOutput.size()) {
104 sIptablesRestoreOutput.pop_front();
105 }
Lorenzo Colitti0f150552016-03-28 02:30:27 +0900106 return 0;
107}
108
Lorenzo Colitticd283772017-01-31 19:00:49 +0900109int IptablesBaseTest::fakeExecIptablesRestore(IptablesTarget target, const std::string& commands) {
110 return fakeExecIptablesRestoreWithOutput(target, commands, nullptr);
111}
112
Lorenzo Colitti9028d912016-03-28 02:34:54 +0900113int IptablesBaseTest::expectIptablesCommand(IptablesTarget target, int pos,
114 const std::string& cmd) {
Lorenzo Colitti54ecf162016-05-13 16:57:15 +0900115
116 if ((unsigned) pos >= sCmds.size()) {
117 ADD_FAILURE() << "Expected too many iptables commands, want command "
118 << pos + 1 << "/" << sCmds.size();
119 return -1;
120 }
121
Lorenzo Colitti9028d912016-03-28 02:34:54 +0900122 if (target == V4 || target == V4V6) {
123 EXPECT_EQ("/system/bin/iptables -w " + cmd, sCmds[pos++]);
124 }
125 if (target == V6 || target == V4V6) {
126 EXPECT_EQ("/system/bin/ip6tables -w " + cmd, sCmds[pos++]);
127 }
Lorenzo Colitti54ecf162016-05-13 16:57:15 +0900128
Lorenzo Colitti9028d912016-03-28 02:34:54 +0900129 return target == V4V6 ? 2 : 1;
130}
Lorenzo Colitti0f150552016-03-28 02:30:27 +0900131
Lorenzo Colitti9028d912016-03-28 02:34:54 +0900132void IptablesBaseTest::expectIptablesCommands(const std::vector<std::string>& expectedCmds) {
133 ExpectedIptablesCommands expected;
134 for (auto cmd : expectedCmds) {
135 expected.push_back({ V4V6, cmd });
136 }
137 expectIptablesCommands(expected);
138}
139
140void IptablesBaseTest::expectIptablesCommands(const ExpectedIptablesCommands& expectedCmds) {
141 size_t pos = 0;
Lorenzo Colitti0f150552016-03-28 02:30:27 +0900142 for (size_t i = 0; i < expectedCmds.size(); i ++) {
Lorenzo Colitti9028d912016-03-28 02:34:54 +0900143 auto target = expectedCmds[i].first;
144 auto cmd = expectedCmds[i].second;
Lorenzo Colitti54ecf162016-05-13 16:57:15 +0900145 int numConsumed = expectIptablesCommand(target, pos, cmd);
146 if (numConsumed < 0) {
147 // Read past the end of the array.
148 break;
149 }
150 pos += numConsumed;
Lorenzo Colitti0f150552016-03-28 02:30:27 +0900151 }
152
Lorenzo Colitti9028d912016-03-28 02:34:54 +0900153 EXPECT_EQ(pos, sCmds.size());
Lorenzo Colitti0f150552016-03-28 02:30:27 +0900154 sCmds.clear();
155}
156
Lorenzo Colitti8e1cee92016-07-09 14:24:08 +0900157void IptablesBaseTest::expectIptablesCommands(
158 const std::vector<ExpectedIptablesCommands>& snippets) {
159 ExpectedIptablesCommands expected;
160 for (const auto& snippet: snippets) {
161 expected.insert(expected.end(), snippet.begin(), snippet.end());
162 }
163 expectIptablesCommands(expected);
164}
165
Lorenzo Colitti0f150552016-03-28 02:30:27 +0900166void IptablesBaseTest::expectIptablesRestoreCommands(const std::vector<std::string>& expectedCmds) {
Lorenzo Colittie60c0a52016-03-29 00:53:45 +0900167 ExpectedIptablesCommands expected;
168 for (auto cmd : expectedCmds) {
169 expected.push_back({ V4V6, cmd });
170 }
171 expectIptablesRestoreCommands(expected);
172}
173
174void IptablesBaseTest::expectIptablesRestoreCommands(const ExpectedIptablesCommands& expectedCmds) {
Lorenzo Colitti0f150552016-03-28 02:30:27 +0900175 EXPECT_EQ(expectedCmds.size(), sRestoreCmds.size());
Lorenzo Colittie60c0a52016-03-29 00:53:45 +0900176 for (size_t i = 0; i < expectedCmds.size(); i++) {
177 EXPECT_EQ(expectedCmds[i], sRestoreCmds[i]) <<
178 "iptables-restore command " << i << " differs";
179 }
Lorenzo Colitti0f150552016-03-28 02:30:27 +0900180 sRestoreCmds.clear();
181}
182
Lorenzo Colitti849a11c2017-02-27 23:01:16 +0900183void IptablesBaseTest::setReturnValues(const std::deque<int>& returnValues) {
184 sReturnValues = returnValues;
185}
186
Lorenzo Colitti0f150552016-03-28 02:30:27 +0900187std::vector<std::string> IptablesBaseTest::sCmds = {};
Lorenzo Colittie60c0a52016-03-29 00:53:45 +0900188IptablesBaseTest::ExpectedIptablesCommands IptablesBaseTest::sRestoreCmds = {};
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900189std::deque<std::string> IptablesBaseTest::sPopenContents = {};
Lorenzo Colitticd283772017-01-31 19:00:49 +0900190std::deque<std::string> IptablesBaseTest::sIptablesRestoreOutput = {};
Lorenzo Colitti849a11c2017-02-27 23:01:16 +0900191std::deque<int> IptablesBaseTest::sReturnValues = {};