Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit dee54a44 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Resolve NPE and unexpected error code in AccountManagerServiceTest"

parents 6e6a4ce6 c76ea600
Loading
Loading
Loading
Loading
+1 −0
Original line number Original line Diff line number Diff line
@@ -65,6 +65,7 @@
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
    <uses-permission android:name="android.permission.SUSPEND_APPS"/>
    <uses-permission android:name="android.permission.SUSPEND_APPS"/>
    <uses-permission android:name="android.permission.CONTROL_KEYGUARD"/>
    <uses-permission android:name="android.permission.CONTROL_KEYGUARD"/>
    <uses-permission android:name="android.permission.MANAGE_BIND_INSTANT_SERVICE"/>


    <!-- Uses API introduced in O (26) -->
    <!-- Uses API introduced in O (26) -->
    <uses-sdk android:minSdkVersion="1"
    <uses-sdk android:minSdkVersion="1"
+33 −7
Original line number Original line Diff line number Diff line
@@ -47,6 +47,7 @@ import android.content.pm.ActivityInfo;
import android.content.pm.ApplicationInfo;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager;
import android.content.pm.PackageManagerInternal;
import android.content.pm.ResolveInfo;
import android.content.pm.ResolveInfo;
import android.content.pm.Signature;
import android.content.pm.Signature;
import android.content.pm.UserInfo;
import android.content.pm.UserInfo;
@@ -111,6 +112,7 @@ public class AccountManagerServiceTest extends AndroidTestCase {
    @Mock private IAccountManagerResponse mMockAccountManagerResponse;
    @Mock private IAccountManagerResponse mMockAccountManagerResponse;
    @Mock private IBinder mMockBinder;
    @Mock private IBinder mMockBinder;
    @Mock private INotificationManager mMockNotificationManager;
    @Mock private INotificationManager mMockNotificationManager;
    @Mock private PackageManagerInternal mMockPackageManagerInternal;


    @Captor private ArgumentCaptor<Intent> mIntentCaptor;
    @Captor private ArgumentCaptor<Intent> mIntentCaptor;
    @Captor private ArgumentCaptor<Bundle> mBundleCaptor;
    @Captor private ArgumentCaptor<Bundle> mBundleCaptor;
@@ -155,6 +157,9 @@ public class AccountManagerServiceTest extends AndroidTestCase {
        when(mMockContext.getSystemService(Context.DEVICE_POLICY_SERVICE)).thenReturn(
        when(mMockContext.getSystemService(Context.DEVICE_POLICY_SERVICE)).thenReturn(
                mMockDevicePolicyManager);
                mMockDevicePolicyManager);
        when(mMockAccountManagerResponse.asBinder()).thenReturn(mMockBinder);
        when(mMockAccountManagerResponse.asBinder()).thenReturn(mMockBinder);
        when(mMockPackageManagerInternal.hasSignatureCapability(anyInt(), anyInt(), anyInt()))
                .thenReturn(true);
        LocalServices.addService(PackageManagerInternal.class, mMockPackageManagerInternal);


        Context realTestContext = getContext();
        Context realTestContext = getContext();
        MyMockContext mockContext = new MyMockContext(realTestContext, mMockContext);
        MyMockContext mockContext = new MyMockContext(realTestContext, mMockContext);
@@ -174,6 +179,7 @@ public class AccountManagerServiceTest extends AndroidTestCase {
            cdl.countDown();
            cdl.countDown();
        });
        });
        cdl.await(1, TimeUnit.SECONDS);
        cdl.await(1, TimeUnit.SECONDS);
        LocalServices.removeServiceForTest(PackageManagerInternal.class);
        super.tearDown();
        super.tearDown();
    }
    }


@@ -607,6 +613,8 @@ public class AccountManagerServiceTest extends AndroidTestCase {
                any(Intent.class), anyInt(), anyInt())).thenReturn(resolveInfo);
                any(Intent.class), anyInt(), anyInt())).thenReturn(resolveInfo);
        when(mMockPackageManager.checkSignatures(
        when(mMockPackageManager.checkSignatures(
                anyInt(), anyInt())).thenReturn(PackageManager.SIGNATURE_NO_MATCH);
                anyInt(), anyInt())).thenReturn(PackageManager.SIGNATURE_NO_MATCH);
        when(mMockPackageManagerInternal.hasSignatureCapability(anyInt(), anyInt(), anyInt()))
                .thenReturn(false);


        final CountDownLatch latch = new CountDownLatch(1);
        final CountDownLatch latch = new CountDownLatch(1);
        Response response = new Response(latch, mMockAccountManagerResponse);
        Response response = new Response(latch, mMockAccountManagerResponse);
