blob: 126c7fd1c3417008926694583c4fbbf37a44142f [file] [log] [blame]
Luke Huang63df9482019-05-25 18:24:03 +08001/*
2 * Copyright (C) 2019 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
17#include <poll.h> /* poll */
18#include <sys/socket.h>
19
20#include <thread>
21
22#include <android-base/parseint.h>
23#include <android-base/unique_fd.h>
24#include <gtest/gtest.h>
25
26#include "NetdClient.h"
27#include "netdclient_priv.h"
28
29namespace {
30
31// Keep in sync with FrameworkListener.cpp (500, "Command not recognized")
32constexpr char NOT_SUPPORT_MSG[] = "500 Command not recognized";
33
34void serverLoop(int dnsProxyFd) {
35 while (true) {
36 pollfd fds[1] = {{.fd = dnsProxyFd, .events = POLLIN}};
37 enum { SERVERFD = 0 };
38
Luke Huang76577c62019-06-04 01:33:14 +080039 int poll_result = TEMP_FAILURE_RETRY(poll(fds, std::size(fds), -1));
40 ASSERT_GT(poll_result, 0);
Luke Huang63df9482019-05-25 18:24:03 +080041
Luke Huang76577c62019-06-04 01:33:14 +080042 if (fds[SERVERFD].revents & POLLERR) return;
Luke Huang63df9482019-05-25 18:24:03 +080043 if (fds[SERVERFD].revents & POLLIN) {
44 char buf[4096];
45 TEMP_FAILURE_RETRY(read(fds[SERVERFD].fd, &buf, sizeof(buf)));
46 // TODO: verify command
47 TEMP_FAILURE_RETRY(write(fds[SERVERFD].fd, NOT_SUPPORT_MSG, sizeof(NOT_SUPPORT_MSG)));
48 }
49 }
50}
51
52} // namespace
53
54TEST(NetdClientTest, getNetworkForDnsInternal) {
55 // Test invalid fd
56 unsigned dnsNetId = 0;
57 const int invalidFd = -1;
58 EXPECT_EQ(-EBADF, getNetworkForDnsInternal(invalidFd, &dnsNetId));
59
60 // Test what the client does if the resolver does not support the "getdnsnetid" command.
61 android::base::unique_fd clientFd, serverFd;
62 ASSERT_TRUE(android::base::Socketpair(AF_UNIX, &clientFd, &serverFd));
63
64 std::thread serverThread = std::thread(serverLoop, serverFd.get());
65
66 EXPECT_EQ(-EOPNOTSUPP, getNetworkForDnsInternal(clientFd.get(), &dnsNetId));
67
68 clientFd.reset(); // Causes serverLoop() to exit
69 serverThread.join();
70}
71
72TEST(NetdClientTest, getNetworkForDns) {
73 // Test null input
74 unsigned* testNull = nullptr;
75 EXPECT_EQ(-EFAULT, getNetworkForDns(testNull));
76}
Treehugger Robot1b966652020-04-07 05:50:41 +000077
78TEST(NetdClientTest, protectFromVpnBadFd) {
79 EXPECT_EQ(-EBADF, protectFromVpn(-1));
80}
81
82TEST(NetdClientTest, protectFromVpnUnixStream) {
83 int s = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
84 ASSERT_GE(s, 3);
85 EXPECT_EQ(-EAFNOSUPPORT, protectFromVpn(s));
86 close(s);
87}
88
89TEST(NetdClientTest, protectFromVpnTcp6) {
90 int s = socket(AF_INET6, SOCK_STREAM | SOCK_CLOEXEC, 0);
91 ASSERT_GE(s, 3);
92 EXPECT_EQ(0, protectFromVpn(s));
93 close(s);
94}