Merge pull request #1816 from fiaxh/no_name_choose_email

Correctly handle keys only having an email address
This commit is contained in:
Dominik Schürmann
2016-04-29 21:40:00 +02:00
4 changed files with 49 additions and 9 deletions

View File

@@ -61,6 +61,8 @@ public abstract class KeyRing {
private static final Pattern USER_ID_PATTERN = Pattern.compile("^(.*?)(?: \\((.*)\\))?(?: <(.*)>)?$"); 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 * Splits userId string into naming part, email part, and comment part
* <p/> * <p/>
@@ -71,7 +73,14 @@ public abstract class KeyRing {
if (!TextUtils.isEmpty(userId)) { if (!TextUtils.isEmpty(userId)) {
final Matcher matcher = USER_ID_PATTERN.matcher(userId); final Matcher matcher = USER_ID_PATTERN.matcher(userId);
if (matcher.matches()) { 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); return new UserId(null, null, null);

View File

@@ -54,7 +54,7 @@ import java.io.IOException;
*/ */
public class KeychainDatabase extends SQLiteOpenHelper { public class KeychainDatabase extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "openkeychain.db"; 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; static Boolean apgHack = false;
private Context mContext; private Context mContext;

View File

@@ -456,12 +456,12 @@ public class KeychainProvider extends ContentProvider {
if (i != 0) { if (i != 0) {
emailWhere += " OR "; emailWhere += " OR ";
} }
emailWhere += "tmp." + UserPackets.USER_ID + " LIKE ";
// match '*<email>', so it has to be at the *end* of the user id
if (match == KEY_RINGS_FIND_BY_EMAIL) { if (match == KEY_RINGS_FIND_BY_EMAIL) {
emailWhere += DatabaseUtils.sqlEscapeString("%<" + chunks[i] + ">"); emailWhere += "tmp." + UserPackets.EMAIL + " LIKE "
+ DatabaseUtils.sqlEscapeString(chunks[i]);
} else { } else {
emailWhere += DatabaseUtils.sqlEscapeString("%" + chunks[i] + "%"); emailWhere += "tmp." + UserPackets.USER_ID + " LIKE "
+ DatabaseUtils.sqlEscapeString("%" + chunks[i] + "%");
} }
gotCondition = true; gotCondition = true;
} }

View File

@@ -28,7 +28,7 @@ import org.sufficientlysecure.keychain.WorkaroundBuildConfig;
@RunWith(RobolectricGradleTestRunner.class) @RunWith(RobolectricGradleTestRunner.class)
@Config(constants = WorkaroundBuildConfig.class, sdk = 21, manifest = "src/main/AndroidManifest.xml") @Config(constants = WorkaroundBuildConfig.class, sdk = 21, manifest = "src/main/AndroidManifest.xml")
public class KeyRingTest { public class SplitUserIdTest {
@Test @Test
public void splitCompleteUserIdShouldReturnAll3Components() throws Exception { public void splitCompleteUserIdShouldReturnAll3Components() throws Exception {
@@ -49,9 +49,40 @@ public class KeyRingTest {
@Test @Test
public void splitUserIdWithAllButEmailShouldReturnNameAndComment() throws Exception { public void splitUserIdWithAllButEmailShouldReturnNameAndComment() throws Exception {
KeyRing.UserId info = KeyRing.splitUserId("Max Mustermann (this is a comment)"); KeyRing.UserId info = KeyRing.splitUserId("Max Mustermann (this is a comment)");
Assert.assertEquals(info.name, "Max Mustermann"); Assert.assertEquals("Max Mustermann", info.name);
Assert.assertEquals(info.comment, "this is a comment"); Assert.assertEquals("this is a comment", info.comment);
Assert.assertNull(info.email); 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);
}
} }