|
12 | 12 | * -----------------------------------------------------------------------------
|
13 | 13 | */
|
14 | 14 | #include <fossil/test/framework.h>
|
15 |
| - |
16 | 15 | #include "fossil/io/framework.h"
|
17 | 16 |
|
18 | 17 | // * * * * * * * * * * * * * * * * * * * * * * * *
|
19 |
| -// * Fossil Logic Test Utilites |
| 18 | +// * Fossil Logic Test Utilities |
20 | 19 | // * * * * * * * * * * * * * * * * * * * * * * * *
|
21 | 20 | // Setup steps for things like test fixtures and
|
22 | 21 | // mock objects are set here.
|
23 | 22 | // * * * * * * * * * * * * * * * * * * * * * * * *
|
24 | 23 |
|
25 |
| -// Define the test suite and add test cases |
26 | 24 | FOSSIL_TEST_SUITE(c_network_suite);
|
27 | 25 |
|
28 | 26 | // Setup function for the test suite
|
29 | 27 | FOSSIL_SETUP(c_network_suite) {
|
30 |
| - // Setup code here |
| 28 | + // Setup code (if needed) |
31 | 29 | }
|
32 | 30 |
|
33 | 31 | // Teardown function for the test suite
|
34 | 32 | FOSSIL_TEARDOWN(c_network_suite) {
|
35 |
| - // Teardown code here |
| 33 | + // Teardown code (if needed) |
36 | 34 | }
|
37 | 35 |
|
38 | 36 | // * * * * * * * * * * * * * * * * * * * * * * * *
|
39 | 37 | // * Fossil Logic Test Cases
|
40 | 38 | // * * * * * * * * * * * * * * * * * * * * * * * *
|
41 |
| -// The test cases below are provided as samples, inspired |
42 |
| -// by the Meson build system's approach of using test cases |
43 |
| -// as samples for library usage. |
44 |
| -// * * * * * * * * * * * * * * * * * * * * * * * * |
45 |
| - |
46 |
| -// Fossil Logic Test Cases |
47 | 39 |
|
48 |
| -// Helper: Open connection or skip |
49 |
| -static fossil_nstream_t *try_open_tcp(const char *addr, const char *port) { |
50 |
| - fossil_nstream_t *ns = fossil_nstream_open("tcp", addr, port, NULL); |
51 |
| - if (!ns) { |
52 |
| - FOSSIL_TEST_SKIP("Unable to open TCP connection to %s:%s", addr, port); |
53 |
| - } |
54 |
| - return ns; |
55 |
| -} |
| 40 | +FOSSIL_TEST_CASE(c_test_nstream_create_and_destroy) { |
| 41 | + const char *protocols[] = {"tcp", "udp", "raw", "icmp", "sctp", "http", "https", "ftp", "ssh", "dns", "ntp", "smtp", "pop3", "imap", "ldap", "mqtt"}; |
| 42 | + const char *clients[] = {"mail-server", "server", "mail-client", "client", "mail-bot", "bot", "multicast", "broadcast"}; |
56 | 43 |
|
57 |
| -static fossil_nstream_t *try_open_tls(const char *addr, const char *port) { |
58 |
| - fossil_nstream_t *ns = fossil_nstream_open("tls", addr, port, NULL); |
59 |
| - if (!ns) { |
60 |
| - FOSSIL_TEST_SKIP("Unable to open TLS connection to %s:%s", addr, port); |
| 44 | + for (size_t i = 0; i < sizeof(protocols) / sizeof(protocols[0]); i++) { |
| 45 | + for (size_t j = 0; j < sizeof(clients) / sizeof(clients[0]); j++) { |
| 46 | + fossil_nstream_t *stream = fossil_nstream_create(protocols[i], clients[j]); |
| 47 | + ASSUME_NOT_CNULL(stream); |
| 48 | + fossil_nstream_destroy(stream); |
| 49 | + } |
61 | 50 | }
|
62 |
| - return ns; |
63 |
| -} |
64 |
| - |
65 |
| -FOSSIL_TEST_CASE(c_test_nstream_open) { |
66 |
| - fossil_nstream_t *ns = try_open_tcp("127.0.0.1", "8080"); |
67 |
| - fossil_nstream_close(ns); |
68 | 51 | }
|
69 | 52 |
|
70 |
| -FOSSIL_TEST_CASE(c_test_nstream_send_recv) { |
71 |
| - fossil_nstream_t *ns = try_open_tcp("127.0.0.1", "8080"); |
72 |
| - |
73 |
| - const char *message = "Hello, Fossil!"; |
74 |
| - ssize_t bytes_sent = fossil_nstream_send(ns, message, strlen(message)); |
75 |
| - ASSUME_ITS_TRUE(bytes_sent >= 0); |
76 |
| - |
77 |
| - char buffer[128]; |
78 |
| - ssize_t bytes_received = fossil_nstream_recv(ns, buffer, sizeof(buffer)); |
79 |
| - ASSUME_ITS_TRUE(bytes_received >= 0); |
| 53 | +FOSSIL_TEST_CASE(c_test_nstream_connect_invalid_host) { |
| 54 | + fossil_nstream_t *stream = fossil_nstream_create("tcp", "client"); |
| 55 | + ASSUME_NOT_CNULL(stream); |
80 | 56 |
|
81 |
| - fossil_nstream_close(ns); |
| 57 | + // Attempt to connect to an invalid host |
| 58 | + ASSUME_ITS_EQUAL_I32(-1, fossil_nstream_connect(stream, "invalid_host", 12345)); |
| 59 | + fossil_nstream_destroy(stream); |
82 | 60 | }
|
83 | 61 |
|
84 |
| -FOSSIL_TEST_CASE(c_test_nstream_listen_accept) { |
85 |
| - fossil_nstream_t *server = try_open_tcp("127.0.0.1", "8080"); |
86 |
| - |
87 |
| - int result = fossil_nstream_listen(server, 5); |
88 |
| - ASSUME_ITS_EQUAL_I32(0, result); |
| 62 | +FOSSIL_TEST_CASE(c_test_nstream_listen_and_accept) { |
| 63 | + fossil_nstream_t *server = fossil_nstream_create("tcp", "server"); |
| 64 | + ASSUME_NOT_CNULL(server); |
89 | 65 |
|
90 |
| - // We don't expect actual client in this minimal test |
91 |
| - fossil_nstream_close(server); |
92 |
| -} |
| 66 | + // Start listening on a local port |
| 67 | + ASSUME_ITS_EQUAL_I32(0, fossil_nstream_listen(server, "127.0.0.1", 12345)); |
93 | 68 |
|
94 |
| -FOSSIL_TEST_CASE(c_test_nstream_set_nonblocking) { |
95 |
| - fossil_nstream_t *ns = try_open_tcp("127.0.0.1", "8080"); |
| 69 | + // Simulate a client connecting |
| 70 | + fossil_nstream_t *client = fossil_nstream_create("tcp", "client"); |
| 71 | + ASSUME_NOT_CNULL(client); |
| 72 | + ASSUME_ITS_EQUAL_I32(0, fossil_nstream_connect(client, "127.0.0.1", 12345)); |
96 | 73 |
|
97 |
| - int result = fossil_nstream_set_nonblocking(ns, 1); |
98 |
| - ASSUME_ITS_EQUAL_I32(0, result); |
| 74 | + // Accept the client connection |
| 75 | + fossil_nstream_t *accepted_client = fossil_nstream_accept(server); |
| 76 | + ASSUME_NOT_CNULL(accepted_client); |
99 | 77 |
|
100 |
| - fossil_nstream_close(ns); |
| 78 | + // Cleanup |
| 79 | + fossil_nstream_destroy(client); |
| 80 | + fossil_nstream_destroy(accepted_client); |
| 81 | + fossil_nstream_destroy(server); |
101 | 82 | }
|
102 | 83 |
|
103 |
| -FOSSIL_TEST_CASE(c_test_nstream_wait_readable_writable) { |
104 |
| - fossil_nstream_t *ns = try_open_tcp("127.0.0.1", "8080"); |
| 84 | +FOSSIL_TEST_CASE(c_test_nstream_send_and_receive) { |
| 85 | + fossil_nstream_t *server = fossil_nstream_create("tcp", "server"); |
| 86 | + ASSUME_NOT_CNULL(server); |
105 | 87 |
|
106 |
| - int readable = fossil_nstream_wait_readable(ns, 1000); |
107 |
| - int writable = fossil_nstream_wait_writable(ns, 1000); |
| 88 | + // Start listening on a local port |
| 89 | + ASSUME_ITS_EQUAL_I32(0, fossil_nstream_listen(server, "127.0.0.1", 12345)); |
108 | 90 |
|
109 |
| - ASSUME_ITS_TRUE(readable >= 0); |
110 |
| - ASSUME_ITS_TRUE(writable >= 0); |
| 91 | + // Simulate a client connecting |
| 92 | + fossil_nstream_t *client = fossil_nstream_create("tcp", "client"); |
| 93 | + ASSUME_NOT_CNULL(client); |
| 94 | + ASSUME_ITS_EQUAL_I32(0, fossil_nstream_connect(client, "127.0.0.1", 12345)); |
111 | 95 |
|
112 |
| - fossil_nstream_close(ns); |
113 |
| -} |
114 |
| - |
115 |
| -FOSSIL_TEST_CASE(c_test_nstream_connect_timeout) { |
116 |
| - fossil_nstream_t *ns = fossil_nstream_open("tcp", NULL, NULL, NULL); |
117 |
| - ASSUME_NOT_CNULL(ns); |
118 |
| - |
119 |
| - int result = fossil_nstream_connect_timeout(ns, "127.0.0.1", "8080", 1000); |
120 |
| - ASSUME_ITS_TRUE(result == 0 || result == -1); // Allow failure if no server. |
| 96 | + // Accept the client connection |
| 97 | + fossil_nstream_t *accepted_client = fossil_nstream_accept(server); |
| 98 | + ASSUME_NOT_CNULL(accepted_client); |
121 | 99 |
|
122 |
| - fossil_nstream_close(ns); |
123 |
| -} |
124 |
| - |
125 |
| -FOSSIL_TEST_CASE(c_test_nstream_get_peer_info) { |
126 |
| - fossil_nstream_t *ns = try_open_tcp("127.0.0.1", "8080"); |
| 100 | + // Send and receive data |
| 101 | + const char *message = "Hello, Fossil!"; |
| 102 | + ASSUME_ITS_EQUAL_I32(strlen(message), fossil_nstream_send(client, message, strlen(message))); |
127 | 103 |
|
128 |
| - char ip_str[64]; |
129 |
| - uint16_t port; |
130 |
| - int result = fossil_nstream_get_peer_info(ns, ip_str, sizeof(ip_str), &port); |
131 |
| - ASSUME_ITS_EQUAL_I32(0, result); |
| 104 | + char buffer[1024] = {0}; |
| 105 | + ASSUME_ITS_EQUAL_I32(strlen(message), fossil_nstream_recv(accepted_client, buffer, sizeof(buffer))); |
| 106 | + ASSUME_ITS_EQUAL_CSTR(message, buffer); |
132 | 107 |
|
133 |
| - fossil_nstream_close(ns); |
| 108 | + // Cleanup |
| 109 | + fossil_nstream_destroy(client); |
| 110 | + fossil_nstream_destroy(accepted_client); |
| 111 | + fossil_nstream_destroy(server); |
134 | 112 | }
|
135 | 113 |
|
136 |
| -FOSSIL_TEST_CASE(c_test_nstream_send_recv_line) { |
137 |
| - fossil_nstream_t *ns = try_open_tcp("127.0.0.1", "8080"); |
| 114 | +FOSSIL_TEST_CASE(c_test_nstream_protocols) { |
| 115 | + const char *protocols[] = {"tcp", "udp", "raw", "icmp", "sctp", "http", "https", "ftp", "ssh", "dns", "ntp", "smtp", "pop3", "imap", "ldap", "mqtt"}; |
138 | 116 |
|
139 |
| - const char *line = "Hello, Fossil Logic!"; |
140 |
| - ssize_t bytes_sent = fossil_nstream_send_line(ns, line); |
141 |
| - ASSUME_ITS_TRUE(bytes_sent >= 0); |
142 |
| - |
143 |
| - char buffer[128]; |
144 |
| - ssize_t bytes_received = fossil_nstream_recv_line(ns, buffer, sizeof(buffer)); |
145 |
| - ASSUME_ITS_TRUE(bytes_received >= 0); |
146 |
| - |
147 |
| - fossil_nstream_close(ns); |
| 117 | + for (size_t i = 0; i < sizeof(protocols) / sizeof(protocols[0]); i++) { |
| 118 | + fossil_nstream_t *stream = fossil_nstream_create(protocols[i], "client"); |
| 119 | + ASSUME_NOT_CNULL(stream); |
| 120 | + fossil_nstream_destroy(stream); |
| 121 | + } |
148 | 122 | }
|
149 | 123 |
|
150 |
| -FOSSIL_TEST_CASE(c_test_nstream_ssl_send_recv) { |
151 |
| - fossil_nstream_t *ns = try_open_tls("127.0.0.1", "8080"); |
| 124 | +FOSSIL_TEST_CASE(c_test_nstream_client_types) { |
| 125 | + const char *clients[] = {"mail-server", "server", "mail-client", "client", "mail-bot", "bot", "multicast", "broadcast"}; |
152 | 126 |
|
153 |
| - const char *message = "Secure Hello!"; |
154 |
| - ssize_t bytes_sent = fossil_nstream_ssl_send(ns, message, strlen(message)); |
155 |
| - ASSUME_ITS_TRUE(bytes_sent >= 0); |
156 |
| - |
157 |
| - char buffer[128]; |
158 |
| - ssize_t bytes_received = fossil_nstream_ssl_recv(ns, buffer, sizeof(buffer)); |
159 |
| - ASSUME_ITS_TRUE(bytes_received >= 0); |
160 |
| - |
161 |
| - fossil_nstream_close(ns); |
| 127 | + for (size_t i = 0; i < sizeof(clients) / sizeof(clients[0]); i++) { |
| 128 | + fossil_nstream_t *stream = fossil_nstream_create("tcp", clients[i]); |
| 129 | + ASSUME_NOT_CNULL(stream); |
| 130 | + fossil_nstream_destroy(stream); |
| 131 | + } |
162 | 132 | }
|
163 | 133 |
|
164 | 134 | // * * * * * * * * * * * * * * * * * * * * * * * *
|
165 | 135 | // * Fossil Logic Test Pool
|
166 | 136 | // * * * * * * * * * * * * * * * * * * * * * * * *
|
167 | 137 |
|
168 | 138 | FOSSIL_TEST_GROUP(c_network_tests) {
|
169 |
| - FOSSIL_TEST_ADD(c_network_tests, c_test_nstream_open); |
170 |
| - FOSSIL_TEST_ADD(c_network_tests, c_test_nstream_send_recv); |
171 |
| - FOSSIL_TEST_ADD(c_network_tests, c_test_nstream_listen_accept); |
172 |
| - FOSSIL_TEST_ADD(c_network_tests, c_test_nstream_set_nonblocking); |
173 |
| - FOSSIL_TEST_ADD(c_network_tests, c_test_nstream_wait_readable_writable); |
174 |
| - FOSSIL_TEST_ADD(c_network_tests, c_test_nstream_connect_timeout); |
175 |
| - FOSSIL_TEST_ADD(c_network_tests, c_test_nstream_get_peer_info); |
176 |
| - FOSSIL_TEST_ADD(c_network_tests, c_test_nstream_send_recv_line); |
177 |
| - FOSSIL_TEST_ADD(c_network_tests, c_test_nstream_ssl_send_recv); |
178 |
| - |
179 |
| - FOSSIL_TEST_REGISTER(c_network_tests); |
| 139 | + FOSSIL_TEST_ADD(c_network_suite, c_test_nstream_create_and_destroy); |
| 140 | + FOSSIL_TEST_ADD(c_network_suite, c_test_nstream_connect_invalid_host); |
| 141 | + FOSSIL_TEST_ADD(c_network_suite, c_test_nstream_listen_and_accept); |
| 142 | + FOSSIL_TEST_ADD(c_network_suite, c_test_nstream_send_and_receive); |
| 143 | + FOSSIL_TEST_ADD(c_network_suite, c_test_nstream_protocols); |
| 144 | + FOSSIL_TEST_ADD(c_network_suite, c_test_nstream_client_types); |
| 145 | + |
| 146 | + FOSSIL_TEST_REGISTER(c_network_suite); |
180 | 147 | }
|
0 commit comments