Skip to content Search Apps Documentation Source Content 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.

wesh_test.gno

3.39 Kb · 98 lines
 1package wesh
 2
 3import (
 4	"testing"
 5
 6	"gno.land/p/nt/uassert/v0"
 7)
 8
 9func TestContactValidate(t *testing.T) {
10	cases := []struct {
11		name string
12		in   Contact
13		want error
14	}{
15		{"well-formed", Contact{AccountPK: repeat(1, 32), Seed: repeat(2, 32)}, nil},
16		{"with a display name", Contact{AccountPK: repeat(1, 32), Seed: repeat(2, 32), DisplayName: "gno"}, nil},
17		{"seed too short", Contact{AccountPK: repeat(1, 32), Seed: repeat(2, 31)}, ErrBadSeedLen},
18		{"seed too long", Contact{AccountPK: repeat(1, 32), Seed: repeat(2, 33)}, ErrBadSeedLen},
19		{"no seed", Contact{AccountPK: repeat(1, 32)}, ErrBadSeedLen},
20		{"account key too short", Contact{AccountPK: repeat(1, 31), Seed: repeat(2, 32)}, ErrBadAccountPKLen},
21		{"no account key", Contact{Seed: repeat(2, 32)}, ErrBadAccountPKLen},
22		{
23			"display name too long",
24			Contact{AccountPK: repeat(1, 32), Seed: repeat(2, 32), DisplayName: string(make([]byte, MaxDisplayNameLen+1))},
25			ErrDisplayNameTooLong,
26		},
27	}
28	for _, tc := range cases {
29		err := tc.in.Validate()
30		if tc.want == nil {
31			uassert.NoError(t, err, tc.name)
32			continue
33		}
34		uassert.ErrorIs(t, err, tc.want, tc.name)
35	}
36}
37
38func TestSeedCommitment(t *testing.T) {
39	seed := mustHex(t, testSeedHex)
40	salt := []byte("a-16-byte-salt!!")
41
42	c, err := SeedCommitment(seed, salt)
43	uassert.NoError(t, err)
44	uassert.Equal(t, DigestLen, len(c))
45
46	again, err := SeedCommitment(seed, salt)
47	uassert.NoError(t, err)
48	uassert.Equal(t, string(c), string(again), "the commitment is deterministic")
49
50	uassert.True(t, OpenCommitment(c, seed, salt), "the correct opening verifies")
51	uassert.False(t, OpenCommitment(c, seed, []byte("another-salt!!!!")), "a wrong salt does not open it")
52	uassert.False(t, OpenCommitment(c, repeat(0xEE, 32), salt), "a wrong seed does not open it")
53	uassert.False(t, OpenCommitment(c[:31], seed, salt), "a truncated commitment does not open it")
54}
55
56func TestSeedCommitmentRejectsABadSeed(t *testing.T) {
57	_, err := SeedCommitment(repeat(1, 31), []byte("salt"))
58	uassert.ErrorIs(t, err, ErrBadSeedLen)
59}
60
61// TestSeedCommitmentIsSaltSeparated guards the length-extension style
62// ambiguity: without distinct inputs, (seed, "ab") and (seed+"a", "b") would
63// collide. The seed is fixed-length, which resolves it, and this pins that.
64func TestSeedCommitmentDependsOnTheSplit(t *testing.T) {
65	seed := repeat(0xAA, 32)
66	a, _ := SeedCommitment(seed, []byte("ab"))
67	b, _ := SeedCommitment(seed, []byte("abc"))
68	uassert.False(t, string(a) == string(b))
69}
70
71func TestDecodeHelpers(t *testing.T) {
72	cases := []struct {
73		name string
74		fn   func(string) ([]byte, error)
75		in   string
76		err  error
77	}{
78		{"account key", DecodeAccountPK, testAccountPKHex, nil},
79		{"seed", DecodeSeed, testSeedHex, nil},
80		{"device key", DecodeDevicePK, testSeedHex, nil},
81		{"commitment", DecodeCommitment, testSeedHex, nil},
82		{"account key, not hex", DecodeAccountPK, "zzzz", ErrBadHex},
83		{"account key, odd length", DecodeAccountPK, "abc", ErrBadHex},
84		{"account key, wrong length", DecodeAccountPK, "abcd", ErrBadAccountPKLen},
85		{"seed, wrong length", DecodeSeed, "abcd", ErrBadSeedLen},
86		{"device key, wrong length", DecodeDevicePK, "abcd", ErrBadDevicePKLen},
87		{"commitment, wrong length", DecodeCommitment, "abcd", ErrBadCommitmentLen},
88	}
89	for _, tc := range cases {
90		got, err := tc.fn(tc.in)
91		if tc.err == nil {
92			uassert.NoError(t, err, tc.name)
93			uassert.Equal(t, 32, len(got), tc.name)
94			continue
95		}
96		uassert.ErrorIs(t, err, tc.err, tc.name)
97	}
98}