xref: /aosp_15_r20/system/netd/server/FirewallControllerTest.cpp (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  * FirewallControllerTest.cpp - unit tests for FirewallController.cpp
17*8542734aSAndroid Build Coastguard Worker  */
18*8542734aSAndroid Build Coastguard Worker 
19*8542734aSAndroid Build Coastguard Worker #include <string>
20*8542734aSAndroid Build Coastguard Worker #include <vector>
21*8542734aSAndroid Build Coastguard Worker #include <stdio.h>
22*8542734aSAndroid Build Coastguard Worker 
23*8542734aSAndroid Build Coastguard Worker #include <gtest/gtest.h>
24*8542734aSAndroid Build Coastguard Worker 
25*8542734aSAndroid Build Coastguard Worker #include "FirewallController.h"
26*8542734aSAndroid Build Coastguard Worker #include "IptablesBaseTest.h"
27*8542734aSAndroid Build Coastguard Worker 
28*8542734aSAndroid Build Coastguard Worker namespace android {
29*8542734aSAndroid Build Coastguard Worker namespace net {
30*8542734aSAndroid Build Coastguard Worker 
31*8542734aSAndroid Build Coastguard Worker class FirewallControllerTest : public IptablesBaseTest {
32*8542734aSAndroid Build Coastguard Worker protected:
FirewallControllerTest()33*8542734aSAndroid Build Coastguard Worker     FirewallControllerTest() {
34*8542734aSAndroid Build Coastguard Worker         FirewallController::execIptablesRestore = fakeExecIptablesRestore;
35*8542734aSAndroid Build Coastguard Worker     }
36*8542734aSAndroid Build Coastguard Worker     FirewallController mFw;
37*8542734aSAndroid Build Coastguard Worker };
38*8542734aSAndroid Build Coastguard Worker 
TEST_F(FirewallControllerTest,TestFirewall)39*8542734aSAndroid Build Coastguard Worker TEST_F(FirewallControllerTest, TestFirewall) {
40*8542734aSAndroid Build Coastguard Worker     std::vector<std::string> enableCommands = {
41*8542734aSAndroid Build Coastguard Worker             "*filter\n"
42*8542734aSAndroid Build Coastguard Worker             "-A fw_INPUT -j DROP\n"
43*8542734aSAndroid Build Coastguard Worker             "-A fw_OUTPUT -j REJECT\n"
44*8542734aSAndroid Build Coastguard Worker             "-A fw_FORWARD -j REJECT\n"
45*8542734aSAndroid Build Coastguard Worker             "COMMIT\n"};
46*8542734aSAndroid Build Coastguard Worker     std::vector<std::string> disableCommands = {
47*8542734aSAndroid Build Coastguard Worker             "*filter\n"
48*8542734aSAndroid Build Coastguard Worker             ":fw_INPUT -\n"
49*8542734aSAndroid Build Coastguard Worker             ":fw_OUTPUT -\n"
50*8542734aSAndroid Build Coastguard Worker             ":fw_FORWARD -\n"
51*8542734aSAndroid Build Coastguard Worker             "-6 -A fw_OUTPUT ! -o lo -s ::1 -j DROP\n"
52*8542734aSAndroid Build Coastguard Worker             "COMMIT\n"};
53*8542734aSAndroid Build Coastguard Worker     std::vector<std::string> noCommands = {};
54*8542734aSAndroid Build Coastguard Worker 
55*8542734aSAndroid Build Coastguard Worker     EXPECT_EQ(0, mFw.resetFirewall());
56*8542734aSAndroid Build Coastguard Worker     expectIptablesRestoreCommands(disableCommands);
57*8542734aSAndroid Build Coastguard Worker 
58*8542734aSAndroid Build Coastguard Worker     EXPECT_EQ(0, mFw.resetFirewall());
59*8542734aSAndroid Build Coastguard Worker     expectIptablesRestoreCommands(disableCommands);
60*8542734aSAndroid Build Coastguard Worker 
61*8542734aSAndroid Build Coastguard Worker     EXPECT_EQ(0, mFw.setFirewallType(DENYLIST));
62*8542734aSAndroid Build Coastguard Worker     expectIptablesRestoreCommands(disableCommands);
63*8542734aSAndroid Build Coastguard Worker 
64*8542734aSAndroid Build Coastguard Worker     EXPECT_EQ(0, mFw.setFirewallType(DENYLIST));
65*8542734aSAndroid Build Coastguard Worker     expectIptablesRestoreCommands(noCommands);
66*8542734aSAndroid Build Coastguard Worker 
67*8542734aSAndroid Build Coastguard Worker     std::vector<std::string> disableEnableCommands;
68*8542734aSAndroid Build Coastguard Worker     disableEnableCommands.insert(
69*8542734aSAndroid Build Coastguard Worker             disableEnableCommands.end(), disableCommands.begin(), disableCommands.end());
70*8542734aSAndroid Build Coastguard Worker     disableEnableCommands.insert(
71*8542734aSAndroid Build Coastguard Worker             disableEnableCommands.end(), enableCommands.begin(), enableCommands.end());
72*8542734aSAndroid Build Coastguard Worker 
73*8542734aSAndroid Build Coastguard Worker     EXPECT_EQ(0, mFw.setFirewallType(ALLOWLIST));
74*8542734aSAndroid Build Coastguard Worker     expectIptablesRestoreCommands(disableEnableCommands);
75*8542734aSAndroid Build Coastguard Worker 
76*8542734aSAndroid Build Coastguard Worker     std::vector<std::string> ifaceCommands = {
77*8542734aSAndroid Build Coastguard Worker         "*filter\n"
78*8542734aSAndroid Build Coastguard Worker         "-I fw_INPUT -i rmnet_data0 -j RETURN\n"
79*8542734aSAndroid Build Coastguard Worker         "-I fw_OUTPUT -o rmnet_data0 -j RETURN\n"
80*8542734aSAndroid Build Coastguard Worker         "COMMIT\n"
81*8542734aSAndroid Build Coastguard Worker     };
82*8542734aSAndroid Build Coastguard Worker     EXPECT_EQ(0, mFw.setInterfaceRule("rmnet_data0", ALLOW));
83*8542734aSAndroid Build Coastguard Worker     expectIptablesRestoreCommands(ifaceCommands);
84*8542734aSAndroid Build Coastguard Worker 
85*8542734aSAndroid Build Coastguard Worker     EXPECT_EQ(0, mFw.setInterfaceRule("rmnet_data0", ALLOW));
86*8542734aSAndroid Build Coastguard Worker     expectIptablesRestoreCommands(noCommands);
87*8542734aSAndroid Build Coastguard Worker 
88*8542734aSAndroid Build Coastguard Worker     ifaceCommands = {
89*8542734aSAndroid Build Coastguard Worker         "*filter\n"
90*8542734aSAndroid Build Coastguard Worker         "-D fw_INPUT -i rmnet_data0 -j RETURN\n"
91*8542734aSAndroid Build Coastguard Worker         "-D fw_OUTPUT -o rmnet_data0 -j RETURN\n"
92*8542734aSAndroid Build Coastguard Worker         "COMMIT\n"
93*8542734aSAndroid Build Coastguard Worker     };
94*8542734aSAndroid Build Coastguard Worker     EXPECT_EQ(0, mFw.setInterfaceRule("rmnet_data0", DENY));
95*8542734aSAndroid Build Coastguard Worker     expectIptablesRestoreCommands(ifaceCommands);
96*8542734aSAndroid Build Coastguard Worker 
97*8542734aSAndroid Build Coastguard Worker     EXPECT_EQ(0, mFw.setInterfaceRule("rmnet_data0", DENY));
98*8542734aSAndroid Build Coastguard Worker     expectIptablesRestoreCommands(noCommands);
99*8542734aSAndroid Build Coastguard Worker 
100*8542734aSAndroid Build Coastguard Worker     EXPECT_EQ(0, mFw.setFirewallType(ALLOWLIST));
101*8542734aSAndroid Build Coastguard Worker     expectIptablesRestoreCommands(noCommands);
102*8542734aSAndroid Build Coastguard Worker 
103*8542734aSAndroid Build Coastguard Worker     EXPECT_EQ(0, mFw.resetFirewall());
104*8542734aSAndroid Build Coastguard Worker     expectIptablesRestoreCommands(disableCommands);
105*8542734aSAndroid Build Coastguard Worker 
106*8542734aSAndroid Build Coastguard Worker     // TODO: calling resetFirewall and then setFirewallType(ALLOWLIST) does
107*8542734aSAndroid Build Coastguard Worker     // nothing. This seems like a clear bug.
108*8542734aSAndroid Build Coastguard Worker     EXPECT_EQ(0, mFw.setFirewallType(ALLOWLIST));
109*8542734aSAndroid Build Coastguard Worker     expectIptablesRestoreCommands(noCommands);
110*8542734aSAndroid Build Coastguard Worker }
111*8542734aSAndroid Build Coastguard Worker 
112*8542734aSAndroid Build Coastguard Worker }  // namespace net
113*8542734aSAndroid Build Coastguard Worker }  // namespace android
114