]>
Commit | Line | Data |
---|---|---|
add7aec0 MT |
1 | /*############################################################################# |
2 | # # | |
3 | # Pakfire - The IPFire package management system # | |
4 | # Copyright (C) 2022 Pakfire development team # | |
5 | # # | |
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. # | |
10 | # # | |
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. # | |
15 | # # | |
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/>. # | |
18 | # # | |
19 | #############################################################################*/ | |
20 | ||
21 | #include <pakfire/digest.h> | |
22 | #include <pakfire/util.h> | |
23 | ||
24 | #include "../testsuite.h" | |
25 | ||
26 | // Just a file with some random data | |
27 | const char* RANDOM_FILE = ABS_TOP_SRCDIR "/tests/data/digest/random"; | |
28 | ||
29 | static const struct pakfire_digests expected_digests = { | |
4500ed0a | 30 | .sha2_512 = { |
add7aec0 MT |
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, | |
37 | }, | |
4500ed0a | 38 | .sha2_256 = { |
add7aec0 MT |
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, | |
42 | }, | |
43 | }; | |
44 | ||
45 | static int test_init(const struct test* t) { | |
46 | struct pakfire_digests digests = PAKFIRE_DIGESTS_INIT; | |
47 | ||
48 | // Check if everything is initialized correctly | |
4500ed0a MT |
49 | ASSERT(pakfire_digest_set(digests.sha2_512) == 0); |
50 | ASSERT(pakfire_digest_set(digests.sha2_256) == 0); | |
add7aec0 MT |
51 | |
52 | return EXIT_SUCCESS; | |
53 | ||
54 | FAIL: | |
55 | return EXIT_FAILURE; | |
56 | } | |
57 | ||
58 | static int test_random(const struct test* t) { | |
59 | struct pakfire_digests digests = PAKFIRE_DIGESTS_INIT; | |
60 | FILE* f = NULL; | |
61 | int r = EXIT_FAILURE; | |
62 | ||
63 | ASSERT(f = fopen(RANDOM_FILE, "r")); | |
64 | ||
65 | // Compute digests | |
66 | ASSERT_SUCCESS(pakfire_digests_compute_from_file(t->pakfire, &digests, | |
67 | PAKFIRE_DIGESTS_ALL, f)); | |
68 | ||
69 | // Compare the digests | |
70 | ASSERT_SUCCESS(pakfire_digests_compare(t->pakfire, | |
4500ed0a | 71 | &digests, &expected_digests, PAKFIRE_DIGEST_SHA2_512|PAKFIRE_DIGEST_SHA2_256)); |
add7aec0 MT |
72 | |
73 | // Check if the individual digests match | |
74 | ASSERT_SUCCESS(pakfire_digests_compare_one(t->pakfire, &digests, | |
4500ed0a | 75 | PAKFIRE_DIGEST_SHA2_512, expected_digests.sha2_512, sizeof(expected_digests.sha2_512))); |
add7aec0 | 76 | ASSERT_SUCCESS(pakfire_digests_compare_one(t->pakfire, &digests, |
4500ed0a | 77 | PAKFIRE_DIGEST_SHA2_256, expected_digests.sha2_256, sizeof(expected_digests.sha2_256))); |
add7aec0 MT |
78 | |
79 | r = EXIT_SUCCESS; | |
80 | ||
81 | FAIL: | |
82 | if (f) | |
83 | fclose(f); | |
84 | ||
85 | return r; | |
86 | } | |
87 | ||
f7dd99ab MT |
88 | static int test_get_by_name(const struct test* t) { |
89 | // Positive cases | |
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); | |
96 | ||
97 | // Negative cases | |
98 | ASSERT(pakfire_digest_get_by_name("XXX") == 0); | |
99 | ASSERT(pakfire_digest_get_by_name(NULL) == 0); | |
100 | ||
101 | return EXIT_SUCCESS; | |
102 | ||
103 | FAIL: | |
104 | return EXIT_FAILURE; | |
105 | } | |
106 | ||
add7aec0 MT |
107 | int main(int argc, const char* argv[]) { |
108 | testsuite_add_test(test_init); | |
109 | testsuite_add_test(test_random); | |
f7dd99ab | 110 | testsuite_add_test(test_get_by_name); |
add7aec0 MT |
111 | |
112 | return testsuite_run(argc, argv); | |
113 | } |