diff options
author | fiaxh <github@lightrise.org> | 2016-04-04 17:40:31 +0200 |
---|---|---|
committer | fiaxh <github@lightrise.org> | 2016-04-12 13:30:43 +0200 |
commit | 6881754062dcd0b83431cfe36398d20e30922096 (patch) | |
tree | 9a79305979ae9ace711ff12434819b751959c4ce | |
parent | 2d73b74dde85f67b4302606938fdcc708bafc4c0 (diff) | |
download | open-keychain-6881754062dcd0b83431cfe36398d20e30922096.tar.gz open-keychain-6881754062dcd0b83431cfe36398d20e30922096.tar.bz2 open-keychain-6881754062dcd0b83431cfe36398d20e30922096.zip |
Better handle user_id sidecases while splitting
-rw-r--r-- | OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/KeyRing.java | 11 | ||||
-rw-r--r-- | OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/KeychainDatabase.java | 2 | ||||
-rw-r--r-- | OpenKeychain/src/test/java/org/sufficientlysecure/keychain/pgp/SplitUserIdTest.java (renamed from OpenKeychain/src/test/java/org/sufficientlysecure/keychain/pgp/KeyRingTest.java) | 37 |
3 files changed, 45 insertions, 5 deletions
diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/KeyRing.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/KeyRing.java index 77977b691..94949d749 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/KeyRing.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/KeyRing.java @@ -61,6 +61,8 @@ public abstract class KeyRing { private static final Pattern USER_ID_PATTERN = Pattern.compile("^(.*?)(?: \\((.*)\\))?(?: <(.*)>)?$"); + private static final Pattern EMAIL_PATTERN = Pattern.compile("^.*@.*\\..*$"); + /** * Splits userId string into naming part, email part, and comment part * <p/> @@ -71,7 +73,14 @@ public abstract class KeyRing { if (!TextUtils.isEmpty(userId)) { final Matcher matcher = USER_ID_PATTERN.matcher(userId); if (matcher.matches()) { - return new UserId(matcher.group(1), matcher.group(3), matcher.group(2)); + String name = matcher.group(1).isEmpty() ? null : matcher.group(1); + String comment = matcher.group(2); + String email = matcher.group(3); + if (comment == null && email == null && name != null && EMAIL_PATTERN.matcher(name).matches()) { + email = name; + name = null; + } + return new UserId(name, email, comment); } } return new UserId(null, null, null); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/KeychainDatabase.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/KeychainDatabase.java index 04c14491b..0eb7a0cdb 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/KeychainDatabase.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/KeychainDatabase.java @@ -54,7 +54,7 @@ import java.io.IOException; */ public class KeychainDatabase extends SQLiteOpenHelper { private static final String DATABASE_NAME = "openkeychain.db"; - private static final int DATABASE_VERSION = 16; + private static final int DATABASE_VERSION = 17; static Boolean apgHack = false; private Context mContext; diff --git a/OpenKeychain/src/test/java/org/sufficientlysecure/keychain/pgp/KeyRingTest.java b/OpenKeychain/src/test/java/org/sufficientlysecure/keychain/pgp/SplitUserIdTest.java index 64316b2a6..97feeea7b 100644 --- a/OpenKeychain/src/test/java/org/sufficientlysecure/keychain/pgp/KeyRingTest.java +++ b/OpenKeychain/src/test/java/org/sufficientlysecure/keychain/pgp/SplitUserIdTest.java @@ -28,7 +28,7 @@ import org.sufficientlysecure.keychain.WorkaroundBuildConfig; @RunWith(RobolectricGradleTestRunner.class) @Config(constants = WorkaroundBuildConfig.class, sdk = 21, manifest = "src/main/AndroidManifest.xml") -public class KeyRingTest { +public class SplitUserIdTest { @Test public void splitCompleteUserIdShouldReturnAll3Components() throws Exception { @@ -49,9 +49,40 @@ public class KeyRingTest { @Test public void splitUserIdWithAllButEmailShouldReturnNameAndComment() throws Exception { KeyRing.UserId info = KeyRing.splitUserId("Max Mustermann (this is a comment)"); - Assert.assertEquals(info.name, "Max Mustermann"); - Assert.assertEquals(info.comment, "this is a comment"); + Assert.assertEquals("Max Mustermann", info.name); + Assert.assertEquals("this is a comment", info.comment); + Assert.assertNull(info.email); + } + + @Test + public void splitUserIdWithCommentAndEmailShouldReturnCommentAndEmail() throws Exception { + KeyRing.UserId info = KeyRing.splitUserId(" (this is a comment) <max@example.com>"); + Assert.assertNull(info.name); + Assert.assertEquals("this is a comment", info.comment); + Assert.assertEquals("max@example.com", info.email); + } + + @Test + public void splitUserIdWithEmailShouldReturnEmail() throws Exception { + KeyRing.UserId info = KeyRing.splitUserId("max@example.com"); + Assert.assertNull(info.name); + Assert.assertNull(info.comment); + Assert.assertEquals("max@example.com", info.email); + } + + @Test + public void splitUserIdWithNameShouldReturnName() throws Exception { + KeyRing.UserId info = KeyRing.splitUserId("Max Mustermann"); + Assert.assertEquals("Max Mustermann", info.name); + Assert.assertNull(info.comment); Assert.assertNull(info.email); } + @Test + public void splitUserIdWithCommentShouldReturnComment() throws Exception { + KeyRing.UserId info = KeyRing.splitUserId(" (this is a comment)"); + Assert.assertNull(info.name); + Assert.assertEquals("this is a comment", info.comment); + Assert.assertNull(info.email); + } }
\ No newline at end of file |