public MFTestResults BoundaryCheck() { MFTestResults result = MFTestResults.Pass; try { for (int i = 250; i < 260; i++) { using (MemoryStream ms = new MemoryStream()) { TestWrite(ms, i); ms.Position = 0; if (!MemoryStreamHelper.VerifyRead(ms)) { result = MFTestResults.Fail; } Log.Comment("Position: " + ms.Position); Log.Comment("Length: " + ms.Length); if (i != ms.Position | i != ms.Length) { result = MFTestResults.Fail; Log.Exception("Expected Position and Length to be " + i); } } } } catch (Exception ex) { Log.Exception("Unexpected exception: " + ex.Message); result = MFTestResults.Fail; } return(result); }
public MFTestResults VerifyValues() { MFTestResults result = MFTestResults.Pass; try { Log.Comment("Verify Data"); using (MemoryStream ms = new MemoryStream()) { Log.Comment("Write 1000 bytes in specific pattern"); MemoryStreamHelper.Write(ms, 1000); byte[] stream = ms.ToArray(); if (!VerifyArray(stream, 1000)) { result = MFTestResults.Fail; } } } catch (Exception ex) { Log.Exception("Unexpected exception", ex); result = MFTestResults.Fail; } return(result); }
public MFTestResults ChangeLengths() { MFTestResults result = MFTestResults.Pass; try { Log.Comment("Verify array is still valid after truncation (copy array)"); using (MemoryStream ms = new MemoryStream()) { MemoryStreamHelper.Write(ms, 1000); ms.SetLength(200); ms.Flush(); byte[] stream = ms.ToArray(); if (!VerifyArray(stream, 200)) { result = MFTestResults.Fail; } } } catch (Exception ex) { Log.Exception("Unexpected exception", ex); result = MFTestResults.Fail; } return(result); }
public MFTestResults InvalidCases() { MFTestResults result = MFTestResults.Pass; try { MemoryStream ms2 = new MemoryStream(); MemoryStreamHelper.Write(ms2, 100); ms2.Seek(0, SeekOrigin.Begin); ms2.Close(); Log.Comment("Read from closed stream"); try { int readBytes = ms2.ReadByte(); result = MFTestResults.Fail; Log.Exception("Expected ObjectDisposedException, but read " + readBytes + " bytes"); } catch (ObjectDisposedException) { /* pass case */ } } catch (Exception ex) { Log.Exception("Unexpected exception", ex); result = MFTestResults.Fail; } return(result); }
public MFTestResults WriteTo_FileStream() { // Don't run test if no FileSystem if (!_fileSystemInit) { return(MFTestResults.Skip); } MFTestResults result = MFTestResults.Pass; string fileName = "WriteTo_FileStream.txt"; try { using (MemoryStream ms = new MemoryStream()) { Log.Comment("Initialize stream with 1234 bytes"); MemoryStreamHelper.Write(ms, 1234); using (FileStream fs = new FileStream(fileName, FileMode.Create)) { Log.Comment("WriteTo FileStream"); ms.WriteTo(fs); } Log.Comment("Verify closed file"); using (FileStream fs = new FileStream(fileName, FileMode.Open)) { if (fs.Length != 1234) { result = MFTestResults.Fail; Log.Exception("Expected 1234 bytes, but got " + fs.Length); } if (!MemoryStreamHelper.VerifyRead(fs)) { result = MFTestResults.Fail; } } } } catch (Exception ex) { Log.Exception("Unexpected exception", ex); result = MFTestResults.Fail; } return(result); }
public MFTestResults WriteTo_MemoryStream() { MFTestResults result = MFTestResults.Pass; try { using (MemoryStream ms = new MemoryStream()) { Log.Comment("Initialize stream with 1234 bytes"); MemoryStreamHelper.Write(ms, 1234); using (MemoryStream ms2 = new MemoryStream()) { Log.Comment("WriteTo MemoryStream"); ms.WriteTo(ms2); Log.Comment("Verify 2nd MemoryStream"); if (ms2.Length != 1234) { result = MFTestResults.Fail; Log.Exception("Expected 1234 bytes, but got " + ms2.Length); } ms2.Position = 0; if (!MemoryStreamHelper.VerifyRead(ms2)) { result = MFTestResults.Fail; } } } } catch (Exception ex) { Log.Exception("Unexpected exception", ex); result = MFTestResults.Fail; } return(result); }
public MFTestResults VanillaCases() { MFTestResults result = MFTestResults.Pass; try { using (MemoryStream ms = new MemoryStream()) { MemoryStreamHelper.Write(ms, 256); ms.Position = 0; Log.Comment("ReadBytes and verify"); for (int i = 0; i < 256; i++) { int b = ms.ReadByte(); if (b != i) { result = MFTestResults.Fail; Log.Exception("Expected " + i + " but got " + b); } } Log.Comment("Bytes past EOS should return -1"); int rb = ms.ReadByte(); if (rb != -1) { result = MFTestResults.Fail; Log.Exception("Expected -1 but got " + rb); } } } catch (Exception ex) { Log.Exception("Unexpected exception", ex); result = MFTestResults.Fail; } return(result); }
public MFTestResults InvalidCases() { MFTestResults result = MFTestResults.Pass; try { using (MemoryStream ms = new MemoryStream()) { Log.Comment("null Buffer"); try { int read = ms.Read(null, 0, 0); result = MFTestResults.Fail; Log.Exception("Expected ArgumentNullException, but read " + read + " bytes"); } catch (ArgumentNullException) { /* pass case */ } Log.Comment("negative offset"); try { int read = ms.Read(new byte[] { 1 }, -1, 0); result = MFTestResults.Fail; Log.Exception("Expected ArgumentOutOfRangeException, but read " + read + " bytes"); } catch (ArgumentOutOfRangeException) { /* pass case */ } Log.Comment("negative count"); try { int read = ms.Read(new byte[] { 1 }, 0, -1); result = MFTestResults.Fail; Log.Exception("Expected ArgumentOutOfRangeException, but read " + read + " bytes"); } catch (ArgumentOutOfRangeException) { /* pass case */ } Log.Comment("offset exceeds buffer size"); try { int read = ms.Read(new byte[] { 1 }, 2, 0); result = MFTestResults.Fail; Log.Exception("Expected ArgumentException, but read " + read + " bytes"); } catch (ArgumentException) { /* pass case */ } Log.Comment("count exceeds buffer size"); try { int read = ms.Read(new byte[] { 1 }, 0, 2); result = MFTestResults.Fail; Log.Exception("Expected ArgumentException, but read " + read + " bytes"); } catch (ArgumentException) { /* pass case */ } } MemoryStream ms2 = new MemoryStream(); MemoryStreamHelper.Write(ms2, 100); ms2.Seek(0, SeekOrigin.Begin); ms2.Close(); Log.Comment("Read from closed stream"); try { int readBytes = ms2.Read(new byte[] { 50 }, 0, 50); result = MFTestResults.Fail; Log.Exception("Expected ObjectDisposedException, but read " + readBytes + " bytes"); } catch (ObjectDisposedException) { /* pass case */ } } catch (Exception ex) { Log.Exception("Unexpected exception", ex); result = MFTestResults.Fail; } return(result); }
public MFTestResults VanillaRead() { MFTestResults result = MFTestResults.Pass; try { using (MemoryStream ms = new MemoryStream()) { // Write to stream then reset to beginning MemoryStreamHelper.Write(ms, 1000); ms.Seek(0, SeekOrigin.Begin); Log.Comment("Read 256 bytes of data"); if (!TestRead(ms, 256)) { result = MFTestResults.Fail; } Log.Comment("Request less bytes then buffer"); if (!TestRead(ms, 256, 100, 100)) { result = MFTestResults.Fail; } // 1000 - 256 - 100 = 644 Log.Comment("Request more bytes then file"); if (!TestRead(ms, 1000, 1000, 644)) { result = MFTestResults.Fail; } Log.Comment("Request bytes after EOF"); if (!TestRead(ms, 100, 100, 0)) { result = MFTestResults.Fail; } Log.Comment("Rewind and read entire file in one buffer larger then file"); ms.Seek(0, SeekOrigin.Begin); if (!TestRead(ms, 1001, 1001, 1000)) { result = MFTestResults.Fail; } Log.Comment("Rewind and read from middle"); ms.Position = 500; if (!TestRead(ms, 256)) { result = MFTestResults.Fail; } Log.Comment("Read position after EOS"); ms.Position = ms.Length + 10; if (!TestRead(ms, 100, 100, 0)) { result = MFTestResults.Fail; } Log.Comment("Verify Read validation with UTF8 string"); ms.SetLength(0); string test = "MFFramework Test"; ms.Write(UTF8Encoding.UTF8.GetBytes(test), 0, test.Length); ms.Flush(); ms.Seek(0, SeekOrigin.Begin); byte[] readbuff = new byte[20]; ms.Read(readbuff, 0, readbuff.Length); string testResult = new string(Encoding.UTF8.GetChars(readbuff)); if (test != testResult) { result = MFTestResults.Fail; Log.Comment("Exepected: " + test + ", but got: " + testResult); } } } catch (Exception ex) { Log.Exception("Unexpected exception", ex); result = MFTestResults.Fail; } return(result); }
public MFTestResults InvalidArgs() { MFTestResults result = MFTestResults.Pass; try { using (MemoryStream ms = new MemoryStream()) { Log.Comment("Initialize stream"); MemoryStreamHelper.Write(ms, 1000); try { Log.Comment("null stream"); ms.WriteTo(null); result = MFTestResults.Fail; Log.Exception("Expected ArgumentNullException"); } catch (ArgumentNullException) { /* pass case */ } if (_fileSystemInit) { try { Log.Comment("pass in read-only stream"); using (FileStream fs = new FileStream("readonly", FileMode.OpenOrCreate, FileAccess.Read)) { ms.WriteTo(fs); } } catch (NotSupportedException) { /* pass case */ } } try { Log.Comment("Target Stream closed"); MemoryStream mst = new MemoryStream(); mst.Close(); ms.WriteTo(mst); result = MFTestResults.Fail; Log.Exception("Expected ObjectDisposedException"); } catch (ObjectDisposedException) { /* pass case */ } try { Log.Comment("Current Stream closed"); ms.Close(); using (MemoryStream mst = new MemoryStream()) { ms.WriteTo(mst); result = MFTestResults.Fail; Log.Exception("Expected ObjectDisposedException"); } } catch (ObjectDisposedException) { /* pass case */ } } } catch (Exception ex) { Log.Exception("Unexpected exception", ex); result = MFTestResults.Fail; } return(result); }
public MFTestResults VanillaWrite_Static_Ctor() { MFTestResults result = MFTestResults.Pass; try { byte[] buffer = new byte[1024]; using (MemoryStream ms = new MemoryStream(buffer)) { Log.Comment("Write 256 bytes of data"); if (!TestWrite(ms, 256, 1024)) { result = MFTestResults.Fail; } Log.Comment("Write middle of buffer"); if (!TestWrite(ms, 256, 100, 100, 1024)) { result = MFTestResults.Fail; } // 1000 - 256 - 100 = 644 Log.Comment("Write start of buffer"); if (!TestWrite(ms, 1000, 644, 0, 1024)) { result = MFTestResults.Fail; } Log.Comment("Write past end of buffer"); try { TestWrite(ms, 50, 1024); result = MFTestResults.Fail; Log.Exception("Expected NotSupportedException"); } catch (NotSupportedException) { /* pass case */ } Log.Comment("Verify failed Write did not move position"); if (ms.Position != 1000) { result = MFTestResults.Fail; Log.Comment("Expected position to be 1000, but it is " + ms.Position); } Log.Comment("Write final 24 bytes of static buffer"); if (!TestWrite(ms, 24)) { result = MFTestResults.Fail; } Log.Comment("Rewind and verify all bytes written"); ms.Seek(0, SeekOrigin.Begin); if (!MemoryStreamHelper.VerifyRead(ms)) { result = MFTestResults.Fail; } Log.Comment("Verify Read validation with UTF8 string"); ms.SetLength(0); string test = "MFFramework Test"; ms.Write(UTF8Encoding.UTF8.GetBytes(test), 0, test.Length); ms.Flush(); ms.Seek(0, SeekOrigin.Begin); byte[] readbuff = new byte[20]; ms.Read(readbuff, 0, readbuff.Length); string testResult = new string(Encoding.UTF8.GetChars(readbuff)); if (test != testResult) { result = MFTestResults.Fail; Log.Comment("Exepected: " + test + ", but got: " + testResult); } } } catch (Exception ex) { Log.Exception("Unexpected exception: " + ex.Message); result = MFTestResults.Fail; } return(result); }
public MFTestResults VanillaWrite_Dynamic_Ctor() { MFTestResults result = MFTestResults.Pass; try { using (MemoryStream ms = new MemoryStream()) { Log.Comment("Write 256 bytes of data"); if (!TestWrite(ms, 256)) { result = MFTestResults.Fail; } Log.Comment("Write middle of buffer"); if (!TestWrite(ms, 256, 100, 100)) { result = MFTestResults.Fail; } // 1000 - 256 - 100 = 644 Log.Comment("Write start of buffer"); if (!TestWrite(ms, 1000, 644, 0)) { result = MFTestResults.Fail; } Log.Comment("Write end of buffer"); if (!TestWrite(ms, 1000, 900, 100)) { result = MFTestResults.Fail; } Log.Comment("Rewind and verify all bytes written"); ms.Seek(0, SeekOrigin.Begin); if (!MemoryStreamHelper.VerifyRead(ms)) { result = MFTestResults.Fail; } Log.Comment("Verify Read validation with UTF8 string"); ms.SetLength(0); string test = "MFFramework Test"; ms.Write(UTF8Encoding.UTF8.GetBytes(test), 0, test.Length); ms.Flush(); ms.Seek(0, SeekOrigin.Begin); byte[] readbuff = new byte[20]; ms.Read(readbuff, 0, readbuff.Length); string testResult = new string(Encoding.UTF8.GetChars(readbuff)); if (test != testResult) { result = MFTestResults.Fail; Log.Comment("Exepected: " + test + ", but got: " + testResult); } } } catch (Exception ex) { Log.Exception("Unexpected exception: " + ex.Message); result = MFTestResults.Fail; } return(result); }
public MFTestResults InvalidCases() { MemoryStream fs = new MemoryStream(); MemoryStreamHelper.Write(fs, 1000); long seek; MFTestResults result = MFTestResults.Pass; try { try { Log.Comment("Seek -1 from Begin"); seek = fs.Seek(-1, SeekOrigin.Begin); result = MFTestResults.Fail; Log.Exception("Expected IOException, but got position " + seek); } catch (IOException) { /* pass case */ } try { Log.Comment("Seek -1001 from Current - at end from write"); seek = fs.Seek(-1001, SeekOrigin.Current); result = MFTestResults.Fail; Log.Exception("Expected IOException, but got position " + seek); } catch (IOException) { /* pass case */ } try { Log.Comment("Seek -1001 from End"); seek = fs.Seek(-1001, SeekOrigin.End); result = MFTestResults.Fail; Log.Exception("Expected IOException, but got position " + seek); } catch (IOException) { /* pass case */ } try { Log.Comment("Seek invalid -1 origin"); seek = fs.Seek(1, (SeekOrigin)(-1)); result = MFTestResults.Fail; Log.Exception("Expected ArgumentException, but got position " + seek); } catch (ArgumentException) { /* pass case */ } try { Log.Comment("Seek invalid 10 origin"); seek = fs.Seek(1, (SeekOrigin)10); result = MFTestResults.Fail; Log.Exception("Expected ArgumentException, but got position " + seek); } catch (ArgumentException) { /* pass case */ } try { Log.Comment("Seek with closed stream"); fs.Close(); seek = fs.Seek(0, SeekOrigin.Begin); result = MFTestResults.Fail; Log.Exception("Expected ObjectDisposedException, but got position " + seek); } catch (ObjectDisposedException) { /* pass case */ } try { Log.Comment("Seek with disposed stream"); fs.Dispose(); seek = fs.Seek(0, SeekOrigin.End); result = MFTestResults.Fail; Log.Exception("Expected ObjectDisposedException, but got position " + seek); } catch (ObjectDisposedException) { /* pass case */ } } catch (Exception ex) { Log.Exception("Unexpected exception: " + ex.Message); result = MFTestResults.Fail; } finally { if (fs != null) { fs.Dispose(); } } return(result); }
public MFTestResults ValidCases() { MFTestResults result = MFTestResults.Pass; try { using (MemoryStream fs = new MemoryStream()) { MemoryStreamHelper.Write(fs, 1000); Log.Comment("Seek to beginning"); if (!TestSeek(fs, 0, SeekOrigin.Begin, 0)) { result = MFTestResults.Fail; } Log.Comment("Seek forward offset from begging"); if (!TestSeek(fs, 10, SeekOrigin.Begin, 0)) { result = MFTestResults.Fail; } Log.Comment("Seek backwards offset from current"); if (!TestSeek(fs, -5, SeekOrigin.Current, 5)) { result = MFTestResults.Fail; } Log.Comment("Seek forwards offset from current"); if (!TestSeek(fs, 20, SeekOrigin.Current, 25)) { result = MFTestResults.Fail; } Log.Comment("Seek to end"); if (!TestSeek(fs, 0, SeekOrigin.End, 1000)) { result = MFTestResults.Fail; } Log.Comment("Seek backwards offset from end"); if (!TestSeek(fs, -35, SeekOrigin.End, 965)) { result = MFTestResults.Fail; } Log.Comment("Seek past end relative to End"); if (!TestExtend(fs, 1, SeekOrigin.End, 1001, 1002)) { result = MFTestResults.Fail; } Log.Comment("Seek past end relative to Begin"); if (!TestExtend(fs, 1002, SeekOrigin.Begin, 1002, 1003)) { result = MFTestResults.Fail; } Log.Comment("Seek past end relative to Current"); if (!TestSeek(fs, 995, SeekOrigin.Begin, 995)) { result = MFTestResults.Fail; } if (!TestExtend(fs, 10, SeekOrigin.Current, 1005, 1006)) { result = MFTestResults.Fail; } // 1000 --123456 // verify 011001 Log.Comment("Verify proper bytes written at end (zero'd bytes from seek beyond end)"); byte[] buff = new byte[6]; byte[] verify = new byte[] { 0, 1, 1, 0, 0, 1 }; fs.Seek(-6, SeekOrigin.End); fs.Read(buff, 0, buff.Length); for (int i = 0; i < buff.Length; i++) { if (buff[i] != verify[i]) { result = MFTestResults.Fail; Log.Comment("Position " + i + ":" + buff[i] + " != " + verify[i]); } } } } catch (Exception ex) { Log.Exception("Unexpected exception: " + ex.Message); result = MFTestResults.Fail; } return(result); }
public MFTestResults VanillaWrite() { MFTestResults result = MFTestResults.Pass; try { Log.Comment("Static Buffer"); byte[] buffer = new byte[100]; using (MemoryStream ms = new MemoryStream(buffer)) { Log.Comment("Write 50 bytes of data"); if (!TestWrite(ms, 50, 100)) { result = MFTestResults.Fail; } Log.Comment("Write final 50 bytes of data"); if (!TestWrite(ms, 50, 100)) { result = MFTestResults.Fail; } Log.Comment("Any more bytes written should throw"); try { ms.WriteByte(50); result = MFTestResults.Fail; Log.Exception("Expected NotSupportedException"); } catch (NotSupportedException) { /* pass case */ } Log.Comment("Rewind and verify all bytes written"); ms.Seek(0, SeekOrigin.Begin); if (!MemoryStreamHelper.VerifyRead(ms)) { result = MFTestResults.Fail; } } Log.Comment("Dynamic Buffer"); using (MemoryStream ms = new MemoryStream()) { Log.Comment("Write 100 bytes of data"); if (!TestWrite(ms, 100)) { result = MFTestResults.Fail; } Log.Comment("Extend internal buffer, write 160"); if (!TestWrite(ms, 160)) { result = MFTestResults.Fail; } Log.Comment("Double extend internal buffer, write 644"); if (!TestWrite(ms, 644)) { result = MFTestResults.Fail; } Log.Comment("write another 1100"); if (!TestWrite(ms, 1100)) { result = MFTestResults.Fail; } Log.Comment("Rewind and verify all bytes written"); ms.Seek(0, SeekOrigin.Begin); if (!MemoryStreamHelper.VerifyRead(ms)) { result = MFTestResults.Fail; } } } catch (Exception ex) { Log.Exception("Unexpected exception", ex); result = MFTestResults.Fail; } return(result); }