@@ -623,7 +631,7 @@ public class AccountManagerServiceTest extends AndroidTestCase {
        waitForLatch(latch);
        waitForLatch(latch);
        verify(mMockAccountManagerResponse, never()).onResult(any(Bundle.class));
        verify(mMockAccountManagerResponse, never()).onResult(any(Bundle.class));
        verify(mMockAccountManagerResponse).onError(
        verify(mMockAccountManagerResponse).onError(
                eq(AccountManager.ERROR_CODE_REMOTE_EXCEPTION), anyString());
                eq(AccountManager.ERROR_CODE_INVALID_RESPONSE), anyString());
    }
    }


    @SmallTest
    @SmallTest
@@ -789,6 +797,8 @@ public class AccountManagerServiceTest extends AndroidTestCase {
                any(Intent.class), anyInt(), anyInt())).thenReturn(resolveInfo);
                any(Intent.class), anyInt(), anyInt())).thenReturn(resolveInfo);
        when(mMockPackageManager.checkSignatures(
        when(mMockPackageManager.checkSignatures(
                anyInt(), anyInt())).thenReturn(PackageManager.SIGNATURE_NO_MATCH);
                anyInt(), anyInt())).thenReturn(PackageManager.SIGNATURE_NO_MATCH);
        when(mMockPackageManagerInternal.hasSignatureCapability(anyInt(), anyInt(), anyInt()))
                .thenReturn(false);


        final CountDownLatch latch = new CountDownLatch(1);
        final CountDownLatch latch = new CountDownLatch(1);
        Response response = new Response(latch, mMockAccountManagerResponse);
        Response response = new Response(latch, mMockAccountManagerResponse);
@@ -805,7 +815,7 @@ public class AccountManagerServiceTest extends AndroidTestCase {
        waitForLatch(latch);
        waitForLatch(latch);
        verify(mMockAccountManagerResponse, never()).onResult(any(Bundle.class));
        verify(mMockAccountManagerResponse, never()).onResult(any(Bundle.class));
        verify(mMockAccountManagerResponse).onError(
        verify(mMockAccountManagerResponse).onError(
                eq(AccountManager.ERROR_CODE_REMOTE_EXCEPTION), anyString());
                eq(AccountManager.ERROR_CODE_INVALID_RESPONSE), anyString());
    }
    }


    @SmallTest
    @SmallTest
@@ -1089,6 +1099,8 @@ public class AccountManagerServiceTest extends AndroidTestCase {
                any(Intent.class), anyInt(), anyInt())).thenReturn(resolveInfo);
                any(Intent.class), anyInt(), anyInt())).thenReturn(resolveInfo);
        when(mMockPackageManager.checkSignatures(
        when(mMockPackageManager.checkSignatures(
                anyInt(), anyInt())).thenReturn(PackageManager.SIGNATURE_NO_MATCH);
                anyInt(), anyInt())).thenReturn(PackageManager.SIGNATURE_NO_MATCH);
        when(mMockPackageManagerInternal.hasSignatureCapability(anyInt(), anyInt(), anyInt()))
                .thenReturn(false);


        final CountDownLatch latch = new CountDownLatch(1);
        final CountDownLatch latch = new CountDownLatch(1);
        Response response = new Response(latch, mMockAccountManagerResponse);
        Response response = new Response(latch, mMockAccountManagerResponse);
@@ -1103,7 +1115,7 @@ public class AccountManagerServiceTest extends AndroidTestCase {
        waitForLatch(latch);
        waitForLatch(latch);
        verify(mMockAccountManagerResponse, never()).onResult(any(Bundle.class));
        verify(mMockAccountManagerResponse, never()).onResult(any(Bundle.class));
        verify(mMockAccountManagerResponse).onError(
        verify(mMockAccountManagerResponse).onError(
                eq(AccountManager.ERROR_CODE_REMOTE_EXCEPTION), anyString());
                eq(AccountManager.ERROR_CODE_INVALID_RESPONSE), anyString());
    }
    }


    @SmallTest
    @SmallTest
