package cn.com.shudun.svs;

import com.shudun.svs.entity.LeeResult;
import com.shudun.svs.enums.AsymAlgoEnum;
import com.shudun.svs.enums.AsymAlgoKeyEnum;
import com.shudun.svs.enums.MessageAlgoEnum;
import com.shudun.svs.enums.PAddingEnum;
import com.shudun.svs.enums.SymAlgoEnum;
import com.shudun.svs.properties.CertDefine;
import com.shudun.svs.properties.ErrCode;
import com.shudun.svs.properties.SingDefine;
import com.shudun.svs.qrcode.QRCodeUtil;
import com.shudun.svs.service.IShudunCSPAPI;
import com.shudun.svs.service.ShudunCSPAPIBeanFactory;
import com.shudun.svs.utils.CommonUtil;
import common.utils.CertTools;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.cert.X509Certificate;
import java.util.UUID;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

/**
 * ShudunCSPAPI Tester.
 *
 * @author Peter
 * @version 1.0
 * @since 2022-12-30
 */
public class ShudunCSPAPITest {
    private final static String CONFIGFILE = "config/cfg.xml";

    //多包签名的待签名文件
    private static String filePath = "file/svs_test_file.txt";
    //XML签名的代签名文件
    private static String xmlFilePath = "file/svs_test_file.xml";

    //pdf签名、添加图片所用文件
    private static String sourcePdfPath = "file/GMT 0006-2012 密码应用标识规范.pdf";
    private static String tarPdfPath = "file/GMT 0006-2012 密码应用标识规范1.pdf";
    private static String imgPath = "file/logo.png";

    //二维码签名后，二维码图片输出路径
    private static String qrImgPath = "file/";
    //Jar包签名的待签名Jar包
    private static String jarPath = "D:\\FineAgent2.jar";
    //APK包签名的待签名APK包
    private static String apkPath = "D:\\app-debug.apk";
    //APK包签名后的APK包存储路径，可与代签APK路径相同，覆盖原APK
    private static String apkOutPath = "D:\\app-debug2.apk";

    private static String inData = "1";
    private IShudunCSPAPI sureCSPAPI = ShudunCSPAPIBeanFactory.getCSPAPIBean();

    private static String keyValue = "1234qwer";

    private static String cerId = "1";
    private static int keyIndex = 1;
    private static String certSN; //自动补全
    private static String cert;   //自动补全

    private static String rsa_cerId = "3";
    private static int rsa_key_index = 3;
    private static String rsa_cert;   //自动补全
    private static String rsa_cert_certNo;   //自动补全

    private static String publicKey;//自动补全
    private static String publicKey_rsa;//自动补全
    private static X509Certificate x509Cert;//自动补全
    private static X509Certificate x509Cert_rsa;//自动补全

    private static int[] algIds = new int[]{SingDefine.SGD_SM3_SM2, SingDefine.SGD_SM3_RSA, SingDefine.SGD_SHA1_RSA, SingDefine.SGD_SHA224_RSA, SingDefine.SGD_SHA256_RSA, SingDefine.SGD_SHA384_RSA, SingDefine.SGD_SHA512_RSA};
    private static int[] keyIndexs = new int[]{keyIndex, rsa_key_index, rsa_key_index, rsa_key_index, rsa_key_index, rsa_key_index, rsa_key_index};
    private static String[] certs;
    private static String[] certSNs;
    private static String[] publicKeys;
    private static X509Certificate[] x509Certs;

    @Before
    public void before() throws Exception {
        // 建立客户端初始化
        LeeResult leeResult = sureCSPAPI.init(CONFIGFILE);
        if (cert == null) {
            if (ErrCode.GM_SUCCESS != leeResult.getRespValue()) {
                System.out.println("init error");
                System.exit(0);
            }
            cerId = CertTools.encodeToString(cerId.getBytes());
            LeeResult leeResult2 = sureCSPAPI.exportCert(cerId);
            cert = leeResult2.getCert();
            LeeResult leeResult1 = sureCSPAPI.parseCert(8, cert);
            LeeResult leeResult4 = sureCSPAPI.parseCert(2, cert);
            certSN = leeResult4.getInfo();
            publicKey = leeResult1.getInfo();

            inData = CertTools.encodeToString(inData.getBytes());
            keyValue = CertTools.encodeToString(keyValue.getBytes());
            x509Cert = CertTools.generateCertificate(cert);

            rsa_cerId = CertTools.encodeToString(rsa_cerId.getBytes());
            LeeResult rsaLeeResult = sureCSPAPI.exportCert(rsa_cerId);
            rsa_cert = rsaLeeResult.getCert();
            LeeResult leeResult6 = sureCSPAPI.parseCert(8, rsa_cert);
            LeeResult leeResult5 = sureCSPAPI.parseCert(2, rsa_cert);
            rsa_cert_certNo = leeResult5.getInfo();

            publicKey_rsa = leeResult6.getInfo();
            x509Cert_rsa = CertTools.generateCertificate(rsa_cert);

            certs = new String[]{cert, rsa_cert, rsa_cert, rsa_cert, rsa_cert, rsa_cert, rsa_cert};
            certSNs = new String[]{certSN, rsa_cert_certNo, rsa_cert_certNo, rsa_cert_certNo, rsa_cert_certNo, rsa_cert_certNo, rsa_cert_certNo};
            publicKeys = new String[]{publicKey, publicKey_rsa, publicKey_rsa, publicKey_rsa, publicKey_rsa, publicKey_rsa, publicKey_rsa};
            x509Certs = new X509Certificate[]{x509Cert, x509Cert_rsa, x509Cert_rsa, x509Cert_rsa, x509Cert_rsa, x509Cert_rsa, x509Cert_rsa};
        }
    }

    @After
    public void after() throws Exception {
    }

    /**
     * Method: init(String cfgFile)
     */
    @Test
    public void testInitCfgFile() throws Exception {
        LeeResult leeResult = sureCSPAPI.init(CONFIGFILE);
        assertEquals(leeResult.getRespValue(), 0);

    }

    /**
     * Method: init(InputStream input)
     */
    @Test
    public void testInitInput() throws Exception {
        LeeResult leeResult = sureCSPAPI.init(new FileInputStream(CONFIGFILE));
        assertEquals(leeResult.getRespValue(), 0);
    }

    /**
     * Method: exportCert(String identification)
     */
    @Test
    public void testExportCertIdentification() throws Exception {
//        for (int i = 0; i < 100; i++) {
            LeeResult leeResult = sureCSPAPI.exportCert(cerId);
            assertEquals(leeResult.getRespValue(), 0);
            assertNotNull(leeResult.getCert());
//        }
    }

    /**
     * Method: exportCert(int signMethod)
     */
    @Test
    public void testExportCertSignMethod() throws Exception {
        LeeResult leeResult = sureCSPAPI.exportCert(SingDefine.SGD_SM3_SM2);
        assertEquals(leeResult.getRespValue(), 0);
        assertNotNull(leeResult.getCert());
    }

    /**
     * Method: parseCert(int infoType, String cert)
     */
    @Test
    public void testParseCert() throws Exception {
        LeeResult leeResult = sureCSPAPI.parseCert(
                CertDefine.SGD_CERT_SERIAL,
                "MIIBxjCCAWugAwIBAgIBSjAKBggqgRzPVQGDdTBNMRwwGgYDVQQDDBNTTTIgSW50ZXJtZWRpYXRlIENBMQ8wDQYDVQQLDAZTSFVEVU4xDzANBgNVBAoMBlNIVURVTjELMAkGA1UEBhMCQ04wHhcNMjEwNzIxMDg1NjQzWhcNNDEwNzE2MDg1NjQzWjAeMQ8wDQYDVQQDDAZJTkRFWDExCzAJBgNVBAYTAkNOMFkwEwYHKoZIzj0CAQYIKoEcz1UBgi0DQgAET0NU2/5HKhTFcUhcuCsb4iBAnmwmqa7wVEbC7fLowV24f2uvXvEQET7f663rEn1PWjrU4+EEx4Hfyvyz5tJEN6NrMGkwHQYDVR0OBBYEFL3/Y1xrsW0s1pKP4q/0QdvByPSSMB8GA1UdIwQYMBaAFDLJt8qhlJmq5S6dU+k9Bm08haxSMAwGA1UdEwEB/wQCMAAwDgYDVR0PAQH/BAQDAgO4MAkGA1UdJQQCMAAwCgYIKoEcz1UBg3UDSQAwRgIhAM1Nx48Bka/U9ooPOfWZeXTzFjpMlx1Eo4CvuAdu+wwoAiEAwQlyIDk/m6GfNFDlb/a8wAWVXMTIUx3Yg27/c2g6OxU=");
        assertEquals(leeResult.getRespValue(), 0);
        assertEquals(leeResult.getInfo(), "NGE=");
    }

