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