@@ -1349,6 +1361,8 @@ public class AccountManagerServiceTest extends AndroidTestCase {
        unlockSystemUser();
        unlockSystemUser();
        when(mMockPackageManager.checkSignatures(anyInt(), anyInt()))
        when(mMockPackageManager.checkSignatures(anyInt(), anyInt()))
                    .thenReturn(PackageManager.SIGNATURE_NO_MATCH);
                    .thenReturn(PackageManager.SIGNATURE_NO_MATCH);
        when(mMockPackageManagerInternal.hasSignatureCapability(anyInt(), anyInt(), anyInt()))
                .thenReturn(false);
        try {
        try {
            mAms.removeAccountAsUser(
            mAms.removeAccountAsUser(
                mMockAccountManagerResponse, // response
                mMockAccountManagerResponse, // response
@@ -1685,6 +1699,8 @@ public class AccountManagerServiceTest extends AndroidTestCase {
                any(Intent.class), anyInt(), anyInt())).thenReturn(resolveInfo);
                any(Intent.class), anyInt(), anyInt())).thenReturn(resolveInfo);
        when(mMockPackageManager.checkSignatures(
        when(mMockPackageManager.checkSignatures(
                anyInt(), anyInt())).thenReturn(PackageManager.SIGNATURE_NO_MATCH);
                anyInt(), anyInt())).thenReturn(PackageManager.SIGNATURE_NO_MATCH);
        when(mMockPackageManagerInternal.hasSignatureCapability(anyInt(), anyInt(), anyInt()))
                .thenReturn(false);


        final CountDownLatch latch = new CountDownLatch(1);
        final CountDownLatch latch = new CountDownLatch(1);
        Response response = new Response(latch, mMockAccountManagerResponse);
        Response response = new Response(latch, mMockAccountManagerResponse);
@@ -1698,7 +1714,7 @@ public class AccountManagerServiceTest extends AndroidTestCase {
        waitForLatch(latch);
        waitForLatch(latch);
        verify(mMockAccountManagerResponse, never()).onResult(any(Bundle.class));
        verify(mMockAccountManagerResponse, never()).onResult(any(Bundle.class));
        verify(mMockAccountManagerResponse).onError(
        verify(mMockAccountManagerResponse).onError(
                eq(AccountManager.ERROR_CODE_REMOTE_EXCEPTION), anyString());
                eq(AccountManager.ERROR_CODE_INVALID_RESPONSE), anyString());
    }
    }


    @SmallTest
    @SmallTest
@@ -1956,6 +1972,8 @@ public class AccountManagerServiceTest extends AndroidTestCase {
                any(Intent.class), anyInt(), anyInt())).thenReturn(resolveInfo);
                any(Intent.class), anyInt(), anyInt())).thenReturn(resolveInfo);
        when(mMockPackageManager.checkSignatures(
        when(mMockPackageManager.checkSignatures(
                anyInt(), anyInt())).thenReturn(PackageManager.SIGNATURE_NO_MATCH);
                anyInt(), anyInt())).thenReturn(PackageManager.SIGNATURE_NO_MATCH);
        when(mMockPackageManagerInternal.hasSignatureCapability(anyInt(), anyInt(), anyInt()))
                .thenReturn(false);


        final CountDownLatch latch = new CountDownLatch(1);
        final CountDownLatch latch = new CountDownLatch(1);
        Response response = new Response(latch, mMockAccountManagerResponse);
        Response response = new Response(latch, mMockAccountManagerResponse);
@@ -1971,7 +1989,7 @@ public class AccountManagerServiceTest extends AndroidTestCase {
        waitForLatch(latch);
        waitForLatch(latch);
        verify(mMockAccountManagerResponse, never()).onResult(any(Bundle.class));
        verify(mMockAccountManagerResponse, never()).onResult(any(Bundle.class));
        verify(mMockAccountManagerResponse).onError(
        verify(mMockAccountManagerResponse).onError(
                eq(AccountManager.ERROR_CODE_REMOTE_EXCEPTION), anyString());
                eq(AccountManager.ERROR_CODE_INVALID_RESPONSE), anyString());
    }
    }


    @SmallTest
    @SmallTest
@@ -2094,6 +2112,8 @@ public class AccountManagerServiceTest extends AndroidTestCase {
                any(Intent.class), anyInt(), anyInt())).thenReturn(resolveInfo);
                any(Intent.class), anyInt(), anyInt())).thenReturn(resolveInfo);
        when(mMockPackageManager.checkSignatures(
        when(mMockPackageManager.checkSignatures(
                anyInt(), anyInt())).thenReturn(PackageManager.SIGNATURE_NO_MATCH);
                anyInt(), anyInt())).thenReturn(PackageManager.SIGNATURE_NO_MATCH);
        when(mMockPackageManagerInternal.hasSignatureCapability(anyInt(), anyInt(), anyInt()))
                .thenReturn(false);


        final CountDownLatch latch = new CountDownLatch(1);
        final CountDownLatch latch = new CountDownLatch(1);
        Response response = new Response(latch, mMockAccountManagerResponse);
        Response response = new Response(latch, mMockAccountManagerResponse);
@@ -2107,7 +2127,7 @@ public class AccountManagerServiceTest extends AndroidTestCase {


        verify(mMockAccountManagerResponse, never()).onResult(any(Bundle.class));
        verify(mMockAccountManagerResponse, never()).onResult(any(Bundle.class));
        verify(mMockAccountManagerResponse).onError(
        verify(mMockAccountManagerResponse).onError(
                eq(AccountManager.ERROR_CODE_REMOTE_EXCEPTION), anyString());
                eq(AccountManager.ERROR_CODE_INVALID_RESPONSE), anyString());
    }
    }


    @SmallTest
    @SmallTest
