1+ package org .lowcoder .domain .encryption ;
2+
3+ import org .junit .jupiter .api .BeforeEach ;
4+ import org .junit .jupiter .api .Test ;
5+ import org .lowcoder .sdk .config .CommonConfig ;
6+ import org .lowcoder .sdk .config .CommonConfig .Encrypt ;
7+ import org .lowcoder .sdk .config .CommonConfig .JsExecutor ;
8+ import org .springframework .security .crypto .encrypt .Encryptors ;
9+ import org .springframework .security .crypto .encrypt .TextEncryptor ;
10+
11+ import static org .junit .jupiter .api .Assertions .*;
12+ import static org .mockito .Mockito .*;
13+
14+ class EncryptionServiceImplTest {
15+
16+ private EncryptionServiceImpl encryptionService ;
17+ private TextEncryptor nodeServerEncryptor ;
18+ private String nodePassword = "nodePassword" ;
19+ private String nodeSalt = "nodeSalt" ;
20+
21+ @ BeforeEach
22+ void setUp () {
23+ // Mock CommonConfig and its nested classes
24+ Encrypt encrypt = mock (Encrypt .class );
25+ when (encrypt .getPassword ()).thenReturn ("testPassword" );
26+ when (encrypt .getSalt ()).thenReturn ("testSalt" );
27+
28+ JsExecutor jsExecutor = mock (JsExecutor .class );
29+ when (jsExecutor .getPassword ()).thenReturn (nodePassword );
30+ when (jsExecutor .getSalt ()).thenReturn (nodeSalt );
31+
32+ CommonConfig commonConfig = mock (CommonConfig .class );
33+ when (commonConfig .getEncrypt ()).thenReturn (encrypt );
34+ when (commonConfig .getJsExecutor ()).thenReturn (jsExecutor );
35+
36+ encryptionService = new EncryptionServiceImpl (commonConfig );
37+
38+ // For direct comparison in test
39+ String saltInHexForNodeServer = org .apache .commons .codec .binary .Hex .encodeHexString (nodeSalt .getBytes ());
40+ nodeServerEncryptor = Encryptors .text (nodePassword , saltInHexForNodeServer );
41+ }
42+
43+ @ Test
44+ void testEncryptStringForNodeServer_NullInput () {
45+ assertNull (encryptionService .encryptStringForNodeServer (null ));
46+ }
47+
48+ @ Test
49+ void testEncryptStringForNodeServer_EmptyInput () {
50+ assertEquals ("" , encryptionService .encryptStringForNodeServer ("" ));
51+ }
52+
53+ @ Test
54+ void testEncryptStringForNodeServer_EncryptsAndDecryptsCorrectly () {
55+ String plain = "node secret" ;
56+ String encrypted = encryptionService .encryptStringForNodeServer (plain );
57+ assertNotNull (encrypted );
58+ assertNotEquals (plain , encrypted );
59+
60+ // Decrypt using the same encryptor to verify correctness
61+ String decrypted = nodeServerEncryptor .decrypt (encrypted );
62+ assertEquals (plain , decrypted );
63+ }
64+
65+ @ Test
66+ void testEncryptStringForNodeServer_DifferentInputsProduceDifferentOutputs () {
67+ String encrypted1 = encryptionService .encryptStringForNodeServer ("abc" );
68+ String encrypted2 = encryptionService .encryptStringForNodeServer ("def" );
69+ assertNotEquals (encrypted1 , encrypted2 );
70+ }
71+
72+ @ Test
73+ void testEncryptStringForNodeServer_SameInputProducesDifferentOutputs () {
74+ String input = "repeat" ;
75+ String encrypted1 = encryptionService .encryptStringForNodeServer (input );
76+ String encrypted2 = encryptionService .encryptStringForNodeServer (input );
77+ // Spring's Encryptors.text uses random IV, so outputs should differ
78+ assertNotEquals (encrypted1 , encrypted2 );
79+ }
80+ }
0 commit comments