]> git.ipfire.org Git - pakfire.git/commitdiff
tests: Add tests for compression
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 18 Mar 2021 18:41:44 +0000 (18:41 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 18 Mar 2021 18:41:44 +0000 (18:41 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
.gitignore
Makefile.am
tests/data/compress/data.xz [new file with mode: 0644]
tests/libpakfire/compress.c [new file with mode: 0644]

index 0f5720b63eacfbf77eafad502c41b63e85621405..f5cb824785641fd4bd9e6139f14e766e122b7580 100644 (file)
@@ -12,6 +12,7 @@
 /tests/.root
 /tests/libpakfire/arch
 /tests/libpakfire/archive
+/tests/libpakfire/compress
 /tests/libpakfire/db
 /tests/libpakfire/downloader
 /tests/libpakfire/execute
index 3b1e43e4f5876e5a51abbe39bb96778dadb44a9c..5f68ba8669fa93ee5d5892cd1fe6558d2f122839 100644 (file)
@@ -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 (file)
index 0000000..4354f93
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 (file)
index 0000000..de20708
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#include <stdio.h>
+
+#include <pakfire/compress.h>
+#include <pakfire/util.h>
+
+#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();
+}