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 | f157caf | 2016-05-13 11:25:54 +0900 | [diff] [blame] | 45 | int createChain(const char* a, const char* b , FirewallType c) { |
Lorenzo Colitti | 932c44c | 2016-04-24 16:58:02 +0900 | [diff] [blame] | 46 | return mFw.createChain(a, b, c); |
| 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 | ExpectedIptablesCommands expectedCommands = { |
Lorenzo Colitti | 932c44c | 2016-04-24 16:58:02 +0900 | [diff] [blame] | 53 | { V4V6, "-t filter -D INPUT -j fw_whitelist" }, |
Lorenzo Colitti | 932c44c | 2016-04-24 16:58:02 +0900 | [diff] [blame] | 54 | }; |
Lorenzo Colitti | a55388e | 2016-05-13 17:03:42 +0900 | [diff] [blame] | 55 | |
| 56 | std::vector<std::string> expectedRestore4 = { |
| 57 | "*filter", |
| 58 | ":fw_whitelist -", |
Lorenzo Colitti | 238e818 | 2016-07-26 17:59:41 +0900 | [diff] [blame^] | 59 | "-A fw_whitelist -i lo -o lo -j RETURN", |
Lorenzo Colitti | a55388e | 2016-05-13 17:03:42 +0900 | [diff] [blame] | 60 | "-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 Colitti | 238e818 | 2016-07-26 17:59:41 +0900 | [diff] [blame^] | 68 | "-A fw_whitelist -i lo -o lo -j RETURN", |
Lorenzo Colitti | a55388e | 2016-05-13 17:03:42 +0900 | [diff] [blame] | 69 | "-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 Colitti | 932c44c | 2016-04-24 16:58:02 +0900 | [diff] [blame] | 85 | createChain("fw_whitelist", "INPUT", WHITELIST); |
Lorenzo Colitti | a55388e | 2016-05-13 17:03:42 +0900 | [diff] [blame] | 86 | expectIptablesCommands(expectedCommands); |
| 87 | expectIptablesRestoreCommands(expectedRestoreCommands); |
Lorenzo Colitti | 932c44c | 2016-04-24 16:58:02 +0900 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | TEST_F(FirewallControllerTest, TestCreateBlacklistChain) { |
Lorenzo Colitti | a55388e | 2016-05-13 17:03:42 +0900 | [diff] [blame] | 91 | ExpectedIptablesCommands expectedCommands = { |
Lorenzo Colitti | 932c44c | 2016-04-24 16:58:02 +0900 | [diff] [blame] | 92 | { V4V6, "-t filter -D INPUT -j fw_blacklist" }, |
Lorenzo Colitti | 932c44c | 2016-04-24 16:58:02 +0900 | [diff] [blame] | 93 | }; |
Lorenzo Colitti | a55388e | 2016-05-13 17:03:42 +0900 | [diff] [blame] | 94 | |
| 95 | std::vector<std::string> expectedRestore = { |
| 96 | "*filter", |
| 97 | ":fw_blacklist -", |
Lorenzo Colitti | 238e818 | 2016-07-26 17:59:41 +0900 | [diff] [blame^] | 98 | "-A fw_blacklist -i lo -o lo -j RETURN", |
Lorenzo Colitti | a55388e | 2016-05-13 17:03:42 +0900 | [diff] [blame] | 99 | "-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 Colitti | 932c44c | 2016-04-24 16:58:02 +0900 | [diff] [blame] | 107 | createChain("fw_blacklist", "INPUT", BLACKLIST); |
Lorenzo Colitti | a55388e | 2016-05-13 17:03:42 +0900 | [diff] [blame] | 108 | expectIptablesCommands(expectedCommands); |
| 109 | expectIptablesRestoreCommands(expectedRestoreCommands); |
Lorenzo Colitti | 932c44c | 2016-04-24 16:58:02 +0900 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | TEST_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 | |
| 126 | TEST_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 | |
| 140 | TEST_F(FirewallControllerTest, TestReplaceWhitelistUidRule) { |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 141 | std::string expected = |
| 142 | "*filter\n" |
| 143 | ":FW_whitechain -\n" |
Lorenzo Colitti | 238e818 | 2016-07-26 17:59:41 +0900 | [diff] [blame^] | 144 | "-A FW_whitechain -i lo -o lo -j RETURN\n" |
Lorenzo Colitti | f157caf | 2016-05-13 11:25:54 +0900 | [diff] [blame] | 145 | "-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 Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 152 | "-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 Colitti | f157caf | 2016-05-13 11:25:54 +0900 | [diff] [blame] | 164 | EXPECT_EQ(expected, makeUidRules(V6, "FW_whitechain", true, uids)); |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 165 | } |
| 166 | |
Lorenzo Colitti | 932c44c | 2016-04-24 16:58:02 +0900 | [diff] [blame] | 167 | TEST_F(FirewallControllerTest, TestReplaceBlacklistUidRule) { |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 168 | std::string expected = |
| 169 | "*filter\n" |
| 170 | ":FW_blackchain -\n" |
Lorenzo Colitti | 238e818 | 2016-07-26 17:59:41 +0900 | [diff] [blame^] | 171 | "-A FW_blackchain -i lo -o lo -j RETURN\n" |
Lorenzo Colitti | f157caf | 2016-05-13 11:25:54 +0900 | [diff] [blame] | 172 | "-A FW_blackchain -p tcp --tcp-flags RST RST -j RETURN\n" |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 173 | "-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 Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 176 | "COMMIT\n\x04"; |
| 177 | |
| 178 | std::vector<int32_t> uids = { 10023, 10059, 10124 }; |
Lorenzo Colitti | f157caf | 2016-05-13 11:25:54 +0900 | [diff] [blame] | 179 | EXPECT_EQ(expected, makeUidRules(V4 ,"FW_blackchain", false, uids)); |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 180 | } |