Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 1 | /* |
| 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 Colitti | 932c44c | 2016-04-24 16:58:02 +0900 | [diff] [blame] | 26 | #include "IptablesBaseTest.h" |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 27 | |
| 28 | |
Lorenzo Colitti | 932c44c | 2016-04-24 16:58:02 +0900 | [diff] [blame] | 29 | class FirewallControllerTest : public IptablesBaseTest { |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 30 | protected: |
Lorenzo Colitti | 932c44c | 2016-04-24 16:58:02 +0900 | [diff] [blame] | 31 | FirewallControllerTest() { |
| 32 | FirewallController::execIptables = fakeExecIptables; |
| 33 | FirewallController::execIptablesSilently = fakeExecIptables; |
| 34 | FirewallController::execIptablesRestore = fakeExecIptablesRestore; |
| 35 | } |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 36 | FirewallController mFw; |
Lorenzo Colitti | 932c44c | 2016-04-24 16:58:02 +0900 | [diff] [blame] | 37 | |
Lorenzo Colitti | f157caf | 2016-05-13 11:25:54 +0900 | [diff] [blame^] | 38 | 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 Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 41 | } |
Lorenzo Colitti | 932c44c | 2016-04-24 16:58:02 +0900 | [diff] [blame] | 42 | |
Lorenzo Colitti | f157caf | 2016-05-13 11:25:54 +0900 | [diff] [blame^] | 43 | int createChain(const char* a, const char* b , FirewallType c) { |
Lorenzo Colitti | 932c44c | 2016-04-24 16:58:02 +0900 | [diff] [blame] | 44 | return mFw.createChain(a, b, c); |
| 45 | } |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 46 | }; |
| 47 | |
| 48 | |
Lorenzo Colitti | 932c44c | 2016-04-24 16:58:02 +0900 | [diff] [blame] | 49 | TEST_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 | |
| 69 | TEST_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 | |
| 81 | TEST_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 | |
| 95 | TEST_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 | |
| 109 | TEST_F(FirewallControllerTest, TestReplaceWhitelistUidRule) { |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 110 | std::string expected = |
| 111 | "*filter\n" |
| 112 | ":FW_whitechain -\n" |
Lorenzo Colitti | f157caf | 2016-05-13 11:25:54 +0900 | [diff] [blame^] | 113 | "-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 Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 120 | "-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 Colitti | f157caf | 2016-05-13 11:25:54 +0900 | [diff] [blame^] | 132 | EXPECT_EQ(expected, makeUidRules(V6, "FW_whitechain", true, uids)); |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 133 | } |
| 134 | |
Lorenzo Colitti | 932c44c | 2016-04-24 16:58:02 +0900 | [diff] [blame] | 135 | TEST_F(FirewallControllerTest, TestReplaceBlacklistUidRule) { |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 136 | std::string expected = |
| 137 | "*filter\n" |
| 138 | ":FW_blackchain -\n" |
Lorenzo Colitti | f157caf | 2016-05-13 11:25:54 +0900 | [diff] [blame^] | 139 | "-A FW_blackchain -p tcp --tcp-flags RST RST -j RETURN\n" |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 140 | "-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 Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 143 | "COMMIT\n\x04"; |
| 144 | |
| 145 | std::vector<int32_t> uids = { 10023, 10059, 10124 }; |
Lorenzo Colitti | f157caf | 2016-05-13 11:25:54 +0900 | [diff] [blame^] | 146 | EXPECT_EQ(expected, makeUidRules(V4 ,"FW_blackchain", false, uids)); |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 147 | } |