1 /*
2  * Copyright (C) 2020 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 <sys/un.h>
18 #include <unistd.h>
19 
20 #include <android-base/file.h>
21 #include <android-base/stringprintf.h>
22 #include <android-base/unique_fd.h>
23 #include <gtest/gtest.h>
24 
25 using android::base::StringPrintf;
26 using android::base::unique_fd;
27 
28 // logd_writer takes advantage of the fact that connect() can be called multiple times for a DGRAM
29 // socket.  This tests for that behavior.
TEST(liblog,multi_connect_dgram_socket)30 TEST(liblog, multi_connect_dgram_socket) {
31 #ifdef __ANDROID__
32   if (getuid() != 0) {
33     GTEST_SKIP() << "Skipping test, must be run as root.";
34     return;
35   }
36   auto temp_dir = TemporaryDir();
37   auto socket_path = StringPrintf("%s/test_socket", temp_dir.path);
38 
39   unique_fd server_socket;
40 
41   auto open_server_socket = [&] {
42     server_socket.reset(TEMP_FAILURE_RETRY(socket(AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, 0)));
43     ASSERT_TRUE(server_socket.ok());
44 
45     sockaddr_un server_sockaddr = {};
46     server_sockaddr.sun_family = AF_UNIX;
47     strlcpy(server_sockaddr.sun_path, socket_path.c_str(), sizeof(server_sockaddr.sun_path));
48     ASSERT_EQ(0,
49               TEMP_FAILURE_RETRY(bind(server_socket, reinterpret_cast<sockaddr*>(&server_sockaddr),
50                                       sizeof(server_sockaddr))));
51   };
52 
53   // Open the server socket.
54   open_server_socket();
55 
56   // Open the client socket.
57   auto client_socket =
58       unique_fd{TEMP_FAILURE_RETRY(socket(AF_UNIX, SOCK_DGRAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0))};
59   ASSERT_TRUE(client_socket.ok());
60   sockaddr_un client_sockaddr = {};
61   client_sockaddr.sun_family = AF_UNIX;
62   strlcpy(client_sockaddr.sun_path, socket_path.c_str(), sizeof(client_sockaddr.sun_path));
63   ASSERT_EQ(0,
64             TEMP_FAILURE_RETRY(connect(client_socket, reinterpret_cast<sockaddr*>(&client_sockaddr),
65                                        sizeof(client_sockaddr))));
66 
67   // Ensure that communication works.
68   constexpr static char kSmoke[] = "smoke test";
69   ssize_t smoke_len = sizeof(kSmoke);
70   ASSERT_EQ(smoke_len, TEMP_FAILURE_RETRY(write(client_socket, kSmoke, sizeof(kSmoke))));
71   char read_buf[512];
72   ASSERT_EQ(smoke_len, TEMP_FAILURE_RETRY(read(server_socket, read_buf, sizeof(read_buf))));
73   ASSERT_STREQ(kSmoke, read_buf);
74 
75   // Close the server socket.
76   server_socket.reset();
77   ASSERT_EQ(0, unlink(socket_path.c_str())) << strerror(errno);
78 
79   // Ensure that write() from the client returns an error since the server is closed.
80   ASSERT_EQ(-1, TEMP_FAILURE_RETRY(write(client_socket, kSmoke, sizeof(kSmoke))));
81   ASSERT_EQ(errno, ECONNREFUSED) << strerror(errno);
82 
83   // Open the server socket again.
84   open_server_socket();
85 
86   // Reconnect the same client socket.
87   ASSERT_EQ(0,
88             TEMP_FAILURE_RETRY(connect(client_socket, reinterpret_cast<sockaddr*>(&client_sockaddr),
89                                        sizeof(client_sockaddr))))
90       << strerror(errno);
91 
92   // Ensure that communication works.
93   ASSERT_EQ(smoke_len, TEMP_FAILURE_RETRY(write(client_socket, kSmoke, sizeof(kSmoke))));
94   ASSERT_EQ(smoke_len, TEMP_FAILURE_RETRY(read(server_socket, read_buf, sizeof(read_buf))));
95   ASSERT_STREQ(kSmoke, read_buf);
96 #else
97   GTEST_LOG_(INFO) << "This test does nothing.\n";
98 #endif
99 }