2 * CUnit - A Unit testing framework library for C.
3 * Copyright (C) 2001 Anil Kumar
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.
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.
19 #include <sys/select.h>
25 #include <sys/types.h>
27 #include <sys/socket.h>
31 #include <netinet/in.h>
32 #include <arpa/inet.h>
42 #include <sys/utsname.h>
46 typedef void testfunc(int Sock);
48 #define LISTEN_QUEUE_LENGTH 100
51 #include "stringbuf_test.h"
52 #include "../lib/libcitadel.h"
54 int msock; /* master listening socket */
58 int n_Lines_to_read = 0;
62 char ip_addr[256]="0.0.0.0";
65 static void TestRevalidateStrBuf(StrBuf *Buf)
67 CU_ASSERT(strlen(ChrPtr(Buf)) == StrLength(Buf));
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.
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
78 static int ig_tcp_server(char *ip_addr, int port_number, int queue_len)
81 struct sockaddr_in sin;
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;
89 sin.sin_addr.s_addr = inet_addr(ip_addr);
92 if (sin.sin_addr.s_addr == INADDR_NONE) {
93 sin.sin_addr.s_addr = INADDR_ANY;
96 if (port_number == 0) {
97 printf("Cannot start: no port number specified.\n");
100 sin.sin_port = htons((u_short) port_number);
102 p = getprotobyname("tcp");
104 s = socket(PF_INET, SOCK_STREAM, (p->p_proto));
106 printf("Can't create a socket: %s\n", strerror(errno));
109 /* Set some socket options that make sense. */
111 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
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
120 if (bind(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
121 printf("Can't bind: %s\n", strerror(errno));
124 if (listen(s, queue_len) < 0) {
125 printf("Can't listen: %s\n", strerror(errno));
134 * Entry point for worker threads
136 static void worker_entry(testfunc F)
142 fd_set readset, tempset;
148 FD_SET(msock, &readset);
151 /* Only one thread can accept at a time */
155 ret = -1; /* just one at once should select... */
158 if (msock > 0) FD_SET(msock, &tempset);
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));
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);
173 } while ((msock > 0) && (ssock < 0) && (time_to_die == 0));
175 if ((msock == -1)||(time_to_die))
176 {/* ok, we're going down. */
179 if (ssock < 0 ) continue;
182 if (ssock > 0) close (ssock);
183 printf( "inbetween.");
185 } else { /* Got it? do some real work! */
186 /* Set the SO_REUSEADDR socket option */
189 setsockopt(ssock, SOL_SOCKET, SO_REUSEADDR,
192 fdflags = fcntl(ssock, F_GETFL);
194 printf("unable to get server socket flags! %s \n",
196 fdflags = fdflags | O_NONBLOCK;
197 if (fcntl(ssock, F_SETFL, fdflags) < 0)
198 printf("unable to set server socket nonblocking flags! %s \n",
205 } while (!time_to_die);
210 static void SimpleLineBufTestFunc(int sock)
214 const char *Pos = NULL;
215 const char *err = NULL;
218 ReadBuffer = NewStrBuf();
221 for (i = 0; i < n_Lines_to_read; i++) {
222 StrBufTCP_read_buffered_line_fast(Line,
229 TestRevalidateStrBuf(Line);
232 CU_ASSERT_PTR_NULL(err);
233 CU_ASSERT_NOT_EQUAL(sock, -1);
236 printf("LINE: >%s<\n", ChrPtr(Line));
238 FreeStrBuf(&ReadBuffer);
243 static void SimpleLinebufferTest(void)
245 msock = ig_tcp_server(ip_addr, listen_port, LISTEN_QUEUE_LENGTH);
247 worker_entry(SimpleLineBufTestFunc);
252 static void SimpleBlobTestFunc(int sock)
256 const char *Pos = NULL;
257 const char *err = NULL;
259 ReadBuffer = NewStrBuf();
262 StrBufReadBLOBBuffered(Blob,
270 TestRevalidateStrBuf(Blob);
273 CU_ASSERT(blobsize == StrLength(Blob));
274 CU_ASSERT_PTR_NULL(err);
275 CU_ASSERT_NOT_EQUAL(sock, -1);
277 printf("BLOB: >%s<\n", ChrPtr(Blob));
279 FreeStrBuf(&ReadBuffer);
285 static void SimpleHttpPostTestFunc(int sock)
290 const char *Pos = NULL;
291 const char *err = NULL;
296 ReadBuffer = NewStrBuf();
300 for (i = 0; 1; i++) {
301 StrBufTCP_read_buffered_line_fast(Line,
308 TestRevalidateStrBuf(Line);
311 CU_ASSERT_PTR_NULL(err);
312 CU_ASSERT_NOT_EQUAL(sock, -1);
315 printf("LINE: >%s<\n", ChrPtr(Line));
316 pch = strstr(ChrPtr(Line), "Content-Length");
318 blobsize = atol(ChrPtr(Line) +
319 sizeof("Content-Length:"));
322 if (StrLength(Line) == 0)
327 StrBufReadBLOBBuffered(Blob,
335 TestRevalidateStrBuf(Blob);
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);
344 printf("BLOB: >%s<\n", ChrPtr(Blob));
346 FreeStrBuf(&ReadBuffer);
353 static void SimpleBLOBbufferTest(void)
355 msock = ig_tcp_server(ip_addr, listen_port, LISTEN_QUEUE_LENGTH);
357 worker_entry(SimpleBlobTestFunc);
361 static void SimpleMixedLineBlob(void)
363 msock = ig_tcp_server(ip_addr, listen_port, LISTEN_QUEUE_LENGTH);
365 worker_entry(SimpleHttpPostTestFunc);
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);
407 static void AddStrBufSimlpeTests(void)
409 CU_pSuite pGroup = NULL;
410 CU_pTest pTest = NULL;
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);
418 pTest = CU_add_test(pGroup,"testSimpleMixedLineBlob", SimpleMixedLineBlob);
423 int main(int argc, char* argv[])
426 setvbuf(stdout, NULL, _IONBF, 0);
429 while ((a = getopt(argc, argv, "p:i:n:b:t:s")) != EOF)
434 listen_port = atoi(optarg);
437 safestrncpy(ip_addr, optarg, sizeof ip_addr);
441 n_Lines_to_read = atoi(optarg);
445 blobsize = atoi(optarg);
446 // else run the simple http test
450 timeout = atoi(optarg);
454 selres = atoi(optarg);
461 CU_BOOL Run = CU_FALSE ;
463 CU_set_output_filename("TestAutomated");
464 if (CU_initialize_registry()) {
465 printf("\nInitialize of test Registry failed.");
469 AddStrBufSimlpeTests();
471 if (CU_TRUE == Run) {
472 //CU_console_run_tests();
473 printf("\nTests completed with return value %d.\n", CU_basic_run_tests());
475 ///CU_automated_run_tests();
478 CU_cleanup_registry();