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.

commitreveal_test.gno

2.26 Kb · 75 lines
 1package commitreveal
 2
 3import "testing"
 4
 5// Known vector, computed off-chain with:
 6//
 7//	printf 's3cret:hello world' | sha256sum
 8const (
 9	testPayload    = "hello world"
10	testSalt       = "s3cret"
11	testCommitment = "b5165d20616e37ff3b4c84c2ba2914ced11bf2df7755a8876f2b22d30fb2409a"
12)
13
14func TestCommitKnownVector(t *testing.T) {
15	got := Commit(testPayload, testSalt)
16	if got != testCommitment {
17		t.Fatalf("Commit(%q, %q) = %q, want %q", testPayload, testSalt, got, testCommitment)
18	}
19}
20
21func TestCommitFormat(t *testing.T) {
22	if !IsCommitment(Commit("anything", "else")) {
23		t.Fatal("Commit output must satisfy IsCommitment")
24	}
25	if !IsCommitment(testCommitment) {
26		t.Fatal("known vector must satisfy IsCommitment")
27	}
28}
29
30func TestVerifyHappyPath(t *testing.T) {
31	if !Verify(testCommitment, testPayload, testSalt) {
32		t.Fatal("Verify must accept the correct payload and salt")
33	}
34	c := Commit("a secret message", "random-salt-123")
35	if !Verify(c, "a secret message", "random-salt-123") {
36		t.Fatal("Verify must accept a freshly computed commitment")
37	}
38}
39
40func TestVerifyWrongPayload(t *testing.T) {
41	if Verify(testCommitment, "hello worle", testSalt) {
42		t.Fatal("Verify must reject a wrong payload")
43	}
44	if Verify(testCommitment, "", testSalt) {
45		t.Fatal("Verify must reject an empty payload")
46	}
47}
48
49func TestVerifyWrongSalt(t *testing.T) {
50	if Verify(testCommitment, testPayload, "s3creT") {
51		t.Fatal("Verify must reject a wrong salt")
52	}
53	if Verify(testCommitment, testPayload, "") {
54		t.Fatal("Verify must reject an empty salt")
55	}
56}
57
58func TestMalformedCommitments(t *testing.T) {
59	bad := []string{
60		"",                                                                 // empty
61		"b5165d20616e37ff3b4c84c2ba2914ced11bf2df7755a8876f2b22d30fb2409",   // 63 chars
62		"b5165d20616e37ff3b4c84c2ba2914ced11bf2df7755a8876f2b22d30fb2409aa", // 65 chars
63		"B5165D20616E37FF3B4C84C2BA2914CED11BF2DF7755A8876F2B22D30FB2409A", // uppercase
64		"g5165d20616e37ff3b4c84c2ba2914ced11bf2df7755a8876f2b22d30fb2409a", // non-hex char
65		" b5165d20616e37ff3b4c84c2ba2914ced11bf2df7755a8876f2b22d30fb2409a", // leading space
66	}
67	for _, s := range bad {
68		if IsCommitment(s) {
69			t.Fatalf("IsCommitment(%q) = true, want false", s)
70		}
71		if Verify(s, testPayload, testSalt) {
72			t.Fatalf("Verify(%q, ...) = true, want false", s)
73		}
74	}
75}