MCPcopy Create free account
hub / github.com/bitcoin/bitcoin / TestVector

Method TestVector

src/test/crypto_tests.cpp:36–62  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

34
35template<typename Hasher, typename In, typename Out>
36void TestVector(const Hasher &h, const In &in, const Out &out) {
37 Out hash;
38 BOOST_CHECK(out.size() == h.OUTPUT_SIZE);
39 hash.resize(out.size());
40 {
41 // Test that writing the whole input string at once works.
42 Hasher(h).Write((const uint8_t*)in.data(), in.size()).Finalize(hash.data());
43 BOOST_CHECK(hash == out);
44 }
45 for (int i=0; i<32; i++) {
46 // Test that writing the string broken up in random pieces works.
47 Hasher hasher(h);
48 size_t pos = 0;
49 while (pos < in.size()) {
50 size_t len = m_rng.randrange((in.size() - pos + 1) / 2 + 1);
51 hasher.Write((const uint8_t*)in.data() + pos, len);
52 pos += len;
53 if (pos > 0 && pos + 2 * out.size() > in.size() && pos < in.size()) {
54 // Test that writing the rest at once to a copy of a hasher works.
55 Hasher(hasher).Write((const uint8_t*)in.data() + pos, in.size() - pos).Finalize(hash.data());
56 BOOST_CHECK(hash == out);
57 }
58 }
59 hasher.Finalize(hash.data());
60 BOOST_CHECK(hash == out);
61 }
62}
63
64void TestSHA1(const std::string &in, const std::string &hexout) { TestVector(CSHA1(), in, ParseHex(hexout));}
65void TestSHA256(const std::string &in, const std::string &hexout) { TestVector(CSHA256(), in, ParseHex(hexout));}

Callers

nothing calls this directly

Calls 6

randrangeMethod · 0.80
sizeMethod · 0.45
resizeMethod · 0.45
FinalizeMethod · 0.45
WriteMethod · 0.45
dataMethod · 0.45

Tested by

no test coverage detected