#include "libstore/derivations.hh" #include #include #include #include #include #include #include #include #include #include #include #include "libexpr/eval.hh" #include "libutil/hash.hh" #include "libutil/types.hh" namespace rc { using nix::Derivation; using nix::DerivationOutput; template <> struct Arbitrary { static Gen arbitrary() { return gen::element(nix::Base16, nix::Base32, nix::Base64); } }; template <> struct Arbitrary { static Gen arbitrary() { return gen::apply( [](std::string content, std::string path, std::string hash_algo, bool recursive, bool include_algo_in_hash, nix::Base base) { auto hash_type = nix::parseHashType(hash_algo); auto hash = nix::hashString(hash_type, content); return DerivationOutput( path, recursive ? absl::StrCat("r:", hash_algo) : hash_algo, hash.to_string(base, include_algo_in_hash)); }, gen::arbitrary(), gen::map(gen::arbitrary(), [](std::string s) { return absl::StrCat("/", s); }), gen::element("md5", "sha1", "sha256", "sha512"), gen::arbitrary(), gen::arbitrary(), gen::arbitrary()); } }; template <> struct Arbitrary { static Gen arbitrary() { auto gen_path = gen::map(gen::arbitrary(), [](std::string s) { return absl::StrCat("/", s); }); return gen::build( gen::set(&nix::BasicDerivation::outputs), gen::set(&nix::BasicDerivation::inputSrcs, gen::container(gen_path)), gen::set(&nix::BasicDerivation::platform), gen::set(&nix::BasicDerivation::builder, gen_path), gen::set(&nix::BasicDerivation::args), gen::set(&nix::BasicDerivation::env), gen::set(&Derivation::inputDrvs, gen::container( gen_path, gen::arbitrary()))); } }; } // namespace rc namespace nix { void AssertDerivationsEqual(const Derivation& lhs, const Derivation& rhs) { RC_ASSERT(lhs.outputs.size() == rhs.outputs.size()); for (const auto& [k, lhs_v] : lhs.outputs) { auto rhs_v = rhs.outputs.find(k); RC_ASSERT(rhs_v != rhs.outputs.end()); RC_ASSERT(lhs_v.path == rhs_v->second.path); RC_ASSERT(lhs_v.hashAlgo == rhs_v->second.hashAlgo); RC_ASSERT(lhs_v.hash == rhs_v->second.hash); } RC_ASSERT(lhs.inputSrcs == rhs.inputSrcs); RC_ASSERT(lhs.platform == rhs.platform); RC_ASSERT(lhs.builder == rhs.builder); RC_ASSERT(lhs.args == rhs.args); RC_ASSERT(lhs.env == rhs.env); RC_ASSERT(lhs.inputDrvs == rhs.inputDrvs); } class DerivationsTest : public ::testing::Test {}; // NOLINTNEXTLINE RC_GTEST_FIXTURE_PROP(DerivationsTest, UnparseParseRoundTrip, (Derivation && drv)) { auto unparsed = drv.unparse(); auto parsed = parseDerivation(unparsed); AssertDerivationsEqual(drv, parsed); } class ParseDrvPathWithOutputsTest : public DerivationsTest {}; TEST(ParseDrvPathWithOutputsTest, ParseDrvPathWithOutputs) { auto input = "/nix/store/my51f75kp056md84gq2v08pd140pcz57-test.drv!out"; auto result = nix::parseDrvPathWithOutputs(input); ASSERT_EQ(result.first, "/nix/store/my51f75kp056md84gq2v08pd140pcz57-test.drv"); ASSERT_EQ(result.second, nix::PathSet{"out"}); } TEST(ParseDrvPathWithOutputsTest, ParseDrvPathWithMultipleOutputs) { auto input = "/nix/store/my51f75kp056md84gq2v08pd140pcz57-test.drv!out,dev"; auto result = nix::parseDrvPathWithOutputs(input); nix::PathSet expected = {"out", "dev"}; ASSERT_EQ(result.first, "/nix/store/my51f75kp056md84gq2v08pd140pcz57-test.drv"); ASSERT_EQ(result.second, expected); } TEST(ParseDrvPathWithOutputsTest, ParseDrvPathWithNoOutputs) { auto input = "/nix/store/my51f75kp056md84gq2v08pd140pcz57-test"; auto result = nix::parseDrvPathWithOutputs(input); ASSERT_EQ(result.first, "/nix/store/my51f75kp056md84gq2v08pd140pcz57-test"); ASSERT_EQ(result.second, nix::PathSet()); } } // namespace nix