Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit998e373

Browse files
author
Thomasr
committed
testcase for payload encryption
1 parent210f292 commit998e373

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
packageorg.lowcoder.domain.encryption;
2+
3+
importorg.junit.jupiter.api.BeforeEach;
4+
importorg.junit.jupiter.api.Test;
5+
importorg.lowcoder.sdk.config.CommonConfig;
6+
importorg.lowcoder.sdk.config.CommonConfig.Encrypt;
7+
importorg.lowcoder.sdk.config.CommonConfig.JsExecutor;
8+
importorg.springframework.security.crypto.encrypt.Encryptors;
9+
importorg.springframework.security.crypto.encrypt.TextEncryptor;
10+
11+
importstaticorg.junit.jupiter.api.Assertions.*;
12+
importstaticorg.mockito.Mockito.*;
13+
14+
classEncryptionServiceImplTest {
15+
16+
privateEncryptionServiceImplencryptionService;
17+
privateTextEncryptornodeServerEncryptor;
18+
privateStringnodePassword ="nodePassword";
19+
privateStringnodeSalt ="nodeSalt";
20+
21+
@BeforeEach
22+
voidsetUp() {
23+
// Mock CommonConfig and its nested classes
24+
Encryptencrypt =mock(Encrypt.class);
25+
when(encrypt.getPassword()).thenReturn("testPassword");
26+
when(encrypt.getSalt()).thenReturn("testSalt");
27+
28+
JsExecutorjsExecutor =mock(JsExecutor.class);
29+
when(jsExecutor.getPassword()).thenReturn(nodePassword);
30+
when(jsExecutor.getSalt()).thenReturn(nodeSalt);
31+
32+
CommonConfigcommonConfig =mock(CommonConfig.class);
33+
when(commonConfig.getEncrypt()).thenReturn(encrypt);
34+
when(commonConfig.getJsExecutor()).thenReturn(jsExecutor);
35+
36+
encryptionService =newEncryptionServiceImpl(commonConfig);
37+
38+
// For direct comparison in test
39+
StringsaltInHexForNodeServer =org.apache.commons.codec.binary.Hex.encodeHexString(nodeSalt.getBytes());
40+
nodeServerEncryptor =Encryptors.text(nodePassword,saltInHexForNodeServer);
41+
}
42+
43+
@Test
44+
voidtestEncryptStringForNodeServer_NullInput() {
45+
assertNull(encryptionService.encryptStringForNodeServer(null));
46+
}
47+
48+
@Test
49+
voidtestEncryptStringForNodeServer_EmptyInput() {
50+
assertEquals("",encryptionService.encryptStringForNodeServer(""));
51+
}
52+
53+
@Test
54+
voidtestEncryptStringForNodeServer_EncryptsAndDecryptsCorrectly() {
55+
Stringplain ="node secret";
56+
Stringencrypted =encryptionService.encryptStringForNodeServer(plain);
57+
assertNotNull(encrypted);
58+
assertNotEquals(plain,encrypted);
59+
60+
// Decrypt using the same encryptor to verify correctness
61+
Stringdecrypted =nodeServerEncryptor.decrypt(encrypted);
62+
assertEquals(plain,decrypted);
63+
}
64+
65+
@Test
66+
voidtestEncryptStringForNodeServer_DifferentInputsProduceDifferentOutputs() {
67+
Stringencrypted1 =encryptionService.encryptStringForNodeServer("abc");
68+
Stringencrypted2 =encryptionService.encryptStringForNodeServer("def");
69+
assertNotEquals(encrypted1,encrypted2);
70+
}
71+
72+
@Test
73+
voidtestEncryptStringForNodeServer_SameInputProducesDifferentOutputs() {
74+
Stringinput ="repeat";
75+
Stringencrypted1 =encryptionService.encryptStringForNodeServer(input);
76+
Stringencrypted2 =encryptionService.encryptStringForNodeServer(input);
77+
// Spring's Encryptors.text uses random IV, so outputs should differ
78+
assertNotEquals(encrypted1,encrypted2);
79+
}
80+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp