Skip to content

Commit

Permalink
test(Java): Add asserations to Local CMC Test
Browse files Browse the repository at this point in the history
  • Loading branch information
texastony committed Jan 28, 2025
1 parent 7dd758c commit 2da2d48
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 20 deletions.
13 changes: 0 additions & 13 deletions AwsCryptographicMaterialProviders/runtimes/java/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -224,19 +224,6 @@ tasks.test {
// This will show System.out.println statements
testLogging.showStandardStreams = true

testLogging {
lifecycle {
events = mutableSetOf(org.gradle.api.tasks.testing.logging.TestLogEvent.FAILED, org.gradle.api.tasks.testing.logging.TestLogEvent.PASSED, org.gradle.api.tasks.testing.logging.TestLogEvent.SKIPPED)
exceptionFormat = org.gradle.api.tasks.testing.logging.TestExceptionFormat.FULL
showExceptions = true
showCauses = true
showStackTraces = true
showStandardStreams = true
}
info.events = lifecycle.events
info.exceptionFormat = lifecycle.exceptionFormat
}

// See https://github.com/gradle/kotlin-dsl/issues/836
addTestListener(object : TestListener {
override fun beforeSuite(suite: TestDescriptor) {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import java.util.HashMap;
import java.util.List;
import java.util.Random;
import java.util.concurrent.atomic.AtomicIntegerArray;
import org.testng.Assert;
import org.testng.annotations.Test;
import software.amazon.cryptography.keystore.model.BeaconKeyMaterials;
import software.amazon.cryptography.materialproviders.ICryptographicMaterialsCache;
Expand All @@ -19,6 +21,7 @@
import software.amazon.cryptography.materialproviders.model.GetCacheEntryOutput;
import software.amazon.cryptography.materialproviders.model.MaterialProvidersConfig;
import software.amazon.cryptography.materialproviders.model.Materials;
import software.amazon.cryptography.materialproviders.model.MultiThreadedCache;
import software.amazon.cryptography.materialproviders.model.PutCacheEntryInput;

public class LocalCMCTests {
Expand All @@ -33,7 +36,9 @@ public class LocalCMCTests {
.cache(
CacheType
.builder()
.Default(DefaultCache.builder().entryCapacity(10).build())
.MultiThreaded(
MultiThreadedCache.builder().entryCapacity(10).build()
)
.build()
)
.build()
Expand Down Expand Up @@ -64,11 +69,20 @@ public class LocalCMCTests {
)
);
private static final int IDS_SIZE = identifies.size();
private static final AtomicIntegerArray CacheHitCounter =
new AtomicIntegerArray(IDS_SIZE);
private static final AtomicIntegerArray CacheMissCounter =
new AtomicIntegerArray(IDS_SIZE);
private static final AtomicIntegerArray IdCounter = new AtomicIntegerArray(
IDS_SIZE
);

@Test(threadPoolSize = 10, invocationCount = 300000, timeOut = 10000)
public void TestALotOfAdding() {
Random rand = ExternRandom.getSecureRandom();
String beaconKeyIdentifier = identifies.get(rand.nextInt(IDS_SIZE));
int index = rand.nextInt(IDS_SIZE);
IdCounter.getAndAdd(index, 1);
String beaconKeyIdentifier = identifies.get(index);

ByteBuffer cacheIdentifier = ByteBuffer.wrap(
beaconKeyIdentifier.getBytes(StandardCharsets.UTF_8)
Expand All @@ -83,11 +97,17 @@ public void TestALotOfAdding() {
GetCacheEntryOutput getCacheEntryOutput = test.GetCacheEntry(
getCacheEntryInput
);
// assertEquals(getCacheEntryOutput.materials().BeaconKey().beaconKey(), binaryData);
// assertEquals(getCacheEntryOutput.materials().BeaconKey().beaconKeyIdentifier(),
// stringData);
// System.out.println("are equal");
Assert.assertNotNull(
getCacheEntryOutput.materials().BeaconKey(),
"Beacon Materials are null!"
);
Assert.assertEquals(
getCacheEntryOutput.materials().BeaconKey().beaconKey(),
cacheIdentifier
);
CacheHitCounter.getAndAdd(index, 1);
} catch (EntryDoesNotExist ex) {
CacheMissCounter.getAndAdd(index, 1);
Materials materials = Materials
.builder()
.BeaconKey(
Expand All @@ -97,7 +117,7 @@ public void TestALotOfAdding() {
// The cacheIdentifier is used as the material
// because we are not testing the cryptography here.
.beaconKey(cacheIdentifier)
.encryptionContext(new HashMap<String, String>())
.encryptionContext(Collections.emptyMap())
.build()
)
.build();
Expand All @@ -112,4 +132,40 @@ public void TestALotOfAdding() {
test.PutCacheEntry(putCacheEntryInput);
}
}

// This test asserts that there were cache misses and cache hits.
@Test(dependsOnMethods = { "TestALotOfAdding" })
public void validateTestALotOfAdding() {
int sumOfHits = 0;
int sumOfMisses = 0;
int idSelectedCount = -1;
for (int i = 0; i < IDS_SIZE; i++) {
// Was the ID ever randomly selected?
idSelectedCount = IdCounter.get(i);
if (idSelectedCount > 1) {
// if the ID was hit, it MUST have been a HIT or MISS
Assert.assertEquals(
idSelectedCount,
CacheHitCounter.get(i) + CacheMissCounter.get(i),
"Cache hits for ID + Cache misses for ID != ID selection"
);
System.out.printf(
"LocalCMC Test: ID: %s hit %s miss %s%n",
i,
CacheHitCounter.get(i),
CacheMissCounter.get(i)
);
}
sumOfHits += CacheHitCounter.get(i);
sumOfMisses += CacheMissCounter.get(i);
idSelectedCount = -1;
}
// Assert there were misses, as the cache started empty
Assert.assertTrue(sumOfMisses > 0, "How were there no misses!");
// Assert there was at least one hit
Assert.assertTrue(sumOfHits > 0, "How were there no hits!");
// Without better ID selection control,
// along with finer timing control,
// we cannot assert expected hits or misses
}
}

0 comments on commit 2da2d48

Please sign in to comment.