/*
 * webkernel_test.c
 *
 * Tests to stress the webkernel API.
 *
 * Author: Dario Rodriguez dario@softhome.net
 * This program is dual licensed: MIT and in "the public domain".
 */


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "sbuf.h"
#include "socklib.h"
#include "webkernel.h"

#define STRING_OK "ok"
#define STRING_FAIL "fail"
#define STRING_SEP ":"

typedef enum test_action {
        test_name=0,
        test_description,
        test_run
} test_action;

char *test1(test_action action);

struct {
        char *(*test)(/* test_action action */);
} tests[]={{test1}};

int
main(int argc, char *argv[])
{
        int i;
        int flagall;
        char *resstr;
        int total,totalfail;
        if(argc==1 || strcmp(argv[argc-1],"--help")==0 || argc!=2) {
                printf("Syntax:\n\t%s { --help | --list | test_name | all }\nNOTE: test_name is one of the tests listed in --list\n",argv[0]);
                return(0);
        }
        if(strcmp(argv[argc-1],"--list")==0) {
                for(i=0;i<(sizeof(tests)/sizeof(tests[0]));i++)
                        printf("%s\n\t%s\n",tests[i].test(test_name),tests[i].test(test_description));
                return(0);
        }
        flagall=(strcmp(argv[argc-1],"all")==0)?1:0;
        for(total=totalfail=0,i=0;i<(sizeof(tests)/sizeof(tests[0]));i++) {
                if(!flagall && strcmp(tests[i].test(test_name),argv[argc-1])!=0)
                        continue;
                printf("%20s...",tests[i].test(test_name));
                fflush(stdout);
                resstr=tests[i].test(test_run);
                total++;
                totalfail+=((memcmp(resstr,STRING_OK,strlen(STRING_OK))==0)?0:1);
                printf("%s\n",resstr);

        }
        if(!flagall && i>=(sizeof(tests)/sizeof(tests[0]))) {
                printf("ERROR: test not found\n");
                return(1);
        }
        if(totalfail!=0)
                printf("Failed %i of %i tests.\n",totalfail,total);
        return((totalfail!=0)?1:0);
}

char *
test1(test_action action)
{
        if(action==test_name)
                return("test1");
        else if(action==test_description)
                return("test the testing framework");
        /* run test */
        return("ok");
}