Search Apps Documentation Source Content File Folder Download Copy Actions Download

converter_test.gno

3.60 Kb · 154 lines
  1package ctg
  2
  3import (
  4	"testing"
  5)
  6
  7func TestConvertKnownAddress(t *testing.T) {
  8	const (
  9		cosmosAddr = "cosmos1jg8mtutu9khhfwc4nxmuhcpftf0pajdh6svrgs"
 10		gnoAddr    = "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5"
 11	)
 12	got, err := ConvertCosmosToGno(cosmosAddr)
 13	if err != nil {
 14		t.Fatalf("unexpected error: %v", err)
 15	}
 16	if got != gnoAddr {
 17		t.Fatalf("got %s, want %s", got, gnoAddr)
 18	}
 19}
 20
 21func TestConvertCosmosToGno(t *testing.T) {
 22	decoded := []byte{
 23		0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
 24		0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,
 25		0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
 26		0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x00,
 27	}
 28
 29	cosmosAddr := mustEncode("cosmos", decoded)
 30	wantGno := mustEncode("g", decoded)
 31
 32	got, err := ConvertCosmosToGno(cosmosAddr)
 33	if err != nil {
 34		t.Fatalf("unexpected error: %v", err)
 35	}
 36	if string(got) != wantGno {
 37		t.Fatalf("got %s, want %s", got, wantGno)
 38	}
 39
 40	// invalid bech32
 41	if _, err := ConvertCosmosToGno("not-bech32"); err == nil {
 42		t.Fatalf("expected error for invalid bech32")
 43	}
 44
 45	// wrong prefix
 46	gAddr := mustEncode("g", decoded)
 47	if _, err := ConvertCosmosToGno(gAddr); err == nil {
 48		t.Fatalf("expected error for non-cosmos prefix")
 49	}
 50}
 51
 52func TestConvertAnyToGno(t *testing.T) {
 53	payload := []byte{
 54		0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
 55		0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,
 56		0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
 57		0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x00,
 58	}
 59
 60	tests := []struct {
 61		name    string
 62		input   string
 63		want    string
 64		wantErr bool
 65	}{
 66		{
 67			name:  "cosmos→g",
 68			input: mustEncode("cosmos", payload),
 69			want:  mustEncode("g", payload),
 70		},
 71		{
 72			name:  "osmo→g",
 73			input: mustEncode("osmo", payload),
 74			want:  mustEncode("g", payload),
 75		},
 76		{
 77			name:    "invalid bech32",
 78			input:   "xyz123",
 79			wantErr: true,
 80		},
 81	}
 82
 83	for _, tc := range tests {
 84		t.Run(tc.name, func(t *testing.T) {
 85			got, err := ConvertAnyToGno(tc.input)
 86			if tc.wantErr {
 87				if err == nil {
 88					t.Fatalf("expected error, got nil")
 89				}
 90				return
 91			}
 92			if err != nil {
 93				t.Fatalf("unexpected error: %v", err)
 94			}
 95			if string(got) != tc.want {
 96				t.Fatalf("got %s, want %s", got, tc.want)
 97			}
 98		})
 99	}
100}
101
102func TestConvertGnoToAny(t *testing.T) {
103	payload := []byte{
104		0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
105		0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,
106		0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
107		0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x00,
108	}
109
110	gno := address(mustEncode("g", payload))
111
112	t.Run("g→cosmos", func(t *testing.T) {
113		got, err := ConvertGnoToAny("cosmos", gno)
114		if err != nil {
115			t.Fatalf("unexpected error: %v", err)
116		}
117		if got != mustEncode("cosmos", payload) {
118			t.Fatalf("conversion incorrect: %s", got)
119		}
120	})
121
122	t.Run("g→foobar", func(t *testing.T) {
123		got, err := ConvertGnoToAny("foobar", gno)
124		if err != nil {
125			t.Fatalf("unexpected error: %v", err)
126		}
127		if got != mustEncode("foobar", payload) {
128			t.Fatalf("conversion incorrect: %s", got)
129		}
130	})
131
132	t.Run("g→osmo", func(t *testing.T) {
133		got, err := ConvertGnoToAny("osmo", gno)
134		if err != nil {
135			t.Fatalf("unexpected error: %v", err)
136		}
137		if got != mustEncode("osmo", payload) {
138			t.Fatalf("conversion incorrect: %s", got)
139		}
140	})
141
142	t.Run("wrong source prefix", func(t *testing.T) {
143		cosmos := mustEncode("cosmos", payload)
144		if _, err := ConvertGnoToAny("g", address(cosmos)); err == nil {
145			t.Fatalf("expected error for non-g source prefix")
146		}
147	})
148
149	t.Run("invalid bech32", func(t *testing.T) {
150		if _, err := ConvertGnoToAny("cosmos", address("nope")); err == nil {
151			t.Fatalf("expected error for invalid bech32")
152		}
153	})
154}