3 * CUnit - A Unit testing framework library for C.
4 * Copyright (C) 2001 Anil Kumar
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include <sys/select.h>
30 #include <sys/types.h>
32 #include <sys/socket.h>
36 #include <netinet/in.h>
37 #include <arpa/inet.h>
47 #include <sys/utsname.h>
51 typedef void testfunc(int Sock);
53 #define LISTEN_QUEUE_LENGTH 100
56 #include "stringbuf_test.h"
57 #include "../lib/libcitadel.h"
59 int msock; /* master listening socket */
63 int n_Lines_to_read = 0;
67 char ip_addr[256]="0.0.0.0";
70 static void TestRevalidateStrBuf(StrBuf *Buf)
72 CU_ASSERT(strlen(ChrPtr(Buf)) == StrLength(Buf));
76 * This is a generic function to set up a master socket for listening on
77 * a TCP port. The server shuts down if the bind fails.
79 * ip_addr IP address to bind
80 * port_number port number to bind
81 * queue_len number of incoming connections to allow in the queue
83 static int ig_tcp_server(char *ip_addr, int port_number, int queue_len)
86 struct sockaddr_in sin;
89 memset(&sin, 0, sizeof(sin));
90 sin.sin_family = AF_INET;
91 if (ip_addr == NULL) {
92 sin.sin_addr.s_addr = INADDR_ANY;
94 sin.sin_addr.s_addr = inet_addr(ip_addr);
97 if (sin.sin_addr.s_addr == INADDR_NONE) {
98 sin.sin_addr.s_addr = INADDR_ANY;
101 if (port_number == 0) {
102 printf("Cannot start: no port number specified.\n");
105 sin.sin_port = htons((u_short) port_number);
107 p = getprotobyname("tcp");
109 s = socket(PF_INET, SOCK_STREAM, (p->p_proto));
111 printf("Can't create a socket: %s\n", strerror(errno));
114 /* Set some socket options that make sense. */
116 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
119 fcntl(s, F_SETFL, O_NONBLOCK); /* maide: this statement is incorrect
120 there should be a preceding F_GETFL
121 and a bitwise OR with the previous
125 if (bind(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
126 printf("Can't bind: %s\n", strerror(errno));
129 if (listen(s, queue_len) < 0) {
130 printf("Can't listen: %s\n", strerror(errno));
139 * Entry point for worker threads
141 static void worker_entry(testfunc F)
145 int fail_this_transaction = 0;
148 fd_set readset, tempset;
154 FD_SET(msock, &readset);
157 /* Only one thread can accept at a time */
158 fail_this_transaction = 0;
162 ret = -1; /* just one at once should select... */
165 if (msock > 0) FD_SET(msock, &tempset);
169 ret = select(msock+1, &tempset, NULL, NULL, &tv);
170 if ((ret < 0) && (errno != EINTR) && (errno != EAGAIN))
171 {/* EINTR and EAGAIN are thrown but not of interest. */
172 printf("accept() failed:%d %s\n",
173 errno, strerror(errno));
175 else if ((ret > 0) && (msock > 0) && FD_ISSET(msock, &tempset))
176 {/* Successfully selected, and still not shutting down? Accept! */
177 ssock = accept(msock, NULL, 0);
180 } while ((msock > 0) && (ssock < 0) && (time_to_die == 0));
182 if ((msock == -1)||(time_to_die))
183 {/* ok, we're going down. */
186 if (ssock < 0 ) continue;
189 if (ssock > 0) close (ssock);
190 printf( "inbetween.");
192 } else { /* Got it? do some real work! */
193 /* Set the SO_REUSEADDR socket option */
196 setsockopt(ssock, SOL_SOCKET, SO_REUSEADDR,
199 fdflags = fcntl(ssock, F_GETFL);
201 printf("unable to get server socket flags! %s \n",
203 fdflags = fdflags | O_NONBLOCK;
204 if (fcntl(ssock, F_SETFL, fdflags) < 0)
205 printf("unable to set server socket nonblocking flags! %s \n",
212 } while (!time_to_die);
217 static void SimpleLineBufTestFunc(int sock)
221 const char *Pos = NULL;
222 const char *err = NULL;
225 ReadBuffer = NewStrBuf();
228 for (i = 0; i < n_Lines_to_read; i++) {
229 StrBufTCP_read_buffered_line_fast(Line,
236 TestRevalidateStrBuf(Line);
239 CU_ASSERT_PTR_NULL(err);
240 CU_ASSERT_NOT_EQUAL(sock, -1);
243 printf("LINE: >%s<\n", ChrPtr(Line));
245 FreeStrBuf(&ReadBuffer);
250 static void SimpleLinebufferTest(void)
252 msock = ig_tcp_server(ip_addr, listen_port, LISTEN_QUEUE_LENGTH);
254 worker_entry(SimpleLineBufTestFunc);
259 static void SimpleBlobTestFunc(int sock)
263 const char *Pos = NULL;
264 const char *err = NULL;
266 ReadBuffer = NewStrBuf();
269 StrBufReadBLOBBuffered(Blob,
277 TestRevalidateStrBuf(Blob);
280 CU_ASSERT(blobsize == StrLength(Blob));
281 CU_ASSERT_PTR_NULL(err);
282 CU_ASSERT_NOT_EQUAL(sock, -1);
284 printf("BLOB: >%s<\n", ChrPtr(Blob));
286 FreeStrBuf(&ReadBuffer);
292 static void SimpleHttpPostTestFunc(int sock)
297 const char *Pos = NULL;
298 const char *err = NULL;
303 ReadBuffer = NewStrBuf();
307 for (i = 0; 1; i++) {
308 StrBufTCP_read_buffered_line_fast(Line,
315 TestRevalidateStrBuf(Line);
318 CU_ASSERT_PTR_NULL(err);
319 CU_ASSERT_NOT_EQUAL(sock, -1);
322 printf("LINE: >%s<\n", ChrPtr(Line));
323 pch = strstr(ChrPtr(Line), "Content-Length");
325 blobsize = atol(ChrPtr(Line) +
326 sizeof("Content-Length:"));
329 if (StrLength(Line) == 0)
334 StrBufReadBLOBBuffered(Blob,
342 TestRevalidateStrBuf(Blob);
345 printf("Blob said/read: %d / %d\n", blobsize, StrLength(Blob));
346 CU_ASSERT(blobsize != 0);
347 CU_ASSERT(blobsize == StrLength(Blob));
348 CU_ASSERT_PTR_NULL(err);
349 CU_ASSERT_NOT_EQUAL(sock, -1);
351 printf("BLOB: >%s<\n", ChrPtr(Blob));
353 FreeStrBuf(&ReadBuffer);
360 static void SimpleBLOBbufferTest(void)
362 msock = ig_tcp_server(ip_addr, listen_port, LISTEN_QUEUE_LENGTH);
364 worker_entry(SimpleBlobTestFunc);
368 static void SimpleMixedLineBlob(void)
370 msock = ig_tcp_server(ip_addr, listen_port, LISTEN_QUEUE_LENGTH);
372 worker_entry(SimpleHttpPostTestFunc);
381 Some samples from the original...
382 CU_ASSERT_EQUAL(10, 10);
383 CU_ASSERT_EQUAL(0, -0);
384 CU_ASSERT_EQUAL(-12, -12);
385 CU_ASSERT_NOT_EQUAL(10, 11);
386 CU_ASSERT_NOT_EQUAL(0, -1);
387 CU_ASSERT_NOT_EQUAL(-12, -11);
388 CU_ASSERT_PTR_EQUAL((void*)0x100, (void*)0x100);
389 CU_ASSERT_PTR_NOT_EQUAL((void*)0x100, (void*)0x101);
390 CU_ASSERT_PTR_NULL(NULL);
391 CU_ASSERT_PTR_NULL(0x0);
392 CU_ASSERT_PTR_NOT_NULL((void*)0x23);
393 CU_ASSERT_STRING_EQUAL(str1, str2);
394 CU_ASSERT_STRING_NOT_EQUAL(str1, str2);
395 CU_ASSERT_NSTRING_EQUAL(str1, str2, strlen(str1));
396 CU_ASSERT_NSTRING_EQUAL(str1, str1, strlen(str1));
397 CU_ASSERT_NSTRING_EQUAL(str1, str1, strlen(str1) + 1);
398 CU_ASSERT_NSTRING_NOT_EQUAL(str1, str2, 3);
399 CU_ASSERT_NSTRING_NOT_EQUAL(str1, str3, strlen(str1) + 1);
400 CU_ASSERT_DOUBLE_EQUAL(10, 10.0001, 0.0001);
401 CU_ASSERT_DOUBLE_EQUAL(10, 10.0001, -0.0001);
402 CU_ASSERT_DOUBLE_EQUAL(-10, -10.0001, 0.0001);
403 CU_ASSERT_DOUBLE_EQUAL(-10, -10.0001, -0.0001);
404 CU_ASSERT_DOUBLE_NOT_EQUAL(10, 10.001, 0.0001);
405 CU_ASSERT_DOUBLE_NOT_EQUAL(10, 10.001, -0.0001);
406 CU_ASSERT_DOUBLE_NOT_EQUAL(-10, -10.001, 0.0001);
407 CU_ASSERT_DOUBLE_NOT_EQUAL(-10, -10.001, -0.0001);
414 static void AddStrBufSimlpeTests(void)
416 CU_pSuite pGroup = NULL;
417 CU_pTest pTest = NULL;
419 pGroup = CU_add_suite("TestStringBufSimpleAppenders", NULL, NULL);
420 if (n_Lines_to_read > 0)
421 pTest = CU_add_test(pGroup, "testSimpleLinebufferTest", SimpleLinebufferTest);
422 else if (blobsize > 0)
423 pTest = CU_add_test(pGroup, "testSimpleBLOBbufferTest", SimpleBLOBbufferTest);
425 pTest = CU_add_test(pGroup,"testSimpleMixedLineBlob", SimpleMixedLineBlob);
430 int main(int argc, char* argv[])
433 setvbuf(stdout, NULL, _IONBF, 0);
436 while ((a = getopt(argc, argv, "p:i:n:b:t:s")) != EOF)
441 listen_port = atoi(optarg);
444 safestrncpy(ip_addr, optarg, sizeof ip_addr);
448 n_Lines_to_read = atoi(optarg);
452 blobsize = atoi(optarg);
453 // else run the simple http test
457 timeout = atoi(optarg);
461 selres = atoi(optarg);
468 CU_BOOL Run = CU_FALSE ;
470 CU_set_output_filename("TestAutomated");
471 if (CU_initialize_registry()) {
472 printf("\nInitialize of test Registry failed.");
476 AddStrBufSimlpeTests();
478 if (CU_TRUE == Run) {
479 //CU_console_run_tests();
480 printf("\nTests completed with return value %d.\n", CU_basic_run_tests());
482 ///CU_automated_run_tests();
485 CU_cleanup_registry();