blob: 71cc532fe169f97ba658b78eab6df1a08b2159e5 [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 Feng89c12f12018-03-21 10:29:18 -070042 mFw.mUseBpfOwnerMatch = false;
Lorenzo Colitti932c44c2016-04-24 16:58:02 +090043 }
Lorenzo Colitti89faa342016-02-26 11:38:47 +090044 FirewallController mFw;
Lorenzo Colitti932c44c2016-04-24 16:58:02 +090045
Lorenzo Colittif157caf2016-05-13 11:25:54 +090046 std::string makeUidRules(IptablesTarget a, const char* b, bool c,
47 const std::vector<int32_t>& d) {
48 return mFw.makeUidRules(a, b, c, d);
Lorenzo Colitti89faa342016-02-26 11:38:47 +090049 }
Lorenzo Colitti932c44c2016-04-24 16:58:02 +090050
Lorenzo Colitti03b23fe2017-02-03 18:46:53 +090051 int createChain(const char* a, FirewallType b) {
52 return mFw.createChain(a, b);
Lorenzo Colitti932c44c2016-04-24 16:58:02 +090053 }
Lorenzo Colitti89faa342016-02-26 11:38:47 +090054};
55
Lorenzo Colitti932c44c2016-04-24 16:58:02 +090056TEST_F(FirewallControllerTest, TestCreateWhitelistChain) {
Lorenzo Colittia55388e2016-05-13 17:03:42 +090057 std::vector<std::string> expectedRestore4 = {
58 "*filter",
59 ":fw_whitelist -",
Lorenzo Colitti8bcb1f42017-04-25 00:17:48 +090060 "-A fw_whitelist -m owner --uid-owner 0-9999 -j RETURN",
Benedict Wongb2daefb2017-12-06 22:05:46 -080061 "-A fw_whitelist -m owner ! --uid-owner 0-4294967294 -j RETURN",
62 "-A fw_whitelist -p esp -j RETURN",
Lorenzo Colitti50b198a2017-03-30 02:50:09 +090063 "-A fw_whitelist -i lo -j RETURN",
64 "-A fw_whitelist -o lo -j RETURN",
Lorenzo Colittia55388e2016-05-13 17:03:42 +090065 "-A fw_whitelist -p tcp --tcp-flags RST RST -j RETURN",
Lorenzo Colittia55388e2016-05-13 17:03:42 +090066 "-A fw_whitelist -j DROP",
Lorenzo Colitti03b23fe2017-02-03 18:46:53 +090067 "COMMIT\n"
Lorenzo Colittia55388e2016-05-13 17:03:42 +090068 };
69 std::vector<std::string> expectedRestore6 = {
70 "*filter",
71 ":fw_whitelist -",
Lorenzo Colitti8bcb1f42017-04-25 00:17:48 +090072 "-A fw_whitelist -m owner --uid-owner 0-9999 -j RETURN",
Benedict Wongb2daefb2017-12-06 22:05:46 -080073 "-A fw_whitelist -m owner ! --uid-owner 0-4294967294 -j RETURN",
74 "-A fw_whitelist -p esp -j RETURN",
Lorenzo Colitti50b198a2017-03-30 02:50:09 +090075 "-A fw_whitelist -i lo -j RETURN",
76 "-A fw_whitelist -o lo -j RETURN",
Lorenzo Colittia55388e2016-05-13 17:03:42 +090077 "-A fw_whitelist -p tcp --tcp-flags RST RST -j RETURN",
78 "-A fw_whitelist -p icmpv6 --icmpv6-type packet-too-big -j RETURN",
79 "-A fw_whitelist -p icmpv6 --icmpv6-type router-solicitation -j RETURN",
80 "-A fw_whitelist -p icmpv6 --icmpv6-type router-advertisement -j RETURN",
81 "-A fw_whitelist -p icmpv6 --icmpv6-type neighbour-solicitation -j RETURN",
82 "-A fw_whitelist -p icmpv6 --icmpv6-type neighbour-advertisement -j RETURN",
83 "-A fw_whitelist -p icmpv6 --icmpv6-type redirect -j RETURN",
Lorenzo Colittia55388e2016-05-13 17:03:42 +090084 "-A fw_whitelist -j DROP",
Lorenzo Colitti03b23fe2017-02-03 18:46:53 +090085 "COMMIT\n"
Lorenzo Colittia55388e2016-05-13 17:03:42 +090086 };
87 std::vector<std::pair<IptablesTarget, std::string>> expectedRestoreCommands = {
Bernie Innocentia5161a02019-01-30 22:40:53 +090088 {V4, Join(expectedRestore4, '\n')},
89 {V6, Join(expectedRestore6, '\n')},
Lorenzo Colittia55388e2016-05-13 17:03:42 +090090 };
91
Lorenzo Colitti03b23fe2017-02-03 18:46:53 +090092 createChain("fw_whitelist", WHITELIST);
Lorenzo Colittia55388e2016-05-13 17:03:42 +090093 expectIptablesRestoreCommands(expectedRestoreCommands);
Lorenzo Colitti932c44c2016-04-24 16:58:02 +090094}
95
96TEST_F(FirewallControllerTest, TestCreateBlacklistChain) {
Lorenzo Colittia55388e2016-05-13 17:03:42 +090097 std::vector<std::string> expectedRestore = {
98 "*filter",
99 ":fw_blacklist -",
Lorenzo Colitti50b198a2017-03-30 02:50:09 +0900100 "-A fw_blacklist -i lo -j RETURN",
101 "-A fw_blacklist -o lo -j RETURN",
Lorenzo Colittia55388e2016-05-13 17:03:42 +0900102 "-A fw_blacklist -p tcp --tcp-flags RST RST -j RETURN",
Lorenzo Colitti03b23fe2017-02-03 18:46:53 +0900103 "COMMIT\n"
Lorenzo Colittia55388e2016-05-13 17:03:42 +0900104 };
105 std::vector<std::pair<IptablesTarget, std::string>> expectedRestoreCommands = {
Bernie Innocentia5161a02019-01-30 22:40:53 +0900106 {V4, Join(expectedRestore, '\n')},
107 {V6, Join(expectedRestore, '\n')},
Lorenzo Colittia55388e2016-05-13 17:03:42 +0900108 };
109
Lorenzo Colitti03b23fe2017-02-03 18:46:53 +0900110 createChain("fw_blacklist", BLACKLIST);
Lorenzo Colittia55388e2016-05-13 17:03:42 +0900111 expectIptablesRestoreCommands(expectedRestoreCommands);
Lorenzo Colitti932c44c2016-04-24 16:58:02 +0900112}
113
114TEST_F(FirewallControllerTest, TestSetStandbyRule) {
115 ExpectedIptablesCommands expected = {
Lorenzo Colittia7357652017-04-25 00:16:36 +0900116 { V4V6, "*filter\n-D fw_standby -m owner --uid-owner 12345 -j DROP\nCOMMIT\n" }
Lorenzo Colitti932c44c2016-04-24 16:58:02 +0900117 };
118 mFw.setUidRule(STANDBY, 12345, ALLOW);
Lorenzo Colittia7357652017-04-25 00:16:36 +0900119 expectIptablesRestoreCommands(expected);
Lorenzo Colitti932c44c2016-04-24 16:58:02 +0900120
121 expected = {
Lorenzo Colittia7357652017-04-25 00:16:36 +0900122 { V4V6, "*filter\n-A fw_standby -m owner --uid-owner 12345 -j DROP\nCOMMIT\n" }
Lorenzo Colitti932c44c2016-04-24 16:58:02 +0900123 };
124 mFw.setUidRule(STANDBY, 12345, DENY);
Lorenzo Colittia7357652017-04-25 00:16:36 +0900125 expectIptablesRestoreCommands(expected);
Lorenzo Colitti932c44c2016-04-24 16:58:02 +0900126}
127
128TEST_F(FirewallControllerTest, TestSetDozeRule) {
129 ExpectedIptablesCommands expected = {
Lorenzo Colittia7357652017-04-25 00:16:36 +0900130 { V4V6, "*filter\n-I fw_dozable -m owner --uid-owner 54321 -j RETURN\nCOMMIT\n" }
Lorenzo Colitti932c44c2016-04-24 16:58:02 +0900131 };
132 mFw.setUidRule(DOZABLE, 54321, ALLOW);
Lorenzo Colittia7357652017-04-25 00:16:36 +0900133 expectIptablesRestoreCommands(expected);
Lorenzo Colitti932c44c2016-04-24 16:58:02 +0900134
135 expected = {
Lorenzo Colittia7357652017-04-25 00:16:36 +0900136 { V4V6, "*filter\n-D fw_dozable -m owner --uid-owner 54321 -j RETURN\nCOMMIT\n" }
Lorenzo Colitti932c44c2016-04-24 16:58:02 +0900137 };
138 mFw.setUidRule(DOZABLE, 54321, DENY);
Lorenzo Colittia7357652017-04-25 00:16:36 +0900139 expectIptablesRestoreCommands(expected);
140}
141
142TEST_F(FirewallControllerTest, TestSetFirewallRule) {
143 ExpectedIptablesCommands expected = {
144 { V4V6, "*filter\n"
145 "-A fw_INPUT -m owner --uid-owner 54321 -j DROP\n"
146 "-A fw_OUTPUT -m owner --uid-owner 54321 -j DROP\n"
147 "COMMIT\n" }
148 };
149 mFw.setUidRule(NONE, 54321, DENY);
150 expectIptablesRestoreCommands(expected);
151
152 expected = {
153 { V4V6, "*filter\n"
154 "-D fw_INPUT -m owner --uid-owner 54321 -j DROP\n"
155 "-D fw_OUTPUT -m owner --uid-owner 54321 -j DROP\n"
156 "COMMIT\n" }
157 };
158 mFw.setUidRule(NONE, 54321, ALLOW);
159 expectIptablesRestoreCommands(expected);
Lorenzo Colitti932c44c2016-04-24 16:58:02 +0900160}
161
162TEST_F(FirewallControllerTest, TestReplaceWhitelistUidRule) {
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900163 std::string expected =
164 "*filter\n"
165 ":FW_whitechain -\n"
Lorenzo Colitti8bcb1f42017-04-25 00:17:48 +0900166 "-A FW_whitechain -m owner --uid-owner 10023 -j RETURN\n"
167 "-A FW_whitechain -m owner --uid-owner 10059 -j RETURN\n"
168 "-A FW_whitechain -m owner --uid-owner 10124 -j RETURN\n"
169 "-A FW_whitechain -m owner --uid-owner 10111 -j RETURN\n"
170 "-A FW_whitechain -m owner --uid-owner 110122 -j RETURN\n"
171 "-A FW_whitechain -m owner --uid-owner 210153 -j RETURN\n"
172 "-A FW_whitechain -m owner --uid-owner 210024 -j RETURN\n"
173 "-A FW_whitechain -m owner --uid-owner 0-9999 -j RETURN\n"
Benedict Wongb2daefb2017-12-06 22:05:46 -0800174 "-A FW_whitechain -m owner ! --uid-owner 0-4294967294 -j RETURN\n"
175 "-A FW_whitechain -p esp -j RETURN\n"
Lorenzo Colitti50b198a2017-03-30 02:50:09 +0900176 "-A FW_whitechain -i lo -j RETURN\n"
177 "-A FW_whitechain -o lo -j RETURN\n"
Lorenzo Colittif157caf2016-05-13 11:25:54 +0900178 "-A FW_whitechain -p tcp --tcp-flags RST RST -j RETURN\n"
179 "-A FW_whitechain -p icmpv6 --icmpv6-type packet-too-big -j RETURN\n"
180 "-A FW_whitechain -p icmpv6 --icmpv6-type router-solicitation -j RETURN\n"
181 "-A FW_whitechain -p icmpv6 --icmpv6-type router-advertisement -j RETURN\n"
182 "-A FW_whitechain -p icmpv6 --icmpv6-type neighbour-solicitation -j RETURN\n"
183 "-A FW_whitechain -p icmpv6 --icmpv6-type neighbour-advertisement -j RETURN\n"
184 "-A FW_whitechain -p icmpv6 --icmpv6-type redirect -j RETURN\n"
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900185 "-A FW_whitechain -j DROP\n"
Lorenzo Colitti03b23fe2017-02-03 18:46:53 +0900186 "COMMIT\n";
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900187
188 std::vector<int32_t> uids = { 10023, 10059, 10124, 10111, 110122, 210153, 210024 };
Lorenzo Colittif157caf2016-05-13 11:25:54 +0900189 EXPECT_EQ(expected, makeUidRules(V6, "FW_whitechain", true, uids));
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900190}
191
Lorenzo Colitti932c44c2016-04-24 16:58:02 +0900192TEST_F(FirewallControllerTest, TestReplaceBlacklistUidRule) {
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900193 std::string expected =
194 "*filter\n"
195 ":FW_blackchain -\n"
Lorenzo Colitti50b198a2017-03-30 02:50:09 +0900196 "-A FW_blackchain -i lo -j RETURN\n"
197 "-A FW_blackchain -o lo -j RETURN\n"
Lorenzo Colittif157caf2016-05-13 11:25:54 +0900198 "-A FW_blackchain -p tcp --tcp-flags RST RST -j RETURN\n"
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900199 "-A FW_blackchain -m owner --uid-owner 10023 -j DROP\n"
200 "-A FW_blackchain -m owner --uid-owner 10059 -j DROP\n"
201 "-A FW_blackchain -m owner --uid-owner 10124 -j DROP\n"
Lorenzo Colitti03b23fe2017-02-03 18:46:53 +0900202 "COMMIT\n";
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900203
204 std::vector<int32_t> uids = { 10023, 10059, 10124 };
Lorenzo Colittif157caf2016-05-13 11:25:54 +0900205 EXPECT_EQ(expected, makeUidRules(V4 ,"FW_blackchain", false, uids));
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900206}
Lorenzo Colittidd9a54b2017-04-26 16:13:22 +0900207
208TEST_F(FirewallControllerTest, TestEnableChildChains) {
209 std::vector<std::string> expected = {
Lorenzo Colittia1611962017-04-26 16:30:39 +0900210 "*filter\n"
211 "-A fw_INPUT -j fw_dozable\n"
212 "-A fw_OUTPUT -j fw_dozable\n"
213 "COMMIT\n"
Lorenzo Colittidd9a54b2017-04-26 16:13:22 +0900214 };
215 EXPECT_EQ(0, mFw.enableChildChains(DOZABLE, true));
Lorenzo Colittia1611962017-04-26 16:30:39 +0900216 expectIptablesRestoreCommands(expected);
Lorenzo Colittidd9a54b2017-04-26 16:13:22 +0900217
218 expected = {
Lorenzo Colittia1611962017-04-26 16:30:39 +0900219 "*filter\n"
220 "-D fw_INPUT -j fw_powersave\n"
221 "-D fw_OUTPUT -j fw_powersave\n"
222 "COMMIT\n"
Lorenzo Colittidd9a54b2017-04-26 16:13:22 +0900223 };
224 EXPECT_EQ(0, mFw.enableChildChains(POWERSAVE, false));
Lorenzo Colittia1611962017-04-26 16:30:39 +0900225 expectIptablesRestoreCommands(expected);
Lorenzo Colittidd9a54b2017-04-26 16:13:22 +0900226}
Lorenzo Colitticc1bb822017-07-16 21:08:54 +0900227
Lorenzo Colitti6324b182017-07-17 21:48:14 +0900228TEST_F(FirewallControllerTest, TestFirewall) {
Lorenzo Colitticc1bb822017-07-16 21:08:54 +0900229 std::vector<std::string> enableCommands = {
Lorenzo Colittid351bea2017-07-16 22:52:30 +0900230 "*filter\n"
231 "-A fw_INPUT -j DROP\n"
232 "-A fw_OUTPUT -j REJECT\n"
233 "-A fw_FORWARD -j REJECT\n"
234 "COMMIT\n"
Lorenzo Colitticc1bb822017-07-16 21:08:54 +0900235 };
236 std::vector<std::string> disableCommands = {
Lorenzo Colittid351bea2017-07-16 22:52:30 +0900237 "*filter\n"
238 ":fw_INPUT -\n"
239 ":fw_OUTPUT -\n"
240 ":fw_FORWARD -\n"
241 "COMMIT\n"
Lorenzo Colitticc1bb822017-07-16 21:08:54 +0900242 };
243 std::vector<std::string> noCommands = {};
244
Luke Huange64fa382018-07-24 16:38:22 +0800245 EXPECT_EQ(0, mFw.resetFirewall());
Lorenzo Colittid351bea2017-07-16 22:52:30 +0900246 expectIptablesRestoreCommands(disableCommands);
Lorenzo Colitticc1bb822017-07-16 21:08:54 +0900247
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.setFirewallType(BLACKLIST));
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(noCommands);
Lorenzo Colitticc1bb822017-07-16 21:08:54 +0900256
257 std::vector<std::string> disableEnableCommands;
258 disableEnableCommands.insert(
259 disableEnableCommands.end(), disableCommands.begin(), disableCommands.end());
260 disableEnableCommands.insert(
261 disableEnableCommands.end(), enableCommands.begin(), enableCommands.end());
262
Luke Huange64fa382018-07-24 16:38:22 +0800263 EXPECT_EQ(0, mFw.setFirewallType(WHITELIST));
Lorenzo Colittid351bea2017-07-16 22:52:30 +0900264 expectIptablesRestoreCommands(disableEnableCommands);
Lorenzo Colitticc1bb822017-07-16 21:08:54 +0900265
Lorenzo Colitti6324b182017-07-17 21:48:14 +0900266 std::vector<std::string> ifaceCommands = {
Lorenzo Colitti1411d452017-07-17 22:12:15 +0900267 "*filter\n"
268 "-I fw_INPUT -i rmnet_data0 -j RETURN\n"
269 "-I fw_OUTPUT -o rmnet_data0 -j RETURN\n"
270 "COMMIT\n"
Lorenzo Colitti6324b182017-07-17 21:48:14 +0900271 };
272 EXPECT_EQ(0, mFw.setInterfaceRule("rmnet_data0", ALLOW));
Lorenzo Colitti1411d452017-07-17 22:12:15 +0900273 expectIptablesRestoreCommands(ifaceCommands);
274
275 EXPECT_EQ(0, mFw.setInterfaceRule("rmnet_data0", ALLOW));
276 expectIptablesRestoreCommands(noCommands);
Lorenzo Colitti6324b182017-07-17 21:48:14 +0900277
278 ifaceCommands = {
Lorenzo Colitti1411d452017-07-17 22:12:15 +0900279 "*filter\n"
280 "-D fw_INPUT -i rmnet_data0 -j RETURN\n"
281 "-D fw_OUTPUT -o rmnet_data0 -j RETURN\n"
282 "COMMIT\n"
Lorenzo Colitti6324b182017-07-17 21:48:14 +0900283 };
284 EXPECT_EQ(0, mFw.setInterfaceRule("rmnet_data0", DENY));
Lorenzo Colitti1411d452017-07-17 22:12:15 +0900285 expectIptablesRestoreCommands(ifaceCommands);
286
287 EXPECT_EQ(0, mFw.setInterfaceRule("rmnet_data0", DENY));
288 expectIptablesRestoreCommands(noCommands);
Lorenzo Colitti6324b182017-07-17 21:48:14 +0900289
Luke Huange64fa382018-07-24 16:38:22 +0800290 EXPECT_EQ(0, mFw.setFirewallType(WHITELIST));
Lorenzo Colittid351bea2017-07-16 22:52:30 +0900291 expectIptablesRestoreCommands(noCommands);
Lorenzo Colitticc1bb822017-07-16 21:08:54 +0900292
Luke Huange64fa382018-07-24 16:38:22 +0800293 EXPECT_EQ(0, mFw.resetFirewall());
Lorenzo Colittid351bea2017-07-16 22:52:30 +0900294 expectIptablesRestoreCommands(disableCommands);
Lorenzo Colitticc1bb822017-07-16 21:08:54 +0900295
Luke Huange64fa382018-07-24 16:38:22 +0800296 // TODO: calling resetFirewall and then setFirewallType(WHITELIST) does
Lorenzo Colitticc1bb822017-07-16 21:08:54 +0900297 // nothing. This seems like a clear bug.
Luke Huange64fa382018-07-24 16:38:22 +0800298 EXPECT_EQ(0, mFw.setFirewallType(WHITELIST));
Lorenzo Colittid351bea2017-07-16 22:52:30 +0900299 expectIptablesRestoreCommands(noCommands);
Lorenzo Colitticc1bb822017-07-16 21:08:54 +0900300}
Hugo Benichi528d3d02018-06-20 13:35:58 +0900301
302TEST_F(FirewallControllerTest, TestDiscoverMaximumValidUid) {
303 struct {
304 const std::string description;
305 const std::string content;
306 const uint32_t expected;
307 } testCases[] = {
308 {
309 .description = "root namespace case",
310 .content = " 0 0 4294967295",
311 .expected = 4294967294,
312 },
313 {
314 .description = "container namespace case",
315 .content = " 0 655360 5000\n"
316 " 5000 600 50\n"
317 " 5050 660410 1994950\n",
318 .expected = 1999999,
319 },
320 {
321 .description = "garbage content case",
322 .content = "garbage",
323 .expected = 4294967294,
324 },
325 {
326 .description = "no content case",
327 .content = "",
328 .expected = 4294967294,
329 },
330 };
331
332 const std::string tempFile = "/data/local/tmp/fake_uid_mapping";
333
334 for (const auto& test : testCases) {
335 EXPECT_TRUE(WriteStringToFile(test.content, tempFile, false));
336 uint32_t got = FirewallController::discoverMaximumValidUid(tempFile);
337 EXPECT_EQ(0, remove(tempFile.c_str()));
338 if (got != test.expected) {
339 FAIL() << test.description << ":\n"
340 << test.content << "\ngot " << got << ", but expected " << test.expected;
341 }
342 }
343
344 // Also check when the file is not defined
345 EXPECT_NE(0, access(tempFile.c_str(), F_OK));
346 EXPECT_EQ(4294967294, FirewallController::discoverMaximumValidUid(tempFile));
347}
Luke Huange64fa382018-07-24 16:38:22 +0800348
349} // namespace net
Bernie Innocentia5161a02019-01-30 22:40:53 +0900350} // namespace android