/// <summary> /// Composes validator ensuring that buffer has valid hash /// </summary> /// <param name="hash">The hash.</param> /// <returns>new instance of the rule validato</returns> /// <seealso cref="BufferUtil.CalculateSimpleHashCode"/> public static Rule <byte[]> WithValidHash(int hash) { return((bytes, scope) => { var result = BufferUtil.CalculateSimpleHashCode(bytes); if (result != hash) { scope.Error("Byte array should have valid hash."); } }); }
public void Check_hash_roundtrips() { var buffer = new byte[Rand.Next(2, 64)]; var prov = new RNGCryptoServiceProvider(); prov.GetBytes(buffer); var hash = BufferUtil.CalculateSimpleHashCode(buffer); Enforce.That(() => buffer, BufferIs.WithValidHash(hash)); unchecked { buffer[Rand.Next(buffer.Length)] += 1; } Assert.IsFalse(Scope.IsValid(buffer, BufferIs.WithValidHash(hash))); }