mirror of
https://github.com/signalapp/libsignal.git
synced 2024-09-20 03:52:17 +02:00
java: Prefer checked exceptions for ServiceId parsing methods
This commit is contained in:
parent
761c8080ee
commit
2938b65ffa
@ -8,6 +8,8 @@ package org.signal.libsignal.protocol;
|
||||
import java.util.UUID;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.signal.libsignal.protocol.ServiceId.InvalidServiceIdException;
|
||||
|
||||
import org.signal.libsignal.protocol.util.Hex;
|
||||
|
||||
public class ServiceIdTest extends TestCase {
|
||||
@ -45,7 +47,7 @@ public class ServiceIdTest extends TestCase {
|
||||
try {
|
||||
ServiceId.parseFromString(String.format("ACI:%s", TEST_UUID_STRING));
|
||||
fail("Successfully parsed an invalid Service-Id-String");
|
||||
} catch (IllegalArgumentException ex) {
|
||||
} catch (InvalidServiceIdException ex) {
|
||||
}
|
||||
}
|
||||
|
||||
@ -58,7 +60,7 @@ public class ServiceIdTest extends TestCase {
|
||||
try {
|
||||
ServiceId.parseFromBinary(invalidAciBytes);
|
||||
fail("Successfully parsed in invalid Service-Id-Binary");
|
||||
} catch (IllegalArgumentException ex) {
|
||||
} catch (InvalidServiceIdException ex) {
|
||||
}
|
||||
}
|
||||
|
||||
@ -74,11 +76,11 @@ public class ServiceIdTest extends TestCase {
|
||||
try {
|
||||
ServiceId.parseFromString((String)null);
|
||||
fail("Should have failed");
|
||||
} catch (IllegalArgumentException ex){}
|
||||
} catch (InvalidServiceIdException ex){}
|
||||
try {
|
||||
ServiceId.parseFromBinary((byte[])null);
|
||||
fail("Should have failed");
|
||||
} catch (IllegalArgumentException ex){}
|
||||
} catch (InvalidServiceIdException ex){}
|
||||
}
|
||||
|
||||
public void testInvalidServiceId() throws Exception {
|
||||
@ -86,11 +88,11 @@ public class ServiceIdTest extends TestCase {
|
||||
byte[] invalidServiceIdBytes = Hex.fromStringCondensedAssert("02" + TEST_UUID_HEX);
|
||||
ServiceId.parseFromBinary(invalidServiceIdBytes);
|
||||
fail("Should have failed");
|
||||
} catch(IllegalArgumentException ex) {}
|
||||
} catch(InvalidServiceIdException ex) {}
|
||||
try {
|
||||
String invalidServiceString = "SGL:" + TEST_UUID_STRING;
|
||||
ServiceId.parseFromString(invalidServiceString);
|
||||
fail("Should have failed");
|
||||
} catch(IllegalArgumentException ex) {}
|
||||
} catch(InvalidServiceIdException ex) {}
|
||||
}
|
||||
}
|
||||
|
@ -78,23 +78,35 @@ public abstract class ServiceId {
|
||||
|
||||
public static ServiceId parseFromString(String serviceIdString) throws InvalidServiceIdException {
|
||||
if (serviceIdString == null) {
|
||||
throw new IllegalArgumentException("Service-Id-String cannot be null");
|
||||
throw new InvalidServiceIdException("Service-Id-String cannot be null");
|
||||
}
|
||||
byte[] storage;
|
||||
try {
|
||||
storage = Native.ServiceId_ParseFromServiceIdString(serviceIdString);
|
||||
}
|
||||
catch (IllegalArgumentException ex) {
|
||||
throw new InvalidServiceIdException();
|
||||
}
|
||||
byte[] storage = Native.ServiceId_ParseFromServiceIdString(serviceIdString);
|
||||
return parseFromFixedWidthBinary(storage);
|
||||
}
|
||||
|
||||
public static ServiceId parseFromBinary(byte[] serviceIdBinary) throws InvalidServiceIdException {
|
||||
if (serviceIdBinary == null) {
|
||||
throw new IllegalArgumentException("Service-Id-Binary cannot be null");
|
||||
throw new InvalidServiceIdException("Service-Id-Binary cannot be null");
|
||||
}
|
||||
byte[] storage;
|
||||
try {
|
||||
storage = Native.ServiceId_ParseFromServiceIdBinary(serviceIdBinary);
|
||||
}
|
||||
catch (IllegalArgumentException ex) {
|
||||
throw new InvalidServiceIdException();
|
||||
}
|
||||
byte[] storage = Native.ServiceId_ParseFromServiceIdBinary(serviceIdBinary);
|
||||
return parseFromFixedWidthBinary(storage);
|
||||
}
|
||||
|
||||
public static ServiceId parseFromFixedWidthBinary(byte[] storage) throws InvalidServiceIdException {
|
||||
if (storage == null) {
|
||||
throw new IllegalArgumentException();
|
||||
throw new InvalidServiceIdException();
|
||||
}
|
||||
switch(storage[0]) {
|
||||
case ACI_MARKER:
|
||||
@ -113,7 +125,10 @@ public abstract class ServiceId {
|
||||
return new UUID(high, low);
|
||||
}
|
||||
|
||||
public static class InvalidServiceIdException extends Exception {}
|
||||
public static class InvalidServiceIdException extends Exception {
|
||||
public InvalidServiceIdException() { super(); }
|
||||
public InvalidServiceIdException(String message) { super(message); }
|
||||
}
|
||||
|
||||
public final static class Aci extends ServiceId {
|
||||
public Aci(UUID uuid) {
|
||||
|
Loading…
Reference in New Issue
Block a user