annotate auth/auth_test.go @ 4:b8545eea86b4

Use proper separator on multipass reads.
author Paul Fisher <paul@pfish.zone>
date Sat, 24 Oct 2015 23:16:42 -0400
parents c18bc7b9d1d9
children 1c194fa9bbf4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
1 package auth
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
2
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
3 import (
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
4 "regexp"
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
5 "testing"
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
6 )
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
7
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
8 var passPattern *regexp.Regexp = regexp.MustCompile(`^(?:[a-z]{4}-){3}[a-z]{4}$`)
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
9
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
10 const basicShadow = "jhh:9999:$2a$12$tcv2MrtXgibAJHsSwVfHiOevXBFmiGy0HTNoOB8QzIhEh46iWS1uC:YW55dGhpbmcgbW9yZSB0aGFuIDUgcmVwcyBpcyBjYXJkaW8="
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
11 const extraDataShadow = "skw:1:$2a$12$lINQdYWHOcLKoqhNOr3mNOpZSAu5JOBS2F7T/VDfYn2rvv6qUJehG::additional:fields"
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
12
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
13 func TestEntryFromShadow(t *testing.T) {
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
14 cases := []struct {
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
15 shadow string
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
16 wantErr bool
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
17 username string
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
18 id uint64
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
19 hash string
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
20 description string
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
21 rest []string
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
22 }{
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
23 {
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
24 shadow: "pfish:1234:$2a$12$apFtWGXKtWBavVy5eo.22Ohs43GudT5IYTqyQkIBX9LpS7YtvKBpa",
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
25 username: "pfish",
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
26 id: 1234,
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
27 hash: "$2a$12$apFtWGXKtWBavVy5eo.22Ohs43GudT5IYTqyQkIBX9LpS7YtvKBpa",
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
28 },
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
29 {
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
30 shadow: basicShadow,
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
31 username: "jhh",
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
32 id: 9999,
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
33 hash: "$2a$12$tcv2MrtXgibAJHsSwVfHiOevXBFmiGy0HTNoOB8QzIhEh46iWS1uC",
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
34 description: "anything more than 5 reps is cardio",
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
35 },
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
36 {
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
37 shadow: extraDataShadow,
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
38 username: "skw",
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
39 id: 1,
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
40 hash: "$2a$12$lINQdYWHOcLKoqhNOr3mNOpZSAu5JOBS2F7T/VDfYn2rvv6qUJehG",
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
41 rest: []string{"additional", "fields"},
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
42 },
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
43 {
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
44 shadow: "user:one:bogushash",
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
45 wantErr: true,
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
46 },
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
47 {
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
48 shadow: "user:-1:bogushash",
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
49 wantErr: true,
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
50 },
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
51 {
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
52 shadow: "tooshort",
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
53 wantErr: true,
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
54 },
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
55 {
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
56 shadow: "test:0:bogushash:invalid base64",
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
57 wantErr: true,
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
58 },
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
59 }
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
60 for _, c := range cases {
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
61 entry, err := EntryFromShadow(c.shadow)
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
62 if c.wantErr {
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
63 if err == nil {
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
64 t.Errorf("EntryFromShadow(%q) == _, nil; want non-nil err", c.shadow)
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
65 }
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
66 continue
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
67 }
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
68 if err != nil {
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
69 t.Errorf("EntryFromShadow(%q) == _, %q; want nil err", c.shadow, err)
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
70 }
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
71 if c.username != entry.username {
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
72 t.Errorf("EntryFromShadow(%q).username = %q; want %q", c.shadow, entry.username, c.username)
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
73 }
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
74 if c.hash != string(entry.hash) {
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
75 t.Errorf("EntryFromShadow(%q).password = %q; want %q", c.shadow, entry.hash, c.hash)
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
76 }
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
77 if c.description != entry.description {
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
78 t.Errorf("EntryFromShadow(%q).description = %q; want %q", c.shadow, entry.description, c.description)
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
79 }
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
80 restEqual := false
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
81 if len(c.rest) == len(entry.rest) {
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
82 restEqual = true
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
83 for i := range c.rest {
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
84 if c.rest[i] != entry.rest[i] {
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
85 restEqual = false
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
86 break
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
87 }
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
88 }
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
89 }
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
90 if !restEqual {
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
91 t.Errorf("EntryFromShadow(%q).rest = %q; want %q", c.shadow, entry.rest, c.rest)
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
92 }
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
93 }
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
94 }
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
95
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
96 func TestNewEntry(t *testing.T) {
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
97 cases := []struct {
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
98 username string
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
99 description string
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
100 wantErr bool
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
101 }{
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
102 {"pfish", "one", false},
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
103 {"pfish", "the other", false},
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
104 {"with:colons", "", true},
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
105 {"pfish", string(make([]byte, 1000)), true},
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
106 }
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
107 for _, c := range cases {
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
108 entry, password, err := NewEntry(c.username, c.description)
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
109 if c.wantErr {
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
110 if err == nil {
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
111 t.Errorf("NewEntry(%q, %q) = _, _, nil; want non-nil err", c.username, c.description)
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
112 }
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
113 continue
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
114 }
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
115 if err != nil {
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
116 t.Errorf("NewEntry(%q, %q) = _, _, %q; want nil err", c.username, c.description, err)
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
117 }
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
118 if c.username != entry.username {
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
119 t.Errorf("NewEntry(%q, %q).username = %q, want %q",
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
120 c.username, c.description, entry.username, c.username)
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
121 }
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
122 if entry.id == 0 {
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
123 // This test has a 1/(2**64) chance of failing! :o
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
124 t.Errorf("NewEntry(_, _).id == 0, want nonzero")
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
125 }
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
126 if c.description != entry.description {
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
127 t.Errorf("NewEntry(%q, %q).description = %q, want %q",
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
128 c.username, c.description, entry.description, c.description)
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
129 }
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
130 if !passPattern.MatchString(password) {
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
131 t.Errorf("NewEntry(_, _) = _, %q, _; wanted to match xxxx-xxxx-xxxx-xxxx", password)
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
132 }
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
133 if !entry.Authenticate(c.username, password) {
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
134 t.Errorf("NewEntry(%q, %q).Authenticate(%q, %q) failed",
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
135 c.username, c.description, c.username, password)
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
136 }
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
137 }
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
138 }
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
139
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
140 func TestGenPassword(t *testing.T) {
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
141 p := genPassword()
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
142 if !passPattern.MatchString(string(p)) {
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
143 t.Errorf("genPassword() = %q; wanted to match xxxx-xxxx-xxxx-xxxx", p)
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
144 }
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
145 }
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
146
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
147 func TestAuthenticate(t *testing.T) {
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
148 entry, password, err := NewEntry("pfish", "")
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
149 if err != nil {
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
150 t.Errorf("Error building entry")
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
151 }
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
152 type testcase struct {
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
153 username, password string
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
154 want bool
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
155 }
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
156
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
157 cases := []testcase{
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
158 {"pfish", password, true},
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
159 {"jhh", password, false},
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
160 {"pfish", "not the password", false},
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
161 {"jhh", "not the password", false},
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
162 }
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
163 for _, c := range cases {
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
164 got := entry.Authenticate(c.username, c.password)
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
165 if got != c.want {
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
166 t.Errorf("entry.Authenticate(%q, %q) == %q, want %q",
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
167 c.username, c.password, got, c.want)
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
168 }
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
169 }
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
170
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
171 entry, err = EntryFromShadow(basicShadow)
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
172 if err != nil {
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
173 t.Errorf("Error loading valid shadow")
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
174 }
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
175
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
176 cases = []testcase{
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
177 {"jhh", "nocardio", true},
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
178 {"pfish", "nocardio", false},
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
179 {"jhh", "not the password", false},
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
180 {"pfish", "not the password", false},
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
181 }
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
182 for _, c := range cases {
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
183 got := entry.Authenticate(c.username, c.password)
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
184 if got != c.want {
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
185 t.Errorf("entry.Authenticate(%q, %q) == %q, want %q",
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
186 c.username, c.password, got, c.want)
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
187 }
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
188 }
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
189 }
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
190
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
191 func testMatchesID(t *testing.T) {
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
192 entry, err := EntryFromShadow(basicShadow)
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
193 if err != nil {
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
194 t.Errorf("Error loading valid shadow")
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
195 }
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
196 cases := []struct {
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
197 username string
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
198 id uint64
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
199 want bool
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
200 }{
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
201 {"jhh", 1234, true},
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
202 {"pfish", 1234, false},
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
203 {"jhh", 9999, false},
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
204 {"pfish", 9999, false},
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
205 }
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
206 for _, c := range cases {
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
207 got := entry.MatchesID(c.username, c.id)
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
208 if got != c.want {
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
209 t.Errorf("entry.MatchesID(%q, %q) == %q, want %q",
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
210 c.username, c.id, got, c.want)
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
211 }
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
212 }
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
213 }
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
214
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
215 func TestEncode(t *testing.T) {
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
216 // Crafted entry
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
217 shadowed, err := EntryFromShadow(basicShadow)
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
218 if err != nil {
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
219 t.Errorf("Error loading valid shadow")
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
220 }
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
221 extraShadowed, err := EntryFromShadow(extraDataShadow)
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
222 if err != nil {
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
223 t.Errorf("Error loading valid shadow")
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
224 }
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
225 cases := []struct {
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
226 entry *Entry
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
227 want string
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
228 }{
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
229 {
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
230 &Entry{
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
231 username: "testuser",
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
232 id: 6775,
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
233 hash: "bogushash",
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
234 description: "something",
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
235 },
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
236 "testuser:6775:bogushash:c29tZXRoaW5n",
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
237 },
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
238 {
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
239 &Entry{
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
240 username: "testuser",
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
241 id: 6775,
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
242 hash: "bogushash",
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
243 description: "something",
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
244 rest: []string{"a", "B"},
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
245 },
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
246 "testuser:6775:bogushash:c29tZXRoaW5n:a:B",
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
247 },
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
248 {shadowed, basicShadow},
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
249 {extraShadowed, extraDataShadow},
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
250 }
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
251 for _, c := range cases {
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
252 got := string(c.entry.Encode())
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
253 if got != c.want {
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
254 t.Errorf("entry.Encode() = %q, want %q", got, c.want)
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
255 }
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
256 }
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
257 }