Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Add local CMC test in Go #1202

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .github/workflows/library_go_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,11 @@ jobs:
shell: bash
run: |
make test_go

# This is only to test AwsCryptographicMaterialProviders which has local cmc test inside it.
- name: Test local CMC
if: matrix.library == 'AwsCryptographicMaterialProviders'
working-directory: ./${{ matrix.library }}/runtimes/go/TestsFromDafny-go
shell: bash
run: |
go test
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package main

import (
"context"
"crypto/rand"
"fmt"
"math/big"
"sync"
"testing"
"time"

"github.com/aws/aws-cryptographic-material-providers-library/releases/go/mpl/awscryptographykeystoresmithygeneratedtypes"
"github.com/aws/aws-cryptographic-material-providers-library/releases/go/mpl/awscryptographymaterialproviderssmithygeneratedtypes"
"github.com/aws/aws-cryptographic-material-providers-library/releases/go/mpl/test/awscryptographymaterialproviderssmithygenerated"
)

// Test setup
var (
identifiers = []string{
"one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten",
"eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen",
"eighteen", "nineteen", "twenty", "twenty one",
}
idSize = len(identifiers)
)

func TestConcurrentCacheOperations(t *testing.T) {
const numWorkers = 10
totalOperations := 300000
timeout := time.After(10 * time.Second)
go func() {
<-timeout
panic("operation timed out")
}()
// Create a WaitGroup to track all operations
var wg sync.WaitGroup
// Create buffered channel to control concurrent operations
ops := make(chan int, totalOperations)
errChannel := make(chan error, numWorkers)
client, err := awscryptographymaterialproviderssmithygenerated.NewClient(awscryptographymaterialproviderssmithygeneratedtypes.MaterialProvidersConfig{})
if err != nil {
panic(err)
}
cacheType := awscryptographymaterialproviderssmithygeneratedtypes.CacheTypeMemberDefault{
Value: awscryptographymaterialproviderssmithygeneratedtypes.DefaultCache{
EntryCapacity: 10,
},
}
cache, err := client.CreateCryptographicMaterialsCache(context.Background(), awscryptographymaterialproviderssmithygeneratedtypes.CreateCryptographicMaterialsCacheInput{
Cache: &cacheType,
})
if err != nil {
panic(err)
}

for range numWorkers {
wg.Add(1)
go func() {
processWorker(ops, &wg, errChannel, cache)
}()
}
// Fill the ops channel with work items
for i := range totalOperations {
ops <- i
}
close(ops)
// Wait for completion in a separate goroutine
go func() {
wg.Wait()
close(errChannel)
}()
// Check for errors in the main test goroutine
for err := range errChannel {
if err != nil {
t.Fatal(err)
}
}
}

func processWorker(ops <-chan int, wg *sync.WaitGroup, errChannel chan<- error, cache awscryptographymaterialproviderssmithygeneratedtypes.ICryptographicMaterialsCache) {
defer wg.Done()
for range ops {
err := testLotsOfAdding(cache)
if err != nil {
errChannel <- err
}
}
}

func testLotsOfAdding(cache awscryptographymaterialproviderssmithygeneratedtypes.ICryptographicMaterialsCache) error {
randIndex, err := rand.Int(rand.Reader, big.NewInt(int64(idSize)))
beaconKeyIdentifier := identifiers[randIndex.Int64()]
getCacheEntryInput := awscryptographymaterialproviderssmithygeneratedtypes.GetCacheEntryInput{
Identifier: []byte(beaconKeyIdentifier),
}
op, err := cache.GetCacheEntry(getCacheEntryInput)
if err != nil {
switch err.(type) {
case awscryptographymaterialproviderssmithygeneratedtypes.EntryDoesNotExist:
materials := awscryptographymaterialproviderssmithygeneratedtypes.MaterialsMemberBeaconKey{
Value: awscryptographykeystoresmithygeneratedtypes.BeaconKeyMaterials{
BeaconKeyIdentifier: beaconKeyIdentifier,
BeaconKey: []byte(beaconKeyIdentifier),
EncryptionContext: map[string]string{},
},
}

putCacheEntryInput := awscryptographymaterialproviderssmithygeneratedtypes.PutCacheEntryInput{
Identifier: []byte(beaconKeyIdentifier),
CreationTime: time.Now().Unix(),
ExpiryTime: time.Now().Unix() + 100,
Materials: &materials,
}
err := cache.PutCacheEntry(putCacheEntryInput)
if err != nil {
return (err)
}
default:
return err
}
} else {
if op.Materials.(*awscryptographymaterialproviderssmithygeneratedtypes.MaterialsMemberBeaconKey).Value.BeaconKeyIdentifier != beaconKeyIdentifier {
return fmt.Errorf("beacon key identifier mismatch: %s != %s", op.Materials.(*awscryptographymaterialproviderssmithygeneratedtypes.MaterialsMemberBeaconKey).Value.BeaconKeyIdentifier, beaconKeyIdentifier)
}
fmt.Printf("Cache hit with beacon key identifier %s \n", beaconKeyIdentifier)
}
return nil
}
Loading