Files

48 lines
1.7 KiB
Go

package coderunner
import (
"strings"
"testing"
"github.com/google/uuid"
"lincodex/backend/internal/port/outbound"
)
func TestDeriveRunIDIsStableOpaqueAndSeparatesKeys(t *testing.T) {
secret := []byte(testRunKey)
key := testRequest().Key
first := deriveRunID(secret, key)
if again := deriveRunID(secret, key); again != first {
t.Fatalf("deriveRunID() = %s then %s, want a deterministic ID", first, again)
}
if first.Version() != 8 || first.Variant() != uuid.RFC4122 {
t.Fatalf("run ID %s has version %d variant %v, want a version 8 RFC 9562 UUID", first, first.Version(), first.Variant())
}
for _, identifier := range []uuid.UUID{key.AttemptItemID, key.SubmissionID} {
text := strings.ReplaceAll(first.String(), "-", "")
if strings.Contains(text, strings.ReplaceAll(identifier.String(), "-", "")) || strings.Contains(text, strings.ReplaceAll(identifier.String(), "-", "")[:8]) {
t.Fatalf("run ID %s contains a source identifier", first)
}
}
swapped := outbound.CodeRunKey{AttemptItemID: key.SubmissionID, SubmissionID: key.AttemptItemID}
nextGeneration := key
nextGeneration.Generation = 1
otherItem := key
otherItem.AttemptItemID = uuid.MustParse("00000000-0000-4000-8000-000000000001")
ids := map[uuid.UUID]string{first: "base"}
for name, derived := range map[string]uuid.UUID{
"swapped identifiers": deriveRunID(secret, swapped),
"next generation": deriveRunID(secret, nextGeneration),
"other item": deriveRunID(secret, otherItem),
"other secret": deriveRunID([]byte(testRunKey+"x"), key),
} {
if previous, clash := ids[derived]; clash {
t.Fatalf("run ID of %q equals the one of %q", name, previous)
}
ids[derived] = name
}
}