1+ package io .mosip .kernel .clientcrypto .test .controller ;
2+
3+ import com .fasterxml .jackson .databind .ObjectMapper ;
4+ import com .fasterxml .jackson .databind .SerializationFeature ;
5+ import com .fasterxml .jackson .databind .json .JsonMapper ;
6+ import com .fasterxml .jackson .datatype .jsr310 .JavaTimeModule ;
7+ import com .fasterxml .jackson .module .afterburner .AfterburnerModule ;
8+ import io .mosip .kernel .clientcrypto .constant .ClientType ;
9+ import io .mosip .kernel .clientcrypto .controller .ClientCryptoController ;
10+ import io .mosip .kernel .clientcrypto .dto .*;
11+ import io .mosip .kernel .clientcrypto .service .impl .ClientCryptoFacade ;
12+ import io .mosip .kernel .clientcrypto .service .spi .ClientCryptoService ;
13+ import io .mosip .kernel .clientcrypto .test .ClientCryptoTestBootApplication ;
14+ import io .mosip .kernel .core .http .RequestWrapper ;
15+ import io .mosip .kernel .core .http .ResponseWrapper ;
16+ import io .mosip .kernel .core .util .CryptoUtil ;
17+ import org .junit .Assert ;
18+ import org .junit .Before ;
19+ import org .junit .Test ;
20+ import org .junit .runner .RunWith ;
21+ import org .springframework .beans .factory .annotation .Autowired ;
22+ import org .springframework .boot .test .autoconfigure .web .servlet .AutoConfigureMockMvc ;
23+ import org .springframework .boot .test .context .SpringBootTest ;
24+ import org .springframework .test .annotation .DirtiesContext ;
25+ import org .springframework .test .context .junit4 .SpringRunner ;
26+ import org .springframework .test .web .servlet .MockMvc ;
27+ import org .springframework .http .MediaType ;
28+ import org .springframework .security .test .context .support .WithUserDetails ;
29+ import org .springframework .test .web .servlet .MvcResult ;
30+
31+ import java .security .KeyPair ;
32+ import java .security .KeyPairGenerator ;
33+ import java .security .PublicKey ;
34+ import java .time .LocalDateTime ;
35+
36+ import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .post ;
37+ import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .*;
38+ import static org .springframework .security .test .web .servlet .request .SecurityMockMvcRequestPostProcessors .csrf ;
39+
40+ @ SpringBootTest (classes = { ClientCryptoTestBootApplication .class })
41+ @ RunWith (SpringRunner .class )
42+ @ DirtiesContext (classMode = DirtiesContext .ClassMode .AFTER_CLASS )
43+ @ AutoConfigureMockMvc
44+ public class ClientCryptoControllerTest {
45+
46+ @ Autowired
47+ private MockMvc mockMvc ;
48+
49+ @ Autowired
50+ private ClientCryptoController clientCryptoController ;
51+
52+ @ Autowired
53+ private ClientCryptoFacade clientCryptoFacade ;
54+
55+ private ObjectMapper mapper ;
56+ private byte [] testData ;
57+ private KeyPair testKeyPair ;
58+ private PublicKey testPublicKey ;
59+
60+ private static final String ID = "mosip.crypto.service" ;
61+ private static final String VERSION = "V1.0" ;
62+
63+ @ Before
64+ public void setUp () throws Exception {
65+ mapper = JsonMapper .builder ().addModule (new AfterburnerModule ()).build ();
66+ mapper .registerModule (new JavaTimeModule ());
67+ mapper .disable (SerializationFeature .WRITE_DATES_AS_TIMESTAMPS );
68+
69+ testData = "Test data for client crypto operations" .getBytes ();
70+
71+ // Generate test key pair
72+ KeyPairGenerator keyPairGenerator = KeyPairGenerator .getInstance ("RSA" );
73+ keyPairGenerator .initialize (2048 );
74+ testKeyPair = keyPairGenerator .generateKeyPair ();
75+ testPublicKey = testKeyPair .getPublic ();
76+ }
77+
78+ @ Test
79+ public void testSignData_Forbidden () throws Exception {
80+ RequestWrapper <TpmSignRequestDto > request = new RequestWrapper <>();
81+ request .setId (ID );
82+ request .setVersion (VERSION );
83+ request .setRequesttime (LocalDateTime .now ());
84+ TpmSignRequestDto requestDto = new TpmSignRequestDto ();
85+ requestDto .setData (CryptoUtil .encodeToURLSafeBase64 (testData ));
86+ request .setRequest (requestDto );
87+
88+ mockMvc .perform (post ("/cssign" )
89+ .contentType (MediaType .APPLICATION_JSON )
90+ .content (mapper .writeValueAsString (request )))
91+ .andExpect (status ().isForbidden ());
92+ }
93+
94+ @ Test
95+ @ WithUserDetails ("test" )
96+ public void testSignDataMvc_Success () throws Exception {
97+ RequestWrapper <TpmSignRequestDto > request = new RequestWrapper <>();
98+ request .setId (ID );
99+ request .setVersion (VERSION );
100+ request .setRequesttime (LocalDateTime .now ());
101+ TpmSignRequestDto requestDto = new TpmSignRequestDto ();
102+ requestDto .setData (CryptoUtil .encodeToURLSafeBase64 (testData ));
103+ request .setRequest (requestDto );
104+
105+ mockMvc .perform (post ("/cssign" )
106+ .contentType (MediaType .APPLICATION_JSON )
107+ .content (mapper .writeValueAsString (request ))
108+ .with (csrf ()))
109+ .andExpect (status ().isOk ())
110+ .andExpect (jsonPath ("$.response.data" ).isNotEmpty ());
111+ }
112+
113+ @ Test
114+ @ WithUserDetails ("test" )
115+ public void testVerifySignatureMvc_Success () throws Exception {
116+ // First sign the data
117+ RequestWrapper <TpmSignRequestDto > signRequest = new RequestWrapper <>();
118+ signRequest .setId (ID );
119+ signRequest .setVersion (VERSION );
120+ signRequest .setRequesttime (LocalDateTime .now ());
121+ TpmSignRequestDto signRequestDto = new TpmSignRequestDto ();
122+ signRequestDto .setData (CryptoUtil .encodeToURLSafeBase64 (testData ));
123+ signRequest .setRequest (signRequestDto );
124+ ResponseWrapper <TpmSignResponseDto > signResult = clientCryptoController .signData (signRequest );
125+
126+ // Now verify the signature
127+ RequestWrapper <TpmSignVerifyRequestDto > request = new RequestWrapper <>();
128+ request .setId (ID );
129+ request .setVersion (VERSION );
130+ request .setRequesttime (LocalDateTime .now ());
131+ TpmSignVerifyRequestDto requestDto = new TpmSignVerifyRequestDto ();
132+ requestDto .setData (CryptoUtil .encodeToURLSafeBase64 (testData ));
133+ requestDto .setSignature (signResult .getResponse ().getData ());
134+
135+ ClientCryptoService clientCryptoService = clientCryptoFacade .getClientSecurity ();
136+ byte [] signingPublicKey = clientCryptoService .getSigningPublicPart ();
137+ requestDto .setPublicKey (CryptoUtil .encodeToURLSafeBase64 (signingPublicKey ));
138+ requestDto .setClientType (ClientType .LOCAL );
139+ request .setRequest (requestDto );
140+
141+ mockMvc .perform (post ("/csverifysign" )
142+ .contentType (MediaType .APPLICATION_JSON )
143+ .content (mapper .writeValueAsString (request ))
144+ .with (csrf ()))
145+ .andExpect (status ().isOk ())
146+ .andExpect (jsonPath ("$.response.verified" ).value (true ));
147+ }
148+
149+ @ Test
150+ @ WithUserDetails ("test" )
151+ public void testTpmEncryptMvc_Success () throws Exception {
152+ RequestWrapper <TpmCryptoRequestDto > request = new RequestWrapper <>();
153+ request .setId (ID );
154+ request .setVersion (VERSION );
155+ request .setRequesttime (LocalDateTime .now ());
156+ TpmCryptoRequestDto requestDto = new TpmCryptoRequestDto ();
157+ requestDto .setValue (CryptoUtil .encodeToURLSafeBase64 (testData ));
158+ requestDto .setPublicKey (CryptoUtil .encodeToURLSafeBase64 (testPublicKey .getEncoded ()));
159+ requestDto .setClientType (ClientType .LOCAL );
160+ request .setRequest (requestDto );
161+
162+ mockMvc .perform (post ("/tpmencrypt" )
163+ .contentType (MediaType .APPLICATION_JSON )
164+ .content (mapper .writeValueAsString (request ))
165+ .with (csrf ()))
166+ .andExpect (status ().isOk ())
167+ .andExpect (jsonPath ("$.response.value" ).isNotEmpty ());
168+ }
169+
170+ @ Test
171+ @ WithUserDetails ("test" )
172+ public void testTpmDecryptMvc_Success () throws Exception {
173+ // First encrypt the data
174+ RequestWrapper <TpmCryptoRequestDto > encryptRequest = new RequestWrapper <>();
175+ encryptRequest .setId (ID );
176+ encryptRequest .setVersion (VERSION );
177+ encryptRequest .setRequesttime (LocalDateTime .now ());
178+ TpmCryptoRequestDto encryptRequestDto = new TpmCryptoRequestDto ();
179+ encryptRequestDto .setValue (CryptoUtil .encodeToURLSafeBase64 (testData ));
180+
181+ ClientCryptoService clientCryptoService = clientCryptoFacade .getClientSecurity ();
182+ byte [] encryptionPublicKey = clientCryptoService .getEncryptionPublicPart ();
183+ encryptRequestDto .setPublicKey (CryptoUtil .encodeToURLSafeBase64 (encryptionPublicKey ));
184+ encryptRequestDto .setClientType (null );
185+ encryptRequest .setRequest (encryptRequestDto );
186+ ResponseWrapper <TpmCryptoResponseDto > encryptResult = clientCryptoController .tpmEncrypt (encryptRequest );
187+
188+ // Now decrypt the data
189+ RequestWrapper <TpmCryptoRequestDto > request = new RequestWrapper <>();
190+ request .setId (ID );
191+ request .setVersion (VERSION );
192+ request .setRequesttime (LocalDateTime .now ());
193+ TpmCryptoRequestDto requestDto = new TpmCryptoRequestDto ();
194+ requestDto .setValue (encryptResult .getResponse ().getValue ());
195+ requestDto .setPublicKey (CryptoUtil .encodeToURLSafeBase64 (encryptionPublicKey ));
196+ request .setRequest (requestDto );
197+
198+ mockMvc .perform (post ("/tpmdecrypt" )
199+ .contentType (MediaType .APPLICATION_JSON )
200+ .content (mapper .writeValueAsString (request ))
201+ .with (csrf ()))
202+ .andExpect (status ().isOk ())
203+ .andExpect (jsonPath ("$.response.value" ).isNotEmpty ());
204+ }
205+
206+ @ Test
207+ @ WithUserDetails ("test" )
208+ public void testGetSigningPublicKeyMvc_Success () throws Exception {
209+ RequestWrapper <PublicKeyRequestDto > request = new RequestWrapper <>();
210+ request .setId (ID );
211+ request .setVersion (VERSION );
212+ request .setRequesttime (LocalDateTime .now ());
213+ PublicKeyRequestDto requestDto = new PublicKeyRequestDto ();
214+ requestDto .setServerProfile ("test" );
215+ request .setRequest (requestDto );
216+
217+ mockMvc .perform (post ("/tpmsigning/publickey" )
218+ .contentType (MediaType .APPLICATION_JSON )
219+ .content (mapper .writeValueAsString (request ))
220+ .with (csrf ()))
221+ .andExpect (status ().isOk ())
222+ .andExpect (jsonPath ("$.response.publicKey" ).isNotEmpty ());
223+ }
224+
225+ @ Test
226+ @ WithUserDetails ("test" )
227+ public void testGetEncPublicKeyMvc_Success () throws Exception {
228+ RequestWrapper <PublicKeyRequestDto > request = new RequestWrapper <>();
229+ request .setId (ID );
230+ request .setVersion (VERSION );
231+ request .setRequesttime (LocalDateTime .now ());
232+ PublicKeyRequestDto requestDto = new PublicKeyRequestDto ();
233+ requestDto .setServerProfile ("test" );
234+ request .setRequest (requestDto );
235+
236+ mockMvc .perform (post ("/tpmencryption/publickey" )
237+ .contentType (MediaType .APPLICATION_JSON )
238+ .content (mapper .writeValueAsString (request ))
239+ .with (csrf ()))
240+ .andExpect (status ().isOk ())
241+ .andExpect (jsonPath ("$.response.publicKey" ).isNotEmpty ());
242+ }
243+
244+ @ Test
245+ @ WithUserDetails ("test" )
246+ public void testVerifySignature_Success () throws Exception {
247+ // First sign the data
248+ RequestWrapper <TpmSignRequestDto > signRequest = new RequestWrapper <>();
249+ signRequest .setId (ID );
250+ signRequest .setVersion (VERSION );
251+ signRequest .setRequesttime (LocalDateTime .now ());
252+ TpmSignRequestDto signRequestDto = new TpmSignRequestDto ();
253+ signRequestDto .setData (CryptoUtil .encodeToURLSafeBase64 (testData ));
254+ signRequest .setRequest (signRequestDto );
255+ ResponseWrapper <TpmSignResponseDto > signResult = clientCryptoController .signData (signRequest );
256+
257+ // Now verify the signature
258+ RequestWrapper <TpmSignVerifyRequestDto > request = new RequestWrapper <>();
259+ request .setId (ID );
260+ request .setVersion (VERSION );
261+ request .setRequesttime (LocalDateTime .now ());
262+ TpmSignVerifyRequestDto requestDto = new TpmSignVerifyRequestDto ();
263+ requestDto .setData (CryptoUtil .encodeToURLSafeBase64 (testData ));
264+ requestDto .setSignature (signResult .getResponse ().getData ());
265+
266+ ClientCryptoService clientCryptoService = clientCryptoFacade .getClientSecurity ();
267+ byte [] signingPublicKey = clientCryptoService .getSigningPublicPart ();
268+ requestDto .setPublicKey (CryptoUtil .encodeToURLSafeBase64 (signingPublicKey ));
269+ requestDto .setClientType (ClientType .LOCAL );
270+ request .setRequest (requestDto );
271+
272+ MvcResult result = mockMvc .perform (post ("/csverifysign" )
273+ .contentType (MediaType .APPLICATION_JSON )
274+ .content (mapper .writeValueAsString (request ))
275+ .with (csrf ()))
276+ .andExpect (status ().isOk ())
277+ .andReturn ();
278+
279+ Assert .assertTrue (result .getResponse ().getContentAsString ().contains ("verified" ));
280+ }
281+ }
0 commit comments