Exemplo n.º 1
0
        public void TestContentMd5()
        {
            var part = new MimePart();

            Assert.IsNull(part.ContentMd5, "Initial ContentMd5 value should be null");

            part.ContentMd5 = "XYZ";
            Assert.AreEqual("XYZ", part.ContentMd5, "Expected ContentMd5 to be updated");
            Assert.IsTrue(part.Headers.Contains(HeaderId.ContentMd5), "Expected header to exist");

            part.ContentMd5 = null;
            Assert.IsNull(part.ContentMd5, "Expected ContentMd5 to be null again");
            Assert.IsFalse(part.Headers.Contains(HeaderId.ContentMd5), "Expected header to be removed");

            part.Headers.Add(HeaderId.ContentMd5, "XYZ");
            Assert.AreEqual("XYZ", part.ContentMd5, "Expected ContentMd5 to be set again");

            part.Headers.Remove(HeaderId.ContentMd5);
            Assert.IsNull(part.ContentMd5, "Expected ContentMd5 to be null again");

            part.ContentMd5 = "XYZ";
            part.Headers.Clear();
            Assert.IsNull(part.ContentMd5, "Expected ContentMd5 to be null again");

            Assert.Throws <InvalidOperationException> (() => part.ComputeContentMd5());
            Assert.IsFalse(part.VerifyContentMd5());
        }
Exemplo n.º 2
0
        public void TestContentMd5()
        {
            var part = new MimePart();

            Assert.IsNull(part.ContentMd5, "Initial ContentMd5 value should be null");

            part.ContentMd5 = "XYZ";
            Assert.AreEqual("XYZ", part.ContentMd5, "Expected ContentMd5 to be updated");
            Assert.IsTrue(part.Headers.Contains(HeaderId.ContentMd5), "Expected header to exist");

            part.ContentMd5 = null;
            Assert.IsNull(part.ContentMd5, "Expected ContentMd5 to be null again");
            Assert.IsFalse(part.Headers.Contains(HeaderId.ContentMd5), "Expected header to be removed");

            part.Headers.Add(HeaderId.ContentMd5, "XYZ");
            Assert.AreEqual("XYZ", part.ContentMd5, "Expected ContentMd5 to be set again");

            part.Headers.Remove(HeaderId.ContentMd5);
            Assert.IsNull(part.ContentMd5, "Expected ContentMd5 to be null again");

            part.ContentMd5 = "XYZ";
            part.Headers.Clear();
            Assert.IsNull(part.ContentMd5, "Expected ContentMd5 to be null again");

            Assert.Throws <InvalidOperationException> (() => part.ComputeContentMd5());
            Assert.IsFalse(part.VerifyContentMd5());

            part = new TextPart("plain")
            {
                Text = "Hello, World.\n\nLet's check the MD5 sum of this text!\n"
            };

            var md5sum = part.ComputeContentMd5();

            Assert.AreEqual("8criUiOQmpfifOuOmYFtEQ==", md5sum, "ComputeContentMd5 text/*");

            // re-encode the base64'd md5sum using a hex encoding so we can easily compare to the output of `md5sum` command-line tools
            var decoded = Convert.FromBase64String(md5sum);
            var encoded = new StringBuilder();

            for (int i = 0; i < decoded.Length; i++)
            {
                encoded.Append(decoded[i].ToString("x2"));
            }

            Assert.AreEqual("f1cae25223909a97e27ceb8e99816d11", encoded.ToString(), "md5sum text/*");

            part.ContentMd5 = md5sum;

            Assert.IsTrue(part.VerifyContentMd5(), "VerifyContentMd5");
        }
Exemplo n.º 3
0
        public void TestMimePartContentObject()
        {
            byte[] data = Encoding.ASCII.GetBytes("abcd");

            // Checksum will be wrong if content is encoded in any way.
            string checksum;

            using (var md5 = MD5.Create())
                checksum = Convert.ToBase64String(md5.ComputeHash(data));

            var msg = new MimePart("application", "octet-stream",
                                   new ContentObject(new MemoryStream(data), ContentEncoding.Binary)
                                   );

            Assert.AreEqual(checksum, msg.ComputeContentMd5(), "Content MD5 is wrong");
            Assert.AreEqual(ContentEncoding.Binary, msg.ContentObject.Encoding, "ContentEncoding is wrong");
        }