11/*
2- * Copyright 2024-2024 IEXEC BLOCKCHAIN TECH
2+ * Copyright 2024-2025 IEXEC BLOCKCHAIN TECH
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
@@ -101,19 +101,20 @@ void shouldBeAuthorizedOnExecutionOfTeeTaskWithDetails() {
101101 when (iexecHubService .getChainTask (auth .getChainTaskId ())).thenReturn (Optional .of (chainTask ));
102102 when (iexecHubService .getChainDeal (chainTask .getDealid ())).thenReturn (Optional .of (chainDeal ));
103103
104- Optional <AuthorizationError > isAuth = authorizationService .isAuthorizedOnExecutionWithDetailedIssue (auth );
104+ final Optional <AuthorizationError > isAuth = authorizationService .isAuthorizedOnExecutionWithDetailedIssue (auth );
105105 assertThat (isAuth ).isEmpty ();
106106 }
107107
108108 @ Test
109109 void shouldNotBeAuthorizedOnExecutionOfTeeTaskWithNullAuthorizationWithDetails () {
110- Optional <AuthorizationError > isAuth = authorizationService .isAuthorizedOnExecutionWithDetailedIssue (null );
110+ final Optional <AuthorizationError > isAuth = authorizationService .isAuthorizedOnExecutionWithDetailedIssue (null );
111111 assertThat (isAuth ).isEqualTo (Optional .of (EMPTY_PARAMS_UNAUTHORIZED ));
112112 }
113113
114114 @ Test
115115 void shouldNotBeAuthorizedOnExecutionOfTeeTaskWithEmptyAuthorizationWithDetails () {
116- Optional <AuthorizationError > isAuth = authorizationService .isAuthorizedOnExecutionWithDetailedIssue (WorkerpoolAuthorization .builder ().build ());
116+ final Optional <AuthorizationError > isAuth = authorizationService .isAuthorizedOnExecutionWithDetailedIssue (WorkerpoolAuthorization .builder ()
117+ .chainTaskId (null ).build ());
117118 assertThat (isAuth ).isEqualTo (Optional .of (EMPTY_PARAMS_UNAUTHORIZED ));
118119 }
119120
@@ -129,7 +130,7 @@ void shouldNotBeAuthorizedOnExecutionOfTeeTaskWhenTaskTypeNotMatchedOnchainWithD
129130 when (iexecHubService .getChainTask (auth .getChainTaskId ())).thenReturn (Optional .of (chainTask ));
130131 when (iexecHubService .getChainDeal (chainTask .getDealid ())).thenReturn (Optional .of (chainDeal ));
131132
132- Optional <AuthorizationError > isAuth = authorizationService .isAuthorizedOnExecutionWithDetailedIssue (auth );
133+ final Optional <AuthorizationError > isAuth = authorizationService .isAuthorizedOnExecutionWithDetailedIssue (auth );
133134 assertThat (isAuth ).isEqualTo (Optional .of (NO_MATCH_ONCHAIN_TYPE ));
134135 }
135136
@@ -138,7 +139,7 @@ void shouldNotBeAuthorizedOnExecutionOfTeeTaskWhenGetTaskFailedWithDetails() {
138139 final WorkerpoolAuthorization auth = getWorkerpoolAuthorization (true );
139140 when (iexecHubService .getChainTask (auth .getChainTaskId ())).thenReturn (Optional .empty ());
140141
141- Optional <AuthorizationError > isAuth = authorizationService .isAuthorizedOnExecutionWithDetailedIssue (auth );
142+ final Optional <AuthorizationError > isAuth = authorizationService .isAuthorizedOnExecutionWithDetailedIssue (auth );
142143 assertThat (isAuth ).isEqualTo (Optional .of (GET_CHAIN_TASK_FAILED ));
143144 }
144145
@@ -151,34 +152,34 @@ void shouldNotBeAuthorizedOnExecutionOfTeeTaskWhenFinalDeadlineReached() {
151152 .build ();
152153 when (iexecHubService .getChainTask (auth .getChainTaskId ())).thenReturn (Optional .of (chainTask ));
153154
154- Optional <AuthorizationError > isAuth = authorizationService .isAuthorizedOnExecutionWithDetailedIssue (auth );
155+ final Optional <AuthorizationError > isAuth = authorizationService .isAuthorizedOnExecutionWithDetailedIssue (auth );
155156 assertThat (isAuth ).isEqualTo (Optional .of (TASK_FINAL_DEADLINE_REACHED ));
156157 }
157158
158159 @ Test
159160 void shouldNotBeAuthorizedOnExecutionOfTeeTaskWhenGetDealFailedWithDetails () {
160161 final ChainTask chainTask = getChainTask (ACTIVE );
161- final WorkerpoolAuthorization auth = getWorkerpoolAuthorization ( true );
162- auth . setSignature ( new Signature ( POOL_WRONG_SIGNATURE ) );
162+ final Signature wrongSignature = new Signature ( POOL_WRONG_SIGNATURE );
163+ final WorkerpoolAuthorization auth = getWorkerpoolAuthorizationWithWrongSignature ( wrongSignature );
163164
164165 when (iexecHubService .getChainTask (auth .getChainTaskId ())).thenReturn (Optional .of (chainTask ));
165166 when (iexecHubService .getChainDeal (chainTask .getDealid ())).thenReturn (Optional .empty ());
166167
167- Optional <AuthorizationError > isAuth = authorizationService .isAuthorizedOnExecutionWithDetailedIssue (auth );
168+ final Optional <AuthorizationError > isAuth = authorizationService .isAuthorizedOnExecutionWithDetailedIssue (auth );
168169 assertThat (isAuth ).isEqualTo (Optional .of (GET_CHAIN_DEAL_FAILED ));
169170 }
170171
171172 @ Test
172173 void shouldNotBeAuthorizedOnExecutionOfTeeTaskWhenPoolSignatureIsNotValidWithDetails () {
173174 final ChainDeal chainDeal = getChainDeal ();
174175 final ChainTask chainTask = getChainTask (ACTIVE );
175- final WorkerpoolAuthorization auth = getWorkerpoolAuthorization ( true );
176- auth . setSignature ( new Signature ( POOL_WRONG_SIGNATURE ) );
176+ final Signature wrongSignature = new Signature ( POOL_WRONG_SIGNATURE );
177+ final WorkerpoolAuthorization auth = getWorkerpoolAuthorizationWithWrongSignature ( wrongSignature );
177178
178179 when (iexecHubService .getChainTask (auth .getChainTaskId ())).thenReturn (Optional .of (chainTask ));
179180 when (iexecHubService .getChainDeal (chainTask .getDealid ())).thenReturn (Optional .of (chainDeal ));
180181
181- Optional <AuthorizationError > isAuth = authorizationService .isAuthorizedOnExecutionWithDetailedIssue (auth );
182+ final Optional <AuthorizationError > isAuth = authorizationService .isAuthorizedOnExecutionWithDetailedIssue (auth );
182183 assertThat (isAuth ).isEqualTo (Optional .of (INVALID_SIGNATURE ));
183184 }
184185 // endregion
@@ -276,15 +277,15 @@ void shouldNotAddAuthorizationTwiceInCollection() {
276277 // endregion
277278
278279 // region utils
279- String getEnclaveSignature (ECKeyPair ecKeyPair ) {
280+ String getEnclaveSignature (final ECKeyPair ecKeyPair ) {
280281 final String resultHash = HashUtils .concatenateAndHash (CHAIN_TASK_ID , RESULT_DIGEST );
281282 final String resultSeal = HashUtils .concatenateAndHash (workerCreds .getAddress (), CHAIN_TASK_ID , RESULT_DIGEST );
282283 final String messageHash = HashUtils .concatenateAndHash (resultHash , resultSeal );
283284 return SignatureUtils .signMessageHashAndGetSignature (messageHash ,
284285 Numeric .toHexStringWithPrefix (ecKeyPair .getPrivateKey ())).getValue ();
285286 }
286287
287- WorkerpoolAuthorization getWorkerpoolAuthorization (boolean isTeeTask ) {
288+ private WorkerpoolAuthorization getWorkerpoolAuthorization (final boolean isTeeTask ) {
288289 final String enclaveChallenge = isTeeTask ? enclaveCreds .getAddress () : BytesUtils .EMPTY_ADDRESS ;
289290 final String hash = HashUtils .concatenateAndHash (workerCreds .getAddress (), CHAIN_TASK_ID , enclaveChallenge );
290291 final Signature signature = SignatureUtils .signMessageHashAndGetSignature (hash , POOL_PRIVATE );
@@ -295,5 +296,15 @@ WorkerpoolAuthorization getWorkerpoolAuthorization(boolean isTeeTask) {
295296 .signature (signature )
296297 .build ();
297298 }
299+
300+ private WorkerpoolAuthorization getWorkerpoolAuthorizationWithWrongSignature (final Signature signature ) {
301+ final String enclaveChallenge = enclaveCreds .getAddress ();
302+ return WorkerpoolAuthorization .builder ()
303+ .chainTaskId (CHAIN_TASK_ID )
304+ .enclaveChallenge (enclaveChallenge )
305+ .workerWallet (workerCreds .getAddress ())
306+ .signature (signature )
307+ .build ();
308+ }
298309 // endregion
299310}
0 commit comments