From: Michael Tremer Date: Wed, 24 Aug 2022 17:07:45 +0000 (+0000) Subject: tests: Add tests for digest X-Git-Tag: 0.9.28~368 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=add7aec03abe17fb13e3c3e77f4a97765006ccb4;p=pakfire.git tests: Add tests for digest Signed-off-by: Michael Tremer --- diff --git a/.gitignore b/.gitignore index 62da71d58..949a9b680 100644 --- a/.gitignore +++ b/.gitignore @@ -21,6 +21,7 @@ /tests/libpakfire/config /tests/libpakfire/db /tests/libpakfire/dependencies +/tests/libpakfire/digest /tests/libpakfire/downloader /tests/libpakfire/execute /tests/libpakfire/file diff --git a/Makefile.am b/Makefile.am index ac558803a..e1a5f89f2 100644 --- a/Makefile.am +++ b/Makefile.am @@ -382,6 +382,7 @@ check_PROGRAMS += \ tests/libpakfire/config \ tests/libpakfire/db \ tests/libpakfire/dependencies \ + tests/libpakfire/digest \ tests/libpakfire/downloader \ tests/libpakfire/file \ tests/libpakfire/jail \ @@ -503,6 +504,18 @@ tests_libpakfire_dependencies_CFLAGS = \ tests_libpakfire_dependencies_LDADD = \ $(TESTSUITE_LDADD) +dist_tests_libpakfire_digest_SOURCES = \ + tests/libpakfire/digest.c + +tests_libpakfire_digest_CPPFLAGS = \ + $(TESTSUITE_CPPFLAGS) + +tests_libpakfire_digest_CFLAGS = \ + $(TESTSUITE_CFLAGS) + +tests_libpakfire_digest_LDADD = \ + $(TESTSUITE_LDADD) + dist_tests_libpakfire_downloader_SOURCES = \ tests/libpakfire/downloader.c @@ -878,6 +891,8 @@ EXTRA_DIST += \ tests/data/compress/data.xz \ tests/data/compress/data.zst \ \ + tests/data/digest/random \ + \ tests/data/packages/dummy/dummy.nm \ \ tests/data/parser/test-comments.txt \ diff --git a/tests/data/digest/random b/tests/data/digest/random new file mode 100644 index 000000000..9fe2a89ed Binary files /dev/null and b/tests/data/digest/random differ diff --git a/tests/libpakfire/digest.c b/tests/libpakfire/digest.c new file mode 100644 index 000000000..1084eedfc --- /dev/null +++ b/tests/libpakfire/digest.c @@ -0,0 +1,93 @@ +/*############################################################################# +# # +# Pakfire - The IPFire package management system # +# Copyright (C) 2022 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 "../testsuite.h" + +// Just a file with some random data +const char* RANDOM_FILE = ABS_TOP_SRCDIR "/tests/data/digest/random"; + +static const struct pakfire_digests expected_digests = { + .sha512 = { + 0xa1, 0x98, 0x97, 0x9b, 0x13, 0x44, 0xf3, 0x64, 0x48, 0x0e, 0x0f, 0xd1, + 0xbc, 0xde, 0x8f, 0x50, 0xf3, 0x3c, 0x92, 0x8e, 0xf0, 0xe7, 0x5d, 0x6e, + 0x1a, 0x28, 0x67, 0x1e, 0x16, 0x49, 0x7b, 0xc2, 0xd6, 0x64, 0xe5, 0x21, + 0x12, 0x60, 0x0f, 0x56, 0x3d, 0x8f, 0x0a, 0x7c, 0x24, 0x1a, 0x7e, 0x5a, + 0x16, 0xe7, 0xe7, 0x27, 0x33, 0xd9, 0x66, 0xb5, 0x0a, 0x39, 0x6d, 0xa8, + 0xb2, 0xb4, 0xac, 0x05, + }, + .sha256 = { + 0x16, 0xf9, 0x01, 0xc1, 0x83, 0x31, 0x43, 0x88, 0x4f, 0x16, 0x6a, 0xe4, + 0x81, 0x34, 0x51, 0x99, 0xcf, 0x91, 0x94, 0xa3, 0xb6, 0x01, 0x75, 0xd9, + 0xb9, 0xca, 0x93, 0xd9, 0xd7, 0xa0, 0x4b, 0xa6, + }, +}; + +static int test_init(const struct test* t) { + struct pakfire_digests digests = PAKFIRE_DIGESTS_INIT; + + // Check if everything is initialized correctly + ASSERT(pakfire_digest_set(digests.sha512) == 0); + ASSERT(pakfire_digest_set(digests.sha256) == 0); + + return EXIT_SUCCESS; + +FAIL: + return EXIT_FAILURE; +} + +static int test_random(const struct test* t) { + struct pakfire_digests digests = PAKFIRE_DIGESTS_INIT; + FILE* f = NULL; + int r = EXIT_FAILURE; + + ASSERT(f = fopen(RANDOM_FILE, "r")); + + // Compute digests + ASSERT_SUCCESS(pakfire_digests_compute_from_file(t->pakfire, &digests, + PAKFIRE_DIGESTS_ALL, f)); + + // Compare the digests + ASSERT_SUCCESS(pakfire_digests_compare(t->pakfire, + &digests, &expected_digests, PAKFIRE_DIGESTS_ALL)); + + // Check if the individual digests match + ASSERT_SUCCESS(pakfire_digests_compare_one(t->pakfire, &digests, + PAKFIRE_DIGEST_SHA512, expected_digests.sha512, sizeof(expected_digests.sha512))); + ASSERT_SUCCESS(pakfire_digests_compare_one(t->pakfire, &digests, + PAKFIRE_DIGEST_SHA256, expected_digests.sha256, sizeof(expected_digests.sha256))); + + r = EXIT_SUCCESS; + +FAIL: + if (f) + fclose(f); + + return r; +} + +int main(int argc, const char* argv[]) { + testsuite_add_test(test_init); + testsuite_add_test(test_random); + + return testsuite_run(argc, argv); +}