fix cmake + add unit tests for TarArchiveReader

This commit is contained in:
Nikita Keba 2023-05-29 19:35:24 +00:00
parent 0bb5af1381
commit 636d50caa0
4 changed files with 78 additions and 3 deletions

View File

@ -127,7 +127,7 @@ set(SRCS
"${LIBRARY_DIR}/libarchive/archive_write_set_format_ustar.c" "${LIBRARY_DIR}/libarchive/archive_write_set_format_ustar.c"
"${LIBRARY_DIR}/libarchive/archive_write_set_format_v7tar.c" "${LIBRARY_DIR}/libarchive/archive_write_set_format_v7tar.c"
"${LIBRARY_DIR}/libarchive/archive_write_set_format_warc.c" "${LIBRARY_DIR}/libarchive/archive_write_set_format_warc.c"
# "${LIBRARY_DIR}/libarchive/archive_write_set_format_xar.c" "${LIBRARY_DIR}/libarchive/archive_write_set_format_xar.c"
"${LIBRARY_DIR}/libarchive/archive_write_set_format_zip.c" "${LIBRARY_DIR}/libarchive/archive_write_set_format_zip.c"
"${LIBRARY_DIR}/libarchive/archive_write_set_options.c" "${LIBRARY_DIR}/libarchive/archive_write_set_options.c"
"${LIBRARY_DIR}/libarchive/archive_write_set_passphrase.c" "${LIBRARY_DIR}/libarchive/archive_write_set_passphrase.c"

View File

@ -730,7 +730,7 @@ typedef uint64_t uintmax_t;
#define HAVE_LIBXML_XMLREADER_H 0 #define HAVE_LIBXML_XMLREADER_H 0
/* Define to 1 if you have the <libxml/xmlwriter.h> header file. */ /* Define to 1 if you have the <libxml/xmlwriter.h> header file. */
#define HAVE_LIBXML_XMLWRITER_H 1 // #define HAVE_LIBXML_XMLWRITER_H 1
/* Define to 1 if you have the `z' library (-lz). */ /* Define to 1 if you have the `z' library (-lz). */
/* #undef HAVE_LIBZ */ /* #undef HAVE_LIBZ */

View File

@ -0,0 +1,27 @@
#include <archive.h>
#include <archive_entry.h>
bool create_tar_with_file(const std::string &archivename, std::map<std::string, std::string> files) {
struct archive *a;
struct archive_entry *entry;
a = archive_write_new();
archive_write_set_format_pax_restricted(a);
archive_write_open_filename(a, archivename.c_str());
for (auto &[filename, content] : files) {
entry = archive_entry_new();
archive_entry_set_pathname(entry, filename.c_str());
archive_entry_set_size(entry, content.size());
archive_entry_set_mode(entry, S_IFREG | 0644); // regular file with rw-r--r-- permissions
archive_entry_set_mtime(entry, time(nullptr), 0);
archive_write_header(a, entry);
archive_write_data(a, content.c_str(), content.size());
archive_entry_free(entry);
}
archive_write_close(a);
archive_write_free(a);
return true;
}

View File

@ -5,6 +5,7 @@
#include <IO/Archives/IArchiveWriter.h> #include <IO/Archives/IArchiveWriter.h>
#include <IO/Archives/createArchiveReader.h> #include <IO/Archives/createArchiveReader.h>
#include <IO/Archives/createArchiveWriter.h> #include <IO/Archives/createArchiveWriter.h>
#include <IO/Archives/util/tar_archive_writer.h>
#include <IO/ReadBufferFromFileBase.h> #include <IO/ReadBufferFromFileBase.h>
#include <IO/ReadBufferFromString.h> #include <IO/ReadBufferFromString.h>
#include <IO/ReadHelpers.h> #include <IO/ReadHelpers.h>
@ -327,6 +328,53 @@ TEST_P(ArchiveReaderAndWriterTest, ArchiveNotExist)
[&]{ createArchiveReader(getPathToArchive()); }); [&]{ createArchiveReader(getPathToArchive()); });
} }
TEST(TarArchiveReaderTest, FileExists) {
String archive_path = "archive.tar";
String filename = "file.txt";
String contents = "test";
bool created = create_tar_with_file(archive_path, {{filename, contents}});
EXPECT_EQ(created, true);
auto reader = createArchiveReader(archive_path);
EXPECT_EQ(reader->fileExists(filename), true);
fs::remove(archive_path);
}
TEST(TarArchiveReaderTest, ReadFile) {
String archive_path = "archive.tar";
String filename = "file.txt";
String contents = "test";
bool created = create_tar_with_file(archive_path, {{filename, contents}});
EXPECT_EQ(created, true);
auto reader = createArchiveReader(archive_path);
auto in = reader->readFile(filename);
String str;
readStringUntilEOF(str, *in);
EXPECT_EQ(str, contents);
fs::remove(archive_path);
}
TEST(TarArchiveReaderTest, ReadTwoFiles) {
String archive_path = "archive.tar";
String file1 = "file1.txt";
String contents1 = "test1";
String file2 = "file2.txt";
String contents2 = "test2";
bool created = create_tar_with_file(archive_path, {{file1, contents1}, {file2, contents2}});
EXPECT_EQ(created, true);
auto reader = createArchiveReader(archive_path);
EXPECT_EQ(reader->fileExists(file1), true);
EXPECT_EQ(reader->fileExists(file2), true);
auto in = reader->readFile(file1);
String str;
readStringUntilEOF(str, *in);
EXPECT_EQ(str, contents1);
in = reader->readFile(file2);
readStringUntilEOF(str, *in);
EXPECT_EQ(str, contents2);
fs::remove(archive_path);
}
#if USE_MINIZIP #if USE_MINIZIP
@ -334,7 +382,7 @@ namespace
{ {
const char * supported_archive_file_exts[] = const char * supported_archive_file_exts[] =
{ {
".zip", ".zip"
}; };
} }