Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for mocking final classes in some instrumented tests #1602

Merged
merged 18 commits into from
Jul 26, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2296,8 +2296,7 @@ private PackageManager getMockedPackageManager() throws PackageManager.NameNotFo
mockedSignature.toByteArray()
).thenReturn(Base64.decode(Util.ENCODED_SIGNATURE, Base64.NO_WRAP));

final PackageInfo mockedPackageInfo = com.microsoft.identity.common.Util.addSignatures(Mockito.mock(PackageInfo.class), new Signature[]{mockedSignature});

final PackageInfo mockedPackageInfo = new MockedPackageInfo(new Signature[]{mockedSignature});
final PackageManager mockedPackageManager = Mockito.mock(PackageManager.class);
when(
mockedPackageManager.getPackageInfo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ public void setUp() throws Exception {
AuthenticationSettings.INSTANCE.setUseBroker(false);
// ADAL is set to this signature for now
PackageInfo info = getInstrumentation().getContext().getPackageManager()
.getPackageInfo(getInstrumentation().getContext().getPackageName(), PackageHelper.getPackageManagerFlag());
.getPackageInfo(getInstrumentation().getContext().getPackageName(), PackageHelper.getPackageManagerSignaturesFlag());

// Broker App can be signed with multiple certificates. It will look
// all of them
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
import androidx.test.core.app.ApplicationProvider;
import androidx.test.rule.ServiceTestRule;

import com.microsoft.identity.common.Util;
import com.microsoft.identity.common.adal.internal.AuthenticationConstants;
import com.microsoft.identity.common.internal.broker.PackageHelper;

Expand Down Expand Up @@ -379,9 +378,8 @@ private void mockPackageManagerBrokerSignatureAndPermission(final PackageManager
Mockito.when(packageManager.checkPermission(Mockito.contains("android.permission.GET_ACCOUNTS"),
Mockito.anyString())).thenReturn(PackageManager.PERMISSION_DENIED);

final PackageInfo packageInfo = Util.addSignatures(Mockito.mock(PackageInfo.class), new Signature[]{signature});

Mockito.when(packageManager.getPackageInfo(Mockito.anyString(), Mockito.anyInt())).thenReturn(packageInfo);
final PackageInfo mockedPackageInfo = new MockedPackageInfo(new Signature[]{signature});
Mockito.when(packageManager.getPackageInfo(Mockito.anyString(), Mockito.anyInt())).thenReturn(mockedPackageInfo);

Mockito.when(packageManager.checkPermission(Mockito.contains("android.permission.GET_ACCOUNTS"),
Mockito.anyString())).thenReturn(PackageManager.PERMISSION_DENIED);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.ResolveInfo;
import android.content.pm.Signature;
import android.content.pm.SigningInfo;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
Expand All @@ -49,7 +50,6 @@
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;

import com.microsoft.identity.common.Util;
import com.microsoft.identity.common.adal.internal.AuthenticationConstants;
import com.microsoft.identity.common.internal.broker.BrokerValidator;
import com.microsoft.identity.common.internal.broker.PackageHelper;
Expand Down Expand Up @@ -130,7 +130,7 @@ public void setUp() throws Exception {
.getPackageInfo(
androidx.test.platform.app.InstrumentationRegistry.getInstrumentation()
.getContext().getPackageName(),
PackageHelper.getPackageManagerFlag()
PackageHelper.getPackageManagerSignaturesFlag()
);

// Broker App can be signed with multiple certificates. It will look
Expand Down Expand Up @@ -1170,13 +1170,11 @@ private Context getMockContext(final Signature signature, final String brokerPac
return mockContext;
}

@SuppressLint("PackageManagerGetSignatures")
private PackageManager getPackageManager(final Signature signature, final String packageName,
boolean permissionStatus) throws NameNotFoundException {
PackageManager mockPackage = mock(PackageManager.class);
PackageInfo info = Util.addSignatures(new PackageInfo(), new Signature[]{signature});

when(mockPackage.getPackageInfo(packageName, PackageHelper.getPackageManagerFlag())).thenReturn(info);
final PackageInfo mockedPackageInfo = new MockedPackageInfo(new Signature[]{signature});
paulkagiri marked this conversation as resolved.
Show resolved Hide resolved
when(mockPackage.getPackageInfo(packageName, PackageHelper.getPackageManagerSignaturesFlag())).thenReturn(mockedPackageInfo);
when(mockPackage.checkPermission(anyString(), anyString()))
.thenReturn(permissionStatus ? PackageManager.PERMISSION_GRANTED : PackageManager.PERMISSION_DENIED);
return mockPackage;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.microsoft.aad.adal;
paulkagiri marked this conversation as resolved.
Show resolved Hide resolved

import android.content.pm.PackageInfo;
import android.content.pm.Signature;

public class MockedPackageInfo extends PackageInfo {

public MockedSigningInfo signingInfo;

public MockedPackageInfo(Signature [] signatures) {
this.signingInfo = new MockedSigningInfo(signatures);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.microsoft.aad.adal;
paulkagiri marked this conversation as resolved.
Show resolved Hide resolved

import android.content.pm.Signature;

public class MockedSigningInfo {
paulkagiri marked this conversation as resolved.
Show resolved Hide resolved

private final Signature[] signatures;

public MockedSigningInfo(Signature[] signatures) {
this.signatures = signatures;
}

public boolean hasMultipleSigners() {
return false;
}

public Signature[] getSigningCertificateHistory() {
return signatures;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@

import androidx.test.ext.junit.runners.AndroidJUnit4;

import com.microsoft.identity.common.Util;
import com.microsoft.identity.common.adal.internal.AuthenticationConstants;
import com.microsoft.identity.common.internal.broker.PackageHelper;

Expand Down Expand Up @@ -220,17 +219,17 @@ private PackageManager getPackageManager(final Signature signature,
final String packageName,
final int callingUID) throws NameNotFoundException {
final PackageManager mockPackage = mock(PackageManager.class);
final PackageInfo info = Util.addSignatures(new PackageInfo(), new Signature[]{signature});
final PackageInfo mockedPackageInfo = new MockedPackageInfo(new Signature[]{signature});

final ApplicationInfo appInfo = new ApplicationInfo();
appInfo.name = packageName;
appInfo.uid = callingUID;
when(
mockPackage.getPackageInfo(
packageName,
PackageHelper.getPackageManagerFlag()
PackageHelper.getPackageManagerSignaturesFlag()
)
).thenReturn(info);
).thenReturn(mockedPackageInfo);

when(mockPackage.getApplicationInfo(packageName, 0)).thenReturn(appInfo);
Context mock = mock(Context.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,7 @@ private PackageManager getMockedPackageManager() throws PackageManager.NameNotFo
when(mockedSignature.toByteArray()).thenReturn(Base64.decode(
Util.ENCODED_SIGNATURE, Base64.NO_WRAP));

final PackageInfo mockedPackageInfo = com.microsoft.identity.common.Util.addSignatures(Mockito.mock(PackageInfo.class), new Signature[]{mockedSignature});

final PackageInfo mockedPackageInfo = new MockedPackageInfo(new Signature[]{mockedSignature});
final PackageManager mockedPackageManager = Mockito.mock(PackageManager.class);
when(mockedPackageManager.getPackageInfo(Mockito.anyString(), anyInt())).thenReturn(mockedPackageInfo);

Expand Down
7 changes: 7 additions & 0 deletions adal/src/androidTest/java/com/microsoft/aad/adal/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
// THE SOFTWARE.
package com.microsoft.aad.adal;

import android.content.pm.PackageInfo;
import android.content.pm.Signature;
import android.content.pm.SigningInfo;
import android.os.Build;
import android.util.Base64;

import com.microsoft.identity.common.adal.internal.AuthenticationConstants;
Expand Down Expand Up @@ -51,6 +55,9 @@
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

final class Util {
public static final int TEST_PASSWORD_EXPIRATION = 1387227772;
static final String TEST_IDTOKEN = "eyJ0eXAiOiJKV1QiLCJhbGciOiJub25lIn0.eyJhdWQiOiJlNzBiMTE1ZS1hYzBhLTQ4MjMtODVkYS04ZjRiN2I0ZjAwZTYiLCJpc3MiOiJodHRwczovL3N0cy53aW5kb3dzLm5ldC8zMGJhYTY2Ni04ZGY4LTQ4ZTctOTdlNi03N2NmZDA5OTU5NjMvIiwibmJmIjoxMzc2NDI4MzEwLCJleHAiOjEzNzY0NTcxMTAsInZlciI6IjEuMCIsInRpZCI6IjMwYmFhNjY2LThkZjgtNDhlNy05N2U2LTc3Y2ZkMDk5NTk2MyIsIm9pZCI6IjRmODU5OTg5LWEyZmYtNDExZS05MDQ4LWMzMjIyNDdhYzYyYyIsInVwbiI6ImFkbWluQGFhbHRlc3RzLm9ubWljcm9zb2Z0LmNvbSIsInVuaXF1ZV9uYW1lIjoiYWRtaW5AYWFsdGVzdHMub25taWNyb3NvZnQuY29tIiwic3ViIjoiVDU0V2hGR1RnbEJMN1VWYWtlODc5UkdhZEVOaUh5LXNjenNYTmFxRF9jNCIsImZhbWlseV9uYW1lIjoiU2VwZWhyaSIsImdpdmVuX25hbWUiOiJBZnNoaW4ifQ.";
Expand Down
2 changes: 1 addition & 1 deletion common
Submodule common updated 112 files