Mercurial > go > multipass
comparison file/auth_test.go @ 18:00d30c67b56d
Put all the library stuff into multipass/file.
author | Paul Fisher <paul@pfish.zone> |
---|---|
date | Sun, 01 Nov 2015 12:16:51 -0500 |
parents | auth/auth_test.go@bfc035bd5132 |
children |
comparison
equal
deleted
inserted
replaced
17:342f63116bfd | 18:00d30c67b56d |
---|---|
1 package file | |
2 | |
3 import ( | |
4 "regexp" | |
5 "testing" | |
6 ) | |
7 | |
8 var passPattern *regexp.Regexp = regexp.MustCompile(`^(?:[a-z]{4}-){3}[a-z]{4}$`) | |
9 | |
10 const basicShadow = "9999:$2a$12$tcv2MrtXgibAJHsSwVfHiOevXBFmiGy0HTNoOB8QzIhEh46iWS1uC:YW55dGhpbmcgbW9yZSB0aGFuIDUgcmVwcyBpcyBjYXJkaW8=" | |
11 const anotherShadow = "1:$2a$12$lINQdYWHOcLKoqhNOr3mNOpZSAu5JOBS2F7T/VDfYn2rvv6qUJehG:" | |
12 | |
13 func TestEntryFromShadow(t *testing.T) { | |
14 cases := []struct { | |
15 shadow string | |
16 wantErr bool | |
17 username string | |
18 id uint64 | |
19 hash string | |
20 description string | |
21 rest []string | |
22 }{ | |
23 { | |
24 shadow: "1234:$2a$12$apFtWGXKtWBavVy5eo.22Ohs43GudT5IYTqyQkIBX9LpS7YtvKBpa:", | |
25 id: 1234, | |
26 hash: "$2a$12$apFtWGXKtWBavVy5eo.22Ohs43GudT5IYTqyQkIBX9LpS7YtvKBpa", | |
27 }, | |
28 { | |
29 shadow: basicShadow, | |
30 id: 9999, | |
31 hash: "$2a$12$tcv2MrtXgibAJHsSwVfHiOevXBFmiGy0HTNoOB8QzIhEh46iWS1uC", | |
32 description: "anything more than 5 reps is cardio", | |
33 }, | |
34 { | |
35 shadow: anotherShadow, | |
36 id: 1, | |
37 hash: "$2a$12$lINQdYWHOcLKoqhNOr3mNOpZSAu5JOBS2F7T/VDfYn2rvv6qUJehG", | |
38 }, | |
39 { | |
40 shadow: "one:bogushash:", | |
41 wantErr: true, | |
42 }, | |
43 { | |
44 shadow: "-1:bogushash:", | |
45 wantErr: true, | |
46 }, | |
47 { | |
48 shadow: "0:tooshort", | |
49 wantErr: true, | |
50 }, | |
51 { | |
52 shadow: "0:bogushash:invalid base64", | |
53 wantErr: true, | |
54 }, | |
55 { | |
56 shadow: "1:bogushash::more things", | |
57 wantErr: true, | |
58 }, | |
59 } | |
60 for _, c := range cases { | |
61 entry, err := EntryFromShadow(c.shadow) | |
62 if c.wantErr { | |
63 if err == nil { | |
64 t.Errorf("EntryFromShadow(%q) == _, nil; want non-nil err", c.shadow) | |
65 } | |
66 continue | |
67 } | |
68 if err != nil { | |
69 t.Errorf("EntryFromShadow(%q) == _, %q; want nil err", c.shadow, err) | |
70 } | |
71 if c.id != entry.id { | |
72 t.Errorf("EntryFromShadow(%q).id = %q; want %q", c.shadow, entry.id, c.id) | |
73 } | |
74 if c.hash != string(entry.hash) { | |
75 t.Errorf("EntryFromShadow(%q).password = %q; want %q", c.shadow, entry.hash, c.hash) | |
76 } | |
77 if c.description != entry.description { | |
78 t.Errorf("EntryFromShadow(%q).description = %q; want %q", c.shadow, entry.description, c.description) | |
79 } | |
80 } | |
81 } | |
82 | |
83 func TestNewEntry(t *testing.T) { | |
84 cases := []struct { | |
85 description string | |
86 wantErr bool | |
87 }{ | |
88 {"one", false}, | |
89 {"the other", false}, | |
90 {string(make([]byte, 1000)), true}, | |
91 } | |
92 for _, c := range cases { | |
93 entry, password, err := NewEntry(c.description) | |
94 if c.wantErr { | |
95 if err == nil { | |
96 t.Errorf("NewEntry(%q) = _, _, nil; want non-nil err", c.description) | |
97 } | |
98 continue | |
99 } | |
100 if err != nil { | |
101 t.Errorf("NewEntry(%q) = _, _, %q; want nil err", c.description, err) | |
102 } | |
103 if entry.id == 0 { | |
104 // This test has a 1/(2**64) chance of failing! :o | |
105 t.Errorf("NewEntry(_).id == 0, want nonzero") | |
106 } | |
107 if c.description != entry.description { | |
108 t.Errorf("NewEntry(%q).description = %q, want %q", | |
109 c.description, entry.description, c.description) | |
110 } | |
111 if !passPattern.MatchString(password) { | |
112 t.Errorf("NewEntry(_) = _, %q, _; wanted to match xxxx-xxxx-xxxx-xxxx", password) | |
113 } | |
114 if !entry.Authenticate(password) { | |
115 t.Errorf("NewEntry(%q).Authenticate(%q) failed", | |
116 c.description, password) | |
117 } | |
118 } | |
119 } | |
120 | |
121 func TestGenPassword(t *testing.T) { | |
122 p := genPassword() | |
123 if !passPattern.MatchString(string(p)) { | |
124 t.Errorf("genPassword() = %q; wanted to match xxxx-xxxx-xxxx-xxxx", p) | |
125 } | |
126 } | |
127 | |
128 func TestAuthenticate(t *testing.T) { | |
129 entry, password, err := NewEntry("") | |
130 if err != nil { | |
131 t.Errorf("Error building entry") | |
132 } | |
133 type testcase struct { | |
134 password string | |
135 want bool | |
136 } | |
137 | |
138 cases := []testcase{ | |
139 {password, true}, | |
140 {"not the password", false}, | |
141 } | |
142 for _, c := range cases { | |
143 got := entry.Authenticate(c.password) | |
144 if got != c.want { | |
145 t.Errorf("entry.Authenticate(%q) == %v, want %v", | |
146 c.password, got, c.want) | |
147 } | |
148 } | |
149 | |
150 entry, err = EntryFromShadow(basicShadow) | |
151 if err != nil { | |
152 t.Errorf("Error loading valid shadow") | |
153 } | |
154 | |
155 cases = []testcase{ | |
156 {"nocardio", true}, | |
157 {"not the password", false}, | |
158 } | |
159 for _, c := range cases { | |
160 got := entry.Authenticate(c.password) | |
161 if got != c.want { | |
162 t.Errorf("entry.Authenticate(%q) == %v, want %v", | |
163 c.password, got, c.want) | |
164 } | |
165 } | |
166 } | |
167 | |
168 func TestEncode(t *testing.T) { | |
169 // Crafted entry | |
170 shadowed, err := EntryFromShadow(basicShadow) | |
171 if err != nil { | |
172 t.Errorf("Error loading valid shadow") | |
173 } | |
174 anotherShadowed, err := EntryFromShadow(anotherShadow) | |
175 if err != nil { | |
176 t.Errorf("Error loading valid shadow") | |
177 } | |
178 cases := []struct { | |
179 entry *Entry | |
180 want string | |
181 }{ | |
182 { | |
183 &Entry{ | |
184 id: 6775, | |
185 hash: "bogushash", | |
186 description: "something", | |
187 }, | |
188 "6775:bogushash:c29tZXRoaW5n", | |
189 }, | |
190 {shadowed, basicShadow}, | |
191 {anotherShadowed, anotherShadow}, | |
192 } | |
193 for _, c := range cases { | |
194 got := string(c.entry.Encode()) | |
195 if got != c.want { | |
196 t.Errorf("entry.Encode() = %q, want %q", got, c.want) | |
197 } | |
198 } | |
199 } |