Raw ↗
+ +
a18b27f fix(windows): resolve Win32 Winsock, signal, and CLI test compatibility
saifmukhtar · 2026-11-24
1 /*
2 * SPDX-License-Identifier: AGPL-3.0-or-later WITH Classpath-exception-2.0
3 * Copyright (c) 2026 Saif Mukhtar
4 *
5 * OpenAVX - Open Audio-Video eXchange
6 * test/openavx_find_test.c - Automated Verification of openavx-find CLI Utility
7 */
8  
9 #define _GNU_SOURCE
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <assert.h>
14 #include "avx_platform.h"
15  
16 #include "openavx.h"
17 #include "avx_internal.h"
18  
19 static volatile bool s_beacon_running = true;
20 static uint16_t s_send_port = 7960;
21  
22 static void* beacon_thread(void* arg) {
23 (void)arg;
24 while (s_beacon_running) {
25 avx_mdns_broadcast_source("TEST_SCAN_SOURCE_42", s_send_port);
26 usleep(100000); /* Broadcast every 100ms */
27 }
28 return NULL;
29 }
30  
31 static const char* get_find_cmd_prefix(void) {
32 if (access("./bin/openavx-find", X_OK) == 0) return "./bin/openavx-find";
33 if (access("./openavx-find", X_OK) == 0) return "./openavx-find";
34 if (access("./bin/openavx.exe", X_OK) == 0) return "./bin/openavx.exe find";
35 if (access("bin/openavx.exe", X_OK) == 0) return "bin/openavx.exe find";
36 if (access("./openavx.exe", X_OK) == 0) return "./openavx.exe find";
37 if (access("./bin/openavx", X_OK) == 0) return "./bin/openavx find";
38 return "./bin/openavx find";
39 }
40  
41 int main(void) {
42 printf("\n=======================================================\n");
43 printf(" OpenAVX (openavx) Standalone Source Finder CLI Test\n");
44 printf("=======================================================\n\n");
45  
46 const char* find_prefix = get_find_cmd_prefix();
47  
48 /* 1. Test CLI Help Banner */
49 printf("[*] 1. Testing CLI Help Banner (--help)...\n");
50 char cmd[512];
51 #ifdef _WIN32
52 snprintf(cmd, sizeof(cmd), "%s --help > NUL 2>&1", find_prefix);
53 #else
54 snprintf(cmd, sizeof(cmd), "%s --help > /dev/null 2>&1", find_prefix);
55 #endif
56 int ret_help = system(cmd);
57 assert(WIFEXITED(ret_help) && WEXITSTATUS(ret_help) == 0);
58 printf(" [+] Help flag on %s returned exit code 0.\n", find_prefix);
59  
60 /* 2. Start Live Test Sender */
61 printf("[*] 2. Spawning Live Test Sender 'TEST_SCAN_SOURCE_42'...\n");
62 oavx_context_t *ctx = oavx_context_create();
63 assert(ctx != NULL);
64  
65 oavx_stream_config_t send_cfg = {
66 .name = "TEST_SCAN_SOURCE_42",
67 .groups = "public",
68 .direction = OAVX_DIR_TRANSMIT,
69 .clock_sync = false
70 };
71 oavx_stream_t *p_send = oavx_stream_open(ctx, &send_cfg);
72 assert(p_send != NULL);
73  
74 const oavx_endpoint_t *ep = oavx_stream_get_endpoint(p_send);
75 if (ep && ep->address) {
76 const char *colon = strchr(ep->address, ':');
77 if (colon) s_send_port = (uint16_t)atoi(colon + 1);
78 }
79 avx_thread_t th;
80 s_beacon_running = true;
81 avx_thread_create(&th, beacon_thread, NULL);
82 usleep(50000); /* 50ms initial wait */
83  
84 /* 3. Test Standard Text Output */
85 printf("[*] 3. Testing Standard Scanner Output (%s)...\n", find_prefix);
86 snprintf(cmd, sizeof(cmd), "%s --wait 1200", find_prefix);
87 FILE *fp = popen(cmd, "r");
88 assert(fp != NULL);
89 char buf[2048] = {0};
90 size_t bytes_read = fread(buf, 1, sizeof(buf) - 1, fp);
91 int pclose_ret = pclose(fp);
92 assert(WIFEXITED(pclose_ret) && WEXITSTATUS(pclose_ret) == 0);
93 assert(bytes_read > 0);
94 assert(strstr(buf, "TEST_SCAN_SOURCE_42") != NULL);
95 printf(" [+] Successfully discovered 'TEST_SCAN_SOURCE_42' in text output!\n");
96  
97 /* 4. Test Machine-Readable JSON Output */
98 printf("[*] 4. Testing JSON Output (%s --json)...\n", find_prefix);
99 snprintf(cmd, sizeof(cmd), "%s --wait 1200 --json", find_prefix);
100 fp = popen(cmd, "r");
101 assert(fp != NULL);
102 memset(buf, 0, sizeof(buf));
103 bytes_read = fread(buf, 1, sizeof(buf) - 1, fp);
104 pclose_ret = pclose(fp);
105 assert(WIFEXITED(pclose_ret) && WEXITSTATUS(pclose_ret) == 0);
106 assert(bytes_read > 0);
107 assert(strstr(buf, "\"name\": \"TEST_SCAN_SOURCE_42\"") != NULL);
108 assert(buf[0] == '[');
109 printf(" [+] Valid JSON array format verified with matching source name!\n");
110  
111 /* 5. Test Quiet URL Output */
112 printf("[*] 5. Testing Quiet URL Output (%s --quiet)...\n", find_prefix);
113 snprintf(cmd, sizeof(cmd), "%s --wait 1200 --quiet", find_prefix);
114 fp = popen(cmd, "r");
115 assert(fp != NULL);
116 memset(buf, 0, sizeof(buf));
117 bytes_read = fread(buf, 1, sizeof(buf) - 1, fp);
118 pclose_ret = pclose(fp);
119 assert(WIFEXITED(pclose_ret) && WEXITSTATUS(pclose_ret) == 0);
120 assert(bytes_read > 0);
121 assert(strstr(buf, "127.0.0.1") != NULL || strstr(buf, "TEST_SCAN_SOURCE_42") != NULL);
122 printf(" [+] Quiet output returned direct URL/name: %s", buf);
123  
124 /* 6. Clean up */
125 s_beacon_running = false;
126 avx_thread_join(th);
127 oavx_stream_close(p_send);
128 oavx_context_destroy(ctx);
129  
130 printf("\n[PASS] openavx_find_test\n\n");
131 return 0;
132 }