package qrc import ( "strings" "testing" ) var messageID = ID{ ChainID: "dev", Registry: "gno.land/r/x/reg/v1", Account: "g1abc", Generation: 7, } // The encoding of messageID, written out by hand. const messageIDBytes = "\x00\x03dev" + "\x00\x13gno.land/r/x/reg/v1" + "\x00\x05g1abc" + "\x00\x00\x00\x07" func TestIDBytes(t *testing.T) { if got := string(messageID.Bytes()); got != messageIDBytes { t.Errorf("got %q, want %q", got, messageIDBytes) } if got := string(ID{}.Bytes()); got != "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" { t.Errorf("empty ID: got %q", got) } // Moving a character from one field to the next changes the encoding. a := ID{ChainID: "ab", Registry: "c", Account: "d"} b := ID{ChainID: "a", Registry: "bc", Account: "d"} c := ID{ChainID: "a", Registry: "b", Account: "cd"} if string(a.Bytes()) == string(b.Bytes()) || string(b.Bytes()) == string(c.Bytes()) { t.Errorf("two different IDs have the same encoding") } // The generation uses its four bytes, most significant first. id := messageID id.Generation = 0x01020304 if got := string(id.Bytes()); !strings.HasSuffix(got, "\x01\x02\x03\x04") { t.Errorf("generation encoded as %q", got[len(got)-4:]) } } func TestBindMessage(t *testing.T) { want := "QuantumRecoveryCommit/v1" + "\x00" + "\x01" + messageIDBytes + "\x00\x00\x01\x02" + // leaf 258 "\x00\x09ML-DSA-65" + "\x00\x00\x00\x03\xaa\xbb\xcc" got := string(BindMessage(messageID, 258, "ML-DSA-65", []byte{0xaa, 0xbb, 0xcc})) if got != want { t.Errorf("got %q\nwant %q", got, want) } // The label and the key cannot trade bytes. a := BindMessage(messageID, 1, "ab", []byte("c")) b := BindMessage(messageID, 1, "a", []byte("bc")) if string(a) == string(b) { t.Errorf("two different requests have the same message") } } func TestRotateMessage(t *testing.T) { var rootSHA256, rootKeccak256 [HashSize]byte for i := range rootSHA256 { rootSHA256[i] = byte(i) rootKeccak256[i] = byte(0xff - i) } want := "QuantumRecoveryCommit/v1" + "\x00" + "\x02" + messageIDBytes + "\x00\x00\x00\x3f" + // leaf 63 string(rootSHA256[:]) + string(rootKeccak256[:]) + "\x10" // height 16 got := string(RotateMessage(messageID, 63, rootSHA256, rootKeccak256, 16)) if got != want { t.Errorf("got %q\nwant %q", got, want) } // The two roots are not interchangeable. swapped := string(RotateMessage(messageID, 63, rootKeccak256, rootSHA256, 16)) if swapped == got { t.Errorf("swapping the roots leaves the message unchanged") } for _, height := range []int{MinHeight - 1, MaxHeight + 1, -1, 256 + MinHeight} { if !panics(func() { RotateMessage(messageID, 0, rootSHA256, rootKeccak256, height) }) { t.Errorf("height %d accepted", height) } } } func TestAuthorizeMessage(t *testing.T) { want := "QuantumRecoveryCommit/v1" + "\x00" + "\x03" + messageIDBytes + "\x00\x00\x00\x00" + // leaf 0 "\x00\x12gno.land/r/x/vault" + "\x00\x00\x00\x08withdraw" got := string(AuthorizeMessage(messageID, 0, "gno.land/r/x/vault", []byte("withdraw"))) if got != want { t.Errorf("got %q\nwant %q", got, want) } // An empty payload is a message like any other. empty := string(AuthorizeMessage(messageID, 0, "gno.land/r/x/vault", nil)) if !strings.HasSuffix(empty, "vault\x00\x00\x00\x00") { t.Errorf("empty payload encoded as %q", empty) } } // TestMessagesAreDistinct checks that no request can pass for another one: // the same fields under two actions, two chains, two registries, two // accounts, two generations or two leaves never give the same bytes. func TestMessagesAreDistinct(t *testing.T) { seen := map[string]string{} add := func(name string, msg []byte) { if other, ok := seen[string(msg)]; ok { t.Errorf("%s and %s have the same message", name, other) } seen[string(msg)] = name } payload := []byte("payload") var rootA, rootB [HashSize]byte rootB[0] = 1 ids := map[string]ID{"base": messageID} id := messageID id.ChainID = "test" ids["chain"] = id id = messageID id.Registry = "gno.land/r/y/reg/v1" ids["registry"] = id id = messageID id.Account = "g1abd" ids["account"] = id id = messageID id.Generation = 8 ids["generation"] = id for name, id := range ids { for _, leaf := range []uint32{0, 1} { suffix := " (" + name + ", leaf " + string(rune('0'+leaf)) + ")" add("bind"+suffix, BindMessage(id, leaf, "app", payload)) add("authorize"+suffix, AuthorizeMessage(id, leaf, "app", payload)) add("rotate"+suffix, RotateMessage(id, leaf, rootA, rootB, MinHeight)) } } if len(seen) != 30 { t.Errorf("%d distinct messages, want 30", len(seen)) } } func panics(f func()) (panicked bool) { defer func() { if recover() != nil { panicked = true } }() f() return false }