blob: c9bf67b21687f640991aae06bfe87679396b3172 [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 Colittic1306ea2017-03-27 05:52:31 +090033using android::base::StringPrintf;
34
Lorenzo Colitti0f150552016-03-28 02:30:27 +090035IptablesBaseTest::IptablesBaseTest() {
36 sCmds.clear();
37 sRestoreCmds.clear();
Lorenzo Colitti849a11c2017-02-27 23:01:16 +090038 sReturnValues.clear();
Lorenzo Colitti0f150552016-03-28 02:30:27 +090039}
40
41int 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++) {
Lorenzo Colitti8e1cee92016-07-09 14:24:08 +090044 if (argv[i] == NULL) break; // NatController likes to pass in invalid argc values.
Lorenzo Colitti0f150552016-03-28 02:30:27 +090045 cmd += " ";
46 cmd += argv[i];
47 }
48 sCmds.push_back(cmd);
Lorenzo Colitti849a11c2017-02-27 23:01:16 +090049
50 int ret;
51 if (sReturnValues.size()) {
52 ret = sReturnValues.front();
53 sReturnValues.pop_front();
54 } else {
55 ret = 0;
Lorenzo Colitti8e1cee92016-07-09 14:24:08 +090056 }
Lorenzo Colitti849a11c2017-02-27 23:01:16 +090057
58 if (status) {
59 *status = ret;
60 }
61 return ret;
Lorenzo Colitti0f150552016-03-28 02:30:27 +090062}
63
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +090064FILE *IptablesBaseTest::fake_popen(const char * /* cmd */, const char *type) {
65 if (sPopenContents.empty() || strcmp(type, "r") != 0) {
66 return NULL;
67 }
68
Lorenzo Colittic1306ea2017-03-27 05:52:31 +090069 std::string realCmd = StringPrintf("echo '%s'", sPopenContents.front().c_str());
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +090070 sPopenContents.pop_front();
71 return popen(realCmd.c_str(), "r");
72}
73
Lorenzo Colitticd283772017-01-31 19:00:49 +090074int IptablesBaseTest::fakeExecIptablesRestoreWithOutput(IptablesTarget target,
75 const std::string& commands,
76 std::string *output) {
Lorenzo Colittie60c0a52016-03-29 00:53:45 +090077 sRestoreCmds.push_back({ target, commands });
Lorenzo Colitticd283772017-01-31 19:00:49 +090078 if (output != nullptr) {
79 *output = sIptablesRestoreOutput.size() ? sIptablesRestoreOutput.front().c_str() : "";
80 }
81 if (sIptablesRestoreOutput.size()) {
82 sIptablesRestoreOutput.pop_front();
83 }
Lorenzo Colitti0f150552016-03-28 02:30:27 +090084 return 0;
85}
86
Lorenzo Colitticd283772017-01-31 19:00:49 +090087int IptablesBaseTest::fakeExecIptablesRestore(IptablesTarget target, const std::string& commands) {
88 return fakeExecIptablesRestoreWithOutput(target, commands, nullptr);
89}
90
Lorenzo Colittic1306ea2017-03-27 05:52:31 +090091int 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 Colitti0f150552016-03-28 02:30:27 +090099void IptablesBaseTest::expectIptablesRestoreCommands(const std::vector<std::string>& expectedCmds) {
Lorenzo Colittie60c0a52016-03-29 00:53:45 +0900100 ExpectedIptablesCommands expected;
Lorenzo Colittieb7eb3e2017-08-11 01:20:04 +0900101 for (const auto& cmd : expectedCmds) {
Lorenzo Colittie60c0a52016-03-29 00:53:45 +0900102 expected.push_back({ V4V6, cmd });
103 }
104 expectIptablesRestoreCommands(expected);
105}
106
107void IptablesBaseTest::expectIptablesRestoreCommands(const ExpectedIptablesCommands& expectedCmds) {
Lorenzo Colitti0f150552016-03-28 02:30:27 +0900108 EXPECT_EQ(expectedCmds.size(), sRestoreCmds.size());
Lorenzo Colittie60c0a52016-03-29 00:53:45 +0900109 for (size_t i = 0; i < expectedCmds.size(); i++) {
110 EXPECT_EQ(expectedCmds[i], sRestoreCmds[i]) <<
111 "iptables-restore command " << i << " differs";
112 }
Lorenzo Colitti0f150552016-03-28 02:30:27 +0900113 sRestoreCmds.clear();
114}
115
Lorenzo Colitti849a11c2017-02-27 23:01:16 +0900116void IptablesBaseTest::setReturnValues(const std::deque<int>& returnValues) {
117 sReturnValues = returnValues;
118}
119
Lorenzo Colitti0f150552016-03-28 02:30:27 +0900120std::vector<std::string> IptablesBaseTest::sCmds = {};
Lorenzo Colittie60c0a52016-03-29 00:53:45 +0900121IptablesBaseTest::ExpectedIptablesCommands IptablesBaseTest::sRestoreCmds = {};
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900122std::deque<std::string> IptablesBaseTest::sPopenContents = {};
Lorenzo Colitticd283772017-01-31 19:00:49 +0900123std::deque<std::string> IptablesBaseTest::sIptablesRestoreOutput = {};
Lorenzo Colitti849a11c2017-02-27 23:01:16 +0900124std::deque<int> IptablesBaseTest::sReturnValues = {};