1
0
mirror of https://github.com/moparisthebest/k-9 synced 2024-11-27 11:42:16 -05:00

Merge pull request #536 from ligi/ligi/refactor/pgp_utils

Test & refactor OpenPgpUtils
This commit is contained in:
cketti 2015-01-29 17:32:59 +01:00
commit 8627e65cab
3 changed files with 87 additions and 38 deletions

View File

@ -0,0 +1,54 @@
package com.fsck.k9.crypto;
import android.support.test.runner.AndroidJUnit4;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.openintents.openpgp.util.OpenPgpUtils;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNull;
@RunWith(AndroidJUnit4.class)
public class OpenPgpUtilsTest {
@Test
public void splitCompleteUserIdShouldReturnAll3Components() throws Exception {
OpenPgpUtils.UserInfo info = OpenPgpUtils.splitUserId("Max Mustermann (this is a comment) <max@example.com>");
assertEquals("Max Mustermann", info.name);
assertEquals("this is a comment", info.comment);
assertEquals("max@example.com", info.email);
}
@Test
public void splitUserIdWithAllButCommentShouldReturnNameAndEmail() throws Exception {
OpenPgpUtils.UserInfo info = OpenPgpUtils.splitUserId("Max Mustermann <max@example.com>");
assertEquals("Max Mustermann", info.name);
assertNull(info.comment);
assertEquals("max@example.com", info.email);
}
@Test
public void splitUserIdWithAllButEmailShouldReturnNameAndComment() throws Exception {
OpenPgpUtils.UserInfo info = OpenPgpUtils.splitUserId("Max Mustermann (this is a comment)");
assertEquals(info.name, "Max Mustermann");
assertEquals(info.comment, "this is a comment");
assertNull(info.email);
}
@Test
public void splitUserIdWithOnlyNameShouldReturnNameOnly() throws Exception {
OpenPgpUtils.UserInfo info = OpenPgpUtils.splitUserId("Max Mustermann [this is a nothing]");
assertEquals("Max Mustermann", info.name);
assertNull(info.comment);
assertNull(info.email);
}
}

View File

@ -161,14 +161,14 @@ public class OpenPgpHeaderView extends LinearLayout {
} }
private void setUserId(OpenPgpSignatureResult signatureResult) { private void setUserId(OpenPgpSignatureResult signatureResult) {
String[] splitUserId = OpenPgpUtils.splitUserId(signatureResult.getPrimaryUserId()); final OpenPgpUtils.UserInfo userInfo = OpenPgpUtils.splitUserId(signatureResult.getPrimaryUserId());
if (splitUserId[0] != null) { if (userInfo.name != null) {
mResultSignatureName.setText(splitUserId[0]); mResultSignatureName.setText(userInfo.name);
} else { } else {
mResultSignatureName.setText(R.string.openpgp_result_no_name); mResultSignatureName.setText(R.string.openpgp_result_no_name);
} }
if (splitUserId[1] != null) { if (userInfo.email != null) {
mResultSignatureEmail.setText(splitUserId[1]); mResultSignatureEmail.setText(userInfo.email);
} else { } else {
mResultSignatureEmail.setText(R.string.openpgp_result_no_email); mResultSignatureEmail.setText(R.string.openpgp_result_no_email);
} }

View File

@ -16,15 +16,16 @@
package org.openintents.openpgp.util; package org.openintents.openpgp.util;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.text.TextUtils;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ResolveInfo;
public class OpenPgpUtils { public class OpenPgpUtils {
public static final Pattern PGP_MESSAGE = Pattern.compile( public static final Pattern PGP_MESSAGE = Pattern.compile(
@ -55,11 +56,7 @@ public class OpenPgpUtils {
public static boolean isAvailable(Context context) { public static boolean isAvailable(Context context) {
Intent intent = new Intent(OpenPgpApi.SERVICE_INTENT); Intent intent = new Intent(OpenPgpApi.SERVICE_INTENT);
List<ResolveInfo> resInfo = context.getPackageManager().queryIntentServices(intent, 0); List<ResolveInfo> resInfo = context.getPackageManager().queryIntentServices(intent, 0);
if (!resInfo.isEmpty()) { return !resInfo.isEmpty();
return true;
} else {
return false;
}
} }
public static String convertKeyIdToHex(long keyId) { public static String convertKeyIdToHex(long keyId) {
@ -74,38 +71,36 @@ public class OpenPgpUtils {
return hexString; return hexString;
} }
private static final Pattern USER_ID_PATTERN = Pattern.compile("^(.*?)(?: \\((.*)\\))?(?: <(.*)>)?$"); private static final Pattern USER_ID_PATTERN = Pattern.compile("^(.*?)(?: (\\[.*\\]))?(?: \\((.*)\\))?(?: <(.*)>)?$");
/** /**
* Splits userId string into naming part, email part, and comment part * Splits userId string into naming part, email part, and comment part
* <p/>
* User ID matching:
* http://fiddle.re/t4p6f
* *
* @param userId * @param userId
* @return array with naming (0), email (1), comment (2) * @return theParsedUserInfo
*/ */
public static String[] splitUserId(String userId) { public static UserInfo splitUserId(final String userId) {
String[] result = new String[]{null, null, null}; if (!TextUtils.isEmpty(userId)) {
final Matcher matcher = USER_ID_PATTERN.matcher(userId);
if (userId == null || userId.equals("")) { if (matcher.matches()) {
return result; return new UserInfo(matcher.group(1), matcher.group(4), matcher.group(3));
}
} }
return new UserInfo(null, null, null);
}
/* public static class UserInfo {
* User ID matching: public final String name;
* http://fiddle.re/t4p6f public final String email;
* public final String comment;
* test cases:
* "Max Mustermann (this is a comment) <max@example.com>" public UserInfo(String name, String email, String comment) {
* "Max Mustermann <max@example.com>" this.name = name;
* "Max Mustermann (this is a comment)" this.email = email;
* "Max Mustermann [this is nothing]" this.comment = comment;
*/
Matcher matcher = USER_ID_PATTERN.matcher(userId);
if (matcher.matches()) {
result[0] = matcher.group(1);
result[1] = matcher.group(3);
result[2] = matcher.group(2);
} }
return result;
} }
} }