diff ui/listutil.c @ 7:992c0ec57660

Add unit tests make CertificateList work.
author Andre Heinecke <aheinecke@intevation.de>
date Wed, 12 Feb 2014 16:52:27 +0000
parents 9849250f50f2
children 2ad9a96518e3
line wrap: on
line diff
--- a/ui/listutil.c	Tue Feb 11 17:57:55 2014 +0000
+++ b/ui/listutil.c	Wed Feb 12 16:52:27 2014 +0000
@@ -7,6 +7,7 @@
 #include <unistd.h>
 #include <sys/types.h>
 #include <sys/stat.h>
+#include <string.h>
 
 #define MAX_FILESIZE_KB 1024
 
@@ -15,72 +16,74 @@
     printf("Error: %s \n", strerror(errno));
 }
 
-list_status_t readList(char *fileName, void *data, size_t *size)
+list_status_t readList(const char *fileName, char **data, size_t *size)
 {
     int fd = -1;
     struct stat fileStat;
     int rc = 0;
     ssize_t bRead = 0;
+
+    memset(&fileStat, 0, sizeof(fileStat));
+
     list_status_t retval = UnknownError;
 
     fd = open(fileName, O_RDONLY);
     if (fd == -1) {
         handle_errno();
         retval = StatFailed;
-        goto failure;
+        goto cleanup;
     }
 
     rc = fstat(fd, &fileStat);
     if (rc < 0) {
         printf ("Stat failed with rc: %i\n", rc);
         retval = StatFailed;
-        goto failure;
+        goto cleanup;
     }
 
     // Check the size of the file
-    if (fileStat.st_size) {
+    if (!fileStat.st_size) {
         printf("Size zero\n");
         retval = StatFailed;
-        goto failure;
+        goto cleanup;
     }
 
-    if (fileStat.st_size / 1024 > MAX_FILESIZE_KB ||
-            fileStat.st_size >= (size_t)-1) {
+    if (fileStat.st_size / 1024 > MAX_FILESIZE_KB &&
+            fileStat.st_size > 0) {
         printf("File too large\n");
         retval = TooLarge;
-        goto failure;
+        goto cleanup;
     }
 
-    // We can cast here as we already checked
-    // that this size is not too large for size_t
     *size = (size_t) fileStat.st_size;
 
-    data = malloc(*size);
+    *data = (char*) malloc(*size);
 
     if (data == NULL) {
         printf("Malloc failed\n");
         retval = UnknownError;
-        goto failure;
+        goto cleanup;
     }
 
-    bRead = read(fd, data, *size);
+    bRead = read(fd, *data, *size);
 
-    if (bRead != *size) {
-        printf("Read failed");
+    if (bRead < 0 || (size_t) bRead != *size) {
+        printf("Read failed\n");
         if (bRead == -1) {
             handle_errno();
         }
         retval = UnknownError;
         *size = 0;
-        if (data) {
-            free(data);
-            data = NULL;
+        if (*data) {
+            free(*data);
+            printf("Nulling data\n");
+            *data = NULL;
         }
-        goto failure;
+        goto cleanup;
     }
 
     retval = Unknown;
-failure:
+cleanup:
 
     if (fd && fd != -1) {
         close(fd);
@@ -90,42 +93,44 @@
     return retval;
 }
 
-list_status_t readAndVerifyList(char *fileName, void *data, size_t *size)
+list_status_t readAndVerifyList(const char *fileName, char **data, size_t *size)
 {
-    int validSig = 0;
-    char * firstChar = NULL;
-    int i = 0;
+//    int validSig = 0;
+    char ** firstChar = NULL;
 
     list_status_t retval = UnknownError;
-    data = NULL;
-    size = NULL;
+    *data = NULL;
+    *size = 0;
 
     retval = readList(fileName, data, size);
 
     if (retval != Unknown) {
+        printf ("Readlist failed\n");
         return retval;
     }
 
-    if (!data || !size) {
+    if (!data || !*size) {
         // should not have happend if readList works as specified
+        printf ("No data or no size\n");
+        printf ("%ld\n", (long)data);
         return UnknownError;
     }
 
-    firstChar = (char*) data;
+    firstChar = (char**) data;
 
-    if (*firstChar != 'S') {
+    if (**firstChar != 'S') {
         printf("Does not start with S\n");
         retval = InvalidFormat;
+        goto cleanup;
     }
 
-    for (i=0; i < *size; i++) {
-        printf("%c", firstChar + i);
-    }
+// TODO VERIFIY
+retval = Valid;
 
-failure:
-    if (retval != Valid && data) {
-        free(data);
-        data = NULL;
+cleanup:
+    if (retval != Valid && *data) {
+        free(*data);
+        *data = NULL;
         *size = 0;
     }
     return retval;

http://wald.intevation.org/projects/trustbridge/