Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mcs / class / System.XML / Test / System.Xml.Schema / XmlSchemaValidatorTests.cs
1 //
2 // XmlSchemaValidatorTests.cs
3 //
4 // Author:
5 //      Atsushi Enomoto <atsushi@ximian.com>
6 //
7 // (C) 2008 Novell Inc.
8 //
9
10
11 using System;
12 using System.Collections;
13 using System.IO;
14 using System.Xml;
15 using System.Xml.Schema;
16 using NUnit.Framework;
17
18 namespace MonoTests.System.Xml
19 {
20         [TestFixture]
21         public class XmlSchemaValidatorTests
22         {
23                 void Validate (string xml, string xsd)
24                 {
25                         XmlSchema schema = XmlSchema.Read (new StringReader (xsd), null);
26                         XmlReaderSettings settings = new XmlReaderSettings ();
27                         settings.ValidationType = ValidationType.Schema;
28                         settings.Schemas.Add (schema);
29                         XmlReader reader = XmlReader.Create (new StringReader (xml), settings);
30                         while (reader.Read ())
31                                 ;
32                 }
33
34                 [Test]
35                 public void XsdAnyToSkipAttributeValidation ()
36                 {
37                         // bug #358408
38                         XmlSchemaSet schemas = new XmlSchemaSet ();
39                         schemas.Add (null, "Test/XmlFiles/xsd/358408.xsd");
40                         XmlSchemaValidator v = new XmlSchemaValidator (
41                                 new NameTable (),
42                                 schemas,
43                                 new XmlNamespaceManager (new NameTable ()),
44                                 XmlSchemaValidationFlags.ProcessIdentityConstraints);
45                         v.Initialize ();
46                         v.ValidateWhitespace (" ");
47                         XmlSchemaInfo info = new XmlSchemaInfo ();
48                         ArrayList list = new ArrayList ();
49
50                         v.ValidateElement ("configuration", "", info, null, null, null, null);
51                         v.GetUnspecifiedDefaultAttributes (list);
52                         v.ValidateEndOfAttributes (info);
53
54                         v.ValidateWhitespace (" ");
55
56                         v.ValidateElement ("host", "", info, null, null, null, null);
57                         v.ValidateAttribute ("auto-start", "", "true", info);
58                         list.Clear ();
59                         v.GetUnspecifiedDefaultAttributes (list);
60                         v.ValidateEndOfAttributes (info);
61                         v.ValidateEndElement (null);//info);
62
63                         v.ValidateWhitespace (" ");
64
65                         v.ValidateElement ("service-managers", "", info, null, null, null, null);
66                         list.Clear ();
67                         v.GetUnspecifiedDefaultAttributes (list);
68                         v.ValidateEndOfAttributes (info);
69
70                         v.ValidateWhitespace (" ");
71
72                         v.ValidateElement ("service-manager", "", info, null, null, null, null);
73                         list.Clear ();
74                         v.GetUnspecifiedDefaultAttributes (list);
75                         v.ValidateEndOfAttributes (info);
76
77                         v.ValidateWhitespace (" ");
78
79                         v.ValidateElement ("foo", "", info, null, null, null, null);
80                         v.ValidateAttribute ("bar", "", "", info);
81                 }
82
83                 [Test]
84                 public void SkipInvolved () // bug #422581
85                 {
86                         XmlReader schemaReader = XmlReader.Create ("Test/XmlFiles/xsd/422581.xsd");
87                         XmlSchema schema = XmlSchema.Read (schemaReader, null);
88                         XmlReaderSettings settings = new XmlReaderSettings ();
89                         settings.ValidationType = ValidationType.Schema;
90                         settings.Schemas.Add (schema);
91                         XmlReader reader = XmlReader.Create ("Test/XmlFiles/xsd/422581.xml", settings);
92                         while (reader.Read ());
93                 }
94
95                 [Test]
96                 public void Bug433774 ()
97                 {
98                         string xsd = @"<xs:schema targetNamespace='urn:foo' xmlns='urn:foo' xmlns:xs='http://www.w3.org/2001/XMLSchema'>
99   <xs:element name='Root'>
100     <xs:complexType>
101       <xs:sequence></xs:sequence>
102       <xs:attribute name='version' type='xs:string' fixed='3' />
103     </xs:complexType>
104   </xs:element>
105 </xs:schema>";
106                         XmlDocument doc = new XmlDocument ();
107                         doc.LoadXml ("<Root version='3' xmlns='urn:foo'/>");
108                         XmlSchemaSet schemaSet = new XmlSchemaSet();
109                         schemaSet.Add (XmlSchema.Read (XmlReader.Create (new StringReader (xsd)), null));
110                         doc.Schemas = schemaSet;
111                         XmlNode root = doc.DocumentElement;
112                         doc.Validate (null, root);
113                 }
114
115                 [Test]
116                 [ExpectedException (typeof (XmlSchemaValidationException))]
117                 public void Bug435206 ()
118                 {
119                         string xsd = @"<xs:schema attributeFormDefault='unqualified' elementFormDefault='qualified' xmlns:xs='http://www.w3.org/2001/XMLSchema'>
120   <xs:element name='myDoc'>
121     <xs:complexType>
122       <xs:attribute name='foo' type='xs:unsignedLong' use='required' />
123       <xs:attribute name='bar' type='xs:dateTime' use='required' />
124     </xs:complexType>
125   </xs:element>
126 </xs:schema>";
127                         string xml = @"<myDoc foo='12' bar='January 1st 1900'/>";
128                         Validate (xml, xsd);
129                 }
130
131                 [Test]
132                 public void Bug469713 ()
133                 {
134                         string xsd = @"<xs:schema elementFormDefault='qualified' xmlns:xs='http://www.w3.org/2001/XMLSchema'>
135   <xs:element name='Message'>
136     <xs:complexType>
137       <xs:all>
138         <xs:element name='MyDateTime' nillable='true' type='xs:dateTime' />
139       </xs:all>
140     </xs:complexType>
141   </xs:element>
142 </xs:schema>";
143                         string xml = @"<Message xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:noNamespaceSchemaLocation='test.xsd'>
144         <MyDateTime xsi:nil='true'></MyDateTime>
145 </Message>";
146                         Validate (xml, xsd);
147                 }
148
149                 [Test]
150                 public void Bug496192_496205 ()
151                 {
152                         using (var xmlr = new StreamReader ("Test/XmlFiles/496192.xml"))
153                                 using (var xsdr = new StreamReader ("Test/XmlFiles/496192.xsd"))
154                                         Validate (xmlr.ReadToEnd (), xsdr.ReadToEnd ());
155                 }
156                 
157                 [Test]          
158                 public void Bug501666 ()
159                 {
160                         string xsd = @"
161                         <xs:schema id='Settings'
162                                 targetNamespace='foo'                
163                                 xmlns='foo'
164                                 xmlns:xs='http://www.w3.org/2001/XMLSchema'>
165
166                                 <xs:element name='Settings' type='Settings'/>
167
168                                 <xs:complexType name='Settings'>
169                                         <xs:attribute name='port' type='PortNumber' use='required'/>
170                                 </xs:complexType>
171                 
172                                 <xs:simpleType name='PortNumber'>
173                                         <xs:restriction base='xs:positiveInteger'>
174                                                 <xs:minInclusive value='1'/>
175                                                 <xs:maxInclusive value='65535'/>
176                                         </xs:restriction>
177                                 </xs:simpleType>
178                         </xs:schema>";
179
180                         string xml = @"<Settings port='1337' xmlns='foo'/>";
181
182                         XmlDocument doc = new XmlDocument ();
183                         doc.LoadXml (xml);
184                         doc.Schemas.Add (XmlSchema.Read (XmlReader.Create (new StringReader (xsd)), null));
185                         doc.Validate (null);
186                 }
187
188                 public void Bug502251 ()
189                 {
190                         string xsd = @"
191    <xs:schema id='foo' targetNamespace='foo' 
192      elementFormDefault='qualified' 
193      xmlns='foo'     
194      xmlns:xs='http://www.w3.org/2001/XMLSchema'>
195
196  <xs:group name='LayoutElementTypes'>
197   <xs:choice>   
198    <xs:element name='Rows' type='Rows' />
199    <xs:element name='Conditional' type='Conditional' />   
200   </xs:choice>
201  </xs:group>
202
203  <xs:complexType name='Element' abstract='true'>
204   <xs:attribute name='id' type='xs:ID' use='optional'/>
205  </xs:complexType>
206
207  <xs:complexType name='SingleChildElement' abstract='true'>
208   <xs:complexContent>
209    <xs:extension base='Element'>
210     <xs:group ref='LayoutElementTypes' minOccurs='1' maxOccurs='1' />
211    </xs:extension>
212   </xs:complexContent>
213  </xs:complexType>
214
215  <xs:complexType name='Rows'>
216   <xs:complexContent>
217    <xs:extension base='Element'>
218     <xs:sequence minOccurs='1' maxOccurs='unbounded'>
219      <xs:element name='Row' type='Row' />
220     </xs:sequence>    
221          </xs:extension>
222   </xs:complexContent>
223  </xs:complexType> 
224
225    <xs:complexType name='Row'>
226   <xs:complexContent>
227    <xs:extension base='SingleChildElement'>    
228    </xs:extension>    
229   </xs:complexContent>
230  </xs:complexType>
231
232  <xs:complexType name='Conditional'>
233   <xs:complexContent>
234    <xs:extension base='Element'>    
235    </xs:extension>
236   </xs:complexContent>
237  </xs:complexType>
238
239  <xs:complexType name='Layout'>
240   <xs:complexContent>
241    <xs:extension base='SingleChildElement'>
242    </xs:extension>
243   </xs:complexContent>
244  </xs:complexType>
245
246  <xs:element name='Layout' type='Layout' />
247 </xs:schema>";
248
249                         XmlDocument doc = new XmlDocument ();
250                         doc.LoadXml (@"<Layout xmlns='foo'>
251   <Rows>
252     <Row><Conditional/></Row>     
253   </Rows>
254 </Layout>");
255
256                         XmlSchema schema = XmlSchema.Read (XmlReader.Create (new StringReader (xsd)), null);
257
258                         doc.Schemas.Add (schema);
259                         doc.Validate (null);
260                 }
261
262                 [Test]
263                 public void Bug557452 ()
264                 {
265                         string xsd = @"
266                         <xs:schema id='Settings'
267                                 targetNamespace='foo'
268                                 xmlns='foo'
269                                 xmlns:xs='http://www.w3.org/2001/XMLSchema'>
270
271                                 <xs:element name='Settings' type='Settings'/>
272
273                                 <xs:complexType name='Settings'>
274                                         <xs:attribute name='port' type='PortNumber' use='required'/>
275                                 </xs:complexType>
276
277                                 <xs:simpleType name='PortNumber'>
278                                         <xs:restriction base='xs:decimal'>
279                                                 <xs:minInclusive value='1'/>
280                                                 <xs:maxInclusive value='65535'/>
281                                         </xs:restriction>
282                                 </xs:simpleType>
283                         </xs:schema>";
284
285                         string xml = @"<Settings port='1337' xmlns='foo'/>";
286
287                         XmlDocument doc = new XmlDocument ();
288                         doc.LoadXml (xml);
289                         doc.Schemas.Add (XmlSchema.Read (XmlReader.Create (new StringReader (xsd)), null));
290                         doc.Validate (null);
291                 }
292
293                 [Test]
294                 public void Bug584664 ()
295                 {
296                         Validate (File.ReadAllText ("Test/XmlFiles/xsd/584664a.xml"), File.ReadAllText ("Test/XmlFiles/xsd/584664a.xsd"));
297                         Validate (File.ReadAllText ("Test/XmlFiles/xsd/584664b.xml"), File.ReadAllText ("Test/XmlFiles/xsd/584664b.xsd"));
298                 }
299
300                 [Test]
301                 public void MultipleMissingIds ()
302                 {
303                         var schema = XmlSchema.Read (new StringReader (@"<?xml version=""1.0"" encoding=""utf-8""?>
304 <xs:schema targetNamespace=""urn:multiple-ids"" elementFormDefault=""qualified"" xmlns=""urn:multiple-ids"" xmlns:xs=""http://www.w3.org/2001/XMLSchema"">
305         <xs:element name=""root"">
306                 <xs:complexType>
307                         <xs:sequence minOccurs=""0"" maxOccurs=""unbounded"">
308                                 <xs:element name=""item"">
309                                         <xs:complexType>
310                                                 <xs:attribute name=""id"" type=""xs:ID"" />
311                                                 <xs:attribute name=""parent"" type=""xs:IDREF"" />
312                                         </xs:complexType>
313                                 </xs:element>
314                         </xs:sequence>
315                 </xs:complexType>
316         </xs:element>
317 </xs:schema>"), null);
318                         var xml = @"<?xml version=""1.0"" encoding=""utf-8""?>
319 <root xmlns=""urn:multiple-ids"">
320         <item id=""id2"" parent=""id1"" />
321         <item id=""id3"" parent=""id1"" />
322         <item id=""id1"" parent=""id1"" />
323 </root>";
324                         var document = new XmlDocument ();
325                         document.LoadXml (xml);
326                         document.Schemas = new XmlSchemaSet ();
327                         document.Schemas.Add (schema);
328                         document.Validate (null);
329                 }
330
331                 [Test]
332                 public void FacetsOnBaseSimpleContentRestriction ()
333                 {
334                         XmlReaderSettings settings = new XmlReaderSettings ();
335                         settings.Schemas.Add (null, "Test/XmlFiles/595947.xsd");
336                         settings.ValidationType = ValidationType.Schema;
337                         settings.Schemas.Compile ();
338
339                         Validate ("TEST 1.1", 1, "0123456789", "0123456789", settings, false);
340                         Validate ("TEST 1.2", 1, "0123456789***", "0123456789", settings, true);
341                         Validate ("TEST 1.3", 1, "0123456789", "0123456789***", settings, true);
342
343                         Validate ("TEST 2.1", 2, "0123456789", "0123456789", settings, false);
344                         Validate ("TEST 2.2", 2, "0123456789***", "0123456789", settings, true);
345                         Validate ("TEST 2.3", 2, "0123456789", "0123456789***", settings, true);
346
347                         Validate ("TEST 3.1", 3, "0123456789", "0123456789", settings, false);
348                         Validate ("TEST 3.2", 3, "0123456789***", "0123456789", settings, true);
349                         Validate ("TEST 3.3", 3, "0123456789", "0123456789***", settings, true);
350                 }
351
352                 void Validate (string testName, int testNumber, string idValue, string elementValue, XmlReaderSettings settings, bool shouldFail)
353                 {
354                         string content = string.Format ("<MyTest{0} Id=\"{1}\">{2}</MyTest{0}>", testNumber, idValue, elementValue);
355                         try
356                         {
357                                 XmlReader reader = XmlReader.Create (new StringReader (content), settings);
358                                 XmlDocument document = new XmlDocument ();
359                                 document.Load (reader);
360                                 document.Validate (null);
361                         } catch (Exception e) {
362                                 if (!shouldFail)
363                                         throw;
364                                 return;
365                         }
366                         if (shouldFail)
367                                 Assert.Fail (testName + " should fail");
368                 }
369
370                 [Test]
371                 public void Bug676993 ()
372                 {
373                         Validate (File.ReadAllText ("Test/XmlFiles/676993.xml"), File.ReadAllText ("Test/XmlFiles/676993.xsd"));
374                 }
375                 
376                 [Test]
377                 public void Bug10245 ()
378                 {
379                         string xsd = @"
380         <xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' targetNamespace='urn:foo'>
381           <xs:element name='root'>
382                 <xs:complexType>
383                   <xs:attribute name='d' default='v' use='optional' />
384                 </xs:complexType>
385           </xs:element>
386         </xs:schema>";
387                         string xml = "<root xmlns='urn:foo' />";
388                         var xrs = new XmlReaderSettings () { ValidationType = ValidationType.Schema };
389                         xrs.Schemas.Add (XmlSchema.Read (new StringReader (xsd), null));
390                         var xr = XmlReader.Create (new StringReader (xml), xrs);
391                         xr.Read ();
392                         bool more;
393                         Assert.AreEqual (2, xr.AttributeCount, "#1");
394                         int i = 0;
395                         for (more = xr.MoveToFirstAttribute (); more; more = xr.MoveToNextAttribute ())
396                                 i++;
397                         Assert.AreEqual (2, i, "#2");
398                 }
399                 
400                 [Test]
401                 public void Bug12035 ()
402                 {
403                         string xml = @"<UserSettings
404   xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
405   xmlns:xsd='http://www.w3.org/2001/XMLSchema'
406   xmlns='http://schema/test'><Enabled>false</Enabled><Time xsi:nil='true' /></UserSettings>";
407                         string xsd = @"<?xml version='1.0' encoding='utf-8'?>
408 <xs:schema
409   targetNamespace='http://schema/test'
410   xmlns='http://schema/test'
411   xmlns:xs='http://www.w3.org/2001/XMLSchema'
412   elementFormDefault='qualified'>
413   <xs:element name='UserSettings'>
414     <xs:complexType>
415       <xs:sequence>
416         <xs:element name='Enabled' type='xs:boolean' />
417         <xs:element name='Time' type='CoarseTime' nillable='true' />
418       </xs:sequence>
419     </xs:complexType>
420   </xs:element>
421
422   <xs:complexType name='CoarseTime'>
423     <xs:sequence>
424       <xs:element name='Hours' type='xs:int' />
425     </xs:sequence>
426   </xs:complexType>
427 </xs:schema>";
428                         var schema = XmlSchema.Read (new StringReader (xsd), null);
429                         var schemaSet = new XmlSchemaSet ();
430                         schemaSet.Add (schema);
431                         var xmlReaderSettings = new XmlReaderSettings { ValidationType = ValidationType.Schema };
432                         xmlReaderSettings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings;
433                         xmlReaderSettings.Schemas.Add (schemaSet);
434                         
435                         using (var configStream = new StringReader (xml)) {
436                                 using (var validatingReader = XmlReader.Create (configStream, xmlReaderSettings)) {
437                                         // Read the XML, throwing an exception if a validation error occurs
438                                         while (validatingReader.Read()) {
439                                         }
440                                 }
441                         }
442                 }
443                 
444                 [Test]
445                 public void IgnoresInvalidBaseUri ()
446                 {
447                         var source = new StringReader (@"<?xml version='1.0' encoding='utf-8'?><Test></Test>");
448                         var readerSettings = new XmlReaderSettings { ValidationType = ValidationType.Schema };
449                         var reader = XmlReader.Create (source, readerSettings, "invalidBaseUri");
450
451                         Assert.IsNotNull (reader);
452                 }
453         }
454 }
455