blob: 7d96c61c7641cd29d071c3b16e3aee9d4cd5b430 [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
Lorenzo Colittia55388e2016-05-13 17:03:42 +090025#include <android-base/strings.h>
26
Lorenzo Colitti89faa342016-02-26 11:38:47 +090027#include "FirewallController.h"
Lorenzo Colitti932c44c2016-04-24 16:58:02 +090028#include "IptablesBaseTest.h"
Lorenzo Colitti89faa342016-02-26 11:38:47 +090029
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() {
34 FirewallController::execIptables = fakeExecIptables;
35 FirewallController::execIptablesSilently = fakeExecIptables;
36 FirewallController::execIptablesRestore = fakeExecIptablesRestore;
37 }
Lorenzo Colitti89faa342016-02-26 11:38:47 +090038 FirewallController mFw;
Lorenzo Colitti932c44c2016-04-24 16:58:02 +090039
Lorenzo Colittif157caf2016-05-13 11:25:54 +090040 std::string makeUidRules(IptablesTarget a, const char* b, bool c,
41 const std::vector<int32_t>& d) {
42 return mFw.makeUidRules(a, b, c, d);
Lorenzo Colitti89faa342016-02-26 11:38:47 +090043 }
Lorenzo Colitti932c44c2016-04-24 16:58:02 +090044
Lorenzo Colittif157caf2016-05-13 11:25:54 +090045 int createChain(const char* a, const char* b , FirewallType c) {
Lorenzo Colitti932c44c2016-04-24 16:58:02 +090046 return mFw.createChain(a, b, c);
47 }
Lorenzo Colitti89faa342016-02-26 11:38:47 +090048};
49
50
Lorenzo Colitti932c44c2016-04-24 16:58:02 +090051TEST_F(FirewallControllerTest, TestCreateWhitelistChain) {
Lorenzo Colittia55388e2016-05-13 17:03:42 +090052 ExpectedIptablesCommands expectedCommands = {
Lorenzo Colitti932c44c2016-04-24 16:58:02 +090053 { V4V6, "-t filter -D INPUT -j fw_whitelist" },
Lorenzo Colitti932c44c2016-04-24 16:58:02 +090054 };
Lorenzo Colittia55388e2016-05-13 17:03:42 +090055
56 std::vector<std::string> expectedRestore4 = {
57 "*filter",
58 ":fw_whitelist -",
Lorenzo Colitti238e8182016-07-26 17:59:41 +090059 "-A fw_whitelist -i lo -o lo -j RETURN",
Lorenzo Colittia55388e2016-05-13 17:03:42 +090060 "-A fw_whitelist -p tcp --tcp-flags RST RST -j RETURN",
61 "-A fw_whitelist -m owner --uid-owner 0-9999 -j RETURN",
62 "-A fw_whitelist -j DROP",
63 "COMMIT\n\x04"
64 };
65 std::vector<std::string> expectedRestore6 = {
66 "*filter",
67 ":fw_whitelist -",
Lorenzo Colitti238e8182016-07-26 17:59:41 +090068 "-A fw_whitelist -i lo -o lo -j RETURN",
Lorenzo Colittia55388e2016-05-13 17:03:42 +090069 "-A fw_whitelist -p tcp --tcp-flags RST RST -j RETURN",
70 "-A fw_whitelist -p icmpv6 --icmpv6-type packet-too-big -j RETURN",
71 "-A fw_whitelist -p icmpv6 --icmpv6-type router-solicitation -j RETURN",
72 "-A fw_whitelist -p icmpv6 --icmpv6-type router-advertisement -j RETURN",
73 "-A fw_whitelist -p icmpv6 --icmpv6-type neighbour-solicitation -j RETURN",
74 "-A fw_whitelist -p icmpv6 --icmpv6-type neighbour-advertisement -j RETURN",
75 "-A fw_whitelist -p icmpv6 --icmpv6-type redirect -j RETURN",
76 "-A fw_whitelist -m owner --uid-owner 0-9999 -j RETURN",
77 "-A fw_whitelist -j DROP",
78 "COMMIT\n\x04"
79 };
80 std::vector<std::pair<IptablesTarget, std::string>> expectedRestoreCommands = {
81 { V4, android::base::Join(expectedRestore4, '\n') },
82 { V6, android::base::Join(expectedRestore6, '\n') },
83 };
84
Lorenzo Colitti932c44c2016-04-24 16:58:02 +090085 createChain("fw_whitelist", "INPUT", WHITELIST);
Lorenzo Colittia55388e2016-05-13 17:03:42 +090086 expectIptablesCommands(expectedCommands);
87 expectIptablesRestoreCommands(expectedRestoreCommands);
Lorenzo Colitti932c44c2016-04-24 16:58:02 +090088}
89
90TEST_F(FirewallControllerTest, TestCreateBlacklistChain) {
Lorenzo Colittia55388e2016-05-13 17:03:42 +090091 ExpectedIptablesCommands expectedCommands = {
Lorenzo Colitti932c44c2016-04-24 16:58:02 +090092 { V4V6, "-t filter -D INPUT -j fw_blacklist" },
Lorenzo Colitti932c44c2016-04-24 16:58:02 +090093 };
Lorenzo Colittia55388e2016-05-13 17:03:42 +090094
95 std::vector<std::string> expectedRestore = {
96 "*filter",
97 ":fw_blacklist -",
Lorenzo Colitti238e8182016-07-26 17:59:41 +090098 "-A fw_blacklist -i lo -o lo -j RETURN",
Lorenzo Colittia55388e2016-05-13 17:03:42 +090099 "-A fw_blacklist -p tcp --tcp-flags RST RST -j RETURN",
100 "COMMIT\n\x04"
101 };
102 std::vector<std::pair<IptablesTarget, std::string>> expectedRestoreCommands = {
103 { V4, android::base::Join(expectedRestore, '\n') },
104 { V6, android::base::Join(expectedRestore, '\n') },
105 };
106
Lorenzo Colitti932c44c2016-04-24 16:58:02 +0900107 createChain("fw_blacklist", "INPUT", BLACKLIST);
Lorenzo Colittia55388e2016-05-13 17:03:42 +0900108 expectIptablesCommands(expectedCommands);
109 expectIptablesRestoreCommands(expectedRestoreCommands);
Lorenzo Colitti932c44c2016-04-24 16:58:02 +0900110}
111
112TEST_F(FirewallControllerTest, TestSetStandbyRule) {
113 ExpectedIptablesCommands expected = {
114 { V4V6, "-D fw_standby -m owner --uid-owner 12345 -j DROP" }
115 };
116 mFw.setUidRule(STANDBY, 12345, ALLOW);
117 expectIptablesCommands(expected);
118
119 expected = {
120 { V4V6, "-A fw_standby -m owner --uid-owner 12345 -j DROP" }
121 };
122 mFw.setUidRule(STANDBY, 12345, DENY);
123 expectIptablesCommands(expected);
124}
125
126TEST_F(FirewallControllerTest, TestSetDozeRule) {
127 ExpectedIptablesCommands expected = {
128 { V4V6, "-I fw_dozable -m owner --uid-owner 54321 -j RETURN" }
129 };
130 mFw.setUidRule(DOZABLE, 54321, ALLOW);
131 expectIptablesCommands(expected);
132
133 expected = {
134 { V4V6, "-D fw_dozable -m owner --uid-owner 54321 -j RETURN" }
135 };
136 mFw.setUidRule(DOZABLE, 54321, DENY);
137 expectIptablesCommands(expected);
138}
139
140TEST_F(FirewallControllerTest, TestReplaceWhitelistUidRule) {
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900141 std::string expected =
142 "*filter\n"
143 ":FW_whitechain -\n"
Lorenzo Colitti238e8182016-07-26 17:59:41 +0900144 "-A FW_whitechain -i lo -o lo -j RETURN\n"
Lorenzo Colittif157caf2016-05-13 11:25:54 +0900145 "-A FW_whitechain -p tcp --tcp-flags RST RST -j RETURN\n"
146 "-A FW_whitechain -p icmpv6 --icmpv6-type packet-too-big -j RETURN\n"
147 "-A FW_whitechain -p icmpv6 --icmpv6-type router-solicitation -j RETURN\n"
148 "-A FW_whitechain -p icmpv6 --icmpv6-type router-advertisement -j RETURN\n"
149 "-A FW_whitechain -p icmpv6 --icmpv6-type neighbour-solicitation -j RETURN\n"
150 "-A FW_whitechain -p icmpv6 --icmpv6-type neighbour-advertisement -j RETURN\n"
151 "-A FW_whitechain -p icmpv6 --icmpv6-type redirect -j RETURN\n"
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900152 "-A FW_whitechain -m owner --uid-owner 0-9999 -j RETURN\n"
153 "-A FW_whitechain -m owner --uid-owner 10023 -j RETURN\n"
154 "-A FW_whitechain -m owner --uid-owner 10059 -j RETURN\n"
155 "-A FW_whitechain -m owner --uid-owner 10124 -j RETURN\n"
156 "-A FW_whitechain -m owner --uid-owner 10111 -j RETURN\n"
157 "-A FW_whitechain -m owner --uid-owner 110122 -j RETURN\n"
158 "-A FW_whitechain -m owner --uid-owner 210153 -j RETURN\n"
159 "-A FW_whitechain -m owner --uid-owner 210024 -j RETURN\n"
160 "-A FW_whitechain -j DROP\n"
161 "COMMIT\n\x04";
162
163 std::vector<int32_t> uids = { 10023, 10059, 10124, 10111, 110122, 210153, 210024 };
Lorenzo Colittif157caf2016-05-13 11:25:54 +0900164 EXPECT_EQ(expected, makeUidRules(V6, "FW_whitechain", true, uids));
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900165}
166
Lorenzo Colitti932c44c2016-04-24 16:58:02 +0900167TEST_F(FirewallControllerTest, TestReplaceBlacklistUidRule) {
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900168 std::string expected =
169 "*filter\n"
170 ":FW_blackchain -\n"
Lorenzo Colitti238e8182016-07-26 17:59:41 +0900171 "-A FW_blackchain -i lo -o lo -j RETURN\n"
Lorenzo Colittif157caf2016-05-13 11:25:54 +0900172 "-A FW_blackchain -p tcp --tcp-flags RST RST -j RETURN\n"
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900173 "-A FW_blackchain -m owner --uid-owner 10023 -j DROP\n"
174 "-A FW_blackchain -m owner --uid-owner 10059 -j DROP\n"
175 "-A FW_blackchain -m owner --uid-owner 10124 -j DROP\n"
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900176 "COMMIT\n\x04";
177
178 std::vector<int32_t> uids = { 10023, 10059, 10124 };
Lorenzo Colittif157caf2016-05-13 11:25:54 +0900179 EXPECT_EQ(expected, makeUidRules(V4 ,"FW_blackchain", false, uids));
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900180}