-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathheader_test.go
More file actions
76 lines (71 loc) · 1.75 KB
/
header_test.go
File metadata and controls
76 lines (71 loc) · 1.75 KB
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
// Copyright 2016 Lars Wiegman. All rights reserved. Use of this source code is
// governed by a BSD-style license that can be found in the LICENSE file.
package multipass
import (
"bytes"
"crypto/rand"
"crypto/rsa"
"fmt"
"net/http"
"testing"
)
func TestConcatonateHeader(t *testing.T) {
tests := []struct {
header map[string][]string
expect []byte
}{
{
header: map[string][]string{
"a": {"a2", "a1"},
"b": {"b1A", "b2", "b1B"},
" B ": {"b3"},
},
expect: []byte("a:a1,a2\nb:b1A,b1B,b2,b3"),
},
}
for i, test := range tests {
got, want := ConcatonateHeader(test.header), test.expect
if n := bytes.Compare(got, want); n != 0 {
t.Errorf("test #%d; want %s, got %s", i, want, got)
}
}
}
func TestVerifySignedHeaderFail(t *testing.T) {
pk, err := rsa.GenerateKey(rand.Reader, DefaultKeySize)
if err != nil {
t.Error(err)
}
tests := []struct {
params string
}{
{""},
{"="},
{"algo=1"},
{fmt.Sprintf("algo=%s", DefaultAlgo)},
{fmt.Sprintf("algo=%s; digest=%s", DefaultAlgo, DefaultDigest)},
{fmt.Sprintf("algo=%s; digest=%s; signature=\"NDI=\"", DefaultAlgo, DefaultDigest)},
}
for i, test := range tests {
h := make(http.Header)
h.Set("Multipass-Signature", test.params)
err := VerifySignedHeader(h, &pk.PublicKey)
if err == nil {
t.Errorf("test #%d; want %s, got %s", i, ErrInvalidSignature, err)
}
}
}
func TestSignAndVerifySignatureHander(t *testing.T) {
h := make(http.Header)
h.Add("Multipass-Handle", "leeloo@dallas")
h.Add("Multipass-Origin", "127.0.0.2")
pk, err := rsa.GenerateKey(rand.Reader, DefaultKeySize)
if err != nil {
t.Error(err)
}
if err := SignHeader(h, pk); err != nil {
t.Error(err)
}
if err := VerifySignedHeader(h, &pk.PublicKey); err != nil {
t.Error(err)
}
}