MCPcopy Create free account
hub / github.com/devaccuracy/ledgerforge / TestKeySize

Function TestKeySize

internal/tokenization/tokenization_test.go:352–398  ·  view source on GitHub ↗

TestKeySize ensures the service works with the required 32-byte key size (AES-256) and rejects other key sizes

(t *testing.T)

Source from the content-addressed store, hash-verified

350// TestKeySize ensures the service works with the required 32-byte key size (AES-256)
351// and rejects other key sizes
352func TestKeySize(t *testing.T) {
353 testValue := "Test value"
354
355 // Test that 32-byte key works (AES-256 - the only supported size)
356 t.Run("32-byte key should work", func(t *testing.T) {
357 key := make([]byte, 32)
358 _, err := rand.Read(key)
359 if err != nil {
360 t.Fatalf("Failed to generate random key: %v", err)
361 }
362
363 service := NewTokenizationService(key)
364 token, err := service.Tokenize(testValue)
365 if err != nil {
366 t.Errorf("Tokenize with 32-byte key error: %v", err)
367 return
368 }
369
370 decrypted, err := service.Detokenize(token)
371 if err != nil {
372 t.Errorf("Detokenize with 32-byte key error: %v", err)
373 return
374 }
375
376 if decrypted != testValue {
377 t.Errorf("Expected %q, got %q with 32-byte key", testValue, decrypted)
378 }
379 })
380
381 // Test that non-32-byte keys are rejected (tokenization disabled)
382 invalidSizes := []int{16, 24} // AES-128 and AES-192 are not supported
383 for _, size := range invalidSizes {
384 t.Run(fmt.Sprintf("%d-byte key should be rejected", size), func(t *testing.T) {
385 key := make([]byte, size)
386 _, err := rand.Read(key)
387 if err != nil {
388 t.Fatalf("Failed to generate random key: %v", err)
389 }
390
391 service := NewTokenizationService(key)
392 _, err = service.Tokenize(testValue)
393 if err == nil {
394 t.Errorf("Expected error with %d-byte key, but got none", size)
395 }
396 })
397 }
398}
399
400// TestInvalidKeySize ensures appropriate error handling for invalid key sizes
401func TestInvalidKeySize(t *testing.T) {

Callers

nothing calls this directly

Calls 3

TokenizeMethod · 0.95
DetokenizeMethod · 0.95
NewTokenizationServiceFunction · 0.85

Tested by

no test coverage detected