Light, fluffy, and always free - The AWS Local Emulator alternative
fix(rekognition): refuse the images Rekognition refuses (#5540)
Jayc65b6e96fc7c
diff --git a/docs/services/rekognition.md b/docs/services/rekognition.mdindex 047e80af9b..5146a43703 100644--- a/docs/services/rekognition.md+++ b/docs/services/rekognition.md@@ -17,10 +17,34 @@## Emulation Behavior-- **Stub responses:** every action returns a fixed, AWS-shaped response — no real- image analysis is performed. `Image`/`SourceImage`/`TargetImage` content is- accepted but never decoded, the same way Textract accepts `Document` without- reading it.+- **Stub responses:** every action returns a fixed, AWS-shaped response. No image+ analysis is performed: what an image shows never changes the result.+- **Images are checked as Rekognition checks them:** an image Rekognition refuses is+ refused here, with Rekognition's error, before any response. Measured against+ Rekognition in us-east-2, for each operation and for both `Bytes` and `S3Object`:+ - Anything but a JPEG or PNG (WebP, GIF, other bytes) is refused with+ `InvalidImageFormatException`, and so is a cut-off or hollow file the way Rekognition+ judges it: a PNG without an `IDAT` or `IEND` chunk, or a JPEG cut inside its headers or+ whose frame or scan header is shorter than its component count requires. A JPEG whose+ headers are complete through the start-of-scan header is accepted however much of the+ image data after it is missing.+ - A JPEG with stray bytes between its segments is accepted, as Rekognition accepts it.+ - `Bytes` that are not base64 are refused with `SerializationException`; base64+ without its `=` padding is accepted.+ - `Bytes` that are empty or longer than 5,242,880 bytes are refused with+ `ValidationException`, naming the member (`image.bytes`, `sourceImage.bytes`,+ `targetImage.bytes`). For `CompareFaces` both images' lengths are checked before+ either image is read, and every violation is reported in one error.+ - An `S3Object` that cannot be read (a missing bucket or object, or a bucket in+ another region than the request) is refused with `InvalidS3ObjectException`. A bucket+ created without a location constraint is in the default region. The object is read+ from Floci's S3, so it has to be uploaded first; only as much of it as its format+ needs is read, so a large object is not loaded into memory.+ - A side over 10,000 pixels is refused with `ImageTooLargeException` by+ `DetectModerationLabels` and `DetectLabels`, and with+ `InvalidImageFormatException` by the other operations.+ - Two documented limits are not enforced, because Rekognition does not enforce them:+ images under 80 pixels are accepted, and an S3 object over 15 MB is accepted.- **Content-listing operations return one canned entry:** `DetectLabels` and`DetectText` always report a single stub label/text detection, matching theTextract stub's block-hierarchy convention.@@ -35,12 +59,9 @@`Bytes` (string-typed) or `S3Object` (structure-typed) — matching AWS's ownmodeling and rejecting wrong JSON types as `SerializationException`. This isprotocol compatibility, not content analysis.-- **Intentional deviations:** `Bytes`'s content is not validated as- well-formed base64 (the `S3Object` shape has no required members in AWS's- own model, so its fields aren't validated either), and supplying both- `Bytes` and `S3Object` is accepted rather than rejected — nothing in the- Rekognition API model declares that combination invalid, and the stub- never reads either field's content regardless.+- **Both `Bytes` and `S3Object`:** supplying both is accepted rather than rejected,+ since nothing in the Rekognition API model declares that combination invalid. The+ bytes are what is checked.- **Out of scope:** face-collection persistence (`CreateCollection`,`IndexFaces`, `SearchFaces`), custom-model training (`Projects`, `Datasets`,`ProjectVersions`), live video (`StreamProcessor`), async video jobs@@ -73,6 +94,10 @@ across Rekognition, Textract, and Comprehend:}```+The object named by the key has to exist in Floci's S3 and be an image Rekognition+accepts: the image is checked before the mock is looked up, so a call naming an object+that was never uploaded fails with `InvalidS3ObjectException`, as it does on AWS.+The lookup key is **`"<Bucket>/<Name>"`** from `Image.S3Object` (or `SourceImage.S3Object`for `CompareFaces` — `TargetImage` has no independent key in this scheme). A `Bytes`-backedimage has no such key, so mocking only applies to S3Object-based calls. The file is re-readdiff --git a/src/main/java/io/github/hectorvent/floci/services/rekognition/RekognitionImageValidator.java b/src/main/java/io/github/hectorvent/floci/services/rekognition/RekognitionImageValidator.javanew file mode 100644index 0000000000..72c6eb3a06--- /dev/null+++ b/src/main/java/io/github/hectorvent/floci/services/rekognition/RekognitionImageValidator.java@@ -0,0 +1,359 @@+package io.github.hectorvent.floci.services.rekognition;++import com.fasterxml.jackson.databind.JsonNode;+import io.github.hectorvent.floci.config.EmulatorConfig;+import io.github.hectorvent.floci.core.common.AwsException;+import io.github.hectorvent.floci.services.s3.S3Service;+import jakarta.enterprise.context.ApplicationScoped;+import jakarta.inject.Inject;+import org.jboss.logging.Logger;++import java.io.ByteArrayInputStream;+import java.io.EOFException;+import java.io.IOException;+import java.io.InputStream;+import java.io.UncheckedIOException;+import java.nio.charset.StandardCharsets;+import java.util.ArrayList;+import java.util.Base64;+import java.util.LinkedHashMap;+import java.util.List;+import java.util.Map;+import java.util.Set;++/**+ * Rejects the images Rekognition rejects, before any response is produced. Measured against+ * Rekognition in us-east-2 for every operation floci emulates, with each image sent as+ * {@code Bytes} and as an {@code S3Object}:+ *+ * <ul>+ * <li>{@code Bytes} that are not base64: {@code SerializationException}.</li>+ * <li>{@code Bytes} empty or longer than 5,242,880 bytes: {@code ValidationException}, naming every+ * such member of the request.</li>+ * <li>An {@code S3Object} that cannot be read, including one in a bucket of another region:+ * {@code InvalidS3ObjectException}.</li>+ * <li>Anything but a complete JPEG or PNG (WebP, GIF, other bytes, a truncated file):+ * {@code InvalidImageFormatException}.</li>+ * <li>A side over 10,000 pixels: {@code ImageTooLargeException} from {@code DetectModerationLabels}+ * and {@code DetectLabels}, {@code InvalidImageFormatException} from the others.</li>+ * </ul>+ *+ * <p>Two documented limits are not enforced because Rekognition does not enforce them: images+ * under 80 pixels were accepted by every operation, and a 17.8 MB S3 object was accepted despite+ * the documented 15 MB limit.+ *+ * <p>Formats are recognised from their structure, not decoded, matching where Rekognition draws+ * the line: a JPEG needs its headers complete through the start-of-scan header, and is accepted+ * however much of the scan after it is missing, even with no end-of-image marker; a PNG needs an+ * {@code IHDR} chunk first, an {@code IDAT} chunk, and an {@code IEND} chunk that its chunk+ * lengths reach.+ */+@ApplicationScoped+public class RekognitionImageValidator {++ private static final Logger LOG = Logger.getLogger(RekognitionImageValidator.class);++ static final int MAX_BYTES = 5_242_880;+ /** The longest base64 that can decode to MAX_BYTES or fewer; anything longer is over the limit. */+ static final int MAX_ENCODED_LENGTH = (MAX_BYTES + 2) / 3 * 4;+ static final int MAX_DIMENSION = 10_000;++ /** The operations whose oversized image fails with ImageTooLargeException rather than an invalid format. */+ private static final Set<String> TOO_LARGE_REPORTED = Set.of("DetectModerationLabels", "DetectLabels");++ private static final String INVALID_FORMAT = "Request has invalid image format";+ private static final String UNREADABLE_OBJECT =+ "Unable to get object metadata from S3. Check object key, region and/or access permissions.";++ private final S3Service s3Service;+ private final EmulatorConfig config;++ @Inject+ public RekognitionImageValidator(S3Service s3Service, EmulatorConfig config) {+ this.s3Service = s3Service;+ this.config = config;+ }++ /**+ * Validates the images in {@code request.<fields>}, each already known to carry {@code Bytes}+ * or an {@code S3Object}, in the order Rekognition does: every {@code Bytes} is decoded, then+ * their lengths are checked together, then each image is read and checked in turn. When an+ * image has both, its bytes are what is checked.+ */+ public void validate(String operation, JsonNode request, String region, String... fields) {+ Map<String, Decoded> bytes = new LinkedHashMap<>();+ for (String field : fields) {+ JsonNode image = request.get(field);+ if (image.hasNonNull("Bytes")) {+ bytes.put(field, decoded(image.get("Bytes").asText()));+ }+ }+ requireLengthsWithinLimits(bytes);+ for (String field : fields) {+ int[] dimensions = bytes.containsKey(field)+ ? dimensions(bytes.get(field).data())+ : s3ImageDimensions(request.get(field).get("S3Object"), region);+ requireAcceptedImage(operation, dimensions);+ }+ }++ /** A {@code Bytes} member: its content, or only its length when that is certainly over the limit. */+ private record Decoded(byte[] data, long length) {+ }++ private static Decoded decoded(String encoded) {+ try {+ if (encoded.length() <= MAX_ENCODED_LENGTH) {+ byte[] data = Base64.getDecoder().decode(encoded);+ return new Decoded(data, data.length);+ }+ return new Decoded(null, decodedLength(encoded));+ } catch (IllegalArgumentException | IOException e) {+ throw new AwsException("SerializationException", "Image bytes are not valid base64.", 400);+ }+ }++ /**+ * Counts what base64 too long to be accepted decodes to, still refusing invalid base64 first,+ * without holding the decoded bytes: the request body may be far larger than 5 MiB.+ */+ private static long decodedLength(String encoded) throws IOException {+ long length = 0;+ byte[] buffer = new byte[8192];+ try (InputStream in = Base64.getDecoder().wrap(new AsciiInputStream(encoded))) {+ int read;+ while ((read = in.read(buffer)) != -1) {+ length += read;+ }+ }+ return length;+ }++ /** Rekognition reports every out-of-range {@code Bytes} member in one ValidationException. */+ private static void requireLengthsWithinLimits(Map<String, Decoded> bytes) {+ List<String> violations = new ArrayList<>();+ for (Map.Entry<String, Decoded> entry : bytes.entrySet()) {+ String field = entry.getKey();+ String member = Character.toLowerCase(field.charAt(0)) + field.substring(1) + ".bytes";+ long length = entry.getValue().length();+ if (length < 1) {+ violations.add("Value at '" + member + "' failed to satisfy constraint: "+ + "Member must have length greater than or equal to 1");+ } else if (length > MAX_BYTES) {+ violations.add("Value at '" + member + "' failed to satisfy constraint: "+ + "Member must have length less than or equal to " + MAX_BYTES);+ }+ }+ if (!violations.isEmpty()) {+ String count = violations.size() == 1 ? "1 validation error" : violations.size() + " validation errors";+ throw new AwsException("ValidationException",+ count + " detected: " + String.join("; ", violations), 400);+ }+ }++ private static void requireAcceptedImage(String operation, int[] dimensions) {+ if (dimensions == null) {+ throw new AwsException("InvalidImageFormatException", INVALID_FORMAT, 400);+ }+ int width = dimensions[0];+ int height = dimensions[1];+ if (width > MAX_DIMENSION || height > MAX_DIMENSION) {+ if (TOO_LARGE_REPORTED.contains(operation)) {+ throw new AwsException("ImageTooLargeException", "Input image dimensions " + width + " x " + height+ + " exceed maximum dimension size of " + MAX_DIMENSION + " pixels", 400);+ }+ throw new AwsException("InvalidImageFormatException", INVALID_FORMAT, 400);+ }+ }++ /**+ * The dimensions of the object, when it exists in a bucket of the request's region. The object+ * is streamed, reading only as far as its format needs, so its size does not matter.+ */+ private int[] s3ImageDimensions(JsonNode s3Object, String region) {+ String bucket = s3Object.path("Bucket").asText(null);+ String name = s3Object.path("Name").asText(null);+ String version = s3Object.path("Version").asText(null);+ if (bucket == null || name == null) {+ throw new AwsException("InvalidS3ObjectException", UNREADABLE_OBJECT, 400);+ }+ String bucketRegion;+ try {+ bucketRegion = s3Service.getBucketRegion(bucket);+ } catch (AwsException e) {+ throw unreadable(bucket, name, e.getErrorCode());+ }+ if (bucketRegion == null || bucketRegion.isBlank()) {+ // A bucket created without a location constraint is in the default region.+ bucketRegion = config.defaultRegion();+ }+ if (!bucketRegion.equalsIgnoreCase(region)) {+ throw unreadable(bucket, name, "bucket is in " + bucketRegion + ", not " + region);+ }+ S3Service.ObjectRead read;+ try {+ read = s3Service.openObject(bucket, name, version);+ } catch (AwsException e) {+ throw unreadable(bucket, name, e.getErrorCode());+ }+ try (read) {+ return dimensions(read.body());+ } catch (IOException e) {+ throw new UncheckedIOException("Failed to close S3 object s3://" + bucket + "/" + name, e);+ }+ }++ private static AwsException unreadable(String bucket, String name, String reason) {+ LOG.debugv("Rekognition image s3://{0}/{1} is unreadable: {2}", bucket, name, reason);+ return new AwsException("InvalidS3ObjectException", UNREADABLE_OBJECT, 400);+ }++ /** The image's width and height, or null when it is not a complete JPEG or PNG. */+ static int[] dimensions(byte[] data) {+ return data == null ? null : dimensions(new ByteArrayInputStream(data));+ }++ static int[] dimensions(InputStream in) {+ try {+ byte[] start = in.readNBytes(2);+ if (start.length == 2 && (start[0] & 0xFF) == 0xFF && (start[1] & 0xFF) == 0xD8) {+ return jpegDimensions(in);+ }+ byte[] rest = in.readNBytes(6);+ if (start.length == 2 && rest.length == 6 && (start[0] & 0xFF) == 0x89 && start[1] == 'P'+ && rest[0] == 'N' && rest[1] == 'G' && rest[2] == 0x0D && rest[3] == 0x0A+ && rest[4] == 0x1A && rest[5] == 0x0A) {+ return pngDimensions(in);+ }+ return null;+ } catch (EOFException expected) {+ // A segment or chunk runs past the end of the data: the file is cut off.+ return null;+ } catch (IOException e) {+ throw new UncheckedIOException("Failed to read Rekognition image", e);+ }+ }++ private static int[] jpegDimensions(InputStream in) throws IOException {+ int[] frame = null;+ while (true) {+ int b = in.read();+ // Rekognition accepted stray bytes between segments, as libjpeg does with a warning.+ while (b != -1 && b != 0xFF) {+ b = in.read();+ }+ while (b == 0xFF) {+ b = in.read();+ }+ if (b == -1) {+ return null;+ }+ int marker = b;+ if (marker == 0x01 || (marker >= 0xD0 && marker <= 0xD7)) {+ continue;+ }+ if (marker == 0xD8 || marker == 0xD9) {+ return null;+ }+ int length = readShort(in);+ if (length < 2) {+ return null;+ }+ boolean startOfFrame = marker >= 0xC0 && marker <= 0xCF && marker != 0xC4 && marker != 0xC8 && marker != 0xCC;+ if (!startOfFrame && marker != 0xDA) {+ in.skipNBytes(length - 2);+ continue;+ }+ byte[] segment = in.readNBytes(length - 2);+ if (segment.length < length - 2) {+ return null;+ }+ if (startOfFrame) {+ int components = length < 8 ? 0 : segment[5] & 0xFF;+ if (components == 0 || length != 8 + 3 * components) {+ return null;+ }+ int height = ((segment[1] & 0xFF) << 8) | (segment[2] & 0xFF);+ int width = ((segment[3] & 0xFF) << 8) | (segment[4] & 0xFF);+ frame = new int[] {width, height};+ } else {+ // A complete start-of-scan header is all Rekognition needs: it accepted JPEGs cut+ // off anywhere in the scan that follows, and one without an end-of-image marker.+ int components = length < 3 ? 0 : segment[0] & 0xFF;+ if (components < 1 || components > 4 || length != 6 + 2 * components) {+ return null;+ }+ return frame != null && frame[0] > 0 && frame[1] > 0 ? frame : null;+ }+ }+ }++ private static int[] pngDimensions(InputStream in) throws IOException {+ int[] header = null;+ boolean imageData = false;+ while (true) {+ byte[] chunk = in.readNBytes(8);+ if (chunk.length < 8) {+ return null;+ }+ long length = readInt(chunk, 0) & 0xFFFFFFFFL;+ String type = new String(chunk, 4, 4, StandardCharsets.US_ASCII);+ if (header == null) {+ if (!"IHDR".equals(type) || length != 13) {+ return null;+ }+ byte[] content = in.readNBytes(13);+ if (content.length < 13) {+ return null;+ }+ header = new int[] {readInt(content, 0), readInt(content, 4)};+ in.skipNBytes(4);+ continue;+ }+ in.skipNBytes(length + 4);+ if ("IDAT".equals(type)) {+ imageData = true;+ }+ if ("IEND".equals(type)) {+ return imageData && header[0] > 0 && header[1] > 0 ? header : null;+ }+ }+ }++ private static int readShort(InputStream in) throws IOException {+ int high = in.read();+ int low = in.read();+ if (low == -1) {+ throw new EOFException();+ }+ return (high << 8) | low;+ }++ private static int readInt(byte[] data, int offset) {+ return ((data[offset] & 0xFF) << 24) | ((data[offset + 1] & 0xFF) << 16)+ | ((data[offset + 2] & 0xFF) << 8) | (data[offset + 3] & 0xFF);+ }++ /** A string's characters as bytes; a character base64 cannot contain fails the read. */+ private static final class AsciiInputStream extends InputStream {++ private final String text;+ private int position;++ AsciiInputStream(String text) {+ this.text = text;+ }++ @Override+ public int read() throws IOException {+ if (position >= text.length()) {+ return -1;+ }+ char c = text.charAt(position++);+ if (c > 0x7F) {+ throw new IOException("Non-ASCII character in base64");+ }+ return c;+ }+ }+}diff --git a/src/main/java/io/github/hectorvent/floci/services/rekognition/RekognitionJsonHandler.java b/src/main/java/io/github/hectorvent/floci/services/rekognition/RekognitionJsonHandler.javaindex 7c20017fe8..2d0186dc9c 100644--- a/src/main/java/io/github/hectorvent/floci/services/rekognition/RekognitionJsonHandler.java+++ b/src/main/java/io/github/hectorvent/floci/services/rekognition/RekognitionJsonHandler.java@@ -18,9 +18,11 @@ import java.util.function.Predicate;public class RekognitionJsonHandler {private static final Logger LOG = Logger.getLogger(RekognitionJsonHandler.class);private final RekognitionService rekognitionService;+ private final RekognitionImageValidator imageValidator;@Inject- public RekognitionJsonHandler(RekognitionService rekognitionService) {+ public RekognitionJsonHandler(RekognitionService rekognitionService, RekognitionImageValidator imageValidator) {this.rekognitionService = rekognitionService;+ this.imageValidator = imageValidator;}/*** Dispatches Rekognition actions received via the AwsJson11Controller.@@ -33,24 +35,31 @@ public class RekognitionJsonHandler {public Response handle(String action, JsonNode request, String region) {LOG.debugv("Rekognition action: {0}", action);return switch (action) {- case "DetectLabels" -> rekognitionService.detectLabels(requireImage(request, "Image"));- case "DetectFaces" -> rekognitionService.detectFaces(requireImage(request, "Image"));- case "DetectText" -> rekognitionService.detectText(requireImage(request, "Image"));+ case "DetectLabels" -> rekognitionService.detectLabels(validImage(action, request, region));+ case "DetectFaces" -> rekognitionService.detectFaces(validImage(action, request, region));+ case "DetectText" -> rekognitionService.detectText(validImage(action, request, region));case "CompareFaces" -> {String sourceKey = requireImage(request, "SourceImage");requireImage(request, "TargetImage");+ imageValidator.validate(action, request, region, "SourceImage", "TargetImage");yield rekognitionService.compareFaces(sourceKey);}- case "DetectModerationLabels" -> rekognitionService.detectModerationLabels(requireImage(request, "Image"));+ case "DetectModerationLabels" -> rekognitionService.detectModerationLabels(validImage(action, request, region));default -> Response.status(400).entity(new AwsErrorResponse("UnknownOperationException","Unknown operation: RekognitionService." + action)).build();};}+ /** The image's mock-response key, once the image has the shape and content Rekognition accepts. */+ private String validImage(String action, JsonNode request, String region) {+ String mockKey = requireImage(request, "Image");+ imageValidator.validate(action, request, region, "Image");+ return mockKey;+ }+/**- * The Image shape (real content ignored — the response is a fixed stub) is still- * required and must carry Bytes or S3Object, matching AWS's own required-member+ * The Image shape is required and must carry Bytes or S3Object, matching AWS's own required-member* and one-of-Bytes-or-S3Object modeling for Image/SourceImage/TargetImage. Returns* an optional "Bucket/Name" mock-response lookup key when S3Object is present (null* for a Bytes-backed image, which has no such key — mock lookup then simply doesn't@@ -87,10 +96,9 @@ public class RekognitionJsonHandler {/*** Bytes (a blob shape, base64-encoded string on the wire) and S3Object (a structure)* must each carry their modeled JSON type when present. Content-level correctness- * (valid base64, S3Object.Bucket/Name presence — S3Object has no required members- * in the modeled shape) is not validated: this stub never reads image content, and- * enforcing more than the wire shape declares would invent stricter validation than- * AWS's own model.+ * (valid base64, S3Object.Bucket/Name presence: S3Object has no required members+ * in the modeled shape) is left to {@link RekognitionImageValidator}, which checks the+ * image itself the way Rekognition does.*/private void requireCorrectTypeIfPresent(JsonNode image, String member, Predicate<JsonNode> isCorrectType, String field) {JsonNode value = image.get(member);diff --git a/src/main/java/io/github/hectorvent/floci/services/rekognition/RekognitionService.java b/src/main/java/io/github/hectorvent/floci/services/rekognition/RekognitionService.javaindex 107d7eb96f..c0fe4db500 100644--- a/src/main/java/io/github/hectorvent/floci/services/rekognition/RekognitionService.java+++ b/src/main/java/io/github/hectorvent/floci/services/rekognition/RekognitionService.java@@ -11,8 +11,10 @@ import jakarta.ws.rs.core.Response;import java.util.Optional;/**- * Dummy response builder for Amazon Rekognition. Stateless — every action ignores- * the actual image content and returns a fixed but AWS-shaped response by default.+ * Dummy response builder for Amazon Rekognition. Stateless: every action returns a fixed but+ * AWS-shaped response by default, whatever the image shows. The image itself has already been+ * checked by {@link RekognitionImageValidator}, so an image Rekognition would refuse never+ * reaches this class.* <p>* Content-listing operations (DetectLabels, DetectText) always return one canned* entry, matching the TextractService stub precedent. Face/moderation operationsdiff --git a/src/test/java/io/github/hectorvent/floci/services/rekognition/RekognitionImageValidatorTest.java b/src/test/java/io/github/hectorvent/floci/services/rekognition/RekognitionImageValidatorTest.javanew file mode 100644index 0000000000..22e1368650--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/services/rekognition/RekognitionImageValidatorTest.java@@ -0,0 +1,427 @@+package io.github.hectorvent.floci.services.rekognition;++import com.fasterxml.jackson.databind.ObjectMapper;+import com.fasterxml.jackson.databind.node.ObjectNode;+import io.github.hectorvent.floci.config.EmulatorConfig;+import io.github.hectorvent.floci.core.common.AwsException;+import io.github.hectorvent.floci.services.s3.S3Service;+import io.github.hectorvent.floci.services.s3.model.S3Object;+import org.junit.jupiter.api.Test;+import org.junit.jupiter.params.ParameterizedTest;+import org.junit.jupiter.params.provider.ValueSource;++import java.io.ByteArrayInputStream;+import java.io.InputStream;+import java.nio.ByteBuffer;+import java.nio.charset.StandardCharsets;+import java.util.Base64;+import java.util.HexFormat;+import java.util.function.Supplier;+import java.util.zip.CRC32;++import static org.junit.jupiter.api.Assertions.assertArrayEquals;+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;+import static org.junit.jupiter.api.Assertions.assertEquals;+import static org.junit.jupiter.api.Assertions.assertNull;+import static org.junit.jupiter.api.Assertions.assertThrows;+import static org.mockito.Mockito.mock;+import static org.mockito.Mockito.when;++/**+ * What Rekognition accepts and refuses, as measured in us-east-2 (see {@link RekognitionImageValidator}).+ */+class RekognitionImageValidatorTest {++ private static final ObjectMapper MAPPER = new ObjectMapper();+ private static final String INVALID_FORMAT = "Request has invalid image format";++ private final S3Service s3 = mock(S3Service.class);+ private final EmulatorConfig config = mock(EmulatorConfig.class);+ private final RekognitionImageValidator validator = new RekognitionImageValidator(s3, config);++ {+ when(config.defaultRegion()).thenReturn("us-east-1");+ }++ private static ObjectNode bytesImage(byte[] data) {+ ObjectNode image = MAPPER.createObjectNode();+ image.put("Bytes", Base64.getEncoder().encodeToString(data));+ return image;+ }++ private static ObjectNode s3Image(String bucket, String name) {+ ObjectNode image = MAPPER.createObjectNode();+ image.putObject("S3Object").put("Bucket", bucket).put("Name", name);+ return image;+ }++ private static final String REGION = "us-east-1";++ private static ObjectNode request(String field, ObjectNode image) {+ ObjectNode request = MAPPER.createObjectNode();+ request.set(field, image);+ return request;+ }++ private static ObjectNode compareFaces(ObjectNode source, ObjectNode target) {+ ObjectNode request = MAPPER.createObjectNode();+ request.set("SourceImage", source);+ request.set("TargetImage", target);+ return request;+ }++ private void accepted(String operation, ObjectNode image) {+ assertDoesNotThrow(() -> validator.validate(operation, request("Image", image), REGION, "Image"));+ }++ private AwsException refused(String operation, ObjectNode image, String field) {+ return refusedIn(REGION, operation, image, field);+ }++ private AwsException refusedIn(String region, String operation, ObjectNode image, String field) {+ return assertThrows(AwsException.class,+ () -> validator.validate(operation, request(field, image), region, field));+ }++ private static byte[] pngChunk(String type, byte[] content) {+ byte[] typeBytes = type.getBytes(StandardCharsets.US_ASCII);+ CRC32 crc = new CRC32();+ crc.update(typeBytes);+ crc.update(content);+ return ByteBuffer.allocate(12 + content.length).putInt(content.length).put(typeBytes).put(content)+ .putInt((int) crc.getValue()).array();+ }++ private AwsException refusedCompareFaces(ObjectNode source, ObjectNode target) {+ return assertThrows(AwsException.class,+ () -> validator.validate("CompareFaces", compareFaces(source, target), REGION, "SourceImage", "TargetImage"));+ }++ private void storeObject(String bucket, String region, String name, byte[] data) {+ storeObject(bucket, region, name, () -> new ByteArrayInputStream(data));+ }++ private void storeObject(String bucket, String region, String name, Supplier<InputStream> body) {+ when(s3.getBucketRegion(bucket)).thenReturn(region);+ when(s3.openObject(bucket, name, null)).thenAnswer(call -> new S3Service.ObjectRead(new S3Object(), body.get()));+ }++ @Test+ void completeJpegAndPngReportTheirDimensions() {+ assertArrayEquals(new int[] {100, 100}, RekognitionImageValidator.dimensions(TestImages.jpeg(100, 100)));+ assertArrayEquals(new int[] {100, 79}, RekognitionImageValidator.dimensions(TestImages.png(100, 79)));+ assertArrayEquals(new int[] {10001, 100}, RekognitionImageValidator.dimensions(TestImages.jpeg(10001, 100)));+ }++ @Test+ void anythingButACompleteJpegOrPngHasNoDimensions() {+ byte[] jpeg = TestImages.jpeg(100, 100);+ byte[] png = TestImages.png(100, 100);+ assertNull(RekognitionImageValidator.dimensions(TestImages.WEBP));+ assertNull(RekognitionImageValidator.dimensions(TestImages.gif(100, 100)));+ assertNull(RekognitionImageValidator.dimensions(new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9}));+ assertNull(RekognitionImageValidator.dimensions(new byte[0]));+ assertNull(RekognitionImageValidator.dimensions(TestImages.truncated(jpeg, TestImages.jpegScanStart(jpeg) - 4)),+ "a JPEG cut inside its start-of-scan header");+ assertNull(RekognitionImageValidator.dimensions(TestImages.truncated(png, png.length - 12)), "a PNG without IEND");+ }++ /**+ * Rekognition accepted a JPEG cut off anywhere after its start-of-scan header, even one cut right+ * at the start of the scan or missing only its end-of-image marker.+ */+ @Test+ void aJpegWhoseHeadersAreCompleteIsAcceptedHoweverMuchOfTheScanIsMissing() {+ byte[] jpeg = TestImages.jpeg(100, 100);+ int scanStart = TestImages.jpegScanStart(jpeg);+ for (int length : new int[] {scanStart, scanStart + (jpeg.length - scanStart) / 2, jpeg.length - 2}) {+ assertArrayEquals(new int[] {100, 100},+ RekognitionImageValidator.dimensions(TestImages.truncated(jpeg, length)), "cut at " + length);+ }+ }++ /** Rekognition refused WebP, GIF, other bytes and a truncated JPEG with this one message. */+ @Test+ void anImageThatIsNotACompleteJpegOrPngIsAnInvalidFormat() {+ byte[] jpeg = TestImages.jpeg(100, 100);+ for (byte[] data : new byte[][] {TestImages.WEBP, TestImages.gif(100, 100), {1, 2, 3},+ TestImages.truncated(jpeg, 300)}) {+ AwsException e = refused("DetectModerationLabels", bytesImage(data), "Image");+ assertEquals("InvalidImageFormatException", e.getErrorCode());+ assertEquals(INVALID_FORMAT, e.getMessage());+ }+ }++ /** The documented 80 pixel minimum is not enforced: 79 px and even 10 x 10 were accepted. */+ @Test+ void smallImagesAreAccepted() {+ accepted("DetectModerationLabels", bytesImage(TestImages.jpeg(10, 10)));+ accepted("DetectFaces", bytesImage(TestImages.png(100, 79)));+ }++ /** 5,062,030 bytes were accepted and 6,421,900 refused: the limit is 5 MiB, 5,242,880 bytes. */+ @Test+ void bytesOverFiveMebibytesAreRefusedNamingTheMember() {+ byte[] over = new byte[RekognitionImageValidator.MAX_BYTES + 1];++ AwsException image = refused("DetectLabels", bytesImage(over), "Image");+ assertEquals("ValidationException", image.getErrorCode());+ assertEquals("1 validation error detected: Value at 'image.bytes' failed to satisfy constraint: "+ + "Member must have length less than or equal to 5242880", image.getMessage());++ AwsException source = refused("CompareFaces", bytesImage(over), "SourceImage");+ assertEquals("1 validation error detected: Value at 'sourceImage.bytes' failed to satisfy constraint: "+ + "Member must have length less than or equal to 5242880", source.getMessage());+ AwsException target = refused("CompareFaces", bytesImage(over), "TargetImage");+ assertEquals("1 validation error detected: Value at 'targetImage.bytes' failed to satisfy constraint: "+ + "Member must have length less than or equal to 5242880", target.getMessage());+ }++ @Test+ void bytesAtTheLimitPassTheSizeCheck() {+ AwsException e = refused("DetectLabels", bytesImage(new byte[RekognitionImageValidator.MAX_BYTES]), "Image");++ assertEquals("InvalidImageFormatException", e.getErrorCode(), "past the size check, refused only as an image");+ }++ @ParameterizedTest+ @ValueSource(strings = {"DetectModerationLabels", "DetectLabels"})+ void aSideOverTenThousandPixelsIsTooLargeForModerationAndLabels(String operation) {+ AwsException wide = refused(operation, bytesImage(TestImages.jpeg(10001, 100)), "Image");+ assertEquals("ImageTooLargeException", wide.getErrorCode());+ assertEquals("Input image dimensions 10001 x 100 exceed maximum dimension size of 10000 pixels", wide.getMessage());++ AwsException tall = refused(operation, bytesImage(TestImages.png(100, 10001)), "Image");+ assertEquals("Input image dimensions 100 x 10001 exceed maximum dimension size of 10000 pixels", tall.getMessage());+ }++ /** The other operations refused the same image as an invalid format instead. */+ @ParameterizedTest+ @ValueSource(strings = {"DetectText", "DetectFaces", "CompareFaces"})+ void aSideOverTenThousandPixelsIsAnInvalidFormatElsewhere(String operation) {+ AwsException e = refused(operation, bytesImage(TestImages.jpeg(10001, 100)), "Image");++ assertEquals("InvalidImageFormatException", e.getErrorCode());+ assertEquals(INVALID_FORMAT, e.getMessage());+ }++ @Test+ void tenThousandPixelsIsAccepted() {+ accepted("DetectModerationLabels", bytesImage(TestImages.jpeg(10000, 100)));+ }++ @Test+ void anS3ObjectIsReadAndCheckedLikeBytes() {+ storeObject("images", REGION, "upload.webp", TestImages.WEBP);+ storeObject("images", REGION, "upload.jpg", TestImages.jpeg(100, 100));++ accepted("DetectModerationLabels", s3Image("images", "upload.jpg"));+ assertEquals("InvalidImageFormatException",+ refused("DetectModerationLabels", s3Image("images", "upload.webp"), "Image").getErrorCode());+ }++ /** A missing object and a missing bucket both answered this. */+ @Test+ void anS3ObjectThatCannotBeReadIsAnInvalidS3Object() {+ when(s3.openObject("images", "gone.jpg", null))+ .thenThrow(new AwsException("NoSuchKey", "The specified key does not exist.", 404));++ for (ObjectNode image : new ObjectNode[] {s3Image("images", "gone.jpg"),+ (ObjectNode) MAPPER.createObjectNode().set("S3Object", MAPPER.createObjectNode().put("Bucket", "images"))}) {+ AwsException e = refused("DetectModerationLabels", image, "Image");+ assertEquals("InvalidS3ObjectException", e.getErrorCode());+ assertEquals("Unable to get object metadata from S3. Check object key, region and/or access permissions.",+ e.getMessage());+ }+ }++ /** A bucket in us-west-2 named in a us-east-2 call answered the same as a missing object. */+ @Test+ void anS3ObjectInAnotherRegionIsAnInvalidS3Object() {+ storeObject("west", "us-west-2", "upload.jpg", TestImages.jpeg(100, 100));++ AwsException e = refused("DetectModerationLabels", s3Image("west", "upload.jpg"), "Image");++ assertEquals("InvalidS3ObjectException", e.getErrorCode());+ }++ @Test+ void emptyBytesAreRefusedNamingTheMember() {+ AwsException e = refused("DetectLabels", bytesImage(new byte[0]), "Image");++ assertEquals("ValidationException", e.getErrorCode());+ assertEquals("1 validation error detected: Value at 'image.bytes' failed to satisfy constraint: "+ + "Member must have length greater than or equal to 1", e.getMessage());+ }++ /** Rekognition fails to deserialize such a request, before any constraint is checked. */+ @Test+ void bytesThatAreNotBase64AreASerializationError() {+ String encoded = Base64.getEncoder().encodeToString(TestImages.jpeg(100, 100));+ for (String bytes : new String[] {"not base64!!", encoded.substring(0, 60) + "\n" + encoded.substring(60)}) {+ ObjectNode image = MAPPER.createObjectNode().put("Bytes", bytes);++ assertEquals("SerializationException", refused("DetectLabels", image, "Image").getErrorCode());+ }+ }++ @Test+ void base64WithoutPaddingIsAccepted() {+ String encoded = Base64.getEncoder().withoutPadding().encodeToString(TestImages.jpeg(100, 100));++ accepted("DetectLabels", MAPPER.createObjectNode().put("Bytes", encoded));+ }++ @Test+ void strayBytesBetweenJpegSegmentsAreAccepted() {+ byte[] jpeg = TestImages.jpeg(100, 100);+ int afterApp0 = 4 + (((jpeg[4] & 0xFF) << 8) | (jpeg[5] & 0xFF));+ byte[] padded = new byte[jpeg.length + 3];+ System.arraycopy(jpeg, 0, padded, 0, afterApp0);+ System.arraycopy(jpeg, afterApp0, padded, afterApp0 + 3, jpeg.length - afterApp0);++ accepted("DetectLabels", bytesImage(padded));+ }++ /** Rekognition checked both images' lengths before reading either image. */+ @Test+ void compareFacesReportsEveryOversizedImageBeforeAnyFormatError() {+ byte[] over = new byte[RekognitionImageValidator.MAX_BYTES + 1];++ AwsException both = refusedCompareFaces(bytesImage(over), bytesImage(over));+ assertEquals("ValidationException", both.getErrorCode());+ assertEquals("2 validation errors detected: "+ + "Value at 'sourceImage.bytes' failed to satisfy constraint: Member must have length less than or equal to 5242880; "+ + "Value at 'targetImage.bytes' failed to satisfy constraint: Member must have length less than or equal to 5242880",+ both.getMessage());++ AwsException target = refusedCompareFaces(bytesImage(TestImages.WEBP), bytesImage(over));+ assertEquals("1 validation error detected: Value at 'targetImage.bytes' failed to satisfy constraint: "+ + "Member must have length less than or equal to 5242880", target.getMessage());+ }++ @Test+ void compareFacesRefusesAnInvalidTargetImage() {+ AwsException e = refusedCompareFaces(bytesImage(TestImages.jpeg(100, 100)), bytesImage(TestImages.WEBP));++ assertEquals("InvalidImageFormatException", e.getErrorCode());+ }++ /** A bucket created without a location constraint is in the default region, not every region. */+ @Test+ void anS3ObjectInABucketWithoutARegionIsReadOnlyFromTheDefaultRegion() {+ storeObject("unconstrained", null, "upload.jpg", TestImages.jpeg(100, 100));++ accepted("DetectModerationLabels", s3Image("unconstrained", "upload.jpg"));+ assertEquals("InvalidS3ObjectException",+ refusedIn("us-west-2", "DetectModerationLabels", s3Image("unconstrained", "upload.jpg"), "Image").getErrorCode());+ }++ /** Rekognition refused a frame header with no components and a scan header with no fields. */+ @Test+ void aJpegWithIncompleteFrameOrScanHeadersIsAnInvalidFormat() {+ byte[] bare = HexFormat.of().parseHex("FFD8FFC00007080064006400FFDA0002");+ assertNull(RekognitionImageValidator.dimensions(bare));++ byte[] jpeg = TestImages.jpeg(100, 100);+ int scanStart = TestImages.jpegScanStart(jpeg);+ byte[] shortScanHeader = jpeg.clone();+ int componentCount = TestImages.jpegScanHeader(jpeg) + 2;+ shortScanHeader[componentCount] = (byte) (shortScanHeader[componentCount] + 1);+ assertNull(RekognitionImageValidator.dimensions(TestImages.truncated(shortScanHeader, scanStart)),+ "a scan header whose length does not match its component count");++ byte[] shortFrameHeader = jpeg.clone();+ int frameComponents = TestImages.jpegSegment(jpeg, 0xC0) + 7;+ shortFrameHeader[frameComponents] = (byte) (shortFrameHeader[frameComponents] + 1);+ assertNull(RekognitionImageValidator.dimensions(shortFrameHeader),+ "a frame header whose length does not match its component count");+ }++ /** Rekognition refused a PNG whose IHDR is followed directly by IEND. */+ @Test+ void aPngWithoutImageDataIsAnInvalidFormat() {+ byte[] signature = {(byte) 0x89, 'P', 'N', 'G', 0x0D, 0x0A, 0x1A, 0x0A};+ byte[] header = pngChunk("IHDR", ByteBuffer.allocate(13).putInt(100).putInt(100).put((byte) 8).put((byte) 2).array());+ byte[] end = pngChunk("IEND", new byte[0]);+ byte[] png = ByteBuffer.allocate(signature.length + header.length + end.length).put(signature).put(header).put(end).array();++ assertNull(RekognitionImageValidator.dimensions(png));+ }++ /** Base64 too long to be accepted is counted, not decoded, and still refused as invalid first. */+ @Test+ void base64LongerThanTheLimitIsCountedAndStillCheckedForValidity() {+ byte[] over = new byte[RekognitionImageValidator.MAX_BYTES + 3];+ String encoded = Base64.getEncoder().encodeToString(over);+ assertEquals(RekognitionImageValidator.MAX_ENCODED_LENGTH + 4, encoded.length(), "past the decoded path");++ AwsException tooLong = refused("DetectLabels", MAPPER.createObjectNode().put("Bytes", encoded), "Image");+ assertEquals("ValidationException", tooLong.getErrorCode());+ assertEquals("1 validation error detected: Value at 'image.bytes' failed to satisfy constraint: "+ + "Member must have length less than or equal to 5242880", tooLong.getMessage());++ for (String invalid : new String[] {encoded.substring(0, 100) + "!" + encoded.substring(101),+ encoded.substring(0, 100) + "\u0141" + encoded.substring(101)}) {+ assertEquals("SerializationException",+ refused("DetectLabels", MAPPER.createObjectNode().put("Bytes", invalid), "Image").getErrorCode());+ }+ }++ /**+ * An S3 object is read only as far as its format needs, so an object of any size is checked+ * without loading it: these are 3 GB, and reading any of their image data fails the test.+ */+ @Test+ void anS3ObjectIsReadOnlyAsFarAsItsFormatNeeds() {+ long imageData = 3_000_000_000L;+ byte[] jpeg = TestImages.jpeg(100, 100);+ byte[] jpegHeaders = TestImages.truncated(jpeg, TestImages.jpegScanStart(jpeg));+ storeObject("images", REGION, "huge.jpg", () -> new UnreadableGapStream(jpegHeaders, imageData, new byte[] {(byte) 0xFF, (byte) 0xD9}));++ byte[] signature = {(byte) 0x89, 'P', 'N', 'G', 0x0D, 0x0A, 0x1A, 0x0A};+ byte[] header = pngChunk("IHDR", ByteBuffer.allocate(13).putInt(100).putInt(100).put((byte) 8).put((byte) 2).array());+ byte[] dataChunkStart = ByteBuffer.allocate(8).putInt((int) imageData).put("IDAT".getBytes(StandardCharsets.US_ASCII)).array();+ byte[] pngHeaders = ByteBuffer.allocate(signature.length + header.length + 8).put(signature).put(header).put(dataChunkStart).array();+ storeObject("images", REGION, "huge.png", () -> new UnreadableGapStream(pngHeaders, imageData + 4, pngChunk("IEND", new byte[0])));++ accepted("DetectModerationLabels", s3Image("images", "huge.jpg"));+ accepted("DetectModerationLabels", s3Image("images", "huge.png"));+ }++ /** Some bytes, then a gap that may be skipped but not read, then more bytes. */+ private static final class UnreadableGapStream extends InputStream {++ private final byte[] head;+ private final long gapEnd;+ private final byte[] tail;+ private long position;++ UnreadableGapStream(byte[] head, long gap, byte[] tail) {+ this.head = head;+ this.gapEnd = head.length + gap;+ this.tail = tail;+ }++ @Override+ public int read() {+ if (position < head.length) {+ return head[(int) position++] & 0xFF;+ }+ if (position < gapEnd) {+ throw new AssertionError("read image data at offset " + position);+ }+ if (position < gapEnd + tail.length) {+ return tail[(int) (position++ - gapEnd)] & 0xFF;+ }+ return -1;+ }++ @Override+ public long skip(long n) {+ long skipped = Math.max(0, Math.min(n, gapEnd + tail.length - position));+ position += skipped;+ return skipped;+ }+ }+}diff --git a/src/test/java/io/github/hectorvent/floci/services/rekognition/RekognitionIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/rekognition/RekognitionIntegrationTest.javaindex 7438acc880..dbda0bd18e 100644--- a/src/test/java/io/github/hectorvent/floci/services/rekognition/RekognitionIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/rekognition/RekognitionIntegrationTest.java@@ -1,9 +1,13 @@package io.github.hectorvent.floci.services.rekognition;import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;import io.quarkus.test.junit.QuarkusTest;+import io.restassured.response.ValidatableResponse;import org.junit.jupiter.api.BeforeAll;+import org.junit.jupiter.api.BeforeEach;import org.junit.jupiter.api.Test;+import java.util.Base64;+import static io.restassured.RestAssured.given;import static org.hamcrest.Matchers.*;/**@@ -22,6 +26,41 @@ class RekognitionIntegrationTest {"AWS4-HMAC-SHA256 Credential=AKID/20260101/us-east-1/rekognition/aws4_request";private static final String S3_IMAGE ="{\"S3Object\":{\"Bucket\":\"my-bucket\",\"Name\":\"photo.jpg\"}}";+ private static final String JPEG_BASE64 = Base64.getEncoder().encodeToString(TestImages.jpeg(100, 100));+ private static boolean imagesUploaded;++ /**+ * Rekognition reads an S3Object and refuses one it cannot read, so the objects the tests name+ * exist: my-bucket/photo.jpg, mock-bucket/cat.jpg (the AI mock fixture's key) and two images+ * Rekognition refuses.+ */+ @BeforeEach+ void uploadImages() {+ if (imagesUploaded) {+ return;+ }+ put("my-bucket", "photo.jpg", TestImages.jpeg(100, 100));+ put("mock-bucket", "cat.jpg", TestImages.jpeg(100, 100));+ put("my-bucket", "upload.webp", TestImages.WEBP);+ put("my-bucket", "panorama.jpg", TestImages.jpeg(10001, 100));+ imagesUploaded = true;+ }++ private static void put(String bucket, String key, byte[] data) {+ given().when().put("/" + bucket);+ given().body(data).when().put("/" + bucket + "/" + key).then().statusCode(200);+ }++ private static ValidatableResponse call(String action, String body) {+ return given()+ .contentType(CONTENT_TYPE)+ .header("X-Amz-Target", "RekognitionService." + action)+ .header("Authorization", AUTH_HEADER)+ .body(body)+ .when()+ .post("/")+ .then();+ }@Testvoid detectLabels_returnsStubLabel() {given()@@ -120,13 +159,12 @@ class RekognitionIntegrationTest {}@Testvoid detectLabels_bothBytesAndS3ObjectAccepted() {- // Neither field's content is read, and nothing in the Rekognition model- // declares supplying both invalid.+ // Nothing in the Rekognition model declares supplying both invalid; the bytes are what is checked.given().contentType(CONTENT_TYPE).header("X-Amz-Target", "RekognitionService.DetectLabels").header("Authorization", AUTH_HEADER)- .body("{\"Image\":{\"Bytes\":\"aGVsbG8=\",\"S3Object\":{\"Bucket\":\"my-bucket\",\"Name\":\"photo.jpg\"}}}")+ .body("{\"Image\":{\"Bytes\":\"" + JPEG_BASE64 + "\",\"S3Object\":{\"Bucket\":\"my-bucket\",\"Name\":\"photo.jpg\"}}}").when().post("/").then()@@ -205,6 +243,105 @@ class RekognitionIntegrationTest {.body("ModerationLabels", hasSize(0)).body("ModerationModelVersion", equalTo("1.0"));}+ /** Measured on Rekognition (us-east-2): WebP is refused, as Bytes or as an S3Object. */+ @Test+ void detectModerationLabels_webpImage_returnsInvalidImageFormat() {+ String webp = Base64.getEncoder().encodeToString(TestImages.WEBP);+ call("DetectModerationLabels", "{\"Image\":{\"Bytes\":\"" + webp + "\"}}")+ .statusCode(400)+ .body("__type", equalTo("InvalidImageFormatException"))+ .body("message", equalTo("Request has invalid image format"));+ call("DetectModerationLabels", "{\"Image\":{\"S3Object\":{\"Bucket\":\"my-bucket\",\"Name\":\"upload.webp\"}}}")+ .statusCode(400)+ .body("__type", equalTo("InvalidImageFormatException"));+ }++ @Test+ void detectModerationLabels_missingObject_returnsInvalidS3Object() {+ call("DetectModerationLabels", "{\"Image\":{\"S3Object\":{\"Bucket\":\"my-bucket\",\"Name\":\"never-uploaded.jpg\"}}}")+ .statusCode(400)+ .body("__type", equalTo("InvalidS3ObjectException"))+ .body("message", equalTo("Unable to get object metadata from S3. Check object key, region and/or access permissions."));+ }++ @Test+ void detectModerationLabels_bucketInAnotherRegion_returnsInvalidS3Object() {+ given()+ .body("<CreateBucketConfiguration><LocationConstraint>us-west-2</LocationConstraint></CreateBucketConfiguration>")+ .when()+ .put("/west-bucket")+ .then()+ .statusCode(200);+ given().body(TestImages.jpeg(100, 100)).when().put("/west-bucket/photo.jpg").then().statusCode(200);++ call("DetectModerationLabels", "{\"Image\":{\"S3Object\":{\"Bucket\":\"west-bucket\",\"Name\":\"photo.jpg\"}}}")+ .statusCode(400)+ .body("__type", equalTo("InvalidS3ObjectException"));+ }++ @Test+ void detectModerationLabels_bucketWithoutARegionCalledFromAnotherRegion_returnsInvalidS3Object() {+ given()+ .contentType(CONTENT_TYPE)+ .header("X-Amz-Target", "RekognitionService.DetectModerationLabels")+ .header("Authorization", "AWS4-HMAC-SHA256 Credential=AKID/20260101/us-west-2/rekognition/aws4_request")+ .body("{\"Image\":" + S3_IMAGE + "}")+ .when()+ .post("/")+ .then()+ .statusCode(400)+ .body("__type", equalTo("InvalidS3ObjectException"));+ }++ @Test+ void compareFaces_bothImagesOversized_reportsBothInOneValidationException() {+ String oversized = "{\"Bytes\":\"" + Base64.getEncoder().encodeToString(new byte[RekognitionImageValidator.MAX_BYTES + 1]) + "\"}";+ call("CompareFaces", "{\"SourceImage\":" + oversized + ",\"TargetImage\":" + oversized + "}")+ .statusCode(400)+ .body("__type", equalTo("ValidationException"))+ .body("message", startsWith("2 validation errors detected: Value at 'sourceImage.bytes'"));+ }++ @Test+ void detectModerationLabels_bytesOverFiveMebibytes_returnsValidationException() {+ String oversized = Base64.getEncoder().encodeToString(new byte[RekognitionImageValidator.MAX_BYTES + 1]);+ call("DetectModerationLabels", "{\"Image\":{\"Bytes\":\"" + oversized + "\"}}")+ .statusCode(400)+ .body("__type", equalTo("ValidationException"))+ .body("message", equalTo("1 validation error detected: Value at 'image.bytes' failed to satisfy constraint: "+ + "Member must have length less than or equal to 5242880"));+ }++ /** DetectModerationLabels reports a side over 10,000 px as too large; DetectText as an invalid format. */+ @Test+ void anImageWiderThanTenThousandPixels_isRefusedWithEachOperationsError() {+ String panorama = "{\"Image\":{\"S3Object\":{\"Bucket\":\"my-bucket\",\"Name\":\"panorama.jpg\"}}}";+ call("DetectModerationLabels", panorama)+ .statusCode(400)+ .body("__type", equalTo("ImageTooLargeException"))+ .body("message", equalTo("Input image dimensions 10001 x 100 exceed maximum dimension size of 10000 pixels"));+ call("DetectText", panorama)+ .statusCode(400)+ .body("__type", equalTo("InvalidImageFormatException"));+ }++ /** The documented 80 pixel minimum is not enforced by Rekognition. */+ @Test+ void detectModerationLabels_imageUnderEightyPixels_isAccepted() {+ String tiny = Base64.getEncoder().encodeToString(TestImages.jpeg(10, 10));+ call("DetectModerationLabels", "{\"Image\":{\"Bytes\":\"" + tiny + "\"}}")+ .statusCode(200)+ .body("ModerationLabels", hasSize(0));+ }++ @Test+ void compareFaces_invalidTargetImage_returnsInvalidImageFormat() {+ String webp = Base64.getEncoder().encodeToString(TestImages.WEBP);+ call("CompareFaces", "{\"SourceImage\":" + S3_IMAGE + ",\"TargetImage\":{\"Bytes\":\"" + webp + "\"}}")+ .statusCode(400)+ .body("__type", equalTo("InvalidImageFormatException"));+ }+@Testvoid unknownAction_returnsUnknownOperationError() {given()diff --git a/src/test/java/io/github/hectorvent/floci/services/rekognition/TestImages.java b/src/test/java/io/github/hectorvent/floci/services/rekognition/TestImages.javanew file mode 100644index 0000000000..2415b397f9--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/services/rekognition/TestImages.java@@ -0,0 +1,87 @@+package io.github.hectorvent.floci.services.rekognition;++import javax.imageio.ImageIO;+import java.awt.image.BufferedImage;+import java.io.ByteArrayOutputStream;+import java.io.IOException;+import java.io.UncheckedIOException;+import java.util.Arrays;+import java.util.Base64;++/** Images for the Rekognition tests, built in memory. */+final class TestImages {++ /** A 100 x 100 WebP, the file the Rekognition probe sent, which Rekognition rejects. */+ static final byte[] WEBP = Base64.getDecoder().decode(+ "UklGRloAAABXRUJQVlA4IE4AAADQBQCdASpkAGQAPm02mUmkIyKhIKgAgA2JaW7hc+lwAAE97iLywDXU3uIvLANdTe4i8sA11N6IAP79TN//tTBJGUv//dBlnDdIAAAAAAA=");++ private TestImages() {+ }++ static byte[] jpeg(int width, int height) {+ return encode(width, height, "jpg");+ }++ static byte[] png(int width, int height) {+ return encode(width, height, "png");+ }++ static byte[] gif(int width, int height) {+ return encode(width, height, "gif");+ }++ /** Where the scan of a JPEG starts: the first byte after its start-of-scan header. */+ static int jpegScanStart(byte[] jpeg) {+ int header = jpegScanHeader(jpeg);+ return header + (((jpeg[header] & 0xFF) << 8) | (jpeg[header + 1] & 0xFF));+ }++ /** Where a JPEG's start-of-scan header starts: its two-byte length, then its component count. */+ static int jpegScanHeader(byte[] jpeg) {+ return jpegSegment(jpeg, 0xDA);+ }++ /** Where a JPEG's first segment with this marker starts: the index of its two-byte length. */+ static int jpegSegment(byte[] jpeg, int wanted) {+ int i = 2;+ while (i < jpeg.length) {+ while ((jpeg[i] & 0xFF) == 0xFF) {+ i++;+ }+ int marker = jpeg[i] & 0xFF;+ i++;+ if (marker == 0x01 || (marker >= 0xD0 && marker <= 0xD7)) {+ continue;+ }+ int length = ((jpeg[i] & 0xFF) << 8) | (jpeg[i + 1] & 0xFF);+ if (marker == wanted) {+ return i;+ }+ i += length;+ }+ throw new IllegalArgumentException("No segment with marker " + Integer.toHexString(wanted));+ }++ /** The first {@code length} bytes of {@code image}, as a cut-off upload would leave it. */+ static byte[] truncated(byte[] image, int length) {+ return Arrays.copyOf(image, length);+ }++ private static byte[] encode(int width, int height, String format) {+ BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);+ for (int x = 0; x < width; x++) {+ for (int y = 0; y < height; y++) {+ image.setRGB(x, y, 0x78A0C8);+ }+ }+ ByteArrayOutputStream out = new ByteArrayOutputStream();+ try {+ if (!ImageIO.write(image, format, out)) {+ throw new IllegalStateException("No ImageIO writer for " + format);+ }+ } catch (IOException e) {+ throw new UncheckedIOException(e);+ }+ return out.toByteArray();+ }+}