sockaddr_util_posix_unittest.cc 3.6 KB

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