-
Notifications
You must be signed in to change notification settings - Fork 0
/
authorization_test.go
158 lines (131 loc) · 3.33 KB
/
authorization_test.go
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
package httpclient
import (
"bytes"
"net/http"
"reflect"
"testing"
)
var basicChallenge = Challenge{
Scheme: "Basic",
Realm: "WallyWorld",
}
var digestChallenge1 = Challenge{
Scheme: "Digest",
Realm: "[email protected]",
Qop: []string{"auth", "auth-int"},
Nonce: "dcd98b7102dd2f0e8b11d0f600bfb0c093",
Opaque: "5ccc069c403ebaf9f0171e9517f40e41",
}
type ParseExpect struct {
Challenge string
Parsed []*Challenge
}
var parseTests = []ParseExpect{
{` Basic
realm="WallyWorld"`,
[]*Challenge{
&basicChallenge,
},
},
{` Digest
realm="[email protected]",
qop="auth,auth-int",
nonce="dcd98b7102dd2f0e8b11d0f600bfb0c093",
opaque="5ccc069c403ebaf9f0171e9517f40e41"`,
[]*Challenge{
&digestChallenge1,
},
},
{` Basic
realm="WallyWorld",
Digest
realm="[email protected]",
qop="auth,auth-int",
nonce="dcd98b7102dd2f0e8b11d0f600bfb0c093",
opaque="5ccc069c403ebaf9f0171e9517f40e41"`,
[]*Challenge{
&basicChallenge,
&digestChallenge1,
},
},
}
func TestParseChallenge(t *testing.T) {
for i, v := range parseTests {
p, err := parseChallenge(v.Challenge)
if err != nil {
t.Errorf("failed parseTests[%d]: %v", i, err)
}
if len(p) != len(v.Parsed) {
t.Errorf("failed parseTests[%d]: parsed %d instead of %d challenges: %s",
i, len(p), len(v.Parsed), v.Challenge)
}
for j, c := range v.Parsed {
if !reflect.DeepEqual(p[j], c) {
t.Errorf("parseTests[%d][%d]: not DeepEqual:\n%v\n%v\n",
i, j, p[j], c)
}
}
}
}
func TestBasicChallenge(t *testing.T) {
username := "Aladdin"
password := "open sesame"
expected := "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=="
credentials := &OrderedCredentials{[]Credential{NewCredential("example.com", "/", username, password)}}
challenge := &basicChallenge
session := NewSession(credentials, 1000, "", -1)
req, err := http.NewRequest("GET", "http://example.com/", nil)
if err != nil {
t.Fatal(err)
}
auth, err := challenge.Basic(session, req)
if err != nil {
t.Fatal(err)
}
if auth != expected {
t.Errorf("expected [%s], got [%s]", expected, auth)
}
}
type testSession struct {
Session
}
func (ts *testSession) CNonce() (string, error) {
return "0a4f113b", nil
}
func TestDigestChallenge(t *testing.T) {
username := "Mufasa"
password := "Circle Of Life"
expected := `Digest username="Mufasa", realm="[email protected]", nonce="dcd98b7102dd2f0e8b11d0f600bfb0c093", uri="/dir/index.html", qop=auth, nc=00000001, cnonce="0a4f113b", response="6629fae49393a05397450978507c4ef1", opaque="5ccc069c403ebaf9f0171e9517f40e41"`
credentials := &OrderedCredentials{[]Credential{NewCredential("host.com", "/", username, password)}}
challenge := &digestChallenge1
challenge.Qop = []string{"auth"}
session := &testSession{NewSession(credentials, 1000, "", -1)}
req, err := http.NewRequest("GET", "http://host.com/dir/index.html", nil)
if err != nil {
t.Fatal(err)
}
auth, err := challenge.Digest(session, req)
if err != nil {
t.Fatal(err)
}
if auth != expected {
t.Errorf("expected [%s], got [%s]", expected, auth)
}
}
func BenchmarkParseChallenge(b *testing.B) {
buf := &bytes.Buffer{}
for i, v := range parseTests {
if i > 0 {
buf.WriteString(", ")
}
buf.WriteString(v.Challenge)
}
s := buf.String()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := parseChallenge(s)
if err != nil {
b.Fatal(err)
}
}
}