net/LocalSocketAddress: new class wrapping struct sockaddr_un

This commit is contained in:
Max Kellermann
2024-04-29 16:46:31 +02:00
committed by Max Kellermann
parent 7c9b7fa311
commit 73509fc189
4 changed files with 203 additions and 0 deletions

View File

@@ -2,12 +2,44 @@
// author: Max Kellermann <max.kellermann@gmail.com>
#include "net/AllocatedSocketAddress.hxx"
#include "net/LocalSocketAddress.hxx"
#include "net/ToString.hxx"
#include <gtest/gtest.h>
#include <sys/un.h>
TEST(LocalSocketAddress, Path1)
{
const char *path = "/run/foo/bar.socket";
LocalSocketAddress a;
a.SetLocal(path);
EXPECT_TRUE(a.IsDefined());
EXPECT_EQ(a.GetFamily(), AF_LOCAL);
EXPECT_EQ(ToString(a), path);
EXPECT_STREQ(a.GetLocalPath(), path);
const struct sockaddr *const sa = a;
const auto &sun = *(const struct sockaddr_un *)sa;
EXPECT_STREQ(sun.sun_path, path);
EXPECT_EQ(sun.sun_path + strlen(path) + 1, (const char *)sa + a.GetSize());
}
TEST(LocalSocketAddress, Path2)
{
static constexpr const char *path = "/run/foo/bar.socket";
static constexpr LocalSocketAddress a{path};
EXPECT_TRUE(a.IsDefined());
EXPECT_EQ(a.GetFamily(), AF_LOCAL);
EXPECT_EQ(ToString(a), path);
EXPECT_STREQ(a.GetLocalPath(), path);
const struct sockaddr *const sa = a;
const auto &sun = *(const struct sockaddr_un *)sa;
EXPECT_STREQ(sun.sun_path, path);
EXPECT_EQ(sun.sun_path + strlen(path) + 1, (const char *)sa + a.GetSize());
}
TEST(LocalSocketAddress, Path)
{
const char *path = "/run/foo/bar.socket";
@@ -17,6 +49,7 @@ TEST(LocalSocketAddress, Path)
EXPECT_TRUE(a.IsDefined());
EXPECT_EQ(a.GetFamily(), AF_LOCAL);
EXPECT_EQ(ToString(a), path);
EXPECT_STREQ(a.GetLocalPath(), path);
const auto &sun = *(const struct sockaddr_un *)a.GetAddress();
EXPECT_STREQ(sun.sun_path, path);
@@ -25,6 +58,27 @@ TEST(LocalSocketAddress, Path)
#ifdef __linux__
TEST(LocalSocketAddress, Abstract1)
{
const char *path = "@foo.bar";
LocalSocketAddress a;
a.SetLocal(path);
EXPECT_TRUE(a.IsDefined());
EXPECT_EQ(a.GetFamily(), AF_LOCAL);
EXPECT_EQ(ToString(a), path);
EXPECT_EQ(a.GetLocalPath(), nullptr);
const struct sockaddr *const sa = a;
const auto &sun = *(const struct sockaddr_un *)sa;
/* Linux's abstract sockets start with a null byte, ... */
EXPECT_EQ(sun.sun_path[0], 0);
/* ... but are not null-terminated */
EXPECT_EQ(memcmp(sun.sun_path + 1, path + 1, strlen(path) - 1), 0);
EXPECT_EQ(sun.sun_path + strlen(path), (const char *)sa + a.GetSize());
}
TEST(LocalSocketAddress, Abstract)
{
const char *path = "@foo.bar";
@@ -34,6 +88,7 @@ TEST(LocalSocketAddress, Abstract)
EXPECT_TRUE(a.IsDefined());
EXPECT_EQ(a.GetFamily(), AF_LOCAL);
EXPECT_EQ(ToString(a), path);
EXPECT_EQ(a.GetLocalPath(), nullptr);
const auto &sun = *(const struct sockaddr_un *)a.GetAddress();