forked from lestrrat-go/libxml2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xsd_test.go
164 lines (148 loc) · 3.82 KB
/
xsd_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
package libxml2_test
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
"github.com/lestrrat-go/libxml2"
"github.com/lestrrat-go/libxml2/xsd"
"github.com/stretchr/testify/assert"
)
func TestXSD(t *testing.T) {
xsdfile := filepath.Join("test", "xmldsig-core-schema.xsd")
f, err := os.Open(xsdfile)
if !assert.NoError(t, err, "open schema") {
return
}
defer f.Close()
buf, err := ioutil.ReadAll(f)
if !assert.NoError(t, err, "reading from schema") {
return
}
s, err := xsd.Parse(buf)
if !assert.NoError(t, err, "parsing schema") {
return
}
defer s.Free()
func() {
d, err := libxml2.ParseString(`<foo></foo>`)
if !assert.NoError(t, err, "parsing XML") {
return
}
defer d.Free()
err = s.Validate(d)
if !assert.Error(t, err, "s.Validate should fail") {
return
}
serr, ok := err.(xsd.SchemaValidationError)
if !assert.True(t, ok, "error is xsd.SchemaValidationErr") {
return
}
if !assert.Len(t, serr.Errors(), 1, "there's one error") {
return
}
for _, e := range serr.Errors() {
t.Logf("err (OK): '%s'", e)
}
}()
func() {
const src = `<?xml version="1.0" encoding="UTF-8"?>
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
<SignedInfo>
<CanonicalizationMethod
Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-
20010315#WithComments"/>
<SignatureMethod Algorithm="http://www.w3.org/2000/09/
xmldsig#dsa-sha1"/>
<Reference URI="">
<Transforms>
<Transform Algorithm="http://www.w3.org/2000/09/
xmldsig#enveloped-signature"/>
</Transforms>
<DigestMethod Algorithm="http://www.w3.org/2000/09/
xmldsig#sha1"/>
<DigestValue>uooqbWYa5VCqcJCbuymBKqm17vY=</DigestValue>
</Reference>
</SignedInfo>
<SignatureValue>
KedJuTob5gtvYx9qM3k3gm7kbLBwVbEQRl26S2tmXjqNND7MRGtoew==
</SignatureValue>
<KeyInfo>
<KeyValue>
<DSAKeyValue>
<P>
/KaCzo4Syrom78z3EQ5SbbB4sF7ey80etKII864WF64B81uRpH5t9jQTxe
Eu0ImbzRMqzVDZkVG9xD7nN1kuFw==
</P>
<Q>li7dzDacuo67Jg7mtqEm2TRuOMU=</Q>
<G>Z4Rxsnqc9E7pGknFFH2xqaryRPBaQ01khpMdLRQnG541Awtx/
XPaF5Bpsy4pNWMOHCBiNU0NogpsQW5QvnlMpA==
</G>
<Y>qV38IqrWJG0V/
mZQvRVi1OHw9Zj84nDC4jO8P0axi1gb6d+475yhMjSc/
BrIVC58W3ydbkK+Ri4OKbaRZlYeRA==
</Y>
</DSAKeyValue>
</KeyValue>
</KeyInfo>
</Signature>
`
d, err := libxml2.ParseString(src)
if !assert.NoError(t, err, "parsing XML") {
return
}
defer d.Free()
err = s.Validate(d)
if !assert.NoError(t, err, "s.Validate should pass") {
if serr, ok := err.(xsd.SchemaValidationError); ok {
for _, e := range serr.Errors() {
t.Logf("err: %s", e)
}
}
return
}
}()
}
func TestXSDDefaultValue(t *testing.T) {
const schemasrc = `<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xs:element name="config">
<xs:complexType mixed="true">
<xs:sequence>
<xs:element ref="attribute"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="attribute">
<xs:complexType>
<xs:sequence>
<xs:element name="linguistic">
<xs:complexType>
<xs:attribute name="item" default="US"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>`
const docsrc = `<config>
<attribute>
<linguistic></linguistic>
</attribute>
</config>`
schema, err := xsd.Parse([]byte(schemasrc))
if !assert.NoError(t, err, `xsd.Parse should succeed`) {
return
}
defer schema.Free()
doc, err := libxml2.ParseString(docsrc)
if !assert.NoError(t, err, "parsing XML") {
return
}
defer doc.Free()
if !assert.NoError(t, schema.Validate(doc, xsd.ValueVCCreate), `schema.Validate should succeed`) {
return
}
t.Logf("%s", doc.String())
}