/* unit test for lib/procinfo.c */
#include <unistd.h> /* for getpid() */
#include "config.h"
#include "cunit/unit.h"
#include "lib/procinfo.h"

static void test_init_piarray(void)
{
    piarray_t piarray;

    init_piarray(&piarray);

    CU_ASSERT_EQUAL(piarray.count, 0);
    CU_ASSERT_EQUAL(piarray.alloc, 0);
    CU_ASSERT_PTR_NULL(piarray.data);
}

#if 0
static void not_test_add_procinfo_generic(void)
{
    piarray_t piarray;
    struct proc_info *pinfo;

    init_piarray(&piarray);

    pinfo = add_procinfo_generic(&piarray, 2342, "service", "host",
                                 "user", "mailbox", "command");

    CU_ASSERT_PTR_NOT_NULL(pinfo);

    CU_ASSERT_EQUAL(piarray.count, 1);
    CU_ASSERT((piarray.alloc >= piarray.count));
    CU_ASSERT_PTR_NOT_NULL(piarray.data);

    CU_ASSERT_EQUAL(pinfo->pid, 2342);
    CU_ASSERT_STRING_EQUAL(pinfo->servicename, "service");
    CU_ASSERT_STRING_EQUAL(pinfo->user, "user");
    CU_ASSERT_STRING_EQUAL(pinfo->host, "host");
    CU_ASSERT_STRING_EQUAL(pinfo->mailbox, "mailbox");
    CU_ASSERT_STRING_EQUAL(pinfo->cmdname, "command");
}
#endif

static void test_add_procinfo(void)
{
    piarray_t piarray;
    struct proc_info *pinfo;
    int res;

    init_piarray(&piarray);

    res = add_procinfo(getpid(), "service", "host", "user",
                       "mailbox", "command", &piarray);

    CU_ASSERT_EQUAL(res, 0);

    CU_ASSERT_EQUAL(piarray.count, 1);
    CU_ASSERT((piarray.alloc >= piarray.count));
    CU_ASSERT_PTR_NOT_NULL(piarray.data);

    pinfo = piarray.data[0];

    CU_ASSERT_PTR_NOT_NULL(pinfo);
    CU_ASSERT_EQUAL(pinfo->pid, getpid());
    CU_ASSERT_STRING_EQUAL(pinfo->servicename, "service");
    CU_ASSERT_STRING_EQUAL(pinfo->user, "user");
    CU_ASSERT_STRING_EQUAL(pinfo->host, "host");
    CU_ASSERT_STRING_EQUAL(pinfo->mailbox, "mailbox");
    CU_ASSERT_STRING_EQUAL(pinfo->cmdname, "command");
    CU_ASSERT_STRING_NOT_EQUAL(pinfo->state, "");
    CU_ASSERT_NOT_EQUAL(pinfo->start, 0);
    CU_ASSERT((pinfo->start <= time(NULL)));
    CU_ASSERT((pinfo->vmsize > 0));

    deinit_piarray(&piarray);
}

