-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinary_security_token_test.go
175 lines (155 loc) · 4.89 KB
/
binary_security_token_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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package xmlsecurity
import (
"fmt"
"testing"
"github.com/beevik/etree"
"github.com/deb-ict/go-xml"
)
func Test_BinarySecurityToken_LoadXml_InvalidElement(t *testing.T) {
// Create test case XML
testCaseXml := fmt.Sprintf(
`<wsse:InvalidTag xmlns:wsse="%s"/>`,
WsseNamespace,
)
// Prepare the test case
testCaseDocument := etree.NewDocument()
err := testCaseDocument.ReadFromString(testCaseXml)
if err != nil {
t.Fatal(err)
}
testCaseContext := xml.NewContext(testCaseDocument)
// Create test case BinarySecurityToken
testCaseBinarySecurityToken, err := NewBinarySecurityToken(testCaseContext)
if err != nil {
t.Fatal(err)
}
// Load test case BinarySecurityToken
err = testCaseBinarySecurityToken.LoadXml(testCaseContext, testCaseDocument.Root())
if err != xml.ErrInvalidElementTag {
t.Fatal(err)
}
}
func Test_BinarySecurityToken_LoadXml(t *testing.T) {
const (
id = "123"
valueType = "x509"
encodingType = "base64"
value = "cert_data"
)
// Create test case
testCase := struct {
id string
valueType string
encodingType string
value string
}{
id: id,
valueType: valueType,
encodingType: encodingType,
value: value,
}
// Create test case XML
testCaseXml := fmt.Sprintf(
`<wsse:BinarySecurityToken EncodingType="%s" ValueType="%s" wsu:Id="%s" xmlns:wsse="%s" xmlns:wsu="%s">%s</wsse:BinarySecurityToken>`,
testCase.encodingType,
testCase.valueType,
testCase.id,
WsseNamespace,
WsuNamespace,
testCase.value,
)
// Prepare the test case
testCaseDocument := etree.NewDocument()
err := testCaseDocument.ReadFromString(testCaseXml)
if err != nil {
t.Fatal(err)
}
testCaseContext := xml.NewContext(testCaseDocument)
testCaseContext.SetNamespacePrefix("wsu", WsuNamespace)
// Create test case BinarySecurityToken
testCaseBinarySecurityToken, err := NewBinarySecurityToken(testCaseContext)
if err != nil {
t.Fatal(err)
}
// Load test case BinarySecurityToken
err = testCaseBinarySecurityToken.LoadXml(testCaseContext, testCaseDocument.Root())
if err != nil {
t.Fatal(err)
}
// Validate test case BinarySecurityToken
if testCaseBinarySecurityToken.GetId() != testCase.id {
t.Fatalf("BinarySecurityToken.Id = %s; want %s", testCaseBinarySecurityToken.GetId(), testCase.id)
}
if testCaseBinarySecurityToken.GetValueType() != testCase.valueType {
t.Fatalf("BinarySecurityToken.ValueType = %s; want %s", testCaseBinarySecurityToken.GetValueType(), testCase.valueType)
}
if testCaseBinarySecurityToken.GetEncodingType() != testCase.encodingType {
t.Fatalf("BinarySecurityToken.EncodingType = %s; want %s", testCaseBinarySecurityToken.GetEncodingType(), testCase.encodingType)
}
if testCaseBinarySecurityToken.GetValue() != testCase.value {
t.Fatalf("BinarySecurityToken.Value = %s; want %s", testCaseBinarySecurityToken.GetValue(), testCase.value)
}
}
func Test_BinarySecurityToken_GetXml(t *testing.T) {
const (
id = "123"
valueType = "x509"
encodingType = "base64"
value = "cert_data"
)
// Create test case
testCase := struct {
id string
valueType string
encodingType string
value string
}{
id: id,
valueType: valueType,
encodingType: encodingType,
value: value,
}
// Create test case XML
testCaseXml := fmt.Sprintf(
`<wsse:BinarySecurityToken EncodingType="%s" ValueType="%s" wsu:Id="%s" xmlns:wsse="%s" xmlns:wsu="%s">%s</wsse:BinarySecurityToken>`,
testCase.encodingType,
testCase.valueType,
testCase.id,
WsseNamespace,
WsuNamespace,
testCase.value,
)
// Prepare the test case
testCaseDocument := etree.NewDocument()
testCaseContext := xml.NewContext(testCaseDocument)
testCaseContext.SetNamespacePrefix("wsse", WsseNamespace)
testCaseContext.SetNamespacePrefix("wsu", WsuNamespace)
// Create test case BinarySecurityToken
testCaseBinarySecurityToken, err := NewBinarySecurityToken(testCaseContext)
if err != nil {
t.Fatal(err)
}
testCaseBinarySecurityToken.SetId(testCase.id)
testCaseBinarySecurityToken.SetValueType(testCase.valueType)
testCaseBinarySecurityToken.SetEncodingType(testCase.encodingType)
testCaseBinarySecurityToken.SetValue(testCase.value)
// Get test case BinarySecurityToken XML
testCaseBinarySecurityTokenElement, err := testCaseBinarySecurityToken.GetXml(testCaseContext)
if err != nil {
t.Fatal(err)
}
// Add the namespace declarations
testCaseBinarySecurityTokenElement.CreateAttr("xmlns:wsse", WsseNamespace)
testCaseBinarySecurityTokenElement.CreateAttr("xmlns:wsu", WsuNamespace)
testCaseBinarySecurityTokenElement.SortAttrs()
// Get the test case XML
testCaseDocument.SetRoot(testCaseBinarySecurityTokenElement)
resultXml, err := testCaseDocument.WriteToString()
if err != nil {
t.Fatal(err)
}
// Validate test case BinarySecurityToken XML
if resultXml != testCaseXml {
t.Fatalf("BinarySecurityToken XML = %s; want %s", resultXml, testCaseXml)
}
}