forked from lestrrat-go/libxml2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xsd_test.go
116 lines (105 loc) · 2.67 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
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
}
}()
}