    /**
     * Method: validataCert(String cert, boolean ocsp)
     */
    @Test
    public void testValidataCert() throws Exception {
        LeeResult leeResult = sureCSPAPI.validataCert(
                "MIIB2DCCAXygAwIBAgIIPpsAAAAAAKQwDAYIKoEcz1UBg3UFADBeMQswCQYDVQQG" +
                        "EwJDTjEnMCUGA1UECgwe5YyX5Lqs5pWw55u+56eR5oqA5pyJ6ZmQ5YWs5Y+4MRIw" +
                        "EAYDVQQLDAnnoJTlj5Hpg6gxEjAQBgNVBAMMCXNodWR1bnNtMjAeFw0yMjA1MTcx" +
                        "NjAwMDBaFw0yNzA3MjYxNTU5NTlaMBkxFzAVBgNVBAMMDnNodWR1bnRlc3RvY3Nw" +
                        "MFkwEwYHKoZIzj0CAQYIKoEcz1UBgi0DQgAEtN0GCkOIuiHVxaULvna25rVUXpnR" +
                        "9bkWU3nUksAGFjco6430Sr01Ev4xubzISAEEF/A/mZB6uMnWrkt97WAaIaNnMGUw" +
                        "DgYDVR0PAQH/BAQDAgDAMBMGA1UdJQQMMAoGCCsGAQUFBwMBMB8GA1UdIwQYMBaA" +
                        "FN5yvhBDcHRZnit/JQ+1q1LvY+gnMB0GA1UdDgQWBBRwLy2xA1cjNgBWG7J5FpRE" +
                        "yRS/BTAMBggqgRzPVQGDdQUAA0gAMEUCIQCcsHIgDHZXFrxlWnhbG3N5Z2Wg3deB" +
                        "TodPDZLpsnwdKAIgKVnVsRE+aUCsQp6OaeUpdTAkGPcKOhmVHtJz9lf66x8=",
                true);
        System.out.println(leeResult);
        assertEquals(leeResult.getRespValue(), 0);
    }

    /**
     * Method: signData(int signMethod, String inData)
     */
    @Test
    public void testSignDataForSignMethodInData() throws Exception {
        for (int i = 0; i < algIds.length; i++) {
            LeeResult leeResult = sureCSPAPI.signData(algIds[i], inData);
            assertEquals(leeResult.getRespValue(), 0);
            assertNotNull(leeResult.getSignature());
        }
    }

    /**
     * Method: signData(int signMethod, int keyIndex, String keyValue, String inData)
     */
    @Test
    public void testSignDataForSignMethodKeyIndexKeyValueInData() throws Exception {
        for (int i = 0; i < algIds.length; i++) {
            LeeResult leeResult = sureCSPAPI.signData(algIds[i], keyIndexs[i], keyValue, inData);
            assertEquals(leeResult.getRespValue(), 0);
            assertNotNull(leeResult.getSignature());
        }

    }

    /**
     * Method: verifySigned(int signMethod, String inData, String signature, int verifyLevel)
     */
    @Test
    public void testVerifySignedForSignMethodInDataSignatureVerifyLevel() throws Exception {
        for (int i = 0; i < algIds.length; i++) {
            LeeResult leeResult = sureCSPAPI.signData(SingDefine.SGD_SM3_SM2, inData);
            String signature = leeResult.getSignature();
            LeeResult leeResult1 = sureCSPAPI.verifySigned(SingDefine.SGD_SM3_SM2, inData, signature, -1);
            assertEquals(leeResult1.getRespValue(), 0);
        }
    }

    /**
     * Method: verifySignedDataByCertSN(int signMethod, String certSN, String inData, String signature, int verifyLevel)
     */
    @Test
    public void testVerifySignedDataByCertSNForSignMethodCertSNInDataSignatureVerifyLevel() throws Exception {

        for (int i = 0; i < algIds.length; i++) {
            LeeResult leeResult = sureCSPAPI.signData(algIds[i], inData);
            String cerN = "";
            String signature = leeResult.getSignature();
            if (SingDefine.SGD_SM3_SM2 == algIds[i]) {
                cerN = certSN;
            } else {
                cerN = rsa_cert_certNo;
            }
            LeeResult leeResult1 = sureCSPAPI.verifySignedDataByCertSN(cerN, inData, signature, -1);
            assertEquals(leeResult1.getRespValue(), 0);
        }
    }

    /**
     * Method: verifySignedDataByCert(int signMethod, String cert, String inData, String signature, int verifyLevel)
     */
    @Test
    public void testVerifySignedDataByCertForSignMethodCertInDataSignatureVerifyLevel() throws Exception {
        for (int i = 0; i < algIds.length; i++) {
            LeeResult leeResult = sureCSPAPI.signData(algIds[i], inData);
            String signature = leeResult.getSignature();
            LeeResult leeResult1 = sureCSPAPI.verifySignedDataByCert(certs[i], inData, signature, -1);
            assertEquals(leeResult1.getRespValue(), 0);
        }
    }

    /**
     * Method: verifySignedDataByX509Cert(int signMethod, X509Certificate x509Cert, String inData, String signature, int verifyLevel)
     */
    @Test
    public void testVerifySignedDataByX509CertForSignMethodX509CertInDataSignatureVerifyLevel() throws Exception {
        for (int i = 0; i < algIds.length; i++) {
            LeeResult leeResult = sureCSPAPI.signData(algIds[i], inData);
            String signature = leeResult.getSignature();
            LeeResult leeResult1 = sureCSPAPI.verifySignedDataByX509Cert(x509Certs[i], inData, signature, -1);
            assertEquals(leeResult1.getRespValue(), 0);
        }
    }

    /**
     * Method: signDataMulti(int signMethod, String filePath)
     */
    @Test
    public void testSignDataMultiForSignMethodFilePath() throws Exception {
        for (int i = 0; i < algIds.length; i++) {
            LeeResult leeResult = sureCSPAPI.signDataMulti(algIds[i], filePath);
            String signature = leeResult.getSignature();
            assertEquals(leeResult.getRespValue(), 0);
            assertNotNull(leeResult.getSignature());
        }
    }

    /**
     * Method: signDataMulti(int signMethod, InputStream input)
     */
    @Test
    public void testSignDataMultiForSignMethodInput() throws Exception {
        for (int i = 0; i < algIds.length; i++) {
            LeeResult leeResult = sureCSPAPI.signDataMulti(algIds[i], new FileInputStream(filePath));
            String signature = leeResult.getSignature();
            assertEquals(leeResult.getRespValue(), 0);
            assertNotNull(leeResult.getSignature());
        }
    }

    /**
     * Method: signDataMulti(int signMethod, String signerPublicKey, String filePath, int keyIndex, String keyValue)
     */
    @Test
    public void testSignDataMultiForSignMethodSignerPublicKeyFilePathKeyIndexKeyValue() throws Exception {
        for (int i = 0; i < algIds.length; i++) {
            LeeResult leeResult = sureCSPAPI.signDataMulti(algIds[i], publicKeys[i], filePath, keyIndexs[i], keyValue);
            String signature = leeResult.getSignature();
            assertEquals(leeResult.getRespValue(), 0);
            assertNotNull(leeResult.getSignature());
        }
    }

