diff options
| author | Luke Huang <huangluke@google.com> | 2019-05-30 11:22:50 +0000 |
|---|---|---|
| committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2019-05-30 11:22:50 +0000 |
| commit | 5477130f5cc49e1fe1b80fa5ac8778d341986485 (patch) | |
| tree | aadd55e903fdab74b3a786229db2499671719158 /client/NetdClientTest.cpp | |
| parent | e038b5b1d6a505aafa51843b0fb496f3e2f6e0c9 (diff) | |
| parent | 63df94810631106480f46e3d0b1c2dd1bd2fdd1a (diff) | |
Merge "Make getdnsnetid returning app_netId instead of dns_netId and fix nits"
Diffstat (limited to 'client/NetdClientTest.cpp')
| -rw-r--r-- | client/NetdClientTest.cpp | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/client/NetdClientTest.cpp b/client/NetdClientTest.cpp new file mode 100644 index 00000000..6f2fc09d --- /dev/null +++ b/client/NetdClientTest.cpp @@ -0,0 +1,75 @@ +/* + * Copyright (C) 2019 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <poll.h> /* poll */ +#include <sys/socket.h> + +#include <thread> + +#include <android-base/parseint.h> +#include <android-base/unique_fd.h> +#include <gtest/gtest.h> + +#include "NetdClient.h" +#include "netdclient_priv.h" + +namespace { + +// Keep in sync with FrameworkListener.cpp (500, "Command not recognized") +constexpr char NOT_SUPPORT_MSG[] = "500 Command not recognized"; + +void serverLoop(int dnsProxyFd) { + while (true) { + pollfd fds[1] = {{.fd = dnsProxyFd, .events = POLLIN}}; + enum { SERVERFD = 0 }; + + const int s = TEMP_FAILURE_RETRY(poll(fds, std::size(fds), -1)); + if (s <= 0) break; + + if (fds[SERVERFD].revents & POLLIN) { + char buf[4096]; + TEMP_FAILURE_RETRY(read(fds[SERVERFD].fd, &buf, sizeof(buf))); + // TODO: verify command + TEMP_FAILURE_RETRY(write(fds[SERVERFD].fd, NOT_SUPPORT_MSG, sizeof(NOT_SUPPORT_MSG))); + } + } +} + +} // namespace + +TEST(NetdClientTest, getNetworkForDnsInternal) { + // Test invalid fd + unsigned dnsNetId = 0; + const int invalidFd = -1; + EXPECT_EQ(-EBADF, getNetworkForDnsInternal(invalidFd, &dnsNetId)); + + // Test what the client does if the resolver does not support the "getdnsnetid" command. + android::base::unique_fd clientFd, serverFd; + ASSERT_TRUE(android::base::Socketpair(AF_UNIX, &clientFd, &serverFd)); + + std::thread serverThread = std::thread(serverLoop, serverFd.get()); + + EXPECT_EQ(-EOPNOTSUPP, getNetworkForDnsInternal(clientFd.get(), &dnsNetId)); + + clientFd.reset(); // Causes serverLoop() to exit + serverThread.join(); +} + +TEST(NetdClientTest, getNetworkForDns) { + // Test null input + unsigned* testNull = nullptr; + EXPECT_EQ(-EFAULT, getNetworkForDns(testNull)); +} |
