Raw ↗
+ +
d9fbf5b fix(ci): resolve windows dshow com exports, qpc timer overflow, and temp path
saifmukhtar · 2026-11-25
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 Library
6 * openavx_config_test.c - OpenAVX JSON Configuration File (openavx.json) Verification
7 */
8  
9 #include "openavx.h"
10 #include "avx_config.h"
11 #include "avx_internal.h"
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <assert.h>
16 #include <unistd.h>
17 #include <sys/stat.h>
18  
19 static void test_json_parser_rfc_standards(void) {
20 printf("[*] 1. Testing Modern RFC OpenAVX JSON Schema (v1.0)...\n");
21  
22 const char* rfc_json =
23 "{\n"
24 " \"$schema\": \"https://raw.githubusercontent.com/saifmukhtar/openavx/main/schema/v1.0.json\",\n"
25 " \"version\": \"1.0\",\n"
26 " \"discovery\": {\n"
27 " \"servers\": [\"192.168.1.100:7959\", \"10.0.0.1:7959\"],\n"
28 " \"static_peers\": [\"10.0.0.2\", \"10.0.0.3\"]\n"
29 " },\n"
30 " \"channels\": {\n"
31 " \"publish\": [\"Public\", \"StudioA\"],\n"
32 " \"subscribe\": [\"Public\", \"StudioA\", \"Monitoring\"]\n"
33 " },\n"
34 " \"multicast\": {\n"
35 " \"enabled\": true,\n"
36 " \"ttl\": 2,\n"
37 " \"prefix\": \"239.255.0.0/16\"\n"
38 " }\n"
39 "}";
40  
41 avx_config_t cfg;
42 bool ok = avx_config_parse_json(rfc_json, &cfg);
43 assert(ok);
44 assert(strcmp(cfg.discovery_servers, "192.168.1.100:7959,10.0.0.1:7959") == 0);
45 assert(strcmp(cfg.static_peers, "10.0.0.2,10.0.0.3") == 0);
46 assert(strcmp(cfg.channels_publish, "Public,StudioA") == 0);
47 assert(strcmp(cfg.channels_subscribe, "Public,StudioA,Monitoring") == 0);
48 assert(cfg.multicast_enabled == true);
49 assert(cfg.multicast_ttl == 2);
50 assert(strcmp(cfg.multicast_prefix, "239.255.0.0/16") == 0);
51  
52 printf(" [+] RFC v1.0 JSON schema parsed and validated\n");
53 }
54  
55 static void test_json_parser_arrays(void) {
56 printf("[*] 2. Testing Array-Style JSON Configurations...\n");
57  
58 const char* array_json =
59 "{\n"
60 " \"discovery\": {\n"
61 " \"servers\": [\"192.168.1.50\", \"10.20.30.40:7959\"],\n"
62 " \"static_peers\": [\"172.16.0.10\", \"172.16.0.11\"]\n"
63 " },\n"
64 " \"channels\": {\n"
65 " \"publish\": [\"StudioB\", \"Live\"],\n"
66 " \"subscribe\": [\"StudioB\", \"ControlRoom\", \"Engineering\"]\n"
67 " }\n"
68 "}";
69  
70 avx_config_t cfg;
71 bool ok = avx_config_parse_json(array_json, &cfg);
72 assert(ok);
73 assert(strcmp(cfg.discovery_servers, "192.168.1.50,10.20.30.40:7959") == 0);
74 assert(strcmp(cfg.static_peers, "172.16.0.10,172.16.0.11") == 0);
75 assert(strcmp(cfg.channels_publish, "StudioB,Live") == 0);
76 assert(strcmp(cfg.channels_subscribe, "StudioB,ControlRoom,Engineering") == 0);
77  
78 printf(" [+] Array-based configuration successfully normalized!\n");
79 }
80  
81 static void test_json_parser_malformed(void) {
82 printf("[*] 3. Testing Malformed JSON Resilience...\n");
83  
84 avx_config_t cfg;
85  
86 /* Truncated JSON */
87 bool ok = avx_config_parse_json("{\"openavx\": {\"networks\": {\"disco", &cfg);
88 assert(ok);
89  
90 /* Empty string */
91 ok = avx_config_parse_json("", &cfg);
92 assert(ok);
93  
94 /* Unclosed quotes */
95 ok = avx_config_parse_json("{\"groups\": {\"send\": \"broken", &cfg);
96 assert(ok);
97  
98 /* Non-JSON random garbage */
99 ok = avx_config_parse_json("NOT_JSON_DATA_AT_ALL_$$$%%%", &cfg);
100 assert(ok);
101  
102 printf(" [+] Parser survived malformed inputs without faults!\n");
103 }
104  
105 static void test_config_resolution_and_precedence(void) {
106 printf("[*] 4. Testing Config Resolution & Precedence Hierarchy...\n");
107  
108 /* Create temporary test config directory */
109 #ifdef _WIN32
110 char tmp_path[MAX_PATH];
111 DWORD plen = GetTempPathA(sizeof(tmp_path), tmp_path);
112 assert(plen > 0 && plen < sizeof(tmp_path));
113 char tmp_dir[MAX_PATH + 64];
114 snprintf(tmp_dir, sizeof(tmp_dir), "%sopenavx_cfg_test_XXXXXX", tmp_path);
115 #else
116 char tmp_dir[] = "/tmp/openavx_cfg_test_XXXXXX";
117 #endif
118 char* dname = mkdtemp(tmp_dir);
119 assert(dname != NULL);
120  
121 char cfg_file[512];
122 snprintf(cfg_file, sizeof(cfg_file), "%s/openavx.json", dname);
123  
124 FILE* f = fopen(cfg_file, "w");
125 assert(f != NULL);
126 fprintf(f,
127 "{\n"
128 " \"$schema\": \"https://raw.githubusercontent.com/saifmukhtar/openavx/main/schema/v1.0.json\",\n"
129 " \"version\": \"1.0\",\n"
130 " \"discovery\": {\n"
131 " \"servers\": \"127.0.0.1:7959\",\n"
132 " \"static_peers\": \"10.99.88.77\"\n"
133 " },\n"
134 " \"channels\": {\n"
135 " \"publish\": \"JsonSendGroup\",\n"
136 " \"subscribe\": \"JsonRecvGroup\"\n"
137 " }\n"
138 "}\n");
139 fclose(f);
140  
141 /* Set OPENAVX_CONFIG_DIR to our test directory */
142 setenv("OPENAVX_CONFIG_DIR", dname, 1);
143 unsetenv("OPENAVX_DISCOVERY_SERVER");
144 unsetenv("OPENAVX_GROUPS");
145  
146 /* Reset and initialize library */
147 avx_config_cleanup();
148 oavx_context_t *ctx = oavx_context_create();
149 assert(ctx != NULL);
150  
151 const avx_config_t* cfg = avx_config_get();
152 assert(cfg != NULL);
153 assert(cfg->loaded == true);
154 assert(strcmp(cfg->channels_publish, "JsonSendGroup") == 0);
155 assert(strcmp(cfg->channels_subscribe, "JsonRecvGroup") == 0);
156 assert(strcmp(cfg->discovery_servers, "127.0.0.1:7959") == 0);
157 assert(strcmp(cfg->static_peers, "10.99.88.77") == 0);
158 printf(" [+] Config correctly loaded from $OPENAVX_CONFIG_DIR (%s)\n", cfg->config_path);
159  
160 /* Test Case A: Sender default group from JSON */
161 oavx_stream_config_t send_settings_default = {
162 .name = "Test Sender A",
163 .groups = NULL,
164 .direction = OAVX_DIR_TRANSMIT,
165 .clock_sync = true
166 };
167 oavx_stream_t *p_send_a = oavx_stream_open(ctx, &send_settings_default);
168 assert(p_send_a != NULL);
169 const oavx_endpoint_t *ep_a = oavx_stream_get_endpoint(p_send_a);
170 assert(ep_a != NULL && ep_a->group != NULL);
171 assert(strcmp(ep_a->group, "JsonSendGroup") == 0);
172 printf(" [+] Sender inherited groups from JSON: '%s'\n", ep_a->group);
173 oavx_stream_close(p_send_a);
174  
175 /* Test Case B: Sender explicit API group overrides JSON */
176 oavx_stream_config_t send_settings_explicit = {
177 .name = "Test Sender B",
178 .groups = "ExplicitApiGroup",
179 .direction = OAVX_DIR_TRANSMIT,
180 .clock_sync = true
181 };
182 oavx_stream_t *p_send_b = oavx_stream_open(ctx, &send_settings_explicit);
183 assert(p_send_b != NULL);
184 const oavx_endpoint_t *ep_b = oavx_stream_get_endpoint(p_send_b);
185 assert(ep_b != NULL && ep_b->group != NULL);
186 assert(strcmp(ep_b->group, "ExplicitApiGroup") == 0);
187 printf(" [+] Explicit API argument overrode JSON: '%s'\n", ep_b->group);
188 oavx_stream_close(p_send_b);
189  
190 /* Test Case C: Environment variable overrides JSON */
191 setenv("OPENAVX_GROUPS", "EnvOverrideGroup", 1);
192 oavx_stream_t *p_send_c = oavx_stream_open(ctx, &send_settings_default);
193 assert(p_send_c != NULL);
194 const oavx_endpoint_t *ep_c = oavx_stream_get_endpoint(p_send_c);
195 assert(ep_c != NULL && ep_c->group != NULL);
196 assert(strcmp(ep_c->group, "EnvOverrideGroup") == 0);
197 printf(" [+] Environment variable OPENAVX_GROUPS overrode JSON: '%s'\n", ep_c->group);
198 oavx_stream_close(p_send_c);
199  
200 /* Test Case D: Finder default groups and extra_ips from JSON */
201 unsetenv("OPENAVX_GROUPS");
202 oavx_discovery_t *p_find = oavx_discovery_create(ctx, NULL);
203 assert(p_find != NULL);
204 printf(" [+] Finder inherited receive groups and extra_ips from JSON!\n");
205 oavx_discovery_destroy(p_find);
206  
207 /* Teardown */
208 oavx_context_destroy(ctx);
209 unlink(cfg_file);
210 rmdir(dname);
211 unsetenv("OPENAVX_CONFIG_DIR");
212 unsetenv("OPENAVX_GROUPS");
213 }
214  
215 static void test_json_schema_v1_layout(void) {
216 printf("[*] 5. Testing Full schema/v1.0.json Layout (network/sources/transports)...\n");
217  
218 const char* schema_json =
219 "{\n"
220 " \"$schema\": \"https://raw.githubusercontent.com/saifmukhtar/openavx/main/schema/v1.0.json\",\n"
221 " \"version\": \"1.0\",\n"
222 " \"network\": {\n"
223 " \"bind_ip\": \"10.10.0.5\",\n"
224 " \"interface\": \"eth1\",\n"
225 " \"discovery_servers\": [\"10.10.0.10:7959\"],\n"
226 " \"peers\": [\"10.10.0.20\"]\n"
227 " },\n"
228 " \"sources\": {\n"
229 " \"groups\": [\"StudioA\"],\n"
230 " \"subscribe\": [\"Control\"],\n"
231 " \"max_clients\": 64\n"
232 " },\n"
233 " \"transports\": {\n"
234 " \"mode\": \"rudp\",\n"
235 " \"multicast\": {\n"
236 " \"enabled\": true,\n"
237 " \"ttl\": 4,\n"
238 " \"prefix\": \"239.255.0.0/16\",\n"
239 " \"netmask\": \"255.255.0.0\"\n"
240 " }\n"
241 " }\n"
242 "}";
243  
244 avx_config_t cfg;
245 bool ok = avx_config_parse_json(schema_json, &cfg);
246 assert(ok);
247 assert(strcmp(cfg.bind_ip, "10.10.0.5") == 0);
248 assert(strcmp(cfg.nic_interface, "eth1") == 0);
249 assert(strcmp(cfg.discovery_servers, "10.10.0.10:7959") == 0);
250 assert(strcmp(cfg.static_peers, "10.10.0.20") == 0);
251 assert(strcmp(cfg.channels_publish, "StudioA") == 0);
252 assert(strcmp(cfg.channels_subscribe, "Control") == 0);
253 assert(cfg.max_clients == 64);
254 assert(strcmp(cfg.transport, "rudp") == 0);
255 assert(cfg.multicast_enabled == true);
256 assert(cfg.multicast_ttl == 4);
257 assert(strcmp(cfg.multicast_prefix, "239.255.0.0/16") == 0);
258 assert(strcmp(cfg.multicast_netmask, "255.255.0.0") == 0);
259  
260 printf(" [+] Full schema/v1.0.json layout parsed with sources.max_clients = %d\n", cfg.max_clients);
261 }
262  
263 int main(void) {
264 printf("=== OpenAVX JSON Configuration Tests ===\n\n");
265  
266 test_json_parser_rfc_standards();
267 test_json_parser_arrays();
268 test_json_parser_malformed();
269 test_config_resolution_and_precedence();
270 test_json_schema_v1_layout();
271  
272 printf("\n[PASS] openavx_config_test\n");
273 return 0;
274 }