From: Michael Tremer Date: Thu, 18 Mar 2021 18:41:44 +0000 (+0000) Subject: tests: Add tests for compression X-Git-Tag: 0.9.28~1285^2~517 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d909a3a2500183731fdf1b2dedb88e2c194796a3;p=pakfire.git tests: Add tests for compression Signed-off-by: Michael Tremer --- diff --git a/.gitignore b/.gitignore index 0f5720b63..f5cb82478 100644 --- a/.gitignore +++ b/.gitignore @@ -12,6 +12,7 @@ /tests/.root /tests/libpakfire/arch /tests/libpakfire/archive +/tests/libpakfire/compress /tests/libpakfire/db /tests/libpakfire/downloader /tests/libpakfire/execute diff --git a/Makefile.am b/Makefile.am index 3b1e43e4f..5f68ba866 100644 --- a/Makefile.am +++ b/Makefile.am @@ -368,6 +368,7 @@ check_PROGRAMS += \ tests/libpakfire/main \ tests/libpakfire/arch \ tests/libpakfire/archive \ + tests/libpakfire/compress \ tests/libpakfire/db \ tests/libpakfire/downloader \ tests/libpakfire/execute \ @@ -409,6 +410,19 @@ tests_libpakfire_archive_LDADD = \ $(TESTSUITE_LDADD) \ $(PAKFIRE_LIBS) +dist_tests_libpakfire_compress_SOURCES = \ + tests/libpakfire/compress.c \ + src/libpakfire/compress.c + +tests_libpakfire_compress_CPPFLAGS = \ + $(TESTSUITE_CPPFLAGS) \ + -DPAKFIRE_PRIVATE + +tests_libpakfire_compress_LDADD = \ + $(TESTSUITE_LDADD) \ + $(PAKFIRE_LIBS) \ + $(LZMA_LIBS) + tests_libpakfire_db_SOURCES = \ tests/libpakfire/db.c @@ -686,6 +700,8 @@ EXTRA_DIST += \ tests/data/beep.nm \ tests/data/kernel.nm \ \ + tests/data/compress/data.xz \ + \ tests/data/parser/test-comments.txt \ tests/data/parser/test-conditionals.txt \ tests/data/parser/test-declarations.txt \ diff --git a/tests/data/compress/data.xz b/tests/data/compress/data.xz new file mode 100644 index 000000000..4354f9366 Binary files /dev/null and b/tests/data/compress/data.xz differ diff --git a/tests/libpakfire/compress.c b/tests/libpakfire/compress.c new file mode 100644 index 000000000..de207084d --- /dev/null +++ b/tests/libpakfire/compress.c @@ -0,0 +1,82 @@ +/*############################################################################# +# # +# Pakfire - The IPFire package management system # +# Copyright (C) 2021 Pakfire development team # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see . # +# # +#############################################################################*/ + +#include + +#include +#include + +#include "../testsuite.h" + +const char TEST_DATA[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ\n"; + +static int test_xzfopen_read(const struct test* t) { + char buffer[1024]; + + char* path = pakfire_path_join(TEST_SRC_PATH, "data/compress/data.xz"); + ASSERT(path); + + FILE* f = fopen(path, "r"); + ASSERT(f); + + // Engage decompressor + f = pakfire_xzfopen(f, "r"); + ASSERT(f); + + // Read into buffer + size_t bytes_read = fread(buffer, 1, sizeof(buffer), f); + ASSERT(bytes_read); + + // Buffer should equal the test data + ASSERT(bytes_read >= sizeof(TEST_DATA) - 1); + ASSERT(memcmp(buffer, TEST_DATA, sizeof(TEST_DATA) - 1) == 0); + + fclose(f); + free(path); + + return EXIT_SUCCESS; +} + +static int test_xzfopen_write(const struct test* t) { + // Create a backend storage file + FILE* f = test_mktemp(); + ASSERT(f); + + // Open compressed file for writing + f = pakfire_xzfopen(f, "w"); + ASSERT(f); + + // Write some data and close fwrite + for (unsigned int i = 0; i < 1000; i++) { + size_t bytes_written = fwrite(TEST_DATA, 1, sizeof(TEST_DATA) - 1, f); + ASSERT(bytes_written == sizeof(TEST_DATA) - 1); + } + + fclose(f); + + return EXIT_SUCCESS; +} + +int main(int argc, char** argv) { + testsuite_add_test(test_xzfopen_read); + testsuite_add_test(test_xzfopen_write); + + return testsuite_run(); +}