    /**
     * Method: signDataMulti(int signMethod, String signerPublicKey, InputStream input, int keyIndex, String keyValue)
     */
    @Test
    public void testSignDataMultiForSignMethodSignerPublicKeyInputKeyIndexKeyValue() throws Exception {
        for (int i = 0; i < algIds.length; i++) {
            LeeResult leeResult = sureCSPAPI.signDataMulti(algIds[i], publicKeys[i], new FileInputStream(filePath), keyIndexs[i], keyValue);
            String signature = leeResult.getSignature();
            assertEquals(leeResult.getRespValue(), 0);
            assertNotNull(leeResult.getSignature());
        }
    }

    /**
     * Method: verifySignedMulti(int signMethod, String filePath, String signature, int verifyLevel)
     */
    @Test
    public void testVerifySignedMultiForSignMethodFilePathSignatureVerifyLevel() throws Exception {
        for (int i = 0; i < algIds.length; i++) {
            LeeResult leeResult = sureCSPAPI.signDataMulti(algIds[i], filePath);
            String signature = leeResult.getSignature();
            LeeResult rleeResult = sureCSPAPI.verifySignedMulti(algIds[i], filePath, signature, -1);
            assertEquals(rleeResult.getRespValue(), 0);
        }
    }

    /**
     * Method: verifySignedMulti(int signMethod, InputStream input, String signature, int verifyLevel)
     */
    @Test
    public void testVerifySignedMultiForSignMethodInputSignatureVerifyLevel() throws Exception {
        for (int i = 0; i < algIds.length; i++) {
            LeeResult leeResult = sureCSPAPI.signDataMulti(algIds[i], new FileInputStream(filePath));
            String signature = leeResult.getSignature();
            LeeResult rleeResult = sureCSPAPI.verifySignedMulti(algIds[i], new FileInputStream(filePath), signature, -1);
            assertEquals(rleeResult.getRespValue(), 0);
        }
    }

    /**
     * Method: verifySignedMultiByCertSN(int signMethod, String signerPublicKey, String filePath, String signature, String certSN, int verifyLevel)
     */
    @Test
    public void testVerifySignedMultiByCertSN() throws Exception {
        for (int i = 0; i < algIds.length; i++) {
            LeeResult leeResult = sureCSPAPI.signDataMulti(algIds[i], filePath);
            String signature = leeResult.getSignature();
            LeeResult rleeResult = sureCSPAPI.verifySignedMultiByCertSN(algIds[i], publicKeys[i], filePath, signature, certSNs[i], -1);
            assertEquals(rleeResult.getRespValue(), 0);
        }
    }

    /**
     * Method: verifySignedMultiByCertSn(int signMethod, String signerPublicKey, InputStream input, String signature, String certSN, int verifyLevel)
     */
    @Test
    public void testVerifySignedMultiByCertSn() throws Exception {
        for (int i = 0; i < algIds.length; i++) {
            LeeResult leeResult = sureCSPAPI.signDataMulti(algIds[i], filePath);
            String signature = leeResult.getSignature();
            LeeResult rleeResult = sureCSPAPI.verifySignedMultiByCertSn(algIds[i], publicKeys[i], new FileInputStream(filePath), signature, certSNs[i], -1);
            assertEquals(rleeResult.getRespValue(), 0);
        }
    }

    /**
     * Method: verifySignedMultiByCert(int signMethod, String signerPublicKey, String filePath, String cert, String signature, int verifyLevel)
     */
    @Test
    public void testVerifySignedMultiByCertForSignMethodSignerPublicKeyFilePathCertSignatureVerifyLevel() throws Exception {
        for (int i = 0; i < algIds.length; i++) {
            LeeResult leeResult = sureCSPAPI.signDataMulti(algIds[i], new FileInputStream(filePath));
            String signature = leeResult.getSignature();
            LeeResult rleeResult = sureCSPAPI.verifySignedMultiByCert(algIds[i], publicKeys[i], filePath, certs[i], signature, -1);
            assertEquals(rleeResult.getRespValue(), 0);
        }
    }

    /**
     * Method: verifySignedMultiByCert(int signMethod, String signerPublicKey, InputStream input, String cert, String signature, int verifyLevel)
     */
    @Test
    public void testVerifySignedMultiByCertForSignMethodSignerPublicKeyInputCertSignatureVerifyLevel() throws Exception {
        for (int i = 0; i < algIds.length; i++) {
            LeeResult leeResult = sureCSPAPI.signDataMulti(algIds[i], new FileInputStream(filePath));
            String signature = leeResult.getSignature();
            LeeResult rleeResult = sureCSPAPI.verifySignedMultiByCert(algIds[i], publicKeys[i], new FileInputStream(filePath), certs[i], signature, -1);
            assertEquals(rleeResult.getRespValue(), 0);
        }
    }


    /**
     * Method: signMessage(boolean detached, int signMethod, String inData)
     */
    @Test
    public void testSignMessageForDetachedSignMethodInData() throws Exception {
        for (int i = 0; i < algIds.length; i++) {
            LeeResult leeResult = sureCSPAPI.signMessage(true, algIds[i], inData);
            assertEquals(leeResult.getRespValue(), 0);
            assertNotNull(leeResult.getSignedMessage());

            leeResult = sureCSPAPI.signMessage(false, algIds[i], inData);
            assertEquals(leeResult.getRespValue(), 0);
            assertNotNull(leeResult.getSignedMessage());
        }
    }

    /**
     * Method: signDetachedMessage(int signMethod, String inData)
     */
    @Test
    public void testSignDetachedMessageForSignMethodInData() throws Exception {
        for (int i = 0; i < algIds.length; i++) {
            LeeResult leeResult = sureCSPAPI.signDetachedMessage(algIds[i], inData);
            assertEquals(leeResult.getRespValue(), 0);
            assertNotNull(leeResult.getSignedMessage());
        }
    }

    /**
     * Method: signAttachMessage(int signMethod, String inData)
     */
    @Test
    public void testSignAttachMessageForSignMethodInData() throws Exception {
        for (int i = 0; i < algIds.length; i++) {
            LeeResult leeResult = sureCSPAPI.signAttachMessage(algIds[i], inData);
            assertEquals(leeResult.getRespValue(), 0);
            assertNotNull(leeResult.getSignedMessage());
        }
    }

    /**
     * Method: signMessage(boolean detached, int signMethod, int keyIndex, String keyValue, String inData)
     */
    @Test
    public void testSignMessageForDetachedSignMethodKeyIndexKeyValueInData() throws Exception {
        for (int i = 0; i < algIds.length; i++) {
            LeeResult leeResult = sureCSPAPI.signMessage(true, algIds[i], keyIndexs[i], keyValue, inData);  //不带原文
            String signature = leeResult.getSignature();
            assertEquals(leeResult.getRespValue(), 0);
            assertNotNull(leeResult.getSignedMessage());

            LeeResult leeResult1 = sureCSPAPI.signMessage(false, algIds[i], keyIndexs[i], keyValue, inData); //带原文
            assertEquals(leeResult1.getRespValue(), 0);
            assertNotNull(leeResult1.getSignedMessage());
        }
    }

    /**
     * Method: signDetachedMessage(int signMethod, int keyIndex, String keyValue, String inData)
     */
    @Test
    public void testSignDetachedMessageForSignMethodKeyIndexKeyValueInData() throws Exception {
        for (int i = 0; i < algIds.length; i++) {
            LeeResult leeResult = sureCSPAPI.signDetachedMessage(algIds[i], keyIndexs[i], keyValue, inData);
            assertEquals(leeResult.getRespValue(), 0);
            assertNotNull(leeResult.getSignedMessage());
        }
    }

