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.

reverse_test.gno

2.59 Kb · 84 lines
 1package gns
 2
 3import (
 4	"testing"
 5)
 6
 7func TestPrimaryNameForwardVerified(cur realm, t *testing.T) {
 8	reset()
 9	doRegister(cur, "alice", alice, 100000)
10
11	// forward address must point to the caller before SetPrimaryName succeeds
12	testing.SetRealm(testing.NewUserRealm(alice))
13	SetAddress(cross(cur), "alice", alice.String())
14	testing.SetRealm(testing.NewUserRealm(alice))
15	SetPrimaryName(cross(cur), "alice")
16
17	name, ok := PrimaryName(alice.String())
18	isTrue(t, ok, "primary resolves")
19	eqStr(t, "alice", name, "primary is alice")
20}
21
22func TestPrimaryNameForwardMismatch(cur realm, t *testing.T) {
23	reset()
24	doRegister(cur, "alice", alice, 100000)
25
26	testing.SetRealm(testing.NewUserRealm(alice))
27	SetAddress(cross(cur), "alice", alice.String())
28	testing.SetRealm(testing.NewUserRealm(alice))
29	SetPrimaryName(cross(cur), "alice")
30
31	// change the forward address away from alice -> primary must stop resolving
32	testing.SetRealm(testing.NewUserRealm(alice))
33	SetAddress(cross(cur), "alice", bob.String())
34
35	// invariant 6: primary returned only when forward resolution matches
36	_, ok := PrimaryName(alice.String())
37	isFalse(t, ok, "stale reverse not returned")
38}
39
40func TestPrimaryNameExpired(cur realm, t *testing.T) {
41	reset()
42	doRegister(cur, "alice", alice, 1000)
43	testing.SetRealm(testing.NewUserRealm(alice))
44	SetAddress(cross(cur), "alice", alice.String())
45	testing.SetRealm(testing.NewUserRealm(alice))
46	SetPrimaryName(cross(cur), "alice")
47
48	testing.SkipHeights(500) // expire the name
49	_, ok := PrimaryName(alice.String())
50	isFalse(t, ok, "expired name yields no primary")
51}
52
53func TestClearPrimaryName(cur realm, t *testing.T) {
54	reset()
55	doRegister(cur, "alice", alice, 100000)
56	testing.SetRealm(testing.NewUserRealm(alice))
57	SetAddress(cross(cur), "alice", alice.String())
58	testing.SetRealm(testing.NewUserRealm(alice))
59	SetPrimaryName(cross(cur), "alice")
60
61	_, ok := PrimaryName(alice.String())
62	isTrue(t, ok, "primary set")
63
64	testing.SetRealm(testing.NewUserRealm(alice))
65	ClearPrimaryName(cross(cur))
66	_, ok = PrimaryName(alice.String())
67	isFalse(t, ok, "primary cleared")
68}
69
70func TestTransferInvalidatesReverse(cur realm, t *testing.T) {
71	reset()
72	doRegister(cur, "alice", alice, 100000)
73	testing.SetRealm(testing.NewUserRealm(alice))
74	SetAddress(cross(cur), "alice", alice.String())
75	testing.SetRealm(testing.NewUserRealm(alice))
76	SetPrimaryName(cross(cur), "alice")
77
78	// transfer to bob, clearing records -> forward no longer points to alice
79	testing.SetRealm(testing.NewUserRealm(alice))
80	Transfer(cross(cur), "alice", bob, true, true)
81
82	_, ok := PrimaryName(alice.String())
83	isFalse(t, ok, "reverse invalidated after transfer+record clear")
84}