This commit is contained in:
Moxie Marlinspike
2018-02-07 14:01:37 -08:00
parent 8bec5a96f5
commit d567534609
72 changed files with 1164 additions and 505 deletions

View File

@@ -1,78 +0,0 @@
package org.thoughtcrime.securesms.database;
import android.net.Uri;
import org.thoughtcrime.securesms.TextSecureTestCase;
import org.thoughtcrime.securesms.attachments.AttachmentId;
import org.thoughtcrime.securesms.attachments.DatabaseAttachment;
import org.thoughtcrime.securesms.crypto.MasterSecret;
import org.thoughtcrime.securesms.util.BitmapDecodingException;
import java.io.FileNotFoundException;
import java.io.InputStream;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyFloat;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
public class AttachmentDatabaseTest extends TextSecureTestCase {
private static final long ROW_ID = 1L;
private static final long UNIQUE_ID = 2L;
private AttachmentDatabase database;
@Override
public void setUp() {
super.setUp();
database = spy(DatabaseFactory.getAttachmentDatabase(getInstrumentation().getTargetContext()));
}
public void testThumbnailGenerationTaskNotRunWhenThumbnailExists() throws Exception {
final AttachmentId attachmentId = new AttachmentId(ROW_ID, UNIQUE_ID);
DatabaseAttachment mockAttachment = getMockAttachment("x/x");
when(database.getAttachment(null, attachmentId)).thenReturn(mockAttachment);
InputStream mockInputStream = mock(InputStream.class);
doReturn(mockInputStream).when(database).getDataStream(any(MasterSecret.class), any(AttachmentId.class), eq("thumbnail"));
database.getThumbnailStream(mock(MasterSecret.class), attachmentId);
// Works as the Future#get() call in AttachmentDatabase#getThumbnailStream() makes updating synchronous
verify(database, never()).updateAttachmentThumbnail(any(MasterSecret.class), any(AttachmentId.class), any(InputStream.class), anyFloat());
}
public void testThumbnailGenerationTaskRunWhenThumbnailMissing() throws Exception {
final AttachmentId attachmentId = new AttachmentId(ROW_ID, UNIQUE_ID);
DatabaseAttachment mockAttachment = getMockAttachment("image/png");
when(database.getAttachment(null, attachmentId)).thenReturn(mockAttachment);
doReturn(null).when(database).getDataStream(any(MasterSecret.class), any(AttachmentId.class), eq("thumbnail"));
doNothing().when(database).updateAttachmentThumbnail(any(MasterSecret.class), any(AttachmentId.class), any(InputStream.class), anyFloat());
try {
database.new ThumbnailFetchCallable(mock(MasterSecret.class), attachmentId).call();
throw new AssertionError("Didn't try to generate thumbnail");
} catch (BitmapDecodingException bde) {
if (!(bde.getCause() instanceof FileNotFoundException)) {
throw new AssertionError("Thumbnail generation failed for another reason than a FileNotFoundException: " + bde.getMessage());
} // else success
}
}
private DatabaseAttachment getMockAttachment(String contentType) {
DatabaseAttachment attachment = mock(DatabaseAttachment.class);
when(attachment.getContentType()).thenReturn(contentType);
when(attachment.getDataUri()).thenReturn(Uri.EMPTY);
when(attachment.hasData()).thenReturn(true);
return attachment;
}
}

View File