    /**
     * Method: signAttachMessage(int signMethod, int keyIndex, String keyValue, String inData)
     */
    @Test
    public void testSignAttachMessageForSignMethodKeyIndexKeyValueInData() throws Exception {
        for (int i = 0; i < algIds.length; i++) {
            LeeResult leeResult = sureCSPAPI.signAttachMessage(algIds[i], keyIndexs[i], keyValue, inData);
            assertEquals(leeResult.getRespValue(), 0);
            assertNotNull(leeResult.getSignedMessage());
        }
    }

    /**
     * Method: verifySignedMessage(boolean detached, int signMethod, String signature, String inData)
     */
    @Test
    public void testVerifySignedMessage() throws Exception {
        for (int i = 0; i < algIds.length; i++) {
            LeeResult leeResult = sureCSPAPI.signMessage(true, algIds[i], inData);
            String signedMessage = leeResult.getSignedMessage();
            leeResult = sureCSPAPI.verifySignedMessage(true, signedMessage, inData);
            assertEquals(leeResult.getRespValue(), 0);

            leeResult = sureCSPAPI.signMessage(false, algIds[i], inData);
            signedMessage = leeResult.getSignedMessage();
            leeResult = sureCSPAPI.verifySignedMessage(false, signedMessage, "");
            assertEquals(leeResult.getRespValue(), 0);
        }
    }

    /**
     * Method: verifyDetachedSignedMessage(int signMethod, String signature, String inData)
     */
    @Test
    public void testVerifyDetachedSignedMessage() throws Exception {
        for (int i = 0; i < algIds.length; i++) {
            LeeResult leeResult = sureCSPAPI.signMessage(true, algIds[i], inData);
            String signedMessage = leeResult.getSignedMessage();
            leeResult = sureCSPAPI.verifyDetachedSignedMessage(signedMessage, inData);
            assertEquals(leeResult.getRespValue(), 0);
        }
    }

    /**
     * Method: verifyAttachSignedMessage(int signMethod, String signature)
     */
    @Test
    public void testVerifyAttachSignedMessage() throws Exception {
        for (int i = 0; i < algIds.length; i++) {
            LeeResult leeResult = sureCSPAPI.signMessage(false, algIds[i], inData);
            String signedMessage = leeResult.getSignedMessage();
            leeResult = sureCSPAPI.verifyAttachSignedMessage(signedMessage);
            assertEquals(leeResult.getRespValue(), 0);
        }
    }

    /**
     * Method: signMessageMulti(int signMethod, String filePath)
     */
    @Test
    public void testSignMessageMultiForSignMethodFilePath() throws Exception {
        for (int i = 0; i < algIds.length; i++) {
            LeeResult leeResult = sureCSPAPI.signMessageMulti(algIds[i], filePath);
            assertEquals(leeResult.getRespValue(), 0);
        }
    }

    /**
     * Method: signMessageMulti(int signMethod, InputStream input)
     */
    @Test
    public void testSignMessageMultiForSignMethodInput() throws Exception {
        for (int i = 0; i < algIds.length; i++) {
            LeeResult leeResult = sureCSPAPI.signMessageMulti(algIds[i], new FileInputStream(filePath));
            assertEquals(leeResult.getRespValue(), 0);
        }
    }

    /**
     * Method: signMessageMulti(int signMethod, String signerPublicKey, String filePath, int keyIndex, String keyValue)
     */
    @Test
    public void testSignMessageMultiForSignMethodSignerPublicKeyFilePathKeyIndexKeyValue() throws Exception {
        for (int i = 0; i < algIds.length; i++) {
            LeeResult leeResult = sureCSPAPI.signMessageMulti(algIds[i], publicKeys[i], filePath, keyIndexs[i], keyValue);
            assertEquals(leeResult.getRespValue(), 0);
        }
    }

    /**
     * Method: signMessageMulti(int signMethod, String signerPublicKey, InputStream input, int keyIndex, String keyValue)
     */
    @Test
    public void testSignMessageMultiForSignMethodSignerPublicKeyInputKeyIndexKeyValue() throws Exception {
        for (int i = 0; i < algIds.length; i++) {
            LeeResult leeResult = sureCSPAPI.signMessageMulti(algIds[i], publicKeys[i], new FileInputStream(filePath), keyIndexs[i], keyValue);
            assertEquals(leeResult.getRespValue(), 0);
        }
    }

    /**
     * Method: verifyMessageMulti(int signMethod, String signature, String filePath)
     */
    @Test
    public void testVerifyMessageMultiForSignMethodSignatureFilePath() throws Exception {
        for (int i = 0; i < algIds.length; i++) {
            LeeResult leeResult = sureCSPAPI.signMessageMulti(algIds[i], filePath);
            String signedMessage = leeResult.getSignedMessage();
            leeResult = sureCSPAPI.verifyMessageMulti(algIds[i], signedMessage, new FileInputStream(filePath));
            assertEquals(leeResult.getRespValue(), 0);
        }
    }

    /**
     * Method: verifyMessageMulti(int signMethod, String signature, InputStream input)
     */
    @Test
    public void testVerifyMessageMultiForSignMethodSignatureInput() throws Exception {
        for (int i = 0; i < algIds.length; i++) {
            LeeResult leeResult = sureCSPAPI.signMessageMulti(algIds[i], filePath);
            String signedMessage = leeResult.getSignedMessage();
            leeResult = sureCSPAPI.verifyMessageMulti(algIds[i], signedMessage, new FileInputStream(filePath));
            assertEquals(leeResult.getRespValue(), 0);
        }
    }

    /**
     * Method: verifyMessageMulti(int signMethod, String signerPublicKey, String signature, String filePath)
     */
    @Test
    public void testVerifyMessageMultiForSignMethodSignerPublicKeySignatureFilePath() throws Exception {
        for (int i = 0; i < algIds.length; i++) {
            LeeResult leeResult = sureCSPAPI.signMessageMulti(algIds[i], filePath);
            String signedMessage = leeResult.getSignedMessage();
            leeResult = sureCSPAPI.verifyMessageMulti(algIds[i], publicKeys[i], signedMessage, filePath);
            assertEquals(leeResult.getRespValue(), 0);
        }
    }

    /**
     * Method: verifyMessageMulti(int signMethod, String signerPublicKey, String signature, InputStream input)
     */
    @Test
    public void testVerifyMessageMultiForSignMethodSignerPublicKeySignatureInput() throws Exception {
        for (int i = 0; i < algIds.length; i++) {
            LeeResult leeResult = sureCSPAPI.signMessageMulti(algIds[i], filePath);
            String signedMessage = leeResult.getSignedMessage();
            leeResult = sureCSPAPI.verifyMessageMulti(algIds[i], publicKeys[i], signedMessage, new FileInputStream(filePath));
            assertEquals(leeResult.getRespValue(), 0);
        }
    }

    /**
     * Method: xmlSignMessageCover(int signMethod, int keyIndex, String keyValue, String filePath)
     */
    @Test
    public void testXmlSignMessageCoverForSignMethodKeyIndexKeyValueFilePath() throws Exception {
        for (int i = 0; i < algIds.length; i++) {
            LeeResult leeResult = sureCSPAPI.xmlSignMessageCover(algIds[i], keyIndexs[i], keyValue, xmlFilePath);
            assertEquals(leeResult.getRespValue(), 0);
            assertNotNull(leeResult.getXmlSignedMessage());
            assertNotEquals(leeResult.getXmlSignedMessage().length, 0);
        }

    }

    /**
     * Method: xmlSignMessageCover(int signMethod, int keyIndex, String keyValue, InputStream input)
     */
    @Test
    public void testXmlSignMessageCoverForSignMethodKeyIndexKeyValueInput() throws Exception {
        for (int i = 0; i < algIds.length; i++) {
            LeeResult leeResult = sureCSPAPI.xmlSignMessageCover(algIds[i], keyIndexs[i], keyValue, new FileInputStream(xmlFilePath));//封皮签名
            assertEquals(leeResult.getRespValue(), 0);
            assertNotNull(leeResult.getXmlSignedMessage());
            assertNotEquals(0, leeResult.getXmlSignedMessage().length);
        }
    }

