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 Microsoft.Test.ModuleCore; 6 using System; 7 using System.IO; 8 using System.Text; 9 using System.Xml; 10 11 namespace CoreXml.Test.XLinq 12 { 13 public partial class FunctionalTests : TestModule 14 { 15 public partial class XNodeReaderTests : XLinqTestCase 16 { 17 public partial class TCReadContentAsBinHex : BridgeHelpers 18 { 19 public const string ST_ELEM_NAME1 = "ElemAll"; 20 public const string ST_ELEM_NAME2 = "ElemEmpty"; 21 public const string ST_ELEM_NAME3 = "ElemNum"; 22 public const string ST_ELEM_NAME4 = "ElemText"; 23 public const string ST_ELEM_NAME5 = "ElemNumText"; 24 public const string ST_ELEM_NAME6 = "ElemLong"; 25 public const string strTextBinHex = "ABCDEF"; 26 public const string strNumBinHex = "0123456789"; 27 Init()28 public override void Init() 29 { 30 base.Init(); 31 CreateBinHexTestFile(pBinHexXml); 32 } 33 Terminate()34 public override void Terminate() 35 { 36 base.Terminate(); 37 } 38 VerifyInvalidReadBinHex(int iBufferSize, int iIndex, int iCount, Type exceptionType)39 private bool VerifyInvalidReadBinHex(int iBufferSize, int iIndex, int iCount, Type exceptionType) 40 { 41 bool bPassed = false; 42 byte[] buffer = new byte[iBufferSize]; 43 44 XmlReader DataReader = GetReader(pBinHexXml); 45 PositionOnElement(DataReader, ST_ELEM_NAME1); 46 DataReader.Read(); 47 48 if (!DataReader.CanReadBinaryContent) return true; 49 try 50 { 51 DataReader.ReadContentAsBinHex(buffer, iIndex, iCount); 52 } 53 catch (Exception e) 54 { 55 bPassed = (e.GetType().ToString() == exceptionType.ToString()); 56 if (!bPassed) 57 { 58 TestLog.WriteLine("Actual exception:{0}", e.GetType().ToString()); 59 TestLog.WriteLine("Expected exception:{0}", exceptionType.ToString()); 60 } 61 } 62 63 return bPassed; 64 } 65 TestInvalidNodeType(XmlNodeType nt)66 protected void TestInvalidNodeType(XmlNodeType nt) 67 { 68 XmlReader DataReader = GetReader(pBinHexXml); 69 70 PositionOnNodeType(DataReader, nt); 71 string name = DataReader.Name; 72 string value = DataReader.Value; 73 74 byte[] buffer = new byte[1]; 75 if (!DataReader.CanReadBinaryContent) return; 76 77 try 78 { 79 int nBytes = DataReader.ReadContentAsBinHex(buffer, 0, 1); 80 } 81 catch (InvalidOperationException) 82 { 83 return; 84 } 85 TestLog.Compare(false, "Invalid OP exception not thrown on wrong nodetype"); 86 } 87 88 //[Variation("ReadBinHex Element with all valid value")] TestReadBinHex_1()89 public void TestReadBinHex_1() 90 { 91 int binhexlen = 0; 92 byte[] binhex = new byte[1000]; 93 94 XmlReader DataReader = GetReader(pBinHexXml); 95 PositionOnElement(DataReader, ST_ELEM_NAME1); 96 DataReader.Read(); 97 if (!DataReader.CanReadBinaryContent) return; 98 99 binhexlen = DataReader.ReadContentAsBinHex(binhex, 0, binhex.Length); 100 101 string strActbinhex = ""; 102 for (int i = 0; i < binhexlen; i = i + 2) 103 { 104 strActbinhex += System.BitConverter.ToChar(binhex, i); 105 } 106 TestLog.Compare(strActbinhex, (strNumBinHex + strTextBinHex), "1. Compare All Valid BinHex"); 107 } 108 109 //[Variation("ReadBinHex Element with all valid Num value", Priority = 0)] TestReadBinHex_2()110 public void TestReadBinHex_2() 111 { 112 int BinHexlen = 0; 113 byte[] BinHex = new byte[1000]; 114 115 XmlReader DataReader = GetReader(pBinHexXml); 116 PositionOnElement(DataReader, ST_ELEM_NAME3); 117 DataReader.Read(); 118 if (!DataReader.CanReadBinaryContent) return; 119 120 BinHexlen = DataReader.ReadContentAsBinHex(BinHex, 0, BinHex.Length); 121 122 string strActBinHex = ""; 123 for (int i = 0; i < BinHexlen; i = i + 2) 124 { 125 strActBinHex += System.BitConverter.ToChar(BinHex, i); 126 } 127 128 TestLog.Compare(strActBinHex, strNumBinHex, "Compare All Valid BinHex"); 129 } 130 131 //[Variation("ReadBinHex Element with all valid Text value")] TestReadBinHex_3()132 public void TestReadBinHex_3() 133 { 134 int BinHexlen = 0; 135 byte[] BinHex = new byte[1000]; 136 137 XmlReader DataReader = GetReader(pBinHexXml); 138 PositionOnElement(DataReader, ST_ELEM_NAME4); 139 DataReader.Read(); 140 if (!DataReader.CanReadBinaryContent) return; 141 142 BinHexlen = DataReader.ReadContentAsBinHex(BinHex, 0, BinHex.Length); 143 144 string strActBinHex = ""; 145 for (int i = 0; i < BinHexlen; i = i + 2) 146 { 147 strActBinHex += System.BitConverter.ToChar(BinHex, i); 148 } 149 150 TestLog.Compare(strActBinHex, strTextBinHex, "Compare All Valid BinHex"); 151 } 152 153 //[Variation("ReadBinHex Element on CDATA", Priority = 0)] TestReadBinHex_4()154 public void TestReadBinHex_4() 155 { 156 int BinHexlen = 0; 157 byte[] BinHex = new byte[3]; 158 159 string xmlStr = "<root><![CDATA[ABCDEF]]></root>"; 160 XmlReader DataReader = GetReader(new StringReader(xmlStr)); 161 PositionOnElement(DataReader, "root"); 162 DataReader.Read(); 163 if (!DataReader.CanReadBinaryContent) return; 164 165 BinHexlen = DataReader.ReadContentAsBinHex(BinHex, 0, BinHex.Length); 166 TestLog.Compare(BinHexlen, 3, "BinHex"); 167 BinHexlen = DataReader.ReadContentAsBinHex(BinHex, 0, BinHex.Length); 168 TestLog.Compare(BinHexlen, 0, "BinHex"); 169 170 DataReader.Read(); 171 TestLog.Compare(DataReader.NodeType, XmlNodeType.None, "Not on none"); 172 } 173 174 //[Variation("ReadBinHex Element with all valid value (from concatenation), Priority=0")] TestReadBinHex_5()175 public void TestReadBinHex_5() 176 { 177 int BinHexlen = 0; 178 byte[] BinHex = new byte[1000]; 179 180 XmlReader DataReader = GetReader(pBinHexXml); 181 PositionOnElement(DataReader, ST_ELEM_NAME5); 182 DataReader.Read(); 183 if (!DataReader.CanReadBinaryContent) return; 184 185 BinHexlen = DataReader.ReadContentAsBinHex(BinHex, 0, BinHex.Length); 186 187 string strActBinHex = ""; 188 for (int i = 0; i < BinHexlen; i = i + 2) 189 { 190 strActBinHex += System.BitConverter.ToChar(BinHex, i); 191 } 192 TestLog.Compare(strActBinHex, (strNumBinHex + strTextBinHex), "Compare All Valid BinHex"); 193 } 194 195 //[Variation("ReadBinHex Element with all long valid value (from concatenation)")] TestReadBinHex_6()196 public void TestReadBinHex_6() 197 { 198 int BinHexlen = 0; 199 byte[] BinHex = new byte[2000]; 200 201 XmlReader DataReader = GetReader(pBinHexXml); 202 PositionOnElement(DataReader, ST_ELEM_NAME6); 203 DataReader.Read(); 204 if (!DataReader.CanReadBinaryContent) return; 205 206 BinHexlen = DataReader.ReadContentAsBinHex(BinHex, 0, BinHex.Length); 207 208 string strActBinHex = ""; 209 for (int i = 0; i < BinHexlen; i = i + 2) 210 { 211 strActBinHex += System.BitConverter.ToChar(BinHex, i); 212 } 213 214 string strExpBinHex = ""; 215 for (int i = 0; i < 10; i++) 216 strExpBinHex += (strNumBinHex + strTextBinHex); 217 218 TestLog.Compare(strActBinHex, strExpBinHex, "Compare All Valid BinHex"); 219 } 220 221 //[Variation("ReadBinHex with count > buffer size")] TestReadBinHex_7()222 public void TestReadBinHex_7() 223 { 224 BoolToLTMResult(VerifyInvalidReadBinHex(5, 0, 6, typeof(ArgumentOutOfRangeException))); 225 } 226 227 //[Variation("ReadBinHex with count < 0")] TestReadBinHex_8()228 public void TestReadBinHex_8() 229 { 230 BoolToLTMResult(VerifyInvalidReadBinHex(5, 2, -1, typeof(ArgumentOutOfRangeException))); 231 } 232 233 //[Variation("ReadBinHex with index > buffer size")] vReadBinHex_9()234 public void vReadBinHex_9() 235 { 236 BoolToLTMResult(VerifyInvalidReadBinHex(5, 5, 1, typeof(ArgumentOutOfRangeException))); 237 } 238 239 //[Variation("ReadBinHex with index < 0")] TestReadBinHex_10()240 public void TestReadBinHex_10() 241 { 242 BoolToLTMResult(VerifyInvalidReadBinHex(5, -1, 1, typeof(ArgumentOutOfRangeException))); 243 } 244 245 //[Variation("ReadBinHex with index + count exceeds buffer")] TestReadBinHex_11()246 public void TestReadBinHex_11() 247 { 248 BoolToLTMResult(VerifyInvalidReadBinHex(5, 0, 10, typeof(ArgumentOutOfRangeException))); 249 } 250 251 //[Variation("ReadBinHex index & count =0")] TestReadBinHex_12()252 public void TestReadBinHex_12() 253 { 254 byte[] buffer = new byte[5]; 255 int iCount = 0; 256 257 XmlReader DataReader = GetReader(pBinHexXml); 258 PositionOnElement(DataReader, ST_ELEM_NAME1); 259 DataReader.Read(); 260 if (!DataReader.CanReadBinaryContent) return; 261 262 try 263 { 264 iCount = DataReader.ReadContentAsBinHex(buffer, 0, 0); 265 } 266 catch (Exception e) 267 { 268 TestLog.WriteLine(e.ToString()); 269 throw new TestException(TestResult.Failed, ""); 270 } 271 272 TestLog.Compare(iCount, 0, "has to be zero"); 273 } 274 275 //[Variation("ReadBinHex Element multiple into same buffer (using offset), Priority=0")] TestReadBinHex_13()276 public void TestReadBinHex_13() 277 { 278 int BinHexlen = 10; 279 byte[] BinHex = new byte[BinHexlen]; 280 281 XmlReader DataReader = GetReader(pBinHexXml); 282 PositionOnElement(DataReader, ST_ELEM_NAME4); 283 DataReader.Read(); 284 if (!DataReader.CanReadBinaryContent) return; 285 286 string strActbinhex = ""; 287 for (int i = 0; i < BinHexlen; i = i + 2) 288 { 289 DataReader.ReadContentAsBinHex(BinHex, i, 2); 290 strActbinhex = (System.BitConverter.ToChar(BinHex, i)).ToString(); 291 TestLog.Compare(String.Compare(strActbinhex, 0, strTextBinHex, i / 2, 1), 0, "Compare All Valid Base64"); 292 } 293 } 294 295 //[Variation("ReadBinHex with buffer == null")] TestReadBinHex_14()296 public void TestReadBinHex_14() 297 { 298 XmlReader DataReader = GetReader(pBinHexXml); 299 300 PositionOnElement(DataReader, ST_ELEM_NAME4); 301 DataReader.Read(); 302 if (!DataReader.CanReadBinaryContent) return; 303 304 try 305 { 306 DataReader.ReadContentAsBinHex(null, 0, 0); 307 } 308 catch (ArgumentNullException) 309 { 310 return; 311 } 312 313 throw new TestException(TestResult.Failed, ""); 314 } 315 316 //[Variation("ReadBinHex after failed ReadBinHex")] TestReadBinHex_15()317 public void TestReadBinHex_15() 318 { 319 XmlReader DataReader = GetReader(pBinHexXml); 320 321 PositionOnElement(DataReader, "ElemErr"); 322 DataReader.Read(); 323 if (!DataReader.CanReadBinaryContent) return; 324 325 byte[] buffer = new byte[10]; 326 int nRead = 0; 327 try 328 { 329 nRead = DataReader.ReadContentAsBinHex(buffer, 0, 1); 330 throw new TestException(TestResult.Failed, ""); 331 } 332 catch (XmlException e) 333 { 334 int idx = e.Message.IndexOf("a&"); 335 TestLog.Compare(idx >= 0, "msg"); 336 CheckXmlException("Xml_UserException", e, 1, 968); 337 } 338 } 339 340 //[Variation("Read after partial ReadBinHex")] TestReadBinHex_16()341 public void TestReadBinHex_16() 342 { 343 XmlReader DataReader = GetReader(pBinHexXml); 344 345 PositionOnElement(DataReader, "ElemNum"); 346 DataReader.Read(); 347 if (!DataReader.CanReadBinaryContent) return; 348 349 byte[] buffer = new byte[10]; 350 int nRead = DataReader.ReadContentAsBinHex(buffer, 0, 8); 351 TestLog.Compare(nRead, 8, "0"); 352 353 DataReader.Read(); 354 TestLog.Compare(VerifyNode(DataReader, XmlNodeType.Element, "ElemText", String.Empty), "1vn"); 355 } 356 357 //[Variation("Current node on multiple calls")] TestReadBinHex_17()358 public void TestReadBinHex_17() 359 { 360 XmlReader DataReader = GetReader(pBinHexXml); 361 362 PositionOnElement(DataReader, "ElemNum"); 363 DataReader.Read(); 364 if (!DataReader.CanReadBinaryContent) return; 365 366 byte[] buffer = new byte[30]; 367 368 int nRead = DataReader.ReadContentAsBinHex(buffer, 0, 2); 369 TestLog.Compare(nRead, 2, "0"); 370 371 nRead = DataReader.ReadContentAsBinHex(buffer, 0, 19); 372 TestLog.Compare(nRead, 18, "1"); 373 TestLog.Compare(VerifyNode(DataReader, XmlNodeType.EndElement, "ElemNum", String.Empty), "1vn"); 374 } 375 376 //[Variation("ReadBinHex with whitespace")] TestTextReadBinHex_21()377 public void TestTextReadBinHex_21() 378 { 379 byte[] buffer = new byte[1]; 380 string strxml = "<abc> 1 1 B </abc>"; 381 XmlReader DataReader = GetReaderStr(strxml); 382 PositionOnElement(DataReader, "abc"); 383 DataReader.Read(); 384 if (!DataReader.CanReadBinaryContent) return; 385 int result = 0; 386 int nRead; 387 while ((nRead = DataReader.ReadContentAsBinHex(buffer, 0, 1)) > 0) 388 result += nRead; 389 390 TestLog.Compare(result, 1, "res"); 391 TestLog.Compare(buffer[0], (byte)17, "buffer[0]"); 392 } 393 394 //[Variation("ReadBinHex with odd number of chars")] TestTextReadBinHex_22()395 public void TestTextReadBinHex_22() 396 { 397 byte[] buffer = new byte[1]; 398 string strxml = "<abc>11B</abc>"; 399 XmlReader DataReader = GetReaderStr(strxml); 400 PositionOnElement(DataReader, "abc"); 401 DataReader.Read(); 402 if (!DataReader.CanReadBinaryContent) return; 403 int result = 0; 404 int nRead; 405 while ((nRead = DataReader.ReadContentAsBinHex(buffer, 0, 1)) > 0) 406 result += nRead; 407 408 TestLog.Compare(result, 1, "res"); 409 TestLog.Compare(buffer[0], (byte)17, "buffer[0]"); 410 } 411 412 //[Variation("ReadBinHex when end tag doesn't exist")] TestTextReadBinHex_23()413 public void TestTextReadBinHex_23() 414 { 415 byte[] buffer = new byte[5000]; 416 string strxml = "<B>" + new string('A', 5000); 417 try 418 { 419 XmlReader DataReader = GetReaderStr(strxml); 420 PositionOnElement(DataReader, "B"); 421 DataReader.Read(); 422 if (!DataReader.CanReadBinaryContent) return; 423 DataReader.ReadContentAsBinHex(buffer, 0, 5000); 424 TestLog.WriteLine("Accepted incomplete element"); 425 throw new TestException(TestResult.Failed, ""); 426 } 427 catch (XmlException e) 428 { 429 CheckXmlException("Xml_UnexpectedEOFInElementContent", e, 1, 5004); 430 } 431 } 432 433 //[Variation("WS:WireCompat:hex binary fails to send/return data after 1787 bytes going Whidbey to Everett")] TestTextReadBinHex_24()434 public void TestTextReadBinHex_24() 435 { 436 string filename = Path.Combine("TestData", "XmlReader", "Common", "Bug99148.xml"); 437 XmlReader DataReader = GetReader(filename); 438 439 DataReader.MoveToContent(); 440 int bytes = -1; 441 DataReader.Read(); 442 if (!DataReader.CanReadBinaryContent) return; 443 444 StringBuilder output = new StringBuilder(); 445 while (bytes != 0) 446 { 447 byte[] bbb = new byte[1024]; 448 bytes = DataReader.ReadContentAsBinHex(bbb, 0, bbb.Length); 449 for (int i = 0; i < bytes; i++) 450 { 451 output.AppendFormat(bbb[i].ToString()); 452 } 453 } 454 455 if (TestLog.Compare(output.ToString().Length, 1735, "Expected Length : 1735")) 456 return; 457 else 458 throw new TestException(TestResult.Failed, ""); 459 } 460 461 //[Variation("DebugAssert in ReadContentAsBinHex")] DebugAssertInReadContentAsBinHex()462 public void DebugAssertInReadContentAsBinHex() 463 { 464 XmlReader DataReader = GetReaderStr(@"<root> 465 <boo>hey</boo> 466 </root>"); 467 468 byte[] buffer = new byte[5]; 469 int iCount = 0; 470 while (DataReader.Read()) 471 { 472 if (DataReader.NodeType == XmlNodeType.Element) 473 break; 474 } 475 if (!DataReader.CanReadBinaryContent) return; 476 DataReader.Read(); 477 iCount = DataReader.ReadContentAsBinHex(buffer, 0, 0); 478 } 479 } 480 481 //[TestCase(Name = "ReadElementContentAsBinHex", Desc = "ReadElementContentAsBinHex")] 482 public partial class TCReadElementContentAsBinHex : BridgeHelpers 483 { 484 public const string ST_ELEM_NAME1 = "ElemAll"; 485 public const string ST_ELEM_NAME2 = "ElemEmpty"; 486 public const string ST_ELEM_NAME3 = "ElemNum"; 487 public const string ST_ELEM_NAME4 = "ElemText"; 488 public const string ST_ELEM_NAME5 = "ElemNumText"; 489 public const string ST_ELEM_NAME6 = "ElemLong"; 490 public const string strTextBinHex = "ABCDEF"; 491 public const string strNumBinHex = "0123456789"; 492 Init()493 public override void Init() 494 { 495 base.Init(); 496 CreateBinHexTestFile(pBinHexXml); 497 } 498 Terminate()499 public override void Terminate() 500 { 501 base.Terminate(); 502 } 503 VerifyInvalidReadBinHex(int iBufferSize, int iIndex, int iCount, Type exceptionType)504 private bool VerifyInvalidReadBinHex(int iBufferSize, int iIndex, int iCount, Type exceptionType) 505 { 506 bool bPassed = false; 507 byte[] buffer = new byte[iBufferSize]; 508 509 XmlReader DataReader = GetReader(pBinHexXml); 510 PositionOnElement(DataReader, ST_ELEM_NAME1); 511 if (!DataReader.CanReadBinaryContent) return true; 512 try 513 { 514 DataReader.ReadElementContentAsBinHex(buffer, iIndex, iCount); 515 } 516 catch (Exception e) 517 { 518 bPassed = (e.GetType().ToString() == exceptionType.ToString()); 519 if (!bPassed) 520 { 521 TestLog.WriteLine("Actual exception:{0}", e.GetType().ToString()); 522 TestLog.WriteLine("Expected exception:{0}", exceptionType.ToString()); 523 } 524 } 525 526 return bPassed; 527 } 528 TestInvalidNodeType(XmlNodeType nt)529 protected void TestInvalidNodeType(XmlNodeType nt) 530 { 531 XmlReader DataReader = GetReader(pBinHexXml); 532 533 PositionOnNodeType(DataReader, nt); 534 string name = DataReader.Name; 535 string value = DataReader.Value; 536 if (!DataReader.CanReadBinaryContent) return; 537 byte[] buffer = new byte[1]; 538 try 539 { 540 int nBytes = DataReader.ReadElementContentAsBinHex(buffer, 0, 1); 541 } 542 catch (InvalidOperationException) 543 { 544 return; 545 } 546 TestLog.Compare(false, "Invalid OP exception not thrown on wrong nodetype"); 547 } 548 549 //[Variation("ReadBinHex Element with all valid value")] TestReadBinHex_1()550 public void TestReadBinHex_1() 551 { 552 int binhexlen = 0; 553 byte[] binhex = new byte[1000]; 554 555 XmlReader DataReader = GetReader(pBinHexXml); 556 PositionOnElement(DataReader, ST_ELEM_NAME1); 557 if (!DataReader.CanReadBinaryContent) return; 558 559 binhexlen = DataReader.ReadElementContentAsBinHex(binhex, 0, binhex.Length); 560 561 string strActbinhex = ""; 562 for (int i = 0; i < binhexlen; i = i + 2) 563 { 564 strActbinhex += System.BitConverter.ToChar(binhex, i); 565 } 566 567 TestLog.Compare(strActbinhex, (strNumBinHex + strTextBinHex), "1. Compare All Valid BinHex"); 568 } 569 570 //[Variation("ReadBinHex Element with all valid Num value", Priority = 0)] TestReadBinHex_2()571 public void TestReadBinHex_2() 572 { 573 int BinHexlen = 0; 574 byte[] BinHex = new byte[1000]; 575 576 XmlReader DataReader = GetReader(pBinHexXml); 577 PositionOnElement(DataReader, ST_ELEM_NAME3); 578 if (!DataReader.CanReadBinaryContent) return; 579 580 BinHexlen = DataReader.ReadElementContentAsBinHex(BinHex, 0, BinHex.Length); 581 582 string strActBinHex = ""; 583 for (int i = 0; i < BinHexlen; i = i + 2) 584 { 585 strActBinHex += System.BitConverter.ToChar(BinHex, i); 586 } 587 TestLog.Compare(strActBinHex, strNumBinHex, "Compare All Valid BinHex"); 588 } 589 590 //[Variation("ReadBinHex Element with all valid Text value")] TestReadBinHex_3()591 public void TestReadBinHex_3() 592 { 593 int BinHexlen = 0; 594 byte[] BinHex = new byte[1000]; 595 596 XmlReader DataReader = GetReader(pBinHexXml); 597 PositionOnElement(DataReader, ST_ELEM_NAME4); 598 if (!DataReader.CanReadBinaryContent) return; 599 600 BinHexlen = DataReader.ReadElementContentAsBinHex(BinHex, 0, BinHex.Length); 601 602 string strActBinHex = ""; 603 for (int i = 0; i < BinHexlen; i = i + 2) 604 { 605 strActBinHex += System.BitConverter.ToChar(BinHex, i); 606 } 607 TestLog.Compare(strActBinHex, strTextBinHex, "Compare All Valid BinHex"); 608 } 609 610 //[Variation("ReadBinHex Element with Comments and PIs", Priority = 0)] TestReadBinHex_4()611 public void TestReadBinHex_4() 612 { 613 int BinHexlen = 0; 614 byte[] BinHex = new byte[3]; 615 616 XmlReader DataReader = GetReader(new StringReader("<root>AB<!--Comment-->CD<?pi target?>EF</root>")); 617 PositionOnElement(DataReader, "root"); 618 if (!DataReader.CanReadBinaryContent) return; 619 620 BinHexlen = DataReader.ReadElementContentAsBinHex(BinHex, 0, BinHex.Length); 621 TestLog.Compare(BinHexlen, 3, "BinHex"); 622 } 623 624 //[Variation("ReadBinHex Element with all valid value (from concatenation), Priority=0")] TestReadBinHex_5()625 public void TestReadBinHex_5() 626 { 627 int BinHexlen = 0; 628 byte[] BinHex = new byte[1000]; 629 630 XmlReader DataReader = GetReader(pBinHexXml); 631 PositionOnElement(DataReader, ST_ELEM_NAME5); 632 if (!DataReader.CanReadBinaryContent) return; 633 634 BinHexlen = DataReader.ReadElementContentAsBinHex(BinHex, 0, BinHex.Length); 635 636 string strActBinHex = ""; 637 for (int i = 0; i < BinHexlen; i = i + 2) 638 { 639 strActBinHex += System.BitConverter.ToChar(BinHex, i); 640 } 641 642 TestLog.Compare(strActBinHex, (strNumBinHex + strTextBinHex), "Compare All Valid BinHex"); 643 } 644 645 //[Variation("ReadBinHex Element with all long valid value (from concatenation)")] TestReadBinHex_6()646 public void TestReadBinHex_6() 647 { 648 int BinHexlen = 0; 649 byte[] BinHex = new byte[2000]; 650 651 XmlReader DataReader = GetReader(pBinHexXml); 652 PositionOnElement(DataReader, ST_ELEM_NAME6); 653 if (!DataReader.CanReadBinaryContent) return; 654 655 BinHexlen = DataReader.ReadElementContentAsBinHex(BinHex, 0, BinHex.Length); 656 657 string strActBinHex = ""; 658 for (int i = 0; i < BinHexlen; i = i + 2) 659 { 660 strActBinHex += System.BitConverter.ToChar(BinHex, i); 661 } 662 663 string strExpBinHex = ""; 664 for (int i = 0; i < 10; i++) 665 strExpBinHex += (strNumBinHex + strTextBinHex); 666 667 TestLog.Compare(strActBinHex, strExpBinHex, "Compare All Valid BinHex"); 668 } 669 670 //[Variation("ReadBinHex with count > buffer size")] TestReadBinHex_7()671 public void TestReadBinHex_7() 672 { 673 BoolToLTMResult(VerifyInvalidReadBinHex(5, 0, 6, typeof(ArgumentOutOfRangeException))); 674 } 675 676 //[Variation("ReadBinHex with count < 0")] TestReadBinHex_8()677 public void TestReadBinHex_8() 678 { 679 BoolToLTMResult(VerifyInvalidReadBinHex(5, 2, -1, typeof(ArgumentOutOfRangeException))); 680 } 681 682 //[Variation("ReadBinHex with index > buffer size")] vReadBinHex_9()683 public void vReadBinHex_9() 684 { 685 BoolToLTMResult(VerifyInvalidReadBinHex(5, 5, 1, typeof(ArgumentOutOfRangeException))); 686 } 687 688 //[Variation("ReadBinHex with index < 0")] TestReadBinHex_10()689 public void TestReadBinHex_10() 690 { 691 BoolToLTMResult(VerifyInvalidReadBinHex(5, -1, 1, typeof(ArgumentOutOfRangeException))); 692 } 693 694 //[Variation("ReadBinHex with index + count exceeds buffer")] TestReadBinHex_11()695 public void TestReadBinHex_11() 696 { 697 BoolToLTMResult(VerifyInvalidReadBinHex(5, 0, 10, typeof(ArgumentOutOfRangeException))); 698 } 699 700 //[Variation("ReadBinHex index & count =0")] TestReadBinHex_12()701 public void TestReadBinHex_12() 702 { 703 byte[] buffer = new byte[5]; 704 int iCount = 0; 705 706 XmlReader DataReader = GetReader(pBinHexXml); 707 PositionOnElement(DataReader, ST_ELEM_NAME1); 708 if (!DataReader.CanReadBinaryContent) return; 709 710 try 711 { 712 iCount = DataReader.ReadElementContentAsBinHex(buffer, 0, 0); 713 } 714 catch (Exception e) 715 { 716 TestLog.WriteLine(e.ToString()); 717 throw new TestException(TestResult.Failed, ""); 718 } 719 720 TestLog.Compare(iCount, 0, "has to be zero"); 721 } 722 723 //[Variation("ReadBinHex Element multiple into same buffer (using offset), Priority=0")] TestReadBinHex_13()724 public void TestReadBinHex_13() 725 { 726 int BinHexlen = 10; 727 byte[] BinHex = new byte[BinHexlen]; 728 729 XmlReader DataReader = GetReader(pBinHexXml); 730 PositionOnElement(DataReader, ST_ELEM_NAME4); 731 if (!DataReader.CanReadBinaryContent) return; 732 733 string strActbinhex = ""; 734 for (int i = 0; i < BinHexlen; i = i + 2) 735 { 736 DataReader.ReadElementContentAsBinHex(BinHex, i, 2); 737 strActbinhex = (System.BitConverter.ToChar(BinHex, i)).ToString(); 738 TestLog.Compare(String.Compare(strActbinhex, 0, strTextBinHex, i / 2, 1), 0, "Compare All Valid Base64"); 739 } 740 } 741 742 //[Variation("ReadBinHex with buffer == null")] TestReadBinHex_14()743 public void TestReadBinHex_14() 744 { 745 XmlReader DataReader = GetReader(pBinHexXml); 746 747 PositionOnElement(DataReader, ST_ELEM_NAME4); 748 if (!DataReader.CanReadBinaryContent) return; 749 try 750 { 751 DataReader.ReadElementContentAsBinHex(null, 0, 0); 752 } 753 catch (ArgumentNullException) 754 { 755 return; 756 } 757 758 throw new TestException(TestResult.Failed, ""); 759 } 760 761 //[Variation("ReadBinHex after failed ReadBinHex")] TestReadBinHex_15()762 public void TestReadBinHex_15() 763 { 764 XmlReader DataReader = GetReader(pBinHexXml); 765 766 PositionOnElement(DataReader, "ElemErr"); 767 if (!DataReader.CanReadBinaryContent) return; 768 769 byte[] buffer = new byte[10]; 770 int nRead = 0; 771 try 772 { 773 nRead = DataReader.ReadElementContentAsBinHex(buffer, 0, 1); 774 throw new TestException(TestResult.Failed, ""); 775 } 776 catch (XmlException e) 777 { 778 int idx = e.Message.IndexOf("a&"); 779 TestLog.Compare(idx >= 0, "msg"); 780 CheckXmlException("Xml_UserException", e, 1, 968); 781 } 782 } 783 784 //[Variation("Read after partial ReadBinHex")] TestReadBinHex_16()785 public void TestReadBinHex_16() 786 { 787 XmlReader DataReader = GetReader(pBinHexXml); 788 789 PositionOnElement(DataReader, "ElemNum"); 790 if (!DataReader.CanReadBinaryContent) return; 791 792 byte[] buffer = new byte[10]; 793 int nRead = DataReader.ReadElementContentAsBinHex(buffer, 0, 8); 794 TestLog.Compare(nRead, 8, "0"); 795 796 DataReader.Read(); 797 TestLog.Compare(DataReader.NodeType, XmlNodeType.Text, "Not on text node"); 798 } 799 800 //[Variation("ReadBinHex with whitespace")] TestTextReadBinHex_21()801 public void TestTextReadBinHex_21() 802 { 803 byte[] buffer = new byte[1]; 804 string strxml = "<abc> 1 1 B </abc>"; 805 XmlReader DataReader = GetReaderStr(strxml); 806 PositionOnElement(DataReader, "abc"); 807 if (!DataReader.CanReadBinaryContent) return; 808 int result = 0; 809 int nRead; 810 while ((nRead = DataReader.ReadElementContentAsBinHex(buffer, 0, 1)) > 0) 811 result += nRead; 812 813 TestLog.Compare(result, 1, "res"); 814 TestLog.Compare(buffer[0], (byte)17, "buffer[0]"); 815 } 816 817 //[Variation("ReadBinHex with odd number of chars")] TestTextReadBinHex_22()818 public void TestTextReadBinHex_22() 819 { 820 byte[] buffer = new byte[1]; 821 string strxml = "<abc>11B</abc>"; 822 XmlReader DataReader = GetReaderStr(strxml); 823 PositionOnElement(DataReader, "abc"); 824 if (!DataReader.CanReadBinaryContent) return; 825 int result = 0; 826 int nRead; 827 while ((nRead = DataReader.ReadElementContentAsBinHex(buffer, 0, 1)) > 0) 828 result += nRead; 829 830 TestLog.Compare(result, 1, "res"); 831 TestLog.Compare(buffer[0], (byte)17, "buffer[0]"); 832 } 833 834 //[Variation("ReadBinHex when end tag doesn't exist")] TestTextReadBinHex_23()835 public void TestTextReadBinHex_23() 836 { 837 byte[] buffer = new byte[5000]; 838 string strxml = "<B>" + new string('A', 5000); 839 try 840 { 841 XmlReader DataReader = GetReaderStr(strxml); 842 PositionOnElement(DataReader, "B"); 843 DataReader.ReadElementContentAsBinHex(buffer, 0, 5000); 844 TestLog.WriteLine("Accepted incomplete element"); 845 throw new TestException(TestResult.Failed, ""); 846 } 847 catch (XmlException e) 848 { 849 CheckXmlException("Xml_UnexpectedEOFInElementContent", e, 1, 5004); 850 } 851 } 852 853 //[Variation("WS:WireCompat:hex binary fails to send/return data after 1787 bytes going Whidbey to Everett")] TestTextReadBinHex_24()854 public void TestTextReadBinHex_24() 855 { 856 string filename = Path.Combine("TestData", "XmlReader", "Common", "Bug99148.xml"); 857 XmlReader DataReader = GetReader(filename); 858 859 DataReader.MoveToContent(); 860 if (!DataReader.CanReadBinaryContent) return; 861 int bytes = -1; 862 863 StringBuilder output = new StringBuilder(); 864 while (bytes != 0) 865 { 866 byte[] bbb = new byte[1024]; 867 bytes = DataReader.ReadElementContentAsBinHex(bbb, 0, bbb.Length); 868 for (int i = 0; i < bytes; i++) 869 { 870 output.AppendFormat(bbb[i].ToString()); 871 } 872 } 873 if (TestLog.Compare(output.ToString().Length, 1735, "Expected Length : 1735")) 874 return; 875 else 876 throw new TestException(TestResult.Failed, ""); 877 } 878 879 //[Variation("SubtreeReader inserted attributes don't work with ReadContentAsBinHex")] TestTextReadBinHex_25()880 public void TestTextReadBinHex_25() 881 { 882 string strxml = "<root xmlns='0102030405060708090a0B0c'><bar/></root>"; 883 using (XmlReader r = GetReader(new StringReader(strxml))) 884 { 885 r.Read(); 886 r.Read(); 887 using (XmlReader sr = r.ReadSubtree()) 888 { 889 if (!sr.CanReadBinaryContent) return; 890 sr.Read(); 891 sr.MoveToFirstAttribute(); 892 sr.MoveToFirstAttribute(); 893 byte[] bytes = new byte[4]; 894 while ((sr.ReadContentAsBinHex(bytes, 0, bytes.Length)) > 0) { } 895 } 896 } 897 } 898 } 899 } 900 } 901 } 902