public void EmptyBufferCanRead() { using (StreamBuffer buffer = new StreamBuffer(0)) { buffer.CanRead.Should().BeTrue(); } }
public void EmptyBufferHasZeroLength() { using (StreamBuffer buffer = new StreamBuffer(0)) { buffer.Length.Should().Be(0); } }
public void EmptyBufferPositionIsZero() { using (StreamBuffer buffer = new StreamBuffer(0)) { buffer.Position.Should().Be(0); } }
public void EmptyBufferCanSetPositionToZero() { using (StreamBuffer buffer = new StreamBuffer(0)) { buffer.Position = 0; buffer.Position.Should().Be(0); } }
public void DisposedEmptyBufferCannotRead() { StreamBuffer buffer; using (buffer = new StreamBuffer(0)) { } buffer.CanRead.Should().BeFalse(); }
public void PositionCannotBeSetOutsideLength() { using (StreamBuffer buffer = new StreamBuffer(0)) { Action under = () => buffer.Position = -1; under.ShouldThrow<ArgumentOutOfRangeException>(); Action over = () => buffer.Position = 1; over.ShouldThrow<ArgumentOutOfRangeException>(); } }
public void EnsureLengthThrowsOnNegativeValue() { using (StreamBuffer buffer = new StreamBuffer()) { Action action = () => buffer.EnsureLength(-1); action.ShouldThrow<ArgumentOutOfRangeException>(); } }
public void SetLengthOnEmptyStream() { using (StreamBuffer buffer = new StreamBuffer()) { buffer.Length.Should().Be(0); buffer.SetLength(7); buffer.Length.Should().Be(7); } }
public void SeekSucceeds() { using (StreamBuffer buffer = new StreamBuffer(0)) { buffer.Length.Should().Be(0); buffer.Write(new byte[] { 0xA, 0xB, 0xC }, 0, 3); buffer.Length.Should().Be(3); buffer.Seek(0, SeekOrigin.Begin); buffer.ReadByte().Should().Be(0xA); buffer.Seek(-1, SeekOrigin.Current); buffer.ReadByte().Should().Be(0xA); buffer.Seek(1, SeekOrigin.Current); buffer.ReadByte().Should().Be(0xC); buffer.Seek(1, SeekOrigin.Begin); buffer.ReadByte().Should().Be(0xB); buffer.Seek(-2, SeekOrigin.End); buffer.ReadByte().Should().Be(0xB); } }
public void EmptyBufferThrowsOnNullBufferRead() { using (StreamBuffer buffer = new StreamBuffer(0)) { Action action = () => buffer.Read(null, 0, 0); action.ShouldThrow<ArgumentNullException>(); } }
public void PositionTest() { using (StreamBuffer buffer = new StreamBuffer(0)) { byte[] data = new byte[] { 0xA, 0xB, 0xC }; buffer.Write(data, 0, data.Length); buffer.Position.Should().Be(data.Length); for (int i = 0; i < data.Length; i++) { buffer.Position = i; buffer.ReadByte().Should().Be(data[i]); } } }
public void EmptyBufferThrowsOnPositiveOffsetRead() { using (StreamBuffer buffer = new StreamBuffer(0)) { Action action = () => buffer.Read(new byte[0], 1, 0); action.ShouldThrow<ArgumentException>(); } }
public void EmptyBufferThrowsOnNegativeOffsetWrite() { using (StreamBuffer buffer = new StreamBuffer(0)) { Action action = () => buffer.Write(new byte[0], -1, 0); action.ShouldThrow<ArgumentOutOfRangeException>(); } }
public void AppendTwice() { using (StreamBuffer buffer = new StreamBuffer(0)) { buffer.Length.Should().Be(0); buffer.Write(new byte[] { 0xA, 0xB, 0xC }, 0, 3); buffer.Position.Should().Be(3); buffer.Length.Should().Be(3); buffer.Position = 0; byte[] output = new byte[3]; buffer.Read(output, 0, 3); buffer.Position.Should().Be(3); output.ShouldAllBeEquivalentTo(new byte[] { 0xA, 0xB, 0xC }); buffer.Write(new byte[] { 0xD, 0xE, 0xF }, 0, 3); buffer.Position.Should().Be(6); buffer.Length.Should().Be(6); buffer.Position = 0; output = new byte[6]; buffer.Read(output, 0, 6); output.ShouldAllBeEquivalentTo(new byte[] { 0xA, 0xB, 0xC, 0xD, 0xE, 0xF }); } }
public void EmptyBufferThrowsOnNegativeCountRead() { using (StreamBuffer buffer = new StreamBuffer(0)) { Action action = () => buffer.Read(new byte[0], 0, -1); action.ShouldThrow<ArgumentOutOfRangeException>(); } }
public void StreamWriterOnEmptyBuffer() { using (StreamBuffer buffer = new StreamBuffer()) { using (StreamWriter writer = new StreamWriter(buffer)) using (StreamReader reader = new StreamReader(buffer)) { writer.AutoFlush = true; writer.WriteLine(testString); reader.BaseStream.Position = 0; reader.ReadLine().Should().Be(testString); } } }
public void WriteToEmptyBuffer() { using (StreamBuffer buffer = new StreamBuffer(0)) { buffer.Length.Should().Be(0); buffer.WriteByte(7); buffer.Length.Should().Be(1); buffer.Position = 0; buffer.ReadByte().Should().Be(7); } }
public void EmptyBufferCanReadNoBytes() { using (StreamBuffer buffer = new StreamBuffer(0)) { buffer.Read(new byte[0], 0, 1).Should().Be(0); } }
public void EmptyBufferThrowsOnPositiveCountWrite() { using (StreamBuffer buffer = new StreamBuffer(0)) { Action action = () => buffer.Write(new byte[0], 0, 1); action.ShouldThrow<ArgumentException>(); } }
public void SetLengthToZero() { using (StreamBuffer buffer = new StreamBuffer(7)) { buffer.Length.Should().Be(7); buffer.SetLength(0); buffer.Length.Should().Be(0); } }
public void NonEmptyBufferCanSeek() { using (StreamBuffer buffer = new StreamBuffer(1)) { buffer.CanSeek.Should().BeTrue(); } }
public void EmptyIntPtrIsNull() { using (StreamBuffer buffer = new StreamBuffer(0)) { ((SafeHandle)buffer).DangerousGetHandle().Should().Be(IntPtr.Zero); } }
public void EmptyBufferThrowsOnSeek() { using (StreamBuffer buffer = new StreamBuffer(0)) { Action action = () => buffer.Seek(1, SeekOrigin.Begin); action.ShouldThrow<IOException>(); } }
public void ShrinkPositionStaysAtEnd() { using (StreamBuffer buffer = new StreamBuffer(0)) { buffer.Write(new byte[] { 0xA, 0xB, 0xC }, 0, 3); buffer.Position.Should().Be(3); buffer.Length.Should().Be(3); buffer.SetLength(2); buffer.Position.Should().Be(2); buffer.Length.Should().Be(2); } }
public void NewBufferCapacityShouldNotImpactLength() { using (StreamBuffer buffer = new StreamBuffer(1, 10)) { buffer.Length.Should().Be(1); } }
public void StreamWriterSetLengthToZero() { using (StreamBuffer buffer = new StreamBuffer()) { using (StreamWriter writer = new StreamWriter(buffer)) using (StreamReader reader = new StreamReader(buffer)) { writer.AutoFlush = true; writer.WriteLine(testString); reader.BaseStream.Position = 0; reader.ReadLine().Should().Be(testString); writer.BaseStream.SetLength(0); reader.ReadLine().Should().BeNull(); } } }
public void EmptyBufferDoesNotThrowOnPositiveOffsetWriteOfNoCharacters() { using (StreamBuffer buffer = new StreamBuffer(0)) { buffer.Write(new byte[0], 1, 0); } }
public void SetNegativeLengthThrows() { using (StreamBuffer buffer = new StreamBuffer(0)) { Action action = () => buffer.SetLength(-1); action.ShouldThrow<ArgumentOutOfRangeException>(); } }
public void NewBufferLengthShouldBeSpecified() { using (StreamBuffer buffer = new StreamBuffer(1)) { buffer.Length.Should().Be(1); } }
internal static IEnumerable<PrivilegeSetting> GetTokenPrivileges(SafeCloseHandle token) { // Get the buffer size we need uint bytesNeeded; if (!Private.GetTokenInformation( token.DangerousGetHandle(), TOKEN_INFORMATION_CLASS.TokenPrivileges, EmptySafeHandle.Instance, 0, out bytesNeeded)) { int error = Marshal.GetLastWin32Error(); if (error != WinError.ERROR_INSUFFICIENT_BUFFER) throw GetIoExceptionForError(error); } else { // Didn't need any space for output, let's assume there are no privileges return Enumerable.Empty<PrivilegeSetting>(); } // Initialize the buffer and get the data var buffer = new StreamBuffer(bytesNeeded); if (!Private.GetTokenInformation( token.DangerousGetHandle(), TOKEN_INFORMATION_CLASS.TokenPrivileges, buffer, (uint)buffer.Length, out bytesNeeded)) { int error = Marshal.GetLastWin32Error(); throw GetIoExceptionForError(error); } // Loop through and get our privileges BinaryReader reader = new BinaryReader(buffer, Encoding.Unicode, leaveOpen: true); uint count = reader.ReadUInt32(); var privileges = new List<PrivilegeSetting>(); StringBuilder sb = new StringBuilder(256); for (int i = 0; i < count; i++) { LUID luid = new LUID { LowPart = reader.ReadUInt32(), HighPart = reader.ReadUInt32(), }; uint length = (uint)sb.Capacity; if (!Private.LookupPrivilegeNameW(IntPtr.Zero, ref luid, sb, ref length)) { int error = Marshal.GetLastWin32Error(); throw GetIoExceptionForError(error); } PrivilegeState state = (PrivilegeState)reader.ReadUInt32(); privileges.Add(new PrivilegeSetting(sb.ToString(), state)); sb.Clear(); } return privileges; }
internal static IEnumerable <PrivilegeSetting> GetTokenPrivileges(SafeCloseHandle token) { // Get the buffer size we need uint bytesNeeded; if (!Private.GetTokenInformation( token.DangerousGetHandle(), TOKEN_INFORMATION_CLASS.TokenPrivileges, EmptySafeHandle.Instance, 0, out bytesNeeded)) { int error = Marshal.GetLastWin32Error(); if (error != WinError.ERROR_INSUFFICIENT_BUFFER) { throw GetIoExceptionForError(error); } } else { // Didn't need any space for output, let's assume there are no privileges return(Enumerable.Empty <PrivilegeSetting>()); } // Initialize the buffer and get the data var buffer = new StreamBuffer(bytesNeeded); if (!Private.GetTokenInformation( token.DangerousGetHandle(), TOKEN_INFORMATION_CLASS.TokenPrivileges, buffer, (uint)buffer.Length, out bytesNeeded)) { int error = Marshal.GetLastWin32Error(); throw GetIoExceptionForError(error); } // Loop through and get our privileges BinaryReader reader = new BinaryReader(buffer, Encoding.Unicode, leaveOpen: true); uint count = reader.ReadUInt32(); var privileges = new List <PrivilegeSetting>(); StringBuilder sb = new StringBuilder(256); for (int i = 0; i < count; i++) { LUID luid = new LUID { LowPart = reader.ReadUInt32(), HighPart = reader.ReadUInt32(), }; uint length = (uint)sb.Capacity; if (!Private.LookupPrivilegeNameW(IntPtr.Zero, ref luid, sb, ref length)) { int error = Marshal.GetLastWin32Error(); throw GetIoExceptionForError(error); } PrivilegeState state = (PrivilegeState)reader.ReadUInt32(); privileges.Add(new PrivilegeSetting(sb.ToString(), state)); sb.Clear(); } return(privileges); }