    /**
     * Method: xmlSignMessageCover(int signMethod, String filePath)
     */
    @Test
    public void testXmlSignMessageCoverForSignMethodFilePath() throws Exception {
        for (int i = 0; i < algIds.length; i++) {
            LeeResult leeResult = sureCSPAPI.xmlSignMessageCover(algIds[i], xmlFilePath);//封皮签名
            assertEquals(leeResult.getRespValue(), 0);
            assertNotNull(leeResult.getXmlSignedMessage());
            assertNotEquals(0, leeResult.getXmlSignedMessage().length);
        }
    }

    /**
     * Method: xmlSignMessageCover(int signMethod, InputStream input)
     */
    @Test
    public void testXmlSignMessageCoverForSignMethodInput() throws Exception {
        for (int i = 0; i < algIds.length; i++) {
            LeeResult leeResult = sureCSPAPI.xmlSignMessageCover(algIds[i], new FileInputStream(xmlFilePath));//封皮签名
            assertEquals(leeResult.getRespValue(), 0);
            assertNotNull(leeResult.getXmlSignedMessage());
            assertNotEquals(0, leeResult.getXmlSignedMessage().length);
        }
    }

    /**
     * Method: xmlSignMessageInside(int signMethod, int keyIndex, String keyValue, String filePath)
     */
    @Test
    public void testXmlSignMessageInsideForSignMethodKeyIndexKeyValueFilePath() throws Exception {
        for (int i = 0; i < algIds.length; i++) {
            LeeResult leeResult = sureCSPAPI.xmlSignMessageInside(algIds[i], keyIndexs[i], keyValue, xmlFilePath);//封内签名
            assertEquals(leeResult.getRespValue(), 0);
            assertNotNull(leeResult.getXmlSignedMessage());
            assertNotEquals(0, leeResult.getXmlSignedMessage().length);
        }
    }

    /**
     * Method: xmlSignMessageInside(int signMethod, int keyIndex, String keyValue, InputStream input)
     */
    @Test
    public void testXmlSignMessageInsideForSignMethodKeyIndexKeyValueInput() throws Exception {
        for (int i = 0; i < algIds.length; i++) {
            LeeResult leeResult = sureCSPAPI.xmlSignMessageInside(algIds[i], keyIndexs[i], keyValue, new FileInputStream(xmlFilePath));//封内签名
            assertEquals(leeResult.getRespValue(), 0);
            assertNotNull(leeResult.getXmlSignedMessage());
            assertNotEquals(0, leeResult.getXmlSignedMessage().length);
        }
    }

    /**
     * Method: xmlSignMessageInside(int signMethod, String filePath)
     */
    @Test
    public void testXmlSignMessageInsideForSignMethodFilePath() throws Exception {
        for (int i = 0; i < algIds.length; i++) {
            LeeResult leeResult = sureCSPAPI.xmlSignMessageInside(algIds[i], xmlFilePath);//封内签名
            assertEquals(leeResult.getRespValue(), 0);
            assertNotNull(leeResult.getXmlSignedMessage());
            assertNotEquals(0, leeResult.getXmlSignedMessage().length);
        }
    }

    /**
     * Method: xmlSignMessageInside(int signMethod, InputStream input)
     */
    @Test
    public void testXmlSignMessageInsideForSignMethodInput() throws Exception {
        for (int i = 0; i < algIds.length; i++) {
            LeeResult leeResult = sureCSPAPI.xmlSignMessageInside(algIds[i], new FileInputStream(xmlFilePath));//封内签名
            assertEquals(leeResult.getRespValue(), 0);
            assertNotNull(leeResult.getXmlSignedMessage());
            assertNotEquals(0, leeResult.getXmlSignedMessage().length);
        }
    }

    /**
     * Method: xmlSignMessage(int signMethod, int keyIndex, String keyValue, String filePath)
     */
    @Test
    public void testXmlSignMessageForSignMethodKeyIndexKeyValueFilePath() throws Exception {
        for (int i = 0; i < algIds.length; i++) {
            LeeResult leeResult = sureCSPAPI.xmlSignMessageInside(algIds[i], keyIndexs[i], keyValue, xmlFilePath);//封内签名
            assertEquals(leeResult.getRespValue(), 0);
            assertNotNull(leeResult.getXmlSignedMessage());
            assertNotEquals(0, leeResult.getXmlSignedMessage().length);
        }
    }

    /**
     * Method: xmlSignMessage(int signMethod, int keyIndex, String keyValue, InputStream input)
     */
    @Test
    public void testXmlSignMessageForSignMethodKeyIndexKeyValueInput() throws Exception {
        for (int i = 0; i < algIds.length; i++) {
            LeeResult leeResult = sureCSPAPI.xmlSignMessageInside(algIds[i], keyIndexs[i], keyValue, new FileInputStream(xmlFilePath));//封内签名
            assertEquals(leeResult.getRespValue(), 0);
            assertNotNull(leeResult.getXmlSignedMessage());
            assertNotEquals(0, leeResult.getXmlSignedMessage().length);
        }
    }

    /**
     * Method: xmlSignMessage(int signMethod, String filePath)
     */
    @Test
    public void testXmlSignMessageForSignMethodFilePath() throws Exception {
        for (int i = 0; i < algIds.length; i++) {
            LeeResult leeResult = sureCSPAPI.xmlSignMessageInside(algIds[i], xmlFilePath);
            assertEquals(leeResult.getRespValue(), 0);
            assertNotNull(leeResult.getXmlSignedMessage());
            assertNotEquals(0, leeResult.getXmlSignedMessage().length);
        }
    }

    /**
     * Method: xmlSignMessage(int signMethod, InputStream input)
     */
    @Test
    public void testXmlSignMessageForSignMethodInput() throws Exception {
        for (int i = 0; i < algIds.length; i++) {
            LeeResult leeResult = sureCSPAPI.xmlSignMessageInside(algIds[i], new FileInputStream(xmlFilePath));
            assertEquals(leeResult.getRespValue(), 0);
            assertNotNull(leeResult.getXmlSignedMessage());
            assertNotEquals(0, leeResult.getXmlSignedMessage().length);
        }
    }

    /**
     * Method: xmlSignMessageAttach(int signMethod, int keyIndex, String keyValue, String filePath)
     */
    @Test
    public void testXmlSignMessageAttachForSignMethodKeyIndexKeyValueFilePath() throws Exception {
        for (int i = 0; i < algIds.length; i++) {
            LeeResult leeResult = sureCSPAPI.xmlSignMessageAttach(algIds[i], keyIndexs[i], keyValue, xmlFilePath);
            assertEquals(leeResult.getRespValue(), 0);
            assertNotNull(leeResult.getXmlSignedMessage());
            assertNotEquals(0, leeResult.getXmlSignedMessage().length);
        }
    }

    /**
     * Method: xmlSignMessageAttach(int signMethod, int keyIndex, String keyValue, InputStream input)
     */
    @Test
    public void testXmlSignMessageAttachForSignMethodKeyIndexKeyValueInput() throws Exception {
        for (int i = 0; i < algIds.length; i++) {
            LeeResult leeResult = sureCSPAPI.xmlSignMessageAttach(algIds[i], keyIndexs[i], keyValue, new FileInputStream(xmlFilePath));
            assertEquals(leeResult.getRespValue(), 0);
            assertNotNull(leeResult.getXmlSignedMessage());
            assertNotEquals(0, leeResult.getXmlSignedMessage().length);
        }
    }

    /**
     * Method: xmlSignMessageAttach(int signMethod, String filePath)
     */
    @Test
    public void testXmlSignMessageAttachForSignMethodFilePath() throws Exception {
        for (int i = 0; i < algIds.length; i++) {
            LeeResult leeResult = sureCSPAPI.xmlSignMessageAttach(algIds[i], xmlFilePath);
            assertEquals(leeResult.getRespValue(), 0);
            assertNotNull(leeResult.getXmlSignedMessage());
            assertNotEquals(0, leeResult.getXmlSignedMessage().length);
        }
    }

