1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
4 
5 using OLEDB.Test.ModuleCore;
6 using System.IO;
7 using XmlCoreTest.Common;
8 
9 namespace System.Xml.Tests
10 {
11     /////////////////////////////////////////////////////////////////////////
12     // TestCase ReadOuterXml
13     //
14     /////////////////////////////////////////////////////////////////////////
15     [InheritRequired()]
16     public abstract partial class TCReadToDescendant : TCXMLReaderBaseGeneral
17     {
18         #region XMLSTR
19         private string _xmlStr = @"<?xml version='1.0'?>
20                                                     <root><!--Comment-->
21                                                         <elem><!-- Comment -->
22                                                             <child1 att='1'><?pi target?>
23                                                                 <child2 xmlns='child2'>
24                                                                     <child3/>
25                                                                     blahblahblah<![CDATA[ blah ]]>
26                                                                     <child4/>
27                                                                 </child2>
28                                                             <?pi target1?>
29                                                             </child1>
30                                                         </elem>
31                                                         <elem att='1'>
32                                                             <child1 att='1'>
33                                                                 <child2 xmlns='child2'>
34                                                                     <child3/>
35                                                                     blahblahblah
36                                                                     <child4/>
37                                                                 </child2>
38                                                             <?pi target1?>
39                                                             </child1>
40                                                         </elem>
41                                                         <elem xmlns='elem'>
42                                                             <child1 att='1'>
43                                                                 <child2 xmlns='child2'>
44                                                                     <child3/>
45                                                                     blahblahblah2
46                                                                     <child4/>
47                                                                 </child2>
48                                                             </child1>
49                                                         </elem>
50                                                         <elem xmlns='elem' att='1'>
51                                                             <child1 att='1'>
52                                                                 <child2 xmlns='child2'>
53                                                                     <child3/>
54                                                                     blahblahblah2
55                                                                     <child4/>
56                                                                 </child2>
57                                                             </child1>
58                                                         </elem>
59                                                         <e:elem xmlns:e='elem2'>
60                                                             <e:child1 att='1'>
61                                                                 <e:child2 xmlns='child2'>
62                                                                     <e:child3/>
63                                                                     blahblahblah2
64                                                                     <e:child4/>
65                                                                 </e:child2>
66                                                             </e:child1>
67                                                         </e:elem>
68                                                         <e:elem xmlns:e='elem2' att='1'>
69                                                             <e:child1 att='1'>
70                                                                 <e:child2 xmlns='child2'>
71                                                                     <e:child3/>
72                                                                     blahblahblah2
73                                                                     <e:child4/>
74                                                                 </e:child2>
75                                                             </e:child1>
76                                                         </e:elem>
77                                                     </root>";
78 
79         #endregion
80 
81         //[Variation("Simple positive test", Pri = 0, Params = new object[] { "NNS" })]
82         //[Variation("Simple positive test", Pri = 0, Params = new object[] { "DNS" })]
83         //[Variation("Simple positive test", Pri = 0, Params = new object[] { "NS" })]
v()84         public int v()
85         {
86             string type = CurVariation.Params[0].ToString();
87             CError.WriteLine("Test Type : " + type);
88 
89             ReloadSource(new StringReader(_xmlStr));
90             DataReader.PositionOnElement("root");
91 
92             switch (type)
93             {
94                 case "NNS":
95                     DataReader.ReadToDescendant("elem");
96                     if (DataReader.HasAttributes)
97                     {
98                         CError.WriteLine("Positioned on wrong element");
99                         CError.WriteIgnore(DataReader.ReadInnerXml() + "\n");
100                         return TEST_FAIL;
101                     }
102                     while (DataReader.Read()) ;
103                     DataReader.Close();
104                     return TEST_PASS;
105 
106                 case "DNS":
107                     DataReader.ReadToDescendant("elem", "elem");
108                     if (DataReader.HasAttributes)
109                     {
110                         if (DataReader.GetAttribute("xmlns") == null)
111                         {
112                             CError.WriteLine("Positioned on wrong element, not on DNS");
113                             return TEST_FAIL;
114                         }
115                     }
116                     while (DataReader.Read()) ;
117                     DataReader.Close();
118                     return TEST_PASS;
119 
120                 case "NS":
121                     DataReader.ReadToDescendant("e:elem");
122                     if (DataReader.HasAttributes)
123                     {
124                         if (DataReader.GetAttribute("xmlns:e") == null)
125                         {
126                             CError.WriteLine("Positioned on wrong element, not on NS");
127                             return TEST_FAIL;
128                         }
129                     }
130                     while (DataReader.Read()) ;
131                     DataReader.Close();
132                     return TEST_PASS;
133                 default:
134                     throw new CTestFailedException("Error in Test type");
135             }
136         }
137 
138         [Variation("Read on a deep tree atleast more than 4K boundary", Pri = 2)]
v2()139         public int v2()
140         {
141             ManagedNodeWriter mnw = new ManagedNodeWriter();
142             mnw.PutPattern("X");
143 
144             int count = 0;
145             do
146             {
147                 mnw.PutPattern("E/");
148                 count++;
149             }
150             while (mnw.GetNodes().Length < 4096);
151             mnw.PutText("<a/>");
152             mnw.Finish();
153             CError.WriteIgnore(mnw.GetNodes() + "\n");
154 
155 
156             ReloadSource(new StringReader(mnw.GetNodes()));
157             DataReader.PositionOnElement("ELEMENT_1");
158             CError.WriteLine("Reading to : " + "a");
159             DataReader.ReadToDescendant("a");
160 
161             CError.Compare(DataReader.Depth, count, "Depth is not correct");
162             CError.Compare(DataReader.NodeType, XmlNodeType.Element, "Nodetype is not correct");
163 
164             while (DataReader.Read()) ;
165             DataReader.Close();
166 
167             return TEST_PASS;
168         }
169         [Variation("Read on a deep tree atleast more than 65535 boundary", Pri = 2)]
v2_1()170         public int v2_1()
171         {
172             ManagedNodeWriter mnw = new ManagedNodeWriter();
173             mnw.PutPattern("X");
174 
175             int count = 0;
176             do
177             {
178                 mnw.PutPattern("E/");
179                 count++;
180             } while (count < 65536);
181             mnw.PutText("<a/>");
182             mnw.Finish();
183 
184             ReloadSource(new StringReader(mnw.GetNodes()));
185             DataReader.PositionOnElement("ELEMENT_1");
186 
187             CError.WriteLine("Reading to : a");
188             DataReader.ReadToDescendant("a");
189 
190             CError.Compare(DataReader.Depth, 65536, "Depth is not correct");
191             CError.Compare(DataReader.NodeType, XmlNodeType.Element, "Nodetype is not correct");
192 
193             while (DataReader.Read()) ;
194             DataReader.Close();
195 
196             return TEST_PASS;
197         }
198 
199         //[Variation("Read on descendant with same names", Pri = 1, Params = new object[] { "NNS" })]
200         //[Variation("Read on descendant with same names", Pri = 1, Params = new object[] { "DNS" })]
201         //[Variation("Read on descendant with same names", Pri = 1, Params = new object[] { "NS" })]
v3()202         public int v3()
203         {
204             string type = CurVariation.Params[0].ToString();
205             CError.WriteLine("Test Type : " + type);
206 
207             ReloadSource(new StringReader(_xmlStr));
208             DataReader.PositionOnElement("root");
209 
210             //Doing a sequential read.
211             switch (type)
212             {
213                 case "NNS":
214                     DataReader.ReadToDescendant("elem");
215                     int depth = DataReader.Depth;
216                     if (DataReader.HasAttributes)
217                     {
218                         CError.WriteLine("Positioned on wrong element");
219                         return TEST_FAIL;
220                     }
221                     CError.Compare(DataReader.ReadToDescendant("elem"), false, "There are no more descendants");
222                     CError.Compare(DataReader.NodeType, XmlNodeType.EndElement, "Wrong node type");
223                     while (DataReader.Read()) ;
224                     DataReader.Close();
225 
226                     return TEST_PASS;
227 
228                 case "DNS":
229                     DataReader.ReadToDescendant("elem", "elem");
230                     if (DataReader.HasAttributes)
231                     {
232                         if (DataReader.GetAttribute("xmlns") == null)
233                         {
234                             CError.WriteLine("Positioned on wrong element, not on DNS");
235                             return TEST_FAIL;
236                         }
237                     }
238                     CError.Compare(DataReader.ReadToDescendant("elem", "elem"), false, "There are no more descendants");
239                     CError.Compare(DataReader.NodeType, XmlNodeType.EndElement, "Wrong node type");
240                     while (DataReader.Read()) ;
241                     DataReader.Close();
242 
243                     return TEST_PASS;
244 
245                 case "NS":
246                     DataReader.ReadToDescendant("e:elem");
247                     if (DataReader.HasAttributes)
248                     {
249                         if (DataReader.GetAttribute("xmlns:e") == null)
250                         {
251                             CError.WriteLine("Positioned on wrong element, not on DNS");
252                             return TEST_FAIL;
253                         }
254                     }
255                     CError.Compare(DataReader.ReadToDescendant("e:elem"), false, "There are no more descendants");
256                     CError.Compare(DataReader.NodeType, XmlNodeType.EndElement, "Wrong node type");
257                     while (DataReader.Read()) ;
258                     DataReader.Close();
259 
260                     return TEST_PASS;
261 
262                 default:
263                     throw new CTestFailedException("Error in Test type");
264             }
265         }
266 
267         [Variation("If name not found, stop at end element of the subtree", Pri = 1)]
v4()268         public int v4()
269         {
270             ReloadSource(new StringReader(_xmlStr));
271             DataReader.PositionOnElement("elem");
272 
273             CError.Compare(DataReader.ReadToDescendant("abc"), false, "Reader returned true for an invalid name");
274             CError.Compare(DataReader.NodeType, XmlNodeType.EndElement, "Wrong node type");
275 
276             DataReader.Read();
277             CError.Compare(DataReader.ReadToDescendant("abc", "elem"), false, "reader returned true for an invalid name,ns combination");
278 
279             while (DataReader.Read()) ;
280             DataReader.Close();
281 
282             return TEST_PASS;
283         }
284 
285         [Variation("Positioning on a level and try to find the name which is on a level higher", Pri = 1)]
v5()286         public int v5()
287         {
288             ReloadSource(new StringReader(_xmlStr));
289             DataReader.PositionOnElement("child3");
290 
291             CError.Compare(DataReader.ReadToDescendant("child1"), false, "Reader returned true for an invalid name");
292             CError.Compare(DataReader.NodeType, XmlNodeType.Element, "Wrong node type");
293             CError.Compare(DataReader.LocalName, "child3", "Wrong name");
294 
295             DataReader.PositionOnElement("child3");
296 
297             CError.Compare(DataReader.ReadToDescendant("child2", "child2"), false, "Reader returned true for an invalid name,ns");
298             CError.Compare(DataReader.NodeType, XmlNodeType.Element, "Wrong node type for name,ns");
299 
300             while (DataReader.Read()) ;
301             DataReader.Close();
302 
303             return TEST_PASS;
304         }
305 
306         [Variation("Read to Descendant on one level and again to level below it", Pri = 1)]
v6()307         public int v6()
308         {
309             ReloadSource(new StringReader(_xmlStr));
310             DataReader.PositionOnElement("root");
311 
312             CError.Compare(DataReader.ReadToDescendant("elem"), true, "Cant find elem");
313             CError.Compare(DataReader.ReadToDescendant("child1"), true, "Cant find child1");
314             CError.Compare(DataReader.ReadToDescendant("child2"), true, "Cant find child2");
315             CError.Compare(DataReader.ReadToDescendant("child3"), true, "Cant find child3");
316             CError.Compare(DataReader.ReadToDescendant("child4"), false, "Shouldn't find child4");
317             CError.Compare(DataReader.NodeType, XmlNodeType.Element, "Not on EndElement");
318             DataReader.Read();
319             CError.Compare(DataReader.NodeType, XmlNodeType.Text, "Not on Element");
320 
321             while (DataReader.Read()) ;
322             DataReader.Close();
323 
324             return TEST_PASS;
325         }
326 
327         [Variation("Read to Descendant on one level and again to level below it, with namespace", Pri = 1)]
v7()328         public int v7()
329         {
330             ReloadSource(new StringReader(_xmlStr));
331             DataReader.PositionOnElement("root");
332 
333             CError.Compare(DataReader.ReadToDescendant("elem", "elem"), true, "Cant find elem");
334             CError.Compare(DataReader.ReadToDescendant("child1", "elem"), true, "Cant find child1");
335             CError.Compare(DataReader.ReadToDescendant("child2", "child2"), true, "Cant find child2");
336             CError.Compare(DataReader.ReadToDescendant("child3", "child2"), true, "Cant find child3");
337             CError.Compare(DataReader.ReadToDescendant("child4", "child2"), false, "Shouldn't find child4");
338             CError.Compare(DataReader.NodeType, XmlNodeType.Element, "Not on EndElement");
339             DataReader.Read();
340             CError.Compare(DataReader.NodeType, XmlNodeType.Text, "Not on Element");
341 
342             while (DataReader.Read()) ;
343             DataReader.Close();
344 
345             return TEST_PASS;
346         }
347 
348         [Variation("Read to Descendant on one level and again to level below it, with prefix", Pri = 1)]
v8()349         public int v8()
350         {
351             ReloadSource(new StringReader(_xmlStr));
352             DataReader.PositionOnElement("root");
353 
354             CError.Compare(DataReader.ReadToDescendant("e:elem"), true, "Cant find elem");
355             CError.Compare(DataReader.ReadToDescendant("e:child1"), true, "Cant find child1");
356             CError.Compare(DataReader.ReadToDescendant("e:child2"), true, "Cant find child2");
357             CError.Compare(DataReader.ReadToDescendant("e:child3"), true, "Cant find child3");
358             CError.Compare(DataReader.ReadToDescendant("e:child4"), false, "Shouldn't find child4");
359             CError.Compare(DataReader.NodeType, XmlNodeType.Element, "Not on EndElement");
360             DataReader.Read();
361             CError.Compare(DataReader.NodeType, XmlNodeType.Text, "Not on Element");
362 
363             while (DataReader.Read()) ;
364             DataReader.Close();
365 
366             return TEST_PASS;
367         }
368 
369         [Variation("Multiple Reads to children and then next siblings, NNS", Pri = 2)]
v9()370         public int v9()
371         {
372             ReloadSource(new StringReader(_xmlStr));
373             DataReader.PositionOnElement("root");
374 
375             CError.Compare(DataReader.ReadToDescendant("elem"), true, "Read fails elem");
376             CError.Compare(DataReader.ReadToDescendant("child3"), true, "Read fails child3");
377             CError.Compare(DataReader.ReadToNextSibling("child4"), true, "Read fails child4");
378 
379             while (DataReader.Read()) ;
380             DataReader.Close();
381 
382             return TEST_PASS;
383         }
384 
385         [Variation("Multiple Reads to children and then next siblings, DNS", Pri = 2)]
v10()386         public int v10()
387         {
388             ReloadSource(new StringReader(_xmlStr));
389             DataReader.PositionOnElement("root");
390 
391             CError.Compare(DataReader.ReadToDescendant("elem", "elem"), true, "Read fails elem");
392             CError.Compare(DataReader.ReadToDescendant("child3", "child2"), true, "Read fails child3");
393             CError.Compare(DataReader.ReadToNextSibling("child4", "child2"), true, "Read fails child4");
394 
395             while (DataReader.Read()) ;
396             DataReader.Close();
397 
398             return TEST_PASS;
399         }
400 
401         [Variation("Multiple Reads to children and then next siblings, NS", Pri = 2)]
v11()402         public int v11()
403         {
404             ReloadSource(new StringReader(_xmlStr));
405             DataReader.PositionOnElement("root");
406 
407             CError.Compare(DataReader.ReadToDescendant("e:elem"), true, "Read fails elem");
408             CError.Compare(DataReader.ReadToDescendant("e:child3"), true, "Read fails child3");
409             CError.Compare(DataReader.ReadToNextSibling("e:child4"), true, "Read fails child4");
410 
411             while (DataReader.Read()) ;
412             DataReader.Close();
413 
414             return TEST_PASS;
415         }
416 
417         [Variation("Call from different nodetypes", Pri = 1)]
v12()418         public int v12()
419         {
420             ReloadSource(new StringReader(_xmlStr));
421 
422             while (DataReader.Read())
423             {
424                 if (DataReader.NodeType != XmlNodeType.Element)
425                 {
426                     CError.WriteLine(DataReader.NodeType.ToString());
427                     CError.Compare(DataReader.ReadToDescendant("child1"), false, "Fails on node");
428                 }
429                 else
430                 {
431                     if (DataReader.HasAttributes)
432                     {
433                         while (DataReader.MoveToNextAttribute())
434                         {
435                             CError.Compare(DataReader.ReadToDescendant("abc"), false, "Fails on attribute node");
436                         }
437                     }
438                 }
439             }
440 
441             DataReader.Close();
442 
443             return TEST_PASS;
444         }
445 
446         [Variation("Interaction with MoveToContent", Pri = 2)]
v13()447         public int v13()
448         {
449             return TEST_SKIPPED;
450         }
451 
452         [Variation("Only child has namespaces and read to it", Pri = 2)]
v14()453         public int v14()
454         {
455             ReloadSource(new StringReader(_xmlStr));
456             DataReader.PositionOnElement("root");
457 
458             CError.Compare(DataReader.ReadToDescendant("child2", "child2"), true, "Fails on attribute node");
459 
460             DataReader.Close();
461             return TEST_PASS;
462         }
463 
464         [Variation("Pass null to both arguments throws ArgumentException", Pri = 2)]
v15()465         public int v15()
466         {
467             ReloadSource(new StringReader("<root><b/></root>"));
468             DataReader.Read();
469             if (IsBinaryReader())
470                 DataReader.Read();
471 
472             try
473             {
474                 DataReader.ReadToDescendant(null);
475             }
476             catch (ArgumentNullException)
477             {
478                 CError.WriteLine("Caught for single param");
479             }
480 
481             try
482             {
483                 DataReader.ReadToDescendant("b", null);
484             }
485             catch (ArgumentNullException)
486             {
487                 CError.WriteLine("Caught for single param");
488             }
489 
490             while (DataReader.Read()) ;
491             DataReader.Close();
492 
493             return TEST_PASS;
494         }
495 
496         [Variation("Different names, same uri works correctly", Pri = 2)]
v17()497         public int v17()
498         {
499             ReloadSource(new StringReader("<root><child1 xmlns='foo'/>blah<child1 xmlns='bar'>blah</child1></root>"));
500             DataReader.Read();
501             if (IsBinaryReader())
502                 DataReader.Read();
503 
504             DataReader.ReadToDescendant("child1", "bar");
505             CError.Compare(DataReader.IsEmptyElement, false, "Not on the correct node");
506 
507             while (DataReader.Read()) ;
508             DataReader.Close();
509 
510             return TEST_PASS;
511         }
512 
513 
514         //[Variation("On Root Node", Pri = 0, Params = new object[] { "NNS" })]
515         //[Variation("On Root Node", Pri = 0, Params = new object[] { "DNS" })]
516         //[Variation("On Root Node", Pri = 0, Params = new object[] { "NS" })]
v18()517         public int v18()
518         {
519             string type = CurVariation.Params[0].ToString();
520             CError.WriteLine("Test Type : " + type);
521 
522             ReloadSource(new StringReader(_xmlStr));
523             switch (type)
524             {
525                 case "NNS":
526                     DataReader.ReadToDescendant("elem");
527                     if (DataReader.HasAttributes)
528                     {
529                         CError.WriteLine("Positioned on wrong element");
530                         CError.WriteIgnore(DataReader.ReadInnerXml() + "\n");
531                         return TEST_FAIL;
532                     }
533                     while (DataReader.Read()) ;
534                     DataReader.Close();
535                     return TEST_PASS;
536 
537                 case "DNS":
538                     DataReader.ReadToDescendant("elem", "elem");
539                     if (DataReader.HasAttributes)
540                     {
541                         if (DataReader.GetAttribute("xmlns") == null)
542                         {
543                             CError.WriteLine("Positioned on wrong element, not on DNS");
544                             return TEST_FAIL;
545                         }
546                     }
547                     while (DataReader.Read()) ;
548                     DataReader.Close();
549                     return TEST_PASS;
550 
551                 case "NS":
552                     DataReader.ReadToDescendant("e:elem");
553                     if (DataReader.HasAttributes)
554                     {
555                         if (DataReader.GetAttribute("xmlns:e") == null)
556                         {
557                             CError.WriteLine("Positioned on wrong element, not on NS");
558                             return TEST_FAIL;
559                         }
560                     }
561                     while (DataReader.Read()) ;
562                     DataReader.Close();
563                     return TEST_PASS;
564                 default:
565                     throw new CTestFailedException("Error in Test type");
566             }
567         }
568 
569         [Variation("Assertion failed when call XmlReader.ReadToDescendant() for non-existing node", Pri = 1)]
v19()570         public int v19()
571         {
572             ReloadSource(new StringReader("<a>b</a>"));
573             CError.Compare(DataReader.ReadToDescendant("foo"), false, "Should fail without assert");
574 
575             DataReader.Close();
576             return TEST_PASS;
577         }
578     }
579 }
580