blob: a7aee47e3c84272b6bb59c957af6c37015e67dd3 [file] [log] [blame]
Lorenzo Colitti89faa342016-02-26 11:38:47 +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 * FirewallControllerTest.cpp - unit tests for FirewallController.cpp
17 */
18
19#include <string>
20#include <vector>
21#include <stdio.h>
22
23#include <gtest/gtest.h>
24
25#include "FirewallController.h"
Lorenzo Colitti932c44c2016-04-24 16:58:02 +090026#include "IptablesBaseTest.h"
Lorenzo Colitti89faa342016-02-26 11:38:47 +090027
Luke Huange64fa382018-07-24 16:38:22 +080028namespace android {
29namespace net {
30
Lorenzo Colitti932c44c2016-04-24 16:58:02 +090031class FirewallControllerTest : public IptablesBaseTest {
Lorenzo Colitti89faa342016-02-26 11:38:47 +090032protected:
Lorenzo Colitti932c44c2016-04-24 16:58:02 +090033 FirewallControllerTest() {
Lorenzo Colitti932c44c2016-04-24 16:58:02 +090034 FirewallController::execIptablesRestore = fakeExecIptablesRestore;
35 }
Lorenzo Colitti89faa342016-02-26 11:38:47 +090036 FirewallController mFw;
Lorenzo Colitti89faa342016-02-26 11:38:47 +090037};
38
Lorenzo Colitti6324b182017-07-17 21:48:14 +090039TEST_F(FirewallControllerTest, TestFirewall) {
Lorenzo Colitticc1bb822017-07-16 21:08:54 +090040 std::vector<std::string> enableCommands = {
Maciej Żenczykowski344bb892021-10-14 20:22:23 -070041 "*filter\n"
42 "-A fw_INPUT -j DROP\n"
43 "-A fw_OUTPUT -j REJECT\n"
44 "-A fw_FORWARD -j REJECT\n"
45 "COMMIT\n"};
Lorenzo Colitticc1bb822017-07-16 21:08:54 +090046 std::vector<std::string> disableCommands = {
Maciej Żenczykowski344bb892021-10-14 20:22:23 -070047 "*filter\n"
48 ":fw_INPUT -\n"
49 ":fw_OUTPUT -\n"
50 ":fw_FORWARD -\n"
51 "-6 -A fw_OUTPUT ! -o lo -s ::1 -j DROP\n"
52 "COMMIT\n"};
Lorenzo Colitticc1bb822017-07-16 21:08:54 +090053 std::vector<std::string> noCommands = {};
54
Luke Huange64fa382018-07-24 16:38:22 +080055 EXPECT_EQ(0, mFw.resetFirewall());
Lorenzo Colittid351bea2017-07-16 22:52:30 +090056 expectIptablesRestoreCommands(disableCommands);
Lorenzo Colitticc1bb822017-07-16 21:08:54 +090057
Luke Huange64fa382018-07-24 16:38:22 +080058 EXPECT_EQ(0, mFw.resetFirewall());
Lorenzo Colittid351bea2017-07-16 22:52:30 +090059 expectIptablesRestoreCommands(disableCommands);
Lorenzo Colitticc1bb822017-07-16 21:08:54 +090060
Lorenzo Colitticdd79f12020-07-30 12:03:40 +090061 EXPECT_EQ(0, mFw.setFirewallType(DENYLIST));
Lorenzo Colittid351bea2017-07-16 22:52:30 +090062 expectIptablesRestoreCommands(disableCommands);
Lorenzo Colitticc1bb822017-07-16 21:08:54 +090063
Lorenzo Colitticdd79f12020-07-30 12:03:40 +090064 EXPECT_EQ(0, mFw.setFirewallType(DENYLIST));
Lorenzo Colittid351bea2017-07-16 22:52:30 +090065 expectIptablesRestoreCommands(noCommands);
Lorenzo Colitticc1bb822017-07-16 21:08:54 +090066
67 std::vector<std::string> disableEnableCommands;
68 disableEnableCommands.insert(
69 disableEnableCommands.end(), disableCommands.begin(), disableCommands.end());
70 disableEnableCommands.insert(
71 disableEnableCommands.end(), enableCommands.begin(), enableCommands.end());
72
Lorenzo Colitticdd79f12020-07-30 12:03:40 +090073 EXPECT_EQ(0, mFw.setFirewallType(ALLOWLIST));
Lorenzo Colittid351bea2017-07-16 22:52:30 +090074 expectIptablesRestoreCommands(disableEnableCommands);
Lorenzo Colitticc1bb822017-07-16 21:08:54 +090075
Lorenzo Colitti6324b182017-07-17 21:48:14 +090076 std::vector<std::string> ifaceCommands = {
Lorenzo Colitti1411d452017-07-17 22:12:15 +090077 "*filter\n"
78 "-I fw_INPUT -i rmnet_data0 -j RETURN\n"
79 "-I fw_OUTPUT -o rmnet_data0 -j RETURN\n"
80 "COMMIT\n"
Lorenzo Colitti6324b182017-07-17 21:48:14 +090081 };
82 EXPECT_EQ(0, mFw.setInterfaceRule("rmnet_data0", ALLOW));
Lorenzo Colitti1411d452017-07-17 22:12:15 +090083 expectIptablesRestoreCommands(ifaceCommands);
84
85 EXPECT_EQ(0, mFw.setInterfaceRule("rmnet_data0", ALLOW));
86 expectIptablesRestoreCommands(noCommands);
Lorenzo Colitti6324b182017-07-17 21:48:14 +090087
88 ifaceCommands = {
Lorenzo Colitti1411d452017-07-17 22:12:15 +090089 "*filter\n"
90 "-D fw_INPUT -i rmnet_data0 -j RETURN\n"
91 "-D fw_OUTPUT -o rmnet_data0 -j RETURN\n"
92 "COMMIT\n"
Lorenzo Colitti6324b182017-07-17 21:48:14 +090093 };
94 EXPECT_EQ(0, mFw.setInterfaceRule("rmnet_data0", DENY));
Lorenzo Colitti1411d452017-07-17 22:12:15 +090095 expectIptablesRestoreCommands(ifaceCommands);
96
97 EXPECT_EQ(0, mFw.setInterfaceRule("rmnet_data0", DENY));
98 expectIptablesRestoreCommands(noCommands);
Lorenzo Colitti6324b182017-07-17 21:48:14 +090099
Lorenzo Colitticdd79f12020-07-30 12:03:40 +0900100 EXPECT_EQ(0, mFw.setFirewallType(ALLOWLIST));
Lorenzo Colittid351bea2017-07-16 22:52:30 +0900101 expectIptablesRestoreCommands(noCommands);
Lorenzo Colitticc1bb822017-07-16 21:08:54 +0900102
Luke Huange64fa382018-07-24 16:38:22 +0800103 EXPECT_EQ(0, mFw.resetFirewall());
Lorenzo Colittid351bea2017-07-16 22:52:30 +0900104 expectIptablesRestoreCommands(disableCommands);
Lorenzo Colitticc1bb822017-07-16 21:08:54 +0900105
Lorenzo Colitticdd79f12020-07-30 12:03:40 +0900106 // TODO: calling resetFirewall and then setFirewallType(ALLOWLIST) does
Lorenzo Colitticc1bb822017-07-16 21:08:54 +0900107 // nothing. This seems like a clear bug.
Lorenzo Colitticdd79f12020-07-30 12:03:40 +0900108 EXPECT_EQ(0, mFw.setFirewallType(ALLOWLIST));
Lorenzo Colittid351bea2017-07-16 22:52:30 +0900109 expectIptablesRestoreCommands(noCommands);
Lorenzo Colitticc1bb822017-07-16 21:08:54 +0900110}
Hugo Benichi528d3d02018-06-20 13:35:58 +0900111
Luke Huange64fa382018-07-24 16:38:22 +0800112} // namespace net
Bernie Innocentia5161a02019-01-30 22:40:53 +0900113} // namespace android