    /**
     * Method: xmlSignMessageAttach(int signMethod, InputStream input)
     */
    @Test
    public void testXmlSignMessageAttachForSignMethodInput() throws Exception {
        for (int i = 0; i < algIds.length; i++) {
            LeeResult leeResult = sureCSPAPI.xmlSignMessageAttach(algIds[i], new FileInputStream(xmlFilePath));
            assertEquals(leeResult.getRespValue(), 0);
            assertNotNull(leeResult.getXmlSignedMessage());
            assertNotEquals(0, leeResult.getXmlSignedMessage().length);
        }
    }

    /**
     * Method: xmlVerifySignedMessageCover(int signMethod, byte[] xmlSignedMessage)
     */
    @Test
    public void testXmlVerifySignedMessageCover() throws Exception {
        for (int i = 0; i < algIds.length; i++) {
            LeeResult leeResult = sureCSPAPI.xmlSignMessageCover(algIds[i], xmlFilePath);
            byte[] xmlSignedMessage = leeResult.getXmlSignedMessage();
            leeResult = sureCSPAPI.xmlVerifySignedMessageCover(algIds[i], xmlSignedMessage);
            assertEquals(leeResult.getRespValue(), 0);
        }

    }

    /**
     * Method: xmlVerifySignedMessageInside(int signMethod, byte[] xmlSignedMessage)
     */
    @Test
    public void testXmlVerifySignedMessageInside() throws Exception {
        for (int i = 0; i < algIds.length; i++) {
            LeeResult leeResult = sureCSPAPI.xmlSignMessageInside(algIds[i], xmlFilePath);
            byte[] xmlSignedMessage = leeResult.getXmlSignedMessage();
            leeResult = sureCSPAPI.xmlVerifySignedMessageInside(algIds[i], xmlSignedMessage);
            assertEquals(leeResult.getRespValue(), 0);
        }
    }

    /**
     * Method: xmlVerifySignedMessage(int signMethod, String filePath, byte[] xmlSignedMessage)
     */
    @Test
    public void testXmlVerifySignedMessageForSignMethodFilePathXmlSignedMessage() throws Exception {
        for (int i = 0; i < algIds.length; i++) {
            LeeResult leeResult = sureCSPAPI.xmlSignMessage(algIds[i], xmlFilePath);
            byte[] xmlSignedMessage = leeResult.getXmlSignedMessage();
            leeResult = sureCSPAPI.xmlVerifySignedMessage(algIds[i], xmlFilePath, xmlSignedMessage);
            assertEquals(leeResult.getRespValue(), 0);
        }
    }

    /**
     * Method: xmlVerifySignedMessage(int signMethod, InputStream input, byte[] xmlSignedMessage)
     */
    @Test
    public void testXmlVerifySignedMessageForSignMethodInputXmlSignedMessage() throws Exception {
        for (int i = 0; i < algIds.length; i++) {
            LeeResult leeResult = sureCSPAPI.xmlSignMessage(algIds[i], xmlFilePath);
            byte[] xmlSignedMessage = leeResult.getXmlSignedMessage();
            leeResult = sureCSPAPI.xmlVerifySignedMessage(algIds[i], new FileInputStream(xmlFilePath), xmlSignedMessage);
            assertEquals(leeResult.getRespValue(), 0);
        }
    }

    /**
     * Method: xmlVerifySignedMessageAttach(int signMethod, byte[] xmlSignedMessage)
     */
    @Test
    public void testXmlVerifySignedMessageAttach() throws Exception {
        for (int i = 0; i < algIds.length; i++) {
            LeeResult leeResult = sureCSPAPI.xmlSignMessageAttach(algIds[i], xmlFilePath);
            assertEquals(leeResult.getRespValue(), 0);
            byte[] xmlSignedMessage = leeResult.getXmlSignedMessage();
            leeResult = sureCSPAPI.xmlVerifySignedMessageAttach(algIds[i], xmlSignedMessage);
            assertEquals(leeResult.getRespValue(), 0);
        }
    }

    /**
     * Method: pdfSignMessage(int signMethod, String inFilePath, String outFilePath)
     */
    @Test
    public void testPdfSignMessageForSignMethodInFilePathOutFilePath() throws Exception {
        for (int algId : algIds) {
            LeeResult leeResult = sureCSPAPI.pdfSignMessage(algId, sourcePdfPath, tarPdfPath);
            assertEquals(leeResult.getRespValue(), 0);
        }
    }

    /**
     * Method: pdfSignMessage(int signMethod, InputStream input, OutputStream outputStream)
     */
    @Test
    public void testPdfSignMessageForSignMethodInputOutputStream() throws Exception {
        for (int algId : algIds) {
            LeeResult leeResult = sureCSPAPI.pdfSignMessage(algId, new FileInputStream(sourcePdfPath), new FileOutputStream(tarPdfPath));
            assertEquals(leeResult.getRespValue(), 0);
        }
    }

    /**
     * Method: pdfVerifySignedMessage(int signMethod, InputStream input)
     */
    @Test
    public void testPdfVerifySignedMessage() throws Exception {
        for (int algId : algIds) {
            LeeResult leeResult = sureCSPAPI.pdfSignMessage(algId, new FileInputStream(sourcePdfPath), new FileOutputStream(tarPdfPath));
            assertEquals(leeResult.getRespValue(), 0);
            leeResult = sureCSPAPI.pdfVerifySignedMessage(algId, new FileInputStream(tarPdfPath));
            assertEquals(leeResult.getRespValue(), 0);
        }
    }

    /**
     * Method: pdfAddImage(String pdfFilePath, String imageFilePath, String outFilePath, int page, float scale, int x, int y)
     */
    @Test
    public void testPdfAddImageForPdfFilePathImageFilePathOutFilePathPageScaleXY() throws Exception {
        LeeResult leeResult = sureCSPAPI.pdfAddImage(sourcePdfPath, imgPath, tarPdfPath, 0, 1, 0, 0);
        assertEquals(leeResult.getRespValue(), 0);
    }

    /**
     * Method: pdfAddImage(InputStream pdfInput, InputStream imageInput, OutputStream outputStream, int page, float scale, int x, int y)
     */
    @Test
    public void testPdfAddImageForPdfInputImageInputOutputStreamPageScaleXY() throws Exception {
        LeeResult leeResult = sureCSPAPI.pdfAddImage(new FileInputStream(sourcePdfPath), new FileInputStream(imgPath), new FileOutputStream(tarPdfPath), 1, 0.2f, 0, 0);
        assertEquals(leeResult.getRespValue(), 0);
    }

    /**
     * Method: digest(MessageAlgoEnum algoEnum, boolean typeFlag, String publicKey, String signerID, String indata)
     */
    @Test
    public void testDigest() throws Exception {
        for (MessageAlgoEnum messageAlgoEnum : MessageAlgoEnum.values()) {
            LeeResult digest = sureCSPAPI.digest(messageAlgoEnum, true, publicKey, CertTools.encodeToString("1234567812345678".getBytes()), inData);
            assertEquals(digest.getRespValue(), 0);
            assertNotNull(digest.getData());
        }
    }

    /**
     * Method: generateAsymKey(AsymAlgoKeyEnum asymAlgoEnum)
     */
    @Test
    public void testGenerateAsymKey() throws Exception {
        for (AsymAlgoKeyEnum asymAlgoKeyEnum : AsymAlgoKeyEnum.values()) {
            LeeResult leeResult = sureCSPAPI.generateAsymKey(asymAlgoKeyEnum);
            assertEquals(leeResult.getRespValue(), 0);
            assertNotNull(leeResult.getData());
        }
    }

    /**
     * Method: asymEncrypt(String publicKey, String srcData)
     */
    @Test
    public void testAsymEncrypt() throws Exception {
        for (AsymAlgoKeyEnum asymAlgoKeyEnum : AsymAlgoKeyEnum.values()) {
            LeeResult leeResult = sureCSPAPI.generateAsymKey(asymAlgoKeyEnum);
            LeeResult leeResult1 = sureCSPAPI.asymEncrypt(leeResult.getData().split("\\|\\|")[1], inData);
            assertEquals(leeResult1.getRespValue(), 0);
            assertNotNull(leeResult1.getData());
        }
    }

