Tests: add StrBufStripAllBut and compare it to stripallbut
[citadel] / libcitadel / tests / stringbuf_IO_test.c
1 /*
2  *  CUnit - A Unit testing framework library for C.
3  *  Copyright (C) 2001  Anil Kumar
4  *  
5  *  This library is open source software; you can redistribute it and/or
6  *  modify it under the terms of the GNU Library General Public
7  *  License as published by the Free Software Foundation; either
8  *  version 2 of the License, or (at your option) any later version.
9  *
10  *  This library is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  *  Library General Public License for more details.
14  */
15
16 #include <stdio.h>
17 #include <stdlib.h>
18
19 #include <sys/select.h>
20
21 #include <ctype.h>
22 #include <unistd.h>
23 #include <fcntl.h>
24 #include <signal.h>
25 #include <sys/types.h>
26 #include <sys/wait.h>
27 #include <sys/socket.h>
28 #include <sys/time.h>
29 #include <sys/stat.h>
30 #include <limits.h>
31 #include <netinet/in.h>
32 #include <arpa/inet.h>
33 #include <sys/un.h>
34 #include <netdb.h>
35 #include <sys/poll.h>
36 #include <string.h>
37 #include <pwd.h>
38 #include <errno.h>
39 #include <stdarg.h>
40 #include <pthread.h>
41 #include <signal.h>
42 #include <sys/utsname.h>
43 #include <string.h>
44
45
46 typedef void testfunc(int Sock);
47
48 #define LISTEN_QUEUE_LENGTH 100
49 #include <string.h>
50
51 #include "stringbuf_test.h"
52 #include "../lib/libcitadel.h"
53
54 int msock;                      /* master listening socket */
55 int BindPort;
56 int time_to_die=0;
57 int listen_port=6666;
58 int n_Lines_to_read = 0;
59 int blobsize = 0;
60 int timeout = 5;
61 int selres = 1;
62 char ip_addr[256]="0.0.0.0";
63
64
65 static void TestRevalidateStrBuf(StrBuf *Buf)
66 {
67         CU_ASSERT(strlen(ChrPtr(Buf)) == StrLength(Buf));
68 }
69
70 /* 
71  * This is a generic function to set up a master socket for listening on
72  * a TCP port.  The server shuts down if the bind fails.
73  *
74  * ip_addr      IP address to bind
75  * port_number  port number to bind
76  * queue_len    number of incoming connections to allow in the queue
77  */
78 static int ig_tcp_server(char *ip_addr, int port_number, int queue_len)
79 {
80         struct protoent *p;
81         struct sockaddr_in sin;
82         int s, i;
83
84         memset(&sin, 0, sizeof(sin));
85         sin.sin_family = AF_INET;
86         if (ip_addr == NULL) {
87                 sin.sin_addr.s_addr = INADDR_ANY;
88         } else {
89                 sin.sin_addr.s_addr = inet_addr(ip_addr);
90         }
91
92         if (sin.sin_addr.s_addr == INADDR_NONE) {
93                 sin.sin_addr.s_addr = INADDR_ANY;
94         }
95
96         if (port_number == 0) {
97                 printf("Cannot start: no port number specified.\n");
98                 return (-1);
99         }
100         sin.sin_port = htons((u_short) port_number);
101
102         p = getprotobyname("tcp");
103
104         s = socket(PF_INET, SOCK_STREAM, (p->p_proto));
105         if (s < 0) {
106                 printf("Can't create a socket: %s\n", strerror(errno));
107                 return (-2);
108         }
109         /* Set some socket options that make sense. */
110         i = 1;
111         setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
112
113         #ifndef __APPLE__
114         fcntl(s, F_SETFL, O_NONBLOCK); /* maide: this statement is incorrect
115                                           there should be a preceding F_GETFL
116                                           and a bitwise OR with the previous
117                                           fd flags */
118         #endif
119         
120         if (bind(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
121                 printf("Can't bind: %s\n", strerror(errno));
122                 return (-3);
123         }
124         if (listen(s, queue_len) < 0) {
125                 printf("Can't listen: %s\n", strerror(errno));
126                 return (-4);
127         }
128         return (s);
129 }
130
131
132
133 /*
134  * Entry point for worker threads
135  */
136 static void worker_entry(testfunc F)
137 {
138         int ssock;
139         int i = 0;
140         int ret;
141         struct timeval tv;
142         fd_set readset, tempset;
143
144         
145         tv.tv_sec = 0;
146         tv.tv_usec = 10000;
147         FD_ZERO(&readset);
148         FD_SET(msock, &readset);
149
150         do {
151                 /* Only one thread can accept at a time */
152                 ssock = -1; 
153                 errno = EAGAIN;
154                 do {
155                         ret = -1; /* just one at once should select... */
156                         
157                         FD_ZERO(&tempset);
158                         if (msock > 0) FD_SET(msock, &tempset);
159                         tv.tv_sec = 0;
160                         tv.tv_usec = 10000;
161                         if (msock > 0)  
162                                 ret = select(msock+1, &tempset, NULL, NULL,  &tv);
163                         if ((ret < 0) && (errno != EINTR) && (errno != EAGAIN))
164                         {/* EINTR and EAGAIN are thrown but not of interest. */
165                                 printf("accept() failed:%d %s\n",
166                                         errno, strerror(errno));
167                         }
168                         else if ((ret > 0) && (msock > 0) && FD_ISSET(msock, &tempset))
169                         {/* Successfully selected, and still not shutting down? Accept! */
170                                 ssock = accept(msock, NULL, 0);
171                         }
172                         
173                 } while ((msock > 0) && (ssock < 0)  && (time_to_die == 0));
174
175                 if ((msock == -1)||(time_to_die))
176                 {/* ok, we're going down. */
177                         return;
178                 }
179                 if (ssock < 0 ) continue;
180
181                 if (msock < 0) {
182                         if (ssock > 0) close (ssock);
183                         printf( "inbetween.");
184                         return;
185                 } else { /* Got it? do some real work! */
186                         /* Set the SO_REUSEADDR socket option */
187                         int fdflags; 
188                         i = 1;
189                         setsockopt(ssock, SOL_SOCKET, SO_REUSEADDR,
190                                    &i, sizeof(i));
191
192                         fdflags = fcntl(ssock, F_GETFL);
193                         if (fdflags < 0)
194                                 printf("unable to get server socket flags! %s \n",
195                                         strerror(errno));
196                         fdflags = fdflags | O_NONBLOCK;
197                         if (fcntl(ssock, F_SETFL, fdflags) < 0)
198                                 printf("unable to set server socket nonblocking flags! %s \n",
199                                         strerror(errno));
200
201
202                         F(ssock);
203                 }
204
205         } while (!time_to_die);
206         printf ("bye\n");
207 }
208
209
210 static void SimpleLineBufTestFunc(int sock)
211 {
212         StrBuf *ReadBuffer;
213         StrBuf *Line;
214         const char *Pos = NULL;
215         const char *err = NULL;
216         int i;
217
218         ReadBuffer = NewStrBuf();
219         Line = NewStrBuf();
220
221         for (i = 0; i < n_Lines_to_read; i++) {
222                 StrBufTCP_read_buffered_line_fast(Line, 
223                                                   ReadBuffer, 
224                                                   &Pos,
225                                                   &sock,
226                                                   timeout,
227                                                   selres,
228                                                   &err);
229                 TestRevalidateStrBuf(Line);
230                 if (err != NULL)
231                         printf("%s", err);
232                 CU_ASSERT_PTR_NULL(err);
233                 CU_ASSERT_NOT_EQUAL(sock, -1);
234                 if (sock == -1)
235                         break;
236                 printf("LINE: >%s<\n", ChrPtr(Line));
237         }
238         FreeStrBuf(&ReadBuffer);
239         FreeStrBuf(&Line);
240         time_to_die = 1;
241 }
242
243 static void SimpleLinebufferTest(void)
244 {
245         msock = ig_tcp_server(ip_addr, listen_port, LISTEN_QUEUE_LENGTH);
246
247         worker_entry(SimpleLineBufTestFunc);
248         close (msock);
249 }
250
251
252 static void SimpleBlobTestFunc(int sock)
253 {
254         StrBuf *ReadBuffer;
255         StrBuf *Blob;
256         const char *Pos = NULL;
257         const char *err = NULL;
258         
259         ReadBuffer = NewStrBuf();
260         Blob = NewStrBuf();
261
262         StrBufReadBLOBBuffered(Blob, 
263                                ReadBuffer, 
264                                &Pos,
265                                &sock,
266                                0,
267                                blobsize,
268                                0,
269                                &err);
270         TestRevalidateStrBuf(Blob);
271         if (err != NULL)
272                 printf("%s", err);
273         CU_ASSERT(blobsize == StrLength(Blob));
274         CU_ASSERT_PTR_NULL(err);
275         CU_ASSERT_NOT_EQUAL(sock, -1);
276         if (sock == -1)
277         printf("BLOB: >%s<\n", ChrPtr(Blob));
278         
279         FreeStrBuf(&ReadBuffer);
280         FreeStrBuf(&Blob);
281         time_to_die = 1;
282 }
283
284
285 static void SimpleHttpPostTestFunc(int sock)
286 {
287         StrBuf *ReadBuffer;
288         StrBuf *Blob;
289         StrBuf *Line;
290         const char *Pos = NULL;
291         const char *err = NULL;
292         int blobsize = 0;
293         int i;
294         const char *pch;
295         
296         ReadBuffer = NewStrBuf();
297         Blob = NewStrBuf();
298         Line = NewStrBuf();
299
300         for (i = 0; 1; i++) {
301                 StrBufTCP_read_buffered_line_fast(Line, 
302                                                   ReadBuffer, 
303                                                   &Pos,
304                                                   &sock,
305                                                   timeout,
306                                                   selres,
307                                                   &err);
308                 TestRevalidateStrBuf(Line);
309                 if (err != NULL)
310                         printf("%s", err);
311                 CU_ASSERT_PTR_NULL(err);
312                 CU_ASSERT_NOT_EQUAL(sock, -1);
313                 if (sock == -1)
314                         break;
315                 printf("LINE: >%s<\n", ChrPtr(Line));
316                 pch = strstr(ChrPtr(Line), "Content-Length");
317                 if (pch != NULL) {
318                         blobsize = atol(ChrPtr(Line) + 
319                                         sizeof("Content-Length:"));
320
321                 }
322                 if (StrLength(Line) == 0)
323                         break;
324                 FlushStrBuf(Line);
325         }
326
327         StrBufReadBLOBBuffered(Blob, 
328                                ReadBuffer, 
329                                &Pos,
330                                &sock,
331                                0,
332                                blobsize,
333                                0,
334                                &err);
335         TestRevalidateStrBuf(Blob);
336         if (err != NULL)
337                 printf("%s", err);
338         printf("Blob said/read: %d / %d\n", blobsize, StrLength(Blob));
339         CU_ASSERT(blobsize != 0);
340         CU_ASSERT(blobsize == StrLength(Blob));
341         CU_ASSERT_PTR_NULL(err);
342         CU_ASSERT_NOT_EQUAL(sock, -1);
343         if (sock == -1)
344         printf("BLOB: >%s<\n", ChrPtr(Blob));
345         
346         FreeStrBuf(&ReadBuffer);
347         FreeStrBuf(&Blob);
348         FreeStrBuf(&Line);
349         time_to_die = 1;
350 }
351
352
353 static void SimpleBLOBbufferTest(void)
354 {
355         msock = ig_tcp_server(ip_addr, listen_port, LISTEN_QUEUE_LENGTH);
356
357         worker_entry(SimpleBlobTestFunc);
358         close (msock);
359 }
360
361 static void SimpleMixedLineBlob(void)
362 {
363         msock = ig_tcp_server(ip_addr, listen_port, LISTEN_QUEUE_LENGTH);
364
365         worker_entry(SimpleHttpPostTestFunc);
366         close (msock);
367 }
368
369
370
371
372
373 /*
374 Some samples from the original...
375         CU_ASSERT_EQUAL(10, 10);
376         CU_ASSERT_EQUAL(0, -0);
377         CU_ASSERT_EQUAL(-12, -12);
378         CU_ASSERT_NOT_EQUAL(10, 11);
379         CU_ASSERT_NOT_EQUAL(0, -1);
380         CU_ASSERT_NOT_EQUAL(-12, -11);
381         CU_ASSERT_PTR_EQUAL((void*)0x100, (void*)0x100);
382         CU_ASSERT_PTR_NOT_EQUAL((void*)0x100, (void*)0x101);
383         CU_ASSERT_PTR_NULL(NULL);
384         CU_ASSERT_PTR_NULL(0x0);
385         CU_ASSERT_PTR_NOT_NULL((void*)0x23);
386         CU_ASSERT_STRING_EQUAL(str1, str2);
387         CU_ASSERT_STRING_NOT_EQUAL(str1, str2);
388         CU_ASSERT_NSTRING_EQUAL(str1, str2, strlen(str1));
389         CU_ASSERT_NSTRING_EQUAL(str1, str1, strlen(str1));
390         CU_ASSERT_NSTRING_EQUAL(str1, str1, strlen(str1) + 1);
391         CU_ASSERT_NSTRING_NOT_EQUAL(str1, str2, 3);
392         CU_ASSERT_NSTRING_NOT_EQUAL(str1, str3, strlen(str1) + 1);
393         CU_ASSERT_DOUBLE_EQUAL(10, 10.0001, 0.0001);
394         CU_ASSERT_DOUBLE_EQUAL(10, 10.0001, -0.0001);
395         CU_ASSERT_DOUBLE_EQUAL(-10, -10.0001, 0.0001);
396         CU_ASSERT_DOUBLE_EQUAL(-10, -10.0001, -0.0001);
397         CU_ASSERT_DOUBLE_NOT_EQUAL(10, 10.001, 0.0001);
398         CU_ASSERT_DOUBLE_NOT_EQUAL(10, 10.001, -0.0001);
399         CU_ASSERT_DOUBLE_NOT_EQUAL(-10, -10.001, 0.0001);
400         CU_ASSERT_DOUBLE_NOT_EQUAL(-10, -10.001, -0.0001);
401 */
402
403
404
405
406
407 static void AddStrBufSimlpeTests(void)
408 {
409         CU_pSuite pGroup = NULL;
410         CU_pTest pTest = NULL;
411
412         pGroup = CU_add_suite("TestStringBufSimpleAppenders", NULL, NULL);
413         if (n_Lines_to_read > 0)
414                 pTest = CU_add_test(pGroup, "testSimpleLinebufferTest", SimpleLinebufferTest);
415         else if (blobsize > 0)
416                 pTest = CU_add_test(pGroup, "testSimpleBLOBbufferTest", SimpleBLOBbufferTest);
417         else 
418                 pTest = CU_add_test(pGroup,"testSimpleMixedLineBlob", SimpleMixedLineBlob);
419
420 }
421
422
423 int main(int argc, char* argv[])
424 {
425         char a;
426         setvbuf(stdout, NULL, _IONBF, 0);
427
428
429         while ((a = getopt(argc, argv, "p:i:n:b:t:s")) != EOF)
430         {
431                 switch (a) {
432
433                 case 'p':
434                         listen_port = atoi(optarg);
435                         break;
436                 case 'i':
437                         safestrncpy(ip_addr, optarg, sizeof ip_addr);
438                         break;
439                 case 'n':
440                         // do linetest?
441                         n_Lines_to_read = atoi(optarg);
442                         break;
443                 case 'b':
444                         // or blobtest?
445                         blobsize = atoi(optarg);
446                         // else run the simple http test
447                         break;
448                 case 't':
449                         if (optarg != NULL)
450                                 timeout = atoi(optarg);
451                         break;
452                 case 's':
453                         if (optarg != NULL)
454                                 selres = atoi(optarg);
455                         break;
456                 }
457         }
458
459
460         StartLibCitadel(8);
461         CU_BOOL Run = CU_FALSE ;
462         
463         CU_set_output_filename("TestAutomated");
464         if (CU_initialize_registry()) {
465                 printf("\nInitialize of test Registry failed.");
466         }
467         
468         Run = CU_TRUE ;
469         AddStrBufSimlpeTests();
470         
471         if (CU_TRUE == Run) {
472                 //CU_console_run_tests();
473     printf("\nTests completed with return value %d.\n", CU_basic_run_tests());
474     
475     ///CU_automated_run_tests();
476         }
477         
478         CU_cleanup_registry();
479
480         return 0;
481 }