2 // XmlSimpleDictionaryReaderTest.cs
5 // Atsushi Enomoto <atsushi@ximian.com>
7 // Copyright (C) 2007 Novell, Inc. http://www.novell.com
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using NUnit.Framework;
36 namespace MonoTests.System.Xml
39 public class XmlBinaryDictionaryReaderTest
42 [ExpectedException (typeof (ArgumentNullException))]
43 public void NullQuotas ()
45 XmlDictionaryReader.CreateBinaryReader (usecase1,null);
49 public void UseCase1 ()
51 string xml = @"<?xml version=""1.0"" encoding=""utf-16""?><root a=""""><!----> <AAA xmlns=""urn:AAA""></AAA><ePfix:AAA xmlns:ePfix=""urn:AAABBB""></ePfix:AAA><AAA>CCC" + "\u3005\u4E00" + @"CCCAAA&AAADDD&DDD" + '\u4E01' + @"<!--COMMENT--></AAA><AAA BBB=""bbb"" pfix:BBB=""bbbbb"" xml:lang=""ja"" xml:space=""preserve"" xml:base=""local:hogehoge"" xmlns:pfix=""urn:bbb"">CCCICAg/4Aw</AAA></root>";
53 XmlDictionaryReader reader =
54 XmlDictionaryReader.CreateBinaryReader (usecase1,new XmlDictionaryReaderQuotas ());
55 StringWriter sw = new StringWriter ();
56 XmlWriter xw = XmlWriter.Create (sw);
59 xw.WriteNode (reader, false);
61 Assert.AreEqual (xml, sw.ToString ());
66 static readonly byte [] usecase1 = new byte [] {
67 // $!root$! a....!__ ___.!AAA $!urn:AA A$$!ePfi
68 0x40, 0x04, 0x72, 0x6F, 0x6F, 0x74, 0x04, 0x01,
69 0x61, 0xA8, 0x02, 0x00, 0x98, 0x05, 0x20, 0x20,
70 0x20, 0x20, 0x20, 0x40, 0x03, 0x41, 0x41, 0x41,
71 0x08, 0x07, 0x75, 0x72, 0x6E, 0x3A, 0x41, 0x41,
72 0x41, 0x01, 0x41, 0x05, 0x65, 0x50, 0x66, 0x69,// 40
73 // x!AAA$!e Pfix!urn :AAABBB$ $!AAA$!C CC......
74 0x78, 0x03, 0x41, 0x41, 0x41, 0x09, 0x05, 0x65,
75 0x50, 0x66, 0x69, 0x78, 0x0A, 0x75, 0x72, 0x6E,
76 0x3A, 0x41, 0x41, 0x41, 0x42, 0x42, 0x42, 0x01,
77 0x40, 0x03, 0x41, 0x41, 0x41, 0x98, 0x0C, 0x43,
78 0x43, 0x43, 0xE3, 0x80, 0x85, 0xE4, 0xB8, 0x80,// 80
79 // AAA$!DDD $AAA$!DD D$DDD... ..$!COMM ENT$$!AA
80 0x43, 0x43, 0x43, 0x98, 0x07, 0x41, 0x41, 0x41,
81 0x26, 0x41, 0x41, 0x41, 0x98, 0x07, 0x44, 0x44,
82 0x44, 0x26, 0x44, 0x44, 0x44, 0x98, 0x03, 0xE4,
83 0xB8, 0x81, 0x02, 0x07, 0x43, 0x4F, 0x4D, 0x4D,
84 0x45, 0x4E, 0x54, 0x01, 0x40, 0x03, 0x41, 0x41,// 120
85 // A$!BBB$! bbb$!pfi x!BBB$!b bbbb$!xm l!lang$!
86 0x41, 0x04, 0x03, 0x42, 0x42, 0x42, 0x98, 0x03,
87 0x62, 0x62, 0x62, 0x05, 0x04, 0x70, 0x66, 0x69,
88 0x78, 0x03, 0x42, 0x42, 0x42, 0x98, 0x05, 0x62,
89 0x62, 0x62, 0x62, 0x62, 0x05, 0x03, 0x78, 0x6D,
90 0x6C, 0x04, 0x6C, 0x61, 0x6E, 0x67, 0x98, 0x02,// 160
91 // ja$!xml! space$!p reserve
92 0x6A, 0x61, 0x05, 0x03, 0x78, 0x6D, 0x6C, 0x05,
93 0x73, 0x70, 0x61, 0x63, 0x65, 0x98, 0x08, 0x70,
94 0x72, 0x65, 0x73, 0x65, 0x72, 0x76, 0x65, 0x05,
95 0x03, 0x78, 0x6D, 0x6C, 0x04, 0x62, 0x61, 0x73,
96 0x65, 0x98, 0x0E, 0x6C, 0x6F, 0x63, 0x61, 0x6C,// 200
97 // ..hogehog e$!pfix! urn:bbb$ $CCC$!BA SE64$
98 0x3A, 0x68, 0x6F, 0x67, 0x65, 0x68, 0x6F, 0x67,
99 0x65, 0x09, 0x04, 0x70, 0x66, 0x69, 0x78, 0x07,
100 0x75, 0x72, 0x6E, 0x3A, 0x62, 0x62, 0x62, 0x98,
101 0x03, 0x43, 0x43, 0x43, 0x9F, 0x06, 0x20, 0x20,
102 0x20, 0xFF, 0x80, 0x30, 0x01,
106 public void UseCase2 ()
108 XmlDictionary dic = new XmlDictionary ();
110 dic.Add (String.Empty);
115 XmlDictionaryReader reader =
116 XmlDictionaryReader.CreateBinaryReader (new MemoryStream (usecase2), dic, new XmlDictionaryReaderQuotas ());
122 // / : especially. EndElement
124 // @ : dictionary index
125 // ^ : missing ns decl?
126 static readonly byte [] usecase2 = new byte [] {
127 // $@$!BAR$ @$@///$@ ^@$!ppp! $!ppp@$! xyz$!bbb
128 0x42, 2, 0x40, 3, 0x42, 0x41, 0x52, 0x42,
129 2, 0x42, 4, 1, 1, 1, 0x42, 4,
130 10, 6, 0x43, 3, 0x70, 0x70, 0x70, 4,
131 11, 3, 0x70, 0x70, 0x70, 6, 0x99, 3,
132 0x78, 0x79, 0x7A, 0x98, 4, 0x62, 0x62, 0x62,
133 // b$!ccc$G UIDGUIDG UIDGUID$ !FOO$!GU IDGUIDGU
134 0x62, 0x98, 3, 0x63, 0x63, 0x63, 0xB1, 0x22,
135 0x22, 0x11, 0x11, 0x33, 0x33, 0x44, 0x44, 0x55,
136 0x55, 0x66, 0x66, 0x77, 0x77, 0x88, 0x88, 0x40,
137 3, 0x46, 0x4F, 0x4F, 0x04, 3, 0x41, 0x41,
138 0x41, 0xB0, 0x22, 0x22, 0x11, 0x11, 0x33, 0x33,
139 // IDGUIDGU ID$!BBB$T IMESPAN $!CC$!UN IQUEIDUN
140 0x44, 0x44, 0x55, 0x55, 0x66, 0x66, 0x77, 0x77,
141 0x88, 0x88, 0x04, 3, 0x42, 0x42, 0x42, 0xAE,
142 0, 0, 0, 0, 0, 0, 0, 0,
143 0x04, 2, 0x43, 0x43, 0x98, 0x2B, 0x75, 0x75,
144 0x69, 0x64, 0x2D, 0x30, 0x30, 0x30, 0x30, 0x30,
145 // IQUEIDUN IQUEIDUN IQUEIDUN IQUEID.. .$!XX$$$!
146 0x30, 0x30, 0x30, 0x2D, 0x30, 0x30, 0x30, 0x30,
147 0x2D, 0x30, 0x30, 0x30, 0x30, 0x2D, 0x30, 0x30,
148 0x30, 0x30, 0x2D, 0x30, 0x30, 0x30, 0x30, 0x30,
149 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x2D,
150 0x31, 0x40, 2, 0x58, 0x58, 0x86, 0x85, 0x41, 2,
151 // xx!aaa$!x x!urn:xxx
152 0x78, 0x78, 3, 0x61, 0x61, 0x61, 0x09, 2, 0x78,
153 0x78, 0x07, 0x75, 0x72, 0x6E, 0x3A, 0x7A, 0x7A,
158 public void ElementWithNS ()
160 byte [] bytes = new byte [] {
161 0x42, 0, 10, 2, 0x98, 3, 0x61, 0x61,
162 0x61, 0x42, 0, 0x42, 2, 1, 1, 1};
164 XmlDictionary dic = new XmlDictionary ();
168 XmlDictionaryReader reader =
169 XmlDictionaryReader.CreateBinaryReader (new MemoryStream (bytes), dic, new XmlDictionaryReaderQuotas ());
175 [Category ("NotWorking")]
176 public void ContainsInvalidIndex ()
178 byte [] bytes = new byte [] {
179 0x42, 1, 10, 2, 0x98, 3, 0x61, 0x61,
180 0x61, 0x42, 0, 0x42, 2, 1, 1, 1};
182 XmlDictionary dic = new XmlDictionary ();
186 XmlDictionaryReader dr = XmlDictionaryReader.CreateBinaryReader (new MemoryStream (bytes), dic, new XmlDictionaryReaderQuotas ());
189 Assert.Fail ("dictionary index 1 should be regarded as invalid.");
190 } catch (XmlException) {
195 [Category ("NotWorking")]
196 public void Beyond128DictionaryEntries ()
198 XmlDictionaryString ds;
199 MemoryStream ms = new MemoryStream ();
200 XmlDictionary dic = new XmlDictionary ();
201 for (int i = 0; i < 260; i++)
202 Assert.AreEqual (i, dic.Add ("n" + i).Key, "d");
203 XmlDictionary dic2 = new XmlDictionary ();
204 XmlBinaryReaderSession session = new XmlBinaryReaderSession ();
206 for (int i = 0; i < 260; i++)
207 session.Add (i, "n" + i);
209 byte [] bytes = new byte [] {
210 // so, when it went beyond 128, the index
211 // becomes 2 bytes, where
212 // - the first byte always becomes > 80, and
213 // - the second byte becomes (n / 0x80) * 2.
214 0x42, 0x80, 2, 0x0A, 0x82, 2,
215 0x42, 0x85, 2, 0x0A, 0x87, 2,
216 0x42, 0x88, 2, 0x0A, 0x8B, 2,
217 0x42, 0x80, 4, 0x0A, 0x81, 4,
220 XmlDictionaryReader reader = XmlDictionaryReader.CreateBinaryReader (new MemoryStream (bytes), dic, new XmlDictionaryReaderQuotas (), session);
226 public void GlobalAttributes ()
228 XmlDictionary dic = new XmlDictionary ();
237 // 0x0C nameidx (value) 0x0D nameidx (value)
238 // 0x07 (prefix) nameidx (value)
239 // 0x05 (prefix) (name) (value)
240 // 0x04... 0x06... 0x05...
242 // 0x0B (prefix) nsidx
244 // 0x09 (prefix) (ns)
245 byte [] bytes = new byte [] {
248 // $!aaa!$! bbb$urn:foo$
251 0x0D, 6, 0x98, 1, 0x76,
252 0x07, 3, 0x61, 0x61, 0x61, 8, 0xA8, // 16
253 0x05, 3, 0x62, 0x62, 0x62, 2, 0x6E, 0x35, 0xA8,
254 0x04, 2, 0x6E, 0x36, 0xA8, // 30
256 0x05, 3, 0x62, 0x62, 0x62, 2, 0x6E, 0x38, 0xA8,
258 0x0B, 1, 0x61, 10, // 48
260 0x0B, 3, 0x61, 0x61, 0x61, 10,
261 0x09, 3, 0x62, 0x62, 0x62,
262 0x07, 0x75, 0x72, 0x6E, 0x3A, 0x66, 0x6F, 0x6F,
265 XmlDictionaryReader reader = XmlDictionaryReader.CreateBinaryReader (new MemoryStream (bytes), dic, new XmlDictionaryReaderQuotas ());
271 public void AttributeXmlns ()
273 // equivalent to WriteXmlnsAttribute()
274 XmlDictionaryString ds;
275 XmlDictionary dic = new XmlDictionary ();
277 dic.Add ("http://www.w3.org/2000/xmlns/");
279 byte [] bytes = new byte [] {
280 // 40 (root) 04 (a) A8
281 // 09 (foo) (urn:foo) 08 (urn:bar)
282 0x40, 4, 0x72, 0x6F, 0x6F, 0x74,
284 0x09, 3, 0x66, 0x6F, 0x6F, 7, 0x75, 0x72, 0x6E, 0x3A, 0x66, 0x6F, 0x6F,
285 0x08, 7, 0x75, 0x72, 0x6E, 0x3A, 0x62, 0x61, 0x72, 1
288 XmlDictionaryReader reader = XmlDictionaryReader.CreateBinaryReader (new MemoryStream (bytes), dic, new XmlDictionaryReaderQuotas ());
294 public void ReadTypedValues ()
296 XmlDictionaryReader reader = XmlDictionaryReader.CreateBinaryReader (new MemoryStream (typed_values), new XmlDictionaryReaderQuotas ());
301 byte [] typed_values = new byte [] {
302 0x40, 4, 0x72, 0x6F, 0x6F, 0x74,
305 0x8C, 0xBC, 0x92, 0, 0, // int
306 0x8C, 0x2C, 0xEB, 0x6D, 0x08, // 20
307 0x8E, 0x80, 0x55, 0xF5, 0x51, 0x01, 0, 0, 0,
308 0x90, 0xD7, 0xB3, 0xDD, 0x3F, // float
309 0x92, 0x4C, 0x15, 0x31, 0x91, 0x77, 0xE3, 0x01, 0x40, // 43
310 0x94, 0, 0, 6, 0, 0, 0, 0, 0, 0xD8, 0xEF, 0x2F, 0, 0, 0, 0, 0,
311 0x97, 0x80, 0x40, 0xA3, 0x29, 0xE5, 0x22, 0xC1, 8