From 0dd8b3423101ea1f07808befd6f23438cc1545df Mon Sep 17 00:00:00 2001 From: Paul Kagiri Date: Tue, 4 May 2021 09:53:01 +0300 Subject: [PATCH 01/13] common update --- common | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common b/common index de4605992..69385b457 160000 --- a/common +++ b/common @@ -1 +1 @@ -Subproject commit de460599261f1b1c750d4f915d341934c3fa7abf +Subproject commit 69385b4576fb408c4f013d09fa3e3d5ec6cf648f From 107cc9e3bae02d41987c956d2e0214fb34f56e57 Mon Sep 17 00:00:00 2001 From: Paul Kagiri Date: Tue, 4 May 2021 21:35:59 +0300 Subject: [PATCH 02/13] fixed failing tests --- .../aad/adal/AcquireTokenRequestTest.java | 3 +-- .../aad/adal/AuthenticationContextTest.java | 2 +- .../aad/adal/BrokerAccountServiceTest.java | 6 ++---- .../microsoft/aad/adal/BrokerProxyTests.java | 10 ++++------ .../microsoft/aad/adal/MockedPackageInfo.java | 13 ++++++++++++ .../microsoft/aad/adal/MockedSigningInfo.java | 20 +++++++++++++++++++ .../aad/adal/PackageHelperTests.java | 7 +++---- .../com/microsoft/aad/adal/TelemetryTest.java | 3 +-- .../java/com/microsoft/aad/adal/Util.java | 7 +++++++ common | 2 +- 10 files changed, 53 insertions(+), 20 deletions(-) create mode 100644 adal/src/androidTest/java/com/microsoft/aad/adal/MockedPackageInfo.java create mode 100644 adal/src/androidTest/java/com/microsoft/aad/adal/MockedSigningInfo.java diff --git a/adal/src/androidTest/java/com/microsoft/aad/adal/AcquireTokenRequestTest.java b/adal/src/androidTest/java/com/microsoft/aad/adal/AcquireTokenRequestTest.java index 50d18d118..a4c29ed36 100644 --- a/adal/src/androidTest/java/com/microsoft/aad/adal/AcquireTokenRequestTest.java +++ b/adal/src/androidTest/java/com/microsoft/aad/adal/AcquireTokenRequestTest.java @@ -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( diff --git a/adal/src/androidTest/java/com/microsoft/aad/adal/AuthenticationContextTest.java b/adal/src/androidTest/java/com/microsoft/aad/adal/AuthenticationContextTest.java index 760ee0226..61ebc616d 100644 --- a/adal/src/androidTest/java/com/microsoft/aad/adal/AuthenticationContextTest.java +++ b/adal/src/androidTest/java/com/microsoft/aad/adal/AuthenticationContextTest.java @@ -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 diff --git a/adal/src/androidTest/java/com/microsoft/aad/adal/BrokerAccountServiceTest.java b/adal/src/androidTest/java/com/microsoft/aad/adal/BrokerAccountServiceTest.java index 3cdd3e757..d9549b4aa 100644 --- a/adal/src/androidTest/java/com/microsoft/aad/adal/BrokerAccountServiceTest.java +++ b/adal/src/androidTest/java/com/microsoft/aad/adal/BrokerAccountServiceTest.java @@ -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; @@ -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); diff --git a/adal/src/androidTest/java/com/microsoft/aad/adal/BrokerProxyTests.java b/adal/src/androidTest/java/com/microsoft/aad/adal/BrokerProxyTests.java index 20dadc62f..4f1f8034d 100644 --- a/adal/src/androidTest/java/com/microsoft/aad/adal/BrokerProxyTests.java +++ b/adal/src/androidTest/java/com/microsoft/aad/adal/BrokerProxyTests.java @@ -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; @@ -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; @@ -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 @@ -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}); + when(mockPackage.getPackageInfo(packageName, PackageHelper.getPackageManagerSignaturesFlag())).thenReturn(mockedPackageInfo); when(mockPackage.checkPermission(anyString(), anyString())) .thenReturn(permissionStatus ? PackageManager.PERMISSION_GRANTED : PackageManager.PERMISSION_DENIED); return mockPackage; diff --git a/adal/src/androidTest/java/com/microsoft/aad/adal/MockedPackageInfo.java b/adal/src/androidTest/java/com/microsoft/aad/adal/MockedPackageInfo.java new file mode 100644 index 000000000..4ffdc819e --- /dev/null +++ b/adal/src/androidTest/java/com/microsoft/aad/adal/MockedPackageInfo.java @@ -0,0 +1,13 @@ +package com.microsoft.aad.adal; + +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); + } +} diff --git a/adal/src/androidTest/java/com/microsoft/aad/adal/MockedSigningInfo.java b/adal/src/androidTest/java/com/microsoft/aad/adal/MockedSigningInfo.java new file mode 100644 index 000000000..b0be5ac64 --- /dev/null +++ b/adal/src/androidTest/java/com/microsoft/aad/adal/MockedSigningInfo.java @@ -0,0 +1,20 @@ +package com.microsoft.aad.adal; + +import android.content.pm.Signature; + +public class MockedSigningInfo { + + private final Signature[] signatures; + + public MockedSigningInfo(Signature[] signatures) { + this.signatures = signatures; + } + + public boolean hasMultipleSigners() { + return false; + } + + public Signature[] getSigningCertificateHistory() { + return signatures; + } +} diff --git a/adal/src/androidTest/java/com/microsoft/aad/adal/PackageHelperTests.java b/adal/src/androidTest/java/com/microsoft/aad/adal/PackageHelperTests.java index c8f40eadb..625994050 100644 --- a/adal/src/androidTest/java/com/microsoft/aad/adal/PackageHelperTests.java +++ b/adal/src/androidTest/java/com/microsoft/aad/adal/PackageHelperTests.java @@ -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; @@ -220,7 +219,7 @@ 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; @@ -228,9 +227,9 @@ private PackageManager getPackageManager(final Signature signature, when( mockPackage.getPackageInfo( packageName, - PackageHelper.getPackageManagerFlag() + PackageHelper.getPackageManagerSignaturesFlag() ) - ).thenReturn(info); + ).thenReturn(mockedPackageInfo); when(mockPackage.getApplicationInfo(packageName, 0)).thenReturn(appInfo); Context mock = mock(Context.class); diff --git a/adal/src/androidTest/java/com/microsoft/aad/adal/TelemetryTest.java b/adal/src/androidTest/java/com/microsoft/aad/adal/TelemetryTest.java index 553e36b79..194584fd0 100644 --- a/adal/src/androidTest/java/com/microsoft/aad/adal/TelemetryTest.java +++ b/adal/src/androidTest/java/com/microsoft/aad/adal/TelemetryTest.java @@ -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); diff --git a/adal/src/androidTest/java/com/microsoft/aad/adal/Util.java b/adal/src/androidTest/java/com/microsoft/aad/adal/Util.java index 42911032d..aae2a173f 100644 --- a/adal/src/androidTest/java/com/microsoft/aad/adal/Util.java +++ b/adal/src/androidTest/java/com/microsoft/aad/adal/Util.java @@ -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; @@ -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."; diff --git a/common b/common index 69385b457..a3e1f4572 160000 --- a/common +++ b/common @@ -1 +1 @@ -Subproject commit 69385b4576fb408c4f013d09fa3e3d5ec6cf648f +Subproject commit a3e1f4572bb578e9854eee39aabe4d204406d19f From 38219345a02b4bd4e53f23f0d327b5c05bfb52e3 Mon Sep 17 00:00:00 2001 From: Paul Kagiri Date: Wed, 5 May 2021 07:52:47 +0300 Subject: [PATCH 03/13] updated common submodule --- common | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common b/common index a3e1f4572..b21016214 160000 --- a/common +++ b/common @@ -1 +1 @@ -Subproject commit a3e1f4572bb578e9854eee39aabe4d204406d19f +Subproject commit b210162146a8626e84fff96746c175148fae6edb From 731329033908294ffeee60498966911bae571887 Mon Sep 17 00:00:00 2001 From: Paul Kagiri Date: Wed, 5 May 2021 07:54:48 +0300 Subject: [PATCH 04/13] added license declarations --- .../microsoft/aad/adal/MockedPackageInfo.java | 23 +++++++++++++++++++ .../microsoft/aad/adal/MockedSigningInfo.java | 23 +++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/adal/src/androidTest/java/com/microsoft/aad/adal/MockedPackageInfo.java b/adal/src/androidTest/java/com/microsoft/aad/adal/MockedPackageInfo.java index 4ffdc819e..41aa23549 100644 --- a/adal/src/androidTest/java/com/microsoft/aad/adal/MockedPackageInfo.java +++ b/adal/src/androidTest/java/com/microsoft/aad/adal/MockedPackageInfo.java @@ -1,3 +1,26 @@ +// 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; import android.content.pm.PackageInfo; diff --git a/adal/src/androidTest/java/com/microsoft/aad/adal/MockedSigningInfo.java b/adal/src/androidTest/java/com/microsoft/aad/adal/MockedSigningInfo.java index b0be5ac64..85dc684f9 100644 --- a/adal/src/androidTest/java/com/microsoft/aad/adal/MockedSigningInfo.java +++ b/adal/src/androidTest/java/com/microsoft/aad/adal/MockedSigningInfo.java @@ -1,3 +1,26 @@ +// 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; import android.content.pm.Signature; From 143ec16eeb6382e5f1514b57f4da42b8bcd3fce0 Mon Sep 17 00:00:00 2001 From: Paul Kagiri Date: Wed, 5 May 2021 08:00:40 +0300 Subject: [PATCH 05/13] some cleanup done --- adal/src/androidTest/java/com/microsoft/aad/adal/Util.java | 7 ------- 1 file changed, 7 deletions(-) diff --git a/adal/src/androidTest/java/com/microsoft/aad/adal/Util.java b/adal/src/androidTest/java/com/microsoft/aad/adal/Util.java index aae2a173f..42911032d 100644 --- a/adal/src/androidTest/java/com/microsoft/aad/adal/Util.java +++ b/adal/src/androidTest/java/com/microsoft/aad/adal/Util.java @@ -22,10 +22,6 @@ // 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; @@ -55,9 +51,6 @@ 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."; From 538e7e43003edcfdcb1946e7fa8dd0865008b39e Mon Sep 17 00:00:00 2001 From: Paul Kagiri Date: Wed, 5 May 2021 12:41:22 +0300 Subject: [PATCH 06/13] fix wip --- .../com/microsoft/aad/adal/MockedPackageInfo.java | 4 ++++ .../com/microsoft/aad/adal/MockedSigningInfo.java | 8 ++++++++ .../microsoft/aad/adal/PackageHelperTests.java | 15 +++++---------- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/adal/src/androidTest/java/com/microsoft/aad/adal/MockedPackageInfo.java b/adal/src/androidTest/java/com/microsoft/aad/adal/MockedPackageInfo.java index 41aa23549..3399d099e 100644 --- a/adal/src/androidTest/java/com/microsoft/aad/adal/MockedPackageInfo.java +++ b/adal/src/androidTest/java/com/microsoft/aad/adal/MockedPackageInfo.java @@ -25,6 +25,7 @@ import android.content.pm.PackageInfo; import android.content.pm.Signature; +import android.os.Build; public class MockedPackageInfo extends PackageInfo { @@ -32,5 +33,8 @@ public class MockedPackageInfo extends PackageInfo { public MockedPackageInfo(Signature [] signatures) { this.signingInfo = new MockedSigningInfo(signatures); + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) { + this.signatures = signatures; + } } } diff --git a/adal/src/androidTest/java/com/microsoft/aad/adal/MockedSigningInfo.java b/adal/src/androidTest/java/com/microsoft/aad/adal/MockedSigningInfo.java index 85dc684f9..8695ce441 100644 --- a/adal/src/androidTest/java/com/microsoft/aad/adal/MockedSigningInfo.java +++ b/adal/src/androidTest/java/com/microsoft/aad/adal/MockedSigningInfo.java @@ -34,10 +34,18 @@ public MockedSigningInfo(Signature[] signatures) { } public boolean hasMultipleSigners() { + return signatures != null && signatures.length > 1; + } + + public boolean hasPastSigningCertificates() { return false; } public Signature[] getSigningCertificateHistory() { return signatures; } + + public Signature[] getApkContentsSigners() { + return signatures; + } } diff --git a/adal/src/androidTest/java/com/microsoft/aad/adal/PackageHelperTests.java b/adal/src/androidTest/java/com/microsoft/aad/adal/PackageHelperTests.java index 625994050..7f3e2785d 100644 --- a/adal/src/androidTest/java/com/microsoft/aad/adal/PackageHelperTests.java +++ b/adal/src/androidTest/java/com/microsoft/aad/adal/PackageHelperTests.java @@ -119,21 +119,16 @@ public void testGetCurrentSignatureForPackage() throws NameNotFoundException, mContext.getPackageName(), 0 // calling uid ); - final Object packageHelper = getInstance(mockContext); - final Method m = ReflectionUtils.getTestMethod( - packageHelper, - "getCurrentSignatureForPackage", // method name - String.class - ); - // act - String actual = (String) m.invoke(packageHelper, mContext.getPackageName()); + MockedPackageInfo mockedPackageInfo = new MockedPackageInfo(new Signature[]{new Signature(mTestSignature)}); + final PackageHelper packageHelper = (PackageHelper) getInstance(mockContext); + String actual = packageHelper.getCurrentSignatureForPackage(mockedPackageInfo); // assert assertEquals("should be same info", mTestTag, actual); // act - actual = (String) m.invoke(packageHelper, (String) null); + actual = packageHelper.getCurrentSignatureForPackage((String) null); // assert assertNull("should return null", actual); @@ -219,7 +214,7 @@ private PackageManager getPackageManager(final Signature signature, final String packageName, final int callingUID) throws NameNotFoundException { final PackageManager mockPackage = mock(PackageManager.class); - final PackageInfo mockedPackageInfo = new MockedPackageInfo(new Signature[]{signature}); + final MockedPackageInfo mockedPackageInfo = new MockedPackageInfo(new Signature[]{signature}); final ApplicationInfo appInfo = new ApplicationInfo(); appInfo.name = packageName; From 3f62a8675256074e3cc5dec72326cf250a471f47 Mon Sep 17 00:00:00 2001 From: Paul Kagiri Date: Mon, 5 Jul 2021 18:25:01 +0300 Subject: [PATCH 07/13] updated common --- common | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common b/common index 8768f61c2..77417e8a8 160000 --- a/common +++ b/common @@ -1 +1 @@ -Subproject commit 8768f61c2deff7c711b12b2a8b997c160e92ccb1 +Subproject commit 77417e8a851663e86bb31a92b8a62cf0391cc6c5 From 9699de5ce77c8e3f3539345ad441f153c22f12f2 Mon Sep 17 00:00:00 2001 From: Paul Kagiri Date: Tue, 6 Jul 2021 18:26:48 +0300 Subject: [PATCH 08/13] updated common submodule --- common | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common b/common index 77417e8a8..8e0c40e04 160000 --- a/common +++ b/common @@ -1 +1 @@ -Subproject commit 77417e8a851663e86bb31a92b8a62cf0391cc6c5 +Subproject commit 8e0c40e045460e0d5a11c1a3a7089b92bc1aab8c From 5d8193cc25778dca68ce3e24855bad1f769c4b29 Mon Sep 17 00:00:00 2001 From: Paul Kagiri Date: Tue, 6 Jul 2021 19:06:13 +0300 Subject: [PATCH 09/13] testGetCurrentSignatureForPackage fixed --- adal/build.gradle | 6 ++ .../aad/adal/PackageHelperTests.java | 25 ------- .../PackageHelperCurrentSignatureTests.java | 67 +++++++++++++++++++ .../microsoft/aad/adal/SigningInfoShadow.java | 58 ++++++++++++++++ common | 2 +- 5 files changed, 132 insertions(+), 26 deletions(-) create mode 100644 adal/src/test/java/com/microsoft/aad/adal/PackageHelperCurrentSignatureTests.java create mode 100644 adal/src/test/java/com/microsoft/aad/adal/SigningInfoShadow.java diff --git a/adal/build.gradle b/adal/build.gradle index 86e50c9bc..7550df4aa 100644 --- a/adal/build.gradle +++ b/adal/build.gradle @@ -165,6 +165,12 @@ dependencies { // Test Dependencies testImplementation "junit:junit:$rootProject.ext.junitVersion" + //mockito-inline was introduced in mockito 2.7.6 + //see: https://javadoc.io/static/org.mockito/mockito-core/3.6.28/org/mockito/Mockito.html#0.1 + testImplementation "org.mockito:mockito-inline:$rootProject.ext.mockitoCoreVersion" + testImplementation ("org.robolectric:robolectric:$rootProject.ext.robolectricVersion") + testImplementation "androidx.test:core:$rootProject.ext.androidxTestCoreVersion" + testImplementation project(':testutils') // Javadoc Dependencies javadocDeps "androidx.annotation:annotation:$rootProject.ext.annotationVersion" diff --git a/adal/src/androidTest/java/com/microsoft/aad/adal/PackageHelperTests.java b/adal/src/androidTest/java/com/microsoft/aad/adal/PackageHelperTests.java index fad342f6a..cb3b4128f 100644 --- a/adal/src/androidTest/java/com/microsoft/aad/adal/PackageHelperTests.java +++ b/adal/src/androidTest/java/com/microsoft/aad/adal/PackageHelperTests.java @@ -55,7 +55,6 @@ import javax.crypto.spec.SecretKeySpec; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -109,30 +108,6 @@ public void tearDown() { Logger.getInstance().setExternalLogger(null); } - @Test - public void testGetCurrentSignatureForPackage() throws NameNotFoundException, - IllegalArgumentException, ClassNotFoundException, NoSuchMethodException, - InstantiationException, IllegalAccessException, InvocationTargetException { - final Context mockContext = getMockContext( - new Signature(mTestSignature), - mContext.getPackageName(), - 0 // calling uid - ); - - MockedPackageInfo mockedPackageInfo = new MockedPackageInfo(new Signature[]{new Signature(mTestSignature)}); - final PackageHelper packageHelper = (PackageHelper) getInstance(mockContext); - String actual = packageHelper.getCurrentSignatureForPackage(mockedPackageInfo.packageName); - - // assert - assertEquals("should be same info", mTestTag, actual); - - // act - actual = packageHelper.getCurrentSignatureForPackage((String) null); - - // assert - assertNull("should return null", actual); - } - @Test public void testGetUIDForPackage() throws NameNotFoundException, IllegalArgumentException, ClassNotFoundException, NoSuchMethodException, InstantiationException, diff --git a/adal/src/test/java/com/microsoft/aad/adal/PackageHelperCurrentSignatureTests.java b/adal/src/test/java/com/microsoft/aad/adal/PackageHelperCurrentSignatureTests.java new file mode 100644 index 000000000..236647e6f --- /dev/null +++ b/adal/src/test/java/com/microsoft/aad/adal/PackageHelperCurrentSignatureTests.java @@ -0,0 +1,67 @@ +// 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; + +import android.content.pm.PackageInfo; +import android.content.pm.Signature; +import android.util.Base64; + +import com.microsoft.identity.common.internal.broker.PackageHelper; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.annotation.Config; + +import java.security.MessageDigest; + +import static org.junit.Assert.assertEquals; + +@RunWith(RobolectricTestRunner.class) +@Config(manifest = Config.NONE, shadows = { + SigningInfoShadow.class +}) +public class PackageHelperCurrentSignatureTests { + static final String ENCODED_SIGNATURE = "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"; + private String testSignature; + private Signature mockedSignature; + + @Before + public void setUp() throws Exception { + mockedSignature = new Signature(Base64.decode(ENCODED_SIGNATURE, Base64.NO_WRAP)); + MessageDigest md = MessageDigest.getInstance("SHA"); + md.update(mockedSignature.toByteArray()); + testSignature = Base64.encodeToString(md.digest(), Base64.NO_WRAP); + } + + @Test + public void testGetCurrentSignatureForPackage() { + SigningInfoShadow.setSignatures(new Signature[]{mockedSignature}); + PackageInfo packageInfo = new PackageInfo(); + packageInfo.signatures = new Signature[]{mockedSignature}; + String signature = PackageHelper.getCurrentSignatureForPackage(packageInfo); + // assert + assertEquals("should be same info", testSignature, signature); + } +} \ No newline at end of file diff --git a/adal/src/test/java/com/microsoft/aad/adal/SigningInfoShadow.java b/adal/src/test/java/com/microsoft/aad/adal/SigningInfoShadow.java new file mode 100644 index 000000000..ad7e6f3c7 --- /dev/null +++ b/adal/src/test/java/com/microsoft/aad/adal/SigningInfoShadow.java @@ -0,0 +1,58 @@ +// 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; + +import android.content.pm.Signature; +import android.content.pm.SigningInfo; + +import org.robolectric.annotation.Implementation; +import org.robolectric.annotation.Implements; + +@Implements(SigningInfo.class) +public class SigningInfoShadow { + private static Signature[] signatures; + + @Implementation + public boolean hasMultipleSigners() { + return signatures != null && signatures.length > 1; + } + + @Implementation + public boolean hasPastSigningCertificates() { + return false; + } + + @Implementation + public Signature[] getSigningCertificateHistory() { + return signatures; + } + + @Implementation + public Signature[] getApkContentsSigners() { + return signatures; + } + + public static void setSignatures(Signature[] signatures) { + SigningInfoShadow.signatures = signatures; + } +} \ No newline at end of file diff --git a/common b/common index 8e0c40e04..4980cf017 160000 --- a/common +++ b/common @@ -1 +1 @@ -Subproject commit 8e0c40e045460e0d5a11c1a3a7089b92bc1aab8c +Subproject commit 4980cf017b0337328615075e7ae9c5aa03a0651a From c5bf9a73ddbc405e0d35f992eb555e419466f88a Mon Sep 17 00:00:00 2001 From: Paul Kagiri Date: Tue, 13 Jul 2021 19:49:48 +0300 Subject: [PATCH 10/13] updated common submodule --- common | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common b/common index 4980cf017..05bd69010 160000 --- a/common +++ b/common @@ -1 +1 @@ -Subproject commit 4980cf017b0337328615075e7ae9c5aa03a0651a +Subproject commit 05bd690100e628e9113012d73344523ae3e3bc59 From 8b3a539bfb5bd2b9811891a35ea3fc28af997c83 Mon Sep 17 00:00:00 2001 From: Paul Kagiri Date: Wed, 21 Jul 2021 11:13:56 +0300 Subject: [PATCH 11/13] common submodule update --- common | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common b/common index 05bd69010..fef0f3e46 160000 --- a/common +++ b/common @@ -1 +1 @@ -Subproject commit 05bd690100e628e9113012d73344523ae3e3bc59 +Subproject commit fef0f3e46f1be4707ae7705d672dfcd3a8d86095 From a19aabe54c0a5a2f1e14f1f3de86c53fdde1985f Mon Sep 17 00:00:00 2001 From: Paul Kagiri Date: Mon, 26 Jul 2021 09:20:03 +0300 Subject: [PATCH 12/13] SigningInfoShadow eof update --- .../src/test/java/com/microsoft/aad/adal/SigningInfoShadow.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adal/src/test/java/com/microsoft/aad/adal/SigningInfoShadow.java b/adal/src/test/java/com/microsoft/aad/adal/SigningInfoShadow.java index ad7e6f3c7..4e9ebeee8 100644 --- a/adal/src/test/java/com/microsoft/aad/adal/SigningInfoShadow.java +++ b/adal/src/test/java/com/microsoft/aad/adal/SigningInfoShadow.java @@ -55,4 +55,4 @@ public Signature[] getApkContentsSigners() { public static void setSignatures(Signature[] signatures) { SigningInfoShadow.signatures = signatures; } -} \ No newline at end of file +} From bae680b8bb71d8d385c5353a70afe511b7349836 Mon Sep 17 00:00:00 2001 From: Paul Kagiri Date: Mon, 26 Jul 2021 11:06:30 +0300 Subject: [PATCH 13/13] update common submodule --- common | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common b/common index fef0f3e46..7c1f4275d 160000 --- a/common +++ b/common @@ -1 +1 @@ -Subproject commit fef0f3e46f1be4707ae7705d672dfcd3a8d86095 +Subproject commit 7c1f4275d2a278719c7fde0d55426c1e8537f338