public void PokeTest() { using SafeHGlobalHandle m = new SafeHGlobalHandle(10); using MarshalingStream ms = new MarshalingStream(m, m.Size); Assert.That(() => ms.Write(0x000001FF), Throws.Nothing); Assert.That(ms.Position, Is.EqualTo(sizeof(int))); ms.Seek(0, SeekOrigin.Begin); byte[] ba = new byte[] { 0x2 }; Assert.That(() => ms.Poke(null, 0), Throws.ArgumentNullException); Assert.That(() => ms.Poke(ba, 1000), Throws.ArgumentException); Assert.That(() => ms.Poke(ba, -1), Throws.TypeOf <ArgumentOutOfRangeException>()); ms.Poke(ba, 1); Assert.That(ms.Read <int>(), Is.EqualTo(0x00000102)); Assert.That(() => ms.Read <ulong>(), Throws.TypeOf <ArgumentOutOfRangeException>()); }
public void PokeTest() { using (var m = new SafeHGlobalHandle(10)) using (var ms = new MarshalingStream((IntPtr)m, m.Size)) { Assert.That(() => ms.Write(0x000001FF), Throws.Nothing); Assert.That(ms.Position, Is.EqualTo(sizeof(int))); ms.Seek(0, SeekOrigin.Begin); var ba = new byte[] { 0x2 }; Assert.That(() => ms.Poke(null, 0), Throws.ArgumentNullException); Assert.That(() => ms.Poke(ba, 1000), Throws.ArgumentException); Assert.That(() => ms.Poke(ba, -1), Throws.TypeOf <ArgumentOutOfRangeException>()); ms.Poke(ba, 1); Assert.That(ms.Read <int>(), Is.EqualTo(0x00000102)); Assert.That(() => ms.Read <Vanara.PInvoke.User32_Gdi.ICONINFO>(), Throws.TypeOf <ArgumentOutOfRangeException>()); } }
public void PokeTest1() { using SafeHGlobalHandle m = new SafeHGlobalHandle(100); using MarshalingStream ms = new MarshalingStream(m, m.Size); Assert.That(ms.Position, Is.Zero); Assert.That(() => ms.Write(new[] { 1L, 2L }), Throws.Nothing); byte[] bytes = new byte[] { 0, 0, 0, 0, 0, 0, 0, 3 }; ms.Write(bytes, 0, bytes.Length); Assert.That(ms.Position, Is.EqualTo(sizeof(long) * 2 + 8)); ms.Seek(0, SeekOrigin.Begin); Assert.That(() => ms.Poke(IntPtr.Zero, 1002), Throws.ArgumentException); Assert.That(() => ms.Poke(IntPtr.Zero, -1), Throws.TypeOf <ArgumentOutOfRangeException>()); ms.Poke(IntPtr.Zero, sizeof(long)); byte[] buf = new byte[24]; ms.Read(buf, 0, buf.Length); Assert.That(buf, Is.EquivalentTo(new byte[] { 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3 })); Assert.That(() => ms.Read(null, 0, 0), Throws.ArgumentNullException); Assert.That(() => ms.Read(buf, 0, 30), Throws.ArgumentException); Assert.That(() => ms.Read(buf, -1, 0), Throws.TypeOf <ArgumentOutOfRangeException>()); ms.Position = m.Size - 10; Assert.That(() => ms.Read(buf, 0, buf.Length), Throws.Nothing); }