1 /*#############################################################################
3 # Pakfire - The IPFire package management system #
4 # Copyright (C) 2022 Pakfire development team #
6 # This program is free software: you can redistribute it and/or modify #
7 # it under the terms of the GNU General Public License as published by #
8 # the Free Software Foundation, either version 3 of the License, or #
9 # (at your option) any later version. #
11 # This program is distributed in the hope that it will be useful, #
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
14 # GNU General Public License for more details. #
16 # You should have received a copy of the GNU General Public License #
17 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
19 #############################################################################*/
21 #include <pakfire/digest.h>
22 #include <pakfire/util.h>
24 #include "../testsuite.h"
26 // Just a file with some random data
27 const char* RANDOM_FILE
= ABS_TOP_SRCDIR
"/tests/data/digest/random";
29 static const struct pakfire_digests expected_digests
= {
31 0xa1, 0x98, 0x97, 0x9b, 0x13, 0x44, 0xf3, 0x64, 0x48, 0x0e, 0x0f, 0xd1,
32 0xbc, 0xde, 0x8f, 0x50, 0xf3, 0x3c, 0x92, 0x8e, 0xf0, 0xe7, 0x5d, 0x6e,
33 0x1a, 0x28, 0x67, 0x1e, 0x16, 0x49, 0x7b, 0xc2, 0xd6, 0x64, 0xe5, 0x21,
34 0x12, 0x60, 0x0f, 0x56, 0x3d, 0x8f, 0x0a, 0x7c, 0x24, 0x1a, 0x7e, 0x5a,
35 0x16, 0xe7, 0xe7, 0x27, 0x33, 0xd9, 0x66, 0xb5, 0x0a, 0x39, 0x6d, 0xa8,
36 0xb2, 0xb4, 0xac, 0x05,
39 0x16, 0xf9, 0x01, 0xc1, 0x83, 0x31, 0x43, 0x88, 0x4f, 0x16, 0x6a, 0xe4,
40 0x81, 0x34, 0x51, 0x99, 0xcf, 0x91, 0x94, 0xa3, 0xb6, 0x01, 0x75, 0xd9,
41 0xb9, 0xca, 0x93, 0xd9, 0xd7, 0xa0, 0x4b, 0xa6,
45 static int test_init(const struct test
* t
) {
46 struct pakfire_digests digests
= PAKFIRE_DIGESTS_INIT
;
48 // Check if everything is initialized correctly
49 ASSERT(pakfire_digest_set(digests
.sha2_512
) == 0);
50 ASSERT(pakfire_digest_set(digests
.sha2_256
) == 0);
58 static int test_random(const struct test
* t
) {
59 struct pakfire_digests digests
= PAKFIRE_DIGESTS_INIT
;
63 ASSERT(f
= fopen(RANDOM_FILE
, "r"));
66 ASSERT_SUCCESS(pakfire_digests_compute_from_file(t
->pakfire
, &digests
,
67 PAKFIRE_DIGESTS_ALL
, f
));
69 // Compare the digests
70 ASSERT_SUCCESS(pakfire_digests_compare(t
->pakfire
,
71 &digests
, &expected_digests
, PAKFIRE_DIGEST_SHA2_512
|PAKFIRE_DIGEST_SHA2_256
));
73 // Check if the individual digests match
74 ASSERT_SUCCESS(pakfire_digests_compare_one(t
->pakfire
, &digests
,
75 PAKFIRE_DIGEST_SHA2_512
, expected_digests
.sha2_512
, sizeof(expected_digests
.sha2_512
)));
76 ASSERT_SUCCESS(pakfire_digests_compare_one(t
->pakfire
, &digests
,
77 PAKFIRE_DIGEST_SHA2_256
, expected_digests
.sha2_256
, sizeof(expected_digests
.sha2_256
)));
88 static int test_get_by_name(const struct test
* t
) {
90 ASSERT(pakfire_digest_get_by_name("sha2-512") == PAKFIRE_DIGEST_SHA2_512
);
91 ASSERT(pakfire_digest_get_by_name("sha2-256") == PAKFIRE_DIGEST_SHA2_256
);
92 ASSERT(pakfire_digest_get_by_name("blake2b512") == PAKFIRE_DIGEST_BLAKE2B512
);
93 ASSERT(pakfire_digest_get_by_name("blake2s256") == PAKFIRE_DIGEST_BLAKE2S256
);
94 ASSERT(pakfire_digest_get_by_name("sha3-512") == PAKFIRE_DIGEST_SHA3_512
);
95 ASSERT(pakfire_digest_get_by_name("sha3-256") == PAKFIRE_DIGEST_SHA3_256
);
98 ASSERT(pakfire_digest_get_by_name("XXX") == 0);
99 ASSERT(pakfire_digest_get_by_name(NULL
) == 0);
107 int main(int argc
, const char* argv
[]) {
108 testsuite_add_test(test_init
);
109 testsuite_add_test(test_random
);
110 testsuite_add_test(test_get_by_name
);
112 return testsuite_run(argc
, argv
);