1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-11-26 20:20:58 +01:00

nlohmann::json instance and JSON Schema for Hash

Improving and codifying our experimental JSON interfacing.

Co-Authored-By: Robert Hensing <robert@roberthensing.nl>
This commit is contained in:
John Ericson 2025-09-13 08:32:26 -04:00
parent f05d240222
commit 5e7ee808de
16 changed files with 252 additions and 24 deletions

View file

@ -0,0 +1,5 @@
{
"algorithm": "blake3",
"format": "base64",
"hash": "nnDuFEmWX7YtBJBAoe0G7Dd0MNpuwTFz58T//NKL6YA="
}

View file

@ -0,0 +1,5 @@
{
"algorithm": "sha256",
"format": "base16",
"hash": "f0e4c2f76c58916ec258f246851bea091d14d4247a2fc3e18694461b1816e13b"
}

View file

@ -0,0 +1,5 @@
{
"algorithm": "sha256",
"format": "base64",
"hash": "8OTC92xYkW7CWPJGhRvqCR0U1CR6L8PhhpRGGxgW4Ts="
}

View file

@ -0,0 +1,5 @@
{
"algorithm": "sha256",
"format": "nix32",
"hash": "0fz12qc1nillhvhw6bvs4ka18789x8dqaipjb316x4aqdkvw5r7h"
}

View file

@ -0,0 +1,5 @@
{
"algorithm": "sha256",
"format": "base64",
"hash": "8OTC92xYkW7CWPJGhRvqCR0U1CR6L8PhhpRGGxgW4Ts="
}

View file

@ -4,30 +4,30 @@
#include <nlohmann/json.hpp>
#include "nix/util/hash.hh"
#include "nix/util/tests/characterization.hh"
#include "nix/util/tests/json-characterization.hh"
namespace nix {
class HashTest : public CharacterizationTest
class HashTest : public virtual CharacterizationTest
{
std::filesystem::path unitTestData = getUnitTestData() / "hash";
public:
/**
* We set these in tests rather than the regular globals so we don't have
* to worry about race conditions if the tests run concurrently.
*/
ExperimentalFeatureSettings mockXpSettings;
std::filesystem::path goldenMaster(std::string_view testStem) const override
{
return unitTestData / testStem;
}
};
class BLAKE3HashTest : public HashTest
struct BLAKE3HashTest : virtual HashTest
{
/**
* We set these in tests rather than the regular globals so we don't have
* to worry about race conditions if the tests run concurrently.
*/
ExperimentalFeatureSettings mockXpSettings;
void SetUp() override
{
mockXpSettings.set("experimental-features", "blake3-hashes");
@ -203,4 +203,97 @@ TEST(hashFormat, testParseHashFormatOptException)
{
ASSERT_EQ(parseHashFormatOpt("sha0042"), std::nullopt);
}
/* ----------------------------------------------------------------------------
* JSON
* --------------------------------------------------------------------------*/
using nlohmann::json;
struct HashJsonTest : virtual HashTest,
JsonCharacterizationTest<Hash>,
::testing::WithParamInterface<std::pair<std::string_view, Hash>>
{};
struct HashJsonParseOnlyTest : virtual HashTest,
JsonCharacterizationTest<Hash>,
::testing::WithParamInterface<std::pair<std::string_view, Hash>>
{};
struct BLAKE3HashJsonTest : virtual HashTest,
BLAKE3HashTest,
JsonCharacterizationTest<Hash>,
::testing::WithParamInterface<std::pair<std::string_view, Hash>>
{};
TEST_P(HashJsonTest, from_json)
{
auto & [name, expected] = GetParam();
readJsonTest(name, expected);
}
TEST_P(HashJsonTest, to_json)
{
auto & [name, value] = GetParam();
writeJsonTest(name, value);
}
TEST_P(HashJsonParseOnlyTest, from_json)
{
auto & [name, expected] = GetParam();
readJsonTest(name, expected);
}
TEST_P(BLAKE3HashJsonTest, from_json)
{
auto & [name, expected] = GetParam();
readJsonTest(name, expected, mockXpSettings);
}
TEST_P(BLAKE3HashJsonTest, to_json)
{
auto & [name, expected] = GetParam();
writeJsonTest(name, expected);
}
// Round-trip tests (from_json + to_json) for base64 format only
// (to_json always outputs base64)
INSTANTIATE_TEST_SUITE_P(
HashJSON,
HashJsonTest,
::testing::Values(
std::pair{
"simple",
hashString(HashAlgorithm::SHA256, "asdf"),
},
std::pair{
"sha256-base64",
hashString(HashAlgorithm::SHA256, "asdf"),
}));
// Parse-only tests for non-base64 formats
// These verify C++ can deserialize other formats correctly
INSTANTIATE_TEST_SUITE_P(
HashJSONParseOnly,
HashJsonParseOnlyTest,
::testing::Values(
std::pair{
"sha256-base16",
hashString(HashAlgorithm::SHA256, "asdf"),
},
std::pair{
"sha256-nix32",
hashString(HashAlgorithm::SHA256, "asdf"),
}));
INSTANTIATE_TEST_SUITE_P(BLAKE3HashJSONParseOnly, BLAKE3HashJsonTest, ([] {
ExperimentalFeatureSettings mockXpSettings;
mockXpSettings.set("experimental-features", "blake3-hashes");
return ::testing::Values(
std::pair{
"blake3-base64",
hashString(HashAlgorithm::BLAKE3, "asdf", mockXpSettings),
});
}()));
} // namespace nix