2424
2525import android .os .Build ;
2626
27+ import androidx .annotation .NonNull ;
2728import androidx .test .core .app .ApplicationProvider ;
2829
2930import com .microsoft .identity .common .java .crypto .IDevicePopManager ;
3031import com .microsoft .identity .common .java .exception .ClientException ;
32+ import com .microsoft .identity .common .java .flighting .CommonFlight ;
33+ import com .microsoft .identity .common .java .flighting .CommonFlightsManager ;
34+ import com .microsoft .identity .common .java .flighting .IFlightsManager ;
35+ import com .microsoft .identity .common .java .flighting .IFlightsProvider ;
3136
37+ import org .jetbrains .annotations .NotNull ;
3238import org .junit .After ;
3339import org .junit .Assert ;
3440import org .junit .Before ;
3541import org .junit .Test ;
3642import org .junit .runner .RunWith ;
3743import org .junit .runners .Parameterized ;
44+ import org .mockito .Mockito ;
3845
3946import java .io .IOException ;
47+ import java .security .InvalidKeyException ;
4048import java .security .KeyStoreException ;
4149import java .security .NoSuchAlgorithmException ;
4250import java .security .cert .CertificateException ;
@@ -92,6 +100,7 @@ public void setUp() throws ClientException {
92100 @ After
93101 public void tearDown () {
94102 devicePopManager .clearAsymmetricKey ();
103+ CommonFlightsManager .INSTANCE .resetFlightsManager ();
95104 }
96105
97106 @ Test
@@ -102,4 +111,46 @@ public void testEncryption() throws ClientException {
102111 Assert .assertEquals (DATA_TO_ENCRYPT , decryptedValue );
103112 Assert .assertNotEquals (DATA_TO_ENCRYPT , cipherText );
104113 }
114+
115+ @ Test
116+ public void testEncryption_Disabled () throws ClientException , CertificateException , KeyStoreException , NoSuchAlgorithmException , IOException {
117+ final IFlightsProvider mockFlightsProvider = Mockito .mock (IFlightsProvider .class );
118+ Mockito .when (mockFlightsProvider .isFlightEnabled (CommonFlight .DISABLE_UNNECESSARY_CRYPTO_PURPOSES_FROM_DEVICE_POP_MANAGER ))
119+ .thenReturn (true );
120+ // Create anonymous IFlightsManager
121+ IFlightsManager anonymousFlightsManager = new IFlightsManager () {
122+ @ Override
123+ public @ NotNull IFlightsProvider getFlightsProvider (long waitForConfigsWithTimeoutInMs ) {
124+ return mockFlightsProvider ;
125+ }
126+ @ Override
127+ public @ NotNull IFlightsProvider getFlightsProviderForTenant (@ NotNull String tenantId , long waitForConfigsWithTimeoutInMs ) {
128+ return mockFlightsProvider ;
129+ }
130+ @ Override
131+ public @ NotNull IFlightsProvider getFlightsProviderForTenant (@ NotNull String tenantId ) {
132+ return mockFlightsProvider ;
133+ }
134+ @ NonNull
135+ @ Override
136+ public IFlightsProvider getFlightsProvider () {
137+ return mockFlightsProvider ;
138+ }
139+ };
140+
141+ // Initialize CommonFlightsManager with the anonymous implementation
142+ CommonFlightsManager .INSTANCE .initializeCommonFlightsManager (anonymousFlightsManager );
143+ IDevicePopManager devicePopManager = new AndroidDevicePopManager (ApplicationProvider .getApplicationContext ());
144+ devicePopManager .generateAsymmetricKey ();
145+ try {
146+ final String cipherText = devicePopManager .encrypt (cipher , DATA_TO_ENCRYPT );
147+ devicePopManager .decrypt (cipher , cipherText );
148+ } catch (Exception exception ) {
149+ Assert .assertTrue (exception instanceof ClientException );
150+ Assert .assertTrue (exception .getCause ().getCause ().getMessage ().contains ("Incompatible purpose" ));
151+ return ;
152+ }
153+ Assert .fail ();
154+
155+ }
105156}
0 commit comments