public void ClearIsDispose() { using (var stream = new PositionValueStream(0)) using (HashAlgorithm hash = new Length32Hash()) { Assert.Throws <NullReferenceException>(() => hash.ComputeHash((Stream)null)); hash.Clear(); Assert.Throws <ObjectDisposedException>(() => hash.ComputeHash(stream)); } }
private void StreamHash(int byteCount) { byte[] result; using (var stream = new PositionValueStream(byteCount)) using (HashAlgorithm hash = new Length32Hash()) { result = hash.ComputeHash(stream); } AssertCorrectAnswer((uint)byteCount, result); }
public void ValidateStreamContents() { byte[] result; const int ByteCount = 1026; using (var stream = new PositionValueStream(ByteCount)) using (HashAlgorithm hash = new Sum32Hash()) { result = hash.ComputeHash(stream); } AssertCorrectAnswer(ExpectedSum(0, ByteCount), result); }
private void ValidateOffset(int arraySize, int hashOffset, int hashCount) { byte[] input; using (var stream = new PositionValueStream(arraySize)) using (var reader = new BinaryReader(stream)) { input = reader.ReadBytes(arraySize); } byte[] result; using (HashAlgorithm hash = new Sum32Hash()) { result = hash.ComputeHash(input, hashOffset, hashCount); } uint expectedSum = ExpectedSum(hashOffset, hashCount); AssertCorrectAnswer(expectedSum, result); }
public async Task VerifyComputeHashAsync(int size) { int fullCycles = size / 256; int partial = size % 256; // SUM(0..255) is 32640 const long CycleSum = 32640L; // The formula for additive sum IS n*(n+1)/2, but size is a count and the first n is 0, // which happens to turn it into (n-1) * n / 2, aka n * (n - 1) / 2. long expectedSum = CycleSum * fullCycles + (partial * (partial - 1) / 2); using (PositionValueStream stream = new PositionValueStream(size)) using (HashAlgorithm hash = new SummingTestHashAlgorithm()) { byte[] result = await hash.ComputeHashAsync(stream); byte[] expected = BitConverter.GetBytes(expectedSum); Assert.Equal(expected, result); } }
public void StreamHashInvalidArguments() { using (var stream = new PositionValueStream(0)) using (HashAlgorithm hash = new Length32Hash()) { Assert.Throws<NullReferenceException>(() => hash.ComputeHash((Stream)null)); hash.Dispose(); Assert.Throws<ObjectDisposedException>(() => hash.ComputeHash(stream)); } }