diff --git a/src/main/java/io/github/hectorvent/floci/core/common/CidrCanonicalizer.java b/src/main/java/io/github/hectorvent/floci/core/common/CidrCanonicalizer.java new file mode 100644 index 0000000000..da07c17f37 --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/core/common/CidrCanonicalizer.java @@ -0,0 +1,204 @@ +package io.github.hectorvent.floci.core.common; + +import java.net.Inet4Address; +import java.net.Inet6Address; +import java.net.InetAddress; +import java.util.Optional; + +/** + * Canonicalises IPv4 and IPv6 CIDR blocks the way AWS does on write: the address is + * rewritten so every bit outside the prefix is zero, then re-rendered in AWS's own + * textual form (dotted-decimal for IPv4, compressed form for IPv6). + * + *
{@code 100.68.0.18/18} becomes {@code 100.68.0.0/18} — AWS's own documented + * example — because bits 18-31 of the address are host bits and AWS zeroes them + * before storing the block. A CIDR that is already canonical is returned unchanged + * (the identical string, not merely an equal one). + * + *
This class draws a hard line between "malformed" and "valid but non-canonical": + * {@link #canonicalize(String)} returns {@link Optional#empty()} only for a CIDR that + * cannot be parsed at all, never as a stand-in for "already canonical". Callers that + * need to tell the two apart before rewriting anything can call + * {@link #isCanonical(String)} directly. + * + *
Pure function: no I/O, no DNS resolution. The address half of the CIDR is parsed
+ * as a literal via {@link InetAddress#ofLiteral(String)}, which never triggers a
+ * hostname lookup — critical here, because {@link InetAddress#getByName(String)} would
+ * silently turn a malformed CIDR into a DNS query.
+ */
+public final class CidrCanonicalizer {
+
+ private CidrCanonicalizer() {}
+
+ /**
+ * Parses {@code cidr}, zeroes its host bits, and re-renders it in canonical form.
+ *
+ * @param cidr an IPv4 or IPv6 CIDR block, e.g. {@code "100.68.0.18/18"} or
+ * {@code "2001:db8::1/32"}
+ * @return the canonical CIDR string, or {@link Optional#empty()} if {@code cidr} is
+ * null, blank, missing a prefix, has a prefix out of range for the address
+ * family (0-32 for IPv4, 0-128 for IPv6), or has an address half that is not a
+ * literal IPv4/IPv6 address
+ */
+ public static Optional {@link InetAddress#getHostAddress()} renders IPv6 addresses in fully expanded
+ * form ({@code 2001:db8:0:0:0:0:0:0}, not {@code 2001:db8::}), so the compression
+ * step is done here explicitly rather than delegated.
+ */
+ private static String hostAddress(InetAddress address) {
+ if (address instanceof Inet6Address v6) {
+ return compressIpv6(v6.getAddress());
+ }
+ return address.getHostAddress();
+ }
+
+ /**
+ * Renders 16 raw IPv6 address bytes as RFC 5952 compressed text: the longest run
+ * of two or more consecutive all-zero 16-bit groups is collapsed to {@code ::}
+ * (the leftmost run wins on a tie), and remaining groups are lowercase hex with no
+ * leading zeros.
+ */
+ private static String compressIpv6(byte[] bytes) {
+ int[] groups = new int[8];
+ for (int i = 0; i < 8; i++) {
+ groups[i] = ((bytes[i * 2] & 0xFF) << 8) | (bytes[i * 2 + 1] & 0xFF);
+ }
+
+ int bestStart = -1;
+ int bestLength = 0;
+ int runStart = -1;
+ for (int i = 0; i <= 8; i++) {
+ boolean zero = i < 8 && groups[i] == 0;
+ if (zero) {
+ if (runStart < 0) {
+ runStart = i;
+ }
+ } else if (runStart >= 0) {
+ int runLength = i - runStart;
+ if (runLength > bestLength) {
+ bestStart = runStart;
+ bestLength = runLength;
+ }
+ runStart = -1;
+ }
+ }
+ // A run must be at least two groups to be worth eliding (RFC 5952 §4.2.2).
+ if (bestLength < 2) {
+ bestStart = -1;
+ }
+
+ StringBuilder sb = new StringBuilder();
+ for (int i = 0; i < 8; ) {
+ if (i == bestStart) {
+ sb.append("::");
+ i += bestLength;
+ continue;
+ }
+ if (sb.length() > 0 && sb.charAt(sb.length() - 1) != ':') {
+ sb.append(':');
+ }
+ sb.append(Integer.toHexString(groups[i]));
+ i++;
+ }
+ // An address with no elided run at all (e.g. all groups non-zero) never hits
+ // the "::" branch, so nothing above needs a leading-colon special case; a run
+ // starting at index 0 naturally produces the correct leading "::".
+ return sb.toString();
+ }
+
+ private static byte[] maskHostBits(byte[] addressBytes, int prefixLength) {
+ byte[] masked = addressBytes.clone();
+ int fullBytes = prefixLength / 8;
+ int remainderBits = prefixLength % 8;
+
+ if (remainderBits != 0 && fullBytes < masked.length) {
+ int keepMask = 0xFF << (8 - remainderBits) & 0xFF;
+ masked[fullBytes] = (byte) (masked[fullBytes] & keepMask);
+ fullBytes++;
+ }
+ for (int i = fullBytes; i < masked.length; i++) {
+ masked[i] = 0;
+ }
+ return masked;
+ }
+
+ private record ParsedCidr(InetAddress address, int prefixLength) {}
+}
diff --git a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2Service.java b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2Service.java
index d5f3607148..4785961e1f 100644
--- a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2Service.java
+++ b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2Service.java
@@ -31,6 +31,7 @@ import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.core.common.AwsArnUtils;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.AwsRegions;
+import io.github.hectorvent.floci.core.common.CidrCanonicalizer;
import io.github.hectorvent.floci.core.common.ContainerTeardown;
import io.github.hectorvent.floci.core.resource.ExplorerResource;
import io.github.hectorvent.floci.core.resource.ResourceProvider;
@@ -4634,6 +4635,36 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
return isSet(destinationIpv6CidrBlock) ? destinationIpv6CidrBlock : destinationPrefixListId;
}
+ /**
+ * AWS canonicalizes an IPv4 destination CIDR on input: "We modify the specified CIDR block to
+ * its canonical form; for example, if you specify 100.68.0.18/18, we modify it to
+ * 100.68.0.0/18." Running every DestinationCidrBlock through this once, here, is what lets
+ * CreateRoute, ReplaceRoute and DeleteRoute agree on "same destination" — two spellings of the
+ * same network now collide as duplicates instead of coexisting as two routes with undefined
+ * ReplaceRoute/DeleteRoute behaviour — and it is also why DescribeRouteTables echoes back the
+ * canonical form: the stored Route never holds anything else.
+ *
+ * DestinationIpv6CidrBlock has no equivalent sentence in the CreateRoute/ReplaceRoute model
+ * and is left untouched. DestinationPrefixListId is an opaque ID, not a CIDR, and is likewise
+ * untouched.
+ *
+ * A block that is not a well-formed "IPv4/prefix" is returned unchanged: canonicalizing is
+ * not this method's job to validate the request, only to reduce what is already well-formed.
+ * The unmodified value fails downstream exactly as it did before this change.
+ *
+ * Delegates the actual bit-twiddling to {@link CidrCanonicalizer}, which also understands
+ * IPv6. That is deliberately not used here: DestinationCidrBlock is AWS's IPv4-only field —
+ * the API reference's canonicalization sentence appears only under it, never under
+ * DestinationIpv6CidrBlock — so a value that parses as an IPv6 literal is left untouched
+ * rather than canonicalized, the same as any other malformed-for-this-field input.
+ */
+ private static String canonicalizeIpv4Cidr(String destinationCidrBlock) {
+ if (!isSet(destinationCidrBlock) || destinationCidrBlock.contains(":")) {
+ return destinationCidrBlock;
+ }
+ return CidrCanonicalizer.canonicalize(destinationCidrBlock).orElse(destinationCidrBlock);
+ }
+
/**
* AWS takes one destination per route, and there are three kinds of it: DestinationCidrBlock,
* DestinationIpv6CidrBlock and DestinationPrefixListId. The CreateRoute reference is explicit
@@ -4690,6 +4721,7 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
requireExactlyOneDestination("CreateRoute", destinationCidrBlock, destinationIpv6CidrBlock,
destinationPrefixListId);
requireEgressOnlyGatewayIsTheOnlyTarget(gatewayId, natGatewayId, egressOnlyInternetGatewayId);
+ final String canonicalDestinationCidrBlock = canonicalizeIpv4Cidr(destinationCidrBlock);
ensureDefaultResources(region);
synchronized (lockFor(key(region, routeTableId))) {
RouteTable current = getRequiredRouteTable(region, routeTableId);
@@ -4698,15 +4730,15 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
// removes every copy, so a table holding two routes with the same destination has no
// well-defined behaviour for either. AWS rejects the second CreateRoute instead, and it
// makes no exception for the local route seeded by CreateRouteTable.
- if (next.stream().anyMatch(r -> matchesDestination(r, destinationCidrBlock,
+ if (next.stream().anyMatch(r -> matchesDestination(r, canonicalDestinationCidrBlock,
destinationIpv6CidrBlock, destinationPrefixListId))) {
throw new AwsException("RouteAlreadyExists",
"The route identified by "
- + destinationLabel(destinationCidrBlock, destinationIpv6CidrBlock,
+ + destinationLabel(canonicalDestinationCidrBlock, destinationIpv6CidrBlock,
destinationPrefixListId)
+ " already exists", 400);
}
- Route route = new Route(destinationCidrBlock, gatewayId, "CreateRoute");
+ Route route = new Route(canonicalDestinationCidrBlock, gatewayId, "CreateRoute");
route.setDestinationIpv6CidrBlock(destinationIpv6CidrBlock);
route.setDestinationPrefixListId(destinationPrefixListId);
route.setNatGatewayId(natGatewayId);
@@ -4731,24 +4763,25 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
throw new AwsException("InvalidParameterCombination",
"ReplaceRoute takes exactly one target, and only GatewayId or NatGatewayId is supported.", 400);
}
+ final String canonicalDestinationCidrBlock = canonicalizeIpv4Cidr(destinationCidrBlock);
ensureDefaultResources(region);
synchronized (lockFor(key(region, routeTableId))) {
RouteTable current = getRequiredRouteTable(region, routeTableId);
List Scoped to IPv4 only, matching the API-boundary decision: a value containing {@code ':'}
+ * (or anything else {@link CidrCanonicalizer} cannot parse) is left untouched, since
+ * DestinationCidrBlock is documented by AWS as an IPv4-only field and
+ * DestinationIpv6CidrBlock is a separate member with its own field/setter.
+ */
+ public void setDestinationCidrBlock(String destinationCidrBlock) {
+ if (destinationCidrBlock == null || destinationCidrBlock.isBlank() || destinationCidrBlock.contains(":")) {
+ this.destinationCidrBlock = destinationCidrBlock;
+ return;
+ }
+ this.destinationCidrBlock = CidrCanonicalizer.canonicalize(destinationCidrBlock).orElse(destinationCidrBlock);
+ }
public String getDestinationIpv6CidrBlock() { return destinationIpv6CidrBlock; }
public void setDestinationIpv6CidrBlock(String destinationIpv6CidrBlock) { this.destinationIpv6CidrBlock = destinationIpv6CidrBlock; }
diff --git a/src/test/java/io/github/hectorvent/floci/core/common/CidrCanonicalizerTest.java b/src/test/java/io/github/hectorvent/floci/core/common/CidrCanonicalizerTest.java
new file mode 100644
index 0000000000..ca69d18b71
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/core/common/CidrCanonicalizerTest.java
@@ -0,0 +1,164 @@
+package io.github.hectorvent.floci.core.common;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertSame;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.util.Optional;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.CsvSource;
+import org.junit.jupiter.params.provider.ValueSource;
+
+class CidrCanonicalizerTest {
+
+ // -----------------------------------------------------------------
+ // Pinned cases from the bead — exact strings, not just equality.
+ // -----------------------------------------------------------------
+
+ @Test
+ void awsDocumentedExample_rewritesHostBitsToZero() {
+ assertEquals(Optional.of("100.68.0.0/18"), CidrCanonicalizer.canonicalize("100.68.0.18/18"));
+ }
+
+ @Test
+ void alreadyCanonicalIpv4_isIdentityNotJustEqual() {
+ String input = "10.0.0.0/16";
+ Optional Canonicalization is IPv4-only. DestinationIpv6CidrBlock has no equivalent sentence in the
+ * model and DestinationPrefixListId is an opaque ID, not a CIDR, so neither is touched — the last
+ * test here pins that an IPv6 destination survives byte-for-byte.
+ *
+ * The XPath assertions use {@code local-name()} because EC2 responses carry a default
+ * namespace.
+ */
+@QuarkusTest
+@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
+class Ec2RouteCidrCanonicalizationTest {
+
+ private static final String AUTH_HEADER =
+ "AWS4-HMAC-SHA256 Credential=test/20260205/us-east-1/ec2/aws4_request";
+
+ private static final String VPC_CIDR = "203.0.113.0/24";
+ private static final String NON_CANONICAL = "100.68.0.18/18";
+ private static final String CANONICAL = "100.68.0.0/18";
+ private static final String OTHER_SPELLING_SAME_NETWORK = "100.68.63.255/18";
+ private static final String IPV6_ROUTE = "2001:db8::/32";
+ private static final String INTERNET_GATEWAY = "igw-0cidrcanon0test00";
+ private static final String OTHER_GATEWAY = "igw-0cidrcanon0othr00";
+
+ private static final String ROUTE_COUNT =
+ "count(//*[local-name()='routeSet']/*[local-name()='item'])";
+
+ private static String routeTableId;
+
+ private static io.restassured.specification.RequestSpecification ec2() {
+ return given().header("Authorization", AUTH_HEADER);
+ }
+
+ private static io.restassured.response.ValidatableResponse describe() {
+ return ec2()
+ .formParam("Action", "DescribeRouteTables")
+ .formParam("RouteTableId.1", routeTableId)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200);
+ }
+
+ private static io.restassured.specification.RequestSpecification createRoute(
+ String destinationCidrBlock, String gatewayId) {
+ return ec2()
+ .formParam("Action", "CreateRoute")
+ .formParam("RouteTableId", routeTableId)
+ .formParam("DestinationCidrBlock", destinationCidrBlock)
+ .formParam("GatewayId", gatewayId);
+ }
+
+ @Test
+ @Order(1)
+ void createARouteTable() {
+ String vpcId = ec2()
+ .formParam("Action", "CreateVpc")
+ .formParam("CidrBlock", VPC_CIDR)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200)
+ .extract().path("CreateVpcResponse.vpc.vpcId");
+
+ routeTableId = ec2()
+ .formParam("Action", "CreateRouteTable")
+ .formParam("VpcId", vpcId)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200)
+ .extract().path("CreateRouteTableResponse.routeTable.routeTableId");
+ }
+
+ /** CreateRoute stores and echoes the canonical form, not the spelling the caller sent. */
+ @Test
+ @Order(2)
+ void createRouteCanonicalizesTheDestination() {
+ createRoute(NON_CANONICAL, INTERNET_GATEWAY).when().post("/").then().statusCode(200);
+
+ describe()
+ .body(hasXPath(ROUTE_COUNT, equalTo("2"))) // the seeded local route, plus this one
+ .body(hasXPath("//*[local-name()='routeSet']/*[local-name()='item']"
+ + "[*[local-name()='destinationCidrBlock']='" + CANONICAL + "']"))
+ .body(hasXPath("count(//*[local-name()='routeSet']/*[local-name()='item']"
+ + "[*[local-name()='destinationCidrBlock']='" + NON_CANONICAL + "'])", equalTo("0")));
+ }
+
+ /**
+ * A second CreateRoute spelling the same /18 network differently is the same destination as
+ * far as AWS is concerned, so it collides with RouteAlreadyExists exactly like a literal
+ * repeat would (see {@link Ec2DuplicateRouteTest}). Without canonicalizing on input, this
+ * would have silently created a second, functionally ambiguous route instead.
+ */
+ @Test
+ @Order(3)
+ void anEquivalentSpellingOfTheSameNetworkCollidesAsADuplicate() {
+ String message = createRoute(OTHER_SPELLING_SAME_NETWORK, INTERNET_GATEWAY)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(400)
+ .body("Response.Errors.Error.Code", equalTo("RouteAlreadyExists"))
+ .extract().path("Response.Errors.Error.Message");
+
+ // The error names the canonical destination, since that is what is actually on file.
+ assertThat(message, containsString(CANONICAL));
+
+ describe().body(hasXPath(ROUTE_COUNT, equalTo("2")));
+ }
+
+ /**
+ * ReplaceRoute must canonicalize its own DestinationCidrBlock before matching, or an
+ * equivalent-but-differently-spelled request would miss the stored canonical route entirely
+ * and fail with InvalidRoute.NotFound instead of retargeting it.
+ */
+ @Test
+ @Order(4)
+ void replaceRouteMatchesTheCanonicalRouteByAnEquivalentSpelling() {
+ ec2()
+ .formParam("Action", "ReplaceRoute")
+ .formParam("RouteTableId", routeTableId)
+ .formParam("DestinationCidrBlock", OTHER_SPELLING_SAME_NETWORK)
+ .formParam("GatewayId", OTHER_GATEWAY)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200);
+
+ describe()
+ .body(hasXPath(ROUTE_COUNT, equalTo("2")))
+ .body("DescribeRouteTablesResponse.routeTableSet.item.routeSet.item"
+ + ".find { it.destinationCidrBlock == '" + CANONICAL + "' }.gatewayId",
+ equalTo(OTHER_GATEWAY));
+ }
+
+ /**
+ * DeleteRoute must canonicalize the same way, or an equivalent spelling would silently match
+ * nothing and leave the route in place while reporting success.
+ */
+ @Test
+ @Order(5)
+ void deleteRouteMatchesTheCanonicalRouteByAnEquivalentSpelling() {
+ ec2()
+ .formParam("Action", "DeleteRoute")
+ .formParam("RouteTableId", routeTableId)
+ .formParam("DestinationCidrBlock", NON_CANONICAL)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200);
+
+ describe().body(hasXPath(ROUTE_COUNT, equalTo("1"))); // only the seeded local route remains
+ }
+
+ /** DestinationIpv6CidrBlock has no canonical form in the model and is stored byte-for-byte. */
+ @Test
+ @Order(6)
+ void ipv6DestinationsAreNotCanonicalized() {
+ ec2()
+ .formParam("Action", "CreateRoute")
+ .formParam("RouteTableId", routeTableId)
+ .formParam("DestinationIpv6CidrBlock", IPV6_ROUTE)
+ .formParam("GatewayId", INTERNET_GATEWAY)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200);
+
+ describe().body(hasXPath("//*[local-name()='routeSet']/*[local-name()='item']"
+ + "[*[local-name()='destinationIpv6CidrBlock']='" + IPV6_ROUTE + "']"));
+ }
+}
diff --git a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ServicePersistenceTest.java b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ServicePersistenceTest.java
index 89138afd36..18810e530b 100644
--- a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ServicePersistenceTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ServicePersistenceTest.java
@@ -2,6 +2,7 @@ package io.github.hectorvent.floci.services.ec2;
import com.fasterxml.jackson.core.type.TypeReference;
import io.github.hectorvent.floci.config.EmulatorConfig;
+import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.storage.PersistentStorage;
import io.github.hectorvent.floci.core.storage.StorageBackend;
import io.github.hectorvent.floci.services.ec2.model.Address;
@@ -32,11 +33,14 @@ import io.github.hectorvent.floci.services.ec2.portforward.Ec2PortForwardManager
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
+import java.io.IOException;
+import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -161,6 +165,85 @@ class Ec2ServicePersistenceTest {
assertEquals("from-source-sg", ingress.getFirst().getDescription());
}
+ /**
+ * Regression test for the RouteAlreadyExists/InvalidRoute.NotFound gap Greptile flagged on
+ * the DestinationCidrBlock canonicalization fix: a route persisted BEFORE that fix stores its
+ * destination in whatever spelling CreateRoute was originally called with (here
+ * {@code 100.68.0.18/18} rather than the canonical {@code 100.68.0.0/18}). Without
+ * canonicalizing on load, that legacy spelling never matches a newly-canonicalized incoming
+ * destination under strict string comparison, so CreateRoute could create a duplicate,
+ * ReplaceRoute could wrongly 404, and DeleteRoute could silently no-op.
+ *
+ * This hand-writes a pre-fix-shaped {@code ec2-route-tables.json} directly (bypassing
+ * {@link io.github.hectorvent.floci.services.ec2.model.Route}'s own setter, which now
+ * canonicalizes) to simulate a file written by a pre-canonicalization Floci, then restarts
+ * over it and asserts: (a) the reloaded route reads back canonical; (b) CreateRoute with an
+ * equivalent-but-differently-spelled destination is detected as a duplicate rather than
+ * creating a second route; (c) ReplaceRoute and DeleteRoute using the legacy, non-canonical
+ * spelling still find the same route. Reverting the {@code Route.setDestinationCidrBlock}
+ * change alone fails this test: (a) reads back {@code 100.68.0.18/18} instead of
+ * {@code 100.68.0.0/18}, and (b)/(c) then also fail because CreateRoute/ReplaceRoute/
+ * DeleteRoute never see the same string as the stored legacy route.
+ */
+ @Test
+ void legacyNonCanonicalRouteDestinationCanonicalizesOnRestart(@TempDir Path dir) throws IOException {
+ String routeTableId = "rtb-legacycidr01";
+ String vpcId = "vpc-legacycidr01";
+ String gatewayId = "igw-legacycidr01";
+ String legacyJson = """
+ {
+ "%s::%s": {
+ "routeTableId": "%s",
+ "vpcId": "%s",
+ "ownerId": "000000000000",
+ "region": "%s",
+ "routes": [
+ {
+ "destinationCidrBlock": "100.68.0.18/18",
+ "gatewayId": "%s",
+ "state": "active",
+ "origin": "CreateRoute"
+ }
+ ],
+ "associations": [],
+ "tags": []
+ }
+ }
+ """.formatted(REGION, routeTableId, routeTableId, vpcId, REGION, gatewayId);
+ Files.writeString(dir.resolve("ec2-route-tables.json"), legacyJson);
+
+ Ec2Service restarted = newService(dir);
+
+ // (a) the loaded route's destination reads back canonical.
+ RouteTable loaded =
+ restarted.describeRouteTables(REGION, List.of(routeTableId), Map.of()).getFirst();
+ assertEquals(1, loaded.getRoutes().size());
+ assertEquals("100.68.0.0/18", loaded.getRoutes().getFirst().getDestinationCidrBlock(),
+ "legacy route destination must be canonicalized on load");
+
+ // (b) CreateRoute with an equivalent-but-differently-spelled destination is a duplicate,
+ // not a new route, of the legacy one.
+ AwsException duplicate = assertThrows(AwsException.class, () -> restarted.createRoute(
+ REGION, routeTableId, "100.68.63.255/18", null, null, "igw-other00000000000", null, null));
+ assertEquals("RouteAlreadyExists", duplicate.getErrorCode());
+
+ // (c) ReplaceRoute against the legacy route's original, non-canonical spelling finds the
+ // same route rather than throwing InvalidRoute.NotFound.
+ restarted.replaceRoute(REGION, routeTableId, "100.68.0.18/18", null, null, "igw-replaced0000000", null);
+ RouteTable afterReplace =
+ restarted.describeRouteTables(REGION, List.of(routeTableId), Map.of()).getFirst();
+ assertEquals(1, afterReplace.getRoutes().size(), "replace must update the existing route in place");
+ assertEquals("igw-replaced0000000", afterReplace.getRoutes().getFirst().getGatewayId());
+ assertEquals("100.68.0.0/18", afterReplace.getRoutes().getFirst().getDestinationCidrBlock());
+
+ // (c) DeleteRoute against the same legacy spelling actually removes the route rather than
+ // reporting success while leaving it behind.
+ restarted.deleteRoute(REGION, routeTableId, "100.68.0.18/18", null, null);
+ RouteTable afterDelete =
+ restarted.describeRouteTables(REGION, List.of(routeTableId), Map.of()).getFirst();
+ assertEquals(0, afterDelete.getRoutes().size(), "delete must actually remove the legacy route");
+ }
+
private BlockDeviceMapping blockDeviceMapping(String snapshotId, int volumeSize) {
EbsBlockDevice ebs = new EbsBlockDevice();
ebs.setSnapshotId(snapshotId);