mirror of
https://gitlab.com/shadow_contributor/ntptun.git
synced 2026-10-03 11:56:43 +00:00
57 lines
1.7 KiB
C++
57 lines
1.7 KiB
C++
#include "ntptun/Bytes.hpp"
|
|
#include "ntptun/Sha256.hpp"
|
|
#include "TestUtil.hpp"
|
|
|
|
#include <string>
|
|
|
|
using namespace ntptun;
|
|
|
|
namespace {
|
|
|
|
bool digest_equals(const Sha256::Digest& got, const std::string& hex) {
|
|
const auto expected = from_hex(hex);
|
|
if (!expected || expected->size() != got.size()) {
|
|
return false;
|
|
}
|
|
for (std::size_t i = 0; i < got.size(); ++i) {
|
|
if (got[i] != (*expected)[i]) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
Sha256::Digest hash_str(const std::string& s) {
|
|
return Sha256::hash(reinterpret_cast<const Byte*>(s.data()), s.size());
|
|
}
|
|
|
|
} // namespace
|
|
|
|
void run_tests() {
|
|
// FIPS 180-4 / NIST test vectors.
|
|
CHECK(digest_equals(
|
|
hash_str(""),
|
|
"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"));
|
|
CHECK(digest_equals(
|
|
hash_str("abc"),
|
|
"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"));
|
|
CHECK(digest_equals(
|
|
hash_str("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"),
|
|
"248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1"));
|
|
|
|
// A message spanning multiple blocks (> 55 bytes) exercises padding paths.
|
|
std::string long_msg(1000, 'a');
|
|
CHECK(digest_equals(
|
|
hash_str(long_msg),
|
|
"41edece42d63e8d9bf515a9ba6932e1c20cbc9f5a5d134645adb5db1b9737ea3"));
|
|
|
|
// Incremental updates must match the one-shot result.
|
|
Sha256 h;
|
|
h.update(reinterpret_cast<const Byte*>("ab"), 2);
|
|
h.update(reinterpret_cast<const Byte*>("c"), 1);
|
|
CHECK(digest_equals(
|
|
h.finish(),
|
|
"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"));
|
|
}
|
|
|
|
TEST_MAIN()
|