blob: efad26d48512851ff7905102b05276ac947c4eb2 [file] [log] [blame]
bungeman@google.comcfcb1be2013-01-31 19:47:48 +00001/*
2 * Copyright 2013 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +00007
bungeman@google.comcfcb1be2013-01-31 19:47:48 +00008#include "SkMD5.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +00009#include "Test.h"
bungeman@google.comcfcb1be2013-01-31 19:47:48 +000010
11static bool digests_equal(const SkMD5::Digest& expectedDigest, const SkMD5::Digest& computedDigest) {
12 for (size_t i = 0; i < SK_ARRAY_COUNT(expectedDigest.data); ++i) {
13 if (expectedDigest.data[i] != computedDigest.data[i]) {
14 return false;
15 }
16 }
17 return true;
18}
19
20static void md5_test(const char* string, const SkMD5::Digest& expectedDigest, skiatest::Reporter* reporter) {
21 size_t len = strlen(string);
22
23 // All at once
24 {
25 SkMD5 context;
26 context.update(reinterpret_cast<const uint8_t*>(string), len);
27 SkMD5::Digest digest;
28 context.finish(digest);
29
30 REPORTER_ASSERT(reporter, digests_equal(expectedDigest, digest));
31 }
32
33 // One byte at a time.
34 {
35 SkMD5 context;
36 const uint8_t* data = reinterpret_cast<const uint8_t*>(string);
37 const uint8_t* end = reinterpret_cast<const uint8_t*>(string + len);
38 for (; data < end; ++data) {
39 context.update(data, 1);
40 }
41 SkMD5::Digest digest;
42 context.finish(digest);
43
44 REPORTER_ASSERT(reporter, digests_equal(expectedDigest, digest));
45 }
46}
47
48static struct MD5Test {
49 const char* message;
50 SkMD5::Digest digest;
51} md5_tests[] = {
52 // Reference tests from RFC1321 Section A.5 ( http://www.ietf.org/rfc/rfc1321.txt )
53 { "", {{ 0xd4, 0x1d, 0x8c, 0xd9, 0x8f, 0x00, 0xb2, 0x04, 0xe9, 0x80, 0x09, 0x98, 0xec, 0xf8, 0x42, 0x7e }} },
54 { "a", {{ 0x0c, 0xc1, 0x75, 0xb9, 0xc0, 0xf1, 0xb6, 0xa8, 0x31, 0xc3, 0x99, 0xe2, 0x69, 0x77, 0x26, 0x61 }} },
55 { "abc", {{ 0x90, 0x01, 0x50, 0x98, 0x3c, 0xd2, 0x4f, 0xb0, 0xd6, 0x96, 0x3f, 0x7d, 0x28, 0xe1, 0x7f, 0x72 }} },
56 { "message digest", {{ 0xf9, 0x6b, 0x69, 0x7d, 0x7c, 0xb7, 0x93, 0x8d, 0x52, 0x5a, 0x2f, 0x31, 0xaa, 0xf1, 0x61, 0xd0 }} },
57 { "abcdefghijklmnopqrstuvwxyz", {{ 0xc3, 0xfc, 0xd3, 0xd7, 0x61, 0x92, 0xe4, 0x00, 0x7d, 0xfb, 0x49, 0x6c, 0xca, 0x67, 0xe1, 0x3b }} },
58 { "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", {{ 0xd1, 0x74, 0xab, 0x98, 0xd2, 0x77, 0xd9, 0xf5, 0xa5, 0x61, 0x1c, 0x2c, 0x9f, 0x41, 0x9d, 0x9f }} },
59 { "12345678901234567890123456789012345678901234567890123456789012345678901234567890", {{ 0x57, 0xed, 0xf4, 0xa2, 0x2b, 0xe3, 0xc9, 0x55, 0xac, 0x49, 0xda, 0x2e, 0x21, 0x07, 0xb6, 0x7a }} },
60};
61
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +000062DEF_TEST(MD5, reporter) {
bungeman@google.comcfcb1be2013-01-31 19:47:48 +000063 for (size_t i = 0; i < SK_ARRAY_COUNT(md5_tests); ++i) {
64 md5_test(md5_tests[i].message, md5_tests[i].digest, reporter);
65 }
66}