@@ -2227,6 +2247,8 @@ public class AccountManagerServiceTest extends AndroidTestCase {
                any(Intent.class), anyInt(), anyInt())).thenReturn(resolveInfo);
                any(Intent.class), anyInt(), anyInt())).thenReturn(resolveInfo);
        when(mMockPackageManager.checkSignatures(
        when(mMockPackageManager.checkSignatures(
                anyInt(), anyInt())).thenReturn(PackageManager.SIGNATURE_NO_MATCH);
                anyInt(), anyInt())).thenReturn(PackageManager.SIGNATURE_NO_MATCH);
        when(mMockPackageManagerInternal.hasSignatureCapability(anyInt(), anyInt(), anyInt()))
                .thenReturn(false);


        final CountDownLatch latch = new CountDownLatch(1);
        final CountDownLatch latch = new CountDownLatch(1);
        Response response = new Response(latch, mMockAccountManagerResponse);
        Response response = new Response(latch, mMockAccountManagerResponse);
@@ -2242,7 +2264,7 @@ public class AccountManagerServiceTest extends AndroidTestCase {


        verify(mMockAccountManagerResponse, never()).onResult(any(Bundle.class));
        verify(mMockAccountManagerResponse, never()).onResult(any(Bundle.class));
        verify(mMockAccountManagerResponse).onError(
        verify(mMockAccountManagerResponse).onError(
                eq(AccountManager.ERROR_CODE_REMOTE_EXCEPTION), anyString());
                eq(AccountManager.ERROR_CODE_INVALID_RESPONSE), anyString());
    }
    }


    @SmallTest
    @SmallTest
@@ -2329,6 +2351,8 @@ public class AccountManagerServiceTest extends AndroidTestCase {
        unlockSystemUser();
        unlockSystemUser();
        when(mMockPackageManager.checkSignatures(anyInt(), anyInt()))
        when(mMockPackageManager.checkSignatures(anyInt(), anyInt()))
                    .thenReturn(PackageManager.SIGNATURE_NO_MATCH);
                    .thenReturn(PackageManager.SIGNATURE_NO_MATCH);
        when(mMockPackageManagerInternal.hasSignatureCapability(anyInt(), anyInt(), anyInt()))
                .thenReturn(false);
        try {
        try {
            mAms.editProperties(
            mAms.editProperties(
                mMockAccountManagerResponse, // response
                mMockAccountManagerResponse, // response
@@ -2618,6 +2642,8 @@ public class AccountManagerServiceTest extends AndroidTestCase {
                PackageManager.PERMISSION_DENIED);
                PackageManager.PERMISSION_DENIED);
        when(mMockPackageManager.checkSignatures(anyInt(), anyInt()))
        when(mMockPackageManager.checkSignatures(anyInt(), anyInt()))
                    .thenReturn(PackageManager.SIGNATURE_NO_MATCH);
                    .thenReturn(PackageManager.SIGNATURE_NO_MATCH);
        when(mMockPackageManagerInternal.hasSignatureCapability(anyInt(), anyInt(), anyInt()))
                .thenReturn(false);


        final CountDownLatch latch = new CountDownLatch(1);
        final CountDownLatch latch = new CountDownLatch(1);
        Response response = new Response(latch, mMockAccountManagerResponse);
        Response response = new Response(latch, mMockAccountManagerResponse);
+7 −0
Original line number Original line Diff line number Diff line
@@ -25,6 +25,7 @@ import static org.junit.Assert.assertTrue;
import android.accounts.Account;
import android.accounts.Account;
import android.content.Context;
import android.content.Context;
import android.database.Cursor;
import android.database.Cursor;
import android.os.Build;
import android.test.suitebuilder.annotation.SmallTest;
import android.test.suitebuilder.annotation.SmallTest;
import android.util.Pair;
import android.util.Pair;


@@ -86,6 +87,12 @@ public class AccountsDbTest {


    @Test
    @Test
    public void testCeNotAvailableInitially() {
    public void testCeNotAvailableInitially() {
        // If the CE database is not attached to the DE database then any calls that modify the CE
        // database will result in a Log.wtf call that will crash this process on eng builds. To
        // allow the test to run through to completion skip this test on eng builds.
        if (Build.IS_ENG) {
            return;
        }
        Account account = new Account("name", "example.com");
        Account account = new Account("name", "example.com");
        long id = mAccountsDb.insertCeAccount(account, "");
        long id = mAccountsDb.insertCeAccount(account, "");
        assertEquals("Insert into CE should fail until CE database is attached", -1, id);
        assertEquals("Insert into CE should fail until CE database is attached", -1, id);