view seobeo/tests/seobeo_server_bind_test.c @ 257:609d3c6aff4e

[seobeo] Add persistent SSE streams Co-authored-by: Copilot <[email protected]>
author MrJuneJune <me@mrjunejune.com>
date Tue, 04 Aug 2026 16:49:11 -0700
parents 117c4d53c9a4
children
line wrap: on
line source

#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;
}