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 "SkSHA1.h"
98f6884aab8aecd7657cf3f9cdbc682f0deca29c5tfarina@chromium.org#include "Test.h"
10cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com
11cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.comstatic bool digests_equal(const SkSHA1::Digest& expectedDigest, const SkSHA1::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 struct SHA1Test {
21cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com    const char* message;
22cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com    const unsigned long int repeatCount;
23cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com    const SkSHA1::Digest digest;
24cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com} sha1_tests[] = {
25cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com    // Reference tests from RFC3174 Section 7.3 ( http://www.ietf.org/rfc/rfc3174.txt )
26cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com    { "abc", 1, {{ 0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A, 0xBA, 0x3E, 0x25, 0x71, 0x78, 0x50, 0xC2, 0x6C, 0x9C, 0xD0, 0xD8, 0x9D }} },
27cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com    { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 1, {{ 0x84, 0x98, 0x3E, 0x44, 0x1C, 0x3B, 0xD2, 0x6E, 0xBA, 0xAE, 0x4A, 0xA1, 0xF9, 0x51, 0x29, 0xE5, 0xE5, 0x46, 0x70, 0xF1 }} },
28cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com    { "a", 1000000, {{ 0x34, 0xAA, 0x97, 0x3C, 0xD4, 0xC4, 0xDA, 0xA4, 0xF6, 0x1E, 0xEB, 0x2B, 0xDB, 0xAD, 0x27, 0x31, 0x65, 0x34, 0x01, 0x6F }} },
29cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com    { "0123456701234567012345670123456701234567012345670123456701234567", 10, {{ 0xDE, 0xA3, 0x56, 0xA2, 0xCD, 0xDD, 0x90, 0xC7, 0xA7, 0xEC, 0xED, 0xC5, 0xEB, 0xB5, 0x63, 0x93, 0x4F, 0x46, 0x04, 0x52 }} },
30cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com
31cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com    // Reference tests from running GNU sha1sum on test input
32cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com    { "The quick brown fox jumps over the lazy dog", 1, {{ 0x2f, 0xd4, 0xe1, 0xc6, 0x7a, 0x2d, 0x28, 0xfc, 0xed, 0x84, 0x9e, 0xe1, 0xbb, 0x76, 0xe7, 0x39, 0x1b, 0x93, 0xeb, 0x12 }} },
33cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com    { "", 1, {{ 0xda, 0x39, 0xa3, 0xee, 0x5e, 0x6b, 0x4b, 0x0d, 0x32, 0x55, 0xbf, 0xef, 0x95, 0x60, 0x18, 0x90, 0xaf, 0xd8, 0x07, 0x09 }} },
34cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com};
35cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com
36cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.comstatic void sha1_test(const SHA1Test& test, skiatest::Reporter* reporter) {
37cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com    size_t len = strlen(test.message);
38cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com
39cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com    SkSHA1 context;
40cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com    for (unsigned long int i = 0; i < test.repeatCount; ++i) {
41cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com        context.update(reinterpret_cast<const uint8_t*>(test.message), len);
42cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com    }
43cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com    SkSHA1::Digest digest;
44cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com    context.finish(digest);
45cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com
46cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com    REPORTER_ASSERT(reporter, digests_equal(test.digest, digest));
47cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com}
48cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com
49e4fafb146e85cdfcf9d5418597b6818aa0754adatfarina@chromium.orgDEF_TEST(SHA1, reporter) {
50cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com    for (size_t i = 0; i < SK_ARRAY_COUNT(sha1_tests); ++i) {
51cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com        sha1_test(sha1_tests[i], reporter);
52cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com    }
53cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com}
54