+ +
a18b27f fix(windows): resolve Win32 Winsock, signal, and CLI test compatibility
saifmukhtar · 2026-11-24
main
| 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_tally_test.c - Automated Verification of openavx tally CLI Utility & APIs |
| 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 | #if defined(AVX_OS_POSIX) |
| 20 | #include <unistd.h> |
| 21 | #include <sys/wait.h> |
| 22 | #endif |
| 23 | |
| 24 | static int run_cmd_capture(const char* cmd, char* out_buf, size_t out_sz) { |
| 25 | if (out_buf && out_sz > 0) out_buf[0] = '\0'; |
| 26 | FILE* fp = popen(cmd, "r"); |
| 27 | if (!fp) return -1; |
| 28 | if (out_buf && out_sz > 0) { |
| 29 | size_t n = fread(out_buf, 1, out_sz - 1, fp); |
| 30 | out_buf[n] = '\0'; |
| 31 | } |
| 32 | int status = pclose(fp); |
| 33 | #if defined(AVX_OS_POSIX) |
| 34 | if (WIFEXITED(status)) return WEXITSTATUS(status); |
| 35 | #endif |
| 36 | return status; |
| 37 | } |
| 38 | |
| 39 | static const char* find_tally_binary(bool symlink) { |
| 40 | static char found_cli[256]; |
| 41 | static char found_sym[256]; |
| 42 | char* found = symlink ? found_sym : found_cli; |
| 43 | const char* symlink_candidates[] = { |
| 44 | "./bin/openavx-tally", |
| 45 | "./bin/openavx_tally", |
| 46 | "./openavx-tally", |
| 47 | "./build/bin/openavx-tally", |
| 48 | "./build/bin/openavx_tally" |
| 49 | }; |
| 50 | const char* direct_candidates[] = { |
| 51 | "./bin/openavx", |
| 52 | "./bin/openavx.exe", |
| 53 | "bin/openavx.exe", |
| 54 | "./openavx", |
| 55 | "./openavx.exe", |
| 56 | "./build/bin/openavx", |
| 57 | "./build/bin/openavx.exe" |
| 58 | }; |
| 59 | |
| 60 | if (symlink) { |
| 61 | for (size_t i = 0; i < sizeof(symlink_candidates)/sizeof(symlink_candidates[0]); ++i) { |
| 62 | if (access(symlink_candidates[i], X_OK) == 0) { |
| 63 | snprintf(found, 256, "%s", symlink_candidates[i]); |
| 64 | return found; |
| 65 | } |
| 66 | } |
| 67 | } else { |
| 68 | for (size_t i = 0; i < sizeof(direct_candidates)/sizeof(direct_candidates[0]); ++i) { |
| 69 | if (access(direct_candidates[i], X_OK) == 0) { |
| 70 | snprintf(found, 256, "%s", direct_candidates[i]); |
| 71 | return found; |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | return symlink ? "./bin/openavx-tally" : "./bin/openavx"; |
| 76 | } |
| 77 | |
| 78 | int main(void) { |
| 79 | const char* bin_cli = find_tally_binary(false); |
| 80 | const char* bin_sym = find_tally_binary(true); |
| 81 | |
| 82 | printf("\n=======================================================\n"); |
| 83 | printf(" OpenAVX Broadcast Tally CLI & API Verification Test\n"); |
| 84 | printf("=======================================================\n\n"); |
| 85 | printf("Using CLI Binary: %s\n", bin_cli); |
| 86 | printf("Using Symlink: %s\n\n", bin_sym); |
| 87 | |
| 88 | /* 1. Test CLI Help Banner */ |
| 89 | printf("[*] 1. Testing CLI Help Banner (--help)...\n"); |
| 90 | char output[2048] = {0}; |
| 91 | char cmd[512]; |
| 92 | snprintf(cmd, sizeof(cmd), "%s tally --help", bin_cli); |
| 93 | int rc = run_cmd_capture(cmd, output, sizeof(output)); |
| 94 | assert(rc == 0); |
| 95 | assert(strstr(output, "Tally Light Monitor & Remote Controller") != NULL); |
| 96 | assert(strstr(output, "--set") != NULL); |
| 97 | assert(strstr(output, "--poll") != NULL); |
| 98 | printf(" [+] 'openavx tally --help' executed successfully with complete usage documentation.\n"); |
| 99 | |
| 100 | /* 2. Test Symlink Invocation */ |
| 101 | #ifndef _WIN32 |
| 102 | printf("[*] 2. Testing Multi-call Symlink 'openavx-tally'...\n"); |
| 103 | snprintf(cmd, sizeof(cmd), "%s --help", bin_sym); |
| 104 | rc = run_cmd_capture(cmd, output, sizeof(output)); |
| 105 | assert(rc == 0); |
| 106 | assert(strstr(output, "Tally Light Monitor") != NULL); |
| 107 | printf(" [+] Multi-call symlink 'openavx-tally --help' confirmed functional.\n"); |
| 108 | #else |
| 109 | printf("[*] 2. Skipping Multi-call Symlink on Windows (symlinks not supported)...\n"); |
| 110 | #endif |
| 111 | |
| 112 | /* 3. Test Missing Argument Handling */ |
| 113 | printf("[*] 3. Testing Error Handling on Missing Target...\n"); |
| 114 | snprintf(cmd, sizeof(cmd), "%s tally 2>&1", bin_cli); |
| 115 | rc = run_cmd_capture(cmd, output, sizeof(output)); |
| 116 | assert(rc != 0); |
| 117 | assert(strstr(output, "Error: Missing target") != NULL); |
| 118 | printf(" [+] Missing target argument rejected with non-zero exit code.\n"); |
| 119 | |
| 120 | /* 4. Start Test Stream Sender with Web Preview Server */ |
| 121 | printf("[*] 4. Launching Test Stream Sender with Web Server...\n"); |
| 122 | oavx_context_t *ctx = oavx_context_create(); |
| 123 | assert(ctx != NULL); |
| 124 | |
| 125 | oavx_stream_config_t send_cfg = { |
| 126 | .name = "TEST_TALLY_STREAM_99", |
| 127 | .direction = OAVX_DIR_TRANSMIT, |
| 128 | .color_format = OAVX_COLOR_FORMAT_RGBX_RGBA, |
| 129 | .clock_sync = false |
| 130 | }; |
| 131 | |
| 132 | oavx_stream_t *stream = oavx_stream_open(ctx, &send_cfg); |
| 133 | assert(stream != NULL); |
| 134 | |
| 135 | /* Enable embedded web server on ephemeral port (port 0) */ |
| 136 | oavx_status_t web_st = oavx_stream_enable_web_preview(stream, 0); |
| 137 | assert(web_st == OAVX_SUCCESS); |
| 138 | uint16_t web_port = oavx_stream_get_web_preview_port(stream); |
| 139 | assert(web_port > 0); |
| 140 | printf(" [+] Test stream sender initialized, Web server listening on port %u\n", web_port); |
| 141 | |
| 142 | /* 5. Test Polling Tally via HTTP REST (Initial State: OFF) */ |
| 143 | printf("[*] 5. Testing HTTP REST Tally Poll (Initial State)...\n"); |
| 144 | snprintf(cmd, sizeof(cmd), "%s tally http://127.0.0.1:%u --poll --text", bin_cli, web_port); |
| 145 | rc = run_cmd_capture(cmd, output, sizeof(output)); |
| 146 | assert(rc == 0); |
| 147 | assert(strstr(output, "OFF") != NULL); |
| 148 | |
| 149 | snprintf(cmd, sizeof(cmd), "%s tally http://127.0.0.1:%u --poll --json", bin_cli, web_port); |
| 150 | rc = run_cmd_capture(cmd, output, sizeof(output)); |
| 151 | assert(rc == 0); |
| 152 | assert(strstr(output, "\"program\":false") != NULL); |
| 153 | assert(strstr(output, "\"preview\":false") != NULL); |
| 154 | assert(strstr(output, "\"recording\":false") != NULL); |
| 155 | assert(strstr(output, "\"flags\":0") != NULL); |
| 156 | printf(" [+] Initial tally polled over HTTP confirmed OFF (flags=0).\n"); |
| 157 | |
| 158 | /* 6. Test Commanding Tally via HTTP REST: Program (Red) */ |
| 159 | printf("[*] 6. Testing HTTP REST Tally Control (--set pgm)...\n"); |
| 160 | snprintf(cmd, sizeof(cmd), "%s tally http://127.0.0.1:%u --set pgm --json", bin_cli, web_port); |
| 161 | rc = run_cmd_capture(cmd, output, sizeof(output)); |
| 162 | assert(rc == 0); |
| 163 | assert(strstr(output, "\"ok\":true") != NULL); |
| 164 | assert(strstr(output, "\"flags\":1") != NULL); |
| 165 | |
| 166 | snprintf(cmd, sizeof(cmd), "%s tally http://127.0.0.1:%u --poll --text", bin_cli, web_port); |
| 167 | rc = run_cmd_capture(cmd, output, sizeof(output)); |
| 168 | assert(rc == 0); |
| 169 | assert(strstr(output, "PROGRAM") != NULL); |
| 170 | printf(" [+] HTTP REST commanded Program tally successfully verified.\n"); |
| 171 | |
| 172 | /* 7. Test Commanding Tally via HTTP REST: Preview + Recording (Green + Amber) */ |
| 173 | printf("[*] 7. Testing HTTP REST Tally Control (--set 'pvw+rec')...\n"); |
| 174 | snprintf(cmd, sizeof(cmd), "%s tally http://127.0.0.1:%u --set \"pvw+rec\" --json", bin_cli, web_port); |
| 175 | rc = run_cmd_capture(cmd, output, sizeof(output)); |
| 176 | assert(rc == 0); |
| 177 | assert(strstr(output, "\"ok\":true") != NULL); |
| 178 | assert(strstr(output, "\"flags\":6") != NULL); |
| 179 | |
| 180 | snprintf(cmd, sizeof(cmd), "%s tally http://127.0.0.1:%u --poll --text", bin_cli, web_port); |
| 181 | rc = run_cmd_capture(cmd, output, sizeof(output)); |
| 182 | assert(rc == 0); |
| 183 | assert(strstr(output, "PREVIEW+RECORDING") != NULL); |
| 184 | printf(" [+] HTTP REST commanded Preview+Recording (flags=6) successfully verified.\n"); |
| 185 | |
| 186 | /* 8. Test Commanding Tally via HTTP REST: Clear / Off */ |
| 187 | printf("[*] 8. Testing HTTP REST Tally Control (--set off)...\n"); |
| 188 | snprintf(cmd, sizeof(cmd), "%s tally http://127.0.0.1:%u --set off --json", bin_cli, web_port); |
| 189 | rc = run_cmd_capture(cmd, output, sizeof(output)); |
| 190 | assert(rc == 0); |
| 191 | assert(strstr(output, "\"flags\":0") != NULL); |
| 192 | |
| 193 | snprintf(cmd, sizeof(cmd), "%s tally http://127.0.0.1:%u --poll --text", bin_cli, web_port); |
| 194 | rc = run_cmd_capture(cmd, output, sizeof(output)); |
| 195 | assert(rc == 0); |
| 196 | assert(strstr(output, "OFF") != NULL); |
| 197 | printf(" [+] HTTP REST cleared tally to OFF successfully.\n"); |
| 198 | |
| 199 | /* 9. Test Sender & Receiver Pipeline APIs */ |
| 200 | printf("[*] 9. Testing Pipeline Stream Tally API Consistency...\n"); |
| 201 | oavx_tally_t direct_tally = { .flags = OAVX_TALLY_PROGRAM | OAVX_TALLY_RECORDING }; |
| 202 | bool set_ok = oavx_stream_set_tally(stream, &direct_tally); |
| 203 | assert(set_ok); |
| 204 | |
| 205 | oavx_tally_t queried_tally = {0}; |
| 206 | bool get_ok = oavx_stream_get_tally(stream, &queried_tally, 500); |
| 207 | assert(get_ok); |
| 208 | assert(queried_tally.flags == (OAVX_TALLY_PROGRAM | OAVX_TALLY_RECORDING)); |
| 209 | printf(" [+] oavx_stream_set_tally and oavx_stream_get_tally bidirectional match verified.\n"); |
| 210 | |
| 211 | /* 10. Test Native Stream Tally Command (--set via endpoint) */ |
| 212 | printf("[*] 10. Testing Native OpenAVX Stream Tally Control...\n"); |
| 213 | const oavx_endpoint_t *ep = oavx_stream_get_endpoint(stream); |
| 214 | if (ep && ep->address) { |
| 215 | snprintf(cmd, sizeof(cmd), "%s tally %s --set pgm --oneshot --json", bin_cli, ep->address); |
| 216 | rc = run_cmd_capture(cmd, output, sizeof(output)); |
| 217 | assert(rc == 0); |
| 218 | assert(strstr(output, "\"ok\":true") != NULL); |
| 219 | assert(strstr(output, "\"flags\":1") != NULL); |
| 220 | printf(" [+] Native stream tally commanded over TCP metadata socket successfully.\n"); |
| 221 | } |
| 222 | |
| 223 | /* Clean up */ |
| 224 | oavx_stream_close(stream); |
| 225 | oavx_context_destroy(ctx); |
| 226 | |
| 227 | printf("\n[SUCCESS] All OpenAVX Tally CLI & API verification tests passed (100%%).\n\n"); |
| 228 | return 0; |
| 229 | } |