-
Notifications
You must be signed in to change notification settings - Fork 5
/
validation_test.go
99 lines (84 loc) · 2.63 KB
/
validation_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
package epp
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
xsd "github.com/lestrrat-go/libxml2/xsd"
)
func TestValidator_setupSchema(t *testing.T) {
validator, err := NewValidator("xml/index.xsd")
require.Nil(t, err)
require.NotNil(t, validator)
cases := []struct {
description string
xml []byte
errContains string
xmlErrors []string
}{
{
description: "no xml should not be valid",
xml: []byte{},
errContains: "failed to create parse context",
},
{
description: "namespace for EPP tag is requried",
xml: []byte(`<epp><command></command></epp>`),
errContains: "schema validation failed",
xmlErrors: []string{"Element 'epp': No matching global declaration available for the validation root."},
},
{
description: "attributes are verified",
xml: []byte(`
<epp xmlns="urn:ietf:params:xml:ns:epp-1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:ietf:params:xml:ns:epp-1.0 epp-1.0.xsd">
<command>
<poll msgID="3" op="-INVALID-"/>
<clTRID>ABC-12345</clTRID>
</command>
</epp>`),
errContains: "schema validation failed",
xmlErrors: []string{"'-INVALID-' is not an element of the set {'ack', 'req'}", "'-INVALID-' is not a valid value of the atomic type"},
},
{
description: "valid XML, including type ns",
xml: []byte(`
<epp xmlns="urn:ietf:params:xml:ns:epp-1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:ietf:params:xml:ns:epp-1.0 epp-1.0.xsd">
<command>
<check>
<domain:check xmlns:domain="urn:ietf:params:xml:ns:domain-1.0" xsi:schemaLocation="urn:ietf:params:xml:ns:domain-1.0 domain-1.0.xsd">
<domain:name>example1.se</domain:name>
<domain:name>example2.se</domain:name>
</domain:check>
</check>
<clTRID>ABC-12345</clTRID>
</command>
</epp>`),
},
}
for _, tc := range cases {
t.Run(tc.description, func(t *testing.T) {
err := validator.Validate(tc.xml)
if tc.errContains == "" {
require.Nil(t, err)
return
}
require.NotNil(t, err)
assert.Contains(t, err.Error(), tc.errContains)
xErr, ok := err.(xsd.SchemaValidationError)
if !ok {
return
}
xErrors := xErr.Errors()
if len(xErrors) != len(tc.xmlErrors) {
t.Logf("all errors not caught, got %d errors:\n", len(xErrors))
for i, err := range xErrors {
t.Logf("%-3d %s\n", i, err.Error())
}
assert.Fail(t, "all errors are not caught")
return
}
for i, err := range tc.xmlErrors {
assert.Contains(t, xErrors[i].Error(), err)
}
})
}
}