Skip to content

Commit 0f3eed2

Browse files
update test for new nstream
1 parent 31b327a commit 0f3eed2

File tree

2 files changed

+154
-293
lines changed

2 files changed

+154
-293
lines changed

code/tests/cases/test_network.c

Lines changed: 78 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -12,169 +12,136 @@
1212
* -----------------------------------------------------------------------------
1313
*/
1414
#include <fossil/test/framework.h>
15-
1615
#include "fossil/io/framework.h"
1716

1817
// * * * * * * * * * * * * * * * * * * * * * * * *
19-
// * Fossil Logic Test Utilites
18+
// * Fossil Logic Test Utilities
2019
// * * * * * * * * * * * * * * * * * * * * * * * *
2120
// Setup steps for things like test fixtures and
2221
// mock objects are set here.
2322
// * * * * * * * * * * * * * * * * * * * * * * * *
2423

25-
// Define the test suite and add test cases
2624
FOSSIL_TEST_SUITE(c_network_suite);
2725

2826
// Setup function for the test suite
2927
FOSSIL_SETUP(c_network_suite) {
30-
// Setup code here
28+
// Setup code (if needed)
3129
}
3230

3331
// Teardown function for the test suite
3432
FOSSIL_TEARDOWN(c_network_suite) {
35-
// Teardown code here
33+
// Teardown code (if needed)
3634
}
3735

3836
// * * * * * * * * * * * * * * * * * * * * * * * *
3937
// * Fossil Logic Test Cases
4038
// * * * * * * * * * * * * * * * * * * * * * * * *
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
4739

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"};
5643

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+
}
6150
}
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);
6851
}
6952

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);
8056

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);
8260
}
8361

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);
8965

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));
9368

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));
9673

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);
9977

100-
fossil_nstream_close(ns);
78+
// Cleanup
79+
fossil_nstream_destroy(client);
80+
fossil_nstream_destroy(accepted_client);
81+
fossil_nstream_destroy(server);
10182
}
10283

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);
10587

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));
10890

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));
11195

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);
12199

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)));
127103

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);
132107

133-
fossil_nstream_close(ns);
108+
// Cleanup
109+
fossil_nstream_destroy(client);
110+
fossil_nstream_destroy(accepted_client);
111+
fossil_nstream_destroy(server);
134112
}
135113

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"};
138116

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+
}
148122
}
149123

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"};
152126

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+
}
162132
}
163133

164134
// * * * * * * * * * * * * * * * * * * * * * * * *
165135
// * Fossil Logic Test Pool
166136
// * * * * * * * * * * * * * * * * * * * * * * * *
167137

168138
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);
180147
}

0 commit comments

Comments
 (0)