package top.continew.admin.util;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import top.continew.admin.common.util.SecureUtils;

import static org.junit.jupiter.api.Assertions.*;

/**
 * SecureUtils 测试类
 *
 * @author Charles7c
 * @since 2024/12/04 16:00
 */
@SpringBootTest
@ActiveProfiles("test")
public class SecureUtilsTest {

    @Test
    public void testRsaEncryptDecrypt() {
        // 测试数据
        String originalPassword = "Test123456";

        try {
            // 公钥加密
            String encryptedPassword = SecureUtils.encryptByRsaPublicKey(originalPassword);
            assertNotNull(encryptedPassword);
            assertNotEquals(originalPassword, encryptedPassword);

            System.out.println("原始密码: " + originalPassword);
            System.out.println("加密后密码: " + encryptedPassword);

            // 私钥解密
            String decryptedPassword = SecureUtils.decryptByRsaPrivateKey(encryptedPassword);
            assertNotNull(decryptedPassword);
            assertEquals(originalPassword, decryptedPassword);

            System.out.println("解密后密码: " + decryptedPassword);

        } catch (Exception e) {
            e.printStackTrace();
            fail("RSA加密解密测试失败: " + e.getMessage());
        }
    }

    @Test
    public void testMultiplePasswords() {
        String[] passwords = {"Test123456", "Admin@2024", "MyPassword123!", "SimplePass"};

        for (String password : passwords) {
            try {
                String encrypted = SecureUtils.encryptByRsaPublicKey(password);
                String decrypted = SecureUtils.decryptByRsaPrivateKey(encrypted);

                assertEquals(password, decrypted, "密码加密解密不一致: " + password);
                System.out.println("✓ 密码测试通过: " + password);

            } catch (Exception e) {
                fail("密码测试失败: " + password + ", 错误: " + e.getMessage());
            }
        }
    }

    @Test
    public void testEmptyAndNullPassword() {
        // 测试空字符串
        try {
            String encrypted = SecureUtils.encryptByRsaPublicKey("");
            String decrypted = SecureUtils.decryptByRsaPrivateKey(encrypted);
            assertEquals("", decrypted);
            System.out.println("✓ 空字符串测试通过");
        } catch (Exception e) {
            System.out.println("空字符串测试失败: " + e.getMessage());
        }

        // 测试null值应该抛出异常
        assertThrows(Exception.class, () -> {
            SecureUtils.encryptByRsaPublicKey(null);
        });
    }

    @Test
    public void testLongPassword() {
        // 测试长密码
        String longPassword = "ThisIsAVeryLongPasswordThatShouldStillWorkWithRSAEncryption123456789!@#$%^&*()";

        try {
            String encrypted = SecureUtils.encryptByRsaPublicKey(longPassword);
            String decrypted = SecureUtils.decryptByRsaPrivateKey(encrypted);

            assertEquals(longPassword, decrypted);
            System.out.println("✓ 长密码测试通过");

        } catch (Exception e) {
            System.out.println("长密码测试失败: " + e.getMessage());
            // RSA加密有长度限制，这是正常的
        }
    }

    @Test
    public void testSpecialCharacters() {
        // 测试特殊字符
        String specialPassword = "密码@2024#测试$%^&*()";

        try {
            String encrypted = SecureUtils.encryptByRsaPublicKey(specialPassword);
            String decrypted = SecureUtils.decryptByRsaPrivateKey(encrypted);

            assertEquals(specialPassword, decrypted);
            System.out.println("✓ 特殊字符测试通过: " + specialPassword);

        } catch (Exception e) {
            System.out.println("特殊字符测试失败: " + e.getMessage());
        }
    }
}
