blob: 7e3686bbd06fc04feb10bc3f03e2bc31c368b595 [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
28
Lorenzo Colitti932c44c2016-04-24 16:58:02 +090029class FirewallControllerTest : public IptablesBaseTest {
Lorenzo Colitti89faa342016-02-26 11:38:47 +090030protected:
Lorenzo Colitti932c44c2016-04-24 16:58:02 +090031 FirewallControllerTest() {
32 FirewallController::execIptables = fakeExecIptables;
33 FirewallController::execIptablesSilently = fakeExecIptables;
34 FirewallController::execIptablesRestore = fakeExecIptablesRestore;
35 }
Lorenzo Colitti89faa342016-02-26 11:38:47 +090036 FirewallController mFw;
Lorenzo Colitti932c44c2016-04-24 16:58:02 +090037
Lorenzo Colittif157caf2016-05-13 11:25:54 +090038 std::string makeUidRules(IptablesTarget a, const char* b, bool c,
39 const std::vector<int32_t>& d) {
40 return mFw.makeUidRules(a, b, c, d);
Lorenzo Colitti89faa342016-02-26 11:38:47 +090041 }
Lorenzo Colitti932c44c2016-04-24 16:58:02 +090042
Lorenzo Colittif157caf2016-05-13 11:25:54 +090043 int createChain(const char* a, const char* b , FirewallType c) {
Lorenzo Colitti932c44c2016-04-24 16:58:02 +090044 return mFw.createChain(a, b, c);
45 }
Lorenzo Colitti89faa342016-02-26 11:38:47 +090046};
47
48
Lorenzo Colitti932c44c2016-04-24 16:58:02 +090049TEST_F(FirewallControllerTest, TestCreateWhitelistChain) {
50 ExpectedIptablesCommands expected = {
51 { V4V6, "-t filter -D INPUT -j fw_whitelist" },
52 { V4V6, "-t filter -F fw_whitelist" },
53 { V4V6, "-t filter -X fw_whitelist" },
54 { V4V6, "-t filter -N fw_whitelist" },
55 { V4V6, "-A fw_whitelist -p tcp --tcp-flags RST RST -j RETURN" },
56 { V6, "-A fw_whitelist -p icmpv6 --icmpv6-type packet-too-big -j RETURN" },
57 { V6, "-A fw_whitelist -p icmpv6 --icmpv6-type router-solicitation -j RETURN" },
58 { V6, "-A fw_whitelist -p icmpv6 --icmpv6-type router-advertisement -j RETURN" },
59 { V6, "-A fw_whitelist -p icmpv6 --icmpv6-type neighbour-solicitation -j RETURN" },
60 { V6, "-A fw_whitelist -p icmpv6 --icmpv6-type neighbour-advertisement -j RETURN" },
61 { V6, "-A fw_whitelist -p icmpv6 --icmpv6-type redirect -j RETURN" },
62 { V4V6, "-A fw_whitelist -m owner --uid-owner 0-9999 -j RETURN" },
63 { V4V6, "-A fw_whitelist -j DROP" },
64 };
65 createChain("fw_whitelist", "INPUT", WHITELIST);
66 expectIptablesCommands(expected);
67}
68
69TEST_F(FirewallControllerTest, TestCreateBlacklistChain) {
70 ExpectedIptablesCommands expected = {
71 { V4V6, "-t filter -D INPUT -j fw_blacklist" },
72 { V4V6, "-t filter -F fw_blacklist" },
73 { V4V6, "-t filter -X fw_blacklist" },
74 { V4V6, "-t filter -N fw_blacklist" },
75 { V4V6, "-A fw_blacklist -p tcp --tcp-flags RST RST -j RETURN" },
76 };
77 createChain("fw_blacklist", "INPUT", BLACKLIST);
78 expectIptablesCommands(expected);
79}
80
81TEST_F(FirewallControllerTest, TestSetStandbyRule) {
82 ExpectedIptablesCommands expected = {
83 { V4V6, "-D fw_standby -m owner --uid-owner 12345 -j DROP" }
84 };
85 mFw.setUidRule(STANDBY, 12345, ALLOW);
86 expectIptablesCommands(expected);
87
88 expected = {
89 { V4V6, "-A fw_standby -m owner --uid-owner 12345 -j DROP" }
90 };
91 mFw.setUidRule(STANDBY, 12345, DENY);
92 expectIptablesCommands(expected);
93}
94
95TEST_F(FirewallControllerTest, TestSetDozeRule) {
96 ExpectedIptablesCommands expected = {
97 { V4V6, "-I fw_dozable -m owner --uid-owner 54321 -j RETURN" }
98 };
99 mFw.setUidRule(DOZABLE, 54321, ALLOW);
100 expectIptablesCommands(expected);
101
102 expected = {
103 { V4V6, "-D fw_dozable -m owner --uid-owner 54321 -j RETURN" }
104 };
105 mFw.setUidRule(DOZABLE, 54321, DENY);
106 expectIptablesCommands(expected);
107}
108
109TEST_F(FirewallControllerTest, TestReplaceWhitelistUidRule) {
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900110 std::string expected =
111 "*filter\n"
112 ":FW_whitechain -\n"
Lorenzo Colittif157caf2016-05-13 11:25:54 +0900113 "-A FW_whitechain -p tcp --tcp-flags RST RST -j RETURN\n"
114 "-A FW_whitechain -p icmpv6 --icmpv6-type packet-too-big -j RETURN\n"
115 "-A FW_whitechain -p icmpv6 --icmpv6-type router-solicitation -j RETURN\n"
116 "-A FW_whitechain -p icmpv6 --icmpv6-type router-advertisement -j RETURN\n"
117 "-A FW_whitechain -p icmpv6 --icmpv6-type neighbour-solicitation -j RETURN\n"
118 "-A FW_whitechain -p icmpv6 --icmpv6-type neighbour-advertisement -j RETURN\n"
119 "-A FW_whitechain -p icmpv6 --icmpv6-type redirect -j RETURN\n"
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900120 "-A FW_whitechain -m owner --uid-owner 0-9999 -j RETURN\n"
121 "-A FW_whitechain -m owner --uid-owner 10023 -j RETURN\n"
122 "-A FW_whitechain -m owner --uid-owner 10059 -j RETURN\n"
123 "-A FW_whitechain -m owner --uid-owner 10124 -j RETURN\n"
124 "-A FW_whitechain -m owner --uid-owner 10111 -j RETURN\n"
125 "-A FW_whitechain -m owner --uid-owner 110122 -j RETURN\n"
126 "-A FW_whitechain -m owner --uid-owner 210153 -j RETURN\n"
127 "-A FW_whitechain -m owner --uid-owner 210024 -j RETURN\n"
128 "-A FW_whitechain -j DROP\n"
129 "COMMIT\n\x04";
130
131 std::vector<int32_t> uids = { 10023, 10059, 10124, 10111, 110122, 210153, 210024 };
Lorenzo Colittif157caf2016-05-13 11:25:54 +0900132 EXPECT_EQ(expected, makeUidRules(V6, "FW_whitechain", true, uids));
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900133}
134
Lorenzo Colitti932c44c2016-04-24 16:58:02 +0900135TEST_F(FirewallControllerTest, TestReplaceBlacklistUidRule) {
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900136 std::string expected =
137 "*filter\n"
138 ":FW_blackchain -\n"
Lorenzo Colittif157caf2016-05-13 11:25:54 +0900139 "-A FW_blackchain -p tcp --tcp-flags RST RST -j RETURN\n"
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900140 "-A FW_blackchain -m owner --uid-owner 10023 -j DROP\n"
141 "-A FW_blackchain -m owner --uid-owner 10059 -j DROP\n"
142 "-A FW_blackchain -m owner --uid-owner 10124 -j DROP\n"
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900143 "COMMIT\n\x04";
144
145 std::vector<int32_t> uids = { 10023, 10059, 10124 };
Lorenzo Colittif157caf2016-05-13 11:25:54 +0900146 EXPECT_EQ(expected, makeUidRules(V4 ,"FW_blackchain", false, uids));
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900147}