Skip to content

Commit eea6d75

Browse files
authored
Merge pull request #14168 from xokdvium/nar-require-contents
libutil: Throw if `str("contents")` not found
2 parents 1e70955 + 242f362 commit eea6d75

File tree

4 files changed

+52
-2
lines changed

4 files changed

+52
-2
lines changed

src/libutil-tests/archive.cc

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include "nix/util/archive.hh"
2+
#include "nix/util/tests/characterization.hh"
3+
#include "nix/util/tests/gmock-matchers.hh"
4+
5+
#include <gtest/gtest.h>
6+
7+
namespace nix {
8+
9+
namespace {
10+
11+
class NarTest : public CharacterizationTest
12+
{
13+
std::filesystem::path unitTestData = getUnitTestData() / "nars";
14+
15+
public:
16+
std::filesystem::path goldenMaster(std::string_view testStem) const override
17+
{
18+
return unitTestData / (std::string(testStem) + ".nar");
19+
}
20+
};
21+
22+
class InvalidNarTest : public NarTest, public ::testing::WithParamInterface<std::tuple<std::string, std::string>>
23+
{};
24+
25+
} // namespace
26+
27+
TEST_P(InvalidNarTest, throwsErrorMessage)
28+
{
29+
const auto & [name, message] = GetParam();
30+
readTest(name, [&](const std::string & narContents) {
31+
ASSERT_THAT(
32+
[&]() {
33+
StringSource source{narContents};
34+
NullFileSystemObjectSink sink;
35+
parseDump(sink, source);
36+
},
37+
::testing::ThrowsMessage<SerialisationError>(testing::HasSubstrIgnoreANSIMatcher(message)));
38+
});
39+
}
40+
41+
INSTANTIATE_TEST_SUITE_P(
42+
NarTest,
43+
InvalidNarTest,
44+
::testing::Values(
45+
std::pair{"invalid-tag-instead-of-contents", "bad archive: expected tag 'contents', got 'AAAAAAAA'"}));
46+
47+
} // namespace nix
104 Bytes
Binary file not shown.

src/libutil-tests/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ subdir('nix-meson-build-support/common')
4545
subdir('nix-meson-build-support/asan-options')
4646

4747
sources = files(
48+
'archive.cc',
4849
'args.cc',
4950
'base-n.cc',
5051
'canon-path.cc',

src/libutil/archive.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,10 @@ static void parse(FileSystemObjectSink & sink, Source & source, const CanonPath
187187
tag = getString();
188188
}
189189

190-
if (tag == "contents")
191-
parseContents(crf, source);
190+
if (tag != "contents")
191+
throw badArchive("expected tag 'contents', got '%s'", tag);
192+
193+
parseContents(crf, source);
192194

193195
expectTag(")");
194196
});

0 commit comments

Comments
 (0)