xref: /aosp_15_r20/system/netd/server/IptablesBaseTest.h (revision 8542734a0dd1db395a4d42aae09c37f3c3c3e7a1)
1*8542734aSAndroid Build Coastguard Worker /*
2*8542734aSAndroid Build Coastguard Worker  * Copyright 2016 The Android Open Source Project
3*8542734aSAndroid Build Coastguard Worker  *
4*8542734aSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*8542734aSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*8542734aSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*8542734aSAndroid Build Coastguard Worker  *
8*8542734aSAndroid Build Coastguard Worker  * http://www.apache.org/licenses/LICENSE-2.0
9*8542734aSAndroid Build Coastguard Worker  *
10*8542734aSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*8542734aSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*8542734aSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*8542734aSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*8542734aSAndroid Build Coastguard Worker  * limitations under the License.
15*8542734aSAndroid Build Coastguard Worker  *
16*8542734aSAndroid Build Coastguard Worker  * IptablesBaseTest.h - utility class for tests that use iptables
17*8542734aSAndroid Build Coastguard Worker  */
18*8542734aSAndroid Build Coastguard Worker 
19*8542734aSAndroid Build Coastguard Worker #include <deque>
20*8542734aSAndroid Build Coastguard Worker 
21*8542734aSAndroid Build Coastguard Worker #include <netdutils/NetNativeTestBase.h>
22*8542734aSAndroid Build Coastguard Worker 
23*8542734aSAndroid Build Coastguard Worker #include "NetdConstants.h"
24*8542734aSAndroid Build Coastguard Worker 
25*8542734aSAndroid Build Coastguard Worker class IptablesBaseTest : public NetNativeTestBase {
26*8542734aSAndroid Build Coastguard Worker public:
27*8542734aSAndroid Build Coastguard Worker     IptablesBaseTest();
28*8542734aSAndroid Build Coastguard Worker 
29*8542734aSAndroid Build Coastguard Worker     typedef std::vector<std::pair<IptablesTarget, std::string>> ExpectedIptablesCommands;
30*8542734aSAndroid Build Coastguard Worker 
31*8542734aSAndroid Build Coastguard Worker     static int fake_android_fork_exec(int argc, char* argv[], int *status, bool, bool);
32*8542734aSAndroid Build Coastguard Worker     static int fake_android_fork_execvp(int argc, char* argv[], int *status, bool, bool);
33*8542734aSAndroid Build Coastguard Worker     static int fakeExecIptablesRestore(IptablesTarget target, const std::string& commands);
34*8542734aSAndroid Build Coastguard Worker     static int fakeExecIptablesRestoreWithOutput(IptablesTarget target, const std::string& commands,
35*8542734aSAndroid Build Coastguard Worker                                                  std::string *output);
36*8542734aSAndroid Build Coastguard Worker     static int fakeExecIptablesRestoreCommand(IptablesTarget target, const std::string& table,
37*8542734aSAndroid Build Coastguard Worker                                               const std::string& commands, std::string *output);
38*8542734aSAndroid Build Coastguard Worker     static FILE *fake_popen(const char *cmd, const char *type);
39*8542734aSAndroid Build Coastguard Worker     void expectIptablesRestoreCommands(const std::vector<std::string>& expectedCmds);
40*8542734aSAndroid Build Coastguard Worker     void expectIptablesRestoreCommands(const ExpectedIptablesCommands& expectedCmds);
41*8542734aSAndroid Build Coastguard Worker 
setReturnValues(const std::deque<int> & returnValues)42*8542734aSAndroid Build Coastguard Worker     static void setReturnValues(const std::deque<int>& returnValues) {
43*8542734aSAndroid Build Coastguard Worker         sReturnValues = returnValues;
44*8542734aSAndroid Build Coastguard Worker     }
45*8542734aSAndroid Build Coastguard Worker 
addIptablesRestoreOutput(std::string contents)46*8542734aSAndroid Build Coastguard Worker     static void addIptablesRestoreOutput(std::string contents) {
47*8542734aSAndroid Build Coastguard Worker         sIptablesRestoreOutput.push_back(contents);
48*8542734aSAndroid Build Coastguard Worker     }
49*8542734aSAndroid Build Coastguard Worker 
addIptablesRestoreOutput(std::string contents1,std::string contents2)50*8542734aSAndroid Build Coastguard Worker     static void addIptablesRestoreOutput(std::string contents1, std::string contents2) {
51*8542734aSAndroid Build Coastguard Worker         addIptablesRestoreOutput(contents1);
52*8542734aSAndroid Build Coastguard Worker         addIptablesRestoreOutput(contents2);
53*8542734aSAndroid Build Coastguard Worker     }
54*8542734aSAndroid Build Coastguard Worker 
clearIptablesRestoreOutput()55*8542734aSAndroid Build Coastguard Worker     static void clearIptablesRestoreOutput() {
56*8542734aSAndroid Build Coastguard Worker         sIptablesRestoreOutput.clear();
57*8542734aSAndroid Build Coastguard Worker     }
58*8542734aSAndroid Build Coastguard Worker 
59*8542734aSAndroid Build Coastguard Worker protected:
60*8542734aSAndroid Build Coastguard Worker     static std::vector<std::string> sCmds;
61*8542734aSAndroid Build Coastguard Worker     static ExpectedIptablesCommands sRestoreCmds;
62*8542734aSAndroid Build Coastguard Worker     static std::deque<int> sReturnValues;
63*8542734aSAndroid Build Coastguard Worker     static std::deque<std::string> sPopenContents;
64*8542734aSAndroid Build Coastguard Worker     static std::deque<std::string> sIptablesRestoreOutput;
65*8542734aSAndroid Build Coastguard Worker };
66