OpenPgpServiceKeyIdExtractor { KeyIdResult returnKeyIdsFromIntent(Intent data, boolean askIfNoUserIdsProvided, String callingPackageName) { boolean hasKeysFromSelectPubkeyActivity = data.hasExtra(OpenPgpApi.EXTRA_KEY_IDS_SELECTED); KeyIdResult result; if (hasKeysFromSelectPubkeyActivity) { HashSet<Long> encryptKeyIds = new HashSet<>(); for (long keyId : data.getLongArrayExtra(OpenPgpApi.EXTRA_KEY_IDS_SELECTED)) { encryptKeyIds.add(keyId); } result = createKeysOkResult(encryptKeyIds, false, AutocryptStatus.AUTOCRYPT_PEER_DISABLED); } else if (data.hasExtra(OpenPgpApi.EXTRA_USER_IDS) || askIfNoUserIdsProvided) { String[] userIds = data.getStringArrayExtra(OpenPgpApi.EXTRA_USER_IDS); result = returnKeyIdsFromEmails(data, userIds, callingPackageName); } else { result = createNoKeysResult(); } if (data.hasExtra(OpenPgpApi.EXTRA_KEY_IDS)) { HashSet<Long> explicitKeyIds = new HashSet<>(); for (long keyId : data.getLongArrayExtra(OpenPgpApi.EXTRA_KEY_IDS)) { explicitKeyIds.add(keyId); } result = result.withExplicitKeyIds(explicitKeyIds); } return result; } private  OpenPgpServiceKeyIdExtractor(ContentResolver contentResolver,
            ApiPendingIntentFactory apiPendingIntentFactory);   }
