clavitor/clavis/clavis-vault/lib/cvt_test.go

62 lines
1.4 KiB
Go

package lib
import (
"bytes"
"testing"
)
func TestMintWireToken_roundtrip(t *testing.T) {
l0 := []byte{0x11, 0x22, 0x33, 0x44}
l1 := []byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88}
agentID := make([]byte, 16)
for i := range agentID {
agentID[i] = byte(0x40 + i)
}
token, err := MintWireToken(l0, l1, agentID)
if err != nil {
t.Fatalf("MintWireToken: %v", err)
}
gotL0, gotL1, gotAgentID, err := ParseWireToken(token)
if err != nil {
t.Fatalf("ParseWireToken: %v", err)
}
if !bytes.Equal(gotL0, l0) {
t.Fatalf("L0 mismatch")
}
if !bytes.Equal(gotL1, l1) {
t.Fatalf("L1 mismatch")
}
if !bytes.Equal(gotAgentID, agentID) {
t.Fatalf("agent_id mismatch")
}
}
func TestCVT_tamper_detection(t *testing.T) {
l0 := []byte{0x11, 0x22, 0x33, 0x44}
l1 := []byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88}
agentID := make([]byte, 16)
token, _ := MintWireToken(l0, l1, agentID)
// Flip a character in the middle
tampered := token[:10] + "X" + token[11:]
_, _, _, err := ParseWireToken(tampered)
if err == nil {
t.Fatal("expected error on tampered token")
}
}
func TestCVT_unique(t *testing.T) {
l0 := []byte{0x11, 0x22, 0x33, 0x44}
l1 := []byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88}
agentID := make([]byte, 16)
t1, _ := MintWireToken(l0, l1, agentID)
t2, _ := MintWireToken(l0, l1, agentID)
if t1 == t2 {
t.Fatal("two tokens with same input should differ (random nonce)")
}
}