summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/uuid/test/digestutils.hpp
blob: 499d95130927bba486f36e74706c2659aef5e778 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//
// Copyright (C) 2019 James E. King III
//
// Permission to copy, use, modify, sell and
// distribute this software is granted provided this copyright notice appears
// in all copies. This software is provided "as is" without express or implied
// warranty, and with no claim as to its suitability for any purpose.
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// https://www.boost.org/LICENSE_1_0.txt)
//

#ifndef BOOST_UUID_TEST_ENDIAN_2019
#define BOOST_UUID_TEST_ENDIAN_2019

#include <boost/predef/other/endian.h>

namespace boost {
namespace uuids {
namespace test {

//
// A utility to copy a raw digest out from one of the detail hash functions
// to a byte string for comparisons in testing to known values.
//
static void copy_raw_digest(unsigned char* out, const unsigned int *in, size_t inlen)
{
    for (size_t chunk = 0; chunk < inlen; ++chunk)
    {
        const unsigned char * cin = reinterpret_cast<const unsigned char *>(&in[chunk]);
        for (size_t byte = 0; byte < 4; ++byte)
        {
#if BOOST_ENDIAN_LITTLE_BYTE
            *out++ = *(cin + (3-byte));
#else
            *out++ = *(cin + byte);
#endif
        }
    }
}

//
// A utility to compare and report two raw hashes
//
static void test_digest_equal_array(char const * file, int line, char const * function,
                             const unsigned char *lhs,
                             const unsigned char *rhs,
                             size_t len)
{
    for (size_t i=0; i<len; i++) {
        if ( lhs[i] != rhs[i]) {
            std::cerr << file << "(" << line << "): digest [";
            for (size_t l=0; l<len; l++) {
                std::cerr << std::hex << (int)lhs[l];
            }

            std::cerr << "] not equal [";
            for (size_t r=0; r<len; r++) {
                std::cerr << std::hex << (int)rhs[r];
            }
            std::cerr << "] in function '" << function << "'" << std::endl;
            ++boost::detail::test_errors();
            return;
        }
    }
}

}
}
}

#endif