blob: 836adb8b8c1fcac5179f4f429374d2201a54df46 [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;
Lorenzo Colitti6324b182017-07-17 21:48:14 +090033using android::base::StringPrintf;
Hugo Benichi528d3d02018-06-20 13:35:58 +090034using android::base::WriteStringToFile;
Lorenzo Colitti89faa342016-02-26 11:38:47 +090035
Luke Huange64fa382018-07-24 16:38:22 +080036namespace android {
37namespace net {
38
Lorenzo Colitti932c44c2016-04-24 16:58:02 +090039class FirewallControllerTest : public IptablesBaseTest {
Lorenzo Colitti89faa342016-02-26 11:38:47 +090040protected:
Lorenzo Colitti932c44c2016-04-24 16:58:02 +090041 FirewallControllerTest() {
Lorenzo Colitti932c44c2016-04-24 16:58:02 +090042 FirewallController::execIptablesRestore = fakeExecIptablesRestore;
Chenbo Feng89c12f12018-03-21 10:29:18 -070043 mFw.mUseBpfOwnerMatch = false;
Lorenzo Colitti932c44c2016-04-24 16:58:02 +090044 }
Lorenzo Colitti89faa342016-02-26 11:38:47 +090045 FirewallController mFw;
Lorenzo Colitti932c44c2016-04-24 16:58:02 +090046
Lorenzo Colittif157caf2016-05-13 11:25:54 +090047 std::string makeUidRules(IptablesTarget a, const char* b, bool c,
48 const std::vector<int32_t>& d) {
49 return mFw.makeUidRules(a, b, c, d);
Lorenzo Colitti89faa342016-02-26 11:38:47 +090050 }
Lorenzo Colitti932c44c2016-04-24 16:58:02 +090051
Lorenzo Colitti03b23fe2017-02-03 18:46:53 +090052 int createChain(const char* a, FirewallType b) {
53 return mFw.createChain(a, b);
Lorenzo Colitti932c44c2016-04-24 16:58:02 +090054 }
Lorenzo Colitti89faa342016-02-26 11:38:47 +090055};
56
Lorenzo Colitti932c44c2016-04-24 16:58:02 +090057TEST_F(FirewallControllerTest, TestCreateWhitelistChain) {
Lorenzo Colittia55388e2016-05-13 17:03:42 +090058 std::vector<std::string> expectedRestore4 = {
59 "*filter",
60 ":fw_whitelist -",
Lorenzo Colitti8bcb1f42017-04-25 00:17:48 +090061 "-A fw_whitelist -m owner --uid-owner 0-9999 -j RETURN",
Benedict Wongb2daefb2017-12-06 22:05:46 -080062 "-A fw_whitelist -m owner ! --uid-owner 0-4294967294 -j RETURN",
63 "-A fw_whitelist -p esp -j RETURN",
Lorenzo Colitti50b198a2017-03-30 02:50:09 +090064 "-A fw_whitelist -i lo -j RETURN",
65 "-A fw_whitelist -o lo -j RETURN",
Lorenzo Colittia55388e2016-05-13 17:03:42 +090066 "-A fw_whitelist -p tcp --tcp-flags RST RST -j RETURN",
Lorenzo Colittia55388e2016-05-13 17:03:42 +090067 "-A fw_whitelist -j DROP",
Lorenzo Colitti03b23fe2017-02-03 18:46:53 +090068 "COMMIT\n"
Lorenzo Colittia55388e2016-05-13 17:03:42 +090069 };
70 std::vector<std::string> expectedRestore6 = {
71 "*filter",
72 ":fw_whitelist -",
Lorenzo Colitti8bcb1f42017-04-25 00:17:48 +090073 "-A fw_whitelist -m owner --uid-owner 0-9999 -j RETURN",
Benedict Wongb2daefb2017-12-06 22:05:46 -080074 "-A fw_whitelist -m owner ! --uid-owner 0-4294967294 -j RETURN",
75 "-A fw_whitelist -p esp -j RETURN",
Lorenzo Colitti50b198a2017-03-30 02:50:09 +090076 "-A fw_whitelist -i lo -j RETURN",
77 "-A fw_whitelist -o lo -j RETURN",
Lorenzo Colittia55388e2016-05-13 17:03:42 +090078 "-A fw_whitelist -p tcp --tcp-flags RST RST -j RETURN",
79 "-A fw_whitelist -p icmpv6 --icmpv6-type packet-too-big -j RETURN",
80 "-A fw_whitelist -p icmpv6 --icmpv6-type router-solicitation -j RETURN",
81 "-A fw_whitelist -p icmpv6 --icmpv6-type router-advertisement -j RETURN",
82 "-A fw_whitelist -p icmpv6 --icmpv6-type neighbour-solicitation -j RETURN",
83 "-A fw_whitelist -p icmpv6 --icmpv6-type neighbour-advertisement -j RETURN",
84 "-A fw_whitelist -p icmpv6 --icmpv6-type redirect -j RETURN",
Lorenzo Colittia55388e2016-05-13 17:03:42 +090085 "-A fw_whitelist -j DROP",
Lorenzo Colitti03b23fe2017-02-03 18:46:53 +090086 "COMMIT\n"
Lorenzo Colittia55388e2016-05-13 17:03:42 +090087 };
88 std::vector<std::pair<IptablesTarget, std::string>> expectedRestoreCommands = {
89 { V4, android::base::Join(expectedRestore4, '\n') },
90 { V6, android::base::Join(expectedRestore6, '\n') },
91 };
92
Lorenzo Colitti03b23fe2017-02-03 18:46:53 +090093 createChain("fw_whitelist", WHITELIST);
Lorenzo Colittia55388e2016-05-13 17:03:42 +090094 expectIptablesRestoreCommands(expectedRestoreCommands);
Lorenzo Colitti932c44c2016-04-24 16:58:02 +090095}
96
97TEST_F(FirewallControllerTest, TestCreateBlacklistChain) {
Lorenzo Colittia55388e2016-05-13 17:03:42 +090098 std::vector<std::string> expectedRestore = {
99 "*filter",
100 ":fw_blacklist -",
Lorenzo Colitti50b198a2017-03-30 02:50:09 +0900101 "-A fw_blacklist -i lo -j RETURN",
102 "-A fw_blacklist -o lo -j RETURN",
Lorenzo Colittia55388e2016-05-13 17:03:42 +0900103 "-A fw_blacklist -p tcp --tcp-flags RST RST -j RETURN",
Lorenzo Colitti03b23fe2017-02-03 18:46:53 +0900104 "COMMIT\n"
Lorenzo Colittia55388e2016-05-13 17:03:42 +0900105 };
106 std::vector<std::pair<IptablesTarget, std::string>> expectedRestoreCommands = {
107 { V4, android::base::Join(expectedRestore, '\n') },
108 { V6, android::base::Join(expectedRestore, '\n') },
109 };
110
Lorenzo Colitti03b23fe2017-02-03 18:46:53 +0900111 createChain("fw_blacklist", BLACKLIST);
Lorenzo Colittia55388e2016-05-13 17:03:42 +0900112 expectIptablesRestoreCommands(expectedRestoreCommands);
Lorenzo Colitti932c44c2016-04-24 16:58:02 +0900113}
114
115TEST_F(FirewallControllerTest, TestSetStandbyRule) {
116 ExpectedIptablesCommands expected = {
Lorenzo Colittia7357652017-04-25 00:16:36 +0900117 { V4V6, "*filter\n-D fw_standby -m owner --uid-owner 12345 -j DROP\nCOMMIT\n" }
Lorenzo Colitti932c44c2016-04-24 16:58:02 +0900118 };
119 mFw.setUidRule(STANDBY, 12345, ALLOW);
Lorenzo Colittia7357652017-04-25 00:16:36 +0900120 expectIptablesRestoreCommands(expected);
Lorenzo Colitti932c44c2016-04-24 16:58:02 +0900121
122 expected = {
Lorenzo Colittia7357652017-04-25 00:16:36 +0900123 { V4V6, "*filter\n-A fw_standby -m owner --uid-owner 12345 -j DROP\nCOMMIT\n" }
Lorenzo Colitti932c44c2016-04-24 16:58:02 +0900124 };
125 mFw.setUidRule(STANDBY, 12345, DENY);
Lorenzo Colittia7357652017-04-25 00:16:36 +0900126 expectIptablesRestoreCommands(expected);
Lorenzo Colitti932c44c2016-04-24 16:58:02 +0900127}
128
129TEST_F(FirewallControllerTest, TestSetDozeRule) {
130 ExpectedIptablesCommands expected = {
Lorenzo Colittia7357652017-04-25 00:16:36 +0900131 { V4V6, "*filter\n-I fw_dozable -m owner --uid-owner 54321 -j RETURN\nCOMMIT\n" }
Lorenzo Colitti932c44c2016-04-24 16:58:02 +0900132 };
133 mFw.setUidRule(DOZABLE, 54321, ALLOW);
Lorenzo Colittia7357652017-04-25 00:16:36 +0900134 expectIptablesRestoreCommands(expected);
Lorenzo Colitti932c44c2016-04-24 16:58:02 +0900135
136 expected = {
Lorenzo Colittia7357652017-04-25 00:16:36 +0900137 { V4V6, "*filter\n-D fw_dozable -m owner --uid-owner 54321 -j RETURN\nCOMMIT\n" }
Lorenzo Colitti932c44c2016-04-24 16:58:02 +0900138 };
139 mFw.setUidRule(DOZABLE, 54321, DENY);
Lorenzo Colittia7357652017-04-25 00:16:36 +0900140 expectIptablesRestoreCommands(expected);
141}
142
143TEST_F(FirewallControllerTest, TestSetFirewallRule) {
144 ExpectedIptablesCommands expected = {
145 { V4V6, "*filter\n"
146 "-A fw_INPUT -m owner --uid-owner 54321 -j DROP\n"
147 "-A fw_OUTPUT -m owner --uid-owner 54321 -j DROP\n"
148 "COMMIT\n" }
149 };
150 mFw.setUidRule(NONE, 54321, DENY);
151 expectIptablesRestoreCommands(expected);
152
153 expected = {
154 { V4V6, "*filter\n"
155 "-D fw_INPUT -m owner --uid-owner 54321 -j DROP\n"
156 "-D fw_OUTPUT -m owner --uid-owner 54321 -j DROP\n"
157 "COMMIT\n" }
158 };
159 mFw.setUidRule(NONE, 54321, ALLOW);
160 expectIptablesRestoreCommands(expected);
Lorenzo Colitti932c44c2016-04-24 16:58:02 +0900161}
162
163TEST_F(FirewallControllerTest, TestReplaceWhitelistUidRule) {
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900164 std::string expected =
165 "*filter\n"
166 ":FW_whitechain -\n"
Lorenzo Colitti8bcb1f42017-04-25 00:17:48 +0900167 "-A FW_whitechain -m owner --uid-owner 10023 -j RETURN\n"
168 "-A FW_whitechain -m owner --uid-owner 10059 -j RETURN\n"
169 "-A FW_whitechain -m owner --uid-owner 10124 -j RETURN\n"
170 "-A FW_whitechain -m owner --uid-owner 10111 -j RETURN\n"
171 "-A FW_whitechain -m owner --uid-owner 110122 -j RETURN\n"
172 "-A FW_whitechain -m owner --uid-owner 210153 -j RETURN\n"
173 "-A FW_whitechain -m owner --uid-owner 210024 -j RETURN\n"
174 "-A FW_whitechain -m owner --uid-owner 0-9999 -j RETURN\n"
Benedict Wongb2daefb2017-12-06 22:05:46 -0800175 "-A FW_whitechain -m owner ! --uid-owner 0-4294967294 -j RETURN\n"
176 "-A FW_whitechain -p esp -j RETURN\n"
Lorenzo Colitti50b198a2017-03-30 02:50:09 +0900177 "-A FW_whitechain -i lo -j RETURN\n"
178 "-A FW_whitechain -o lo -j RETURN\n"
Lorenzo Colittif157caf2016-05-13 11:25:54 +0900179 "-A FW_whitechain -p tcp --tcp-flags RST RST -j RETURN\n"
180 "-A FW_whitechain -p icmpv6 --icmpv6-type packet-too-big -j RETURN\n"
181 "-A FW_whitechain -p icmpv6 --icmpv6-type router-solicitation -j RETURN\n"
182 "-A FW_whitechain -p icmpv6 --icmpv6-type router-advertisement -j RETURN\n"
183 "-A FW_whitechain -p icmpv6 --icmpv6-type neighbour-solicitation -j RETURN\n"
184 "-A FW_whitechain -p icmpv6 --icmpv6-type neighbour-advertisement -j RETURN\n"
185 "-A FW_whitechain -p icmpv6 --icmpv6-type redirect -j RETURN\n"
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900186 "-A FW_whitechain -j DROP\n"
Lorenzo Colitti03b23fe2017-02-03 18:46:53 +0900187 "COMMIT\n";
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900188
189 std::vector<int32_t> uids = { 10023, 10059, 10124, 10111, 110122, 210153, 210024 };
Lorenzo Colittif157caf2016-05-13 11:25:54 +0900190 EXPECT_EQ(expected, makeUidRules(V6, "FW_whitechain", true, uids));
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900191}
192
Lorenzo Colitti932c44c2016-04-24 16:58:02 +0900193TEST_F(FirewallControllerTest, TestReplaceBlacklistUidRule) {
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900194 std::string expected =
195 "*filter\n"
196 ":FW_blackchain -\n"
Lorenzo Colitti50b198a2017-03-30 02:50:09 +0900197 "-A FW_blackchain -i lo -j RETURN\n"
198 "-A FW_blackchain -o lo -j RETURN\n"
Lorenzo Colittif157caf2016-05-13 11:25:54 +0900199 "-A FW_blackchain -p tcp --tcp-flags RST RST -j RETURN\n"
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900200 "-A FW_blackchain -m owner --uid-owner 10023 -j DROP\n"
201 "-A FW_blackchain -m owner --uid-owner 10059 -j DROP\n"
202 "-A FW_blackchain -m owner --uid-owner 10124 -j DROP\n"
Lorenzo Colitti03b23fe2017-02-03 18:46:53 +0900203 "COMMIT\n";
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900204
205 std::vector<int32_t> uids = { 10023, 10059, 10124 };
Lorenzo Colittif157caf2016-05-13 11:25:54 +0900206 EXPECT_EQ(expected, makeUidRules(V4 ,"FW_blackchain", false, uids));
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900207}
Lorenzo Colittidd9a54b2017-04-26 16:13:22 +0900208
209TEST_F(FirewallControllerTest, TestEnableChildChains) {
210 std::vector<std::string> expected = {
Lorenzo Colittia1611962017-04-26 16:30:39 +0900211 "*filter\n"
212 "-A fw_INPUT -j fw_dozable\n"
213 "-A fw_OUTPUT -j fw_dozable\n"
214 "COMMIT\n"
Lorenzo Colittidd9a54b2017-04-26 16:13:22 +0900215 };
216 EXPECT_EQ(0, mFw.enableChildChains(DOZABLE, true));
Lorenzo Colittia1611962017-04-26 16:30:39 +0900217 expectIptablesRestoreCommands(expected);
Lorenzo Colittidd9a54b2017-04-26 16:13:22 +0900218
219 expected = {
Lorenzo Colittia1611962017-04-26 16:30:39 +0900220 "*filter\n"
221 "-D fw_INPUT -j fw_powersave\n"
222 "-D fw_OUTPUT -j fw_powersave\n"
223 "COMMIT\n"
Lorenzo Colittidd9a54b2017-04-26 16:13:22 +0900224 };
225 EXPECT_EQ(0, mFw.enableChildChains(POWERSAVE, false));
Lorenzo Colittia1611962017-04-26 16:30:39 +0900226 expectIptablesRestoreCommands(expected);
Lorenzo Colittidd9a54b2017-04-26 16:13:22 +0900227}
Lorenzo Colitticc1bb822017-07-16 21:08:54 +0900228
Lorenzo Colitti6324b182017-07-17 21:48:14 +0900229TEST_F(FirewallControllerTest, TestFirewall) {
Lorenzo Colitticc1bb822017-07-16 21:08:54 +0900230 std::vector<std::string> enableCommands = {
Lorenzo Colittid351bea2017-07-16 22:52:30 +0900231 "*filter\n"
232 "-A fw_INPUT -j DROP\n"
233 "-A fw_OUTPUT -j REJECT\n"
234 "-A fw_FORWARD -j REJECT\n"
235 "COMMIT\n"
Lorenzo Colitticc1bb822017-07-16 21:08:54 +0900236 };
237 std::vector<std::string> disableCommands = {
Lorenzo Colittid351bea2017-07-16 22:52:30 +0900238 "*filter\n"
239 ":fw_INPUT -\n"
240 ":fw_OUTPUT -\n"
241 ":fw_FORWARD -\n"
242 "COMMIT\n"
Lorenzo Colitticc1bb822017-07-16 21:08:54 +0900243 };
244 std::vector<std::string> noCommands = {};
245
Luke Huange64fa382018-07-24 16:38:22 +0800246 EXPECT_EQ(0, mFw.resetFirewall());
Lorenzo Colittid351bea2017-07-16 22:52:30 +0900247 expectIptablesRestoreCommands(disableCommands);
Lorenzo Colitticc1bb822017-07-16 21:08:54 +0900248
Luke Huange64fa382018-07-24 16:38:22 +0800249 EXPECT_EQ(0, mFw.resetFirewall());
Lorenzo Colittid351bea2017-07-16 22:52:30 +0900250 expectIptablesRestoreCommands(disableCommands);
Lorenzo Colitticc1bb822017-07-16 21:08:54 +0900251
Luke Huange64fa382018-07-24 16:38:22 +0800252 EXPECT_EQ(0, mFw.setFirewallType(BLACKLIST));
Lorenzo Colittid351bea2017-07-16 22:52:30 +0900253 expectIptablesRestoreCommands(disableCommands);
Lorenzo Colitticc1bb822017-07-16 21:08:54 +0900254
Luke Huange64fa382018-07-24 16:38:22 +0800255 EXPECT_EQ(0, mFw.setFirewallType(BLACKLIST));
Lorenzo Colittid351bea2017-07-16 22:52:30 +0900256 expectIptablesRestoreCommands(noCommands);
Lorenzo Colitticc1bb822017-07-16 21:08:54 +0900257
258 std::vector<std::string> disableEnableCommands;
259 disableEnableCommands.insert(
260 disableEnableCommands.end(), disableCommands.begin(), disableCommands.end());
261 disableEnableCommands.insert(
262 disableEnableCommands.end(), enableCommands.begin(), enableCommands.end());
263
Luke Huange64fa382018-07-24 16:38:22 +0800264 EXPECT_EQ(0, mFw.setFirewallType(WHITELIST));
Lorenzo Colittid351bea2017-07-16 22:52:30 +0900265 expectIptablesRestoreCommands(disableEnableCommands);
Lorenzo Colitticc1bb822017-07-16 21:08:54 +0900266
Lorenzo Colitti6324b182017-07-17 21:48:14 +0900267 std::vector<std::string> ifaceCommands = {
Lorenzo Colitti1411d452017-07-17 22:12:15 +0900268 "*filter\n"
269 "-I fw_INPUT -i rmnet_data0 -j RETURN\n"
270 "-I fw_OUTPUT -o rmnet_data0 -j RETURN\n"
271 "COMMIT\n"
Lorenzo Colitti6324b182017-07-17 21:48:14 +0900272 };
273 EXPECT_EQ(0, mFw.setInterfaceRule("rmnet_data0", ALLOW));
Lorenzo Colitti1411d452017-07-17 22:12:15 +0900274 expectIptablesRestoreCommands(ifaceCommands);
275
276 EXPECT_EQ(0, mFw.setInterfaceRule("rmnet_data0", ALLOW));
277 expectIptablesRestoreCommands(noCommands);
Lorenzo Colitti6324b182017-07-17 21:48:14 +0900278
279 ifaceCommands = {
Lorenzo Colitti1411d452017-07-17 22:12:15 +0900280 "*filter\n"
281 "-D fw_INPUT -i rmnet_data0 -j RETURN\n"
282 "-D fw_OUTPUT -o rmnet_data0 -j RETURN\n"
283 "COMMIT\n"
Lorenzo Colitti6324b182017-07-17 21:48:14 +0900284 };
285 EXPECT_EQ(0, mFw.setInterfaceRule("rmnet_data0", DENY));
Lorenzo Colitti1411d452017-07-17 22:12:15 +0900286 expectIptablesRestoreCommands(ifaceCommands);
287
288 EXPECT_EQ(0, mFw.setInterfaceRule("rmnet_data0", DENY));
289 expectIptablesRestoreCommands(noCommands);
Lorenzo Colitti6324b182017-07-17 21:48:14 +0900290
Luke Huange64fa382018-07-24 16:38:22 +0800291 EXPECT_EQ(0, mFw.setFirewallType(WHITELIST));
Lorenzo Colittid351bea2017-07-16 22:52:30 +0900292 expectIptablesRestoreCommands(noCommands);
Lorenzo Colitticc1bb822017-07-16 21:08:54 +0900293
Luke Huange64fa382018-07-24 16:38:22 +0800294 EXPECT_EQ(0, mFw.resetFirewall());
Lorenzo Colittid351bea2017-07-16 22:52:30 +0900295 expectIptablesRestoreCommands(disableCommands);
Lorenzo Colitticc1bb822017-07-16 21:08:54 +0900296
Luke Huange64fa382018-07-24 16:38:22 +0800297 // TODO: calling resetFirewall and then setFirewallType(WHITELIST) does
Lorenzo Colitticc1bb822017-07-16 21:08:54 +0900298 // nothing. This seems like a clear bug.
Luke Huange64fa382018-07-24 16:38:22 +0800299 EXPECT_EQ(0, mFw.setFirewallType(WHITELIST));
Lorenzo Colittid351bea2017-07-16 22:52:30 +0900300 expectIptablesRestoreCommands(noCommands);
Lorenzo Colitticc1bb822017-07-16 21:08:54 +0900301}
Hugo Benichi528d3d02018-06-20 13:35:58 +0900302
303TEST_F(FirewallControllerTest, TestDiscoverMaximumValidUid) {
304 struct {
305 const std::string description;
306 const std::string content;
307 const uint32_t expected;
308 } testCases[] = {
309 {
310 .description = "root namespace case",
311 .content = " 0 0 4294967295",
312 .expected = 4294967294,
313 },
314 {
315 .description = "container namespace case",
316 .content = " 0 655360 5000\n"
317 " 5000 600 50\n"
318 " 5050 660410 1994950\n",
319 .expected = 1999999,
320 },
321 {
322 .description = "garbage content case",
323 .content = "garbage",
324 .expected = 4294967294,
325 },
326 {
327 .description = "no content case",
328 .content = "",
329 .expected = 4294967294,
330 },
331 };
332
333 const std::string tempFile = "/data/local/tmp/fake_uid_mapping";
334
335 for (const auto& test : testCases) {
336 EXPECT_TRUE(WriteStringToFile(test.content, tempFile, false));
337 uint32_t got = FirewallController::discoverMaximumValidUid(tempFile);
338 EXPECT_EQ(0, remove(tempFile.c_str()));
339 if (got != test.expected) {
340 FAIL() << test.description << ":\n"
341 << test.content << "\ngot " << got << ", but expected " << test.expected;
342 }
343 }
344
345 // Also check when the file is not defined
346 EXPECT_NE(0, access(tempFile.c_str(), F_OK));
347 EXPECT_EQ(4294967294, FirewallController::discoverMaximumValidUid(tempFile));
348}
Luke Huange64fa382018-07-24 16:38:22 +0800349
350} // namespace net
351} // namespace android