removed StartLibCitadel()
[citadel.git] / libcitadel / tests / stringbuf_IO_test.c
index 1097b960d4fe8cb10c5fc25605f5256649aff5d0..984b5beb4c608537068e60868f25bfcf8e0d16bf 100644 (file)
@@ -1,9 +1,8 @@
-
 /*
  *  CUnit - A Unit testing framework library for C.
  *  Copyright (C) 2001  Anil Kumar
  *  
- *  This library is free software; you can redistribute it and/or
+ *  This library is open source software; you can redistribute it and/or
  *  modify it under the terms of the GNU Library General Public
  *  License as published by the Free Software Foundation; either
  *  version 2 of the License, or (at your option) any later version.
  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  *  Library General Public License for more details.
- *
- *  You should have received a copy of the GNU Library General Public
- *  License along with this library; if not, write to the Free Software
- *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 
 #include <stdio.h>
@@ -45,6 +40,7 @@
 #include <pthread.h>
 #include <signal.h>
 #include <sys/utsname.h>
+#include <string.h>
 
 
 typedef void testfunc(int Sock);
@@ -141,7 +137,6 @@ static void worker_entry(testfunc F)
 {
        int ssock;
        int i = 0;
-       int fail_this_transaction = 0;
        int ret;
        struct timeval tv;
        fd_set readset, tempset;
@@ -154,7 +149,6 @@ static void worker_entry(testfunc F)
 
        do {
                /* Only one thread can accept at a time */
-               fail_this_transaction = 0;
                ssock = -1; 
                errno = EAGAIN;
                do {
@@ -180,7 +174,7 @@ static void worker_entry(testfunc F)
 
                if ((msock == -1)||(time_to_die))
                {/* ok, we're going down. */
-                       exit(0);
+                       return;
                }
                if (ssock < 0 ) continue;
 
@@ -218,7 +212,7 @@ static void SimpleLineBufTestFunc(int sock)
        StrBuf *ReadBuffer;
        StrBuf *Line;
        const char *Pos = NULL;
-       const char *err;
+       const char *err = NULL;
        int i;
 
        ReadBuffer = NewStrBuf();
@@ -232,9 +226,10 @@ static void SimpleLineBufTestFunc(int sock)
                                                  timeout,
                                                  selres,
                                                  &err);
+               TestRevalidateStrBuf(Line);
                if (err != NULL)
                        printf("%s", err);
-               CU_ASSERT_PTR_NOT_NULL(err);
+               CU_ASSERT_PTR_NULL(err);
                CU_ASSERT_NOT_EQUAL(sock, -1);
                if (sock == -1)
                        break;
@@ -245,14 +240,136 @@ static void SimpleLineBufTestFunc(int sock)
        time_to_die = 1;
 }
 
