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 Xunit; 7 8 namespace System.Xml.Tests 9 { 10 public abstract class TCWriteBuffer 11 { VerifyInvalidWrite(XmlWriterUtils utils, string methodName, int iBufferSize, int iIndex, int iCount, Type exceptionType)12 public void VerifyInvalidWrite(XmlWriterUtils utils, string methodName, int iBufferSize, int iIndex, int iCount, Type exceptionType) 13 { 14 byte[] byteBuffer = new byte[iBufferSize]; 15 for (int i = 0; i < iBufferSize; i++) 16 byteBuffer[i] = (byte)(i + '0'); 17 18 char[] charBuffer = new char[iBufferSize]; 19 for (int i = 0; i < iBufferSize; i++) 20 charBuffer[i] = (char)(i + '0'); 21 22 XmlWriter w = utils.CreateWriter(); 23 w.WriteStartElement("root"); 24 try 25 { 26 switch (methodName) 27 { 28 case "WriteBase64": 29 w.WriteBase64(byteBuffer, iIndex, iCount); 30 break; 31 case "WriteRaw": 32 w.WriteRaw(charBuffer, iIndex, iCount); 33 break; 34 case "WriteBinHex": 35 w.WriteBinHex(byteBuffer, iIndex, iCount); 36 break; 37 case "WriteChars": 38 w.WriteChars(charBuffer, iIndex, iCount); 39 break; 40 default: 41 CError.Compare(false, "Unexpected method name " + methodName); 42 break; 43 } 44 } 45 catch (Exception e) 46 { 47 CError.WriteLineIgnore("Exception: " + e.ToString()); 48 if (exceptionType.FullName.Equals(e.GetType().FullName)) 49 { 50 return; 51 } 52 else 53 { 54 CError.WriteLine("Did not throw exception of type {0}", exceptionType); 55 } 56 } 57 w.Flush(); 58 Assert.True(false, "Expected exception"); 59 } 60 StringToByteArray(string src)61 public byte[] StringToByteArray(string src) 62 { 63 byte[] base64 = new byte[src.Length * 2]; 64 65 for (int i = 0; i < src.Length; i++) 66 { 67 byte[] temp = System.BitConverter.GetBytes(src[i]); 68 base64[2 * i] = temp[0]; 69 base64[2 * i + 1] = temp[1]; 70 } 71 return base64; 72 } 73 ensureSpace(ref byte[] buffer, int len)74 public static void ensureSpace(ref byte[] buffer, int len) 75 { 76 if (len >= buffer.Length) 77 { 78 int originalLen = buffer.Length; 79 byte[] newBuffer = new byte[(int)(len * 2)]; 80 for (int i = 0; i < originalLen; newBuffer[i] = buffer[i++]) 81 { 82 // Intentionally Empty 83 } 84 buffer = newBuffer; 85 } 86 } WriteToBuffer(ref byte[] destBuff, ref int len, byte srcByte)87 public static void WriteToBuffer(ref byte[] destBuff, ref int len, byte srcByte) 88 { 89 ensureSpace(ref destBuff, len); 90 destBuff[len++] = srcByte; 91 } 92 WriteToBuffer(ref byte[] destBuff, ref int len, byte[] srcBuff)93 public static void WriteToBuffer(ref byte[] destBuff, ref int len, byte[] srcBuff) 94 { 95 int srcArrayLen = srcBuff.Length; 96 WriteToBuffer(ref destBuff, ref len, srcBuff, 0, (int)srcArrayLen); 97 } 98 WriteToBuffer(ref byte[] destBuff, ref int destStart, byte[] srcBuff, int srcStart, int count)99 public static void WriteToBuffer(ref byte[] destBuff, ref int destStart, byte[] srcBuff, int srcStart, int count) 100 { 101 ensureSpace(ref destBuff, destStart + count - 1); 102 for (int i = srcStart; i < srcStart + count; i++) 103 { 104 destBuff[destStart++] = srcBuff[i]; 105 } 106 } 107 WriteToBuffer(ref byte[] destBuffer, ref int destBuffLen, String strValue)108 public static void WriteToBuffer(ref byte[] destBuffer, ref int destBuffLen, String strValue) 109 { 110 for (int i = 0; i < strValue.Length; i++) 111 { 112 WriteToBuffer(ref destBuffer, ref destBuffLen, System.BitConverter.GetBytes(strValue[i])); 113 } 114 115 WriteToBuffer(ref destBuffer, ref destBuffLen, System.BitConverter.GetBytes('\0')); 116 } 117 } 118 } 119