    /**
     * Method: asymDecrypt(String privatekey, String cypherData)
     */
    @Test
    public void testAsymDecrypt() throws Exception {
        for (AsymAlgoKeyEnum asymAlgoKeyEnum : AsymAlgoKeyEnum.values()) {
            LeeResult leeResult = sureCSPAPI.generateAsymKey(asymAlgoKeyEnum);
            LeeResult leeResult1 = sureCSPAPI.asymEncrypt(leeResult.getData().split("\\|\\|")[1], inData);
            assertEquals(leeResult1.getRespValue(), 0);
            LeeResult leeResult2 = sureCSPAPI.asymDecrypt(leeResult.getData().split("\\|\\|")[0], leeResult1.getData());
            assertEquals(leeResult2.getRespValue(), 0);
            assertEquals(leeResult2.getData(), inData);
        }
    }

    /**
     * Method: generateSymKey(SymAlgoEnum symAlgoEnum)
     */
    @Test
    public void testGenerateSymKey() throws Exception {
        for (SymAlgoEnum symAlgoEnum : SymAlgoEnum.values()) {
            LeeResult leeResult = sureCSPAPI.generateSymKey(symAlgoEnum);
            assertEquals(leeResult.getRespValue(), 0);
            assertNotNull(leeResult.getData());
        }
    }

    /**
     * Method: symEncrypt(SymAlgoEnum symAlgoEnum, String symKey, String iv, PAddingEnum pAddingEnum, String srcData)
     */
    @Test
    public void testSymEncrypt() throws Exception {
        String iv8 = "MTIzNDU2Nzg=";
        String iv16 = "MTIzNDU2NzgxMjM0NTY3OA==";
        String iv = "";
        String data;
        for (SymAlgoEnum symAlgoEnum : SymAlgoEnum.values()) {
            if (symAlgoEnum.equals(SymAlgoEnum.SGD_SM1_ECB) || symAlgoEnum.equals(SymAlgoEnum.SGD_SM1_CBC)) {
                continue;
            }
            switch (symAlgoEnum.getIvLen()) {
                case 8:
                    iv = iv8;
                    break;
                case 16:
                    iv = iv16;
                    break;
                default:
                    iv = "";
                    break;
            }
            LeeResult leeResult = sureCSPAPI.generateSymKey(symAlgoEnum);
            for (PAddingEnum paddingEnum : PAddingEnum.values()) {
                if (paddingEnum == PAddingEnum.NO_PADDING) {
                    data = iv16;
                } else {
                    data = inData;
                }
                LeeResult leeResult1 = sureCSPAPI.symEncrypt(symAlgoEnum, leeResult.getData(), iv, paddingEnum, data);
                assertEquals(leeResult1.getRespValue(), 0);
                assertNotNull(leeResult1.getData());
            }
        }
    }

    /**
     * Method: symDecrypt(SymAlgoEnum symAlgoEnum, String symKey, String iv, PAddingEnum pAddingEnum, String cypherData)
     */
    @Test
    public void testSymDecrypt() throws Exception {
        String iv8 = "MTIzNDU2Nzg=";
        String iv16 = "MTIzNDU2NzgxMjM0NTY3OA==";
        String iv = "";
        String data;
        for (SymAlgoEnum symAlgoEnum : SymAlgoEnum.values()) {
            if (symAlgoEnum.equals(SymAlgoEnum.SGD_SM1_ECB) || symAlgoEnum.equals(SymAlgoEnum.SGD_SM1_CBC)) {
                continue;
            }
            switch (symAlgoEnum.getIvLen()) {
                case 8:
                    iv = iv8;
                    break;
                case 16:
                    iv = iv16;
                    break;
                default:
                    iv = "";
                    break;
            }
            LeeResult leeResult = sureCSPAPI.generateSymKey(symAlgoEnum);
            for (PAddingEnum paddingEnum : PAddingEnum.values()) {
                if (paddingEnum == PAddingEnum.NO_PADDING) {
                    data = iv16;
                } else {
                    data = inData;
                }
                LeeResult leeResult1 = sureCSPAPI.symEncrypt(symAlgoEnum, leeResult.getData(), iv, paddingEnum, data);
                assertEquals(leeResult1.getRespValue(), 0);
                LeeResult leeResult2 = sureCSPAPI.symDecrypt(symAlgoEnum, leeResult.getData(), iv, paddingEnum, leeResult1.getData());
                assertEquals(leeResult2.getRespValue(), 0);
                assertEquals(leeResult2.getData(), data);
            }
        }
    }

    /**
     * Method: envelopeEncode(String cert, boolean isGMCert, String srcData)
     */
    @Test
    public void testEnvelopeEncodeForCertIsGMCertSrcData() throws Exception {
        LeeResult leeResult = sureCSPAPI.envelopeEncode(cert, inData);//SM2
        assertEquals(leeResult.getRespValue(), 0);
        assertNotNull(leeResult.getData());

        leeResult = sureCSPAPI.envelopeEncode(rsa_cert, inData);//RSA
        assertEquals(leeResult.getRespValue(), 0);
        assertNotNull(leeResult.getData());
    }

    /**
     * Method: envelopeDecode(boolean isGMCert, int keyIndex, String keyValue, String encodeData)
     */
    @Test
    public void testEnvelopeDecodeForIsGMCertKeyIndexKeyValueEncodeData() throws Exception {
        LeeResult leeResult = sureCSPAPI.envelopeEncode(cert, inData);//SM2
        System.out.println("1:" + leeResult.getData());
        leeResult = sureCSPAPI.envelopeDecode(keyIndex, keyValue, leeResult.getData());
        System.out.println("2:" + leeResult.getData());
        assertEquals(leeResult.getData(), inData);

        leeResult = sureCSPAPI.envelopeEncode(rsa_cert, inData);//RSA
        System.out.println("3:" + leeResult.getData());
        leeResult = sureCSPAPI.envelopeDecode(rsa_key_index, keyValue, leeResult.getData());
        System.out.println("4:" + leeResult.getData());
        assertEquals(leeResult.getData(), inData);
    }

    /**
     * Method: envelopeEncode(SymAlgoEnum symAlgoEnum, AsymAlgoEnum symMethod, String cert, String iv, PAddingEnum paddingEnum, String srcData)
     */
    @Test
    public void testEnvelopeEncodeForSymAlgoEnumSymMethodCertIvPaddingEnumSrcData() throws Exception {
        String iv8 = "MTIzNDU2Nzg=";
        String iv16 = "MTIzNDU2NzgxMjM0NTY3OA==";
        String iv = "";
        String data;
        int i;
        for (SymAlgoEnum symAlgoEnum : SymAlgoEnum.values()) {
            if (symAlgoEnum.equals(SymAlgoEnum.SGD_SM1_ECB) || symAlgoEnum.equals(SymAlgoEnum.SGD_SM1_CBC)) {
                continue;
            }
            switch (symAlgoEnum.getIvLen()) {
                case 8:
                    iv = iv8;
                    break;
                case 16:
                    iv = iv16;
                    break;
                default:
                    iv = "";
                    break;
            }
            for (AsymAlgoEnum asymAlgoEnum : AsymAlgoEnum.values()) {
                switch (asymAlgoEnum) {
                    case SGD_SM2:
                        i = 0;
                        break;
                    default:
                        i = 1;
                        break;
                }
                for (PAddingEnum paddingEnum : PAddingEnum.values()) {
                    if (paddingEnum == PAddingEnum.NO_PADDING) {
                        data = iv16;
                    } else {
                        data = inData;
                    }
                    LeeResult leeResult = sureCSPAPI.envelopeEncode(symAlgoEnum, certs[i], iv, paddingEnum, data);
                    assertEquals(leeResult.getRespValue(), 0);
                    assertNotNull(leeResult.getData());
                }
            }
        }
    }

