-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcryptogo_test.go
72 lines (60 loc) · 2.31 KB
/
cryptogo_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
// Copyright 2013-2014 Vasiliy Gorin. All rights reserved.
// Use of this source code is governed by a GNU-style
// license that can be found in the LICENSE file.
package cryptogo
import "testing"
import "net/http"
import "github.com/vgorin/cryptogo/pb"
import "github.com/vgorin/cryptogo/util"
func TestSignRequest(t *testing.T) {
pattern := util.NewSignaturePattern([]string{"header0, header1, header2, header3"}, nil)
request, _ := create_request()
pb.PBSignRequest(request, "password0", pattern)
if !pb.PBVerifyRequest(request, "password0", pattern) {
t.Fatal("check signature failed on the same request")
}
request_copy, _ := create_request()
pb.PBSignRequest(request_copy, "password0", pattern)
if !pb.PBVerifyRequest(request_copy, "password0", pattern) {
t.Fatal("check signature failed on the request copy")
}
modified, _ := create_request()
pb.PBSignRequest(request_copy, "password0", pattern)
modified.Header.Add("header3", "value0")
if pb.PBVerifyRequest(modified, "password0", pattern) {
t.Fatal("check signature succeeded on different requests")
}
shuffled, _ := create_request()
pb.PBSignRequest(shuffled, "password0", pattern)
shuffled.Header.Del("header0")
shuffled.Header.Add("header0", "value0")
if !pb.PBVerifyRequest(shuffled, "password0", pattern) {
t.Fatal("check signature failed on the shuffled request copy")
}
shuffled, _ = create_request()
pb.PBSignRequest(shuffled, "password0", pattern)
shuffled.Header.Del("header1")
shuffled.Header.Add("header1", "value1")
shuffled.Header.Add("header1", "value0")
if !pb.PBVerifyRequest(shuffled, "password0", pattern) {
t.Fatal("check signature failed on the shuffled request copy")
}
excluded, _ := create_request()
pb.PBSignRequest(excluded, "password0", pattern)
excluded.Header.Add("header5", "value0")
if !pb.PBVerifyRequest(excluded, "password0", pattern) {
t.Fatal("check signature failed on the excluded header request copy")
}
}
func create_request() (*http.Request, error) {
request, err := http.NewRequest("GET", "http://google.com/?query=search+me=please&pages=100", nil)
if err != nil {
return nil, err
}
request.RemoteAddr = "remote_address"
request.RequestURI = "request_uri"
request.Header.Add("header0", "value0")
request.Header.Add("header1", "value0")
request.Header.Add("header1", "value1")
return request, nil
}