@@ -31,6 +31,89 @@ func (t *toks) Token() (Token, error) {
31
31
return tok , nil
32
32
}
33
33
34
+ func TestDecodeBadName (t * testing.T ) {
35
+ tests := []struct {
36
+ name string
37
+ invalid string
38
+ message string
39
+ }{
40
+ {
41
+ name : "Number after colon" ,
42
+ invalid : `<a:1/>` ,
43
+ message : "invalid XML name: 1" ,
44
+ },
45
+ {
46
+ name : "Two colons at end" ,
47
+ invalid : `<a::/>` ,
48
+ message : "expected element name after <" ,
49
+ },
50
+ {
51
+ name : "Two colons together in middle" ,
52
+ invalid : "<a::a/>" ,
53
+ message : "expected element name after <" ,
54
+ },
55
+ {
56
+ name : "Colon at end" ,
57
+ invalid : "<a:/>" ,
58
+ message : "expected element name after <" ,
59
+ },
60
+ {
61
+ name : "Colon at start" ,
62
+ invalid : "<:a/>" ,
63
+ message : "expected element name after <" ,
64
+ },
65
+ {
66
+ name : "Number after colon in attribute" ,
67
+ invalid : `<a a:1=""/>` ,
68
+ message : "invalid XML name: 1" ,
69
+ },
70
+ {
71
+ name : "Two colons separate" ,
72
+ invalid : `<a a:b:c="1"/>` ,
73
+ message : "colon after prefixed XML name a:b" ,
74
+ },
75
+ {
76
+ name : "Two colons at end" ,
77
+ invalid : `<a a::="1"/>` ,
78
+ message : "expected attribute name in element" ,
79
+ },
80
+ {
81
+ name : "Two colons together in middle" ,
82
+ invalid : `<a a::a="1"/>` ,
83
+ message : "expected attribute name in element" ,
84
+ },
85
+ {
86
+ name : "Colon at end" ,
87
+ invalid : `<a a:="1"/>` ,
88
+ message : "expected attribute name in element" ,
89
+ },
90
+ {
91
+ name : "Colon at start" ,
92
+ invalid : `<a :a="1"/>` ,
93
+ message : "expected attribute name in element" ,
94
+ },
95
+ }
96
+ for i , j := range tests {
97
+ t .Run (j .name , func (t * testing.T ) {
98
+ d := NewDecoder (strings .NewReader (j .invalid ))
99
+ tok , err := d .RawToken ()
100
+ if tok != nil {
101
+ t .Fatalf ("%d: d.Decode: expected nil token, got %#v" , i , tok )
102
+ }
103
+ if err == nil {
104
+ t .Fatalf ("%d: d.Decode: expected non-nil error, got nil" , i )
105
+ }
106
+ syntaxError , ok := err .(* SyntaxError )
107
+ if ! ok {
108
+ t .Fatalf ("%d: d.Decode: expected syntax error" , i )
109
+ }
110
+ if syntaxError .Msg != j .message {
111
+ t .Errorf ("%d: bad message: expected %q, got %q" , i , j .message , syntaxError .Msg )
112
+ }
113
+ })
114
+ }
115
+ }
116
+
34
117
func TestDecodeEOF (t * testing.T ) {
35
118
start := StartElement {Name : Name {Local : "test" }}
36
119
tests := []struct {
@@ -1130,12 +1213,12 @@ func TestIssue20396(t *testing.T) {
1130
1213
wantErr error
1131
1214
}{
1132
1215
{`<a:te:st xmlns:a="abcd"/>` , // Issue 20396
1133
- UnmarshalError ("XML syntax error on line 1: expected element name after < " )},
1216
+ UnmarshalError ("XML syntax error on line 1: colon after prefixed XML name a:te " )},
1134
1217
{`<a:te=st xmlns:a="abcd"/>` , attrError },
1135
1218
{`<a:te&st xmlns:a="abcd"/>` , attrError },
1136
1219
{`<a:test xmlns:a="abcd"/>` , nil },
1137
1220
{`<a:te:st xmlns:a="abcd">1</a:te:st>` ,
1138
- UnmarshalError ("XML syntax error on line 1: expected element name after < " )},
1221
+ UnmarshalError ("XML syntax error on line 1: colon after prefixed XML name a:te " )},
1139
1222
{`<a:te=st xmlns:a="abcd">1</a:te=st>` , attrError },
1140
1223
{`<a:te&st xmlns:a="abcd">1</a:te&st>` , attrError },
1141
1224
{`<a:test xmlns:a="abcd">1</a:test>` , nil },
@@ -1324,7 +1407,6 @@ func testRoundTrip(t *testing.T, input string) {
1324
1407
1325
1408
func TestRoundTrip (t * testing.T ) {
1326
1409
tests := map [string ]string {
1327
- "trailing colon" : `<foo abc:="x"></foo>` ,
1328
1410
"comments in directives" : `<!ENTITY x<!<!-- c1 [ " -->--x --> > <e></e> <!DOCTYPE xxx [ x<!-- c2 " -->--x ]>` ,
1329
1411
}
1330
1412
for name , input := range tests {
0 commit comments