@@ -31,6 +31,89 @@ func (t *toks) Token() (Token, error) {
3131return tok , nil
3232}
3333
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+
34117func TestDecodeEOF (t * testing.T ) {
35118start := StartElement {Name : Name {Local : "test" }}
36119tests := []struct {
@@ -1130,12 +1213,12 @@ func TestIssue20396(t *testing.T) {
11301213wantErr error
11311214}{
11321215{`<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 " )},
11341217{`<a:te=st xmlns:a="abcd"/>` , attrError },
11351218{`<a:te&st xmlns:a="abcd"/>` , attrError },
11361219{`<a:test xmlns:a="abcd"/>` , nil },
11371220{`<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 " )},
11391222{`<a:te=st xmlns:a="abcd">1</a:te=st>` , attrError },
11401223{`<a:te&st xmlns:a="abcd">1</a:te&st>` , attrError },
11411224{`<a:test xmlns:a="abcd">1</a:test>` , nil },
@@ -1324,7 +1407,6 @@ func testRoundTrip(t *testing.T, input string) {
13241407
13251408func TestRoundTrip (t * testing.T ) {
13261409tests := map [string ]string {
1327- "trailing colon" : `<foo abc:="x"></foo>` ,
13281410"comments in directives" : `<!ENTITY x<!<!-- c1 [ " -->--x --> > <e></e> <!DOCTYPE xxx [ x<!-- c2 " -->--x ]>` ,
13291411}
13301412for name , input := range tests {
0 commit comments