1
2 #include "cs_config.h"
3 #include <unistd.h>
4 #include <stdlib.h>
5 #include <stdio.h>
6 #include <string.h>
7 #include <sys/wait.h>
8
9 #include "util/neo_misc.h"
10 #include "util/neo_err.h"
11 #include "util/neo_net.h"
12 #include "util/ulist.h"
13 #include "util/neo_rand.h"
14
15 #define TEST_PORT 46032
16 #define COUNT 10000
17
18 typedef struct _rand_thing {
19 int is_num;
20 int n;
21 char *s;
22 } RAND_THING;
23
24
client_proc(int port,ULIST * stuff)25 NEOERR *client_proc(int port, ULIST *stuff)
26 {
27 NEOERR *err;
28 NSOCK *nsock;
29 int x;
30 RAND_THING *thing;
31
32 sleep(1);
33 ne_warn("[c] Connecting to port %d", port);
34 err = ne_net_connect(&nsock, "localhost", port, 10, 10);
35 if (err) return nerr_pass(err);
36
37 ne_warn("[c] Connected.");
38
39 do
40 {
41 err = ne_net_write_int(nsock, uListLength(stuff));
42 if (err) break;
43
44 for (x = 0; x < uListLength(stuff); x++)
45 {
46 err = uListGet(stuff, x, (void *)&thing);
47 if (err) break;
48 if (thing->is_num)
49 {
50 err = ne_net_write_int(nsock, thing->n);
51 /* ne_warn("[c] Sending %d", thing->n); */
52 }
53 else
54 {
55 err = ne_net_write_str(nsock, thing->s);
56 /* ne_warn("[c] Sending %s", thing->s); */
57 }
58 if (err) break;
59 }
60 } while (0);
61
62 ne_net_close(&nsock);
63 return nerr_pass(err);
64 }
65
server_proc(int port,ULIST * stuff)66 NEOERR *server_proc(int port, ULIST *stuff)
67 {
68 NEOERR *err;
69 int server;
70 NSOCK *nsock;
71 int x, i;
72 RAND_THING *thing;
73 char *s;
74
75 ne_warn("[s] Listening on port %d", port);
76 err = ne_net_listen(port, &server);
77 if (err) return nerr_pass(err);
78
79 err = ne_net_accept(&nsock, server, 10);
80 if (err) return nerr_pass(err);
81
82 ne_warn("[s] Connection.");
83
84 do {
85 err = ne_net_read_int(nsock, &x);
86 if (err) break;
87
88 if (x != uListLength(stuff))
89 {
90 err = nerr_raise(NERR_ASSERT, "Incoming length is not equal to expected length: %d != %d", x, uListLength(stuff));
91 break;
92 }
93
94 for (x = 0; x < uListLength(stuff); x++)
95 {
96 err = uListGet(stuff, x, (void *)&thing);
97 if (err) break;
98 if (thing->is_num)
99 {
100 err = ne_net_read_int(nsock, &i);
101 if (err) break;
102 /* ne_warn("[s] Received %d", i); */
103 if (thing->n != i)
104 {
105 err = nerr_raise(NERR_ASSERT, "Incoming %d number is not equal to expected: %d != %d", x, i, thing->n);
106 break;
107 }
108 }
109 else
110 {
111 err = ne_net_read_str_alloc(nsock, &s, NULL);
112 if (err) break;
113 /* ne_warn("[s] Received %s", s); */
114 if (strcmp(s, thing->s))
115 {
116 err = nerr_raise(NERR_ASSERT, "Incoming %d string is not equal to expected: '%s' != '%s'", x, s, thing->s);
117 break;
118 }
119 free(s);
120 }
121 printf("\rs");
122 }
123 } while (0);
124 ne_net_close(&nsock);
125
126 return nerr_pass(err);
127 }
128
run_test(void)129 NEOERR *run_test(void)
130 {
131 NEOERR *err;
132 ULIST *stuff;
133 char word[64000];
134 RAND_THING *thing;
135 pid_t child;
136 int x;
137
138 ne_warn("starting net_test");
139 ne_warn("generating random list");
140
141 err = uListInit(&stuff, COUNT, 0);
142 if (err) return nerr_pass(err);
143 for (x = 0; x < COUNT; x++)
144 {
145 thing = (RAND_THING *) calloc(1, sizeof(RAND_THING));
146 if (neo_rand(100) > 50)
147 {
148 thing->is_num = 1;
149 thing->n = neo_rand(1000000);
150 }
151 else
152 {
153 neo_rand_word(word, sizeof(word));
154 thing->s = strdup(word);
155 }
156 err = uListAppend(stuff, thing);
157 if (err) return nerr_pass(err);
158 }
159
160 child = fork();
161 if (!child)
162 {
163 /* child */
164 return nerr_pass(client_proc(TEST_PORT, stuff));
165 }
166 /* parent */
167 err = server_proc(TEST_PORT, stuff);
168
169 if (!err) waitpid(child, NULL, 0);
170 return nerr_pass(err);
171 }
172
main(int argc,char ** argv)173 int main(int argc, char **argv)
174 {
175 NEOERR *err;
176
177 nerr_init();
178 err = run_test();
179 if (err)
180 {
181 nerr_log_error(err);
182 return -1;
183 }
184 return 0;
185 }
186