diff seobeo/tests/seobeo_server_bind_test.c @ 251:117c4d53c9a4

[ui] Add HTML-first Web Component system Co-authored-by: Copilot <[email protected]>
author MrJuneJune <me@mrjunejune.com>
date Tue, 04 Aug 2026 09:14:57 -0700
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/seobeo/tests/seobeo_server_bind_test.c	Tue Aug 04 09:14:57 2026 -0700
@@ -0,0 +1,59 @@
+#include "seobeo/seobeo.h"
+
+#include <assert.h>
+#include <dirent.h>
+#include <netinet/in.h>
+#include <sys/socket.h>
+#include <unistd.h>
+
+static int open_descriptor_count(void)
+{
+  DIR *directory = opendir("/proc/self/fd");
+  assert(directory);
+  int count = 0;
+  struct dirent *entry;
+  while ((entry = readdir(directory)) != NULL)
+  {
+    if (entry->d_name[0] != '.')
+      count++;
+  }
+  closedir(directory);
+  return count;
+}
+
+int main(void)
+{
+  int listener = socket(AF_INET, SOCK_STREAM, 0);
+  assert(listener >= 0);
+
+  struct sockaddr_in address = {
+    .sin_family = AF_INET,
+    .sin_addr.s_addr = htonl(INADDR_LOOPBACK),
+    .sin_port = 0,
+  };
+  assert(bind(
+      listener,
+      (struct sockaddr *)&address,
+      sizeof(address)) == 0);
+  assert(listen(listener, 1) == 0);
+
+  socklen_t address_length = sizeof(address);
+  assert(getsockname(
+      listener,
+      (struct sockaddr *)&address,
+      &address_length) == 0);
+  char port[16];
+  snprintf(port, sizeof(port), "%u", ntohs(address.sin_port));
+
+  int before = open_descriptor_count();
+  Seobeo_Handle *server =
+      Seobeo_Stream_Handle_Server_Create("127.0.0.1", port);
+  assert(server == NULL);
+  assert(open_descriptor_count() == before);
+
+  assert(Seobeo_Stream_Handle_Server_Create(
+      "127.0.0.1",
+      "not-a-port") == NULL);
+  close(listener);
+  return 0;
+}