    /**
     * Method: envelopeDecode(SymAlgoEnum symAlgoEnum, AsymAlgoEnum symMethod, int keyIndex, String keyValue, String iv, PAddingEnum paddingEnum, String encodeData)
     */
    @Test
    public void testEnvelopeDecodeForSymAlgoEnumSymMethodKeyIndexKeyValueIvPaddingEnumEncodeData() throws Exception {
        String iv8 = "MTIzNDU2Nzg=";
        String iv16 = "MTIzNDU2NzgxMjM0NTY3OA==";
        String iv = "";
        String data;
        int i;
        for (AsymAlgoEnum asymAlgoEnum : AsymAlgoEnum.values()) {
            switch (asymAlgoEnum) {
                case SGD_SM2:
                    i = 0;
                    break;
                default:
                    i = 1;
                    break;
            }
            for (SymAlgoEnum symAlgoEnum : SymAlgoEnum.values()) {
                if (symAlgoEnum.equals(SymAlgoEnum.SGD_SM1_ECB) || symAlgoEnum.equals(SymAlgoEnum.SGD_SM1_CBC)) {
                    continue;
                }
                switch (symAlgoEnum.getIvLen()) {
                    case 8:
                        iv = iv8;
                        break;
                    case 16:
                        iv = iv16;
                        break;
                    default:
                        iv = "";
                        break;
                }
                for (PAddingEnum paddingEnum : PAddingEnum.values()) {
                    if (paddingEnum == PAddingEnum.NO_PADDING) {
                        data = iv16;
                    } else {
                        data = inData;
                    }
                    LeeResult leeResult = sureCSPAPI.envelopeEncode(symAlgoEnum, certs[i], iv, paddingEnum, data);//SM2
                    leeResult = sureCSPAPI.envelopeDecode(keyIndexs[i], keyValue, iv, paddingEnum, leeResult.getData());
                    assertEquals(leeResult.getData(), data);
                }
            }
        }
    }

    /**
     * Method: qrCodeSigned(int signMethod, String inData)
     */
    @Test
    public void testQrCodeSignedForSignMethodInData() throws Exception {
        for (int algId : algIds) {
            LeeResult leeResult = sureCSPAPI.qrCodeSigned(algId, inData);
            String data = leeResult.getData();
            byte[] decode = CommonUtil.base64DecodeByte(data);
            FileOutputStream fileOutputStream = new FileOutputStream(qrImgPath + algId + ".png");
            fileOutputStream.write(decode);
            fileOutputStream.close();
            assertEquals(leeResult.getRespValue(), 0);
            assertNotNull(leeResult.getData());
        }
    }

    /**
     * Method: qrVerifyCodeSigned(String inData)
     */
    @Test
    public void testQrVerifyCodeSignedInData() throws Exception {
        for (int algId : algIds) {
            LeeResult leeResult = sureCSPAPI.qrCodeSigned(algId, inData);
            byte[] decode = CommonUtil.base64DecodeByte(leeResult.getData());
            leeResult = sureCSPAPI.qrVerifyCodeSigned(QRCodeUtil.decode(decode));
            assertEquals(leeResult.getRespValue(), 0);
        }
    }

    /**
     * Method: signJarData(int signMethod, String filePath)
     */
    @Test
    public void testSignJarDataForSignMethodFilePath() throws Exception {
        for (int i = 0; i < algIds.length; i++) {
            if (algIds[i] != SingDefine.SGD_SM3_SM2) {
                continue;
            }
            LeeResult leeResult = sureCSPAPI.signJarData(algIds[i], jarPath);
            assertEquals(leeResult.getRespValue(), 0);
        }
    }

    /**
     * Method: verifySignedJarData(String filePath)
     */
    @Test
    public void testVerifySignedJarData() throws Exception {
        for (int i = 0; i < algIds.length; i++) {
            if (algIds[i] != SingDefine.SGD_SM3_SM2) {
                continue;
            }
            LeeResult leeResult = sureCSPAPI.signJarData(algIds[i], jarPath);
            assertEquals(leeResult.getRespValue(), 0);
            LeeResult leeResult1 = sureCSPAPI.verifySignedJarData(jarPath);
            assertEquals(leeResult1.getRespValue(), 0);
        }
    }

    /**
     * Method: signApkData(int signMethod, String filePath, String outFilePath)
     */
    @Test
    public void testSignApkDataSignMethodFilePathOutFilePath() throws Exception {
        LeeResult leeResult = sureCSPAPI.signApkData(algIds[4], apkPath, apkOutPath);
        assertEquals(leeResult.getRespValue(), 0);

        leeResult = sureCSPAPI.signApkData(algIds[6], apkPath, apkOutPath);
        assertEquals(leeResult.getRespValue(), 0);
    }

    /**
     * verifySignedApkData(String filePath)
     */
    @Test
    public void testVerifySignedApkDataFilePath() throws Exception {
        LeeResult leeResult = new LeeResult();
        leeResult = sureCSPAPI.signApkData(algIds[4], apkPath, apkOutPath);
        assertEquals(leeResult.getRespValue(), 0);

        leeResult = sureCSPAPI.verifySignedApkData(apkOutPath);
        assertEquals(leeResult.getRespValue(), 0);

        leeResult = sureCSPAPI.signApkData(algIds[6], apkPath, apkOutPath);
        assertEquals(leeResult.getRespValue(), 0);

        leeResult = sureCSPAPI.verifySignedApkData(apkOutPath);
        assertEquals(leeResult.getRespValue(), 0);
    }

    @Test
    public void testCreateFiles() throws IOException {
        String filename = UUID.randomUUID().toString() + ".txt";
        LeeResult file = sureCSPAPI.createFile(filename, 1000);
        LeeResult files = sureCSPAPI.getFiles();
        String s = CommonUtil.base64DecodeString(files.getData());
        assertTrue(s.contains(filename));
        sureCSPAPI.deleteFile(filename);
    }

    @Test
    public void testReadFile() throws IOException {
        String filename = UUID.randomUUID().toString() + ".txt";
        LeeResult file = sureCSPAPI.createFile(filename, 1000);
        assertEquals(file.getRespValue(), 0);
        LeeResult leeResult = sureCSPAPI.writeFile(filename, 0, "1234567890".getBytes());
        assertEquals(leeResult.getRespValue(), 0);
        LeeResult leeResult1 = sureCSPAPI.readFile(filename, 0, 10);
        assertEquals(leeResult1.getRespValue(), 0);
        String data = leeResult1.getData();
        sureCSPAPI.deleteFile(filename);
    }

    @Test
    public void testWirteFile() throws IOException {
        String filename = UUID.randomUUID().toString() + ".txt";
        LeeResult file = sureCSPAPI.createFile(filename, 1000);
        assertEquals(file.getRespValue(), 0);
        LeeResult leeResult = sureCSPAPI.writeFile(filename, 0, "1234567890".getBytes());
        assertEquals(leeResult.getRespValue(), 0);
        sureCSPAPI.deleteFile(filename);
    }

    @Test
    public void testGetFiles() throws Exception {
        String filename = UUID.randomUUID().toString() + ".txt";
        LeeResult file = sureCSPAPI.createFile(filename, 1000);
        assertEquals(file.getRespValue(), 0);
        LeeResult files = sureCSPAPI.getFiles();
        assertEquals(files.getRespValue(), 0);
        String data = files.getData();
        String s = CommonUtil.base64DecodeString(data);
        sureCSPAPI.deleteFile(filename);
    }

    @Test
    public void testDeleteFile() throws IOException {
        String filename = UUID.randomUUID().toString() + ".txt";
        LeeResult file = sureCSPAPI.createFile(filename, 1000);
        assertEquals(file.getRespValue(), 0);
        LeeResult leeResult = sureCSPAPI.deleteFile(filename);
        assertEquals(leeResult.getRespValue(), 0);
    }
}
