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>
50 typedef void testfunc(int Sock);
52 #define LISTEN_QUEUE_LENGTH 100
55 #include "stringbuf_test.h"
56 #include "../lib/libcitadel.h"
58 int msock; /* master listening socket */
62 int n_Lines_to_read = 0;
66 char ip_addr[256]="0.0.0.0";
69 static void TestRevalidateStrBuf(StrBuf *Buf)
71 CU_ASSERT(strlen(ChrPtr(Buf)) == StrLength(Buf));
75 * This is a generic function to set up a master socket for listening on
76 * a TCP port. The server shuts down if the bind fails.
78 * ip_addr IP address to bind
79 * port_number port number to bind
80 * queue_len number of incoming connections to allow in the queue
82 static int ig_tcp_server(char *ip_addr, int port_number, int queue_len)
85 struct sockaddr_in sin;
88 memset(&sin, 0, sizeof(sin));
89 sin.sin_family = AF_INET;
90 if (ip_addr == NULL) {
91 sin.sin_addr.s_addr = INADDR_ANY;
93 sin.sin_addr.s_addr = inet_addr(ip_addr);
96 if (sin.sin_addr.s_addr == INADDR_NONE) {
97 sin.sin_addr.s_addr = INADDR_ANY;
100 if (port_number == 0) {
101 printf("Cannot start: no port number specified.\n");
104 sin.sin_port = htons((u_short) port_number);
106 p = getprotobyname("tcp");
108 s = socket(PF_INET, SOCK_STREAM, (p->p_proto));
110 printf("Can't create a socket: %s\n", strerror(errno));
113 /* Set some socket options that make sense. */
115 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
118 fcntl(s, F_SETFL, O_NONBLOCK); /* maide: this statement is incorrect
119 there should be a preceding F_GETFL
120 and a bitwise OR with the previous
124 if (bind(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
125 printf("Can't bind: %s\n", strerror(errno));
128 if (listen(s, queue_len) < 0) {
129 printf("Can't listen: %s\n", strerror(errno));
138 * Entry point for worker threads
140 static void worker_entry(testfunc F)
144 int fail_this_transaction = 0;
147 fd_set readset, tempset;
153 FD_SET(msock, &readset);
156 /* Only one thread can accept at a time */
157 fail_this_transaction = 0;
161 ret = -1; /* just one at once should select... */
164 if (msock > 0) FD_SET(msock, &tempset);
168 ret = select(msock+1, &tempset, NULL, NULL, &tv);
169 if ((ret < 0) && (errno != EINTR) && (errno != EAGAIN))
170 {/* EINTR and EAGAIN are thrown but not of interest. */
171 printf("accept() failed:%d %s\n",
172 errno, strerror(errno));
174 else if ((ret > 0) && (msock > 0) && FD_ISSET(msock, &tempset))
175 {/* Successfully selected, and still not shutting down? Accept! */
176 ssock = accept(msock, NULL, 0);
179 } while ((msock > 0) && (ssock < 0) && (time_to_die == 0));
181 if ((msock == -1)||(time_to_die))
182 {/* ok, we're going down. */
185 if (ssock < 0 ) continue;
188 if (ssock > 0) close (ssock);
189 printf( "inbetween.");
191 } else { /* Got it? do some real work! */
192 /* Set the SO_REUSEADDR socket option */
195 setsockopt(ssock, SOL_SOCKET, SO_REUSEADDR,
198 fdflags = fcntl(ssock, F_GETFL);
200 printf("unable to get server socket flags! %s \n",
202 fdflags = fdflags | O_NONBLOCK;
203 if (fcntl(ssock, F_SETFL, fdflags) < 0)
204 printf("unable to set server socket nonblocking flags! %s \n",
211 } while (!time_to_die);
216 static void SimpleLineBufTestFunc(int sock)
220 const char *Pos = NULL;
224 ReadBuffer = NewStrBuf();
227 for (i = 0; i < n_Lines_to_read; i++) {
228 StrBufTCP_read_buffered_line_fast(Line,
237 CU_ASSERT_PTR_NOT_NULL(err);
238 CU_ASSERT_NOT_EQUAL(sock, -1);
241 printf("LINE: >%s<\n", ChrPtr(Line));
243 FreeStrBuf(&ReadBuffer);
249 static void SimpleLinebufferTest(void)
251 msock = ig_tcp_server(ip_addr, listen_port, LISTEN_QUEUE_LENGTH);
253 worker_entry(SimpleLineBufTestFunc);
257 Some samples from the original...
258 CU_ASSERT_EQUAL(10, 10);
259 CU_ASSERT_EQUAL(0, -0);
260 CU_ASSERT_EQUAL(-12, -12);
261 CU_ASSERT_NOT_EQUAL(10, 11);
262 CU_ASSERT_NOT_EQUAL(0, -1);
263 CU_ASSERT_NOT_EQUAL(-12, -11);
264 CU_ASSERT_PTR_EQUAL((void*)0x100, (void*)0x100);
265 CU_ASSERT_PTR_NOT_EQUAL((void*)0x100, (void*)0x101);
266 CU_ASSERT_PTR_NULL(NULL);
267 CU_ASSERT_PTR_NULL(0x0);
268 CU_ASSERT_PTR_NOT_NULL((void*)0x23);
269 CU_ASSERT_STRING_EQUAL(str1, str2);
270 CU_ASSERT_STRING_NOT_EQUAL(str1, str2);
271 CU_ASSERT_NSTRING_EQUAL(str1, str2, strlen(str1));
272 CU_ASSERT_NSTRING_EQUAL(str1, str1, strlen(str1));
273 CU_ASSERT_NSTRING_EQUAL(str1, str1, strlen(str1) + 1);
274 CU_ASSERT_NSTRING_NOT_EQUAL(str1, str2, 3);
275 CU_ASSERT_NSTRING_NOT_EQUAL(str1, str3, strlen(str1) + 1);
276 CU_ASSERT_DOUBLE_EQUAL(10, 10.0001, 0.0001);
277 CU_ASSERT_DOUBLE_EQUAL(10, 10.0001, -0.0001);
278 CU_ASSERT_DOUBLE_EQUAL(-10, -10.0001, 0.0001);
279 CU_ASSERT_DOUBLE_EQUAL(-10, -10.0001, -0.0001);
280 CU_ASSERT_DOUBLE_NOT_EQUAL(10, 10.001, 0.0001);
281 CU_ASSERT_DOUBLE_NOT_EQUAL(10, 10.001, -0.0001);
282 CU_ASSERT_DOUBLE_NOT_EQUAL(-10, -10.001, 0.0001);
283 CU_ASSERT_DOUBLE_NOT_EQUAL(-10, -10.001, -0.0001);
290 static void AddStrBufSimlpeTests(void)
292 CU_pSuite pGroup = NULL;
293 CU_pTest pTest = NULL;
295 pGroup = CU_add_suite("TestStringBufSimpleAppenders", NULL, NULL);
296 pTest = CU_add_test(pGroup, "testSimpleLinebufferTest", SimpleLinebufferTest);
302 int main(int argc, char* argv[])
305 setvbuf(stdout, NULL, _IONBF, 0);
308 while ((a = getopt(argc, argv, "p:i:n:b:t:s")) != EOF)
313 listen_port = atoi(optarg);
316 safestrncpy(ip_addr, optarg, sizeof ip_addr);
319 n_Lines_to_read = atoi(optarg);
322 blobsize = atoi(optarg);
325 timeout = atoi(optarg);
328 selres = atoi(optarg);
335 CU_BOOL Run = CU_FALSE ;
337 CU_set_output_filename("TestAutomated");
338 if (CU_initialize_registry()) {
339 printf("\nInitialize of test Registry failed.");
343 AddStrBufSimlpeTests();
345 if (CU_TRUE == Run) {
346 //CU_console_run_tests();
347 printf("\nTests completed with return value %d.\n", CU_basic_run_tests());
349 ///CU_automated_run_tests();
352 CU_cleanup_registry();