mirror of
https://github.com/mhx/dwarfs.git
synced 2025-09-09 04:19:10 -04:00
test(filesystem): find_image_offset tests
This commit is contained in:
parent
a0acdacd03
commit
f95ae41a9b
@ -26,9 +26,13 @@
|
||||
|
||||
#include <fmt/format.h>
|
||||
|
||||
#include <folly/Utility.h>
|
||||
|
||||
#include "dwarfs/filesystem_v2.h"
|
||||
#include "dwarfs/fstypes.h"
|
||||
#include "dwarfs/mmap.h"
|
||||
|
||||
#include "mmap_mock.h"
|
||||
#include "test_logger.h"
|
||||
|
||||
using namespace dwarfs;
|
||||
@ -178,3 +182,84 @@ TEST(filesystem, metadata_symlink_unix) {
|
||||
EXPECT_EQ(-EINVAL, r.error());
|
||||
}
|
||||
}
|
||||
|
||||
namespace {
|
||||
|
||||
std::string valid_v1_header() {
|
||||
section_header hdr;
|
||||
hdr.type = section_type::BLOCK;
|
||||
hdr.compression = compression_type_v1::NONE;
|
||||
hdr.length = 1;
|
||||
std::string buf;
|
||||
buf.resize(8 + sizeof(hdr));
|
||||
std::memcpy(buf.data(), "DWARFS\x02\x01", 8);
|
||||
std::memcpy(buf.data() + 8, &hdr, sizeof(hdr));
|
||||
return buf;
|
||||
}
|
||||
|
||||
std::string valid_v2_header(uint32_t section_number = 0) {
|
||||
section_header_v2 hdr;
|
||||
std::memset(&hdr, 0, sizeof(hdr));
|
||||
std::memcpy(hdr.magic, "DWARFS", 6);
|
||||
hdr.major = 2;
|
||||
hdr.minor = 3;
|
||||
hdr.number = section_number;
|
||||
hdr.type = folly::to_underlying(section_type::BLOCK);
|
||||
hdr.compression = folly::to_underlying(compression_type::NONE);
|
||||
hdr.length = 1;
|
||||
std::string buf;
|
||||
buf.resize(sizeof(hdr));
|
||||
std::memcpy(buf.data(), &hdr, sizeof(hdr));
|
||||
return buf;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
TEST(filesystem, find_image_offset) {
|
||||
test::test_logger lgr;
|
||||
|
||||
auto make_fs =
|
||||
[&](std::string data,
|
||||
filesystem_options const& opt = {
|
||||
.image_offset = filesystem_options::IMAGE_OFFSET_AUTO}) {
|
||||
return filesystem_v2(
|
||||
lgr, std::make_shared<test::mmap_mock>(std::move(data)), opt);
|
||||
};
|
||||
|
||||
auto throws_rt_error = [](auto substr) {
|
||||
return ::testing::ThrowsMessage<runtime_error>(
|
||||
::testing::HasSubstr(substr));
|
||||
};
|
||||
|
||||
auto throws_no_fs_found = throws_rt_error("no filesystem found");
|
||||
auto throws_no_schema = throws_rt_error("no metadata schema found");
|
||||
|
||||
std::string valid_fs;
|
||||
{
|
||||
mmap mm(test_dir / "unixlink.dwarfs");
|
||||
auto data = mm.span<char>();
|
||||
valid_fs.assign(data.data(), data.size());
|
||||
}
|
||||
auto v1_header = valid_v1_header();
|
||||
auto v2_header = valid_v2_header();
|
||||
auto prefix = "DWARFS\x02\x02 DWARFS\x02\x02 xxxxxxxxxxxxxxxxxxxxDWARFS\x02";
|
||||
|
||||
EXPECT_NO_THROW(make_fs(valid_fs));
|
||||
EXPECT_NO_THROW(make_fs(prefix + valid_fs));
|
||||
|
||||
EXPECT_THAT([&] { make_fs("dwarfs"); }, throws_no_fs_found);
|
||||
EXPECT_THAT([&] { make_fs(v1_header + "X"); }, throws_no_schema);
|
||||
EXPECT_THAT([&] { make_fs(v2_header + "X"); }, throws_no_fs_found);
|
||||
EXPECT_THAT([&] { make_fs(v2_header + "X" + valid_v2_header(1) + "X"); },
|
||||
throws_no_schema);
|
||||
EXPECT_THAT([&] { make_fs(prefix); }, throws_no_fs_found);
|
||||
|
||||
for (size_t len = 0; len < valid_fs.size() - 1; ++len) {
|
||||
auto truncated = valid_fs.substr(0, len);
|
||||
EXPECT_THAT([&] { make_fs(truncated); }, ::testing::Throws<runtime_error>())
|
||||
<< "len=" << len;
|
||||
EXPECT_THAT([&] { make_fs(prefix + truncated); },
|
||||
::testing::Throws<runtime_error>())
|
||||
<< "len=" << len;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user