]> git.ipfire.org Git - pakfire.git/commitdiff
downloader: Add tests that check digests
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 21 Sep 2021 13:45:26 +0000 (13:45 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 21 Sep 2021 13:45:26 +0000 (13:45 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
tests/libpakfire/downloader.c

index 9d2730c047d10832f44c038ade1779364e54098a..ee58df0f5b79ced9e0d1b50b4a4bdc5c4955715f 100644 (file)
@@ -119,6 +119,16 @@ FAIL:
 
 #ifdef ENABLE_ONLINE_TESTS
 
+static const unsigned char expected_digest[] = {
+       0xb7, 0x9b, 0xd6, 0xd2, 0x6a, 0x24, 0xe8, 0xeb, 0x46, 0x09, 0x26, 0x55, 0x70, 0x95,
+       0x2f, 0xf5, 0xd2, 0xfc, 0xd0, 0x8e, 0x14, 0x0d, 0x68, 0x36, 0x87, 0x70, 0xa8, 0x86,
+       0x46, 0xa9, 0x4e, 0xa7
+};
+
+static const unsigned char incorrect_digest[] = {
+       0x01, 0x02, 0x03, 0x04,
+};
+
 static int test_retrieve_online(const struct test* t) {
        struct pakfire_downloader* d = NULL;
        int r = EXIT_FAILURE;
@@ -143,6 +153,54 @@ FAIL:
        return r;
 }
 
+static int test_retrieve_online_with_digest(const struct test* t) {
+       struct pakfire_downloader* d = NULL;
+       int r = EXIT_FAILURE;
+
+       // Create downloader
+       ASSERT_SUCCESS(pakfire_downloader_create(&d, t->pakfire));
+
+       // Retrieve a file
+       ASSERT_SUCCESS(
+               pakfire_downloader_retrieve(d, NULL, NULL, NULL,
+                       "https://mirror1.ipfire.org/releases/pakfire/pakfire-0.9.27.tar.gz",
+                       DOWNLOAD_PATH, PAKFIRE_DIGEST_SHA256, expected_digest, sizeof(expected_digest), 0)
+       );
+
+       // Everything passed
+       r = EXIT_SUCCESS;
+
+FAIL:
+       if (d)
+               pakfire_downloader_unref(d);
+
+       return r;
+}
+
+static int test_retrieve_online_with_incorrect_digest(const struct test* t) {
+       struct pakfire_downloader* d = NULL;
+       int r = EXIT_FAILURE;
+
+       // Create downloader
+       ASSERT_SUCCESS(pakfire_downloader_create(&d, t->pakfire));
+
+       // Retrieve a file
+       ASSERT_FAILURE(
+               pakfire_downloader_retrieve(d, NULL, NULL, NULL,
+                       "https://mirror1.ipfire.org/releases/pakfire/pakfire-0.9.27.tar.gz",
+                       DOWNLOAD_PATH, PAKFIRE_DIGEST_SHA256, incorrect_digest, sizeof(incorrect_digest), 0)
+       );
+
+       // Everything passed
+       r = EXIT_SUCCESS;
+
+FAIL:
+       if (d)
+               pakfire_downloader_unref(d);
+
+       return r;
+}
+
 #endif /* ENABLE_ONLINE_TESTS */
 
 int main(int argc, char** argv) {
@@ -152,6 +210,8 @@ int main(int argc, char** argv) {
 
 #ifdef ENABLE_ONLINE_TESTS
        testsuite_add_test(test_retrieve_online);
+       testsuite_add_test(test_retrieve_online_with_digest);
+       testsuite_add_test(test_retrieve_online_with_incorrect_digest);
 #endif /* ENABLE_ONLINE_TESTS */
 
        return testsuite_run();