import unittest
from djzhpcom import P2PEncryptor

class TestP2PEncryptor(unittest.TestCase):
    def setUp(self):
        self.encryptor = P2PEncryptor()
        
    def test_encrypt_decrypt(self):
        """测试加密解密功能"""
        plaintext = "这是一个测试消息"
        encrypted = self.encryptor.encrypt(plaintext)
        decrypted = self.encryptor.decrypt(encrypted)
        self.assertEqual(plaintext, decrypted)
        
    def test_custom_key(self):
        """测试自定义密钥功能"""
        key = "my_secret_key"
        encryptor1 = P2PEncryptor(key)
        encryptor2 = P2PEncryptor(key)
        
        plaintext = "共享密钥测试"
        encrypted = encryptor1.encrypt(plaintext)
        decrypted = encryptor2.decrypt(encrypted)
        self.assertEqual(plaintext, decrypted)
        
    def test_dynamic_iv(self):
        """测试动态IV功能"""
        encryptor = P2PEncryptor(dynamic_iv=True)
        plaintext = "动态IV测试"
        encrypted1 = encryptor.encrypt(plaintext)
        encrypted2 = encryptor.encrypt(plaintext)
        self.assertNotEqual(encrypted1, encrypted2)
        
    def test_static_iv(self):
        """测试静态IV功能"""
        encryptor = P2PEncryptor(dynamic_iv=False)
        plaintext = "静态IV测试"
        encrypted1 = encryptor.encrypt(plaintext)
        encrypted2 = encryptor.encrypt(plaintext)
        self.assertEqual(encrypted1, encrypted2)
        
    def test_session_key(self):
        """测试会话密钥生成"""
        encryptor = P2PEncryptor()
        key1 = encryptor.generate_session_key()
        key2 = encryptor.generate_session_key()
        self.assertNotEqual(key1, key2)
        self.assertEqual(len(key1), 32)

if __name__ == "__main__":
    unittest.main()