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.

contract_test.gno

3.34 Kb · 141 lines
  1package vault
  2
  3import (
  4	"strings"
  5	"testing"
  6
  7	"gno.land/p/nt/testutils/v0"
  8)
  9
 10func Test(cur realm, t *testing.T) {
 11	test2 := testutils.TestAddress("test2")
 12	test3 := testutils.TestAddress("test3")
 13
 14	// check token metadata.
 15	{
 16		got := Token.GetName()
 17		expected := "Foo Token"
 18		if strings.TrimSpace(got) != strings.TrimSpace(expected) {
 19			t.Fatalf("expected %q, got %q.", expected, got)
 20		}
 21	}
 22
 23	// check balance of test1.
 24	{
 25		got := Token.BalanceOf(test1)
 26		expected := int64(100000000)
 27		if got != expected {
 28			t.Fatalf("expected %d, got %d.", expected, got)
 29		}
 30	}
 31
 32	// transfer tokens from test1 to test2.
 33	if err := privateLedger.Transfer(test1, test2, 1000); err != nil {
 34		t.Fatal(err)
 35	}
 36
 37	// check balance of test1.
 38	{
 39		got := Token.BalanceOf(test1)
 40		expected := int64(99999000)
 41		if got != expected {
 42			t.Fatalf("expected %d, got %d.", expected, got)
 43		}
 44	}
 45
 46	// check balance of test2.
 47	{
 48		got := Token.BalanceOf(test2)
 49		expected := int64(1000)
 50		if got != expected {
 51			t.Fatalf("expected %d, got %d.", expected, got)
 52		}
 53	}
 54
 55	// test1 deposits 300 with test3 as recovery address.
 56	testing.SetOriginCaller(test1)
 57	Deposit(cross(cur), 300, test3)
 58
 59	// check balance of test1.
 60	{
 61		got := Token.BalanceOf(test1)
 62		expected := int64(99998700)
 63		if got != expected {
 64			t.Fatalf("expected %d, got %d.", expected, got)
 65		}
 66	}
 67
 68	// check balance of the vault.
 69	{
 70		got := Token.BalanceOf(vaultAddr)
 71		expected := int64(300)
 72		if got != expected {
 73			t.Fatalf("expected %d, got %d.", expected, got)
 74		}
 75	}
 76
 77	// test1 unvaults 50 tokens, waits past the lock, then redeems them.
 78	Unvault(cross(cur), 50)
 79	testing.SkipHeights(101)  // SkipHeights resets the context, so re-set the caller.
 80	testing.SetOriginCaller(test1)
 81	Redeem(cross(cur), 50)
 82
 83	// check balance of test1.
 84	{
 85		got := Token.BalanceOf(test1)
 86		expected := int64(99998750)
 87		if got != expected {
 88			t.Fatalf("expected %d, got %d.", expected, got)
 89		}
 90	}
 91
 92	// check balance of the vault.
 93	{
 94		got := Token.BalanceOf(vaultAddr)
 95		expected := int64(250)
 96		if got != expected {
 97			t.Fatalf("expected %d, got %d.", expected, got)
 98		}
 99	}
100
101	// Recover sends test1's still-locked 250 to the recovery address recorded
102	// at deposit time (test3), leaving the vault empty.
103	//
104	// It is called here by test2, a bystander who never deposited, because
105	// Recover has NO caller check. That is a real flaw and this test pins it:
106	// it cannot steal (the destination is whatever the target itself recorded)
107	// but anyone can rewind anyone's deposit at will, which is a free denial of
108	// service. If a caller check is ever added, this assertion is what will
109	// fail, so the change has to be deliberate.
110	testing.SetOriginCaller(test2)
111	Recover(cross(cur), test1)
112
113	// the recovery address received the whole locked balance.
114	{
115		got := Token.BalanceOf(test3)
116		expected := int64(250)
117		if got != expected {
118			t.Fatalf("expected %d, got %d.", expected, got)
119		}
120	}
121
122	// the vault is empty.
123	{
124		got := Token.BalanceOf(vaultAddr)
125		expected := int64(0)
126		if got != expected {
127			t.Fatalf("expected %d, got %d.", expected, got)
128		}
129	}
130
131	// Recovering twice is a no-op rather than a second payout: the vaulted
132	// balance is zero, so Recover returns before transferring anything.
133	Recover(cross(cur), test1)
134	{
135		got := Token.BalanceOf(test3)
136		expected := int64(250)
137		if got != expected {
138			t.Fatalf("expected %d, got %d.", expected, got)
139		}
140	}
141}