-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathid_test.go
More file actions
164 lines (151 loc) · 4.28 KB
/
id_test.go
File metadata and controls
164 lines (151 loc) · 4.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package trn
import (
"fmt"
"strings"
"testing"
)
func TestValidity(t *testing.T) {
c := []struct {
t TRN
eo bool
}{
{TRN(`trn:partition:content:region:account:prefix/id`), true},
{TRN(`trn:partition:content:region:account`), false}, // not enough components
{TRN(`:partition:content:region:account:prefix/id`), false}, // missing preamble
{TRN(`trn:partition:content:region:account:prefix/id:extra`), true}, // extra components are treated as part of id
}
for _, e := range c {
if IsValid(e.t) != e.eo {
t.Errorf("case %v expected: %v, got: %v", e.t, e.eo, !e.eo)
}
}
}
// TOOD test for invalid input
func TestNewTRN(t *testing.T) {
c := []struct {
p string
s string
r string
a string
pre string
prefix string
eo error
}{
{``, ``, ``, ``, ``, `trn:::::/`, nil},
{`topple`, ``, ``, ``, ``, `trn:topple::::/`, nil},
{``, `content`, ``, ``, ``, `trn::content:::/`, nil},
{``, ``, `us-west`, ``, ``, `trn:::us-west::/`, nil},
{``, ``, ``, `1234`, ``, `trn::::1234:/`, nil},
{``, ``, ``, ``, `prefix`, `trn:::::prefix/`, nil},
{`topple`, `content`, `us-west`, `1234`, `prefix`, `trn:topple:content:us-west:1234:prefix/`, nil},
}
for _, e := range c {
o := NewTRN(e.p, e.s, e.r, e.a, e.pre)
if !strings.HasPrefix(string(o), e.prefix) {
t.Errorf("ContentID for (%v, %v, %v, %v) was incorrect, got: %v wanted w/ prefix: %v", e.p, e.r, e.a, e.pre, o, e.prefix)
}
}
}
func TestGenerateServiceInvite(t *testing.T) {
o := NewTRN(`test`, `account`, `local`, ``, `/serviceinvite`)
printTRN("Random ServiceInvite", o)
o = NewTRN(`test`, `account`, `local`, ``, `/invite`)
printTRN("Random Invite", o)
o = NewTRN(`test`, `account`, `local`, ``, `/discount`)
printTRN("Random Discount", o)
o = NewTRN(`test`, `account`, `local`, `1`, `/user`)
printTRN("Random User", o)
}
func printTRN(note string, o TRN) {
fmt.Printf("%s: %s\n\t%s\t%s\t%s\t%s\t%s\n", note, o.Encode(), o.Partition(), o.Service(), o.Region(), o.Account(), o.Resource())
}
func TestEncoding(t *testing.T) {
i := NewTRN(`topple`, `content`, `us-west`, `1234`, `prefix`)
encoded := i.Encode()
o, err := Decode(encoded)
if err != nil {
t.Error(err)
}
if o != i {
t.Error(`decoded TRN does not match original`)
}
}
func TestDecodeShortcut(t *testing.T) {
rawCorrect := `trn:::::f`
r1, err := Decode(rawCorrect)
if err != nil {
t.Error(err)
}
if r1.Resource() != `f` {
t.Errorf(`unexpected Resource value, %s`, r1.Resource())
}
rawShort := `trn:::`
if _, err = Decode(rawShort); err == nil {
t.Errorf(`missing expected error`)
}
}
func TestDecodeFailure(t *testing.T) {
i := `not a b32 trn`
_, err := Decode(i)
if err == nil {
t.Error(`apparently decoded a non-base32 non-trn string into a TRN`)
}
}
func TestComponentReads(t *testing.T) {
i := NewTRN(`topple`, `content`, `us-west`, `1234`, `prefix`)
id, p, s, r, a, _ := i.Components()
if id != `trn` {
t.Error(`incorrect ID from Components, expected trn`)
}
if p != `topple` {
t.Errorf(`incorrect partition from Components %v`, p)
}
if s != `content` {
t.Errorf(`incorrect service from Components %v`, s)
}
if r != `us-west` {
t.Errorf(`incorrect region from Components %v`, r)
}
if a != `1234` {
t.Errorf(`incorrect account from Components %v`, a)
}
id = i.ID()
p = i.Partition()
s = i.Service()
r = i.Region()
a = i.Account()
if id != `trn` {
t.Error(`incorrect ID, expected trn`)
}
if p != `topple` {
t.Errorf(`incorrect partition %v`, p)
}
if s != `content` {
t.Errorf(`incorrect service %v`, s)
}
if r != `us-west` {
t.Errorf(`incorrect region %v`, r)
}
if a != `1234` {
t.Errorf(`incorrect account %v`, a)
}
}
func TestParseServiceIdentifier(t *testing.T) {
o, err := ParseServiceIdentifier(`metadata`)
if err != nil || o != Metadata {
t.Errorf(`incorrectly parsed the SI for "metadata" err: %v o: %v`, err, o)
}
o, err = ParseServiceIdentifier(`content`)
if err != nil || o != Content {
t.Errorf(`incorrectly parsed the SI for "content" err: %v o: %v`, err, o)
}
o, err = ParseServiceIdentifier(``)
if err == nil {
t.Errorf(`failed to return error on parsing empty string o: %v`, o)
}
}
func TestSIString(t *testing.T) {
if Metadata.String() != `metadata` {
t.Errorf(`incorrect name for metadata service String(): %v`, Metadata.String())
}
}