-
 static void SimpleLinebufferTest(void)
 {
        msock = ig_tcp_server(ip_addr, listen_port, LISTEN_QUEUE_LENGTH);
 
        worker_entry(SimpleLineBufTestFunc);
+       close (msock);
+}
+
+
+static void SimpleBlobTestFunc(int sock)
+{
+       StrBuf *ReadBuffer;
+       StrBuf *Blob;
+       const char *Pos = NULL;
+       const char *err = NULL;
+       
+       ReadBuffer = NewStrBuf();
+       Blob = NewStrBuf();
+
+       StrBufReadBLOBBuffered(Blob, 
+                              ReadBuffer, 
+                              &Pos,
+                              &sock,
+                              0,
+                              blobsize,
+                              0,
+                              &err);
+       TestRevalidateStrBuf(Blob);
+       if (err != NULL)
+               printf("%s", err);
+       CU_ASSERT(blobsize == StrLength(Blob));
+       CU_ASSERT_PTR_NULL(err);
+       CU_ASSERT_NOT_EQUAL(sock, -1);
+       if (sock == -1)
+       printf("BLOB: >%s<\n", ChrPtr(Blob));
+       
+       FreeStrBuf(&ReadBuffer);
+       FreeStrBuf(&Blob);
+       time_to_die = 1;
+}
+
+
+static void SimpleHttpPostTestFunc(int sock)
+{
+       StrBuf *ReadBuffer;
+       StrBuf *Blob;
+       StrBuf *Line;
+       const char *Pos = NULL;
+       const char *err = NULL;
+       int blobsize = 0;
+       int i;
+       const char *pch;
+       
+       ReadBuffer = NewStrBuf();
+       Blob = NewStrBuf();
+       Line = NewStrBuf();
+
+       for (i = 0; 1; i++) {
+               StrBufTCP_read_buffered_line_fast(Line, 
+                                                 ReadBuffer, 
+                                                 &Pos,
+                                                 &sock,
+                                                 timeout,
+                                                 selres,
+                                                 &err);
+               TestRevalidateStrBuf(Line);
+               if (err != NULL)
+                       printf("%s", err);
+               CU_ASSERT_PTR_NULL(err);
+               CU_ASSERT_NOT_EQUAL(sock, -1);
+               if (sock == -1)
+                       break;
+               printf("LINE: >%s<\n", ChrPtr(Line));
+               pch = strstr(ChrPtr(Line), "Content-Length");
+               if (pch != NULL) {
+                       blobsize = atol(ChrPtr(Line) + 
+                                       sizeof("Content-Length:"));
+
+               }
+               if (StrLength(Line) == 0)
+                       break;
+               FlushStrBuf(Line);
+       }
+
+       StrBufReadBLOBBuffered(Blob, 
+                              ReadBuffer, 
+                              &Pos,
+                              &sock,
+                              0,
+                              blobsize,
+                              0,
+                              &err);
+       TestRevalidateStrBuf(Blob);
+       if (err != NULL)
+               printf("%s", err);
+       printf("Blob said/read: %d / %d\n", blobsize, StrLength(Blob));
+       CU_ASSERT(blobsize != 0);
+       CU_ASSERT(blobsize == StrLength(Blob));
+       CU_ASSERT_PTR_NULL(err);
+       CU_ASSERT_NOT_EQUAL(sock, -1);
+       if (sock == -1)
+       printf("BLOB: >%s<\n", ChrPtr(Blob));
+       
+       FreeStrBuf(&ReadBuffer);
+       FreeStrBuf(&Blob);
+       FreeStrBuf(&Line);
+       time_to_die = 1;
 }
 
+
+static void SimpleBLOBbufferTest(void)
+{
+       msock = ig_tcp_server(ip_addr, listen_port, LISTEN_QUEUE_LENGTH);
+
+       worker_entry(SimpleBlobTestFunc);
+       close (msock);
+}
+
+static void SimpleMixedLineBlob(void)
+{
+       msock = ig_tcp_server(ip_addr, listen_port, LISTEN_QUEUE_LENGTH);
+
+       worker_entry(SimpleHttpPostTestFunc);
+       close (msock);
+}
+
+
+
+
+
 /*
 Some samples from the original...
        CU_ASSERT_EQUAL(10, 10);
@@ -293,8 +410,12 @@ static void AddStrBufSimlpeTests(void)
        CU_pTest pTest = NULL;
 
        pGroup = CU_add_suite("TestStringBufSimpleAppenders", NULL, NULL);
-       pTest = CU_add_test(pGroup, "testSimpleLinebufferTest", SimpleLinebufferTest);
-
+       if (n_Lines_to_read > 0)
+               pTest = CU_add_test(pGroup, "testSimpleLinebufferTest", SimpleLinebufferTest);
+       else if (blobsize > 0)
+               pTest = CU_add_test(pGroup, "testSimpleBLOBbufferTest", SimpleBLOBbufferTest);
+       else 
+               pTest = CU_add_test(pGroup,"testSimpleMixedLineBlob", SimpleMixedLineBlob);
 
 }
 
@@ -316,22 +437,26 @@ int main(int argc, char* argv[])
                        safestrncpy(ip_addr, optarg, sizeof ip_addr);
                        break;
                case 'n':
+                       // do linetest?
                        n_Lines_to_read = atoi(optarg);
                        break;
                case 'b':
+                       // or blobtest?
                        blobsize = atoi(optarg);
+                       // else run the simple http test
                        break;
                case 't':
-                       timeout = atoi(optarg);
+                       if (optarg != NULL)
+                               timeout = atoi(optarg);
                        break;
                case 's':
-                       selres = atoi(optarg);
+                       if (optarg != NULL)
+                               selres = atoi(optarg);
                        break;
                }
        }
 
 
-       StartLibCitadel(8);
        CU_BOOL Run = CU_FALSE ;
        
        CU_set_output_filename("TestAutomated");