Skip to content

Commit

Permalink
Improve handling of invalid leg reference
Browse files Browse the repository at this point in the history
  • Loading branch information
vpaturet committed Mar 21, 2024
1 parent 2b7a431 commit 7fc7687
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,13 @@ public static LegReference decode(String legReference) {
return null;
}

var buf = Base64.getUrlDecoder().decode(legReference);
byte[] buf;
try {
buf = Base64.getUrlDecoder().decode(legReference);
} catch (IllegalArgumentException e) {
LOG.warn("Unable to decode leg reference (invalid base64 encoding): '{}'", legReference, e);
return null;
}
var input = new ByteArrayInputStream(buf);

try (var in = new ObjectInputStream(input)) {
Expand All @@ -60,7 +66,7 @@ public static LegReference decode(String legReference) {
var type = readEnum(in, LegReferenceType.class);
return type.getDeserializer().read(in);
} catch (IOException e) {
LOG.error("Unable to decode leg reference: '" + legReference + "'", e);
LOG.warn("Unable to decode leg reference: '{}'", legReference, e);
return null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;

import java.time.LocalDate;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -86,4 +87,19 @@ void testScheduledTransitLegReferenceLegacyV2Deserialize() {
assertEquals(FROM_STOP_POS, ref.fromStopPositionInPattern());
assertEquals(TO_STOP_POS, ref.toStopPositionInPattern());
}

@Test
void testNullSerializedLegReference() {
assertNull(LegReferenceSerializer.decode(null));
}

@Test
void testEmptySerializedLegReference() {
assertNull(LegReferenceSerializer.decode(""));
}

@Test
void testIllegalBase64CharacterInSerializedLegReference() {
assertNull(LegReferenceSerializer.decode("RUT:Line:5"));
}
}

0 comments on commit 7fc7687

Please sign in to comment.