...
1 package htpasswd
2
3 import (
4 "fmt"
5 "reflect"
6 "strings"
7 "testing"
8 )
9
10 func TestParseHTPasswd(t *testing.T) {
11
12 for _, tc := range []struct {
13 desc string
14 input string
15 err error
16 entries map[string][]byte
17 }{
18 {
19 desc: "basic example",
20 input: `
21 # This is a comment in a basic example.
22 bilbo:{SHA}5siv5c0SHx681xU6GiSx9ZQryqs=
23 frodo:$2y$05$926C3y10Quzn/LnqQH86VOEVh/18T6RnLaS.khre96jLNL/7e.K5W
24 MiShil:$2y$05$0oHgwMehvoe8iAWS8I.7l.KoECXrwVaC16RPfaSCU5eVTFrATuMI2
25 DeokMan:공주님
26 `,
27 entries: map[string][]byte{
28 "bilbo": []byte("{SHA}5siv5c0SHx681xU6GiSx9ZQryqs="),
29 "frodo": []byte("$2y$05$926C3y10Quzn/LnqQH86VOEVh/18T6RnLaS.khre96jLNL/7e.K5W"),
30 "MiShil": []byte("$2y$05$0oHgwMehvoe8iAWS8I.7l.KoECXrwVaC16RPfaSCU5eVTFrATuMI2"),
31 "DeokMan": []byte("공주님"),
32 },
33 },
34 {
35 desc: "ensures comments are filtered",
36 input: `
37 # asdf:asdf
38 `,
39 },
40 {
41 desc: "ensure midline hash is not comment",
42 input: `
43 asdf:as#df
44 `,
45 entries: map[string][]byte{
46 "asdf": []byte("as#df"),
47 },
48 },
49 {
50 desc: "ensure midline hash is not comment",
51 input: `
52 # A valid comment
53 valid:entry
54 asdf
55 `,
56 err: fmt.Errorf(`htpasswd: invalid entry at line 4: "asdf"`),
57 },
58 } {
59
60 entries, err := parseHTPasswd(strings.NewReader(tc.input))
61 if err != tc.err {
62 if tc.err == nil {
63 t.Fatalf("%s: unexpected error: %v", tc.desc, err)
64 } else {
65 if err.Error() != tc.err.Error() {
66 t.Fatalf("%s: expected error not returned: %v != %v", tc.desc, err, tc.err)
67 }
68 }
69 }
70
71 if tc.err != nil {
72 continue
73 }
74
75
76 if tc.entries == nil {
77 tc.entries = map[string][]byte{}
78 }
79
80 if !reflect.DeepEqual(entries, tc.entries) {
81 t.Fatalf("%s: entries not parsed correctly: %v != %v", tc.desc, entries, tc.entries)
82 }
83 }
84
85 }
86
View as plain text