comparison 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
comparison
equal deleted inserted replaced
250:745fd127b2a1 251:117c4d53c9a4
1 #include "seobeo/seobeo.h"
2
3 #include <assert.h>
4 #include <dirent.h>
5 #include <netinet/in.h>
6 #include <sys/socket.h>
7 #include <unistd.h>
8
9 static int open_descriptor_count(void)
10 {
11 DIR *directory = opendir("/proc/self/fd");
12 assert(directory);
13 int count = 0;
14 struct dirent *entry;
15 while ((entry = readdir(directory)) != NULL)
16 {
17 if (entry->d_name[0] != '.')
18 count++;
19 }
20 closedir(directory);
21 return count;
22 }
23
24 int main(void)
25 {
26 int listener = socket(AF_INET, SOCK_STREAM, 0);
27 assert(listener >= 0);
28
29 struct sockaddr_in address = {
30 .sin_family = AF_INET,
31 .sin_addr.s_addr = htonl(INADDR_LOOPBACK),
32 .sin_port = 0,
33 };
34 assert(bind(
35 listener,
36 (struct sockaddr *)&address,
37 sizeof(address)) == 0);
38 assert(listen(listener, 1) == 0);
39
40 socklen_t address_length = sizeof(address);
41 assert(getsockname(
42 listener,
43 (struct sockaddr *)&address,
44 &address_length) == 0);
45 char port[16];
46 snprintf(port, sizeof(port), "%u", ntohs(address.sin_port));
47
48 int before = open_descriptor_count();
49 Seobeo_Handle *server =
50 Seobeo_Stream_Handle_Server_Create("127.0.0.1", port);
51 assert(server == NULL);
52 assert(open_descriptor_count() == before);
53
54 assert(Seobeo_Stream_Handle_Server_Create(
55 "127.0.0.1",
56 "not-a-port") == NULL);
57 close(listener);
58 return 0;
59 }