package com.mtoken.gm3000;

import com.yumawk.mToken;
import org.junit.Test;

public class TestToken {

    @Test
    public void TestGetUserName() {
        String userName;
        mToken token = new mToken();
        userName = token.GetUserList();
        if (userName == null || userName.isEmpty()) {
            System.out.println(token.GetLastError());
        } else {
            System.out.println("用户名称：" + userName);
        }
    }

    @Test
    public void TestSignData() {
        String userName = "";
        String signdata = "";
        String cert = "";

        //1.初始化组件
        mToken token = new mToken();

        //2.获取用户列表
        userName = token.GetUserList();
        if (userName == null || userName.isEmpty()) {
            System.out.println(token.GetLastError());
            return;
        }

        //3.签名数据，为方便演示，这里数据为“Hello"
        signdata = token.SignData(userName, "123456", "你好");
        cert = token.exportCert(userName);

        //4.导出签名证书,用于验证签名
        if (signdata == null || signdata.isEmpty() || cert == null || cert.isEmpty()) {
            System.out.println(token.GetLastError());
        } else {
            System.out.println("签名数据：" + signdata);
            System.out.println("签名证书：" + cert);
        }
    }


    @Test
    public void TestGetInfo() {
        String userName = "";
        String certSerialNumber = "";
        String certSubject = "";
        String deviceSerialNumber = "";
        String certBeforeTime = "";
        String certValidTime = "";

        //1.初始化组件
        mToken token = new mToken();

        //2.获取用户列表
        userName = token.GetUserList();
        if (userName == null || userName.isEmpty()) {
            System.out.println(token.GetLastError());
            return;
        }

        //3.获取证书序列号
        certSerialNumber = token.GetCertSerialNumber(userName);
        if (certSerialNumber == null || certSerialNumber.isEmpty()) {
            System.out.println(token.GetLastError());
            return;
        }

        //4.获取证书使用者信息
        certSubject = token.GetCertSubject(userName);
        if (certSubject == null || certSubject.isEmpty()) {
            System.out.println(token.GetLastError());
            return;
        }

        //5.获取设备序号
        deviceSerialNumber = token.GetDeviceSerialNumber(userName);
        if (deviceSerialNumber == null || deviceSerialNumber.isEmpty()) {
            System.out.println(token.GetLastError());
            return;
        }

        //6.获取证书生效时间
        certBeforeTime = token.GetCertNotBeforeTime(userName);
        if (certBeforeTime == null || certBeforeTime.isEmpty()) {
            System.out.println(token.GetLastError());
            return;
        }

        //7.获取证书过期时间
        certValidTime = token.GetCertValidTime(userName);
        if (certValidTime == null || certValidTime.isEmpty()) {
            System.out.println(token.GetLastError());
            return;
        }

        System.out.println("证书序号：" + certSerialNumber);
        System.out.println("设备序号：" + deviceSerialNumber);
        System.out.println("证书信息：" + certSubject);
        System.out.println("生效时间：" + certBeforeTime);
        System.out.println("过期时间：" + certValidTime);
    }

}
