1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-11-19 16:59:35 +01:00

Introduce Hash::parseExplicitFormatUnprefixed

This commit is contained in:
John Ericson 2025-09-12 08:13:45 -04:00
parent c6d06ce486
commit 095ac66d4c
3 changed files with 87 additions and 8 deletions

View file

@ -1,13 +1,17 @@
#include <regex>
#include <gtest/gtest.h>
#include <nlohmann/json.hpp>
#include "nix/util/hash.hh"
#include "nix/util/tests/characterization.hh"
namespace nix {
class BLAKE3HashTest : public virtual ::testing::Test
class HashTest : public CharacterizationTest
{
std::filesystem::path unitTestData = getUnitTestData() / "hash";
public:
/**
@ -16,8 +20,14 @@ public:
*/
ExperimentalFeatureSettings mockXpSettings;
private:
std::filesystem::path goldenMaster(std::string_view testStem) const override
{
return unitTestData / testStem;
}
};
class BLAKE3HashTest : public HashTest
{
void SetUp() override
{
mockXpSettings.set("experimental-features", "blake3-hashes");
@ -137,6 +147,46 @@ TEST(hashString, testKnownSHA512Hashes2)
"c7d329eeb6dd26545e96e55b874be909");
}
/* ----------------------------------------------------------------------------
* parsing hashes
* --------------------------------------------------------------------------*/
TEST(hashParseExplicitFormatUnprefixed, testKnownSHA256Hashes1_correct)
{
// values taken from: https://tools.ietf.org/html/rfc4634
auto s = "abc";
auto hash = hashString(HashAlgorithm::SHA256, s);
ASSERT_EQ(
hash,
Hash::parseExplicitFormatUnprefixed(
"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad",
HashAlgorithm::SHA256,
HashFormat::Base16));
}
TEST(hashParseExplicitFormatUnprefixed, testKnownSHA256Hashes1_wrongAlgo)
{
// values taken from: https://tools.ietf.org/html/rfc4634
ASSERT_THROW(
Hash::parseExplicitFormatUnprefixed(
"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad",
HashAlgorithm::SHA1,
HashFormat::Base16),
BadHash);
}
TEST(hashParseExplicitFormatUnprefixed, testKnownSHA256Hashes1_wrongBase)
{
// values taken from: https://tools.ietf.org/html/rfc4634
ASSERT_THROW(
Hash::parseExplicitFormatUnprefixed(
"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad",
HashAlgorithm::SHA256,
HashFormat::Nix32),
BadHash);
}
/* ----------------------------------------------------------------------------
* parseHashFormat, parseHashFormatOpt, printHashFormat
* --------------------------------------------------------------------------*/