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 6 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,36 @@
// Copyright (c) Microsoft Corporation.
// All rights reserved.
//
// This code is licensed under the MIT License.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files(the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions :
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

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,43 @@
// Copyright (c) Microsoft Corporation.
// All rights reserved.
//
// This code is licensed under the MIT License.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files(the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions :
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

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
2 changes: 1 addition & 1 deletion common
Submodule common updated 248 files