static void test_sort_procinfo(void)
{
    piarray_t piarray;
    struct proc_info *pinfo1, *pinfo2, *pinfo3, *pinfo4;
    int res;

    init_piarray(&piarray);

    res = add_procinfo(getpid(), "service1", "host1", "user1",
                       "mailbox1", "command1", &piarray);
    CU_ASSERT_EQUAL(res, 0);
    CU_ASSERT_EQUAL(piarray.count, 1);

    res = add_procinfo(1, "service2", "host2", "user2",
                       "mailbox2", "command2", &piarray);
    CU_ASSERT_EQUAL(res, 0);
    CU_ASSERT_EQUAL(piarray.count, 2);

    res = add_procinfo(getpid(), "service1", "host1", "user1",
                       "mailbox1", "command1", &piarray);
    CU_ASSERT_EQUAL(res, 0);
    CU_ASSERT_EQUAL(piarray.count, 3);

    res = add_procinfo(1, "service2", "host2", "user2",
                       "mailbox2", "command2", &piarray);
    CU_ASSERT_EQUAL(res, 0);
    CU_ASSERT_EQUAL(piarray.count, 4);

    pinfo1 = piarray.data[0];
    pinfo2 = piarray.data[1];

    snprintf(pinfo1->state, sizeof(pinfo1->state), "%s", "running1");
    snprintf(pinfo2->state, sizeof(pinfo2->state), "%s", "running2");
    pinfo1->start = 1;
    pinfo2->start = 2;
    pinfo1->vmsize = 1;
    pinfo2->vmsize = 2;

    CU_ASSERT((sort_procinfo QSORT_R_COMPAR_ARGS(&pinfo1, &pinfo2, (void *) "p") < 0));
    CU_ASSERT((sort_procinfo QSORT_R_COMPAR_ARGS(&pinfo1, &pinfo2, (void *) "s") > 0));
    CU_ASSERT((sort_procinfo QSORT_R_COMPAR_ARGS(&pinfo1, &pinfo2, (void *) "q") > 0));
    CU_ASSERT((sort_procinfo QSORT_R_COMPAR_ARGS(&pinfo1, &pinfo2, (void *) "t") > 0));
    CU_ASSERT((sort_procinfo QSORT_R_COMPAR_ARGS(&pinfo1, &pinfo2, (void *) "v") > 0));
    CU_ASSERT((sort_procinfo QSORT_R_COMPAR_ARGS(&pinfo1, &pinfo2, (void *) "h") > 0));
    CU_ASSERT((sort_procinfo QSORT_R_COMPAR_ARGS(&pinfo1, &pinfo2, (void *) "u") > 0));
    CU_ASSERT((sort_procinfo QSORT_R_COMPAR_ARGS(&pinfo1, &pinfo2, (void *) "r") > 0));
    CU_ASSERT((sort_procinfo QSORT_R_COMPAR_ARGS(&pinfo1, &pinfo2, (void *) "c") > 0));

    CU_ASSERT((sort_procinfo QSORT_R_COMPAR_ARGS(&pinfo1, &pinfo2, (void *) "P") > 0));
    CU_ASSERT((sort_procinfo QSORT_R_COMPAR_ARGS(&pinfo1, &pinfo2, (void *) "S") < 0));
    CU_ASSERT((sort_procinfo QSORT_R_COMPAR_ARGS(&pinfo1, &pinfo2, (void *) "Q") < 0));
    CU_ASSERT((sort_procinfo QSORT_R_COMPAR_ARGS(&pinfo1, &pinfo2, (void *) "T") < 0));
    CU_ASSERT((sort_procinfo QSORT_R_COMPAR_ARGS(&pinfo1, &pinfo2, (void *) "V") < 0));
    CU_ASSERT((sort_procinfo QSORT_R_COMPAR_ARGS(&pinfo1, &pinfo2, (void *) "H") < 0));
    CU_ASSERT((sort_procinfo QSORT_R_COMPAR_ARGS(&pinfo1, &pinfo2, (void *) "U") < 0));
    CU_ASSERT((sort_procinfo QSORT_R_COMPAR_ARGS(&pinfo1, &pinfo2, (void *) "R") < 0));
    CU_ASSERT((sort_procinfo QSORT_R_COMPAR_ARGS(&pinfo1, &pinfo2, (void *) "C") < 0));

    pinfo3 = piarray.data[2];
    pinfo4 = piarray.data[3];

    snprintf(pinfo3->state, sizeof(pinfo3->state), "%s", "running1");
    snprintf(pinfo4->state, sizeof(pinfo4->state), "%s", "running2");
    pinfo3->start = 1;
    pinfo4->start = 2;
    pinfo3->vmsize = 1;
    pinfo4->vmsize = 2;

    CU_ASSERT((sort_procinfo QSORT_R_COMPAR_ARGS(&pinfo1, &pinfo3, (void *) "p") == 0));
    CU_ASSERT((sort_procinfo QSORT_R_COMPAR_ARGS(&pinfo1, &pinfo3, (void *) "s") == 0));
    CU_ASSERT((sort_procinfo QSORT_R_COMPAR_ARGS(&pinfo1, &pinfo3, (void *) "q") == 0));
    CU_ASSERT((sort_procinfo QSORT_R_COMPAR_ARGS(&pinfo1, &pinfo3, (void *) "t") == 0));
    CU_ASSERT((sort_procinfo QSORT_R_COMPAR_ARGS(&pinfo1, &pinfo3, (void *) "v") == 0));
    CU_ASSERT((sort_procinfo QSORT_R_COMPAR_ARGS(&pinfo1, &pinfo3, (void *) "h") == 0));
    CU_ASSERT((sort_procinfo QSORT_R_COMPAR_ARGS(&pinfo1, &pinfo3, (void *) "u") == 0));
    CU_ASSERT((sort_procinfo QSORT_R_COMPAR_ARGS(&pinfo1, &pinfo3, (void *) "r") == 0));
    CU_ASSERT((sort_procinfo QSORT_R_COMPAR_ARGS(&pinfo1, &pinfo3, (void *) "c") == 0));

    CU_ASSERT((sort_procinfo QSORT_R_COMPAR_ARGS(&pinfo2, &pinfo4, (void *) "P") == 0));
    CU_ASSERT((sort_procinfo QSORT_R_COMPAR_ARGS(&pinfo2, &pinfo4, (void *) "S") == 0));
    CU_ASSERT((sort_procinfo QSORT_R_COMPAR_ARGS(&pinfo2, &pinfo4, (void *) "Q") == 0));
    CU_ASSERT((sort_procinfo QSORT_R_COMPAR_ARGS(&pinfo2, &pinfo4, (void *) "T") == 0));
    CU_ASSERT((sort_procinfo QSORT_R_COMPAR_ARGS(&pinfo2, &pinfo4, (void *) "V") == 0));
    CU_ASSERT((sort_procinfo QSORT_R_COMPAR_ARGS(&pinfo2, &pinfo4, (void *) "H") == 0));
    CU_ASSERT((sort_procinfo QSORT_R_COMPAR_ARGS(&pinfo2, &pinfo4, (void *) "U") == 0));
    CU_ASSERT((sort_procinfo QSORT_R_COMPAR_ARGS(&pinfo2, &pinfo4, (void *) "R") == 0));
    CU_ASSERT((sort_procinfo QSORT_R_COMPAR_ARGS(&pinfo2, &pinfo4, (void *) "C") == 0));

    deinit_piarray(&piarray);
}
