public void Hashers_MD5() { EnhancedMemoryStream ms; byte[] data; byte[] digest1, digest2; digest1 = MD5Hasher.Compute(new byte[] { 0, 1, 2, 3 }, 0, 4); Assert.AreEqual(16, digest1.Length); Assert.AreEqual(16, MD5Hasher.DigestSize); digest2 = MD5Hasher.Compute(new byte[] { 1, 1, 2, 3 }, 0, 4); Assert.AreNotEqual(digest1, digest2); digest1 = MD5Hasher.Compute(new byte[0]); Assert.AreEqual(16, digest1.Length); Assert.AreEqual(16, MD5Hasher.DigestSize); digest1 = MD5Hasher.Compute(new byte[] { 0, 1, 2, 3 }); ms = new EnhancedMemoryStream(); ms.Seek(0, SeekOrigin.Begin); ms.Write(new byte[] { 0, 1, 2, 3 }, 0, 4); ms.Seek(0, SeekOrigin.Begin); digest2 = MD5Hasher.Compute(ms, 4); CollectionAssert.AreEqual(digest1, digest2); Assert.AreEqual(16, digest2.Length); Assert.AreEqual(0, ms.Position); data = new byte[2048]; for (int i = 0; i < data.Length; i++) { data[i] = (byte)i; } digest1 = MD5Hasher.Compute(data); ms.Seek(0, SeekOrigin.Begin); ms.Write(data, 0, data.Length); ms.Seek(0, SeekOrigin.Begin); digest2 = MD5Hasher.Compute(ms, data.Length); CollectionAssert.AreEqual(digest1, digest2); Assert.AreEqual(16, digest2.Length); Assert.AreEqual(0, ms.Position); digest1 = MD5Hasher.Compute("hello"); digest2 = MD5Hasher.Compute("world"); CollectionAssert.AreNotEqual(digest1, digest2); CollectionAssert.AreEqual(digest1, MD5Hasher.Compute("hello")); // These really aren't very good tests for folding but // at least they'll verify that it doesn't crash Assert.AreEqual(MD5Hasher.FoldOnce(new byte[] { 0, 1, 2, 3 }), MD5Hasher.FoldOnce(new byte[] { 0, 1, 2, 3 })); Assert.AreNotEqual((object)MD5Hasher.FoldOnce(new byte[] { 1, 1, 2, 3 }), (object)MD5Hasher.FoldOnce(new byte[] { 0, 1, 2, 3 })); Assert.AreEqual(MD5Hasher.FoldTwice(new byte[] { 0, 1, 2, 3 }), MD5Hasher.FoldTwice(new byte[] { 0, 1, 2, 3 })); Assert.AreNotEqual(MD5Hasher.FoldTwice(new byte[] { 1, 1, 2, 3 }), MD5Hasher.FoldTwice(new byte[] { 0, 1, 2, 3 })); }
public void Hashers_SHA512() { EnhancedMemoryStream ms; byte[] data; byte[] digest1, digest2; digest1 = SHA512Hasher.Compute(new byte[] { 0, 1, 2, 3 }, 0, 4); Assert.AreEqual(64, digest1.Length); Assert.AreEqual(64, SHA512Hasher.DigestSize); digest2 = SHA512Hasher.Compute(new byte[] { 1, 1, 2, 3 }, 0, 4); CollectionAssert.AreNotEqual(digest1, digest2); digest1 = SHA512Hasher.Compute(new byte[0]); Assert.AreEqual(64, digest1.Length); Assert.AreEqual(64, SHA512Hasher.DigestSize); digest1 = SHA512Hasher.Compute(new byte[] { 0, 1, 2, 3 }); ms = new EnhancedMemoryStream(); ms.Seek(0, SeekOrigin.Begin); ms.Write(new byte[] { 0, 1, 2, 3 }, 0, 4); ms.Seek(0, SeekOrigin.Begin); digest2 = SHA512Hasher.Compute(ms, 4); CollectionAssert.AreEqual(digest1, digest2); Assert.AreEqual(64, digest2.Length); Assert.AreEqual(0, ms.Position); data = new byte[2048]; for (int i = 0; i < data.Length; i++) { data[i] = (byte)i; } digest1 = SHA512Hasher.Compute(data); ms.Seek(0, SeekOrigin.Begin); ms.Write(data, 0, data.Length); ms.Seek(0, SeekOrigin.Begin); digest2 = SHA512Hasher.Compute(ms, data.Length); CollectionAssert.AreEqual(digest1, digest2); Assert.AreEqual(64, digest2.Length); Assert.AreEqual(0, ms.Position); digest1 = SHA512Hasher.Compute("hello"); digest2 = SHA512Hasher.Compute("world"); CollectionAssert.AreNotEqual(digest1, digest2); CollectionAssert.AreEqual(digest1, SHA512Hasher.Compute("hello")); }
public void EnhancedMemoryStream_ToByteArray() { var es = new EnhancedMemoryStream(); byte[] buf; es.Write(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, 0, 10); buf = es.ToArray(); CollectionAssert.AreEqual(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, buf); buf[0] = 255; es.Position = 0; Assert.AreEqual(0, es.ReadByte()); }
public void HttpRequest_Content_ChunkedTransfer() { HttpRequest r; EnhancedMemoryStream ms; byte[] bufOut; byte[] buf; bufOut = new byte[300]; for (int i = 0; i < bufOut.Length; i++) { bufOut[i] = (byte)i; } // Test parsing where the entire request is in a single input block. ms = new EnhancedMemoryStream(); ms.WriteBytesNoLen(Encoding.ASCII.GetBytes("GET /foo.htm HTTP/1.1\r\nTransfer-Encoding: chunked\r\n\r\n")); ms.WriteBytesNoLen(Encoding.ASCII.GetBytes("12c; extra crap\r\n")); // 12c = 300 as hex ms.WriteBytesNoLen(bufOut); ms.WriteBytesNoLen(Encoding.ASCII.GetBytes("\r\n")); ms.WriteBytesNoLen(Encoding.ASCII.GetBytes("0; extra crap\r\n\r\n")); buf = ms.ToArray(); r = new HttpRequest(); r.BeginParse(80); Assert.IsTrue(r.Parse(buf, buf.Length)); r.EndParse(); Assert.AreEqual(bufOut.Length, r.Content.Size); CollectionAssert.AreEqual(bufOut, r.Content.ToByteArray()); // Test parsing of two chunks in a single input block. ms = new EnhancedMemoryStream(); ms.WriteBytesNoLen(Encoding.ASCII.GetBytes("GET /foo.htm HTTP/1.1\r\nTransfer-Encoding: chunked\r\n\r\n")); ms.WriteBytesNoLen(Encoding.ASCII.GetBytes("FF\r\n")); // FF = 255 as hex ms.Write(bufOut, 0, 255); ms.WriteBytesNoLen(Encoding.ASCII.GetBytes("\r\n")); ms.WriteBytesNoLen(Encoding.ASCII.GetBytes("2D\r\n")); // 2D = 45 as hex (300 - 255) ms.Write(bufOut, 255, 45); ms.WriteBytesNoLen(Encoding.ASCII.GetBytes("\r\n")); ms.WriteBytesNoLen(Encoding.ASCII.GetBytes("0\r\n\r\n")); buf = ms.ToArray(); r = new HttpRequest(); r.BeginParse(80); Assert.IsTrue(r.Parse(buf, buf.Length)); r.EndParse(); Assert.AreEqual(bufOut.Length, r.Content.Size); CollectionAssert.AreEqual(bufOut, r.Content.ToByteArray()); // Repeat the 2 chunk test but this time break the input into blocks of // only a single byte each. This will torture test the parsing state machines. buf = ms.ToArray(); r = new HttpRequest(); r.BeginParse(80); for (int i = 0; i < buf.Length - 1; i++) { Assert.IsFalse(r.Parse(new byte[] { buf[i] }, 1)); } Assert.IsTrue(r.Parse(new byte[] { buf[buf.Length - 1] }, 1)); r.EndParse(); Assert.AreEqual(bufOut.Length, r.Content.Size); CollectionAssert.AreEqual(bufOut, r.Content.ToByteArray()); }