... | ... |
@@ -18,7 +18,6 @@ |
18 | 18 |
|
19 | 19 |
#define STRING_OK "ok" |
20 | 20 |
#define STRING_FAIL "fail" |
21 |
-#define STRING_SEP ":" |
|
22 | 21 |
|
23 | 22 |
typedef enum test_action { |
24 | 23 |
test_name=0, |
... | ... |
@@ -27,10 +26,15 @@ typedef enum test_action { |
27 | 26 |
} test_action; |
28 | 27 |
|
29 | 28 |
char *test1(test_action action); |
29 |
+char *socklib_connect(test_action action); |
|
30 |
+char *socklib_sselect(test_action action); |
|
30 | 31 |
|
31 | 32 |
struct { |
32 | 33 |
char *(*test)(/* test_action action */); |
33 |
-} tests[]={{test1}}; |
|
34 |
+} tests[]={{test1}, |
|
35 |
+ {socklib_connect}, |
|
36 |
+ {socklib_sselect}, |
|
37 |
+ }; |
|
34 | 38 |
|
35 | 39 |
int |
36 | 40 |
main(int argc, char *argv[]) |
... | ... |
@@ -80,4 +84,192 @@ test1(test_action action) |
80 | 84 |
return("ok"); |
81 | 85 |
} |
82 | 86 |
|
87 |
+char * |
|
88 |
+socklib_connect(test_action action) |
|
89 |
+{ |
|
90 |
+ int server,client; |
|
91 |
+ char *host; |
|
92 |
+ long hostsize; |
|
93 |
+ int port; |
|
94 |
+ int off; |
|
95 |
+ int timeout=100; |
|
96 |
+ if(action==test_name) |
|
97 |
+ return("socklib_connect"); |
|
98 |
+ if(action==test_description) |
|
99 |
+ return("listen and connect"); |
|
100 |
+ if((host=ipv4_genip("localhost",&hostsize))==NULL) |
|
101 |
+ return(STRING_FAIL ": couldn't resove localhost"); |
|
102 |
+ for(server=-1,off=0,port=19747;off<1024;off++,port++) { |
|
103 |
+ if((server=ipv4_serverbinded(host,hostsize,port))!=-1) |
|
104 |
+ break; |
|
105 |
+ } |
|
106 |
+ if(server==-1) { |
|
107 |
+ free(host),host=NULL; |
|
108 |
+ return(STRING_FAIL ": couldn't find empty port for server\n"); |
|
109 |
+ } |
|
110 |
+ sock_setunsafe(server); |
|
111 |
+ if((client=ipv4_preconnect(host,hostsize,port))==-1) { |
|
112 |
+ close(server),server=-1; |
|
113 |
+ free(host),host=NULL; |
|
114 |
+ return(STRING_FAIL ": couldn't connect to server\n"); |
|
115 |
+ } |
|
116 |
+ if(ipv4_connect(client,timeout)==-1) { |
|
117 |
+ close(client),client=-1; |
|
118 |
+ close(server),server=-1; |
|
119 |
+ free(host),host=NULL; |
|
120 |
+ return(STRING_FAIL ": timeout on connect\n"); |
|
121 |
+ } |
|
122 |
+ ipv4_postconnect(client); |
|
123 |
+ close(client),client=-1; |
|
124 |
+ close(server),server=-1; |
|
125 |
+ free(host),host=NULL; |
|
126 |
+ return(STRING_OK); |
|
127 |
+} |
|
128 |
+ |
|
129 |
+char * |
|
130 |
+test_socketsinit(int *server, int *client, char **host, long *hostsize, int *port) |
|
131 |
+{ |
|
132 |
+ int timeout=100; |
|
133 |
+ int off; |
|
134 |
+ *server=*client=*port=-1; |
|
135 |
+ *hostsize=0; |
|
136 |
+ *host=NULL; |
|
137 |
+ *port=-1; |
|
138 |
+ if((*host=ipv4_genip("localhost",hostsize))==NULL) |
|
139 |
+ return(STRING_FAIL ": couldn't resove localhost"); |
|
140 |
+ for(*server=-1,off=0,*port=19747;off<1024;off++,(*port)++) { |
|
141 |
+ if((*server=ipv4_serverbinded(*host,*hostsize,*port))!=-1) |
|
142 |
+ break; |
|
143 |
+ } |
|
144 |
+ if(*server==-1) { |
|
145 |
+ free(*host),*host=NULL; |
|
146 |
+ return(STRING_FAIL ": couldn't find empty port for server\n"); |
|
147 |
+ } |
|
148 |
+ sock_setunsafe(*server); |
|
149 |
+ if((*client=ipv4_preconnect(*host,*hostsize,*port))==-1) { |
|
150 |
+ close(*server),*server=-1; |
|
151 |
+ free(*host),*host=NULL; |
|
152 |
+ return(STRING_FAIL ": couldn't connect to server\n"); |
|
153 |
+ } |
|
154 |
+ if(ipv4_connect(*client,timeout)==-1) { |
|
155 |
+ close(*client),*client=-1; |
|
156 |
+ close(*server),*server=-1; |
|
157 |
+ free(*host),*host=NULL; |
|
158 |
+ return(STRING_FAIL ": timeout on connect\n"); |
|
159 |
+ } |
|
160 |
+ ipv4_postconnect(*client); |
|
161 |
+ return(NULL); |
|
162 |
+} |
|
163 |
+ |
|
164 |
+void |
|
165 |
+test_socketsfini(int *server, int *client, char **host, long *hostsize, int *port) |
|
166 |
+{ |
|
167 |
+ if(*server!=-1) |
|
168 |
+ close(*server),*server=-1; |
|
169 |
+ if(*client!=-1) |
|
170 |
+ close(*client),*client=-1; |
|
171 |
+ if(*host!=NULL) |
|
172 |
+ free(*host),*host=NULL; |
|
173 |
+ *hostsize=0; |
|
174 |
+ *port=-1; |
|
175 |
+} |
|
176 |
+ |
|
177 |
+char * |
|
178 |
+socklib_sselect(test_action action) |
|
179 |
+{ |
|
180 |
+ int server,client; |
|
181 |
+ char *host; |
|
182 |
+ long hostsize; |
|
183 |
+ int port; |
|
184 |
+ char *result; |
|
185 |
+ char buf[128]; |
|
186 |
+ int readyfds[16]; |
|
187 |
+ sselect *ssel; |
|
188 |
+ int timeout=100; |
|
189 |
+ int workfd; |
|
190 |
+ if(action==test_name) |
|
191 |
+ return("socklib_sselect"); |
|
192 |
+ if(action==test_description) |
|
193 |
+ return("select over sockets"); |
|
194 |
+ if((result=test_socketsinit(&server,&client,&host,&hostsize,&port))!=NULL) { |
|
195 |
+ test_socketsfini(&server,&client,&host,&hostsize,&port); |
|
196 |
+ return(result); |
|
197 |
+ } |
|
198 |
+ if((ssel=sselect_init())==NULL) { |
|
199 |
+ test_socketsfini(&server,&client,&host,&hostsize,&port); |
|
200 |
+ return(STRING_FAIL ": couldn't init sselect struct"); |
|
201 |
+ } |
|
202 |
+ if(sselect_reset(ssel)!=0) { |
|
203 |
+ sselect_free(ssel),ssel=NULL; |
|
204 |
+ test_socketsfini(&server,&client,&host,&hostsize,&port); |
|
205 |
+ return(STRING_FAIL ": couldn't test the sselect reset function"); |
|
206 |
+ } |
|
207 |
+ if(sselect_wait(ssel,timeout)!=0) { |
|
208 |
+ sselect_free(ssel),ssel=NULL; |
|
209 |
+ test_socketsfini(&server,&client,&host,&hostsize,&port); |
|
210 |
+ return(STRING_FAIL ": (1) was expecting a timeout, something else happened"); |
|
211 |
+ } |
|
212 |
+ if(sselect_addread(ssel,server,buf)!=0) { |
|
213 |
+ sselect_free(ssel),ssel=NULL; |
|
214 |
+ test_socketsfini(&server,&client,&host,&hostsize,&port); |
|
215 |
+ return(STRING_FAIL ": couldn't add 'read serverfd' to the sselect"); |
|
216 |
+ } |
|
217 |
+ if(sselect_wait(ssel,timeout)!=1) { |
|
218 |
+ sselect_free(ssel),ssel=NULL; |
|
219 |
+ test_socketsfini(&server,&client,&host,&hostsize,&port); |
|
220 |
+ return(STRING_FAIL ": (2) was expecting a ready-to-read, something else happened"); |
|
221 |
+ } |
|
222 |
+ if((workfd=sock_accept(server))==-1) { |
|
223 |
+ sselect_free(ssel),ssel=NULL; |
|
224 |
+ test_socketsfini(&server,&client,&host,&hostsize,&port); |
|
225 |
+ return(STRING_FAIL ": couldn't accept connection"); |
|
226 |
+ } |
|
227 |
+ write(client,".",1); |
|
228 |
+ if(sselect_wait(ssel,timeout)!=0) { |
|
229 |
+ sselect_free(ssel),ssel=NULL; |
|
230 |
+ test_socketsfini(&server,&client,&host,&hostsize,&port); |
|
231 |
+ return(STRING_FAIL ": (2) was expecting a timeout, something else happened"); |
|
232 |
+ } |
|
233 |
+ if(sselect_addread(ssel,workfd,&workfd)!=0) { |
|
234 |
+ sselect_free(ssel),ssel=NULL; |
|
235 |
+ close(workfd),workfd=-1; |
|
236 |
+ test_socketsfini(&server,&client,&host,&hostsize,&port); |
|
237 |
+ return(STRING_FAIL ": couldn't add 'read workfd' to the sselect"); |
|
238 |
+ } |
|
239 |
+ if(sselect_wait(ssel,timeout)!=1) { |
|
240 |
+ sselect_free(ssel),ssel=NULL; |
|
241 |
+ close(workfd),workfd=-1; |
|
242 |
+ test_socketsfini(&server,&client,&host,&hostsize,&port); |
|
243 |
+ return(STRING_FAIL ": was expecting a something-to-read, something else happened"); |
|
244 |
+ } |
|
245 |
+ if((sselect_getread(ssel,readyfds,sizeof(readyfds)/sizeof(readyfds[0])))!=1 || *readyfds!=workfd) { |
|
246 |
+ sselect_free(ssel),ssel=NULL; |
|
247 |
+ close(workfd),workfd=-1; |
|
248 |
+ test_socketsfini(&server,&client,&host,&hostsize,&port); |
|
249 |
+ return(STRING_FAIL ": the list of ready-to-read fds is wrong"); |
|
250 |
+ } |
|
251 |
+ if(sselect_getuserptr(ssel,workfd)!=&workfd) { |
|
252 |
+ sselect_free(ssel),ssel=NULL; |
|
253 |
+ close(workfd),workfd=-1; |
|
254 |
+ test_socketsfini(&server,&client,&host,&hostsize,&port); |
|
255 |
+ return(STRING_FAIL ": Couldn't recover the userptr of workfd"); |
|
256 |
+ } |
|
257 |
+ read(workfd,buf,1); |
|
258 |
+ if(sselect_addwrite(ssel,client,buf)!=0) { |
|
259 |
+ sselect_free(ssel),ssel=NULL; |
|
260 |
+ close(workfd),workfd=-1; |
|
261 |
+ test_socketsfini(&server,&client,&host,&hostsize,&port); |
|
262 |
+ return(STRING_FAIL ": couldn't add 'write clientfd' to the sselect"); |
|
263 |
+ } |
|
264 |
+ if(sselect_wait(ssel,timeout)!=1) { |
|
265 |
+ sselect_free(ssel),ssel=NULL; |
|
266 |
+ close(workfd),workfd=-1; |
|
267 |
+ test_socketsfini(&server,&client,&host,&hostsize,&port); |
|
268 |
+ return(STRING_FAIL ": was expecting an ok-to-write, something else happened"); |
|
269 |
+ } |
|
270 |
+ sselect_free(ssel),ssel=NULL; |
|
271 |
+ close(workfd),workfd=-1; |
|
272 |
+ test_socketsfini(&server,&client,&host,&hostsize,&port); |
|
273 |
+ return(STRING_OK); |
|
274 |
+} |
|
83 | 275 |
|