mirror of
https://github.com/NixOS/nix.git
synced 2025-11-09 03:56:01 +01:00
Merge pull request #14168 from xokdvium/nar-require-contents
libutil: Throw if `str("contents")` not found
This commit is contained in:
commit
eea6d75783
4 changed files with 52 additions and 2 deletions
47
src/libutil-tests/archive.cc
Normal file
47
src/libutil-tests/archive.cc
Normal 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
|
||||||
BIN
src/libutil-tests/data/nars/invalid-tag-instead-of-contents.nar
Normal file
BIN
src/libutil-tests/data/nars/invalid-tag-instead-of-contents.nar
Normal file
Binary file not shown.
|
|
@ -45,6 +45,7 @@ subdir('nix-meson-build-support/common')
|
||||||
subdir('nix-meson-build-support/asan-options')
|
subdir('nix-meson-build-support/asan-options')
|
||||||
|
|
||||||
sources = files(
|
sources = files(
|
||||||
|
'archive.cc',
|
||||||
'args.cc',
|
'args.cc',
|
||||||
'base-n.cc',
|
'base-n.cc',
|
||||||
'canon-path.cc',
|
'canon-path.cc',
|
||||||
|
|
|
||||||
|
|
@ -187,7 +187,9 @@ static void parse(FileSystemObjectSink & sink, Source & source, const CanonPath
|
||||||
tag = getString();
|
tag = getString();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (tag == "contents")
|
if (tag != "contents")
|
||||||
|
throw badArchive("expected tag 'contents', got '%s'", tag);
|
||||||
|
|
||||||
parseContents(crf, source);
|
parseContents(crf, source);
|
||||||
|
|
||||||
expectTag(")");
|
expectTag(")");
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue