Guard against malformed group ids.

This commit is contained in:
Alan Evans
2020-04-20 12:01:31 -03:00
committed by Greyson Parrelli
parent 00ee6d0bbd
commit 9a8094cb8a
21 changed files with 200 additions and 94 deletions

View File

@@ -23,7 +23,7 @@ public final class GroupIdTest {
@Test
public void can_create_for_gv1() {
GroupId.V1 groupId = GroupId.v1(new byte[]{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 });
GroupId.V1 groupId = GroupId.v1orThrow(new byte[]{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 });
assertEquals("__textsecure_group__!000102030405060708090a0b0c0d0e0f", groupId.toString());
assertFalse(groupId.isMms());
@@ -31,7 +31,7 @@ public final class GroupIdTest {
@Test
public void can_parse_gv1() {
GroupId groupId = GroupId.parse("__textsecure_group__!000102030405060708090a0b0c0d0e0f");
GroupId groupId = GroupId.parseOrThrow("__textsecure_group__!000102030405060708090a0b0c0d0e0f");
assertEquals("__textsecure_group__!000102030405060708090a0b0c0d0e0f", groupId.toString());
assertArrayEquals(new byte[]{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }, groupId.getDecodedId());
@@ -67,7 +67,7 @@ public final class GroupIdTest {
@Test
public void can_parse_gv2() throws IOException {
GroupId groupId = GroupId.parse("__textsecure_group__!9f475f59b2518bff6df22e820803f0e3585bd99e686fa7e7fbfc2f92fd5d953e");
GroupId groupId = GroupId.parseOrThrow("__textsecure_group__!9f475f59b2518bff6df22e820803f0e3585bd99e686fa7e7fbfc2f92fd5d953e");
assertEquals("__textsecure_group__!9f475f59b2518bff6df22e820803f0e3585bd99e686fa7e7fbfc2f92fd5d953e", groupId.toString());
assertArrayEquals(Hex.fromStringCondensed("9f475f59b2518bff6df22e820803f0e3585bd99e686fa7e7fbfc2f92fd5d953e"), groupId.getDecodedId());
@@ -90,7 +90,7 @@ public final class GroupIdTest {
@Test
public void can_parse_mms() {
GroupId groupId = GroupId.parse("__signal_mms_group__!000102030405060708090a0b0c0d0e0f");
GroupId groupId = GroupId.parseOrThrow("__signal_mms_group__!000102030405060708090a0b0c0d0e0f");
assertEquals("__signal_mms_group__!000102030405060708090a0b0c0d0e0f", groupId.toString());
assertArrayEquals(new byte[]{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }, groupId.getDecodedId());
@@ -103,14 +103,14 @@ public final class GroupIdTest {
@SuppressWarnings("ConstantConditions")
@Test
public void can_parse_null() {
GroupId groupId = GroupId.parseNullable(null);
GroupId groupId = GroupId.parseNullableOrThrow(null);
assertNull(groupId);
}
@Test
public void can_parse_gv1_with_parseNullable() {
GroupId groupId = GroupId.parseNullable("__textsecure_group__!000102030405060708090a0b0c0d0e0f");
GroupId groupId = GroupId.parseNullableOrThrow("__textsecure_group__!000102030405060708090a0b0c0d0e0f");
assertEquals("__textsecure_group__!000102030405060708090a0b0c0d0e0f", groupId.toString());
assertArrayEquals(new byte[]{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }, groupId.getDecodedId());
@@ -122,41 +122,41 @@ public final class GroupIdTest {
@Test(expected = AssertionError.class)
public void bad_encoding__bad_prefix__parseNullable() {
GroupId.parseNullable("__BAD_PREFIX__!000102030405060708090a0b0c0d0e0f");
GroupId.parseNullableOrThrow("__BAD_PREFIX__!000102030405060708090a0b0c0d0e0f");
}
@Test(expected = AssertionError.class)
public void bad_encoding__empty__parseNullable() {
GroupId.parseNullable("");
GroupId.parseNullableOrThrow("");
}
@Test(expected = AssertionError.class)
public void bad_encoding__odd_hex__parseNullable() {
GroupId.parseNullable("__textsecure_group__!0001020305060708090bODD_HEX");
GroupId.parseNullableOrThrow("__textsecure_group__!0001020305060708090bODD_HEX");
}
@Test(expected = AssertionError.class)
public void bad_encoding__bad_prefix__parse() {
GroupId.parse("__BAD_PREFIX__!000102030405060708090a0b0c0d0e0f");
GroupId.parseOrThrow("__BAD_PREFIX__!000102030405060708090a0b0c0d0e0f");
}
@Test(expected = AssertionError.class)
public void bad_encoding__odd_hex__parse() {
GroupId.parse("__textsecure_group__!0001020305060708090b0c0d0e0fODD_HEX");
GroupId.parseOrThrow("__textsecure_group__!0001020305060708090b0c0d0e0fODD_HEX");
}
@Test
public void get_bytes() {
byte[] bytes = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
GroupId groupId = GroupId.v1(bytes);
GroupId groupId = GroupId.v1orThrow(bytes);
assertArrayEquals(bytes, groupId.getDecodedId());
}
@Test
public void equality() {
GroupId groupId1 = GroupId.v1(new byte[]{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 });
GroupId groupId2 = GroupId.v1(new byte[]{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 });
GroupId groupId1 = GroupId.v1orThrow(new byte[]{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 });
GroupId groupId2 = GroupId.v1orThrow(new byte[]{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 });
assertNotSame(groupId1, groupId2);
assertEquals(groupId1, groupId2);
@@ -165,8 +165,8 @@ public final class GroupIdTest {
@Test
public void inequality_by_bytes() {
GroupId groupId1 = GroupId.v1(new byte[]{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 });
GroupId groupId2 = GroupId.v1(new byte[]{ 0, 3, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 });
GroupId groupId1 = GroupId.v1orThrow(new byte[]{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 });
GroupId groupId2 = GroupId.v1orThrow(new byte[]{ 0, 3, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 });
assertNotSame(groupId1, groupId2);
assertNotEquals(groupId1, groupId2);
@@ -175,7 +175,7 @@ public final class GroupIdTest {
@Test
public void inequality_of_sms_and_mms() {
GroupId groupId1 = GroupId.v1(new byte[]{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 });
GroupId groupId1 = GroupId.v1orThrow(new byte[]{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 });
GroupId groupId2 = GroupId.mms(new byte[]{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 });
assertNotSame(groupId1, groupId2);
@@ -185,14 +185,14 @@ public final class GroupIdTest {
@Test
public void inequality_with_null() {
GroupId groupId = GroupId.v1(new byte[]{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 });
GroupId groupId = GroupId.v1orThrow(new byte[]{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 });
assertNotEquals(groupId, null);
}
@Test
public void require_mms() {
GroupId groupId = GroupId.parse("__signal_mms_group__!000102030405060708090a0b0c0d0e0f");
GroupId groupId = GroupId.parseOrThrow("__signal_mms_group__!000102030405060708090a0b0c0d0e0f");
GroupId.Mms mms = groupId.requireMms();
@@ -201,7 +201,7 @@ public final class GroupIdTest {
@Test
public void require_v1_and_push() {
GroupId groupId = GroupId.parse("__textsecure_group__!000102030405060708090a0b0c0d0e0f");
GroupId groupId = GroupId.parseOrThrow("__textsecure_group__!000102030405060708090a0b0c0d0e0f");
GroupId.V1 v1 = groupId.requireV1();
GroupId.Push push = groupId.requirePush();
@@ -212,9 +212,9 @@ public final class GroupIdTest {
@Test
public void require_v2_and_push() {
GroupId groupId = GroupId.parse("__textsecure_group__!9f475f59b2518bff6df22e820803f0e3585bd99e686fa7e7fbfc2f92fd5d953e");
GroupId groupId = GroupId.parseOrThrow("__textsecure_group__!9f475f59b2518bff6df22e820803f0e3585bd99e686fa7e7fbfc2f92fd5d953e");
GroupId.V2 v2 = groupId.requireV2 ();
GroupId.V2 v2 = groupId.requireV2();
GroupId.Push push = groupId.requirePush();
assertSame(groupId, v2);
@@ -222,35 +222,35 @@ public final class GroupIdTest {
}
@Test(expected = AssertionError.class)
public void cannot_require_push_of_mms() {
public void cannot_require_push_of_mms() throws BadGroupIdException {
GroupId groupId = GroupId.parse("__signal_mms_group__!000102030405060708090a0b0c0d0e0f");
groupId.requirePush();
}
@Test(expected = AssertionError.class)
public void cannot_require_v1_of_mms() {
public void cannot_require_v1_of_mms() throws BadGroupIdException {
GroupId groupId = GroupId.parse("__signal_mms_group__!000102030405060708090a0b0c0d0e0f");
groupId.requireV1();
}
@Test(expected = AssertionError.class)
public void cannot_require_v2_of_mms() {
public void cannot_require_v2_of_mms() throws BadGroupIdException {
GroupId groupId = GroupId.parse("__signal_mms_group__!000102030405060708090a0b0c0d0e0f");
groupId.requireV2();
}
@Test(expected = AssertionError.class)
public void cannot_require_v1_of_v2() {
public void cannot_require_v1_of_v2() throws BadGroupIdException {
GroupId groupId = GroupId.parse("__textsecure_group__!9f475f59b2518bff6df22e820803f0e3585bd99e686fa7e7fbfc2f92fd5d953e");
groupId.requireV1();
}
@Test(expected = AssertionError.class)
public void cannot_require_v2_of_v1() {
public void cannot_require_v2_of_v1() throws BadGroupIdException {
GroupId groupId = GroupId.parse("__textsecure_group__!000102030405060708090a0b0c0d0e0f");
groupId.requireV2();
@@ -258,12 +258,17 @@ public final class GroupIdTest {
@Test(expected = AssertionError.class)
public void cannot_create_v1_with_a_v2_length() throws IOException {
GroupId.v1(Hex.fromStringCondensed("9f475f59b2518bff6df22e820803f0e3585bd99e686fa7e7fbfc2f92fd5d953e"));
GroupId.v1orThrow(Hex.fromStringCondensed("9f475f59b2518bff6df22e820803f0e3585bd99e686fa7e7fbfc2f92fd5d953e"));
}
@Test(expected = BadGroupIdException.class)
public void cannot_create_v2_with_a_v1_length() throws IOException, BadGroupIdException {
GroupId.v2(Hex.fromStringCondensed("000102030405060708090a0b0c0d0e0f"));
}
@Test(expected = AssertionError.class)
public void cannot_create_v2_with_a_v1_length() throws IOException {
GroupId.v2(Hex.fromStringCondensed("000102030405060708090a0b0c0d0e0f"));
public void cannot_create_v2_with_a_v1_length_assert() throws IOException {
GroupId.v2orThrow(Hex.fromStringCondensed("000102030405060708090a0b0c0d0e0f"));
}
@Test
@@ -283,14 +288,14 @@ public final class GroupIdTest {
}
@Test
public void parse_bytes_to_v1_via_push() {
public void parse_bytes_to_v1_via_push() throws BadGroupIdException {
GroupId.V1 v1 = GroupId.push(new byte[]{ 9, 10, 11, 12, 13, 14, 15, 0, 1, 2, 3, 4, 5, 6, 7, 8 }).requireV1();
assertEquals("__textsecure_group__!090a0b0c0d0e0f000102030405060708", v1.toString());
}
@Test
public void parse_bytes_to_v2_via_by_push() {
public void parse_bytes_to_v2_via_by_push() throws BadGroupIdException {
GroupId.V2 v2 = GroupId.push(new byte[]{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }).requireV2();
assertEquals("__textsecure_group__!000102030405060708090a0b0c0d0e0f000102030405060708090a0b0c0d0e0f", v2.toString());