#include "otp_corec32.h"
#include <time.h>
#include <stdio.h>

#ifndef NULL
#define NULL 0
#endif
int main()
{
	unsigned char text[8] = {0};
	unsigned int nchallenge;
	char pszpwd[6];
	char sztime[MAX_DATEBUF_LEN+1] = {0};
	time_t lt;
	struct tm *tm1, *tm2;
	char psztm[30] = {0};
	
	char sztar[] = "./tktest.uinf";
	char szuser[] = "./usertest.uinf";
	char szplain[] = "./002.uinf";
	int ret = secuotp_initenv("./otp_conf.con");
    char *pserr = NULL;
	int i = 0;
    char *pszerr;
    char szusername[30];
    char szotppin[30];
    char szotp1[30];
    char szotp2[30];
    printf("\n******Authentication test******\n");
    fflush(0);
    printf("Please input the UniOTP username:");
    scanf("%s", szusername);
    printf("Please input the OTP[PIN]:");
    fflush(0);
    scanf("%s", szotppin);

    if(0 == (ret = secuotp_auth(szusername, szotppin, &nchallenge)))
    {
        printf("auth successfully!\n");
    }
    else
    {
        printf("auth failed!\n");
    }
    
    printf("\n******Synchronize test******\n");
    fflush(0);
    printf("Please input the UniOTP username for UniOTP synchronize:");
    scanf("%s", szusername);
    
    printf("Please input a OTP:");
    fflush(0);
    scanf("%s", szotp1);
    printf("Please input a second OTP:");
    fflush(0);
    scanf("%s", szotp2);

    if(0 == (ret=secuotp_synchronize(szusername, szotp1, szotp2)))
    {
        printf("synchronize successfully\n");
    }
    else
    {
        printf("synchronize failed\n");
	}
    
    printf("\n******Tokenimport test******\n");

    ret = secuotp_tokenimport(sztar, "1q2w3e4r5t6y7u8i", &pszerr);
    if (SECUOTP_ERROR_SUCCESS != ret)
    {
        printf("token import failed.Error information:\n%s", pszerr);
    }
    //call secuotp_free to safe free buffer allocated in the interal of API
    secuotp_free((void **)&pszerr);
    printf("\n******Rebind test******\n");
	ret = secuotp_rebind("test", "secu2011053101");
	if (SECUOTP_ERROR_SUCCESS == ret)
	{
		printf("rebind user successfully!\n");
	}
	else
	{
		printf("rebind user failed.#Error code:%d\n", ret);
	}
/* userimport
    printf("\n****User import test******\n");
    ret = secuotp_userimport(sztar, "1234567890123456", &pserr);
    if (ret == SECUOTP_ERROR_CONNECTDBFAILED)
    {
        printf("\nloop count:%d\n", i);
        system("pause");
    }
    if(SECUOTP_ERROR_SUCCESS_WITHINFO == ret)
    {
        printf("%s\n", pserr);
        if (pserr)
        {
            secuotp_free(&pserr);
            pserr = NULL;
        }
    }
    else if (SECUOTP_ERROR_SUCCESS != ret)
    {
        printf("user import failed.#Errorcode:%d\n", ret);
    }
*///user import
	
	if (SECUOTP_ERROR_SUCCESS == secuotp_makelog(20, NULL, 23, "makelog test here!", "192.168.1.211"))
	{
		printf("make log successfully!\n");
	}
	else
	{
		printf("make log failed!\n");
	}
	return 0;
	if (0 == secuotp_tokendelete("1000000007"))
	{
		printf("delete token successfully!\n");
	}
	else
	{
		printf("delete token failed!\n");
	}
	if (0 == secuotp_adduser("test2", "123456789098", "15947115102", "1000000008", NULL, "yangpei2sktbj.com", NULL, "hello world", NULL, 2))
	{
		printf("add user successfully!\n");
	}
	else
	{
		printf("add user failed!\n");
	}

	if (0 == secuotp_setuserlock("yangd", 0))
	{
		printf("user lock successfully!\n");
	}
	else
	{
		printf("user lock failed!\n");
	}

	if (0 == secuotp_setuserinvalid("yang", 0))
	{
		printf("set user invalid success!\n");
	}
	else
	{
		printf("set user invalid failed!\n");
	}
	
	if (0 == secuotp_deleteuser("test2"))
	{
		printf("delete user success!\n");
	}
	else
	{
		printf("delete user failed!\n");
	}
	if (SECUOTP_ERROR_INVALID_AUTHTYPE == secuotp_setauthtype("yang", 20))
	{
		printf("invalid auth type!\n");
	}
	else
	{
		printf("change authtype failed!\n");
	}
	if (0 == secuotp_setuserpin("test6", "123456"))
	{
		printf("update pin successfully!\n");
	}
	else
	{
		printf("update pin failed!\n");
	}
	if (0 == secuotp_resetpin("test6", "123456", "234567"))
	{
		printf("reset pin successfully!\n");
	}
	else
	{
		printf("reset pin failed!\n");
	}
	return 0;
}
