#include "test_common.h"

int
main(int argc, char *argv[])
{
    CK_RV        rc;
    CK_SLOT_INFO sinfo;
    CK_ULONG     i;

    printf("Info test\n");

    for (i = 1; i < (CK_ULONG) argc; i++)
    {
        if (strcmp("-api", argv[i]) == 0)
        {
            ++i;
            api_path = argv[i];
        }
    }
    rc = init(api_path, &funcs, NULL);
    if (rc != CKR_OK)
    {
        printf("init failed\n");
        return rc;
    }

    rc = funcs->C_GetSlotList(CK_FALSE, NULL, &SlotCount);
    if (rc != CKR_OK)
    {
        SlotList = NULL;
        printf("Get slot list result: 0x%lx\n", rc);
        return rc;
    }
    printf("%ld slots found\n", SlotCount);
    if (SlotCount > 0)
    {
        SlotList = (CK_SLOT_ID_PTR) malloc(SlotCount * sizeof(CK_SLOT_ID));
        rc       = funcs->C_GetSlotList(CK_FALSE, SlotList, &SlotCount);
        if (rc != CKR_OK)
        {
            SlotList  = NULL;
            SlotCount = 0;
            printf("Get slot list result: 0x%lx\n", rc);
            return rc;
        }
    }
    else
    {
        SlotList = NULL;
        return CKR_OK;
    }

    printf("Cycle through slot list\n");
    for (i = 0; i < SlotCount; i++)
    {
        SlotId = SlotList[i];
        rc     = display_slot_info(funcs, SlotId);
        if (rc != CKR_OK)
        {
            printf("display slot info failed\n");
            return rc;
        }
        rc = funcs->C_GetSlotInfo(SlotId, &sinfo);
        if (sinfo.flags & CKF_TOKEN_PRESENT)
        {
            rc = display_token_info(funcs, SlotId);
            if (rc != CKR_OK)
            {
                printf("display_token_info failed\n");
                return rc;
            }

            rc = display_mechanisms(funcs, SlotId);
            if (rc != CKR_OK)
            {
                printf("display_mechanisms failed\n");
                return rc;
            }
        }
        else
        {
            printf("Token not present\n");
        }
    }

    if (SlotList)
    {
        free(SlotList);
        SlotList = NULL;
    }

    printf("Info test SUCCESS\n");
    return CKR_OK;
}
