1 // Copyright 2022 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "net/base/sockaddr_util_posix.h"
6
7 #include <string.h>
8
9 #include <sys/socket.h>
10 #include <sys/un.h>
11
12 #include "net/base/sockaddr_storage.h"
13 #include "testing/gmock/include/gmock/gmock-matchers.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15
16 namespace net {
17
18 namespace {
19
MaxPathLength(SockaddrStorage * storage)20 size_t MaxPathLength(SockaddrStorage* storage) {
21 // |storage.addr_len| is initialized to the largest possible platform-
22 // dependent value. Subtracting the size of the initial fields in
23 // sockaddr_un gives us the longest permissible path value including space
24 // for an extra NUL character at the front or back.
25 return storage->addr_len - offsetof(struct sockaddr_un, sun_path) - 1;
26 }
27
28 } // namespace
29
TEST(FillUnixAddressTest,SimpleAddress)30 TEST(FillUnixAddressTest, SimpleAddress) {
31 SockaddrStorage storage;
32 std::string path = "/tmp/socket/path";
33
34 EXPECT_TRUE(
35 FillUnixAddress(path, /*use_abstract_namespace=*/false, &storage));
36
37 // |storage.addr_len| indicates the full size of the data in sockaddr_un.
38 // The size is increased by one byte to include the string NUL terminator.
39 EXPECT_EQ(path.size() + 1U + offsetof(struct sockaddr_un, sun_path),
40 (unsigned int)storage.addr_len);
41
42 struct sockaddr_un* socket_addr =
43 reinterpret_cast<struct sockaddr_un*>(storage.addr);
44 EXPECT_EQ(socket_addr->sun_family, AF_UNIX);
45
46 // Implicit conversion to std::string for comparison is fine since the path
47 // is always NUL terminated.
48 EXPECT_EQ(socket_addr->sun_path, path);
49 }
50
TEST(FillUnixAddressTest,PathEmpty)51 TEST(FillUnixAddressTest, PathEmpty) {
52 SockaddrStorage storage;
53 std::string path = "";
54 EXPECT_FALSE(
55 FillUnixAddress(path, /*use_abstract_namespace=*/false, &storage));
56 }
57
TEST(FillUnixAddressTest,AddressMaxLength)58 TEST(FillUnixAddressTest, AddressMaxLength) {
59 SockaddrStorage storage;
60 size_t path_max = MaxPathLength(&storage);
61 std::string path(path_max, '0');
62
63 EXPECT_TRUE(
64 FillUnixAddress(path, /*use_abstract_namespace=*/false, &storage));
65
66 struct sockaddr_un* socket_addr =
67 reinterpret_cast<struct sockaddr_un*>(storage.addr);
68 EXPECT_EQ(socket_addr->sun_family, AF_UNIX);
69 EXPECT_EQ(socket_addr->sun_path, path);
70 }
71
TEST(FillUnixAddressTest,AddressTooLong)72 TEST(FillUnixAddressTest, AddressTooLong) {
73 SockaddrStorage storage;
74 size_t path_max = MaxPathLength(&storage);
75 std::string path(path_max + 1, '0');
76
77 EXPECT_FALSE(
78 FillUnixAddress(path, /*use_abstract_namespace=*/false, &storage));
79 }
80
TEST(FillUnixAddressTest,AbstractLinuxAddress)81 TEST(FillUnixAddressTest, AbstractLinuxAddress) {
82 SockaddrStorage storage;
83 size_t path_max = MaxPathLength(&storage);
84 std::string path(path_max, '0');
85
86 #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
87 EXPECT_TRUE(FillUnixAddress(path, /*use_abstract_namespace=*/true, &storage));
88
89 EXPECT_EQ(path.size() + 1U + offsetof(struct sockaddr_un, sun_path),
90 (unsigned int)storage.addr_len);
91
92 struct sockaddr_un* socket_addr =
93 reinterpret_cast<struct sockaddr_un*>(storage.addr);
94 EXPECT_EQ(socket_addr->sun_family, AF_UNIX);
95
96 // The path buffer is preceded by a NUL character for abstract Linux
97 // addresses.
98 EXPECT_EQ(socket_addr->sun_path[0], '\0');
99
100 // The path string may not be NUL terminated, so do a buffer copy when
101 // converting to std::string.
102 std::string unix_path(reinterpret_cast<char*>(socket_addr->sun_path + 1),
103 path.size());
104 EXPECT_EQ(unix_path, path);
105 #else
106 // Other platforms don't support the abstract Linux namespace.
107 EXPECT_FALSE(
108 FillUnixAddress(path, /*use_abstract_namespace=*/true, &storage));
109 #endif
110 }
111
112 } // namespace net
113