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 Apr 4, 2024
1 parent b0d28ff commit 4a1b0d0
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,26 @@ public static LegReference decode(String legReference) {
return null;
}

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

try (var in = new ObjectInputStream(input)) {
// The order must be the same in the encode and decode function

var type = readEnum(in, LegReferenceType.class);
return type.getDeserializer().read(in);
} catch (IOException e) {
LOG.error("Unable to decode leg reference: '" + legReference + "'", e);
LOG.info(
"Unable to decode leg reference (incompatible serialization format): '{}'",
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 4a1b0d0

Please sign in to comment.