public static void TestInvalidArguments() { DataHmac hmac = null; Assert.Throws<ArgumentNullException>(() => { hmac = new DataHmac(null); }); Assert.Throws<InternalErrorException>(() => { hmac = new DataHmac(new byte[20]); }); // Use the instance to avoid FxCop errors. Object.Equals(hmac, null); }
public static void TestMethods() { DataHmac hmac = new DataHmac(new byte[] { 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 }); Assert.That(hmac.Length, Is.EqualTo(16), "The length should always be 16."); Assert.That(hmac.GetBytes(), Is.EquivalentTo(new byte[] { 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 }), "Check that GetBytes() returns the expected."); Assert.That(hmac, Is.EqualTo(new DataHmac(new byte[] { 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 })), "Check Equals() override."); Assert.That(hmac == new DataHmac(new byte[] { 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 }), Is.True, "Check operator== override."); Assert.That(hmac != new DataHmac(new byte[] { 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 }), Is.False, "Check operator!= override."); DataHmac hmac2 = new DataHmac(new byte[] { 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 }); Assert.That(hmac.GetHashCode(), Is.Not.EqualTo(0), "The hash code should not be zero (can be, but it's not in this case)."); Assert.That(hmac.GetHashCode(), Is.EqualTo(hmac2.GetHashCode()), "The hash code for two different instances with same value should be the same."); Assert.That(hmac.Equals(null), Is.False, "An instance is never equal to null."); Assert.That(hmac.Equals(new object()), Is.False, "An instance is never equal to another instance of a differing type."); DataHmac hmacSynonym = hmac; Assert.That(hmac == hmacSynonym, Is.True, "These should compare equal via reference equality."); }
public static void TestHmacFromSimpleFile() { DataHmac expectedHmac = new DataHmac(new byte[] { 0xF9, 0xAF, 0x2E, 0x67, 0x7D, 0xCF, 0xC9, 0xFE, 0x06, 0x4B, 0x39, 0x08, 0xE7, 0x5A, 0x87, 0x81 }); using (AxCryptDocument document = new AxCryptDocument()) { Passphrase passphrase = new Passphrase("a"); bool keyIsOk = document.Load(FakeRuntimeFileInfo.ExpandableMemoryStream(Resources.helloworld_key_a_txt), passphrase.DerivedPassphrase); Assert.That(keyIsOk, Is.True, "The passphrase provided is correct!"); DataHmac hmac = document.DocumentHeaders.Hmac; Assert.That(hmac.GetBytes(), Is.EqualTo(expectedHmac.GetBytes()), "Wrong HMAC"); } }