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 | |
Lorenzo Colitti | a55388e | 2016-05-13 17:03:42 +0900 | [diff] [blame] | 25 | #include <android-base/strings.h> |
| 26 | |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 27 | #include "FirewallController.h" |
Lorenzo Colitti | 932c44c | 2016-04-24 16:58:02 +0900 | [diff] [blame] | 28 | #include "IptablesBaseTest.h" |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 29 | |
| 30 | |
Lorenzo Colitti | 932c44c | 2016-04-24 16:58:02 +0900 | [diff] [blame] | 31 | class FirewallControllerTest : public IptablesBaseTest { |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 32 | protected: |
Lorenzo Colitti | 932c44c | 2016-04-24 16:58:02 +0900 | [diff] [blame] | 33 | FirewallControllerTest() { |
| 34 | FirewallController::execIptables = fakeExecIptables; |
| 35 | FirewallController::execIptablesSilently = fakeExecIptables; |
| 36 | FirewallController::execIptablesRestore = fakeExecIptablesRestore; |
| 37 | } |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 38 | FirewallController mFw; |
Lorenzo Colitti | 932c44c | 2016-04-24 16:58:02 +0900 | [diff] [blame] | 39 | |
Lorenzo Colitti | f157caf | 2016-05-13 11:25:54 +0900 | [diff] [blame] | 40 | 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 Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 43 | } |
Lorenzo Colitti | 932c44c | 2016-04-24 16:58:02 +0900 | [diff] [blame] | 44 | |
Lorenzo Colitti | 03b23fe | 2017-02-03 18:46:53 +0900 | [diff] [blame] | 45 | int createChain(const char* a, FirewallType b) { |
| 46 | return mFw.createChain(a, b); |
Lorenzo Colitti | 932c44c | 2016-04-24 16:58:02 +0900 | [diff] [blame] | 47 | } |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 48 | }; |
| 49 | |
| 50 | |
Lorenzo Colitti | 932c44c | 2016-04-24 16:58:02 +0900 | [diff] [blame] | 51 | TEST_F(FirewallControllerTest, TestCreateWhitelistChain) { |
Lorenzo Colitti | a55388e | 2016-05-13 17:03:42 +0900 | [diff] [blame] | 52 | std::vector<std::string> expectedRestore4 = { |
| 53 | "*filter", |
| 54 | ":fw_whitelist -", |
Lorenzo Colitti | 8bcb1f4 | 2017-04-25 00:17:48 +0900 | [diff] [blame] | 55 | "-A fw_whitelist -m owner --uid-owner 0-9999 -j RETURN", |
Lorenzo Colitti | 50b198a | 2017-03-30 02:50:09 +0900 | [diff] [blame] | 56 | "-A fw_whitelist -i lo -j RETURN", |
| 57 | "-A fw_whitelist -o lo -j RETURN", |
Lorenzo Colitti | a55388e | 2016-05-13 17:03:42 +0900 | [diff] [blame] | 58 | "-A fw_whitelist -p tcp --tcp-flags RST RST -j RETURN", |
Lorenzo Colitti | a55388e | 2016-05-13 17:03:42 +0900 | [diff] [blame] | 59 | "-A fw_whitelist -j DROP", |
Lorenzo Colitti | 03b23fe | 2017-02-03 18:46:53 +0900 | [diff] [blame] | 60 | "COMMIT\n" |
Lorenzo Colitti | a55388e | 2016-05-13 17:03:42 +0900 | [diff] [blame] | 61 | }; |
| 62 | std::vector<std::string> expectedRestore6 = { |
| 63 | "*filter", |
| 64 | ":fw_whitelist -", |
Lorenzo Colitti | 8bcb1f4 | 2017-04-25 00:17:48 +0900 | [diff] [blame] | 65 | "-A fw_whitelist -m owner --uid-owner 0-9999 -j RETURN", |
Lorenzo Colitti | 50b198a | 2017-03-30 02:50:09 +0900 | [diff] [blame] | 66 | "-A fw_whitelist -i lo -j RETURN", |
| 67 | "-A fw_whitelist -o lo -j RETURN", |
Lorenzo Colitti | a55388e | 2016-05-13 17:03:42 +0900 | [diff] [blame] | 68 | "-A fw_whitelist -p tcp --tcp-flags RST RST -j RETURN", |
| 69 | "-A fw_whitelist -p icmpv6 --icmpv6-type packet-too-big -j RETURN", |
| 70 | "-A fw_whitelist -p icmpv6 --icmpv6-type router-solicitation -j RETURN", |
| 71 | "-A fw_whitelist -p icmpv6 --icmpv6-type router-advertisement -j RETURN", |
| 72 | "-A fw_whitelist -p icmpv6 --icmpv6-type neighbour-solicitation -j RETURN", |
| 73 | "-A fw_whitelist -p icmpv6 --icmpv6-type neighbour-advertisement -j RETURN", |
| 74 | "-A fw_whitelist -p icmpv6 --icmpv6-type redirect -j RETURN", |
Lorenzo Colitti | a55388e | 2016-05-13 17:03:42 +0900 | [diff] [blame] | 75 | "-A fw_whitelist -j DROP", |
Lorenzo Colitti | 03b23fe | 2017-02-03 18:46:53 +0900 | [diff] [blame] | 76 | "COMMIT\n" |
Lorenzo Colitti | a55388e | 2016-05-13 17:03:42 +0900 | [diff] [blame] | 77 | }; |
| 78 | std::vector<std::pair<IptablesTarget, std::string>> expectedRestoreCommands = { |
| 79 | { V4, android::base::Join(expectedRestore4, '\n') }, |
| 80 | { V6, android::base::Join(expectedRestore6, '\n') }, |
| 81 | }; |
| 82 | |
Lorenzo Colitti | 03b23fe | 2017-02-03 18:46:53 +0900 | [diff] [blame] | 83 | createChain("fw_whitelist", WHITELIST); |
Lorenzo Colitti | a55388e | 2016-05-13 17:03:42 +0900 | [diff] [blame] | 84 | expectIptablesRestoreCommands(expectedRestoreCommands); |
Lorenzo Colitti | 932c44c | 2016-04-24 16:58:02 +0900 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | TEST_F(FirewallControllerTest, TestCreateBlacklistChain) { |
Lorenzo Colitti | a55388e | 2016-05-13 17:03:42 +0900 | [diff] [blame] | 88 | std::vector<std::string> expectedRestore = { |
| 89 | "*filter", |
| 90 | ":fw_blacklist -", |
Lorenzo Colitti | 50b198a | 2017-03-30 02:50:09 +0900 | [diff] [blame] | 91 | "-A fw_blacklist -i lo -j RETURN", |
| 92 | "-A fw_blacklist -o lo -j RETURN", |
Lorenzo Colitti | a55388e | 2016-05-13 17:03:42 +0900 | [diff] [blame] | 93 | "-A fw_blacklist -p tcp --tcp-flags RST RST -j RETURN", |
Lorenzo Colitti | 03b23fe | 2017-02-03 18:46:53 +0900 | [diff] [blame] | 94 | "COMMIT\n" |
Lorenzo Colitti | a55388e | 2016-05-13 17:03:42 +0900 | [diff] [blame] | 95 | }; |
| 96 | std::vector<std::pair<IptablesTarget, std::string>> expectedRestoreCommands = { |
| 97 | { V4, android::base::Join(expectedRestore, '\n') }, |
| 98 | { V6, android::base::Join(expectedRestore, '\n') }, |
| 99 | }; |
| 100 | |
Lorenzo Colitti | 03b23fe | 2017-02-03 18:46:53 +0900 | [diff] [blame] | 101 | createChain("fw_blacklist", BLACKLIST); |
Lorenzo Colitti | a55388e | 2016-05-13 17:03:42 +0900 | [diff] [blame] | 102 | expectIptablesRestoreCommands(expectedRestoreCommands); |
Lorenzo Colitti | 932c44c | 2016-04-24 16:58:02 +0900 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | TEST_F(FirewallControllerTest, TestSetStandbyRule) { |
| 106 | ExpectedIptablesCommands expected = { |
Lorenzo Colitti | a735765 | 2017-04-25 00:16:36 +0900 | [diff] [blame] | 107 | { V4V6, "*filter\n-D fw_standby -m owner --uid-owner 12345 -j DROP\nCOMMIT\n" } |
Lorenzo Colitti | 932c44c | 2016-04-24 16:58:02 +0900 | [diff] [blame] | 108 | }; |
| 109 | mFw.setUidRule(STANDBY, 12345, ALLOW); |
Lorenzo Colitti | a735765 | 2017-04-25 00:16:36 +0900 | [diff] [blame] | 110 | expectIptablesRestoreCommands(expected); |
Lorenzo Colitti | 932c44c | 2016-04-24 16:58:02 +0900 | [diff] [blame] | 111 | |
| 112 | expected = { |
Lorenzo Colitti | a735765 | 2017-04-25 00:16:36 +0900 | [diff] [blame] | 113 | { V4V6, "*filter\n-A fw_standby -m owner --uid-owner 12345 -j DROP\nCOMMIT\n" } |
Lorenzo Colitti | 932c44c | 2016-04-24 16:58:02 +0900 | [diff] [blame] | 114 | }; |
| 115 | mFw.setUidRule(STANDBY, 12345, DENY); |
Lorenzo Colitti | a735765 | 2017-04-25 00:16:36 +0900 | [diff] [blame] | 116 | expectIptablesRestoreCommands(expected); |
Lorenzo Colitti | 932c44c | 2016-04-24 16:58:02 +0900 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | TEST_F(FirewallControllerTest, TestSetDozeRule) { |
| 120 | ExpectedIptablesCommands expected = { |
Lorenzo Colitti | a735765 | 2017-04-25 00:16:36 +0900 | [diff] [blame] | 121 | { V4V6, "*filter\n-I fw_dozable -m owner --uid-owner 54321 -j RETURN\nCOMMIT\n" } |
Lorenzo Colitti | 932c44c | 2016-04-24 16:58:02 +0900 | [diff] [blame] | 122 | }; |
| 123 | mFw.setUidRule(DOZABLE, 54321, ALLOW); |
Lorenzo Colitti | a735765 | 2017-04-25 00:16:36 +0900 | [diff] [blame] | 124 | expectIptablesRestoreCommands(expected); |
Lorenzo Colitti | 932c44c | 2016-04-24 16:58:02 +0900 | [diff] [blame] | 125 | |
| 126 | expected = { |
Lorenzo Colitti | a735765 | 2017-04-25 00:16:36 +0900 | [diff] [blame] | 127 | { V4V6, "*filter\n-D fw_dozable -m owner --uid-owner 54321 -j RETURN\nCOMMIT\n" } |
Lorenzo Colitti | 932c44c | 2016-04-24 16:58:02 +0900 | [diff] [blame] | 128 | }; |
| 129 | mFw.setUidRule(DOZABLE, 54321, DENY); |
Lorenzo Colitti | a735765 | 2017-04-25 00:16:36 +0900 | [diff] [blame] | 130 | expectIptablesRestoreCommands(expected); |
| 131 | } |
| 132 | |
| 133 | TEST_F(FirewallControllerTest, TestSetFirewallRule) { |
| 134 | ExpectedIptablesCommands expected = { |
| 135 | { V4V6, "*filter\n" |
| 136 | "-A fw_INPUT -m owner --uid-owner 54321 -j DROP\n" |
| 137 | "-A fw_OUTPUT -m owner --uid-owner 54321 -j DROP\n" |
| 138 | "COMMIT\n" } |
| 139 | }; |
| 140 | mFw.setUidRule(NONE, 54321, DENY); |
| 141 | expectIptablesRestoreCommands(expected); |
| 142 | |
| 143 | expected = { |
| 144 | { V4V6, "*filter\n" |
| 145 | "-D fw_INPUT -m owner --uid-owner 54321 -j DROP\n" |
| 146 | "-D fw_OUTPUT -m owner --uid-owner 54321 -j DROP\n" |
| 147 | "COMMIT\n" } |
| 148 | }; |
| 149 | mFw.setUidRule(NONE, 54321, ALLOW); |
| 150 | expectIptablesRestoreCommands(expected); |
Lorenzo Colitti | 932c44c | 2016-04-24 16:58:02 +0900 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | TEST_F(FirewallControllerTest, TestReplaceWhitelistUidRule) { |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 154 | std::string expected = |
| 155 | "*filter\n" |
| 156 | ":FW_whitechain -\n" |
Lorenzo Colitti | 8bcb1f4 | 2017-04-25 00:17:48 +0900 | [diff] [blame] | 157 | "-A FW_whitechain -m owner --uid-owner 10023 -j RETURN\n" |
| 158 | "-A FW_whitechain -m owner --uid-owner 10059 -j RETURN\n" |
| 159 | "-A FW_whitechain -m owner --uid-owner 10124 -j RETURN\n" |
| 160 | "-A FW_whitechain -m owner --uid-owner 10111 -j RETURN\n" |
| 161 | "-A FW_whitechain -m owner --uid-owner 110122 -j RETURN\n" |
| 162 | "-A FW_whitechain -m owner --uid-owner 210153 -j RETURN\n" |
| 163 | "-A FW_whitechain -m owner --uid-owner 210024 -j RETURN\n" |
| 164 | "-A FW_whitechain -m owner --uid-owner 0-9999 -j RETURN\n" |
Lorenzo Colitti | 50b198a | 2017-03-30 02:50:09 +0900 | [diff] [blame] | 165 | "-A FW_whitechain -i lo -j RETURN\n" |
| 166 | "-A FW_whitechain -o lo -j RETURN\n" |
Lorenzo Colitti | f157caf | 2016-05-13 11:25:54 +0900 | [diff] [blame] | 167 | "-A FW_whitechain -p tcp --tcp-flags RST RST -j RETURN\n" |
| 168 | "-A FW_whitechain -p icmpv6 --icmpv6-type packet-too-big -j RETURN\n" |
| 169 | "-A FW_whitechain -p icmpv6 --icmpv6-type router-solicitation -j RETURN\n" |
| 170 | "-A FW_whitechain -p icmpv6 --icmpv6-type router-advertisement -j RETURN\n" |
| 171 | "-A FW_whitechain -p icmpv6 --icmpv6-type neighbour-solicitation -j RETURN\n" |
| 172 | "-A FW_whitechain -p icmpv6 --icmpv6-type neighbour-advertisement -j RETURN\n" |
| 173 | "-A FW_whitechain -p icmpv6 --icmpv6-type redirect -j RETURN\n" |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 174 | "-A FW_whitechain -j DROP\n" |
Lorenzo Colitti | 03b23fe | 2017-02-03 18:46:53 +0900 | [diff] [blame] | 175 | "COMMIT\n"; |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 176 | |
| 177 | std::vector<int32_t> uids = { 10023, 10059, 10124, 10111, 110122, 210153, 210024 }; |
Lorenzo Colitti | f157caf | 2016-05-13 11:25:54 +0900 | [diff] [blame] | 178 | EXPECT_EQ(expected, makeUidRules(V6, "FW_whitechain", true, uids)); |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 179 | } |
| 180 | |
Lorenzo Colitti | 932c44c | 2016-04-24 16:58:02 +0900 | [diff] [blame] | 181 | TEST_F(FirewallControllerTest, TestReplaceBlacklistUidRule) { |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 182 | std::string expected = |
| 183 | "*filter\n" |
| 184 | ":FW_blackchain -\n" |
Lorenzo Colitti | 50b198a | 2017-03-30 02:50:09 +0900 | [diff] [blame] | 185 | "-A FW_blackchain -i lo -j RETURN\n" |
| 186 | "-A FW_blackchain -o lo -j RETURN\n" |
Lorenzo Colitti | f157caf | 2016-05-13 11:25:54 +0900 | [diff] [blame] | 187 | "-A FW_blackchain -p tcp --tcp-flags RST RST -j RETURN\n" |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 188 | "-A FW_blackchain -m owner --uid-owner 10023 -j DROP\n" |
| 189 | "-A FW_blackchain -m owner --uid-owner 10059 -j DROP\n" |
| 190 | "-A FW_blackchain -m owner --uid-owner 10124 -j DROP\n" |
Lorenzo Colitti | 03b23fe | 2017-02-03 18:46:53 +0900 | [diff] [blame] | 191 | "COMMIT\n"; |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 192 | |
| 193 | std::vector<int32_t> uids = { 10023, 10059, 10124 }; |
Lorenzo Colitti | f157caf | 2016-05-13 11:25:54 +0900 | [diff] [blame] | 194 | EXPECT_EQ(expected, makeUidRules(V4 ,"FW_blackchain", false, uids)); |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 195 | } |