-
Notifications
You must be signed in to change notification settings - Fork 2
/
c14n_test.go
66 lines (53 loc) · 1.52 KB
/
c14n_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
package c14n_test
import (
"bytes"
"encoding/xml"
"errors"
"fmt"
"io"
"io/ioutil"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/ucarion/c14n"
"golang.org/x/net/html/charset"
)
func ExampleCanonicalize() {
input := `<foo z="2" a="1"><bar /></foo>`
decoder := xml.NewDecoder(strings.NewReader(input))
out, err := c14n.Canonicalize(decoder)
fmt.Println(string(out), err)
// Output:
// <foo a="1" z="2"><bar></bar></foo> <nil>
}
func TestCanonicalize(t *testing.T) {
entries, err := ioutil.ReadDir("tests")
assert.NoError(t, err)
for _, file := range entries {
t.Run(file.Name(), func(t *testing.T) {
in, err := ioutil.ReadFile(fmt.Sprintf("tests/%s/in.xml", file.Name()))
assert.NoError(t, err)
out, err := ioutil.ReadFile(fmt.Sprintf("tests/%s/out.xml", file.Name()))
assert.NoError(t, err)
decoder := xml.NewDecoder(bytes.NewReader(in))
decoder.CharsetReader = charset.NewReaderLabel
actual, err := c14n.Canonicalize(decoder)
assert.NoError(t, err)
assert.Equal(t, out, actual)
})
}
}
func TestCanonicalize_NoStartElement(t *testing.T) {
decoder := xml.NewDecoder(strings.NewReader("<!-- foo -->"))
_, err := c14n.Canonicalize(decoder)
assert.Equal(t, io.ErrUnexpectedEOF, err)
}
func TestCanonicalize_RawTokenError(t *testing.T) {
_, err := c14n.Canonicalize(&errRawTokener{})
assert.Equal(t, errDummy, err)
}
var errDummy = errors.New("dummy error")
type errRawTokener struct{}
func (e *errRawTokener) RawToken() (xml.Token, error) {
return nil, errDummy
}