@@ -1,119 +0,0 @@
//package org.thoughtcrime.securesms.database;
//
//import org.thoughtcrime.securesms.TextSecureTestCase;
//
//import static org.assertj.core.api.Assertions.assertThat;
//
//public class CanonicalAddressDatabaseTest extends TextSecureTestCase {
// private static final String AMBIGUOUS_NUMBER = "222-3333";
// private static final String SPECIFIC_NUMBER = "+49 444 222 3333";
// private static final String EMAIL = "a@b.fom";
// private static final String SIMILAR_EMAIL = "a@b.com";
// private static final String GROUP = "__textsecure_group__!000111222333";
// private static final String SIMILAR_GROUP = "__textsecure_group__!100111222333";
// private static final String ALPHA = "T-Mobile";
// private static final String SIMILAR_ALPHA = "T-Mobila";
//
// private CanonicalAddressDatabase db;
//
// @Override
// public void setUp() {
// super.setUp();
// this.db = CanonicalAddressDatabase.getInstance(getInstrumentation().getTargetContext());
// }
//
// public void tearDown() throws Exception {
//
// }
//
// /**
// * Throw two equivalent numbers (one without locale info, one with full info) at the canonical
// * address db and see that the caching and DB operations work properly in revealing the right
// * addresses. This is run twice to ensure cache logic is hit.
// *
// * @throws Exception
// */
// public void testNumberAddressUpdates() throws Exception {
// final long id = db.getCanonicalAddressId(AMBIGUOUS_NUMBER);
//
// assertThat(db.getAddressFromId(id)).isEqualTo(AMBIGUOUS_NUMBER);
// assertThat(db.getCanonicalAddressId(SPECIFIC_NUMBER)).isEqualTo(id);
// assertThat(db.getAddressFromId(id)).isEqualTo(SPECIFIC_NUMBER);
// assertThat(db.getCanonicalAddressId(AMBIGUOUS_NUMBER)).isEqualTo(id);
//
// assertThat(db.getCanonicalAddressId(AMBIGUOUS_NUMBER)).isEqualTo(id);
// assertThat(db.getAddressFromId(id)).isEqualTo(AMBIGUOUS_NUMBER);
// assertThat(db.getCanonicalAddressId(SPECIFIC_NUMBER)).isEqualTo(id);
// assertThat(db.getAddressFromId(id)).isEqualTo(SPECIFIC_NUMBER);
// assertThat(db.getCanonicalAddressId(AMBIGUOUS_NUMBER)).isEqualTo(id);
// }
//
// public void testSimilarNumbers() throws Exception {
// assertThat(db.getCanonicalAddressId("This is a phone number 222-333-444"))
// .isNotEqualTo(db.getCanonicalAddressId("222-333-4444"));
// assertThat(db.getCanonicalAddressId("222-333-444"))
// .isNotEqualTo(db.getCanonicalAddressId("222-333-4444"));
// assertThat(db.getCanonicalAddressId("222-333-44"))
// .isNotEqualTo(db.getCanonicalAddressId("222-333-4444"));
// assertThat(db.getCanonicalAddressId("222-333-4"))
// .isNotEqualTo(db.getCanonicalAddressId("222-333-4444"));
// assertThat(db.getCanonicalAddressId("+49 222-333-4444"))
// .isNotEqualTo(db.getCanonicalAddressId("+1 222-333-4444"));
//
// assertThat(db.getCanonicalAddressId("1 222-333-4444"))
// .isEqualTo(db.getCanonicalAddressId("222-333-4444"));
// assertThat(db.getCanonicalAddressId("1 (222) 333-4444"))
// .isEqualTo(db.getCanonicalAddressId("222-333-4444"));
// assertThat(db.getCanonicalAddressId("+12223334444"))
// .isEqualTo(db.getCanonicalAddressId("222-333-4444"));
// assertThat(db.getCanonicalAddressId("+1 (222) 333.4444"))
// .isEqualTo(db.getCanonicalAddressId("222-333-4444"));
// assertThat(db.getCanonicalAddressId("+49 (222) 333.4444"))
// .isEqualTo(db.getCanonicalAddressId("222-333-4444"));
//
// }
//
// public void testEmailAddresses() throws Exception {
// final long emailId = db.getCanonicalAddressId(EMAIL);
// final long similarEmailId = db.getCanonicalAddressId(SIMILAR_EMAIL);
//
// assertThat(emailId).isNotEqualTo(similarEmailId);
//
// assertThat(db.getAddressFromId(emailId)).isEqualTo(EMAIL);
// assertThat(db.getAddressFromId(similarEmailId)).isEqualTo(SIMILAR_EMAIL);
// }
//
// public void testGroups() throws Exception {
// final long groupId = db.getCanonicalAddressId(GROUP);
// final long similarGroupId = db.getCanonicalAddressId(SIMILAR_GROUP);
//
// assertThat(groupId).isNotEqualTo(similarGroupId);
//
// assertThat(db.getAddressFromId(groupId)).isEqualTo(GROUP);
// assertThat(db.getAddressFromId(similarGroupId)).isEqualTo(SIMILAR_GROUP);
// }
//
// public void testAlpha() throws Exception {
// final long id = db.getCanonicalAddressId(ALPHA);
// final long similarId = db.getCanonicalAddressId(SIMILAR_ALPHA);
//
// assertThat(id).isNotEqualTo(similarId);
//
// assertThat(db.getAddressFromId(id)).isEqualTo(ALPHA);
// assertThat(db.getAddressFromId(similarId)).isEqualTo(SIMILAR_ALPHA);
// }
//
// public void testIsNumber() throws Exception {
// assertThat(CanonicalAddressDatabase.isNumberAddress("+495556666777")).isTrue();
// assertThat(CanonicalAddressDatabase.isNumberAddress("(222) 333-4444")).isTrue();
// assertThat(CanonicalAddressDatabase.isNumberAddress("1 (222) 333-4444")).isTrue();
// assertThat(CanonicalAddressDatabase.isNumberAddress("T-Mobile123")).isTrue();
// assertThat(CanonicalAddressDatabase.isNumberAddress("333-4444")).isTrue();
// assertThat(CanonicalAddressDatabase.isNumberAddress("12345")).isTrue();
// assertThat(CanonicalAddressDatabase.isNumberAddress("T-Mobile")).isFalse();
// assertThat(CanonicalAddressDatabase.isNumberAddress("T-Mobile1")).isFalse();
// assertThat(CanonicalAddressDatabase.isNumberAddress("Wherever bank")).isFalse();
// assertThat(CanonicalAddressDatabase.isNumberAddress("__textsecure_group__!afafafafafaf")).isFalse();
// assertThat(CanonicalAddressDatabase.isNumberAddress("email@domain.com")).isFalse();
// }
//}