-
Notifications
You must be signed in to change notification settings - Fork 25
/
docker_test.go
125 lines (105 loc) · 4.37 KB
/
docker_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
package beyond
import (
"encoding/json"
"fmt"
"log"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/drewolson/testflight"
"github.com/gorilla/securecookie"
"github.com/stretchr/testify/assert"
)
const dockerToken = "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6ImU3NDFhODNlODBhMzYwZWVhYmM1NDExZWE3NjE5MmM4NzdjMjdlZjJjYmZmNGQxMWQwZTExN2IyNzRjMDhkNWEifQ.eyJhY2Nlc3MiOltdLCJjb250ZXh0Ijp7ImVudGl0eV9raW5kIjoidXNlciIsImtpbmQiOiJ1c2VyIiwidmVyc2lvbiI6MiwiY29tLmFwb3N0aWxsZS5yb290IjoiJGRpc2FibGVkIiwidXNlciI6ImpvZSIsImVudGl0eV9yZWZlcmVuY2UiOiJjY2VhYmFhOS1mZmM5LTQ4MWUtOTdhZS1iZmMzYTExODMxNDAifSwiYXVkIjpudWxsLCJleHAiOjE1OTM5MTE3MzEsImlzcyI6InF1YXkiLCJpYXQiOjE1OTM5MDgxMzEsIm5iZiI6MTU5MzkwODEzMSwic3ViIjoiam9lIn0.VCZnfwtoJgpEh2U5sAHZlIJAm5pWLnwZVRoH4wnPy6jCQ4ZVw4gUNfZ4xQdBa1nDW-Zc3-iaTGCpVX12bEpaA-b98A7vzN0w6F8HCXij4QXLHGhGibxDO7k5UyPziBQCCXXB960ZVItkyttPsnCFgCPqhAwB5e3acuKKfJgtd-r8qkGXUAKIrk3zJPQvzzb4aI0poBcZh822r4hFY3BvjMlXeR4cKTzdn-96p5ZDj7zCYZanB81vVuENDhxxy_aGLwQWRp3p9GApVgcZCO2WKFDp-P7YYVpcZ5bc7ZlqWBy9RLn6wFGePAykygXwJfdkoeC2ShaHusLTNvqLMoMUYw"
var dockerHost = "docker.localhost.cogolabs.com"
func init() {
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Println(r.URL)
w.Header().Set("WWW-Authenticate", "always-overwrite")
switch r.Header.Get("Authorization") {
case "":
w.WriteHeader(401)
fmt.Fprint(w, "{\"errors\":[{\"code\":\"UNAUTHORIZED\",\"detail\":{},")
case "err":
w.WriteHeader(200)
fmt.Fprint(w, `{"token":`)
default:
w.WriteHeader(200)
fmt.Fprint(w, `{"token":"`+dockerToken+`"}`)
}
}))
testServer.URL = strings.Replace(testServer.URL, "127.0.0.1", dockerHost, -1)
*dockerBase = testServer.URL
*dockerScheme = "http"
dockerSetup(*dockerBase)
}
func TestDockerIE(t *testing.T) {
req, err := http.NewRequest("GET", "http://"+dockerHost+"/", nil)
assert.NoError(t, err)
req.Header.Set("User-Agent", "MSIE")
testMux.ServeHTTP(nil, req)
setCacheControl(nil)
jsRedirect(nil, "")
login(nil, req)
}
func TestDockerV2(t *testing.T) {
err := dockerSetup(":")
assert.Error(t, err)
testflight.WithServer(testMux, func(r *testflight.Requester) {
v2get := r.Get("/v2/auth")
assert.Equal(t, 418, v2get.StatusCode)
req, err := http.NewRequest("GET", "/v2/", nil)
assert.NoError(t, err)
req.Host = dockerHost
req.Header.Set("User-Agent", "docker/1.12.6 go/go1.7.4")
resp := r.Do(req)
assert.Equal(t, 401, resp.StatusCode)
assert.Equal(t, "", resp.Body)
// assert.Equal(t, `Bearer realm="`+*dockerBase+`/v2/auth",service="docker.colofoo.net"`, resp.Header.Get("WWW-Authenticate"))
assert.True(t, strings.HasPrefix(resp.Header.Get("WWW-Authenticate"), "Bearer realm="))
req, err = http.NewRequest("GET", "/v2/auth?account=joe&client_id=docker&offline_token=true&service=docker.colofoo.net", nil)
assert.NoError(t, err)
req.Host = dockerHost
req.SetBasicAuth("joe", "secret0")
req.Header.Set("User-Agent", "docker/1.12.6 go/go1.7.4")
resp = r.Do(req)
assert.Equal(t, 200, resp.StatusCode)
assert.True(t, strings.HasPrefix(resp.Body, "{\"token\":\""))
v := map[string]interface{}{}
err = json.Unmarshal([]byte(resp.Body), &v)
assert.NoError(t, err)
token := v["token"].(string)
assert.NotZero(t, token)
assert.True(t, len(token) > 500)
err = securecookie.DecodeMulti("token", token, &token, store.Codecs...)
assert.NoError(t, err)
assert.Equal(t, token, dockerToken)
token = v["token"].(string)
req, err = http.NewRequest("GET", "/v2/auth", nil)
assert.NoError(t, err)
req.Host = dockerHost
req.Header.Set("Authorization", "err")
req.Header.Set("User-Agent", "docker/1.12.6 go/go1.7.4")
resp = r.Do(req)
assert.Equal(t, 502, resp.StatusCode)
assert.Equal(t, "", resp.Body)
req, err = http.NewRequest("GET", "/v2/namespaces", nil)
assert.NoError(t, err)
req.Host = dockerHost
req.Header.Set("Authorization", "Bearer "+token)
req.Header.Set("User-Agent", "docker/1.12.6 go/go1.7.4")
resp = r.Do(req)
assert.Equal(t, 200, resp.StatusCode)
assert.True(t, strings.HasPrefix(resp.Body, "{\"token\":\""))
token = token[:len(token)/2]
req, err = http.NewRequest("GET", "/v2/namespaces", nil)
assert.NoError(t, err)
req.Host = dockerHost
req.Header.Set("Authorization", "Bearer "+token)
req.Header.Set("User-Agent", "docker/1.12.6 go/go1.7.4")
resp = r.Do(req)
assert.Equal(t, 401, resp.StatusCode)
assert.Equal(t, "", resp.Body)
})
}