mirror of
https://github.com/oxen-io/session-android.git
synced 2025-08-11 07:37:25 +00:00
be safer when processing parts in AttachmentDownloadJob
Closes #4166 // FREEBIE
This commit is contained in:

committed by
Moxie Marlinspike

parent
c6abb7dc64
commit
cdf982a356
@@ -1,5 +1,6 @@
|
||||
package org.thoughtcrime.securesms;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.util.Log;
|
||||
@@ -11,15 +12,25 @@ import org.mockito.stubbing.Answer;
|
||||
import org.powermock.api.mockito.PowerMockito;
|
||||
import org.powermock.core.classloader.annotations.PrepareForTest;
|
||||
import org.powermock.modules.junit4.PowerMockRunner;
|
||||
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
||||
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
|
||||
import static org.mockito.Matchers.anyString;
|
||||
import static org.powermock.api.mockito.PowerMockito.mock;
|
||||
import static org.powermock.api.mockito.PowerMockito.mockStatic;
|
||||
|
||||
@RunWith(PowerMockRunner.class)
|
||||
@PrepareForTest( { Log.class, Handler.class, Looper.class })
|
||||
@PrepareForTest({ Log.class, Handler.class, Looper.class })
|
||||
public abstract class BaseUnitTest {
|
||||
protected Context context;
|
||||
protected MasterSecret masterSecret;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
context = mock(Context.class);
|
||||
masterSecret = new MasterSecret(new SecretKeySpec(new byte[16], "AES"),
|
||||
new SecretKeySpec(new byte[16], "HmacSHA1"));
|
||||
mockStatic(Looper.class);
|
||||
mockStatic(Log.class);
|
||||
mockStatic(Handler.class);
|
||||
|
@@ -0,0 +1,24 @@
|
||||
package org.thoughtcrime.securesms.crypto;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.powermock.core.classloader.annotations.PowerMockIgnore;
|
||||
import org.thoughtcrime.securesms.BaseUnitTest;
|
||||
import org.whispersystems.libaxolotl.InvalidMessageException;
|
||||
|
||||
@PowerMockIgnore("javax.crypto.*")
|
||||
public class MasterCipherTest extends BaseUnitTest {
|
||||
private MasterCipher masterCipher;
|
||||
|
||||
@Before
|
||||
@Override
|
||||
public void setUp() throws Exception {
|
||||
super.setUp();
|
||||
masterCipher = new MasterCipher(masterSecret);
|
||||
}
|
||||
|
||||
@Test(expected = InvalidMessageException.class)
|
||||
public void testEncryptBytesWithZeroBody() throws Exception {
|
||||
masterCipher.decryptBytes(new byte[]{});
|
||||
}
|
||||
}
|
@@ -0,0 +1,39 @@
|
||||
package org.thoughtcrime.securesms.jobs;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.thoughtcrime.securesms.BaseUnitTest;
|
||||
import org.thoughtcrime.securesms.database.PartDatabase.PartId;
|
||||
import org.thoughtcrime.securesms.jobs.AttachmentDownloadJob.InvalidPartException;
|
||||
import org.thoughtcrime.securesms.util.Util;
|
||||
|
||||
import ws.com.google.android.mms.pdu.PduPart;
|
||||
|
||||
import static org.powermock.api.mockito.PowerMockito.mock;
|
||||
|
||||
public class AttachmentDownloadJobTest extends BaseUnitTest {
|
||||
private AttachmentDownloadJob job;
|
||||
|
||||
@Before
|
||||
@Override
|
||||
public void setUp() throws Exception {
|
||||
super.setUp();
|
||||
job = new AttachmentDownloadJob(mock(Context.class), 1L, new PartId(1L, 1L));
|
||||
}
|
||||
|
||||
@Test(expected = InvalidPartException.class)
|
||||
public void testCreateAttachmentPointerInvalidId() throws Exception {
|
||||
PduPart part = new PduPart();
|
||||
part.setContentDisposition(Util.toIsoBytes("a long and acceptable valid key like we all want"));
|
||||
job.createAttachmentPointer(masterSecret, part);
|
||||
}
|
||||
|
||||
@Test(expected = InvalidPartException.class)
|
||||
public void testCreateAttachmentPointerInvalidKey() throws Exception {
|
||||
PduPart part = new PduPart();
|
||||
part.setContentDisposition(Util.toIsoBytes("1"));
|
||||
job.createAttachmentPointer(masterSecret, part);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user