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.

usernames_test.gno

5.97 Kb · 200 lines
  1package usernames
  2
  3import (
  4	"strings"
  5	"testing"
  6	"unicode/utf8"
  7
  8	"gno.land/p/nt/uassert/v0"
  9	"gno.land/p/nt/urequire/v0"
 10	"gno.land/r/sys/users"
 11)
 12
 13const (
 14	moulAddr  = address("g1manfred47kzduec920z88wfr64ylksmdcedlf5")
 15	plainAddr = address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5")
 16)
 17
 18// controllerPath is the realm r/sys/users whitelists in its own init when the
 19// chain id is "dev", which is what `gno test` runs as. Registering a fixture
 20// means borrowing that identity: this package is not a controller and
 21// AddControllerAtGenesis cannot be reached from here, since its `_ realm`
 22// first parameter is the non-crossing threaded form and an external realm
 23// refuses a cur-call to one.
 24const controllerPath = "gno.land/r/sys/users/init"
 25
 26// seed registers the fixtures every test in this file reads.
 27//
 28// The SetRealm is immediately followed by a crossing call, which is the only
 29// shape that works: in a helper that does not itself cross, the switch is
 30// silently dropped and the caller stays whoever it was.
 31func seed(cur realm, t *testing.T) {
 32	t.Helper()
 33	if users.IsNameTaken("moul") {
 34		return
 35	}
 36	testing.SetRealm(testing.NewCodeRealm(controllerPath))
 37	urequire.NoError(t, users.RegisterUser(cross(cur), "moul", moulAddr))
 38}
 39
 40func TestResolveByName(cur realm, t *testing.T) {
 41	seed(cur, t)
 42
 43	name, addr, found := Resolve("moul")
 44	uassert.True(t, found)
 45	uassert.Equal(t, "moul", name)
 46	uassert.Equal(t, moulAddr.String(), addr)
 47}
 48
 49func TestResolveByAddress(cur realm, t *testing.T) {
 50	seed(cur, t)
 51
 52	name, addr, found := Resolve(moulAddr.String())
 53	uassert.True(t, found)
 54	uassert.Equal(t, "moul", name)
 55	uassert.Equal(t, moulAddr.String(), addr)
 56}
 57
 58func TestResolveMisses(cur realm, t *testing.T) {
 59	seed(cur, t)
 60
 61	cases := []struct {
 62		name  string
 63		input string
 64	}{
 65		{"empty", ""},
 66		{"unknown name", "nobodyholdsthis"},
 67		{"valid address with no name", plainAddr.String()},
 68		{"not an address and not a name", "g1notanaddress"},
 69	}
 70	for _, tc := range cases {
 71		t.Run(tc.name, func(t *testing.T) {
 72			_, _, found := Resolve(tc.input)
 73			uassert.False(t, found)
 74		})
 75	}
 76}
 77
 78// TestResolveAnyTrap is the defect this tool exists not to repeat: for an
 79// input that parses as an address, r/sys/users.ResolveAny reports true
 80// whatever the lookup produced, so the pointer it returns can be nil. Pinned
 81// against the vendored copy so a later upstream fix is visible here.
 82func TestResolveAnyTrap(cur realm, t *testing.T) {
 83	seed(cur, t)
 84
 85	data, ok := users.ResolveAny(plainAddr.String())
 86	uassert.True(t, ok)
 87	uassert.True(t, data == nil)
 88
 89	_, _, found := Resolve(plainAddr.String())
 90	uassert.False(t, found)
 91}
 92
 93func TestAccessors(cur realm, t *testing.T) {
 94	seed(cur, t)
 95
 96	uassert.Equal(t, moulAddr.String(), AddressOf("moul"))
 97	uassert.Equal(t, "moul", NameOf(moulAddr.String()))
 98	uassert.Equal(t, "@moul", Display("moul"))
 99	uassert.Equal(t, "@moul", Display(moulAddr.String()))
100
101	uassert.Equal(t, "", AddressOf("nobodyholdsthis"))
102	uassert.Equal(t, "", NameOf("nobodyholdsthis"))
103	uassert.Equal(t, "nobodyholdsthis", Display("nobodyholdsthis"))
104}
105
106func TestCanonicalAndTaken(cur realm, t *testing.T) {
107	seed(cur, t)
108
109	uassert.Equal(t, users.Canonicalize("moul"), Canonical("moul"))
110	uassert.True(t, Taken("moul"))
111	uassert.False(t, Taken("nobodyholdsthis"))
112	uassert.False(t, Taken(""))
113
114	uassert.Equal(t, "moul", CanonicalOwner("moul"))
115	uassert.Equal(t, "", CanonicalOwner("nobodyholdsthis"))
116	uassert.Equal(t, "", CanonicalOwner(""))
117}
118
119// TestCurrentNameOnAlias: r/sys/users keeps an old name resolving after a
120// rename and nothing in the resolve result says it is old. CurrentName is the
121// only call that does.
122func TestCurrentNameOnAlias(cur realm, t *testing.T) {
123	seed(cur, t)
124
125	const old = "aliasfixture"
126	const now = "renamedfixture"
127
128	if !users.IsNameTaken(old) {
129		testing.SetRealm(testing.NewCodeRealm(controllerPath))
130		urequire.NoError(t, users.RegisterUser(cross(cur), old, plainAddr))
131	}
132
133	data := users.ResolveAddress(plainAddr)
134	urequire.True(t, data != nil)
135	if data.Name() == old {
136		testing.SetRealm(testing.NewCodeRealm(controllerPath))
137		urequire.NoError(t, data.UpdateName(0, cur, now))
138	}
139
140	uassert.Equal(t, now, CurrentName(old))
141	uassert.Equal(t, "", CurrentName(now))
142	uassert.Equal(t, "", CurrentName(plainAddr.String()))
143	uassert.Equal(t, "", CurrentName("nobodyholdsthis"))
144	uassert.Equal(t, "", CurrentName(""))
145
146	// The old name still resolves, to the current one.
147	name, _, found := Resolve(old)
148	uassert.True(t, found)
149	uassert.Equal(t, now, name)
150}
151
152func TestSafeArg(t *testing.T) {
153	cases := []struct {
154		name string
155		in   string
156		want bool
157	}{
158		{"username", "moul", true},
159		{"address", "g1manfred47kzduec920z88wfr64ylksmdcedlf5", true},
160		{"dashes and dots", "a-b.c_d", true},
161		{"empty", "", false},
162		{"quote", "a'b", false},
163		{"backtick", "a`b", false},
164		{"fence", "```", false},
165		{"dollar", "$(id)", false},
166		{"too long", strings.Repeat("a", maxInput+1), false},
167		{"exactly max", strings.Repeat("a", maxInput), true},
168	}
169	for _, tc := range cases {
170		t.Run(tc.name, func(t *testing.T) {
171			uassert.Equal(t, tc.want, safeArg(tc.in))
172		})
173	}
174}
175
176func TestTrim(t *testing.T) {
177	uassert.Equal(t, "moul", trim("moul"))
178	long := strings.Repeat("a", maxInput+10)
179	uassert.Equal(t, strings.Repeat("a", maxInput)+"…", trim(long))
180}
181
182// TestTrimCutsOnARuneBoundary: a byte cut at maxInput lands inside the
183// multi-byte character here and leaves half of it, which no escaper
184// downstream can do anything sensible with.
185func TestTrimCutsOnARuneBoundary(t *testing.T) {
186	in := strings.Repeat("a", maxInput-1) + "é" + "tail"
187	got := trim(in)
188	uassert.Equal(t, strings.Repeat("a", maxInput-1)+"é"+"…", got)
189	uassert.True(t, utf8.ValidString(got))
190
191	// And the cap counts characters, not bytes, so an all-multibyte input
192	// keeps maxInput of them rather than half as many.
193	accents := strings.Repeat("é", maxInput+5)
194	uassert.Equal(t, strings.Repeat("é", maxInput)+"…", trim(accents))
195}
196
197// TestDisplayOnEmpty pins the one input Display has nothing to say about.
198func TestDisplayOnEmpty(t *testing.T) {
199	uassert.Equal(t, "", Display(""))
200}