2015-05-21 18:14:46 +00:00
|
|
|
package auth
|
2015-05-07 20:16:52 +00:00
|
|
|
|
|
|
|
import (
|
2016-02-26 22:18:09 +00:00
|
|
|
"fmt"
|
2015-05-07 20:16:52 +00:00
|
|
|
"net/http"
|
2016-02-26 22:18:09 +00:00
|
|
|
"net/url"
|
|
|
|
"strings"
|
2016-08-23 14:09:18 +00:00
|
|
|
"sync"
|
2016-08-24 23:34:36 +00:00
|
|
|
"testing"
|
2015-05-07 20:16:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestAuthChallengeParse(t *testing.T) {
|
|
|
|
header := http.Header{}
|
|
|
|
header.Add("WWW-Authenticate", `Bearer realm="https://auth.example.com/token",service="registry.example.com",other=fun,slashed="he\"\l\lo"`)
|
|
|
|
|
|
|
|
challenges := parseAuthHeader(header)
|
|
|
|
if len(challenges) != 1 {
|
|
|
|
t.Fatalf("Unexpected number of auth challenges: %d, expected 1", len(challenges))
|
|
|
|
}
|
2015-05-21 18:14:46 +00:00
|
|
|
challenge := challenges[0]
|
2015-05-07 20:16:52 +00:00
|
|
|
|
2015-05-11 18:31:22 +00:00
|
|
|
if expected := "bearer"; challenge.Scheme != expected {
|
|
|
|
t.Fatalf("Unexpected scheme: %s, expected: %s", challenge.Scheme, expected)
|
2015-05-07 20:16:52 +00:00
|
|
|
}
|
|
|
|
|
2015-05-11 18:31:22 +00:00
|
|
|
if expected := "https://auth.example.com/token"; challenge.Parameters["realm"] != expected {
|
|
|
|
t.Fatalf("Unexpected param: %s, expected: %s", challenge.Parameters["realm"], expected)
|
2015-05-07 20:16:52 +00:00
|
|
|
}
|
|
|
|
|
2015-05-11 18:31:22 +00:00
|
|
|
if expected := "registry.example.com"; challenge.Parameters["service"] != expected {
|
|
|
|
t.Fatalf("Unexpected param: %s, expected: %s", challenge.Parameters["service"], expected)
|
2015-05-07 20:16:52 +00:00
|
|
|
}
|
|
|
|
|
2015-05-11 18:31:22 +00:00
|
|
|
if expected := "fun"; challenge.Parameters["other"] != expected {
|
|
|
|
t.Fatalf("Unexpected param: %s, expected: %s", challenge.Parameters["other"], expected)
|
2015-05-07 20:16:52 +00:00
|
|
|
}
|
|
|
|
|
2015-05-11 18:31:22 +00:00
|
|
|
if expected := "he\"llo"; challenge.Parameters["slashed"] != expected {
|
|
|
|
t.Fatalf("Unexpected param: %s, expected: %s", challenge.Parameters["slashed"], expected)
|
2015-05-07 20:16:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2016-02-26 22:18:09 +00:00
|
|
|
|
|
|
|
func TestAuthChallengeNormalization(t *testing.T) {
|
|
|
|
testAuthChallengeNormalization(t, "reg.EXAMPLE.com")
|
|
|
|
testAuthChallengeNormalization(t, "bɿɒʜɔiɿ-ɿɘƚƨim-ƚol-ɒ-ƨʞnɒʜƚ.com")
|
2016-09-01 21:46:31 +00:00
|
|
|
testAuthChallengeNormalization(t, "reg.example.com:80")
|
2016-08-23 14:09:18 +00:00
|
|
|
testAuthChallengeConcurrent(t, "reg.EXAMPLE.com")
|
2016-02-26 22:18:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func testAuthChallengeNormalization(t *testing.T, host string) {
|
|
|
|
|
|
|
|
scm := NewSimpleChallengeManager()
|
|
|
|
|
|
|
|
url, err := url.Parse(fmt.Sprintf("http://%s/v2/", host))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := &http.Response{
|
|
|
|
Request: &http.Request{
|
|
|
|
URL: url,
|
|
|
|
},
|
|
|
|
Header: make(http.Header),
|
|
|
|
StatusCode: http.StatusUnauthorized,
|
|
|
|
}
|
|
|
|
resp.Header.Add("WWW-Authenticate", fmt.Sprintf("Bearer realm=\"https://%s/token\",service=\"registry.example.com\"", host))
|
|
|
|
|
|
|
|
err = scm.AddResponse(resp)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
lowered := *url
|
|
|
|
lowered.Host = strings.ToLower(lowered.Host)
|
2016-09-19 21:14:07 +00:00
|
|
|
lowered.Host = canonicalAddr(&lowered)
|
2016-02-26 22:18:09 +00:00
|
|
|
c, err := scm.GetChallenges(lowered)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(c) == 0 {
|
|
|
|
t.Fatal("Expected challenge for lower-cased-host URL")
|
|
|
|
}
|
|
|
|
}
|
2016-08-23 14:09:18 +00:00
|
|
|
|
|
|
|
func testAuthChallengeConcurrent(t *testing.T, host string) {
|
|
|
|
|
|
|
|
scm := NewSimpleChallengeManager()
|
|
|
|
|
|
|
|
url, err := url.Parse(fmt.Sprintf("http://%s/v2/", host))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := &http.Response{
|
|
|
|
Request: &http.Request{
|
|
|
|
URL: url,
|
|
|
|
},
|
|
|
|
Header: make(http.Header),
|
|
|
|
StatusCode: http.StatusUnauthorized,
|
|
|
|
}
|
|
|
|
resp.Header.Add("WWW-Authenticate", fmt.Sprintf("Bearer realm=\"https://%s/token\",service=\"registry.example.com\"", host))
|
|
|
|
var s sync.WaitGroup
|
|
|
|
s.Add(2)
|
|
|
|
go func() {
|
2016-08-26 01:41:54 +00:00
|
|
|
defer s.Done()
|
2016-08-24 23:34:36 +00:00
|
|
|
for i := 0; i < 200; i++ {
|
2016-08-23 14:09:18 +00:00
|
|
|
err = scm.AddResponse(resp)
|
|
|
|
if err != nil {
|
2016-08-26 01:41:54 +00:00
|
|
|
t.Error(err)
|
2016-08-23 14:09:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
go func() {
|
2016-08-26 01:41:54 +00:00
|
|
|
defer s.Done()
|
2016-08-23 14:09:18 +00:00
|
|
|
lowered := *url
|
|
|
|
lowered.Host = strings.ToLower(lowered.Host)
|
2016-08-24 23:34:36 +00:00
|
|
|
for k := 0; k < 200; k++ {
|
2016-08-23 14:09:18 +00:00
|
|
|
_, err := scm.GetChallenges(lowered)
|
|
|
|
if err != nil {
|
2016-08-26 01:41:54 +00:00
|
|
|
t.Error(err)
|
2016-08-23 14:09:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
s.Wait()
|
|
|
|
}
|