Repository: iExecBlockchainComputing/iexec-worker
Pull request #640: Stop using TestUtils.getTeeWorkerpoolAuth() in ContributionServiceTests
Author: login: Natchica
Created: 2025-03-20T09:47:07Z | Merged: 2025-03-20T15:07:14Z



Diff:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 7f36db826..98e4a4d4a 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -16,6 +16,7 @@ All notable changes to this project will be documented in this file.
 - Replace deprecated `connect` with `connectAsync` in `StompClientService`. (#627)
 - Remove redundant blockchain calls to diminish pressure on Ethereum JSON-RPC API. (#632)
 - Harmonize YML internal variables to proper case. (#638)
+- Stop using `TestUtils` in `ContributionServiceTests`. (#640)
 
 ### Breaking API changes
 
diff --git a/src/test/java/com/iexec/worker/chain/ContributionServiceTests.java b/src/test/java/com/iexec/worker/chain/ContributionServiceTests.java
index b429302dd..23a33af46 100644
--- a/src/test/java/com/iexec/worker/chain/ContributionServiceTests.java
+++ b/src/test/java/com/iexec/worker/chain/ContributionServiceTests.java
@@ -18,10 +18,11 @@
 
 import com.iexec.common.result.ComputedFile;
 import com.iexec.commons.poco.chain.*;
+import com.iexec.commons.poco.security.Signature;
 import com.iexec.commons.poco.task.TaskDescription;
 import com.iexec.commons.poco.utils.BytesUtils;
 import com.iexec.commons.poco.utils.HashUtils;
-import com.iexec.commons.poco.utils.TestUtils;
+import com.iexec.commons.poco.utils.SignatureUtils;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.extension.ExtendWith;
@@ -48,6 +49,10 @@ class ContributionServiceTests {
 
     private static final String CHAIN_DEAL_ID = "0x1566a9348a284d12f7d81fa017fbc440fd501ddef5746821860ffda7113eb847";
     private static final String WORKER_WALLET_ADDRESS = "0x49713c374C0D5259A0c0c4fCCd1254CdFd631b80";
+    private static final String ENCLAVE_ADDRESS = "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee";
+    private static final String CHAIN_TASK_ID = "0x1111111111111111111111111111111111111111111111111111111111111111";
+    private static final String POOL_PRIVATE = "0xe2a973b083fae8043543f15313955aecee9de809a318656c1cfb22d3a6d52de1";
+    private static final String WORKER_PRIVATE = "0xd0db6df0ebcd1d41439d91d86c5fc5c1806ee9cd8e71e3d5544bb7294b435c26";
 
     @Mock
     private IexecHubService iexecHubService;
@@ -294,21 +299,21 @@ void getCannotContributeAndFinalizeStatusCauseShouldReturnEmpty() {
 
     @Test
     void getContribution() {
-        String chainTaskId = "0x0000000000000000000000000000000000000000000000000000000000000001";
-        String resultDigest = "0x0000000000000000000000000000000000000000000000000000000000000002";
+        final String chainTaskId = "0x0000000000000000000000000000000000000000000000000000000000000001";
+        final String resultDigest = "0x0000000000000000000000000000000000000000000000000000000000000002";
 
-        String resultHash = HashUtils.concatenateAndHash(chainTaskId, resultDigest);
-        String resultSeal = HashUtils.concatenateAndHash(Credentials.create(TestUtils.WORKER_PRIVATE).getAddress(), chainTaskId, resultDigest);
+        final String resultHash = HashUtils.concatenateAndHash(chainTaskId, resultDigest);
+        final String resultSeal = HashUtils.concatenateAndHash(Credentials.create(WORKER_PRIVATE).getAddress(), chainTaskId, resultDigest);
 
-        WorkerpoolAuthorization teeWorkerpoolAuth = TestUtils.getTeeWorkerpoolAuth();
+        final WorkerpoolAuthorization teeWorkerpoolAuth = getTeeWorkerpoolAuth();
         when(workerpoolAuthorizationService.getWorkerpoolAuthorization(chainTaskId)).thenReturn(teeWorkerpoolAuth);
         when(iexecHubService.isTeeTask(chainTaskId)).thenReturn(false);
 
-        ComputedFile computedFile = ComputedFile.builder()
+        final ComputedFile computedFile = ComputedFile.builder()
                 .taskId(chainTaskId)
                 .resultDigest(resultDigest)
                 .build();
-        Contribution contribution = contributionService.getContribution(computedFile);
+        final Contribution contribution = contributionService.getContribution(computedFile);
 
         assertNotNull(contribution);
 
@@ -327,41 +332,40 @@ void getContribution() {
 
     @Test
     void getContributionWithTee() {
-        String chainTaskId = "0x0000000000000000000000000000000000000000000000000000000000000002";
-        String resultDigest = "0x0000000000000000000000000000000000000000000000000000000000000001";
+        final String chainTaskId = "0x0000000000000000000000000000000000000000000000000000000000000002";
+        final String resultDigest = "0x0000000000000000000000000000000000000000000000000000000000000001";
 
-        String resultHash = HashUtils.concatenateAndHash(chainTaskId, resultDigest);
-        String resultSeal = HashUtils.concatenateAndHash(Credentials.create(TestUtils.WORKER_PRIVATE).getAddress(), chainTaskId, resultDigest);
+        final String resultHash = HashUtils.concatenateAndHash(chainTaskId, resultDigest);
+        final String resultSeal = HashUtils.concatenateAndHash(Credentials.create(WORKER_PRIVATE).getAddress(), chainTaskId, resultDigest);
 
-        WorkerpoolAuthorization teeWorkerpoolAuth = TestUtils.getTeeWorkerpoolAuth();
-        teeWorkerpoolAuth.setEnclaveChallenge(TestUtils.ENCLAVE_ADDRESS);
+        final WorkerpoolAuthorization teeWorkerpoolAuth = getTeeWorkerpoolAuth();
         when(workerpoolAuthorizationService.getWorkerpoolAuthorization(chainTaskId)).thenReturn(teeWorkerpoolAuth);
         when(enclaveAuthorizationService.
                 isVerifiedEnclaveSignature(anyString(), anyString(), anyString(), anyString(), anyString()))
                 .thenReturn(true);
         when(iexecHubService.isTeeTask(chainTaskId)).thenReturn(true);
 
-        ComputedFile computedFile = ComputedFile.builder()
+        final ComputedFile computedFile = ComputedFile.builder()
                 .taskId(chainTaskId)
                 .resultDigest(resultDigest)
                 .enclaveSignature("0xenclaveSignature")
                 .build();
-        Contribution contribution = contributionService.getContribution(computedFile);
+        final Contribution contribution = contributionService.getContribution(computedFile);
 
         assertNotNull(contribution);
         assertEquals(chainTaskId, contribution.chainTaskId());
         assertEquals(resultDigest, contribution.resultDigest());
         assertEquals(resultSeal, contribution.resultSeal());
-        assertEquals(TestUtils.ENCLAVE_ADDRESS, contribution.enclaveChallenge());
+        assertEquals(ENCLAVE_ADDRESS, contribution.enclaveChallenge());
         assertEquals("0xenclaveSignature", contribution.enclaveSignature());
         assertEquals(teeWorkerpoolAuth.getSignature().getValue(), contribution.workerPoolSignature());
 
-        Contribution expectedContribution = Contribution.builder()
+        final Contribution expectedContribution = Contribution.builder()
                 .chainTaskId(chainTaskId)
                 .resultDigest(resultDigest)
                 .resultHash(resultHash)
                 .resultSeal(resultSeal)
-                .enclaveChallenge(TestUtils.ENCLAVE_ADDRESS)
+                .enclaveChallenge(ENCLAVE_ADDRESS)
                 .enclaveSignature("0xenclaveSignature")
                 .workerPoolSignature(teeWorkerpoolAuth.getSignature().getValue())
                 .build();
@@ -369,4 +373,15 @@ void getContributionWithTee() {
 
     }
 
+    private WorkerpoolAuthorization getTeeWorkerpoolAuth() {
+        final String hash = HashUtils.concatenateAndHash(WORKER_WALLET_ADDRESS, CHAIN_TASK_ID, ENCLAVE_ADDRESS);
+        final Signature signature = SignatureUtils.signMessageHashAndGetSignature(hash, POOL_PRIVATE);
+        return WorkerpoolAuthorization.builder()
+                .chainTaskId(CHAIN_TASK_ID)
+                .workerWallet(WORKER_WALLET_ADDRESS)
+                .enclaveChallenge(ENCLAVE_ADDRESS)
+                .signature(signature)
+                .build();
+    }
+
 }


Reviews:
user: login: jbern0rd
state: COMMENTED
author_association: CONTRIBUTOR
submitted_at: 2025-03-20T10:55:40Z
user: login: Natchica
state: COMMENTED
author_association: CONTRIBUTOR
submitted_at: 2025-03-20T11:37:50Z
user: login: jbern0rd
state: COMMENTED
author_association: CONTRIBUTOR
submitted_at: 2025-03-20T13:37:53Z
user: login: Natchica
state: COMMENTED
author_association: CONTRIBUTOR
submitted_at: 2025-03-20T13:46:06Z
user: login: jbern0rd
state: COMMENTED
author_association: CONTRIBUTOR
submitted_at: 2025-03-20T14:23:26Z
user: login: Natchica
state: COMMENTED
author_association: CONTRIBUTOR
submitted_at: 2025-03-20T14:42:35Z
user: login: jbern0rd
state: APPROVED
author_association: CONTRIBUTOR
submitted_at: 2025-03-20T14:43:25Z

Review comments:
diff_hunk: @@ -333,7 +336,7 @@ void getContributionWithTee() {
         String resultHash = HashUtils.concatenateAndHash(chainTaskId, resultDigest);
         String resultSeal = HashUtils.concatenateAndHash(Credentials.create(TestUtils.WORKER_PRIVATE).getAddress(), chainTaskId, resultDigest);
 
-        WorkerpoolAuthorization teeWorkerpoolAuth = TestUtils.getTeeWorkerpoolAuth();
+        WorkerpoolAuthorization teeWorkerpoolAuth = getTeeWorkerpoolAuth();
         teeWorkerpoolAuth.setEnclaveChallenge(TestUtils.ENCLAVE_ADDRESS);
path: src/test/java/com/iexec/worker/chain/ContributionServiceTests.java
user: login: jbern0rd
body: We want to remove this, the field is already defined with `ENCLAVE_ADDRESS` in `enclaveChallenge` field
created_at: 2025-03-20T10:55:32Z
author_association: CONTRIBUTOR
diff_hunk: @@ -333,7 +336,7 @@ void getContributionWithTee() {
         String resultHash = HashUtils.concatenateAndHash(chainTaskId, resultDigest);
         String resultSeal = HashUtils.concatenateAndHash(Credentials.create(TestUtils.WORKER_PRIVATE).getAddress(), chainTaskId, resultDigest);
 
-        WorkerpoolAuthorization teeWorkerpoolAuth = TestUtils.getTeeWorkerpoolAuth();
+        WorkerpoolAuthorization teeWorkerpoolAuth = getTeeWorkerpoolAuth();
         teeWorkerpoolAuth.setEnclaveChallenge(TestUtils.ENCLAVE_ADDRESS);
path: src/test/java/com/iexec/worker/chain/ContributionServiceTests.java
user: login: Natchica
body: Resolved in [0bf65b0](https://github.com/iExecBlockchainComputing/iexec-worker/pull/640/commits/0bf65b0e4087f228fbac022920e3f9850b036a01)
created_at: 2025-03-20T11:37:49Z
author_association: CONTRIBUTOR
diff_hunk: @@ -48,6 +49,10 @@ class ContributionServiceTests {
 
     private static final String CHAIN_DEAL_ID = "0x1566a9348a284d12f7d81fa017fbc440fd501ddef5746821860ffda7113eb847";
     private static final String WORKER_WALLET_ADDRESS = "0x49713c374C0D5259A0c0c4fCCd1254CdFd631b80";
+    private static final String ENCLAVE_ADDRESS = "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee";
+    public static final String CHAIN_TASK_ID = "0x1111111111111111111111111111111111111111111111111111111111111111";
+    public static final String POOL_PRIVATE = "0xe2a973b083fae8043543f15313955aecee9de809a318656c1cfb22d3a6d52de1";
+    public static final String WORKER_PRIVATE = "0xd0db6df0ebcd1d41439d91d86c5fc5c1806ee9cd8e71e3d5544bb7294b435c26";
path: src/test/java/com/iexec/worker/chain/ContributionServiceTests.java
user: login: jbern0rd
body: If not use elsewhere, we may want to make them `private`
created_at: 2025-03-20T13:37:29Z
author_association: CONTRIBUTOR
diff_hunk: @@ -48,6 +49,10 @@ class ContributionServiceTests {
 
     private static final String CHAIN_DEAL_ID = "0x1566a9348a284d12f7d81fa017fbc440fd501ddef5746821860ffda7113eb847";
     private static final String WORKER_WALLET_ADDRESS = "0x49713c374C0D5259A0c0c4fCCd1254CdFd631b80";
+    private static final String ENCLAVE_ADDRESS = "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee";
+    public static final String CHAIN_TASK_ID = "0x1111111111111111111111111111111111111111111111111111111111111111";
+    public static final String POOL_PRIVATE = "0xe2a973b083fae8043543f15313955aecee9de809a318656c1cfb22d3a6d52de1";
+    public static final String WORKER_PRIVATE = "0xd0db6df0ebcd1d41439d91d86c5fc5c1806ee9cd8e71e3d5544bb7294b435c26";
path: src/test/java/com/iexec/worker/chain/ContributionServiceTests.java
user: login: Natchica
body: Resolved in [bcefad1](https://github.com/iExecBlockchainComputing/iexec-worker/pull/640/commits/bcefad1870c1c7ed4b029ff74d6def75444729b3)
created_at: 2025-03-20T13:46:05Z
author_association: CONTRIBUTOR
diff_hunk: @@ -327,46 +332,56 @@ void getContribution() {
 
     @Test
     void getContributionWithTee() {
-        String chainTaskId = "0x0000000000000000000000000000000000000000000000000000000000000002";
-        String resultDigest = "0x0000000000000000000000000000000000000000000000000000000000000001";
+        final String chainTaskId = "0x0000000000000000000000000000000000000000000000000000000000000002";
+        final String resultDigest = "0x0000000000000000000000000000000000000000000000000000000000000001";
 
-        String resultHash = HashUtils.concatenateAndHash(chainTaskId, resultDigest);
-        String resultSeal = HashUtils.concatenateAndHash(Credentials.create(TestUtils.WORKER_PRIVATE).getAddress(), chainTaskId, resultDigest);
+        final String resultHash = HashUtils.concatenateAndHash(chainTaskId, resultDigest);
+        final String resultSeal = HashUtils.concatenateAndHash(Credentials.create(WORKER_PRIVATE).getAddress(), chainTaskId, resultDigest);
 
-        WorkerpoolAuthorization teeWorkerpoolAuth = TestUtils.getTeeWorkerpoolAuth();
-        teeWorkerpoolAuth.setEnclaveChallenge(TestUtils.ENCLAVE_ADDRESS);
+        WorkerpoolAuthorization teeWorkerpoolAuth = getTeeWorkerpoolAuth();
path: src/test/java/com/iexec/worker/chain/ContributionServiceTests.java
user: login: jbern0rd
body: ```suggestion
        final WorkerpoolAuthorization teeWorkerpoolAuth = getTeeWorkerpoolAuth();
```
created_at: 2025-03-20T14:23:13Z
author_association: CONTRIBUTOR
diff_hunk: @@ -327,46 +332,56 @@ void getContribution() {
 
     @Test
     void getContributionWithTee() {
-        String chainTaskId = "0x0000000000000000000000000000000000000000000000000000000000000002";
-        String resultDigest = "0x0000000000000000000000000000000000000000000000000000000000000001";
+        final String chainTaskId = "0x0000000000000000000000000000000000000000000000000000000000000002";
+        final String resultDigest = "0x0000000000000000000000000000000000000000000000000000000000000001";
 
-        String resultHash = HashUtils.concatenateAndHash(chainTaskId, resultDigest);
-        String resultSeal = HashUtils.concatenateAndHash(Credentials.create(TestUtils.WORKER_PRIVATE).getAddress(), chainTaskId, resultDigest);
+        final String resultHash = HashUtils.concatenateAndHash(chainTaskId, resultDigest);
+        final String resultSeal = HashUtils.concatenateAndHash(Credentials.create(WORKER_PRIVATE).getAddress(), chainTaskId, resultDigest);
 
-        WorkerpoolAuthorization teeWorkerpoolAuth = TestUtils.getTeeWorkerpoolAuth();
-        teeWorkerpoolAuth.setEnclaveChallenge(TestUtils.ENCLAVE_ADDRESS);
+        WorkerpoolAuthorization teeWorkerpoolAuth = getTeeWorkerpoolAuth();
path: src/test/java/com/iexec/worker/chain/ContributionServiceTests.java
user: login: Natchica
body: Resolved in [8bd2ea3](https://github.com/iExecBlockchainComputing/iexec-worker/pull/640/commits/8bd2ea3c7fe26f470c725da243783549f35fcde1)
created_at: 2025-03-20T14:42:35Z
author_association: CONTRIBUTOR

PR comments:
user: login: sonarqubecloud[bot]
created_at: 2025-03-20T14:51:26Z
body: ## [![Quality Gate Passed](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/QualityGateBadge/qg-passed-20px.png 'Quality Gate Passed')](https://sonarcloud.io/dashboard?id=com.iexec.worker%3Aiexec-worker&pullRequest=640) **Quality Gate passed**  
Issues  
![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0 New issues](https://sonarcloud.io/project/issues?id=com.iexec.worker%3Aiexec-worker&pullRequest=640&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/accepted-16px.png '') [0 Accepted issues](https://sonarcloud.io/project/issues?id=com.iexec.worker%3Aiexec-worker&pullRequest=640&issueStatuses=ACCEPTED)

Measures  
![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=com.iexec.worker%3Aiexec-worker&pullRequest=640&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0.0% Coverage on New Code](https://sonarcloud.io/component_measures?id=com.iexec.worker%3Aiexec-worker&pullRequest=640&metric=new_coverage&view=list)  
![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0.0% Duplication on New Code](https://sonarcloud.io/component_measures?id=com.iexec.worker%3Aiexec-worker&pullRequest=640&metric=new_duplicated_lines_density&view=list)  
  
[See analysis details on SonarQube Cloud](https://sonarcloud.io/dashboard?id=com.iexec.worker%3Aiexec-worker&pullRequest=640)


author_association: NONE