예제 #1
0
        public static void TryGetCurrentHash_TooSmall()
        {
            Span <byte>       buf  = stackalloc byte[7];
            FlexibleAlgorithm hash = new FlexibleAlgorithm(buf.Length + 1);

            Assert.True(hash.IsReset);
            hash.Append(buf);
            Assert.False(hash.IsReset);

            while (true)
            {
                Assert.False(hash.TryGetCurrentHash(buf, out int written));
                Assert.Equal(0, written);

                if (buf.IsEmpty)
                {
                    break;
                }

                buf = buf.Slice(1);
            }

            Assert.Equal(0, hash.GetCurrentHashCoreCallCount);
            Assert.False(hash.IsReset);
        }
예제 #2
0
        public static void TryGetHashAndReset_TooBig_Succeeds()
        {
            Span <byte>       buf  = stackalloc byte[16];
            FlexibleAlgorithm hash = new FlexibleAlgorithm(buf.Length / 2);
            int i = 0;

            while (buf.Length > hash.HashLengthInBytes)
            {
                Assert.Equal(i, hash.GetCurrentHashCoreCallCount);

                hash.Append(buf);
                Assert.False(hash.IsReset);

                // TryGetCurrentHash in turn asserts that buf got Sliced down to HashLengthInBytes.
                Assert.True(hash.TryGetHashAndReset(buf, out int written));
                Assert.Equal(hash.HashLengthInBytes, written);
                Assert.True(hash.IsReset);

                buf = buf.Slice(1);
                i++;
            }

            Assert.Equal(i, hash.GetCurrentHashCoreCallCount);
            Assert.True(hash.IsReset);
        }
예제 #3
0
        public static void DefaultGetHashAndResetDoesWhatItSays()
        {
            Span <byte>       buf  = stackalloc byte[16];
            FlexibleAlgorithm hash = new FlexibleAlgorithm(buf.Length);

            Assert.True(hash.IsReset);
            hash.Append(buf);
            Assert.False(hash.IsReset);
            Assert.Equal(0, hash.GetCurrentHashCoreCallCount);

            byte[] ret = hash.GetHashAndReset();
            Assert.True(hash.IsReset);
            Assert.Equal(1, hash.GetCurrentHashCoreCallCount);
            Assert.Equal(hash.HashLengthInBytes, ret.Length);

            hash.Append(ret);
            Assert.False(hash.IsReset);

            Assert.True(hash.TryGetHashAndReset(buf, out int written));
            Assert.True(hash.IsReset);
            Assert.Equal(2, hash.GetCurrentHashCoreCallCount);
            Assert.Equal(hash.HashLengthInBytes, written);

            hash.Append(ret);
            Assert.False(hash.IsReset);

            written = hash.GetHashAndReset(buf);
            Assert.True(hash.IsReset);
            Assert.Equal(3, hash.GetCurrentHashCoreCallCount);
            Assert.Equal(hash.HashLengthInBytes, written);
        }
예제 #4
0
        public static void AllocatingGetHashAndReset_CorrectSize()
        {
            FlexibleAlgorithm hash = new FlexibleAlgorithm(12);

            byte[] ret = hash.GetHashAndReset();
            Assert.Equal(hash.HashLengthInBytes, ret.Length);
            Assert.Equal(1, hash.GetCurrentHashCoreCallCount);
        }
예제 #5
0
        public static void GetCurrentHash_TooSmall()
        {
            byte[]            buf  = new byte[7];
            FlexibleAlgorithm hash = new FlexibleAlgorithm(buf.Length + 1);

            Assert.True(hash.IsReset);
            hash.Append(buf);
            Assert.False(hash.IsReset);

            for (int i = 0; i <= buf.Length; i++)
            {
                AssertExtensions.Throws <ArgumentException>(
                    "destination",
                    () => hash.GetCurrentHash(buf.AsSpan(i)));
            }

            Assert.False(hash.IsReset);
        }
예제 #6
0
        public static void AppendAsyncNullStreamThrows_OutsideTask()
        {
            NonCryptographicHashAlgorithm hash = new FlexibleAlgorithm(5);

            AssertExtensions.Throws <ArgumentNullException>("stream", () => hash.AppendAsync(null));
        }
예제 #7
0
        public static void AppendNullArrayThrows()
        {
            NonCryptographicHashAlgorithm hash = new FlexibleAlgorithm(5);

            AssertExtensions.Throws <ArgumentNullException>("source", () => hash.Append((byte[])null));
        }