migration: some cleanup and better test coverage here and there
This commit is contained in:
parent
057309c147
commit
863cb9a2e7
2 changed files with 44 additions and 51 deletions
|
@ -439,20 +439,19 @@ class StoreSchemaDefinition implements LockableDatabase.SchemaDefinition {
|
|||
|
||||
Log.d(K9.LOG_TAG, "Attempting to migrate multipart/encrypted as pgp/mime");
|
||||
|
||||
Cursor cursor = null;
|
||||
try {
|
||||
// we only handle attachment count == 2 here, so simply sorting application/pgp-encrypted
|
||||
// to the front (and application/octet-stream second) should suffice.
|
||||
String orderBy = "(mime_type LIKE 'application/pgp-encrypted') DESC";
|
||||
cursor = db.query("attachments",
|
||||
new String[] {
|
||||
"id", "size", "name", "mime_type", "store_data",
|
||||
"content_uri", "content_id", "content_disposition"
|
||||
},
|
||||
"message_id = ?", new String[] { Long.toString(messageId) }, null, null, orderBy);
|
||||
// we only handle attachment count == 2 here, so simply sorting application/pgp-encrypted
|
||||
// to the front (and application/octet-stream second) should suffice.
|
||||
String orderBy = "(mime_type LIKE 'application/pgp-encrypted') DESC";
|
||||
Cursor cursor = db.query("attachments",
|
||||
new String[] {
|
||||
"id", "size", "name", "mime_type", "store_data",
|
||||
"content_uri", "content_id", "content_disposition"
|
||||
},
|
||||
"message_id = ?", new String[] { Long.toString(messageId) }, null, null, orderBy);
|
||||
|
||||
if (cursor.getCount() < 2) {
|
||||
Log.e(K9.LOG_TAG, "Found multipart/encrypted but not enough attachments, handling as regular mail");
|
||||
try {
|
||||
if (cursor.getCount() != 2) {
|
||||
Log.e(K9.LOG_TAG, "Found multipart/encrypted but bad number of attachments, handling as regular mail");
|
||||
return null;
|
||||
}
|
||||
|
||||
|
@ -514,15 +513,8 @@ class StoreSchemaDefinition implements LockableDatabase.SchemaDefinition {
|
|||
insertMimeAttachmentPart(db, attachmentDirOld, attachmentDirNew, structureState, secondPartId,
|
||||
secondPartSize, secondPartName, "application/octet-stream", secondPartStoreData,
|
||||
secondPartContentUriString, null, null);
|
||||
|
||||
while (cursor.moveToNext()) {
|
||||
Log.e(K9.LOG_TAG, "Ignoring trailing part after multipart/encrypted data.");
|
||||
}
|
||||
|
||||
} finally {
|
||||
if (cursor != null) {
|
||||
cursor.close();
|
||||
}
|
||||
cursor.close();
|
||||
}
|
||||
|
||||
return structureState;
|
||||
|
@ -584,9 +576,7 @@ class StoreSchemaDefinition implements LockableDatabase.SchemaDefinition {
|
|||
htmlContent = htmlContent.replaceAll(Pattern.quote(contentUriString), "cid:" + contentId);
|
||||
}
|
||||
} finally {
|
||||
if (cursor != null) {
|
||||
cursor.close();
|
||||
}
|
||||
cursor.close();
|
||||
}
|
||||
|
||||
return htmlContent;
|
||||
|
@ -610,15 +600,14 @@ class StoreSchemaDefinition implements LockableDatabase.SchemaDefinition {
|
|||
|
||||
private static MimeStructureState insertAttachments(SQLiteDatabase db, File attachmentDirOld, File attachmentDirNew,
|
||||
long messageId, MimeStructureState structureState) {
|
||||
Cursor cursor = null;
|
||||
try {
|
||||
cursor = db.query("attachments",
|
||||
new String[] {
|
||||
"id", "size", "name", "mime_type", "store_data",
|
||||
"content_uri", "content_id", "content_disposition"
|
||||
},
|
||||
"message_id = ?", new String[] { Long.toString(messageId) }, null, null, null);
|
||||
Cursor cursor = db.query("attachments",
|
||||
new String[] {
|
||||
"id", "size", "name", "mime_type", "store_data",
|
||||
"content_uri", "content_id", "content_disposition"
|
||||
},
|
||||
"message_id = ?", new String[] { Long.toString(messageId) }, null, null, null);
|
||||
|
||||
try {
|
||||
while (cursor.moveToNext()) {
|
||||
long id = cursor.getLong(0);
|
||||
int size = cursor.getInt(1);
|
||||
|
@ -635,9 +624,7 @@ class StoreSchemaDefinition implements LockableDatabase.SchemaDefinition {
|
|||
|
||||
}
|
||||
} finally {
|
||||
if (cursor != null) {
|
||||
cursor.close();
|
||||
}
|
||||
cursor.close();
|
||||
}
|
||||
|
||||
return structureState;
|
||||
|
@ -666,8 +653,7 @@ class StoreSchemaDefinition implements LockableDatabase.SchemaDefinition {
|
|||
}
|
||||
|
||||
boolean hasData = contentUriString != null;
|
||||
boolean isMatchingIdAndDataOnDisk;
|
||||
File attachmentFileToMove = null;
|
||||
File attachmentFileToMove;
|
||||
if (hasData) {
|
||||
try {
|
||||
Uri contentUri = Uri.parse(contentUriString);
|
||||
|
@ -677,24 +663,24 @@ class StoreSchemaDefinition implements LockableDatabase.SchemaDefinition {
|
|||
|
||||
File attachmentFile = new File(attachmentDirOld, attachmentId);
|
||||
boolean isExistingAttachmentFile = attachmentFile.exists();
|
||||
isMatchingIdAndDataOnDisk = isMatchingAttachmentId && isExistingAttachmentFile;
|
||||
|
||||
if (!isMatchingAttachmentId) {
|
||||
Log.e(K9.LOG_TAG, "mismatched attachment id. mark as missing");
|
||||
}
|
||||
if (isExistingAttachmentFile) {
|
||||
attachmentFileToMove = attachmentFile;
|
||||
} else {
|
||||
attachmentFileToMove = null;
|
||||
} else if (!isExistingAttachmentFile) {
|
||||
Log.e(K9.LOG_TAG, "attached file doesn't exist. mark as missing");
|
||||
attachmentFileToMove = null;
|
||||
} else {
|
||||
attachmentFileToMove = attachmentFile;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
// anything here fails, conservatively assume the data doesn't exist
|
||||
isMatchingIdAndDataOnDisk = false;
|
||||
attachmentFileToMove = null;
|
||||
}
|
||||
} else {
|
||||
isMatchingIdAndDataOnDisk = false;
|
||||
attachmentFileToMove = null;
|
||||
}
|
||||
if (K9.DEBUG && isMatchingIdAndDataOnDisk) {
|
||||
if (K9.DEBUG && attachmentFileToMove == null) {
|
||||
Log.d(K9.LOG_TAG, "matching attachment is in local cache");
|
||||
}
|
||||
|
||||
|
@ -708,7 +694,7 @@ class StoreSchemaDefinition implements LockableDatabase.SchemaDefinition {
|
|||
cv.put("display_name", name);
|
||||
cv.put("header", mimeHeader.toString());
|
||||
cv.put("encoding", MimeUtil.ENC_BINARY);
|
||||
cv.put("data_location", isMatchingIdAndDataOnDisk ? DataLocation.ON_DISK : DataLocation.MISSING);
|
||||
cv.put("data_location", attachmentFileToMove != null ? DataLocation.ON_DISK : DataLocation.MISSING);
|
||||
cv.put("content_id", contentId);
|
||||
cv.put("server_extra", storeData);
|
||||
structureState.applyValues(cv);
|
||||
|
|
|
@ -188,7 +188,7 @@ public class MigrationTest {
|
|||
"INSERT INTO messages VALUES(3,0,16,'4','mail with attach',1453380649000," +
|
||||
"'X_GOT_ALL_HEADERS,X_DOWNLOADED_PARTIAL','look@my.amazin.horse;','valodim@mugenguild.com'," +
|
||||
"'','','','<pre class=\"k9mail\">ooohh, an attachment!<br /></pre>','ooohh, an attachment!\n'," +
|
||||
"1,1453380654000,'<20160121125049.GB31046@littlepip>','ooohh, an attachment!'," +
|
||||
"2,1453380654000,'<20160121125049.GB31046@littlepip>','ooohh, an attachment!'," +
|
||||
"'multipart/mixed',NULL,0,1,0,0,0)",
|
||||
|
||||
"INSERT INTO headers (message_id, name, value) VALUES (3,'Date','Thu, 21 Jan 2016 13:50:49 +0100')",
|
||||
|
@ -205,6 +205,9 @@ public class MigrationTest {
|
|||
"INSERT INTO attachments VALUES(3,3,'2'," +
|
||||
"'content://com.fsck.k9.attachmentprovider/" + account.getUuid() + "/3/RAW',2250," +
|
||||
"'k9small.png','image/png',NULL,'attachment')",
|
||||
"INSERT INTO attachments VALUES(4,3,'2'," +
|
||||
"'content://com.fsck.k9.attachmentprovider/" + account.getUuid() + "/5/RAW',2250," +
|
||||
"'baduri.png','application/whatevs',NULL,'attachment')",
|
||||
};
|
||||
|
||||
for (String statement : statements) {
|
||||
|
@ -212,6 +215,7 @@ public class MigrationTest {
|
|||
}
|
||||
|
||||
copyAttachmentFromFile("k9small.png", 3, 2250);
|
||||
copyAttachmentFromFile("k9small.png", 5, 2250);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -235,10 +239,10 @@ public class MigrationTest {
|
|||
MimeUtility.getHeaderParameter(msg.getHeader(MimeHeader.HEADER_CONTENT_TYPE)[0], null));
|
||||
Assert.assertEquals("----5D6OUTIYLNN2X63O0R2M0V53TOUAQP",
|
||||
MimeUtility.getHeaderParameter(msg.getHeader(MimeHeader.HEADER_CONTENT_TYPE)[0], "boundary"));
|
||||
Assert.assertEquals(1, msg.getAttachmentCount());
|
||||
Assert.assertEquals(2, msg.getAttachmentCount());
|
||||
|
||||
Multipart body = (Multipart) msg.getBody();
|
||||
Assert.assertEquals(2, body.getCount());
|
||||
Assert.assertEquals(3, body.getCount());
|
||||
|
||||
Assert.assertEquals("multipart/alternative", body.getBodyPart(0).getMimeType());
|
||||
LocalBodyPart attachmentPart = (LocalBodyPart) body.getBodyPart(1);
|
||||
|
@ -253,6 +257,9 @@ public class MigrationTest {
|
|||
FileBackedBody attachmentBody = (FileBackedBody) attachmentPart.getBody();
|
||||
Assert.assertEquals(2250, attachmentBody.getSize());
|
||||
Assert.assertEquals(MimeUtil.ENC_BINARY, attachmentBody.getEncoding());
|
||||
|
||||
Assert.assertEquals("application/whatevs", body.getBodyPart(2).getMimeType());
|
||||
Assert.assertNull(body.getBodyPart(2).getBody());
|
||||
}
|
||||
|
||||
private void insertPgpMimeSignedMessage(SQLiteDatabase db) {
|
||||
|
@ -276,8 +283,8 @@ public class MigrationTest {
|
|||
|
||||
"INSERT INTO threads VALUES(5,4,5,NULL)",
|
||||
|
||||
"INSERT INTO attachments VALUES(5,4,'2',NULL,836,'signature.asc','application/pgp-signature',NULL,'')",
|
||||
"INSERT INTO attachments VALUES(4,4,'1.2',NULL,39456,'smirk.png','image/png',NULL,'attachment')",
|
||||
"INSERT INTO attachments VALUES(6,4,'2',NULL,836,'signature.asc','application/pgp-signature',NULL,'')",
|
||||
"INSERT INTO attachments VALUES(5,4,'1.2',NULL,39456,'smirk.png','image/png',NULL,'attachment')",
|
||||
};
|
||||
|
||||
for (String statement : statements) {
|
||||
|
|
Loading…
Reference in a new issue