blob: 661680c301b1327a837e4efd89c54a0c9dbb4daf [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 * binder_test.cpp - unit tests for netd binder RPCs.
17 */
18
Robin Leeb8087362016-03-30 18:43:08 +010019#include <cerrno>
20#include <cinttypes>
Lorenzo Colitti89faa342016-02-26 11:38:47 +090021#include <cstdint>
Lorenzo Colittidedd2712016-03-22 12:36:29 +090022#include <cstdio>
23#include <cstdlib>
Lorenzo Colitti563d98b2016-04-24 13:13:14 +090024#include <set>
Lorenzo Colitti89faa342016-02-26 11:38:47 +090025#include <vector>
26
Lorenzo Colitti755faa92016-07-27 22:10:49 +090027#include <fcntl.h>
28#include <netdb.h>
Lorenzo Colitti563d98b2016-04-24 13:13:14 +090029#include <sys/socket.h>
Lorenzo Colitti755faa92016-07-27 22:10:49 +090030#include <sys/types.h>
Lorenzo Colitti563d98b2016-04-24 13:13:14 +090031#include <netinet/in.h>
Lorenzo Colitti755faa92016-07-27 22:10:49 +090032#include <linux/if.h>
33#include <linux/if_tun.h>
Lorenzo Colitti563d98b2016-04-24 13:13:14 +090034
Lorenzo Colitti89faa342016-02-26 11:38:47 +090035#include <android-base/stringprintf.h>
Lorenzo Colittidedd2712016-03-22 12:36:29 +090036#include <android-base/strings.h>
Robin Leeb8087362016-03-30 18:43:08 +010037#include <cutils/multiuser.h>
Lorenzo Colitti89faa342016-02-26 11:38:47 +090038#include <gtest/gtest.h>
39#include <logwrap/logwrap.h>
Lorenzo Colitti755faa92016-07-27 22:10:49 +090040#include <netutils/ifc.h>
Lorenzo Colitti89faa342016-02-26 11:38:47 +090041
42#include "NetdConstants.h"
43#include "android/net/INetd.h"
Robin Leeb8087362016-03-30 18:43:08 +010044#include "android/net/UidRange.h"
Lorenzo Colitti89faa342016-02-26 11:38:47 +090045#include "binder/IServiceManager.h"
46
Lorenzo Colitti755faa92016-07-27 22:10:49 +090047#define TUN_DEV "/dev/tun"
48
Lorenzo Colitti89faa342016-02-26 11:38:47 +090049using namespace android;
50using namespace android::base;
51using namespace android::binder;
52using android::net::INetd;
Robin Leeb8087362016-03-30 18:43:08 +010053using android::net::UidRange;
54
55static const char* IP_RULE_V4 = "-4";
56static const char* IP_RULE_V6 = "-6";
Lorenzo Colitti89faa342016-02-26 11:38:47 +090057
58class BinderTest : public ::testing::Test {
59
60public:
61 BinderTest() {
62 sp<IServiceManager> sm = defaultServiceManager();
63 sp<IBinder> binder = sm->getService(String16("netd"));
64 if (binder != nullptr) {
65 mNetd = interface_cast<INetd>(binder);
66 }
67 }
68
Lorenzo Colitti755faa92016-07-27 22:10:49 +090069 void SetUp() override {
Lorenzo Colitti89faa342016-02-26 11:38:47 +090070 ASSERT_NE(nullptr, mNetd.get());
71 }
72
Lorenzo Colitti755faa92016-07-27 22:10:49 +090073 // Static because setting up the tun interface takes about 40ms.
74 static void SetUpTestCase() {
75 sTunFd = createTunInterface();
76 ASSERT_NE(-1, sTunFd);
77 }
78
79 static void TearDownTestCase() {
80 // Closing the socket removes the interface and IP addresses.
81 close(sTunFd);
82 }
83
84 static void fakeRemoteSocketPair(int *clientSocket, int *serverSocket, int *acceptedSocket);
85 static int createTunInterface();
86
Lorenzo Colitti89faa342016-02-26 11:38:47 +090087protected:
88 sp<INetd> mNetd;
Lorenzo Colitti755faa92016-07-27 22:10:49 +090089 static int sTunFd;
90 static in6_addr sSrcAddr, sDstAddr;
91 static char sSrcStr[], sDstStr[];
Lorenzo Colitti89faa342016-02-26 11:38:47 +090092};
93
Lorenzo Colitti755faa92016-07-27 22:10:49 +090094int BinderTest::sTunFd;
95in6_addr BinderTest::sSrcAddr;
96in6_addr BinderTest::sDstAddr;
97char BinderTest::sSrcStr[INET6_ADDRSTRLEN];
98char BinderTest::sDstStr[INET6_ADDRSTRLEN];
Lorenzo Colitti89faa342016-02-26 11:38:47 +090099
Lorenzo Colitti699aa992016-04-15 10:22:37 +0900100class TimedOperation : public Stopwatch {
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900101public:
Lorenzo Colitti699aa992016-04-15 10:22:37 +0900102 TimedOperation(std::string name): mName(name) {}
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900103 virtual ~TimedOperation() {
Lorenzo Colitti699aa992016-04-15 10:22:37 +0900104 fprintf(stderr, " %s: %6.1f ms\n", mName.c_str(), timeTaken());
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900105 }
106
107private:
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900108 std::string mName;
109};
110
111TEST_F(BinderTest, TestIsAlive) {
112 TimedOperation t("isAlive RPC");
113 bool isAlive = false;
114 mNetd->isAlive(&isAlive);
115 ASSERT_TRUE(isAlive);
116}
117
118static int randomUid() {
119 return 100000 * arc4random_uniform(7) + 10000 + arc4random_uniform(5000);
120}
121
Robin Leeb8087362016-03-30 18:43:08 +0100122static std::vector<std::string> runCommand(const std::string& command) {
Lorenzo Colittidedd2712016-03-22 12:36:29 +0900123 std::vector<std::string> lines;
124 FILE *f;
125
Lorenzo Colittidedd2712016-03-22 12:36:29 +0900126 if ((f = popen(command.c_str(), "r")) == nullptr) {
127 perror("popen");
128 return lines;
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900129 }
Lorenzo Colittidedd2712016-03-22 12:36:29 +0900130
131 char *line = nullptr;
Robin Leeb8087362016-03-30 18:43:08 +0100132 size_t bufsize = 0;
133 ssize_t linelen = 0;
134 while ((linelen = getline(&line, &bufsize, f)) >= 0) {
Lorenzo Colittidedd2712016-03-22 12:36:29 +0900135 lines.push_back(std::string(line, linelen));
136 free(line);
137 line = nullptr;
138 }
139
140 pclose(f);
141 return lines;
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900142}
143
Robin Leeb8087362016-03-30 18:43:08 +0100144static std::vector<std::string> listIpRules(const char *ipVersion) {
145 std::string command = StringPrintf("%s %s rule list", IP_PATH, ipVersion);
146 return runCommand(command);
147}
148
149static std::vector<std::string> listIptablesRule(const char *binary, const char *chainName) {
Lorenzo Colitti80545772016-06-09 14:20:08 +0900150 std::string command = StringPrintf("%s -w -n -L %s", binary, chainName);
Robin Leeb8087362016-03-30 18:43:08 +0100151 return runCommand(command);
152}
153
Lorenzo Colittidedd2712016-03-22 12:36:29 +0900154static int iptablesRuleLineLength(const char *binary, const char *chainName) {
155 return listIptablesRule(binary, chainName).size();
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900156}
157
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900158TEST_F(BinderTest, TestFirewallReplaceUidChain) {
159 std::string chainName = StringPrintf("netd_binder_test_%u", arc4random_uniform(10000));
160 const int kNumUids = 500;
161 std::vector<int32_t> noUids(0);
162 std::vector<int32_t> uids(kNumUids);
163 for (int i = 0; i < kNumUids; i++) {
164 uids[i] = randomUid();
165 }
166
167 bool ret;
168 {
169 TimedOperation op(StringPrintf("Programming %d-UID whitelist chain", kNumUids));
170 mNetd->firewallReplaceUidChain(String16(chainName.c_str()), true, uids, &ret);
171 }
172 EXPECT_EQ(true, ret);
Lorenzo Colittia95e1142016-07-26 17:59:41 +0900173 EXPECT_EQ((int) uids.size() + 6, iptablesRuleLineLength(IPTABLES_PATH, chainName.c_str()));
174 EXPECT_EQ((int) uids.size() + 12, iptablesRuleLineLength(IP6TABLES_PATH, chainName.c_str()));
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900175 {
176 TimedOperation op("Clearing whitelist chain");
177 mNetd->firewallReplaceUidChain(String16(chainName.c_str()), false, noUids, &ret);
178 }
179 EXPECT_EQ(true, ret);
Lorenzo Colittia95e1142016-07-26 17:59:41 +0900180 EXPECT_EQ(4, iptablesRuleLineLength(IPTABLES_PATH, chainName.c_str()));
181 EXPECT_EQ(4, iptablesRuleLineLength(IP6TABLES_PATH, chainName.c_str()));
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900182
183 {
184 TimedOperation op(StringPrintf("Programming %d-UID blacklist chain", kNumUids));
185 mNetd->firewallReplaceUidChain(String16(chainName.c_str()), false, uids, &ret);
186 }
187 EXPECT_EQ(true, ret);
Lorenzo Colittia95e1142016-07-26 17:59:41 +0900188 EXPECT_EQ((int) uids.size() + 4, iptablesRuleLineLength(IPTABLES_PATH, chainName.c_str()));
189 EXPECT_EQ((int) uids.size() + 4, iptablesRuleLineLength(IP6TABLES_PATH, chainName.c_str()));
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900190
191 {
192 TimedOperation op("Clearing blacklist chain");
193 mNetd->firewallReplaceUidChain(String16(chainName.c_str()), false, noUids, &ret);
194 }
195 EXPECT_EQ(true, ret);
Lorenzo Colittia95e1142016-07-26 17:59:41 +0900196 EXPECT_EQ(4, iptablesRuleLineLength(IPTABLES_PATH, chainName.c_str()));
197 EXPECT_EQ(4, iptablesRuleLineLength(IP6TABLES_PATH, chainName.c_str()));
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900198
199 // Check that the call fails if iptables returns an error.
200 std::string veryLongStringName = "netd_binder_test_UnacceptablyLongIptablesChainName";
201 mNetd->firewallReplaceUidChain(String16(veryLongStringName.c_str()), true, noUids, &ret);
202 EXPECT_EQ(false, ret);
203}
Lorenzo Colittidedd2712016-03-22 12:36:29 +0900204
205static int bandwidthDataSaverEnabled(const char *binary) {
Lorenzo Colitti464eabe2016-03-25 13:38:19 +0900206 std::vector<std::string> lines = listIptablesRule(binary, "bw_data_saver");
Lorenzo Colittidedd2712016-03-22 12:36:29 +0900207
208 // Output looks like this:
209 //
Lorenzo Colitti464eabe2016-03-25 13:38:19 +0900210 // Chain bw_data_saver (1 references)
Lorenzo Colittidedd2712016-03-22 12:36:29 +0900211 // target prot opt source destination
Lorenzo Colittidedd2712016-03-22 12:36:29 +0900212 // RETURN all -- 0.0.0.0/0 0.0.0.0/0
Lorenzo Colitti464eabe2016-03-25 13:38:19 +0900213 EXPECT_EQ(3U, lines.size());
214 if (lines.size() != 3) return -1;
Lorenzo Colittidedd2712016-03-22 12:36:29 +0900215
Lorenzo Colitti464eabe2016-03-25 13:38:19 +0900216 EXPECT_TRUE(android::base::StartsWith(lines[2], "RETURN ") ||
217 android::base::StartsWith(lines[2], "REJECT "));
Lorenzo Colittidedd2712016-03-22 12:36:29 +0900218
Lorenzo Colitti464eabe2016-03-25 13:38:19 +0900219 return android::base::StartsWith(lines[2], "REJECT");
Lorenzo Colittidedd2712016-03-22 12:36:29 +0900220}
221
222bool enableDataSaver(sp<INetd>& netd, bool enable) {
223 TimedOperation op(enable ? " Enabling data saver" : "Disabling data saver");
224 bool ret;
225 netd->bandwidthEnableDataSaver(enable, &ret);
226 return ret;
227}
228
229int getDataSaverState() {
230 const int enabled4 = bandwidthDataSaverEnabled(IPTABLES_PATH);
231 const int enabled6 = bandwidthDataSaverEnabled(IP6TABLES_PATH);
232 EXPECT_EQ(enabled4, enabled6);
233 EXPECT_NE(-1, enabled4);
234 EXPECT_NE(-1, enabled6);
235 if (enabled4 != enabled6 || (enabled6 != 0 && enabled6 != 1)) {
236 return -1;
237 }
238 return enabled6;
239}
240
241TEST_F(BinderTest, TestBandwidthEnableDataSaver) {
242 const int wasEnabled = getDataSaverState();
243 ASSERT_NE(-1, wasEnabled);
244
245 if (wasEnabled) {
246 ASSERT_TRUE(enableDataSaver(mNetd, false));
247 EXPECT_EQ(0, getDataSaverState());
248 }
249
250 ASSERT_TRUE(enableDataSaver(mNetd, false));
251 EXPECT_EQ(0, getDataSaverState());
252
253 ASSERT_TRUE(enableDataSaver(mNetd, true));
254 EXPECT_EQ(1, getDataSaverState());
255
256 ASSERT_TRUE(enableDataSaver(mNetd, true));
257 EXPECT_EQ(1, getDataSaverState());
258
259 if (!wasEnabled) {
260 ASSERT_TRUE(enableDataSaver(mNetd, false));
261 EXPECT_EQ(0, getDataSaverState());
262 }
263}
Robin Leeb8087362016-03-30 18:43:08 +0100264
265static bool ipRuleExistsForRange(const uint32_t priority, const UidRange& range,
266 const std::string& action, const char* ipVersion) {
267 // Output looks like this:
Robin Lee6c84ef62016-05-03 13:17:58 +0100268 // "12500:\tfrom all fwmark 0x0/0x20000 iif lo uidrange 1000-2000 prohibit"
Robin Leeb8087362016-03-30 18:43:08 +0100269 std::vector<std::string> rules = listIpRules(ipVersion);
270
271 std::string prefix = StringPrintf("%" PRIu32 ":", priority);
272 std::string suffix = StringPrintf(" iif lo uidrange %d-%d %s\n",
273 range.getStart(), range.getStop(), action.c_str());
274 for (std::string line : rules) {
275 if (android::base::StartsWith(line, prefix.c_str())
276 && android::base::EndsWith(line, suffix.c_str())) {
277 return true;
278 }
279 }
280 return false;
281}
282
283static bool ipRuleExistsForRange(const uint32_t priority, const UidRange& range,
284 const std::string& action) {
285 bool existsIp4 = ipRuleExistsForRange(priority, range, action, IP_RULE_V4);
286 bool existsIp6 = ipRuleExistsForRange(priority, range, action, IP_RULE_V6);
287 EXPECT_EQ(existsIp4, existsIp6);
288 return existsIp4;
289}
290
291TEST_F(BinderTest, TestNetworkRejectNonSecureVpn) {
Robin Lee6c84ef62016-05-03 13:17:58 +0100292 constexpr uint32_t RULE_PRIORITY = 12500;
Robin Leeb8087362016-03-30 18:43:08 +0100293
294 constexpr int baseUid = MULTIUSER_APP_PER_USER_RANGE * 5;
295 std::vector<UidRange> uidRanges = {
296 {baseUid + 150, baseUid + 224},
297 {baseUid + 226, baseUid + 300}
298 };
299
300 const std::vector<std::string> initialRulesV4 = listIpRules(IP_RULE_V4);
301 const std::vector<std::string> initialRulesV6 = listIpRules(IP_RULE_V6);
302
303 // Create two valid rules.
304 ASSERT_TRUE(mNetd->networkRejectNonSecureVpn(true, uidRanges).isOk());
305 EXPECT_EQ(initialRulesV4.size() + 2, listIpRules(IP_RULE_V4).size());
306 EXPECT_EQ(initialRulesV6.size() + 2, listIpRules(IP_RULE_V6).size());
307 for (auto const& range : uidRanges) {
308 EXPECT_TRUE(ipRuleExistsForRange(RULE_PRIORITY, range, "prohibit"));
309 }
310
311 // Remove the rules.
312 ASSERT_TRUE(mNetd->networkRejectNonSecureVpn(false, uidRanges).isOk());
313 EXPECT_EQ(initialRulesV4.size(), listIpRules(IP_RULE_V4).size());
314 EXPECT_EQ(initialRulesV6.size(), listIpRules(IP_RULE_V6).size());
315 for (auto const& range : uidRanges) {
316 EXPECT_FALSE(ipRuleExistsForRange(RULE_PRIORITY, range, "prohibit"));
317 }
318
319 // Fail to remove the rules a second time after they are already deleted.
320 binder::Status status = mNetd->networkRejectNonSecureVpn(false, uidRanges);
321 ASSERT_EQ(binder::Status::EX_SERVICE_SPECIFIC, status.exceptionCode());
322 EXPECT_EQ(ENOENT, status.serviceSpecificErrorCode());
323
324 // All rules should be the same as before.
325 EXPECT_EQ(initialRulesV4, listIpRules(IP_RULE_V4));
326 EXPECT_EQ(initialRulesV6, listIpRules(IP_RULE_V6));
327}
Lorenzo Colitti563d98b2016-04-24 13:13:14 +0900328
Lorenzo Colitti755faa92016-07-27 22:10:49 +0900329int BinderTest::createTunInterface() {
330 // Generate a random ULA address pair.
331 arc4random_buf(&sSrcAddr, sizeof(sSrcAddr));
332 sSrcAddr.s6_addr[0] = 0xfd;
333 memcpy(&sDstAddr, &sSrcAddr, sizeof(sDstAddr));
334 sDstAddr.s6_addr[15] ^= 1;
335
336 // Convert the addresses to strings because that's what ifc_add_address takes.
337 sockaddr_in6 src6 = { .sin6_family = AF_INET6, .sin6_addr = sSrcAddr, };
338 sockaddr_in6 dst6 = { .sin6_family = AF_INET6, .sin6_addr = sDstAddr, };
339 int flags = NI_NUMERICHOST;
340 if (getnameinfo((sockaddr *) &src6, sizeof(src6), sSrcStr, sizeof(sSrcStr), NULL, 0, flags) ||
341 getnameinfo((sockaddr *) &dst6, sizeof(dst6), sDstStr, sizeof(sDstStr), NULL, 0, flags)) {
342 return -1;
343 }
344
345 // Create a tun interface with a name based on our PID.
346 struct ifreq ifr = {
347 .ifr_ifru = { .ifru_flags = IFF_TUN },
348 };
349 snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "netdtest%u", getpid());
350
351 int fd = open(TUN_DEV, O_RDWR | O_NONBLOCK | O_CLOEXEC);
352 EXPECT_NE(-1, fd) << TUN_DEV << ": " << strerror(errno);
353 if (fd == -1) return fd;
354
355 int ret = ioctl(fd, TUNSETIFF, &ifr, sizeof(ifr));
356 EXPECT_EQ(0, ret) << "TUNSETIFF: " << strerror(errno);
357 if (ret) {
358 close(fd);
359 return -1;
360 }
361
362 if (ifc_add_address(ifr.ifr_name, sSrcStr, 64) ||
363 ifc_add_address(ifr.ifr_name, sDstStr, 64)) {
364 close(fd);
365 return -1;
366 }
367 return fd;
368}
369
370// Create a socket pair that isLoopbackSocket won't think is local.
371void BinderTest::fakeRemoteSocketPair(int *clientSocket, int *serverSocket, int *acceptedSocket) {
Lorenzo Colitti563d98b2016-04-24 13:13:14 +0900372 *serverSocket = socket(AF_INET6, SOCK_STREAM, 0);
Lorenzo Colitti755faa92016-07-27 22:10:49 +0900373 struct sockaddr_in6 server6 = { .sin6_family = AF_INET6, .sin6_addr = sDstAddr };
Lorenzo Colitti563d98b2016-04-24 13:13:14 +0900374 ASSERT_EQ(0, bind(*serverSocket, (struct sockaddr *) &server6, sizeof(server6)));
375
376 socklen_t addrlen = sizeof(server6);
377 ASSERT_EQ(0, getsockname(*serverSocket, (struct sockaddr *) &server6, &addrlen));
378 ASSERT_EQ(0, listen(*serverSocket, 10));
379
380 *clientSocket = socket(AF_INET6, SOCK_STREAM, 0);
Lorenzo Colitti755faa92016-07-27 22:10:49 +0900381 struct sockaddr_in6 client6 = { .sin6_family = AF_INET6, .sin6_addr = sSrcAddr };
382 ASSERT_EQ(0, bind(*clientSocket, (struct sockaddr *) &client6, sizeof(client6)));
Lorenzo Colitti563d98b2016-04-24 13:13:14 +0900383 ASSERT_EQ(0, connect(*clientSocket, (struct sockaddr *) &server6, sizeof(server6)));
384 ASSERT_EQ(0, getsockname(*clientSocket, (struct sockaddr *) &client6, &addrlen));
385
386 *acceptedSocket = accept(*serverSocket, (struct sockaddr *) &server6, &addrlen);
387 ASSERT_NE(-1, *acceptedSocket);
388
389 ASSERT_EQ(0, memcmp(&client6, &server6, sizeof(client6)));
390}
391
392void checkSocketpairOpen(int clientSocket, int acceptedSocket) {
393 char buf[4096];
394 EXPECT_EQ(4, write(clientSocket, "foo", sizeof("foo")));
395 EXPECT_EQ(4, read(acceptedSocket, buf, sizeof(buf)));
396 EXPECT_EQ(0, memcmp(buf, "foo", sizeof("foo")));
397}
398
399void checkSocketpairClosed(int clientSocket, int acceptedSocket) {
400 // Check that the client socket was closed with ECONNABORTED.
401 int ret = write(clientSocket, "foo", sizeof("foo"));
402 int err = errno;
403 EXPECT_EQ(-1, ret);
404 EXPECT_EQ(ECONNABORTED, err);
405
406 // Check that it sent a RST to the server.
407 ret = write(acceptedSocket, "foo", sizeof("foo"));
408 err = errno;
409 EXPECT_EQ(-1, ret);
410 EXPECT_EQ(ECONNRESET, err);
411}
412
413TEST_F(BinderTest, TestSocketDestroy) {
414 int clientSocket, serverSocket, acceptedSocket;
Lorenzo Colitti755faa92016-07-27 22:10:49 +0900415 ASSERT_NO_FATAL_FAILURE(fakeRemoteSocketPair(&clientSocket, &serverSocket, &acceptedSocket));
Lorenzo Colitti563d98b2016-04-24 13:13:14 +0900416
417 // Pick a random UID in the system UID range.
418 constexpr int baseUid = AID_APP - 2000;
419 static_assert(baseUid > 0, "Not enough UIDs? Please fix this test.");
420 int uid = baseUid + 500 + arc4random_uniform(1000);
421 EXPECT_EQ(0, fchown(clientSocket, uid, -1));
422
423 // UID ranges that don't contain uid.
424 std::vector<UidRange> uidRanges = {
425 {baseUid + 42, baseUid + 449},
426 {baseUid + 1536, AID_APP - 4},
427 {baseUid + 498, uid - 1},
428 {uid + 1, baseUid + 1520},
429 };
430 // A skip list that doesn't contain UID.
431 std::vector<int32_t> skipUids { baseUid + 123, baseUid + 1600 };
432
433 // Close sockets. Our test socket should be intact.
434 EXPECT_TRUE(mNetd->socketDestroy(uidRanges, skipUids).isOk());
435 checkSocketpairOpen(clientSocket, acceptedSocket);
436
437 // UID ranges that do contain uid.
438 uidRanges = {
439 {baseUid + 42, baseUid + 449},
440 {baseUid + 1536, AID_APP - 4},
441 {baseUid + 498, baseUid + 1520},
442 };
443 // Add uid to the skip list.
444 skipUids.push_back(uid);
445
446 // Close sockets. Our test socket should still be intact because it's in the skip list.
447 EXPECT_TRUE(mNetd->socketDestroy(uidRanges, skipUids).isOk());
448 checkSocketpairOpen(clientSocket, acceptedSocket);
449
450 // Now remove uid from skipUids, and close sockets. Our test socket should have been closed.
451 skipUids.resize(skipUids.size() - 1);
452 EXPECT_TRUE(mNetd->socketDestroy(uidRanges, skipUids).isOk());
453 checkSocketpairClosed(clientSocket, acceptedSocket);
454
455 close(clientSocket);
456 close(serverSocket);
457 close(acceptedSocket);
458}