/root/bitcoin/src/test/fuzz/crypto_common.cpp
Line | Count | Source |
1 | | // Copyright (c) 2020 The Bitcoin Core developers |
2 | | // Distributed under the MIT software license, see the accompanying |
3 | | // file COPYING or http://www.opensource.org/licenses/mit-license.php. |
4 | | |
5 | | #include <crypto/common.h> |
6 | | #include <test/fuzz/FuzzedDataProvider.h> |
7 | | #include <test/fuzz/fuzz.h> |
8 | | #include <test/fuzz/util.h> |
9 | | |
10 | | #include <array> |
11 | | #include <cassert> |
12 | | #include <cstdint> |
13 | | #include <cstring> |
14 | | #include <vector> |
15 | | |
16 | | FUZZ_TARGET(crypto_common) |
17 | 0 | { |
18 | 0 | FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()}; |
19 | 0 | const uint16_t random_u16 = fuzzed_data_provider.ConsumeIntegral<uint16_t>(); |
20 | 0 | const uint32_t random_u32 = fuzzed_data_provider.ConsumeIntegral<uint32_t>(); |
21 | 0 | const uint64_t random_u64 = fuzzed_data_provider.ConsumeIntegral<uint64_t>(); |
22 | 0 | const std::vector<uint8_t> random_bytes_2 = ConsumeFixedLengthByteVector(fuzzed_data_provider, 2); |
23 | 0 | const std::vector<uint8_t> random_bytes_4 = ConsumeFixedLengthByteVector(fuzzed_data_provider, 4); |
24 | 0 | const std::vector<uint8_t> random_bytes_8 = ConsumeFixedLengthByteVector(fuzzed_data_provider, 8); |
25 | |
|
26 | 0 | std::array<uint8_t, 2> writele16_arr; |
27 | 0 | WriteLE16(writele16_arr.data(), random_u16); |
28 | 0 | assert(ReadLE16(writele16_arr.data()) == random_u16); |
29 | | |
30 | 0 | std::array<uint8_t, 4> writele32_arr; |
31 | 0 | WriteLE32(writele32_arr.data(), random_u32); |
32 | 0 | assert(ReadLE32(writele32_arr.data()) == random_u32); |
33 | | |
34 | 0 | std::array<uint8_t, 8> writele64_arr; |
35 | 0 | WriteLE64(writele64_arr.data(), random_u64); |
36 | 0 | assert(ReadLE64(writele64_arr.data()) == random_u64); |
37 | | |
38 | 0 | std::array<uint8_t, 2> writebe16_arr; |
39 | 0 | WriteBE16(writebe16_arr.data(), random_u16); |
40 | 0 | assert(ReadBE16(writebe16_arr.data()) == random_u16); |
41 | | |
42 | 0 | std::array<uint8_t, 4> writebe32_arr; |
43 | 0 | WriteBE32(writebe32_arr.data(), random_u32); |
44 | 0 | assert(ReadBE32(writebe32_arr.data()) == random_u32); |
45 | | |
46 | 0 | std::array<uint8_t, 8> writebe64_arr; |
47 | 0 | WriteBE64(writebe64_arr.data(), random_u64); |
48 | 0 | assert(ReadBE64(writebe64_arr.data()) == random_u64); |
49 | | |
50 | 0 | const uint16_t readle16_result = ReadLE16(random_bytes_2.data()); |
51 | 0 | std::array<uint8_t, 2> readle16_arr; |
52 | 0 | WriteLE16(readle16_arr.data(), readle16_result); |
53 | 0 | assert(std::memcmp(random_bytes_2.data(), readle16_arr.data(), 2) == 0); |
54 | | |
55 | 0 | const uint32_t readle32_result = ReadLE32(random_bytes_4.data()); |
56 | 0 | std::array<uint8_t, 4> readle32_arr; |
57 | 0 | WriteLE32(readle32_arr.data(), readle32_result); |
58 | 0 | assert(std::memcmp(random_bytes_4.data(), readle32_arr.data(), 4) == 0); |
59 | | |
60 | 0 | const uint64_t readle64_result = ReadLE64(random_bytes_8.data()); |
61 | 0 | std::array<uint8_t, 8> readle64_arr; |
62 | 0 | WriteLE64(readle64_arr.data(), readle64_result); |
63 | 0 | assert(std::memcmp(random_bytes_8.data(), readle64_arr.data(), 8) == 0); |
64 | | |
65 | 0 | const uint32_t readbe32_result = ReadBE32(random_bytes_4.data()); |
66 | 0 | std::array<uint8_t, 4> readbe32_arr; |
67 | 0 | WriteBE32(readbe32_arr.data(), readbe32_result); |
68 | 0 | assert(std::memcmp(random_bytes_4.data(), readbe32_arr.data(), 4) == 0); |
69 | | |
70 | 0 | const uint64_t readbe64_result = ReadBE64(random_bytes_8.data()); |
71 | 0 | std::array<uint8_t, 8> readbe64_arr; |
72 | 0 | WriteBE64(readbe64_arr.data(), readbe64_result); |
73 | | assert(std::memcmp(random_bytes_8.data(), readbe64_arr.data(), 8) == 0); |
74 | 0 | } |