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

Replaced deprecated PackageInfo.signatures with PackageInfo.signingInfo #1587

Merged
merged 13 commits into from
Apr 15, 2021
Merged
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 = Mockito.mock(PackageInfo.class);
mockedPackageInfo.signatures = new Signature[]{mockedSignature};
final PackageInfo mockedPackageInfo = com.microsoft.identity.common.Util.addSignatures(Mockito.mock(PackageInfo.class), new Signature[]{mockedSignature});

final PackageManager mockedPackageManager = Mockito.mock(PackageManager.class);
when(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

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

import junit.framework.Assert;

Expand Down Expand Up @@ -81,10 +82,7 @@ public void setUp() throws Exception {
.getInstrumentation()
.getContext();

PackageInfo info = context.getPackageManager()
.getPackageInfo(context.getPackageName(), PackageManager.GET_SIGNATURES);

for (Signature signature : info.signatures) {
for (Signature signature : PackageHelper.getSignatures(context)) {
mTestSignature = signature.toByteArray();
MessageDigest md = MessageDigest.getInstance("SHA");
md.update(mTestSignature);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import com.microsoft.identity.common.adal.internal.AuthenticationConstants;
import com.microsoft.identity.common.adal.internal.cache.StorageHelper;
import com.microsoft.identity.common.adal.internal.net.HttpUrlConnectionFactory;
import com.microsoft.identity.common.internal.broker.PackageHelper;
import com.microsoft.identity.common.internal.cache.CacheKeyValueDelegate;
import com.microsoft.identity.common.internal.cache.IAccountCredentialCache;
import com.microsoft.identity.common.internal.cache.MicrosoftStsAccountCredentialAdapter;
Expand Down Expand Up @@ -195,12 +196,12 @@ 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(), PackageManager.GET_SIGNATURES);
.getPackageInfo(getInstrumentation().getContext().getPackageName(), PackageHelper.getPackageManagerFlag());

// Broker App can be signed with multiple certificates. It will look
// all of them
// until it finds the correct one for ADAL broker.
for (Signature signature : info.signatures) {
for (Signature signature : PackageHelper.getSignatures(info)) {
final byte[] testSignature = signature.toByteArray();
MessageDigest md = MessageDigest.getInstance("SHA");
md.update(testSignature);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@
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;

import junit.framework.Assert;

Expand Down Expand Up @@ -377,8 +379,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 = Mockito.mock(PackageInfo.class);
packageInfo.signatures = new Signature[]{signature};
final PackageInfo packageInfo = Util.addSignatures(Mockito.mock(PackageInfo.class), new Signature[]{signature});

Mockito.when(packageManager.getPackageInfo(Mockito.anyString(), Mockito.anyInt())).thenReturn(packageInfo);

Mockito.when(packageManager.checkPermission(Mockito.contains("android.permission.GET_ACCOUNTS"),
Expand All @@ -387,15 +389,14 @@ private void mockPackageManagerBrokerSignatureAndPermission(final PackageManager

private SignatureData getSignature(final Context context, final String packageName)
throws PackageManager.NameNotFoundException, NoSuchAlgorithmException {
PackageInfo info = context.getPackageManager().getPackageInfo(packageName,
PackageManager.GET_SIGNATURES);
PackageInfo info = PackageHelper.getPackageInfo(context.getPackageManager(), packageName);

// Broker App can be signed with multiple certificates. It will look
// all of them
// until it finds the correct one for ADAL broker.
byte[] signatureByte;
String signatureTag;
for (final Signature signature : info.signatures) {
for (final Signature signature : PackageHelper.getSignatures(info)) {
signatureByte = signature.toByteArray();
MessageDigest md = MessageDigest.getInstance("SHA");
md.update(signatureByte);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@
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;

import junit.framework.Assert;

Expand Down Expand Up @@ -128,13 +130,13 @@ public void setUp() throws Exception {
.getPackageInfo(
androidx.test.platform.app.InstrumentationRegistry.getInstrumentation()
.getContext().getPackageName(),
PackageManager.GET_SIGNATURES
PackageHelper.getPackageManagerFlag()
);

// Broker App can be signed with multiple certificates. It will look
// all of them
// until it finds the correct one for ADAL broker.
for (Signature signature : info.signatures) {
for (Signature signature : PackageHelper.getSignatures(info)) {
mTestSignature = signature.toByteArray();
MessageDigest md = MessageDigest.getInstance("SHA");
md.update(mTestSignature);
Expand Down Expand Up @@ -1172,11 +1174,9 @@ private Context getMockContext(final Signature signature, final String brokerPac
private PackageManager getPackageManager(final Signature signature, final String packageName,
boolean permissionStatus) throws NameNotFoundException {
PackageManager mockPackage = mock(PackageManager.class);
PackageInfo info = new PackageInfo();
Signature[] signatures = new Signature[1];
signatures[0] = signature;
info.signatures = signatures;
when(mockPackage.getPackageInfo(packageName, PackageManager.GET_SIGNATURES)).thenReturn(info);
PackageInfo info = Util.addSignatures(new PackageInfo(), new Signature[]{signature});

when(mockPackage.getPackageInfo(packageName, PackageHelper.getPackageManagerFlag())).thenReturn(info);
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
Expand Up @@ -33,6 +33,7 @@
import androidx.test.core.app.ApplicationProvider;

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

import org.junit.Before;

Expand Down Expand Up @@ -70,14 +71,10 @@ protected void setUp() throws Exception {
.getPath()
);

// ADAL is set to this signature for now
PackageInfo info = mContext.getPackageManager().getPackageInfo(mContext.getPackageName(),
PackageManager.GET_SIGNATURES);

// Broker App can be signed with multiple certificates. It will look
// all of them
// until it finds the correct one for ADAL broker.
for (Signature signature : info.signatures) {
for (Signature signature : PackageHelper.getSignatures(mContext)) {
mTestSignature = signature.toByteArray();
MessageDigest md = MessageDigest.getInstance("SHA");
md.update(mTestSignature);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@

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;

import org.junit.After;
import org.junit.Before;
Expand Down Expand Up @@ -91,14 +93,11 @@ public void setUp() throws Exception {

AuthenticationSettings.INSTANCE.setBrokerPackageName("invalid_do_not_switch");
AuthenticationSettings.INSTANCE.setBrokerSignature("invalid_do_not_switch");
// ADAL is set to this signature for now
PackageInfo info = mContext.getPackageManager().getPackageInfo(mContext.getPackageName(),
PackageManager.GET_SIGNATURES);

// Broker App can be signed with multiple certificates. It will look
// all of them
// until it finds the correct one for ADAL broker.
for (final Signature signature : info.signatures) {
for (final Signature signature : PackageHelper.getSignatures(mContext)) {
mTestSignature = signature.toByteArray();
MessageDigest md = MessageDigest.getInstance("SHA");
md.update(mTestSignature);
Expand Down Expand Up @@ -221,19 +220,15 @@ private PackageManager getPackageManager(final Signature signature,
final String packageName,
final int callingUID) throws NameNotFoundException {
final PackageManager mockPackage = mock(PackageManager.class);
final PackageInfo info = new PackageInfo();

final Signature[] signatures = new Signature[1];
signatures[0] = signature;
info.signatures = signatures;
final PackageInfo info = Util.addSignatures(new PackageInfo(), new Signature[]{signature});

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

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 = Mockito.mock(PackageInfo.class);
mockedPackageInfo.signatures = new Signature[]{mockedSignature};
final PackageInfo mockedPackageInfo = com.microsoft.identity.common.Util.addSignatures(Mockito.mock(PackageInfo.class), new Signature[]{mockedSignature});

final PackageManager mockedPackageManager = Mockito.mock(PackageManager.class);
when(mockedPackageManager.getPackageInfo(Mockito.anyString(), anyInt())).thenReturn(mockedPackageInfo);
Expand Down
1 change: 1 addition & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ v.Next
- [PATCH] Fixes for MFA setup using Authenticator app.(#1579)
- [MINOR] Changes to Broker Validation to allow setting whether to trust debug brokers (prod brokers are always trusted).
- [PATCH] Replaced deprecated PackageInfo.versionCode with PackageInfoCompat.getLongVersionCode(packageInfo) (#1584)
- [PATCH] Fixes deprecated PackageInfo.signatures (#1587)
- [PATCH] Deprecated ADAL namespaced AuthenticationSettings#setSecretKey(), #getSecretKey() (#1586)
- Picks up [email protected]

Expand Down