@Test public void returnKeyIdsFromIntent__withKeyIdsExtra() throws Exception { Intent intent = new Intent(); intent.putExtra(OpenPgpApi.EXTRA_KEY_IDS, KEY_IDS); KeyIdResult keyIdResult = openPgpServiceKeyIdExtractor.returnKeyIdsFromIntent(intent, false, BuildConfig.APPLICATION_ID); assertEquals(KeyIdResultStatus.NO_KEYS_ERROR, keyIdResult.getStatus()); assertFalse(keyIdResult.hasKeySelectionPendingIntent()); assertArrayEqualsSorted(KEY_IDS, keyIdResult.getKeyIds()); }
@Test public void returnKeyIdsFromIntent__withKeyIdsSelectedExtra() throws Exception { Intent intent = new Intent(); intent.putExtra(OpenPgpApi.EXTRA_KEY_IDS_SELECTED, KEY_IDS); intent.putExtra(OpenPgpApi.EXTRA_USER_IDS, USER_IDS); KeyIdResult keyIdResult = openPgpServiceKeyIdExtractor.returnKeyIdsFromIntent(intent, false, BuildConfig.APPLICATION_ID); assertEquals(KeyIdResultStatus.OK, keyIdResult.getStatus()); assertFalse(keyIdResult.hasKeySelectionPendingIntent()); assertArrayEqualsSorted(KEY_IDS, keyIdResult.getKeyIds()); }
@Test(expected = IllegalStateException.class) public void returnKeyIdsFromIntent__withUserIds__withEmptyQueryResult() throws Exception { Intent intent = new Intent(); intent.putExtra(OpenPgpApi.EXTRA_USER_IDS, USER_IDS); PendingIntent pendingIntent = mock(PendingIntent.class); setupSelectPubkeyPendingIntentFactoryResult(pendingIntent); openPgpServiceKeyIdExtractor.returnKeyIdsFromIntent(intent, false, BuildConfig.APPLICATION_ID); }
@Test public void returnKeyIdsFromIntent__withNoData() throws Exception { Intent intent = new Intent(); intent.putExtra(OpenPgpApi.EXTRA_USER_IDS, new String[] { }); PendingIntent pendingIntent = mock(PendingIntent.class); setupSelectPubkeyPendingIntentFactoryResult(pendingIntent); KeyIdResult keyIdResult = openPgpServiceKeyIdExtractor.returnKeyIdsFromIntent(intent, false, BuildConfig.APPLICATION_ID); assertEquals(KeyIdResultStatus.NO_KEYS, keyIdResult.getStatus()); assertTrue(keyIdResult.hasKeySelectionPendingIntent()); assertSame(pendingIntent, keyIdResult.getKeySelectionPendingIntent()); }
@Test public void returnKeyIdsFromIntent__withEmptyUserId() throws Exception { Intent intent = new Intent(); intent.putExtra(OpenPgpApi.EXTRA_USER_IDS, new String[0]); PendingIntent pendingIntent = mock(PendingIntent.class); setupSelectPubkeyPendingIntentFactoryResult(pendingIntent); KeyIdResult keyIdResult = openPgpServiceKeyIdExtractor.returnKeyIdsFromIntent(intent, false, BuildConfig.APPLICATION_ID); assertEquals(KeyIdResultStatus.NO_KEYS, keyIdResult.getStatus()); assertTrue(keyIdResult.hasKeySelectionPendingIntent()); assertSame(pendingIntent, keyIdResult.getKeySelectionPendingIntent()); }
@Test public void returnKeyIdsFromIntent__withNoData__askIfNoData() throws Exception { Intent intent = new Intent(); PendingIntent pendingIntent = mock(PendingIntent.class); setupSelectPubkeyPendingIntentFactoryResult(pendingIntent); KeyIdResult keyIdResult = openPgpServiceKeyIdExtractor.returnKeyIdsFromIntent(intent, true, BuildConfig.APPLICATION_ID); assertEquals(KeyIdResultStatus.NO_KEYS, keyIdResult.getStatus()); assertTrue(keyIdResult.hasKeySelectionPendingIntent()); assertSame(pendingIntent, keyIdResult.getKeySelectionPendingIntent()); }
@Test public void returnKeyIdsFromIntent__withUserIds() throws Exception { Intent intent = new Intent(); intent.putExtra(OpenPgpApi.EXTRA_USER_IDS, USER_IDS); setupContentResolverResult(USER_IDS, new Long[] { 123L, 234L }, new int[] { 0, 0 }, new int[] { 1, 1, 1 }); KeyIdResult keyIdResult = openPgpServiceKeyIdExtractor.returnKeyIdsFromIntent(intent, false, BuildConfig.APPLICATION_ID); assertEquals(KeyIdResultStatus.OK, keyIdResult.getStatus()); assertFalse(keyIdResult.hasKeySelectionPendingIntent()); assertArrayEqualsSorted(KEY_IDS, keyIdResult.getKeyIds()); }
@Test public void returnKeyIdsFromIntent__withUserIds__withDuplicate() throws Exception { Intent intent = new Intent(); intent.putExtra(OpenPgpApi.EXTRA_USER_IDS, USER_IDS); setupContentResolverResult(new String[] { USER_IDS[0], USER_IDS[1] }, new Long[] { 123L, 234L }, new int[] { 0, 0 }, new int[] { 2, 1 }); PendingIntent pendingIntent = mock(PendingIntent.class); setupDeduplicatePendingIntentFactoryResult(pendingIntent); KeyIdResult keyIdResult = openPgpServiceKeyIdExtractor.returnKeyIdsFromIntent(intent, false, BuildConfig.APPLICATION_ID); assertEquals(KeyIdResultStatus.DUPLICATE, keyIdResult.getStatus()); assertTrue(keyIdResult.hasKeySelectionPendingIntent()); }
@Test public void returnKeyIdsFromIntent__withUserIds__withMissing() throws Exception { Intent intent = new Intent(); intent.putExtra(OpenPgpApi.EXTRA_USER_IDS, USER_IDS); setupContentResolverResult(USER_IDS, new Long[] { null, 234L }, new int[] { 0, 0 }, new int[] { 0, 1 }); PendingIntent pendingIntent = mock(PendingIntent.class); setupSelectPubkeyPendingIntentFactoryResult(pendingIntent); KeyIdResult keyIdResult = openPgpServiceKeyIdExtractor.returnKeyIdsFromIntent(intent, false, BuildConfig.APPLICATION_ID); assertEquals(KeyIdResultStatus.MISSING, keyIdResult.getStatus()); assertTrue(keyIdResult.hasKeySelectionPendingIntent()); }