Moved ProviderCollectionTest.cs from System assembly to System.Configuration.
[mono.git] / mcs / class / System.Runtime.Serialization / Test / System.Xml / XmlBinaryDictionaryReaderTest.cs
1 //
2 // XmlSimpleDictionaryReaderTest.cs
3 //
4 // Author:
5 //      Atsushi Enomoto <atsushi@ximian.com>
6 //
7 // Copyright (C) 2007 Novell, Inc.  http://www.novell.com
8
9 //
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:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
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.
28 //
29
30 using System;
31 using System.IO;
32 using System.Text;
33 using System.Xml;
34 using NUnit.Framework;
35
36 namespace MonoTests.System.Xml
37 {
38         [TestFixture]
39         public class XmlBinaryDictionaryReaderTest
40         {
41                 [Test]
42                 [ExpectedException (typeof (ArgumentNullException))]
43                 public void NullQuotas ()
44                 {
45                         XmlDictionaryReader.CreateBinaryReader (usecase1,null);
46                 }
47
48                 [Test]
49                 public void UseCase1 ()
50                 {
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&amp;AAADDD&amp;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>";
52
53                         XmlDictionaryReader reader =
54                                 XmlDictionaryReader.CreateBinaryReader (usecase1,new XmlDictionaryReaderQuotas ());
55                         StringWriter sw = new StringWriter ();
56                         XmlWriter xw = XmlWriter.Create (sw);
57                         reader.Read ();
58                         while (!reader.EOF)
59                                 xw.WriteNode (reader, false);
60                         xw.Close ();
61                         Assert.AreEqual (xml, sw.ToString ());
62                 }
63
64                 // $ : kind
65                 // ! : length
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,
103                         };
104
105                 [Test]
106                 public void UseCase2 ()
107                 {
108                         XmlDictionary dic = new XmlDictionary ();
109
110                         dic.Add (String.Empty);
111                         dic.Add ("FOO");
112                         dic.Add ("BAR");
113                         dic.Add ("urn:bar");
114
115                         XmlDictionaryReader reader =
116                                 XmlDictionaryReader.CreateBinaryReader (new MemoryStream (usecase2), dic, new XmlDictionaryReaderQuotas ());
117                         while (!reader.EOF)
118                                 reader.Read ();
119                 }
120
121                 // $ : kind
122                 // / : especially. EndElement
123                 // ! : length
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,
154                         0x7A, 1, 1, 1
155                         };
156
157                 [Test]
158                 public void ElementWithNS ()
159                 {
160                         byte [] bytes = new byte [] {
161                                 0x42, 0, 10, 2, 0x98, 3, 0x61, 0x61,
162                                 0x61, 0x42, 0, 0x42, 2, 1, 1, 1};
163
164                         XmlDictionary dic = new XmlDictionary ();
165                         dic.Add ("FOO");
166                         dic.Add ("foo");
167
168                         XmlDictionaryReader reader =
169                                 XmlDictionaryReader.CreateBinaryReader (new MemoryStream (bytes), dic, new XmlDictionaryReaderQuotas ());
170                         while (!reader.EOF)
171                                 reader.Read ();
172                 }
173
174                 [Test]
175                 [Category ("NotWorking")]
176                 public void ContainsInvalidIndex ()
177                 {
178                         byte [] bytes = new byte [] {
179                                 0x42, 1, 10, 2, 0x98, 3, 0x61, 0x61,
180                                 0x61, 0x42, 0, 0x42, 2, 1, 1, 1};
181
182                         XmlDictionary dic = new XmlDictionary ();
183                         dic.Add ("FOO");
184                         dic.Add ("foo");
185
186                         XmlDictionaryReader dr = XmlDictionaryReader.CreateBinaryReader (new MemoryStream (bytes), dic, new XmlDictionaryReaderQuotas ());
187                         try {
188                                 dr.Read ();
189                                 Assert.Fail ("dictionary index 1 should be regarded as invalid.");
190                         } catch (XmlException) {
191                         }
192                 }
193
194                 [Test]
195                 [Category ("NotWorking")]
196                 public void Beyond128DictionaryEntries ()
197                 {
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 ();
205                         int idx;
206                         for (int i = 0; i < 260; i++)
207                                 session.Add (i, "n" + i);
208
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,
218                                 1, 1, 1, 1};
219
220                         XmlDictionaryReader reader = XmlDictionaryReader.CreateBinaryReader (new MemoryStream (bytes), dic, new XmlDictionaryReaderQuotas (), session);
221                         while (!reader.EOF)
222                                 reader.Read ();
223                 }
224
225                 [Test]
226                 public void GlobalAttributes ()
227                 {
228                         XmlDictionary dic = new XmlDictionary ();
229                         dic.Add ("n1");
230                         dic.Add ("urn:foo");
231                         dic.Add ("n2");
232                         dic.Add ("n3");
233                         dic.Add ("n4");
234                         dic.Add ("urn:bar");
235                         dic.Add ("n7");
236
237                         // 0x0C nameidx (value) 0x0D nameidx (value)
238                         // 0x07 (prefix) nameidx (value)
239                         // 0x05 (prefix) (name) (value)
240                         // 0x04...  0x06...  0x05...
241                         // 0x0A nsidx
242                         // 0x0B (prefix) nsidx
243                         // 0x0B...  0x0B...
244                         // 0x09 (prefix) (ns)
245                         byte [] bytes = new byte [] {
246                                 // $@$@$$@$  !v$!aaa@
247                                 // $@!bbb!n  5$$@$!a@
248                                 // $!aaa!$!  bbb$urn:foo$
249                                 0x42, 0,
250                                 0x0C, 4, 0xA8,
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
255                                 0x06, 12, 0xA8,
256                                 0x05, 3, 0x62, 0x62, 0x62, 2, 0x6E, 0x38, 0xA8,
257                                 0x0A, 2,
258                                 0x0B, 1, 0x61, 10, // 48
259                                 0x0B, 1, 0x62, 2,
260                                 0x0B, 3, 0x61, 0x61, 0x61, 10,
261                                 0x09, 3, 0x62, 0x62, 0x62,
262                                 0x07, 0x75, 0x72, 0x6E, 0x3A, 0x66, 0x6F, 0x6F,
263                                 1};
264
265                         XmlDictionaryReader reader = XmlDictionaryReader.CreateBinaryReader (new MemoryStream (bytes), dic, new XmlDictionaryReaderQuotas ());
266                         while (!reader.EOF)
267                                 reader.Read ();
268                 }
269
270                 [Test]
271                 public void AttributeXmlns ()
272                 {
273                         // equivalent to WriteXmlnsAttribute()
274                         XmlDictionaryString ds;
275                         XmlDictionary dic = new XmlDictionary ();
276                         dic.Add ("xmlns");
277                         dic.Add ("http://www.w3.org/2000/xmlns/");
278
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,
283                                 0x04, 1, 0x61, 0xA8,
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
286                                 };
287
288                         XmlDictionaryReader reader = XmlDictionaryReader.CreateBinaryReader (new MemoryStream (bytes), dic, new XmlDictionaryReaderQuotas ());
289                         while (!reader.EOF)
290                                 reader.Read ();
291                 }
292
293                 [Test]
294                 public void ReadTypedValues ()
295                 {
296                         XmlDictionaryReader reader = XmlDictionaryReader.CreateBinaryReader (new MemoryStream (typed_values), new XmlDictionaryReaderQuotas ());
297                         while (!reader.EOF)
298                                 reader.Read ();
299                 }
300
301                 byte [] typed_values = new byte [] {
302                         0x40, 4, 0x72, 0x6F, 0x6F, 0x74,
303                         0x88, 5,
304                         0x8A, 0x7D, 0x03,
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
312                         };
313         }
314 }