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