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