• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //  libs/uuid/test/test_md5.cpp  --------------------------------//
2 
3 // (C) Copyright 2017 - 2019 James E. King III
4 
5 // Distributed under the Boost Software License, Version 1.0. (See
6 // accompanying file LICENSE_1_0.txt or copy at
7 // https://www.boost.org/LICENSE_1_0.txt)
8 
9 #include <boost/cstdint.hpp>
10 #include <boost/detail/lightweight_test.hpp>
11 #include <boost/uuid/detail/md5.hpp>
12 
13 #if !(defined(BOOST_UUID_COMPAT_PRE_1_71_MD5) && BOOST_ENDIAN_LITTLE_BYTE)
14 #include "digestutils.hpp"
15 #endif
16 
17 #define BOOST_TEST_MD5_DIGEST(lhs, rhs) \
18     ( boost::uuids::test::test_digest_equal_array(__FILE__, __LINE__, BOOST_CURRENT_FUNCTION, (lhs), (rhs), 16) )
19 
main(int,char **)20 int main(int, char**)
21 {
22     typedef struct
23     {
24         const char * data;
25         boost::uint32_t len;
26         unsigned char expected[16];
27     } Expectation;
28 
29     /* http://www.febooti.com/products/filetweak/members/hash-and-crc/test-vectors/ */
30     Expectation expectations[3] = {
31         { "The quick brown fox jumps over the lazy dog", 43,
32           { 0x9e, 0x10, 0x7d, 0x9d, 0x37, 0x2b, 0xb6, 0x82,
33             0x6b, 0xd8, 0x1d, 0x35, 0x42, 0xa4, 0x19, 0xd6 }},
34         { "Test vector from febooti.com", 28,
35           { 0x50, 0x0a, 0xb6, 0x61, 0x3c, 0x6d, 0xb7, 0xfb,
36             0xd3, 0x0c, 0x62, 0xf5, 0xff, 0x57, 0x3d, 0x0f }},
37         { "", 0,
38           { 0xd4, 0x1d, 0x8c, 0xd9, 0x8f, 0x00, 0xb2, 0x04,
39             0xe9, 0x80, 0x09, 0x98, 0xec, 0xf8, 0x42, 0x7e }}
40     };
41 
42     for (boost::uint32_t i = 0; i < 3; ++i) {
43         boost::uuids::detail::md5 hash;
44         hash.process_bytes(expectations[i].data, expectations[i].len);
45         boost::uuids::detail::md5::digest_type result;
46         hash.get_digest(result);
47 #if defined(BOOST_UUID_COMPAT_PRE_1_71_MD5) && BOOST_ENDIAN_LITTLE_BYTE
48         // this is the original, incorrect behavior from pre-1.71
49         BOOST_TEST_EQ(0, memcmp(result, expectations[i].expected,
50             sizeof(boost::uuids::detail::md5::digest_type)));
51 #else
52         unsigned char raw_result[16];
53         boost::uuids::test::copy_raw_digest(raw_result, result, 4);
54         BOOST_TEST_MD5_DIGEST(raw_result, expectations[i].expected);
55 #endif
56         BOOST_TEST_EQ(hash.get_version(), 0x03);
57 
58     }
59 
60     return boost::report_errors();
61 }
62