Skip to content

Commit 18e396f

Browse files
update test cases
1 parent 9f12961 commit 18e396f

File tree

2 files changed

+264
-184
lines changed

2 files changed

+264
-184
lines changed

code/tests/cases/test_network.c

Lines changed: 99 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -43,92 +43,130 @@ FOSSIL_TEARDOWN(c_network_suite) {
4343
// as samples for library usage.
4444
// * * * * * * * * * * * * * * * * * * * * * * * *
4545

46-
FOSSIL_TEST_CASE(c_test_network_init) {
47-
int result = fossil_io_network_create();
48-
ASSUME_ITS_EQUAL_I32(0, result);
49-
fossil_io_network_destroy();
46+
FOSSIL_TEST_CASE(c_test_nstream_open) {
47+
fossil_nstream_t *ns = fossil_nstream_open("tcp", "127.0.0.1", "8080", NULL);
48+
ASSUME_NOT_CNULL(ns);
49+
fossil_nstream_close(ns);
5050
}
5151

52-
FOSSIL_TEST_CASE(c_test_network_create_socket) {
53-
fossil_io_network_create();
54-
fossil_io_socket_t sock = fossil_io_network_create_socket(FOSSIL_IO_SOCKET_TYPE_TCP);
55-
ASSUME_NOT_EQUAL_I32(FOSSIL_IO_INVALID_SOCKET, sock);
56-
fossil_io_network_close(sock);
57-
fossil_io_network_destroy();
52+
FOSSIL_TEST_CASE(c_test_nstream_send_recv) {
53+
fossil_nstream_t *ns = fossil_nstream_open("tcp", "127.0.0.1", "8080", NULL);
54+
ASSUME_NOT_CNULL(ns);
55+
56+
const char *message = "Hello, Fossil!";
57+
ssize_t bytes_sent = fossil_nstream_send(ns, message, strlen(message));
58+
ASSUME_ITS_EQUAL_I32((int)strlen(message), bytes_sent);
59+
60+
char buffer[128];
61+
ssize_t bytes_received = fossil_nstream_recv(ns, buffer, sizeof(buffer));
62+
ASSUME_ITS_TRUE(bytes_received > 0);
63+
64+
fossil_nstream_close(ns);
5865
}
5966

60-
FOSSIL_TEST_CASE(c_test_network_bind) {
61-
fossil_io_network_create();
62-
fossil_io_socket_t sock = fossil_io_network_create_socket(FOSSIL_IO_SOCKET_TYPE_TCP);
63-
ASSUME_NOT_EQUAL_I32(FOSSIL_IO_INVALID_SOCKET, sock);
64-
int result = fossil_io_network_bind(sock, "127.0.0.1", 8080);
67+
FOSSIL_TEST_CASE(c_test_nstream_listen_accept) {
68+
fossil_nstream_t *server = fossil_nstream_open("tcp", "127.0.0.1", "8080", NULL);
69+
ASSUME_NOT_CNULL(server);
70+
71+
int result = fossil_nstream_listen(server, 5);
6572
ASSUME_ITS_EQUAL_I32(0, result);
66-
fossil_io_network_close(sock);
67-
fossil_io_network_destroy();
73+
74+
fossil_nstream_t *client = fossil_nstream_accept(server);
75+
ASSUME_NOT_CNULL(client);
76+
77+
fossil_nstream_close(client);
78+
fossil_nstream_close(server);
6879
}
6980

70-
FOSSIL_TEST_CASE(c_test_network_listen) {
71-
fossil_io_network_create();
72-
fossil_io_socket_t sock = fossil_io_network_create_socket(FOSSIL_IO_SOCKET_TYPE_TCP);
73-
ASSUME_NOT_EQUAL_I32(FOSSIL_IO_INVALID_SOCKET, sock);
74-
fossil_io_network_bind(sock, "127.0.0.1", 8080);
75-
int result = fossil_io_network_listen(sock, 5);
81+
FOSSIL_TEST_CASE(c_test_nstream_set_nonblocking) {
82+
fossil_nstream_t *ns = fossil_nstream_open("tcp", "127.0.0.1", "8080", NULL);
83+
ASSUME_NOT_CNULL(ns);
84+
85+
int result = fossil_nstream_set_nonblocking(ns, 1);
7686
ASSUME_ITS_EQUAL_I32(0, result);
77-
fossil_io_network_close(sock);
78-
fossil_io_network_destroy();
87+
88+
fossil_nstream_close(ns);
7989
}
8090

81-
FOSSIL_TEST_CASE(c_test_network_close) {
82-
fossil_io_network_create();
83-
fossil_io_socket_t sock = fossil_io_network_create_socket(FOSSIL_IO_SOCKET_TYPE_TCP);
84-
ASSUME_NOT_EQUAL_I32(FOSSIL_IO_INVALID_SOCKET, sock);
85-
fossil_io_network_close(sock);
86-
// No direct way to test close, but we assume no errors if it reaches here
87-
fossil_io_network_destroy();
91+
FOSSIL_TEST_CASE(c_test_nstream_wait_readable_writable) {
92+
fossil_nstream_t *ns = fossil_nstream_open("tcp", "127.0.0.1", "8080", NULL);
93+
ASSUME_NOT_CNULL(ns);
94+
95+
int result = fossil_nstream_wait_readable(ns, 1000);
96+
ASSUME_ITS_TRUE(result == 0 || result == 1);
97+
98+
result = fossil_nstream_wait_writable(ns, 1000);
99+
ASSUME_ITS_TRUE(result == 0 || result == 1);
100+
101+
fossil_nstream_close(ns);
88102
}
89103

90-
FOSSIL_TEST_CASE(c_test_network_create_udp_socket) {
91-
fossil_io_network_create();
92-
fossil_io_socket_t sock = fossil_io_network_create_socket(FOSSIL_IO_SOCKET_TYPE_UDP);
93-
ASSUME_NOT_EQUAL_I32(FOSSIL_IO_INVALID_SOCKET, sock);
94-
fossil_io_network_close(sock);
95-
fossil_io_network_destroy();
104+
FOSSIL_TEST_CASE(c_test_nstream_connect_timeout) {
105+
fossil_nstream_t *ns = fossil_nstream_open("tcp", NULL, NULL, NULL);
106+
ASSUME_NOT_CNULL(ns);
107+
108+
int result = fossil_nstream_connect_timeout(ns, "127.0.0.1", "8080", 1000);
109+
ASSUME_ITS_EQUAL_I32(0, result);
110+
111+
fossil_nstream_close(ns);
96112
}
97113

98-
FOSSIL_TEST_CASE(c_test_network_bind_udp) {
99-
fossil_io_network_create();
100-
fossil_io_socket_t sock = fossil_io_network_create_socket(FOSSIL_IO_SOCKET_TYPE_UDP);
101-
ASSUME_NOT_EQUAL_I32(FOSSIL_IO_INVALID_SOCKET, sock);
102-
int result = fossil_io_network_bind(sock, "127.0.0.1", 8081);
114+
FOSSIL_TEST_CASE(c_test_nstream_get_peer_info) {
115+
fossil_nstream_t *ns = fossil_nstream_open("tcp", "127.0.0.1", "8080", NULL);
116+
ASSUME_NOT_CNULL(ns);
117+
118+
char ip_str[64];
119+
uint16_t port;
120+
int result = fossil_nstream_get_peer_info(ns, ip_str, sizeof(ip_str), &port);
103121
ASSUME_ITS_EQUAL_I32(0, result);
104-
fossil_io_network_close(sock);
105-
fossil_io_network_destroy();
122+
123+
fossil_nstream_close(ns);
106124
}
107125

108-
FOSSIL_TEST_CASE(c_test_network_sendto_udp) {
109-
fossil_io_network_create();
110-
fossil_io_socket_t sock = fossil_io_network_create_socket(FOSSIL_IO_SOCKET_TYPE_UDP);
111-
ASSUME_NOT_EQUAL_I32(FOSSIL_IO_INVALID_SOCKET, sock);
112-
const char *message = "Hello, UDP!";
113-
int bytes_sent = fossil_io_network_sendto(sock, message, strlen(message), "127.0.0.1", 8081);
126+
FOSSIL_TEST_CASE(c_test_nstream_send_recv_line) {
127+
fossil_nstream_t *ns = fossil_nstream_open("tcp", "127.0.0.1", "8080", NULL);
128+
ASSUME_NOT_CNULL(ns);
129+
130+
const char *line = "Hello, Fossil Logic!\n";
131+
ssize_t bytes_sent = fossil_nstream_send_line(ns, line);
132+
ASSUME_ITS_EQUAL_I32((int)strlen(line), bytes_sent);
133+
134+
char buffer[128];
135+
ssize_t bytes_received = fossil_nstream_recv_line(ns, buffer, sizeof(buffer));
136+
ASSUME_ITS_TRUE(bytes_received > 0);
137+
138+
fossil_nstream_close(ns);
139+
}
140+
141+
FOSSIL_TEST_CASE(c_test_nstream_ssl_send_recv) {
142+
fossil_nstream_t *ns = fossil_nstream_open("tls", "127.0.0.1", "8080", NULL);
143+
ASSUME_NOT_CNULL(ns);
144+
145+
const char *message = "Secure Hello!";
146+
ssize_t bytes_sent = fossil_nstream_ssl_send(ns, message, strlen(message));
114147
ASSUME_ITS_EQUAL_I32((int)strlen(message), bytes_sent);
115-
fossil_io_network_close(sock);
116-
fossil_io_network_destroy();
148+
149+
char buffer[128];
150+
ssize_t bytes_received = fossil_nstream_ssl_recv(ns, buffer, sizeof(buffer));
151+
ASSUME_ITS_TRUE(bytes_received > 0);
152+
153+
fossil_nstream_close(ns);
117154
}
118155

119156
// * * * * * * * * * * * * * * * * * * * * * * * *
120157
// * Fossil Logic Test Pool
121158
// * * * * * * * * * * * * * * * * * * * * * * * *
122159

123160
FOSSIL_TEST_GROUP(c_network_tests) {
124-
FOSSIL_TEST_ADD(c_network_suite, c_test_network_init);
125-
FOSSIL_TEST_ADD(c_network_suite, c_test_network_create_socket);
126-
FOSSIL_TEST_ADD(c_network_suite, c_test_network_bind);
127-
FOSSIL_TEST_ADD(c_network_suite, c_test_network_listen);
128-
FOSSIL_TEST_ADD(c_network_suite, c_test_network_close);
129-
FOSSIL_TEST_ADD(c_network_suite, c_test_network_create_udp_socket);
130-
FOSSIL_TEST_ADD(c_network_suite, c_test_network_bind_udp);
131-
FOSSIL_TEST_ADD(c_network_suite, c_test_network_sendto_udp);
161+
FOSSIL_TEST_ADD(c_network_suite, c_test_nstream_open);
162+
FOSSIL_TEST_ADD(c_network_suite, c_test_nstream_send_recv);
163+
FOSSIL_TEST_ADD(c_network_suite, c_test_nstream_listen_accept);
164+
FOSSIL_TEST_ADD(c_network_suite, c_test_nstream_set_nonblocking);
165+
FOSSIL_TEST_ADD(c_network_suite, c_test_nstream_wait_readable_writable);
166+
FOSSIL_TEST_ADD(c_network_suite, c_test_nstream_connect_timeout);
167+
FOSSIL_TEST_ADD(c_network_suite, c_test_nstream_get_peer_info);
168+
FOSSIL_TEST_ADD(c_network_suite, c_test_nstream_send_recv_line);
169+
FOSSIL_TEST_ADD(c_network_suite, c_test_nstream_ssl_send_recv);
132170

133171
FOSSIL_TEST_REGISTER(c_network_suite);
134172
}

0 commit comments

Comments
 (0)