refactor(core): share JSON authorization and CBOR test helpers (#9133)

* refactor(core): share JSON authorization and CBOR test helpers
This commit is contained in:
Aniket Shukla authored and GitHub committed 2026-11-21 09:23:10 -04:00
1 parent e725ed6e9a
commit 98efc5f949
7 files changed
+100 -68

No files matched your search

@@ -0,0 +1,55 @@
package io.floci.aws.core.common;
import jakarta.ws.rs.core.HttpHeaders;
import jakarta.ws.rs.core.MultivaluedHashMap;
import jakarta.ws.rs.core.MultivaluedMap;
import jakarta.ws.rs.core.UriInfo;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.NullAndEmptySource;
import org.junit.jupiter.params.provider.ValueSource;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.when;
class AwsJson11ControllerAuthorizationTest {
@Test
void nonBlankHeaderWinsWithoutReadingQueryParameters() {
HttpHeaders headers = mock(HttpHeaders.class);
UriInfo uriInfo = mock(UriInfo.class);
String authorization = "AWS4-HMAC-SHA256 Credential=header/20261121/us-east-1/ssm/aws4_request";
when(headers.getHeaderString("Authorization")).thenReturn(authorization);
assertEquals(authorization, AwsJson11Controller.requestAuthorization(headers, uriInfo));
verifyNoInteractions(uriInfo);
}
@ParameterizedTest
@NullAndEmptySource
@ValueSource(strings = {" ", "\t"})
void absentOrBlankHeaderFallsBackToPresignedQuery(String authorization) {
HttpHeaders headers = mock(HttpHeaders.class);
UriInfo uriInfo = mock(UriInfo.class);
MultivaluedMap<String, String> query = new MultivaluedHashMap<>();
String credential = "query/20261121/us-east-1/ssm/aws4_request";
query.putSingle("X-Amz-Credential", credential);
when(headers.getHeaderString("Authorization")).thenReturn(authorization);
when(uriInfo.getQueryParameters()).thenReturn(query);
assertEquals("AWS4-HMAC-SHA256 Credential=" + credential,
AwsJson11Controller.requestAuthorization(headers, uriInfo));
}
@Test
void absentHeaderAndCredentialRemainUnsigned() {
HttpHeaders headers = mock(HttpHeaders.class);
UriInfo uriInfo = mock(UriInfo.class);
when(uriInfo.getQueryParameters()).thenReturn(new MultivaluedHashMap<>());
assertNull(AwsJson11Controller.requestAuthorization(headers, uriInfo));
}
}
@@ -5,6 +5,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.cbor.CBORFactory;
import com.fasterxml.jackson.dataformat.cbor.CBORParser;
import io.floci.aws.testing.AwsTestClients;
import io.floci.aws.testing.EventStreamMessages;
import io.quarkus.test.junit.QuarkusTest;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
@@ -12,17 +13,13 @@ import software.amazon.awssdk.core.SdkBytes;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.kinesis.KinesisClient;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.zip.CRC32;
import static io.restassured.RestAssured.given;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
@QuarkusTest
@@ -54,12 +51,13 @@ class AwsJsonCborEventStreamIntegrationTest {
.then().statusCode(200).contentType("application/vnd.amazon.eventstream")
.header("smithy-protocol", "rpc-v2-cbor").extract().asByteArray();
ByteBuffer frames = ByteBuffer.wrap(response);
Frame initial = readFrame(frames);
List<EventStreamMessages.Message> frames = EventStreamMessages.decode(response);
assertEquals(2, frames.size());
EventStreamMessages.Message initial = frames.getFirst();
assertEquals("initial-response", initial.headers().get(":event-type"));
assertEquals("application/cbor", initial.headers().get(":content-type"));
assertTrue(CBOR.readTree(initial.payload()).isEmpty());
Frame event = readFrame(frames);
EventStreamMessages.Message event = frames.get(1);
assertEquals("SubscribeToShardEvent", event.headers().get(":event-type"));
assertEquals("application/cbor", event.headers().get(":content-type"));
JsonNode record = CBOR.readTree(event.payload()).path("Records").get(0);
@@ -77,7 +75,6 @@ class AwsJsonCborEventStreamIntegrationTest {
}
assertTrue(taggedDate);
}
assertFalse(frames.hasRemaining());
} finally {
if (consumer != null) {
String consumerArn = consumer;
@@ -88,34 +85,4 @@ class AwsJsonCborEventStreamIntegrationTest {
}
}
private static Frame readFrame(ByteBuffer frames) {
int start = frames.position();
int length = frames.getInt();
int headersLength = frames.getInt();
CRC32 crc = new CRC32();
crc.update(frames.array(), start, 8);
assertEquals((int) crc.getValue(), frames.getInt(), "prelude checksum");
int headersEnd = frames.position() + headersLength;
Map<String, String> headers = new LinkedHashMap<>();
while (frames.position() < headersEnd) {
String name = readText(frames, Byte.toUnsignedInt(frames.get()));
assertEquals(7, frames.get(), "event header string type");
headers.put(name, readText(frames, Short.toUnsignedInt(frames.getShort())));
}
byte[] payload = new byte[length - headersLength - 16];
frames.get(payload);
crc.reset();
crc.update(frames.array(), start, length - 4);
assertEquals((int) crc.getValue(), frames.getInt(), "message checksum");
return new Frame(headers, payload);
}
private static String readText(ByteBuffer input, int length) {
byte[] bytes = new byte[length];
input.get(bytes);
return new String(bytes, StandardCharsets.UTF_8);
}
private record Frame(Map<String, String> headers, byte[] payload) {
}
}
@@ -31,6 +31,7 @@ import java.util.Map;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicReference;
import static io.floci.aws.testing.CborTestUtils.tagOf;
import static io.restassured.RestAssured.given;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -229,14 +230,4 @@ class AwsJsonCborTypedShapesIntegrationTest {
throw new AssertionError("Missing CBOR member: " + member);
}
private static int tagOf(byte[] body, String member) throws Exception {
try (CBORParser parser = (CBORParser) CBOR.getFactory().createParser(body)) {
while (parser.nextToken() != null) {
if (member.equals(parser.currentName()) && parser.currentToken().isScalarValue()) {
return parser.getCurrentTag();
}
}
}
throw new AssertionError("Missing CBOR member: " + member);
}
}
@@ -4,11 +4,11 @@ import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.dataformat.cbor.CBORFactory;
import com.fasterxml.jackson.dataformat.cbor.CBORParser;
import org.junit.jupiter.api.Test;
import java.math.BigDecimal;
import static io.floci.aws.testing.CborTestUtils.tagOf;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -107,14 +107,4 @@ class RpcV2CborCodecTest {
assertEquals(snapshot, request);
}
private static int tagOf(byte[] bytes, String member) throws Exception {
try (CBORParser parser = (CBORParser) CBOR.getFactory().createParser(bytes)) {
while (parser.nextToken() != null) {
if (member.equals(parser.currentName()) && parser.currentToken().isScalarValue()) {
return parser.getCurrentTag();
}
}
}
throw new AssertionError("Missing CBOR member: " + member);
}
}
@@ -0,0 +1,26 @@
package io.floci.aws.testing;
import com.fasterxml.jackson.dataformat.cbor.CBORFactory;
import com.fasterxml.jackson.dataformat.cbor.CBORParser;
import java.io.IOException;
/** Reads wire-level details that Jackson's JSON tree does not retain. */
public final class CborTestUtils {
private static final CBORFactory FACTORY = new CBORFactory();
private CborTestUtils() {
}
public static int tagOf(byte[] bytes, String member) throws IOException {
try (CBORParser parser = FACTORY.createParser(bytes)) {
while (parser.nextToken() != null) {
if (member.equals(parser.currentName()) && parser.currentToken().isScalarValue()) {
return parser.getCurrentTag();
}
}
}
throw new AssertionError("Missing CBOR member: " + member);
}
}