示例#1
0
 public void EmptyBufferCanRead()
 {
     using (StreamBuffer buffer = new StreamBuffer(0))
     {
         buffer.CanRead.Should().BeTrue();
     }
 }
示例#2
0
 public void EmptyBufferHasZeroLength()
 {
     using (StreamBuffer buffer = new StreamBuffer(0))
     {
         buffer.Length.Should().Be(0);
     }
 }
示例#3
0
 public void EmptyBufferPositionIsZero()
 {
     using (StreamBuffer buffer = new StreamBuffer(0))
     {
         buffer.Position.Should().Be(0);
     }
 }
示例#4
0
 public void EmptyBufferCanSetPositionToZero()
 {
     using (StreamBuffer buffer = new StreamBuffer(0))
     {
         buffer.Position = 0;
         buffer.Position.Should().Be(0);
     }
 }
示例#5
0
 public void DisposedEmptyBufferCannotRead()
 {
     StreamBuffer buffer;
     using (buffer = new StreamBuffer(0))
     {
     }
     buffer.CanRead.Should().BeFalse();
 }
示例#6
0
 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>();
     }
 }
示例#7
0
 public void EnsureLengthThrowsOnNegativeValue()
 {
     using (StreamBuffer buffer = new StreamBuffer())
     {
         Action action = () => buffer.EnsureLength(-1);
         action.ShouldThrow<ArgumentOutOfRangeException>();
     }
 }
示例#8
0
 public void SetLengthOnEmptyStream()
 {
     using (StreamBuffer buffer = new StreamBuffer())
     {
         buffer.Length.Should().Be(0);
         buffer.SetLength(7);
         buffer.Length.Should().Be(7);
     }
 }
示例#9
0
 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);
     }
 }
示例#10
0
 public void EmptyBufferThrowsOnNullBufferRead()
 {
     using (StreamBuffer buffer = new StreamBuffer(0))
     {
         Action action = () => buffer.Read(null, 0, 0);
         action.ShouldThrow<ArgumentNullException>();
     }
 }
示例#11
0
 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]);
         }
     }
 }
示例#12
0
 public void EmptyBufferThrowsOnPositiveOffsetRead()
 {
     using (StreamBuffer buffer = new StreamBuffer(0))
     {
         Action action = () => buffer.Read(new byte[0], 1, 0);
         action.ShouldThrow<ArgumentException>();
     }
 }
示例#13
0
 public void EmptyBufferThrowsOnNegativeOffsetWrite()
 {
     using (StreamBuffer buffer = new StreamBuffer(0))
     {
         Action action = () => buffer.Write(new byte[0], -1, 0);
         action.ShouldThrow<ArgumentOutOfRangeException>();
     }
 }
示例#14
0
 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 });
     }
 }
示例#15
0
 public void EmptyBufferThrowsOnNegativeCountRead()
 {
     using (StreamBuffer buffer = new StreamBuffer(0))
     {
         Action action = () => buffer.Read(new byte[0], 0, -1);
         action.ShouldThrow<ArgumentOutOfRangeException>();
     }
 }
示例#16
0
 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);
         }
     }
 }
示例#17
0
 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);
     }
 }
示例#18
0
 public void EmptyBufferCanReadNoBytes()
 {
     using (StreamBuffer buffer = new StreamBuffer(0))
     {
         buffer.Read(new byte[0], 0, 1).Should().Be(0);
     }
 }
示例#19
0
 public void EmptyBufferThrowsOnPositiveCountWrite()
 {
     using (StreamBuffer buffer = new StreamBuffer(0))
     {
         Action action = () => buffer.Write(new byte[0], 0, 1);
         action.ShouldThrow<ArgumentException>();
     }
 }
示例#20
0
 public void SetLengthToZero()
 {
     using (StreamBuffer buffer = new StreamBuffer(7))
     {
         buffer.Length.Should().Be(7);
         buffer.SetLength(0);
         buffer.Length.Should().Be(0);
     }
 }
示例#21
0
 public void NonEmptyBufferCanSeek()
 {
     using (StreamBuffer buffer = new StreamBuffer(1))
     {
         buffer.CanSeek.Should().BeTrue();
     }
 }
示例#22
0
 public void EmptyIntPtrIsNull()
 {
     using (StreamBuffer buffer = new StreamBuffer(0))
     {
         ((SafeHandle)buffer).DangerousGetHandle().Should().Be(IntPtr.Zero);
     }
 }
示例#23
0
 public void EmptyBufferThrowsOnSeek()
 {
     using (StreamBuffer buffer = new StreamBuffer(0))
     {
         Action action = () => buffer.Seek(1, SeekOrigin.Begin);
         action.ShouldThrow<IOException>();
     }
 }
示例#24
0
 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);
     }
 }
示例#25
0
 public void NewBufferCapacityShouldNotImpactLength()
 {
     using (StreamBuffer buffer = new StreamBuffer(1, 10))
     {
         buffer.Length.Should().Be(1);
     }
 }
示例#26
0
 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();
         }
     }
 }
示例#27
0
 public void EmptyBufferDoesNotThrowOnPositiveOffsetWriteOfNoCharacters()
 {
     using (StreamBuffer buffer = new StreamBuffer(0))
     {
         buffer.Write(new byte[0], 1, 0);
     }
 }
示例#28
0
 public void SetNegativeLengthThrows()
 {
     using (StreamBuffer buffer = new StreamBuffer(0))
     {
         Action action = () => buffer.SetLength(-1);
         action.ShouldThrow<ArgumentOutOfRangeException>();
     }
 }
示例#29
0
 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;
            }
示例#31
0
            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);
            }