blob: 1b53fb858e6a499d6672093f4f1dcf2300dc4cfd [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
Hugo Benichi528d3d02018-06-20 13:35:58 +090025#include <android-base/file.h>
Lorenzo Colitti6324b182017-07-17 21:48:14 +090026#include <android-base/stringprintf.h>
Hugo Benichi528d3d02018-06-20 13:35:58 +090027#include <android-base/strings.h>
Lorenzo Colittia55388e2016-05-13 17:03:42 +090028
Lorenzo Colitti89faa342016-02-26 11:38:47 +090029#include "FirewallController.h"
Lorenzo Colitti932c44c2016-04-24 16:58:02 +090030#include "IptablesBaseTest.h"
Lorenzo Colitti89faa342016-02-26 11:38:47 +090031
Lorenzo Colittid351bea2017-07-16 22:52:30 +090032using android::base::Join;
Hugo Benichi528d3d02018-06-20 13:35:58 +090033using android::base::WriteStringToFile;
Lorenzo Colitti89faa342016-02-26 11:38:47 +090034
Luke Huange64fa382018-07-24 16:38:22 +080035namespace android {
36namespace net {
37
Lorenzo Colitti932c44c2016-04-24 16:58:02 +090038class FirewallControllerTest : public IptablesBaseTest {
Lorenzo Colitti89faa342016-02-26 11:38:47 +090039protected:
Lorenzo Colitti932c44c2016-04-24 16:58:02 +090040 FirewallControllerTest() {
Lorenzo Colitti932c44c2016-04-24 16:58:02 +090041 FirewallController::execIptablesRestore = fakeExecIptablesRestore;
Chenbo Feng47dd0732018-12-11 12:23:24 -080042 // This unit test currently doesn't cover the eBPF owner match case so
43 // we have to manually turn eBPF support off.
44 // TODO: find a way to unit test the eBPF code path.
Maciej Żenczykowski10a58922020-02-11 15:40:19 -080045 mFw.mUseBpfOwnerMatch = false;
Lorenzo Colitti932c44c2016-04-24 16:58:02 +090046 }
Lorenzo Colitti89faa342016-02-26 11:38:47 +090047 FirewallController mFw;
Lorenzo Colitti932c44c2016-04-24 16:58:02 +090048
Lorenzo Colittif157caf2016-05-13 11:25:54 +090049 std::string makeUidRules(IptablesTarget a, const char* b, bool c,
50 const std::vector<int32_t>& d) {
51 return mFw.makeUidRules(a, b, c, d);
Lorenzo Colitti89faa342016-02-26 11:38:47 +090052 }
Lorenzo Colitti932c44c2016-04-24 16:58:02 +090053
Lorenzo Colitti03b23fe2017-02-03 18:46:53 +090054 int createChain(const char* a, FirewallType b) {
55 return mFw.createChain(a, b);
Lorenzo Colitti932c44c2016-04-24 16:58:02 +090056 }
Lorenzo Colitti89faa342016-02-26 11:38:47 +090057};
58
Lorenzo Colitti932c44c2016-04-24 16:58:02 +090059TEST_F(FirewallControllerTest, TestCreateWhitelistChain) {
Lorenzo Colittia55388e2016-05-13 17:03:42 +090060 std::vector<std::string> expectedRestore4 = {
61 "*filter",
62 ":fw_whitelist -",
Lorenzo Colitti8bcb1f42017-04-25 00:17:48 +090063 "-A fw_whitelist -m owner --uid-owner 0-9999 -j RETURN",
Benedict Wongb2daefb2017-12-06 22:05:46 -080064 "-A fw_whitelist -m owner ! --uid-owner 0-4294967294 -j RETURN",
65 "-A fw_whitelist -p esp -j RETURN",
Lorenzo Colitti50b198a2017-03-30 02:50:09 +090066 "-A fw_whitelist -i lo -j RETURN",
67 "-A fw_whitelist -o lo -j RETURN",
Lorenzo Colittia55388e2016-05-13 17:03:42 +090068 "-A fw_whitelist -p tcp --tcp-flags RST RST -j RETURN",
Lorenzo Colittia55388e2016-05-13 17:03:42 +090069 "-A fw_whitelist -j DROP",
Lorenzo Colitti03b23fe2017-02-03 18:46:53 +090070 "COMMIT\n"
Lorenzo Colittia55388e2016-05-13 17:03:42 +090071 };
72 std::vector<std::string> expectedRestore6 = {
73 "*filter",
74 ":fw_whitelist -",
Lorenzo Colitti8bcb1f42017-04-25 00:17:48 +090075 "-A fw_whitelist -m owner --uid-owner 0-9999 -j RETURN",
Benedict Wongb2daefb2017-12-06 22:05:46 -080076 "-A fw_whitelist -m owner ! --uid-owner 0-4294967294 -j RETURN",
77 "-A fw_whitelist -p esp -j RETURN",
Lorenzo Colitti50b198a2017-03-30 02:50:09 +090078 "-A fw_whitelist -i lo -j RETURN",
79 "-A fw_whitelist -o lo -j RETURN",
Lorenzo Colittia55388e2016-05-13 17:03:42 +090080 "-A fw_whitelist -p tcp --tcp-flags RST RST -j RETURN",
81 "-A fw_whitelist -p icmpv6 --icmpv6-type packet-too-big -j RETURN",
82 "-A fw_whitelist -p icmpv6 --icmpv6-type router-solicitation -j RETURN",
83 "-A fw_whitelist -p icmpv6 --icmpv6-type router-advertisement -j RETURN",
84 "-A fw_whitelist -p icmpv6 --icmpv6-type neighbour-solicitation -j RETURN",
85 "-A fw_whitelist -p icmpv6 --icmpv6-type neighbour-advertisement -j RETURN",
86 "-A fw_whitelist -p icmpv6 --icmpv6-type redirect -j RETURN",
Lorenzo Colittia55388e2016-05-13 17:03:42 +090087 "-A fw_whitelist -j DROP",
Lorenzo Colitti03b23fe2017-02-03 18:46:53 +090088 "COMMIT\n"
Lorenzo Colittia55388e2016-05-13 17:03:42 +090089 };
90 std::vector<std::pair<IptablesTarget, std::string>> expectedRestoreCommands = {
Bernie Innocentia5161a02019-01-30 22:40:53 +090091 {V4, Join(expectedRestore4, '\n')},
92 {V6, Join(expectedRestore6, '\n')},
Lorenzo Colittia55388e2016-05-13 17:03:42 +090093 };
94
Lorenzo Colitti03b23fe2017-02-03 18:46:53 +090095 createChain("fw_whitelist", WHITELIST);
Lorenzo Colittia55388e2016-05-13 17:03:42 +090096 expectIptablesRestoreCommands(expectedRestoreCommands);
Lorenzo Colitti932c44c2016-04-24 16:58:02 +090097}
98
99TEST_F(FirewallControllerTest, TestCreateBlacklistChain) {
Lorenzo Colittia55388e2016-05-13 17:03:42 +0900100 std::vector<std::string> expectedRestore = {
101 "*filter",
102 ":fw_blacklist -",
Lorenzo Colitti50b198a2017-03-30 02:50:09 +0900103 "-A fw_blacklist -i lo -j RETURN",
104 "-A fw_blacklist -o lo -j RETURN",
Lorenzo Colittia55388e2016-05-13 17:03:42 +0900105 "-A fw_blacklist -p tcp --tcp-flags RST RST -j RETURN",
Lorenzo Colitti03b23fe2017-02-03 18:46:53 +0900106 "COMMIT\n"
Lorenzo Colittia55388e2016-05-13 17:03:42 +0900107 };
108 std::vector<std::pair<IptablesTarget, std::string>> expectedRestoreCommands = {
Bernie Innocentia5161a02019-01-30 22:40:53 +0900109 {V4, Join(expectedRestore, '\n')},
110 {V6, Join(expectedRestore, '\n')},
Lorenzo Colittia55388e2016-05-13 17:03:42 +0900111 };
112
Lorenzo Colitti03b23fe2017-02-03 18:46:53 +0900113 createChain("fw_blacklist", BLACKLIST);
Lorenzo Colittia55388e2016-05-13 17:03:42 +0900114 expectIptablesRestoreCommands(expectedRestoreCommands);
Lorenzo Colitti932c44c2016-04-24 16:58:02 +0900115}
116
117TEST_F(FirewallControllerTest, TestSetStandbyRule) {
118 ExpectedIptablesCommands expected = {
Lorenzo Colittia7357652017-04-25 00:16:36 +0900119 { V4V6, "*filter\n-D fw_standby -m owner --uid-owner 12345 -j DROP\nCOMMIT\n" }
Lorenzo Colitti932c44c2016-04-24 16:58:02 +0900120 };
121 mFw.setUidRule(STANDBY, 12345, ALLOW);
Lorenzo Colittia7357652017-04-25 00:16:36 +0900122 expectIptablesRestoreCommands(expected);
Lorenzo Colitti932c44c2016-04-24 16:58:02 +0900123
124 expected = {
Lorenzo Colittia7357652017-04-25 00:16:36 +0900125 { V4V6, "*filter\n-A fw_standby -m owner --uid-owner 12345 -j DROP\nCOMMIT\n" }
Lorenzo Colitti932c44c2016-04-24 16:58:02 +0900126 };
127 mFw.setUidRule(STANDBY, 12345, DENY);
Lorenzo Colittia7357652017-04-25 00:16:36 +0900128 expectIptablesRestoreCommands(expected);
Lorenzo Colitti932c44c2016-04-24 16:58:02 +0900129}
130
131TEST_F(FirewallControllerTest, TestSetDozeRule) {
132 ExpectedIptablesCommands expected = {
Lorenzo Colittia7357652017-04-25 00:16:36 +0900133 { V4V6, "*filter\n-I fw_dozable -m owner --uid-owner 54321 -j RETURN\nCOMMIT\n" }
Lorenzo Colitti932c44c2016-04-24 16:58:02 +0900134 };
135 mFw.setUidRule(DOZABLE, 54321, ALLOW);
Lorenzo Colittia7357652017-04-25 00:16:36 +0900136 expectIptablesRestoreCommands(expected);
Lorenzo Colitti932c44c2016-04-24 16:58:02 +0900137
138 expected = {
Lorenzo Colittia7357652017-04-25 00:16:36 +0900139 { V4V6, "*filter\n-D fw_dozable -m owner --uid-owner 54321 -j RETURN\nCOMMIT\n" }
Lorenzo Colitti932c44c2016-04-24 16:58:02 +0900140 };
141 mFw.setUidRule(DOZABLE, 54321, DENY);
Lorenzo Colittia7357652017-04-25 00:16:36 +0900142 expectIptablesRestoreCommands(expected);
143}
144
145TEST_F(FirewallControllerTest, TestSetFirewallRule) {
146 ExpectedIptablesCommands expected = {
147 { V4V6, "*filter\n"
148 "-A fw_INPUT -m owner --uid-owner 54321 -j DROP\n"
149 "-A fw_OUTPUT -m owner --uid-owner 54321 -j DROP\n"
150 "COMMIT\n" }
151 };
152 mFw.setUidRule(NONE, 54321, DENY);
153 expectIptablesRestoreCommands(expected);
154
155 expected = {
156 { V4V6, "*filter\n"
157 "-D fw_INPUT -m owner --uid-owner 54321 -j DROP\n"
158 "-D fw_OUTPUT -m owner --uid-owner 54321 -j DROP\n"
159 "COMMIT\n" }
160 };
161 mFw.setUidRule(NONE, 54321, ALLOW);
162 expectIptablesRestoreCommands(expected);
Lorenzo Colitti932c44c2016-04-24 16:58:02 +0900163}
164
165TEST_F(FirewallControllerTest, TestReplaceWhitelistUidRule) {
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900166 std::string expected =
167 "*filter\n"
168 ":FW_whitechain -\n"
Lorenzo Colitti8bcb1f42017-04-25 00:17:48 +0900169 "-A FW_whitechain -m owner --uid-owner 10023 -j RETURN\n"
170 "-A FW_whitechain -m owner --uid-owner 10059 -j RETURN\n"
171 "-A FW_whitechain -m owner --uid-owner 10124 -j RETURN\n"
172 "-A FW_whitechain -m owner --uid-owner 10111 -j RETURN\n"
173 "-A FW_whitechain -m owner --uid-owner 110122 -j RETURN\n"
174 "-A FW_whitechain -m owner --uid-owner 210153 -j RETURN\n"
175 "-A FW_whitechain -m owner --uid-owner 210024 -j RETURN\n"
176 "-A FW_whitechain -m owner --uid-owner 0-9999 -j RETURN\n"
Benedict Wongb2daefb2017-12-06 22:05:46 -0800177 "-A FW_whitechain -m owner ! --uid-owner 0-4294967294 -j RETURN\n"
178 "-A FW_whitechain -p esp -j RETURN\n"
Lorenzo Colitti50b198a2017-03-30 02:50:09 +0900179 "-A FW_whitechain -i lo -j RETURN\n"
180 "-A FW_whitechain -o lo -j RETURN\n"
Lorenzo Colittif157caf2016-05-13 11:25:54 +0900181 "-A FW_whitechain -p tcp --tcp-flags RST RST -j RETURN\n"
182 "-A FW_whitechain -p icmpv6 --icmpv6-type packet-too-big -j RETURN\n"
183 "-A FW_whitechain -p icmpv6 --icmpv6-type router-solicitation -j RETURN\n"
184 "-A FW_whitechain -p icmpv6 --icmpv6-type router-advertisement -j RETURN\n"
185 "-A FW_whitechain -p icmpv6 --icmpv6-type neighbour-solicitation -j RETURN\n"
186 "-A FW_whitechain -p icmpv6 --icmpv6-type neighbour-advertisement -j RETURN\n"
187 "-A FW_whitechain -p icmpv6 --icmpv6-type redirect -j RETURN\n"
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900188 "-A FW_whitechain -j DROP\n"
Lorenzo Colitti03b23fe2017-02-03 18:46:53 +0900189 "COMMIT\n";
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900190
191 std::vector<int32_t> uids = { 10023, 10059, 10124, 10111, 110122, 210153, 210024 };
Lorenzo Colittif157caf2016-05-13 11:25:54 +0900192 EXPECT_EQ(expected, makeUidRules(V6, "FW_whitechain", true, uids));
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900193}
194
Lorenzo Colitti932c44c2016-04-24 16:58:02 +0900195TEST_F(FirewallControllerTest, TestReplaceBlacklistUidRule) {
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900196 std::string expected =
197 "*filter\n"
198 ":FW_blackchain -\n"
Lorenzo Colitti50b198a2017-03-30 02:50:09 +0900199 "-A FW_blackchain -i lo -j RETURN\n"
200 "-A FW_blackchain -o lo -j RETURN\n"
Lorenzo Colittif157caf2016-05-13 11:25:54 +0900201 "-A FW_blackchain -p tcp --tcp-flags RST RST -j RETURN\n"
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900202 "-A FW_blackchain -m owner --uid-owner 10023 -j DROP\n"
203 "-A FW_blackchain -m owner --uid-owner 10059 -j DROP\n"
204 "-A FW_blackchain -m owner --uid-owner 10124 -j DROP\n"
Lorenzo Colitti03b23fe2017-02-03 18:46:53 +0900205 "COMMIT\n";
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900206
207 std::vector<int32_t> uids = { 10023, 10059, 10124 };
Lorenzo Colittif157caf2016-05-13 11:25:54 +0900208 EXPECT_EQ(expected, makeUidRules(V4 ,"FW_blackchain", false, uids));
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900209}
Lorenzo Colittidd9a54b2017-04-26 16:13:22 +0900210
211TEST_F(FirewallControllerTest, TestEnableChildChains) {
212 std::vector<std::string> expected = {
Lorenzo Colittia1611962017-04-26 16:30:39 +0900213 "*filter\n"
214 "-A fw_INPUT -j fw_dozable\n"
215 "-A fw_OUTPUT -j fw_dozable\n"
216 "COMMIT\n"
Lorenzo Colittidd9a54b2017-04-26 16:13:22 +0900217 };
218 EXPECT_EQ(0, mFw.enableChildChains(DOZABLE, true));
Lorenzo Colittia1611962017-04-26 16:30:39 +0900219 expectIptablesRestoreCommands(expected);
Lorenzo Colittidd9a54b2017-04-26 16:13:22 +0900220
221 expected = {
Lorenzo Colittia1611962017-04-26 16:30:39 +0900222 "*filter\n"
223 "-D fw_INPUT -j fw_powersave\n"
224 "-D fw_OUTPUT -j fw_powersave\n"
225 "COMMIT\n"
Lorenzo Colittidd9a54b2017-04-26 16:13:22 +0900226 };
227 EXPECT_EQ(0, mFw.enableChildChains(POWERSAVE, false));
Lorenzo Colittia1611962017-04-26 16:30:39 +0900228 expectIptablesRestoreCommands(expected);
Lorenzo Colittidd9a54b2017-04-26 16:13:22 +0900229}
Lorenzo Colitticc1bb822017-07-16 21:08:54 +0900230
Lorenzo Colitti6324b182017-07-17 21:48:14 +0900231TEST_F(FirewallControllerTest, TestFirewall) {
Lorenzo Colitticc1bb822017-07-16 21:08:54 +0900232 std::vector<std::string> enableCommands = {
Lorenzo Colittid351bea2017-07-16 22:52:30 +0900233 "*filter\n"
234 "-A fw_INPUT -j DROP\n"
235 "-A fw_OUTPUT -j REJECT\n"
236 "-A fw_FORWARD -j REJECT\n"
237 "COMMIT\n"
Lorenzo Colitticc1bb822017-07-16 21:08:54 +0900238 };
239 std::vector<std::string> disableCommands = {
Lorenzo Colittid351bea2017-07-16 22:52:30 +0900240 "*filter\n"
241 ":fw_INPUT -\n"
242 ":fw_OUTPUT -\n"
243 ":fw_FORWARD -\n"
244 "COMMIT\n"
Lorenzo Colitticc1bb822017-07-16 21:08:54 +0900245 };
246 std::vector<std::string> noCommands = {};
247
Luke Huange64fa382018-07-24 16:38:22 +0800248 EXPECT_EQ(0, mFw.resetFirewall());
Lorenzo Colittid351bea2017-07-16 22:52:30 +0900249 expectIptablesRestoreCommands(disableCommands);
Lorenzo Colitticc1bb822017-07-16 21:08:54 +0900250
Luke Huange64fa382018-07-24 16:38:22 +0800251 EXPECT_EQ(0, mFw.resetFirewall());
Lorenzo Colittid351bea2017-07-16 22:52:30 +0900252 expectIptablesRestoreCommands(disableCommands);
Lorenzo Colitticc1bb822017-07-16 21:08:54 +0900253
Luke Huange64fa382018-07-24 16:38:22 +0800254 EXPECT_EQ(0, mFw.setFirewallType(BLACKLIST));
Lorenzo Colittid351bea2017-07-16 22:52:30 +0900255 expectIptablesRestoreCommands(disableCommands);
Lorenzo Colitticc1bb822017-07-16 21:08:54 +0900256
Luke Huange64fa382018-07-24 16:38:22 +0800257 EXPECT_EQ(0, mFw.setFirewallType(BLACKLIST));
Lorenzo Colittid351bea2017-07-16 22:52:30 +0900258 expectIptablesRestoreCommands(noCommands);
Lorenzo Colitticc1bb822017-07-16 21:08:54 +0900259
260 std::vector<std::string> disableEnableCommands;
261 disableEnableCommands.insert(
262 disableEnableCommands.end(), disableCommands.begin(), disableCommands.end());
263 disableEnableCommands.insert(
264 disableEnableCommands.end(), enableCommands.begin(), enableCommands.end());
265
Luke Huange64fa382018-07-24 16:38:22 +0800266 EXPECT_EQ(0, mFw.setFirewallType(WHITELIST));
Lorenzo Colittid351bea2017-07-16 22:52:30 +0900267 expectIptablesRestoreCommands(disableEnableCommands);
Lorenzo Colitticc1bb822017-07-16 21:08:54 +0900268
Lorenzo Colitti6324b182017-07-17 21:48:14 +0900269 std::vector<std::string> ifaceCommands = {
Lorenzo Colitti1411d452017-07-17 22:12:15 +0900270 "*filter\n"
271 "-I fw_INPUT -i rmnet_data0 -j RETURN\n"
272 "-I fw_OUTPUT -o rmnet_data0 -j RETURN\n"
273 "COMMIT\n"
Lorenzo Colitti6324b182017-07-17 21:48:14 +0900274 };
275 EXPECT_EQ(0, mFw.setInterfaceRule("rmnet_data0", ALLOW));
Lorenzo Colitti1411d452017-07-17 22:12:15 +0900276 expectIptablesRestoreCommands(ifaceCommands);
277
278 EXPECT_EQ(0, mFw.setInterfaceRule("rmnet_data0", ALLOW));
279 expectIptablesRestoreCommands(noCommands);
Lorenzo Colitti6324b182017-07-17 21:48:14 +0900280
281 ifaceCommands = {
Lorenzo Colitti1411d452017-07-17 22:12:15 +0900282 "*filter\n"
283 "-D fw_INPUT -i rmnet_data0 -j RETURN\n"
284 "-D fw_OUTPUT -o rmnet_data0 -j RETURN\n"
285 "COMMIT\n"
Lorenzo Colitti6324b182017-07-17 21:48:14 +0900286 };
287 EXPECT_EQ(0, mFw.setInterfaceRule("rmnet_data0", DENY));
Lorenzo Colitti1411d452017-07-17 22:12:15 +0900288 expectIptablesRestoreCommands(ifaceCommands);
289
290 EXPECT_EQ(0, mFw.setInterfaceRule("rmnet_data0", DENY));
291 expectIptablesRestoreCommands(noCommands);
Lorenzo Colitti6324b182017-07-17 21:48:14 +0900292
Luke Huange64fa382018-07-24 16:38:22 +0800293 EXPECT_EQ(0, mFw.setFirewallType(WHITELIST));
Lorenzo Colittid351bea2017-07-16 22:52:30 +0900294 expectIptablesRestoreCommands(noCommands);
Lorenzo Colitticc1bb822017-07-16 21:08:54 +0900295
Luke Huange64fa382018-07-24 16:38:22 +0800296 EXPECT_EQ(0, mFw.resetFirewall());
Lorenzo Colittid351bea2017-07-16 22:52:30 +0900297 expectIptablesRestoreCommands(disableCommands);
Lorenzo Colitticc1bb822017-07-16 21:08:54 +0900298
Luke Huange64fa382018-07-24 16:38:22 +0800299 // TODO: calling resetFirewall and then setFirewallType(WHITELIST) does
Lorenzo Colitticc1bb822017-07-16 21:08:54 +0900300 // nothing. This seems like a clear bug.
Luke Huange64fa382018-07-24 16:38:22 +0800301 EXPECT_EQ(0, mFw.setFirewallType(WHITELIST));
Lorenzo Colittid351bea2017-07-16 22:52:30 +0900302 expectIptablesRestoreCommands(noCommands);
Lorenzo Colitticc1bb822017-07-16 21:08:54 +0900303}
Hugo Benichi528d3d02018-06-20 13:35:58 +0900304
305TEST_F(FirewallControllerTest, TestDiscoverMaximumValidUid) {
306 struct {
307 const std::string description;
308 const std::string content;
309 const uint32_t expected;
310 } testCases[] = {
311 {
312 .description = "root namespace case",
313 .content = " 0 0 4294967295",
314 .expected = 4294967294,
315 },
316 {
317 .description = "container namespace case",
318 .content = " 0 655360 5000\n"
319 " 5000 600 50\n"
320 " 5050 660410 1994950\n",
321 .expected = 1999999,
322 },
323 {
324 .description = "garbage content case",
325 .content = "garbage",
326 .expected = 4294967294,
327 },
328 {
329 .description = "no content case",
330 .content = "",
331 .expected = 4294967294,
332 },
333 };
334
335 const std::string tempFile = "/data/local/tmp/fake_uid_mapping";
336
337 for (const auto& test : testCases) {
338 EXPECT_TRUE(WriteStringToFile(test.content, tempFile, false));
339 uint32_t got = FirewallController::discoverMaximumValidUid(tempFile);
340 EXPECT_EQ(0, remove(tempFile.c_str()));
341 if (got != test.expected) {
342 FAIL() << test.description << ":\n"
343 << test.content << "\ngot " << got << ", but expected " << test.expected;
344 }
345 }
346
347 // Also check when the file is not defined
348 EXPECT_NE(0, access(tempFile.c_str(), F_OK));
349 EXPECT_EQ(4294967294, FirewallController::discoverMaximumValidUid(tempFile));
350}
Luke Huange64fa382018-07-24 16:38:22 +0800351
352} // namespace net
Bernie Innocentia5161a02019-01-30 22:40:53 +0900353} // namespace android