Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

message_test.gno

4.64 Kb · 155 lines
  1package qrc
  2
  3import (
  4	"strings"
  5	"testing"
  6)
  7
  8var messageID = ID{
  9	ChainID:    "dev",
 10	Registry:   "gno.land/r/x/reg/v1",
 11	Account:    "g1abc",
 12	Generation: 7,
 13}
 14
 15// The encoding of messageID, written out by hand.
 16const messageIDBytes = "\x00\x03dev" + "\x00\x13gno.land/r/x/reg/v1" + "\x00\x05g1abc" + "\x00\x00\x00\x07"
 17
 18func TestIDBytes(t *testing.T) {
 19	if got := string(messageID.Bytes()); got != messageIDBytes {
 20		t.Errorf("got %q, want %q", got, messageIDBytes)
 21	}
 22	if got := string(ID{}.Bytes()); got != "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" {
 23		t.Errorf("empty ID: got %q", got)
 24	}
 25
 26	// Moving a character from one field to the next changes the encoding.
 27	a := ID{ChainID: "ab", Registry: "c", Account: "d"}
 28	b := ID{ChainID: "a", Registry: "bc", Account: "d"}
 29	c := ID{ChainID: "a", Registry: "b", Account: "cd"}
 30	if string(a.Bytes()) == string(b.Bytes()) || string(b.Bytes()) == string(c.Bytes()) {
 31		t.Errorf("two different IDs have the same encoding")
 32	}
 33
 34	// The generation uses its four bytes, most significant first.
 35	id := messageID
 36	id.Generation = 0x01020304
 37	if got := string(id.Bytes()); !strings.HasSuffix(got, "\x01\x02\x03\x04") {
 38		t.Errorf("generation encoded as %q", got[len(got)-4:])
 39	}
 40}
 41
 42func TestBindMessage(t *testing.T) {
 43	want := "QuantumRecoveryCommit/v1" + "\x00" + "\x01" + messageIDBytes +
 44		"\x00\x00\x01\x02" + // leaf 258
 45		"\x00\x09ML-DSA-65" +
 46		"\x00\x00\x00\x03\xaa\xbb\xcc"
 47	got := string(BindMessage(messageID, 258, "ML-DSA-65", []byte{0xaa, 0xbb, 0xcc}))
 48	if got != want {
 49		t.Errorf("got  %q\nwant %q", got, want)
 50	}
 51
 52	// The label and the key cannot trade bytes.
 53	a := BindMessage(messageID, 1, "ab", []byte("c"))
 54	b := BindMessage(messageID, 1, "a", []byte("bc"))
 55	if string(a) == string(b) {
 56		t.Errorf("two different requests have the same message")
 57	}
 58}
 59
 60func TestRotateMessage(t *testing.T) {
 61	var rootSHA256, rootKeccak256 [HashSize]byte
 62	for i := range rootSHA256 {
 63		rootSHA256[i] = byte(i)
 64		rootKeccak256[i] = byte(0xff - i)
 65	}
 66	want := "QuantumRecoveryCommit/v1" + "\x00" + "\x02" + messageIDBytes +
 67		"\x00\x00\x00\x3f" + // leaf 63
 68		string(rootSHA256[:]) + string(rootKeccak256[:]) +
 69		"\x10" // height 16
 70	got := string(RotateMessage(messageID, 63, rootSHA256, rootKeccak256, 16))
 71	if got != want {
 72		t.Errorf("got  %q\nwant %q", got, want)
 73	}
 74
 75	// The two roots are not interchangeable.
 76	swapped := string(RotateMessage(messageID, 63, rootKeccak256, rootSHA256, 16))
 77	if swapped == got {
 78		t.Errorf("swapping the roots leaves the message unchanged")
 79	}
 80
 81	for _, height := range []int{MinHeight - 1, MaxHeight + 1, -1, 256 + MinHeight} {
 82		if !panics(func() { RotateMessage(messageID, 0, rootSHA256, rootKeccak256, height) }) {
 83			t.Errorf("height %d accepted", height)
 84		}
 85	}
 86}
 87
 88func TestAuthorizeMessage(t *testing.T) {
 89	want := "QuantumRecoveryCommit/v1" + "\x00" + "\x03" + messageIDBytes +
 90		"\x00\x00\x00\x00" + // leaf 0
 91		"\x00\x12gno.land/r/x/vault" +
 92		"\x00\x00\x00\x08withdraw"
 93	got := string(AuthorizeMessage(messageID, 0, "gno.land/r/x/vault", []byte("withdraw")))
 94	if got != want {
 95		t.Errorf("got  %q\nwant %q", got, want)
 96	}
 97
 98	// An empty payload is a message like any other.
 99	empty := string(AuthorizeMessage(messageID, 0, "gno.land/r/x/vault", nil))
100	if !strings.HasSuffix(empty, "vault\x00\x00\x00\x00") {
101		t.Errorf("empty payload encoded as %q", empty)
102	}
103}
104
105// TestMessagesAreDistinct checks that no request can pass for another one:
106// the same fields under two actions, two chains, two registries, two
107// accounts, two generations or two leaves never give the same bytes.
108func TestMessagesAreDistinct(t *testing.T) {
109	seen := map[string]string{}
110	add := func(name string, msg []byte) {
111		if other, ok := seen[string(msg)]; ok {
112			t.Errorf("%s and %s have the same message", name, other)
113		}
114		seen[string(msg)] = name
115	}
116
117	payload := []byte("payload")
118	var rootA, rootB [HashSize]byte
119	rootB[0] = 1
120	ids := map[string]ID{"base": messageID}
121	id := messageID
122	id.ChainID = "test"
123	ids["chain"] = id
124	id = messageID
125	id.Registry = "gno.land/r/y/reg/v1"
126	ids["registry"] = id
127	id = messageID
128	id.Account = "g1abd"
129	ids["account"] = id
130	id = messageID
131	id.Generation = 8
132	ids["generation"] = id
133
134	for name, id := range ids {
135		for _, leaf := range []uint32{0, 1} {
136			suffix := " (" + name + ", leaf " + string(rune('0'+leaf)) + ")"
137			add("bind"+suffix, BindMessage(id, leaf, "app", payload))
138			add("authorize"+suffix, AuthorizeMessage(id, leaf, "app", payload))
139			add("rotate"+suffix, RotateMessage(id, leaf, rootA, rootB, MinHeight))
140		}
141	}
142	if len(seen) != 30 {
143		t.Errorf("%d distinct messages, want 30", len(seen))
144	}
145}
146
147func panics(f func()) (panicked bool) {
148	defer func() {
149		if recover() != nil {
150			panicked = true
151		}
152	}()
153	f()
154	return false
155}