Skip native LibSignal tests on unsupported and non-unix OS.

This commit is contained in:
Alan Evans
2021-01-22 00:33:06 -04:00
parent a3176bbb67
commit e2d297eb8a
8 changed files with 114 additions and 28 deletions

View File

@@ -16,8 +16,8 @@ import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.thoughtcrime.securesms.groups.ZkGroupLibraryUtil.assumeZkGroupSupportedOnOS;
import static org.thoughtcrime.securesms.testutil.SecureRandomTestUtil.mockRandom;
import static org.thoughtcrime.securesms.testutil.ZkGroupLibraryUtil.assumeZkGroupSupportedOnOS;
public final class GroupIdTest {

View File

@@ -1,5 +1,6 @@
package org.thoughtcrime.securesms.groups;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
@@ -10,11 +11,18 @@ import java.util.Arrays;
import java.util.Collection;
import static org.junit.Assert.assertEquals;
import static org.thoughtcrime.securesms.groups.ZkGroupLibraryUtil.assumeZkGroupSupportedOnOS;
import static org.thoughtcrime.securesms.testutil.LibSignalLibraryUtil.assumeLibSignalSupportedOnOS;
import static org.thoughtcrime.securesms.testutil.ZkGroupLibraryUtil.assumeZkGroupSupportedOnOS;
@RunWith(Parameterized.class)
public final class GroupId_v1_v2_migration_derivation_Test {
@Before
public void ensureNativeSupported() {
assumeLibSignalSupportedOnOS();
assumeZkGroupSupportedOnOS();
}
@Parameterized.Parameter(0)
public String inputV1GroupId;
@@ -44,8 +52,6 @@ public final class GroupId_v1_v2_migration_derivation_Test {
@Test
public void deriveMigrationV2GroupId() {
assumeZkGroupSupportedOnOS();
GroupId.V1 groupV1Id = GroupId.v1orThrow(Hex.fromStringOrThrow(inputV1GroupId));
GroupId.V2 migratedV2GroupId = groupV1Id.deriveV2MigrationGroupId();

View File

@@ -7,9 +7,11 @@ import com.annimon.stream.Stream;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.modules.junit4.PowerMockRunnerDelegate;
import org.signal.core.util.logging.Log;
import org.thoughtcrime.securesms.recipients.Recipient;
import org.thoughtcrime.securesms.storage.StorageSyncHelper.KeyDifferenceResult;
@@ -42,6 +44,7 @@ import static org.junit.Assert.assertNotEquals;
import static org.mockito.Mockito.when;
import static org.powermock.api.mockito.PowerMockito.mock;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
import static org.thoughtcrime.securesms.testutil.LibSignalLibraryUtil.assumeLibSignalSupportedOnOS;
import static org.thoughtcrime.securesms.testutil.TestHelpers.assertByteListEquals;
import static org.thoughtcrime.securesms.testutil.TestHelpers.assertContentsEqual;
import static org.thoughtcrime.securesms.testutil.TestHelpers.byteArray;
@@ -51,6 +54,7 @@ import static org.thoughtcrime.securesms.testutil.TestHelpers.setOf;
@RunWith(PowerMockRunner.class)
@PrepareForTest({ Recipient.class, FeatureFlags.class})
@PowerMockIgnore("javax.crypto.*")
@PowerMockRunnerDelegate(JUnit4.class)
public final class StorageSyncHelperTest {
private static final UUID UUID_A = UuidUtil.parseOrThrow("ebef429e-695e-4f51-bcc4-526a60ac68c7");
@@ -214,6 +218,8 @@ public final class StorageSyncHelperTest {
@Test
public void resolveConflict_group_v1_sameAsRemote() {
assumeLibSignalSupportedOnOS();
SignalGroupV1Record remote1 = groupV1(1, 1, true, false);
SignalGroupV1Record local1 = groupV1(2, 1, true, false);
@@ -279,6 +285,8 @@ public final class StorageSyncHelperTest {
@Test
public void resolveConflict_complex() {
assumeLibSignalSupportedOnOS();
SignalContactRecord remote1 = contact(1, UUID_A, null, "a");
SignalContactRecord local1 = contact(2, UUID_A, E164_A, "a");

View File

@@ -1,21 +1,24 @@
package org.thoughtcrime.securesms.groups;
package org.thoughtcrime.securesms.testutil;
import org.signal.zkgroup.internal.Native;
import org.signal.client.internal.Native;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;
import static org.junit.Assume.assumeNoException;
class ZkGroupLibraryUtil {
public final class LibSignalLibraryUtil {
/**
* Attempts to initialize the ZkGroup Native class, which will load the native binaries.
* Attempts to initialize the LibSignal Native class, which will load the native binaries.
* <p>
* If that fails to link, then on Unix, it will fail as we rely on that for CI.
* <p>
* If that fails to link, and it's not Unix, it will skip the test via assumption violation.
* <p>
* If using inside a PowerMocked test, the assumption violation can be fatal, use:
* {@code @PowerMockRunnerDelegate(JUnit4.class)}
*/
static void assumeZkGroupSupportedOnOS() {
public static void assumeLibSignalSupportedOnOS() {
try {
Class.forName(Native.class.getName());
} catch (ClassNotFoundException e) {
@@ -24,9 +27,9 @@ class ZkGroupLibraryUtil {
String osName = System.getProperty("os.name");
if (isUnix(osName)) {
fail("Not able to link native ZkGroup on a key OS: " + osName);
fail("Not able to link native LibSignal on a key OS: " + osName);
} else {
assumeNoException("Not able to link native ZkGroup on this operating system: " + osName, e);
assumeNoException("Not able to link native LibSignal on this operating system: " + osName, e);
}
}
}

View File

@@ -17,6 +17,9 @@ public final class ZkGroupLibraryUtil {
* If that fails to link, then on Unix, it will fail as we rely on that for CI.
* <p>
* If that fails to link, and it's not Unix, it will skip the test via assumption violation.
* <p>
* If using inside a PowerMocked test, the assumption violation can be fatal, use:
* {@code @PowerMockRunnerDelegate(JUnit4.class)}
*/
public static void assumeZkGroupSupportedOnOS() {
try {

View File

@@ -1,8 +1,7 @@
package org.whispersystems.signalservice.api.crypto;
import junit.framework.TestCase;
import org.conscrypt.Conscrypt;
import org.junit.Test;
import org.whispersystems.libsignal.InvalidMessageException;
import org.whispersystems.libsignal.kdf.HKDFv3;
import org.whispersystems.signalservice.internal.util.Util;
@@ -16,13 +15,18 @@ import java.io.OutputStream;
import java.security.Security;
import java.util.Arrays;
public class AttachmentCipherTest extends TestCase {
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertTrue;
import static org.whispersystems.signalservice.testutil.LibSignalLibraryUtil.assumeLibSignalSupportedOnOS;
public final class AttachmentCipherTest {
static {
Security.insertProviderAt(Conscrypt.newProvider(), 1);
}
public void test_attachment_encryptDecrypt() throws IOException, InvalidMessageException {
@Test
public void attachment_encryptDecrypt() throws IOException, InvalidMessageException {
byte[] key = Util.getSecretBytes(64);
byte[] plaintextInput = "Peter Parker".getBytes();
EncryptResult encryptResult = encryptData(plaintextInput, key);
@@ -30,12 +34,13 @@ public class AttachmentCipherTest extends TestCase {
InputStream inputStream = AttachmentCipherInputStream.createForAttachment(cipherFile, plaintextInput.length, key, encryptResult.digest);
byte[] plaintextOutput = readInputStreamFully(inputStream);
assertTrue(Arrays.equals(plaintextInput, plaintextOutput));
assertArrayEquals(plaintextInput, plaintextOutput);
cipherFile.delete();
}
public void test_attachment_encryptDecryptEmpty() throws IOException, InvalidMessageException {
@Test
public void attachment_encryptDecryptEmpty() throws IOException, InvalidMessageException {
byte[] key = Util.getSecretBytes(64);
byte[] plaintextInput = "".getBytes();
EncryptResult encryptResult = encryptData(plaintextInput, key);
@@ -43,12 +48,13 @@ public class AttachmentCipherTest extends TestCase {
InputStream inputStream = AttachmentCipherInputStream.createForAttachment(cipherFile, plaintextInput.length, key, encryptResult.digest);
byte[] plaintextOutput = readInputStreamFully(inputStream);
assertTrue(Arrays.equals(plaintextInput, plaintextOutput));
assertArrayEquals(plaintextInput, plaintextOutput);
cipherFile.delete();
}
public void test_attachment_decryptFailOnBadKey() throws IOException{
@Test
public void attachment_decryptFailOnBadKey() throws IOException{
File cipherFile = null;
boolean hitCorrectException = false;
@@ -72,7 +78,8 @@ public class AttachmentCipherTest extends TestCase {
assertTrue(hitCorrectException);
}
public void test_attachment_decryptFailOnBadDigest() throws IOException{
@Test
public void attachment_decryptFailOnBadDigest() throws IOException{
File cipherFile = null;
boolean hitCorrectException = false;
@@ -96,7 +103,8 @@ public class AttachmentCipherTest extends TestCase {
assertTrue(hitCorrectException);
}
public void test_attachment_decryptFailOnNullDigest() throws IOException{
@Test
public void attachment_decryptFailOnNullDigest() throws IOException {
File cipherFile = null;
boolean hitCorrectException = false;
@@ -119,7 +127,8 @@ public class AttachmentCipherTest extends TestCase {
assertTrue(hitCorrectException);
}
public void test_attachment_decryptFailOnBadMac() throws IOException {
@Test
public void attachment_decryptFailOnBadMac() throws IOException {
File cipherFile = null;
boolean hitCorrectException = false;
@@ -145,27 +154,36 @@ public class AttachmentCipherTest extends TestCase {
assertTrue(hitCorrectException);
}
public void test_sticker_encryptDecrypt() throws IOException, InvalidMessageException {
@Test
public void sticker_encryptDecrypt() throws IOException, InvalidMessageException {
assumeLibSignalSupportedOnOS();
byte[] packKey = Util.getSecretBytes(32);
byte[] plaintextInput = "Peter Parker".getBytes();
EncryptResult encryptResult = encryptData(plaintextInput, expandPackKey(packKey));
InputStream inputStream = AttachmentCipherInputStream.createForStickerData(encryptResult.ciphertext, packKey);
byte[] plaintextOutput = readInputStreamFully(inputStream);
assertTrue(Arrays.equals(plaintextInput, plaintextOutput));
assertArrayEquals(plaintextInput, plaintextOutput);
}
public void test_sticker_encryptDecryptEmpty() throws IOException, InvalidMessageException {
@Test
public void sticker_encryptDecryptEmpty() throws IOException, InvalidMessageException {
assumeLibSignalSupportedOnOS();
byte[] packKey = Util.getSecretBytes(32);
byte[] plaintextInput = "".getBytes();
EncryptResult encryptResult = encryptData(plaintextInput, expandPackKey(packKey));
InputStream inputStream = AttachmentCipherInputStream.createForStickerData(encryptResult.ciphertext, packKey);
byte[] plaintextOutput = readInputStreamFully(inputStream);
assertTrue(Arrays.equals(plaintextInput, plaintextOutput));
assertArrayEquals(plaintextInput, plaintextOutput);
}
public void test_sticker_decryptFailOnBadKey() throws IOException{
@Test
public void sticker_decryptFailOnBadKey() throws IOException {
assumeLibSignalSupportedOnOS();
boolean hitCorrectException = false;
try {
@@ -182,7 +200,10 @@ public class AttachmentCipherTest extends TestCase {
assertTrue(hitCorrectException);
}
public void test_sticker_decryptFailOnBadMac() throws IOException {
@Test
public void sticker_decryptFailOnBadMac() throws IOException {
assumeLibSignalSupportedOnOS();
boolean hitCorrectException = false;
try {

View File

@@ -0,0 +1,42 @@
package org.whispersystems.signalservice.testutil;
import org.signal.client.internal.Native;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;
import static org.junit.Assume.assumeNoException;
public final class LibSignalLibraryUtil {
/**
* Attempts to initialize the LibSignal Native class, which will load the native binaries.
* <p>
* If that fails to link, then on Unix, it will fail as we rely on that for CI.
* <p>
* If that fails to link, and it's not Unix, it will skip the test via assumption violation.
* <p>
* If using inside a PowerMocked test, the assumption violation can be fatal, use:
* {@code @PowerMockRunnerDelegate(JUnit4.class)}
*/
public static void assumeLibSignalSupportedOnOS() {
try {
Class.forName(Native.class.getName());
} catch (ClassNotFoundException e) {
fail();
} catch (NoClassDefFoundError | UnsatisfiedLinkError e) {
String osName = System.getProperty("os.name");
if (isUnix(osName)) {
fail("Not able to link native LibSignal on a key OS: " + osName);
} else {
assumeNoException("Not able to link native LibSignal on this operating system: " + osName, e);
}
}
}
private static boolean isUnix(String osName) {
assertNotNull(osName);
osName = osName.toLowerCase();
return osName.contains("nix") || osName.contains("nux") || osName.contains("aix");
}
}

View File

@@ -17,6 +17,9 @@ public final class ZkGroupLibraryUtil {
* If that fails to link, then on Unix, it will fail as we rely on that for CI.
* <p>
* If that fails to link, and it's not Unix, it will skip the test via assumption violation.
* <p>
* If using inside a PowerMocked test, the assumption violation can be fatal, use:
* {@code @PowerMockRunnerDelegate(JUnit4.class)}
*/
public static void assumeZkGroupSupportedOnOS() {
try {