Test & Refactor OpenPgpUtils

This commit is contained in:
ligi 2015-01-29 10:54:31 +01:00
parent 4827b4c437
commit 4bc9d94831
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

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

View File

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