1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-11-08 19:46:02 +01:00

libutil: Throw if str("contents") not found

This was broken in 7aa3e7e3a5 (since 2.25).
This commit is contained in:
Samuel Connelly 2025-10-03 18:41:01 -04:00 committed by Sergei Zimmerman
parent 1e709554d5
commit 242f362567
No known key found for this signature in database
4 changed files with 52 additions and 2 deletions

View file

@ -0,0 +1,47 @@
#include "nix/util/archive.hh"
#include "nix/util/tests/characterization.hh"
#include "nix/util/tests/gmock-matchers.hh"
#include <gtest/gtest.h>
namespace nix {
namespace {
class NarTest : public CharacterizationTest
{
std::filesystem::path unitTestData = getUnitTestData() / "nars";
public:
std::filesystem::path goldenMaster(std::string_view testStem) const override
{
return unitTestData / (std::string(testStem) + ".nar");
}
};
class InvalidNarTest : public NarTest, public ::testing::WithParamInterface<std::tuple<std::string, std::string>>
{};
} // namespace
TEST_P(InvalidNarTest, throwsErrorMessage)
{
const auto & [name, message] = GetParam();
readTest(name, [&](const std::string & narContents) {
ASSERT_THAT(
[&]() {
StringSource source{narContents};
NullFileSystemObjectSink sink;
parseDump(sink, source);
},
::testing::ThrowsMessage<SerialisationError>(testing::HasSubstrIgnoreANSIMatcher(message)));
});
}
INSTANTIATE_TEST_SUITE_P(
NarTest,
InvalidNarTest,
::testing::Values(
std::pair{"invalid-tag-instead-of-contents", "bad archive: expected tag 'contents', got 'AAAAAAAA'"}));
} // namespace nix

View file

@ -45,6 +45,7 @@ subdir('nix-meson-build-support/common')
subdir('nix-meson-build-support/asan-options')
sources = files(
'archive.cc',
'args.cc',
'base-n.cc',
'canon-path.cc',

View file

@ -187,8 +187,10 @@ static void parse(FileSystemObjectSink & sink, Source & source, const CanonPath
tag = getString();
}
if (tag == "contents")
parseContents(crf, source);
if (tag != "contents")
throw badArchive("expected tag 'contents', got '%s'", tag);
parseContents(crf, source);
expectTag(")");
});