forked from alex-arce/gosoap
-
Notifications
You must be signed in to change notification settings - Fork 3
/
wsse_test.go
85 lines (77 loc) · 2.3 KB
/
wsse_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
package soap
import (
"errors"
"testing"
"github.com/m29h/xml"
"github.com/stretchr/testify/assert"
)
type newWsseAuthInfoTest struct {
name string
inCertPath string
inKeyPath string
err error
}
var newWsseAuthInfoTests = []newWsseAuthInfoTest{
{
name: "base case",
inCertPath: "./testdata/cert.pem",
inKeyPath: "./testdata/key.pem",
err: nil,
},
{
name: "invalid key file case",
inCertPath: "./testdata/cert.pem",
inKeyPath: "./testdata/badkey.pem",
err: errors.New("tls: private key does not match public key"),
},
}
func TestNewWSSEAuthInfo(t *testing.T) {
for _, tt := range newWsseAuthInfoTests {
t.Run(tt.name, func(t *testing.T) {
wsseInfo, err := NewWSSEAuthInfo(tt.inCertPath, tt.inKeyPath)
assert.Equal(t, tt.err, err)
if tt.err == nil {
assert.NotNil(t, wsseInfo)
}
})
}
}
func TestAddSignature(t *testing.T) {
wsseInfo, err := NewWSSEAuthInfo(newWsseAuthInfoTests[0].inCertPath, newWsseAuthInfoTests[0].inKeyPath)
assert.NoError(t, err)
body := ×tamp{
WsuID: "",
Created: "_",
Expires: "_",
}
err = wsseInfo.addSignature(body)
assert.NoError(t, err)
//there must be
assert.NotEqual(t, len(body.WsuID), 0)
assert.Equal(t, len(wsseInfo.sigRef), 1)
}
func TestSecurityHeader(t *testing.T) {
wsseInfo, err := NewWSSEAuthInfo(newWsseAuthInfoTests[0].inCertPath, newWsseAuthInfoTests[0].inKeyPath)
assert.NoError(t, err)
body := ×tamp{
WsuID: "",
}
secHeader, err := wsseInfo.securityHeader(body)
assert.NoError(t, err)
//there must be the WsuID set to a string of a length larger than 0 for body and timestamp
assert.NotEqual(t, len(body.WsuID), 0)
assert.NotEqual(t, len(secHeader.Timestamp.WsuID), 0)
//timestamp must be autoset to some value
assert.NotEqual(t, len(secHeader.Timestamp.Created), 0)
assert.NotEqual(t, len(secHeader.Timestamp.Expires), 0)
//the sigRef must be cleared after generating the header
assert.Equal(t, len(wsseInfo.sigRef), 0)
//the length of signed references in the header must be 2 (body+timestamp)
assert.Len(t, secHeader.Signature.SignedInfo.Reference, 2)
assert.Equal(t, secHeader.MustUnderstand, 1)
bodyEnc, err := xml.MarshalIndent(&secHeader, "", " ")
b := string(bodyEnc)
assert.NoError(t, err)
//fmt.Println(b)
assert.Contains(t, b, "</wsu:Expires>")
}