view 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 source
#include "listutil.h"

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>

#define MAX_FILESIZE_KB 1024

void handle_errno()
{
    printf("Error: %s \n", strerror(errno));
}

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 cleanup;
    }

    rc = fstat(fd, &fileStat);
    if (rc < 0) {
        printf ("Stat failed with rc: %i\n", rc);
        retval = StatFailed;
        goto cleanup;
    }

    // Check the size of the file
    if (!fileStat.st_size) {
        printf("Size zero\n");
        retval = StatFailed;
        goto cleanup;
    }

    if (fileStat.st_size / 1024 > MAX_FILESIZE_KB &&
            fileStat.st_size > 0) {
        printf("File too large\n");
        retval = TooLarge;
        goto cleanup;
    }

    *size = (size_t) fileStat.st_size;

    *data = (char*) malloc(*size);

    if (data == NULL) {
        printf("Malloc failed\n");
        retval = UnknownError;
        goto cleanup;
    }

    bRead = read(fd, *data, *size);

    if (bRead < 0 || (size_t) bRead != *size) {
        printf("Read failed\n");
        if (bRead == -1) {
            handle_errno();
        }
        retval = UnknownError;
        *size = 0;
        if (*data) {
            free(*data);
            printf("Nulling data\n");
            *data = NULL;
        }
        goto cleanup;
    }

    retval = Unknown;
cleanup:

    if (fd && fd != -1) {
        close(fd);
        fd = -1;
    }

    return retval;
}

list_status_t readAndVerifyList(const char *fileName, char **data, size_t *size)
{
//    int validSig = 0;
    char ** firstChar = NULL;

    list_status_t retval = UnknownError;
    *data = NULL;
    *size = 0;

    retval = readList(fileName, data, size);

    if (retval != Unknown) {
        printf ("Readlist failed\n");
        return retval;
    }

    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;

    if (**firstChar != 'S') {
        printf("Does not start with S\n");
        retval = InvalidFormat;
        goto cleanup;
    }

// TODO VERIFIY
retval = Valid;

cleanup:
    if (retval != Valid && *data) {
        free(*data);
        *data = NULL;
        *size = 0;
    }
    return retval;
}

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