forked from galaxydi/go-loghub
-
Notifications
You must be signed in to change notification settings - Fork 113
/
signature_test.go
110 lines (101 loc) · 2.54 KB
/
signature_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
package sls
import (
"crypto/md5"
"fmt"
"github.com/gogo/protobuf/proto"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
"testing"
)
type SignerV1Suite struct {
suite.Suite
AccessKeyID string
AccessKeySecret string
Endpoint string
signer Signer
}
func (s *SignerV1Suite) SetupTest() {
s.Endpoint = "cn-hangzhou.log.aliyuncs.com"
s.AccessKeyID = "mockAccessKeyID"
s.AccessKeySecret = "mockAccessKeySecret"
s.signer = &SignerV1{
accessKeyID: s.AccessKeyID,
accessKeySecret: s.AccessKeySecret,
}
}
func (s *SignerV1Suite) TestSignatureGet() {
headers := map[string]string{
"x-log-apiversion": "0.6.0",
"x-log-signaturemethod": "hmac-sha1",
"x-log-bodyrawsize": "0",
"Date": "Mon, 3 Jan 2010 08:33:47 GMT",
}
digest := "Rwm6cTKzoti4HWoe+GKcb6Kv07E="
expectedAuthStr := fmt.Sprintf("SLS %v:%v", s.AccessKeyID, digest)
err := s.signer.Sign("GET", "/logstores", headers, nil)
if err != nil {
assert.Fail(s.T(), err.Error())
}
auth := headers[HTTPHeaderAuthorization]
assert.Equal(s.T(), expectedAuthStr, auth)
}
func (s *SignerV1Suite) TestSignaturePost() {
/*
topic=""
time=1405409656
source="10.230.201.117"
"TestKey": "TestContent"
*/
ct := &LogContent{
Key: proto.String("TestKey"),
Value: proto.String("TestContent"),
}
lg := &Log{
Time: proto.Uint32(1405409656),
Contents: []*LogContent{
ct,
},
}
lgGrp := &LogGroup{
Topic: proto.String(""),
Source: proto.String("10.230.201.117"),
Logs: []*Log{
lg,
},
}
lgGrpLst := &LogGroupList{
LogGroups: []*LogGroup{
lgGrp,
},
}
body, err := proto.Marshal(lgGrpLst)
if err != nil {
assert.Fail(s.T(), err.Error())
}
md5Sum := fmt.Sprintf("%X", md5.Sum(body))
newLgGrpLst := &LogGroupList{}
err = proto.Unmarshal(body, newLgGrpLst)
if err != nil {
assert.Fail(s.T(), err.Error())
}
h := map[string]string{
"x-log-apiversion": "0.6.0",
"x-log-signaturemethod": "hmac-sha1",
"x-log-bodyrawsize": "50",
"Content-MD5": md5Sum,
"Content-Type": "application/x-protobuf",
"Content-Length": "50",
"Date": "Mon, 3 Jan 2010 08:33:47 GMT",
}
digest := "87xQWqFaOSewqRIma8kPjGYlXHc="
err = s.signer.Sign("GET", "/logstores/app_log", h, body)
if err != nil {
assert.Fail(s.T(), err.Error())
}
expectedAuthStr := fmt.Sprintf("SLS %v:%v", s.AccessKeyID, digest)
auth := h[HTTPHeaderAuthorization]
assert.Equal(s.T(), expectedAuthStr, auth)
}
func TestSignerV1Suite(t *testing.T) {
suite.Run(t, new(SignerV1Suite))
}