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)
140 int fail_this_transaction = 0;
143 fd_set readset, tempset;
149 FD_SET(msock, &readset);
152 /* Only one thread can accept at a time */
153 fail_this_transaction = 0;
157 ret = -1; /* just one at once should select... */
160 if (msock > 0) FD_SET(msock, &tempset);
164 ret = select(msock+1, &tempset, NULL, NULL, &tv);
165 if ((ret < 0) && (errno != EINTR) && (errno != EAGAIN))
166 {/* EINTR and EAGAIN are thrown but not of interest. */
167 printf("accept() failed:%d %s\n",
168 errno, strerror(errno));
170 else if ((ret > 0) && (msock > 0) && FD_ISSET(msock, &tempset))
171 {/* Successfully selected, and still not shutting down? Accept! */
172 ssock = accept(msock, NULL, 0);
175 } while ((msock > 0) && (ssock < 0) && (time_to_die == 0));
177 if ((msock == -1)||(time_to_die))
178 {/* ok, we're going down. */
181 if (ssock < 0 ) continue;
184 if (ssock > 0) close (ssock);
185 printf( "inbetween.");
187 } else { /* Got it? do some real work! */
188 /* Set the SO_REUSEADDR socket option */
191 setsockopt(ssock, SOL_SOCKET, SO_REUSEADDR,
194 fdflags = fcntl(ssock, F_GETFL);
196 printf("unable to get server socket flags! %s \n",
198 fdflags = fdflags | O_NONBLOCK;
199 if (fcntl(ssock, F_SETFL, fdflags) < 0)
200 printf("unable to set server socket nonblocking flags! %s \n",
207 } while (!time_to_die);
212 static void SimpleLineBufTestFunc(int sock)
216 const char *Pos = NULL;
217 const char *err = NULL;
220 ReadBuffer = NewStrBuf();
223 for (i = 0; i < n_Lines_to_read; i++) {
224 StrBufTCP_read_buffered_line_fast(Line,
231 TestRevalidateStrBuf(Line);
234 CU_ASSERT_PTR_NULL(err);
235 CU_ASSERT_NOT_EQUAL(sock, -1);
238 printf("LINE: >%s<\n", ChrPtr(Line));
240 FreeStrBuf(&ReadBuffer);
245 static void SimpleLinebufferTest(void)
247 msock = ig_tcp_server(ip_addr, listen_port, LISTEN_QUEUE_LENGTH);
249 worker_entry(SimpleLineBufTestFunc);
254 static void SimpleBlobTestFunc(int sock)
258 const char *Pos = NULL;
259 const char *err = NULL;
261 ReadBuffer = NewStrBuf();
264 StrBufReadBLOBBuffered(Blob,
272 TestRevalidateStrBuf(Blob);
275 CU_ASSERT(blobsize == StrLength(Blob));
276 CU_ASSERT_PTR_NULL(err);
277 CU_ASSERT_NOT_EQUAL(sock, -1);
279 printf("BLOB: >%s<\n", ChrPtr(Blob));
281 FreeStrBuf(&ReadBuffer);
287 static void SimpleHttpPostTestFunc(int sock)
292 const char *Pos = NULL;
293 const char *err = NULL;
298 ReadBuffer = NewStrBuf();
302 for (i = 0; 1; i++) {
303 StrBufTCP_read_buffered_line_fast(Line,
310 TestRevalidateStrBuf(Line);
313 CU_ASSERT_PTR_NULL(err);
314 CU_ASSERT_NOT_EQUAL(sock, -1);
317 printf("LINE: >%s<\n", ChrPtr(Line));
318 pch = strstr(ChrPtr(Line), "Content-Length");
320 blobsize = atol(ChrPtr(Line) +
321 sizeof("Content-Length:"));
324 if (StrLength(Line) == 0)
329 StrBufReadBLOBBuffered(Blob,
337 TestRevalidateStrBuf(Blob);
340 printf("Blob said/read: %d / %d\n", blobsize, StrLength(Blob));
341 CU_ASSERT(blobsize != 0);
342 CU_ASSERT(blobsize == StrLength(Blob));
343 CU_ASSERT_PTR_NULL(err);
344 CU_ASSERT_NOT_EQUAL(sock, -1);
346 printf("BLOB: >%s<\n", ChrPtr(Blob));
348 FreeStrBuf(&ReadBuffer);
355 static void SimpleBLOBbufferTest(void)
357 msock = ig_tcp_server(ip_addr, listen_port, LISTEN_QUEUE_LENGTH);
359 worker_entry(SimpleBlobTestFunc);
363 static void SimpleMixedLineBlob(void)
365 msock = ig_tcp_server(ip_addr, listen_port, LISTEN_QUEUE_LENGTH);
367 worker_entry(SimpleHttpPostTestFunc);
376 Some samples from the original...
377 CU_ASSERT_EQUAL(10, 10);
378 CU_ASSERT_EQUAL(0, -0);
379 CU_ASSERT_EQUAL(-12, -12);
380 CU_ASSERT_NOT_EQUAL(10, 11);
381 CU_ASSERT_NOT_EQUAL(0, -1);
382 CU_ASSERT_NOT_EQUAL(-12, -11);
383 CU_ASSERT_PTR_EQUAL((void*)0x100, (void*)0x100);
384 CU_ASSERT_PTR_NOT_EQUAL((void*)0x100, (void*)0x101);
385 CU_ASSERT_PTR_NULL(NULL);
386 CU_ASSERT_PTR_NULL(0x0);
387 CU_ASSERT_PTR_NOT_NULL((void*)0x23);
388 CU_ASSERT_STRING_EQUAL(str1, str2);
389 CU_ASSERT_STRING_NOT_EQUAL(str1, str2);
390 CU_ASSERT_NSTRING_EQUAL(str1, str2, strlen(str1));
391 CU_ASSERT_NSTRING_EQUAL(str1, str1, strlen(str1));
392 CU_ASSERT_NSTRING_EQUAL(str1, str1, strlen(str1) + 1);
393 CU_ASSERT_NSTRING_NOT_EQUAL(str1, str2, 3);
394 CU_ASSERT_NSTRING_NOT_EQUAL(str1, str3, strlen(str1) + 1);
395 CU_ASSERT_DOUBLE_EQUAL(10, 10.0001, 0.0001);
396 CU_ASSERT_DOUBLE_EQUAL(10, 10.0001, -0.0001);
397 CU_ASSERT_DOUBLE_EQUAL(-10, -10.0001, 0.0001);
398 CU_ASSERT_DOUBLE_EQUAL(-10, -10.0001, -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 CU_ASSERT_DOUBLE_NOT_EQUAL(-10, -10.001, 0.0001);
402 CU_ASSERT_DOUBLE_NOT_EQUAL(-10, -10.001, -0.0001);
409 static void AddStrBufSimlpeTests(void)
411 CU_pSuite pGroup = NULL;
412 CU_pTest pTest = NULL;
414 pGroup = CU_add_suite("TestStringBufSimpleAppenders", NULL, NULL);
415 if (n_Lines_to_read > 0)
416 pTest = CU_add_test(pGroup, "testSimpleLinebufferTest", SimpleLinebufferTest);
417 else if (blobsize > 0)
418 pTest = CU_add_test(pGroup, "testSimpleBLOBbufferTest", SimpleBLOBbufferTest);
420 pTest = CU_add_test(pGroup,"testSimpleMixedLineBlob", SimpleMixedLineBlob);
425 int main(int argc, char* argv[])
428 setvbuf(stdout, NULL, _IONBF, 0);
431 while ((a = getopt(argc, argv, "p:i:n:b:t:s")) != EOF)
436 listen_port = atoi(optarg);
439 safestrncpy(ip_addr, optarg, sizeof ip_addr);
443 n_Lines_to_read = atoi(optarg);
447 blobsize = atoi(optarg);
448 // else run the simple http test
452 timeout = atoi(optarg);
456 selres = atoi(optarg);
463 CU_BOOL Run = CU_FALSE ;
465 CU_set_output_filename("TestAutomated");
466 if (CU_initialize_registry()) {
467 printf("\nInitialize of test Registry failed.");
471 AddStrBufSimlpeTests();
473 if (CU_TRUE == Run) {
474 //CU_console_run_tests();
475 printf("\nTests completed with return value %d.\n", CU_basic_run_tests());
477 ///CU_automated_run_tests();
480 CU_cleanup_registry();