Strictly enforce ACI service identifier strings have no prefix
This commit is contained in:
parent
408b065b9e
commit
3820a231ec
|
@ -25,8 +25,6 @@ import org.whispersystems.textsecuregcm.util.UUIDUtil;
|
||||||
description = "An identifier for an account based on the account's ACI"
|
description = "An identifier for an account based on the account's ACI"
|
||||||
)
|
)
|
||||||
public record AciServiceIdentifier(UUID uuid) implements ServiceIdentifier {
|
public record AciServiceIdentifier(UUID uuid) implements ServiceIdentifier {
|
||||||
private static final String SERVICE_ID_STRING_COUNTER_NAME = MetricsUtil.name(AciServiceIdentifier.class, "serviceIdString");
|
|
||||||
|
|
||||||
private static final IdentityType IDENTITY_TYPE = IdentityType.ACI;
|
private static final IdentityType IDENTITY_TYPE = IdentityType.ACI;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -61,10 +59,7 @@ public record AciServiceIdentifier(UUID uuid) implements ServiceIdentifier {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static AciServiceIdentifier valueOf(final String string) {
|
public static AciServiceIdentifier valueOf(final String string) {
|
||||||
final boolean valid = !string.startsWith(IDENTITY_TYPE.getStringPrefix());
|
return new AciServiceIdentifier(UUID.fromString(string));
|
||||||
final UUID uuid = UUID.fromString(valid ? string : string.substring(IDENTITY_TYPE.getStringPrefix().length()));
|
|
||||||
Metrics.counter(SERVICE_ID_STRING_COUNTER_NAME, "valid", String.valueOf(valid)).increment();
|
|
||||||
return new AciServiceIdentifier(uuid);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static AciServiceIdentifier fromBytes(final byte[] bytes) {
|
public static AciServiceIdentifier fromBytes(final byte[] bytes) {
|
||||||
|
|
|
@ -56,6 +56,12 @@ public sealed interface ServiceIdentifier permits AciServiceIdentifier, PniServi
|
||||||
*/
|
*/
|
||||||
byte[] toFixedWidthByteArray();
|
byte[] toFixedWidthByteArray();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parse a service identifier string, which should be a plain UUID string for ACIs and a prefixed UUID string for PNIs
|
||||||
|
*
|
||||||
|
* @param string A service identifier string
|
||||||
|
* @return The parsed {@link ServiceIdentifier}
|
||||||
|
*/
|
||||||
static ServiceIdentifier valueOf(final String string) {
|
static ServiceIdentifier valueOf(final String string) {
|
||||||
try {
|
try {
|
||||||
return AciServiceIdentifier.valueOf(string);
|
return AciServiceIdentifier.valueOf(string);
|
||||||
|
|
|
@ -53,11 +53,12 @@ class AciServiceIdentifierTest {
|
||||||
final UUID uuid = UUID.randomUUID();
|
final UUID uuid = UUID.randomUUID();
|
||||||
|
|
||||||
assertEquals(uuid, AciServiceIdentifier.valueOf(uuid.toString()).uuid());
|
assertEquals(uuid, AciServiceIdentifier.valueOf(uuid.toString()).uuid());
|
||||||
assertEquals(uuid, AciServiceIdentifier.valueOf("ACI:" + uuid).uuid());
|
|
||||||
assertThrows(IllegalArgumentException.class, () -> AciServiceIdentifier.valueOf("Not a valid UUID"));
|
assertThrows(IllegalArgumentException.class, () -> AciServiceIdentifier.valueOf("Not a valid UUID"));
|
||||||
assertThrows(IllegalArgumentException.class, () -> AciServiceIdentifier.valueOf("PNI:" + uuid));
|
assertThrows(IllegalArgumentException.class, () -> AciServiceIdentifier.valueOf("PNI:" + uuid));
|
||||||
|
assertThrows(IllegalArgumentException.class, () -> AciServiceIdentifier.valueOf("ACI:" + uuid).uuid());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void fromBytes() {
|
void fromBytes() {
|
||||||
final UUID uuid = UUID.randomUUID();
|
final UUID uuid = UUID.randomUUID();
|
||||||
|
|
|
@ -32,12 +32,11 @@ class ServiceIdentifierTest {
|
||||||
|
|
||||||
return Stream.of(
|
return Stream.of(
|
||||||
Arguments.of(uuid.toString(), IdentityType.ACI, uuid),
|
Arguments.of(uuid.toString(), IdentityType.ACI, uuid),
|
||||||
Arguments.of("ACI:" + uuid, IdentityType.ACI, uuid),
|
|
||||||
Arguments.of("PNI:" + uuid, IdentityType.PNI, uuid));
|
Arguments.of("PNI:" + uuid, IdentityType.PNI, uuid));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ParameterizedTest
|
@ParameterizedTest
|
||||||
@ValueSource(strings = {"Not a valid UUID", "BAD:a9edc243-3e93-45d4-95c6-e3a84cd4a254"})
|
@ValueSource(strings = {"Not a valid UUID", "BAD:a9edc243-3e93-45d4-95c6-e3a84cd4a254", "ACI:a9edc243-3e93-45d4-95c6-e3a84cd4a254"})
|
||||||
void valueOfIllegalArgument(final String identifierString) {
|
void valueOfIllegalArgument(final String identifierString) {
|
||||||
assertThrows(IllegalArgumentException.class, () -> ServiceIdentifier.valueOf(identifierString));
|
assertThrows(IllegalArgumentException.class, () -> ServiceIdentifier.valueOf(identifierString));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue