11/*
2- * Copyright 2020-2023 IEXEC BLOCKCHAIN TECH
2+ * Copyright 2020-2024 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.
2323import com .iexec .sms .tee .session .base .SecretEnclaveBase ;
2424import com .iexec .sms .tee .session .base .SecretSessionBase ;
2525import com .iexec .sms .tee .session .base .SecretSessionBaseService ;
26+ import com .iexec .sms .tee .session .generic .TeeSessionGenerationException ;
2627import com .iexec .sms .tee .session .generic .TeeSessionRequest ;
2728import com .iexec .sms .tee .session .scone .cas .SconeSession ;
2829import lombok .extern .slf4j .Slf4j ;
2930import org .junit .jupiter .api .BeforeEach ;
31+ import org .junit .jupiter .api .Nested ;
3032import org .junit .jupiter .api .Test ;
33+ import org .junit .jupiter .api .extension .ExtendWith ;
3134import org .mockito .InjectMocks ;
3235import org .mockito .Mock ;
33- import org .mockito .MockitoAnnotations ;
36+ import org .mockito .junit . jupiter . MockitoExtension ;
3437import org .yaml .snakeyaml .Yaml ;
3538
39+ import java .net .MalformedURLException ;
40+ import java .net .URI ;
41+ import java .net .URISyntaxException ;
42+ import java .net .URL ;
3643import java .util .List ;
3744import java .util .Map ;
3845
3946import static com .iexec .sms .tee .session .TeeSessionTestUtils .*;
4047import static org .mockito .Mockito .when ;
4148
4249@ Slf4j
50+ @ ExtendWith (MockitoExtension .class )
4351class SconeSessionMakerServiceTests {
4452
4553 private static final String PRE_COMPUTE_ENTRYPOINT = "entrypoint1" ;
4654 private static final String APP_FINGERPRINT = "01ba4719c80b6fe911b091a7c05124b64eeece964e09c058ef8f9805daca546b" ;
4755 private static final String APP_ENTRYPOINT = "appEntrypoint" ;
4856 private static final String POST_COMPUTE_ENTRYPOINT = "entrypoint3" ;
57+ private static final URL MAA_URL ;
58+
59+ static {
60+ try {
61+ MAA_URL = new URI ("https://maa.attestation.service" ).toURL ();
62+ } catch (MalformedURLException | URISyntaxException e ) {
63+ throw new RuntimeException (e );
64+ }
65+ }
66+
67+ private final List <String > toleratedInsecureOptions = List .of ("hyperthreading" , "debug-mode" );
68+ private final List <String > ignoredSgxAdvisories = List .of ("INTEL-SA-00161" , "INTEL-SA-00289" );
4969
5070 private final TeeAppProperties preComputeProperties = TeeAppProperties .builder ()
5171 .image ("PRE_COMPUTE_IMAGE" )
@@ -63,27 +83,29 @@ class SconeSessionMakerServiceTests {
6383 private SconeServicesProperties teeServicesConfig ;
6484 @ Mock
6585 private SecretSessionBaseService teeSecretsService ;
66- @ Mock
86+
6787 private SconeSessionSecurityConfig attestationSecurityConfig ;
6888
6989 @ InjectMocks
7090 private SconeSessionMakerService palaemonSessionService ;
7191
72- @ BeforeEach
73- void beforeEach () {
74- MockitoAnnotations .openMocks (this );
92+ private TeeSessionRequest request ;
93+
94+ private void setupCommonMocks () throws TeeSessionGenerationException {
95+ palaemonSessionService = new SconeSessionMakerService (
96+ teeSecretsService ,
97+ teeServicesConfig ,
98+ attestationSecurityConfig
99+ );
100+
75101 when (teeServicesConfig .getPreComputeProperties ()).thenReturn (preComputeProperties );
76102 when (teeServicesConfig .getPostComputeProperties ()).thenReturn (postComputeProperties );
77- }
78103
79- // region getSessionYml
80- @ Test
81- void shouldGetSessionYml () throws Exception {
82104 TeeEnclaveConfiguration enclaveConfig = TeeEnclaveConfiguration .builder ()
83105 .fingerprint (APP_FINGERPRINT )
84106 .entrypoint (APP_ENTRYPOINT )
85107 .build ();
86- TeeSessionRequest request = createSessionRequest (createTaskDescription (enclaveConfig ).build ());
108+ request = createSessionRequest (createTaskDescription (enclaveConfig ).build ());
87109
88110 SecretEnclaveBase preCompute = SecretEnclaveBase .builder ()
89111 .name ("pre-compute" )
@@ -141,25 +163,57 @@ void shouldGetSessionYml() throws Exception {
141163 .appCompute (appCompute )
142164 .postCompute (postCompute )
143165 .build ());
166+ }
144167
145- when (attestationSecurityConfig .getToleratedInsecureOptions ())
146- .thenReturn (List .of ("hyperthreading" , "debug-mode" ));
147- when (attestationSecurityConfig .getIgnoredSgxAdvisories ())
148- .thenReturn (List .of ("INTEL-SA-00161" , "INTEL-SA-00289" ));
168+ // region HardwareModeTests
169+ @ Nested
170+ class HardwareModeTests {
171+ @ BeforeEach
172+ void setup () throws TeeSessionGenerationException {
173+ attestationSecurityConfig = new SconeSessionSecurityConfig (
174+ toleratedInsecureOptions ,
175+ ignoredSgxAdvisories ,
176+ "hardware" ,
177+ null
178+ );
179+ setupCommonMocks ();
180+ }
149181
150- when (teeSecretsService .getSecretsTokens (request ))
151- .thenReturn (SecretSessionBase .builder ()
152- .preCompute (preCompute )
153- .appCompute (appCompute )
154- .postCompute (postCompute )
155- .build ());
182+ @ Test
183+ void shouldGenerateHardwareSession () throws Exception {
184+ SconeSession actualCasSession = palaemonSessionService .generateSession (request );
185+ log .info (actualCasSession .toString ());
186+ Map <String , Object > actualYmlMap = new Yaml ().load (actualCasSession .toString ());
187+ String expectedYamlString = FileHelper .readFile ("src/test/resources/palaemon-tee-session-hardware.yml" );
188+ Map <String , Object > expectedYmlMap = new Yaml ().load (expectedYamlString );
189+ assertRecursively (expectedYmlMap , actualYmlMap );
190+ }
191+ }
192+ // endregion
193+
194+ // region MaaModeTests
195+ @ Nested
196+ class MaaModeTests {
197+ @ BeforeEach
198+ void setup () throws TeeSessionGenerationException {
199+ attestationSecurityConfig = new SconeSessionSecurityConfig (
200+ toleratedInsecureOptions ,
201+ ignoredSgxAdvisories ,
202+ "maa" ,
203+ MAA_URL
204+ );
205+ setupCommonMocks ();
206+ }
156207
157- SconeSession actualCasSession = palaemonSessionService .generateSession (request );
158- log .info (actualCasSession .toString ());
159- Map <String , Object > actualYmlMap = new Yaml ().load (actualCasSession .toString ());
160- String expectedYamlString = FileHelper .readFile ("src/test/resources/palaemon-tee-session.yml" );
161- Map <String , Object > expectedYmlMap = new Yaml ().load (expectedYamlString );
162- assertRecursively (expectedYmlMap , actualYmlMap );
208+ @ Test
209+ void shouldGenerateMaaSession () throws Exception {
210+ SconeSession actualCasSession = palaemonSessionService .generateSession (request );
211+ log .info (actualCasSession .toString ());
212+ Map <String , Object > actualYmlMap = new Yaml ().load (actualCasSession .toString ());
213+ String expectedYamlString = FileHelper .readFile ("src/test/resources/palaemon-tee-session-maa.yml" );
214+ Map <String , Object > expectedYmlMap = new Yaml ().load (expectedYamlString );
215+ assertRecursively (expectedYmlMap , actualYmlMap );
216+ }
163217 }
164218 // endregion
165219}
0 commit comments