diff --git a/X10D.Tests/src/IO/StreamTests.cs b/X10D.Tests/src/IO/StreamTests.cs index 402a854..c8c9430 100644 --- a/X10D.Tests/src/IO/StreamTests.cs +++ b/X10D.Tests/src/IO/StreamTests.cs @@ -56,4 +56,136 @@ public class StreamTests Assert.AreEqual(expectedHash.Length, bytesWritten); CollectionAssert.AreEqual(expectedHash, hash.ToArray()); } + + [TestMethod] + public void NonReadableStreamShouldThrow() + { + Assert.ThrowsException(() => + { + using var stream = new DummyStream(); + stream.GetHash(); + }); + + Assert.ThrowsException(() => + { + using var stream = new DummyStream(); + stream.TryWriteHash(Span.Empty, out _); + }); + } + + [TestMethod] + public void LargeStreamShouldThrow() + { + Assert.ThrowsException(() => + { + using var stream = new DummyStream(true); + stream.TryWriteHash(Span.Empty, out _); + }); + } + + [TestMethod] + public void NullCreateMethodShouldThrow() + { + Assert.ThrowsException(() => Stream.Null.GetHash()); + Assert.ThrowsException(() => + Stream.Null.TryWriteHash(Span.Empty, out _)); + } + + [TestMethod] + public void NoCreateMethodShouldThrow() + { + Assert.ThrowsException(() => Stream.Null.GetHash()); + Assert.ThrowsException(() => + Stream.Null.TryWriteHash(Span.Empty, out _)); + } + + private class DummyStream : Stream + { + public DummyStream(bool readable = false) + { + CanRead = readable; + CanSeek = readable; + CanWrite = readable; + } + + public override void Flush() + { + } + + public override int Read(byte[] buffer, int offset, int count) + { + return 0; + } + + public override long Seek(long offset, SeekOrigin origin) + { + return 0; + } + + public override void SetLength(long value) + { + } + + public override void Write(byte[] buffer, int offset, int count) + { + } + + public override bool CanRead { get; } + + public override bool CanSeek { get; } + + public override bool CanWrite { get; } + + public override long Length + { + get => long.MaxValue; + } + + public override long Position + { + get => 0; + set { } + } + } + + private class HashAlgorithmTestClass : HashAlgorithm + { + public static new HashAlgorithmTestClass? Create() + { + return null; + } + + protected override void HashCore(byte[] array, int ibStart, int cbSize) + { + throw new NotImplementedException(); + } + + protected override byte[] HashFinal() + { + throw new NotImplementedException(); + } + + public override void Initialize() + { + throw new NotImplementedException(); + } + } + + private class HashAlgorithmTestClassNoCreateMethod : HashAlgorithm + { + protected override void HashCore(byte[] array, int ibStart, int cbSize) + { + throw new NotImplementedException(); + } + + protected override byte[] HashFinal() + { + throw new NotImplementedException(); + } + + public override void Initialize() + { + throw new NotImplementedException(); + } + } }