Branch data Line data Source code
1 : : /* SPDX-License-Identifier: LGPL-2.1+ */
2 : :
3 : : #include <errno.h>
4 : : #include <stdio.h>
5 : :
6 : : #include "alloc-util.h"
7 : : #include "log.h"
8 : : #include "string-util.h"
9 : : #include "khash.h"
10 : : #include "tests.h"
11 : :
12 : 4 : int main(int argc, char *argv[]) {
13 : 4 : _cleanup_(khash_unrefp) khash *h = NULL, *copy = NULL;
14 : 4 : _cleanup_free_ char *s = NULL;
15 : : int r;
16 : :
17 : 4 : test_setup_logging(LOG_DEBUG);
18 : :
19 [ - + ]: 4 : assert_se(khash_new(&h, NULL) == -EINVAL);
20 [ - + ]: 4 : assert_se(khash_new(&h, "") == -EINVAL);
21 : :
22 : 4 : r = khash_supported();
23 [ - + ]: 4 : assert_se(r >= 0);
24 [ - + ]: 4 : if (r == 0)
25 : 0 : return log_tests_skipped("khash not supported on this kernel");
26 : :
27 [ - + ]: 4 : assert_se(khash_new(&h, "foobar") == -EOPNOTSUPP); /* undefined hash function */
28 : :
29 [ - + ]: 4 : assert_se(khash_new(&h, "sha256") >= 0);
30 [ - + ]: 4 : assert_se(khash_get_size(h) == 32);
31 [ - + ]: 4 : assert_se(streq(khash_get_algorithm(h), "sha256"));
32 : :
33 [ - + ]: 4 : assert_se(khash_digest_string(h, &s) >= 0);
34 [ - + ]: 4 : assert_se(streq(s, "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"));
35 : 4 : s = mfree(s);
36 : :
37 [ - + ]: 4 : assert_se(khash_put(h, "foobar", 6) >= 0);
38 [ - + ]: 4 : assert_se(khash_digest_string(h, &s) >= 0);
39 [ - + ]: 4 : assert_se(streq(s, "c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2"));
40 : 4 : s = mfree(s);
41 : :
42 [ - + ]: 4 : assert_se(khash_put(h, "piep", 4) >= 0);
43 [ - + ]: 4 : assert_se(khash_digest_string(h, &s) >= 0);
44 [ - + ]: 4 : assert_se(streq(s, "f114d872b5ea075d3be9040d0b7a429514b3f9324a8e8e3dc3fb24c34ee56bea"));
45 : 4 : s = mfree(s);
46 : :
47 [ - + ]: 4 : assert_se(khash_put(h, "foo", 3) >= 0);
48 [ - + ]: 4 : assert_se(khash_dup(h, ©) >= 0);
49 : :
50 [ - + ]: 4 : assert_se(khash_put(h, "bar", 3) >= 0);
51 [ - + ]: 4 : assert_se(khash_put(copy, "bar", 3) >= 0);
52 : :
53 [ - + ]: 4 : assert_se(khash_digest_string(h, &s) >= 0);
54 [ - + ]: 4 : assert_se(streq(s, "c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2"));
55 : 4 : s = mfree(s);
56 : :
57 [ - + ]: 4 : assert_se(khash_digest_string(copy, &s) >= 0);
58 [ - + ]: 4 : assert_se(streq(s, "c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2"));
59 : 4 : s = mfree(s);
60 : :
61 : 4 : h = khash_unref(h);
62 : :
63 [ - + ]: 4 : assert_se(khash_new_with_key(&h, "hmac(sha256)", "quux", 4) >= 0);
64 [ - + ]: 4 : assert_se(khash_get_size(h) == 32);
65 [ - + ]: 4 : assert_se(streq(khash_get_algorithm(h), "hmac(sha256)"));
66 : :
67 [ - + ]: 4 : assert_se(khash_digest_string(h, &s) >= 0);
68 [ - + ]: 4 : assert_se(streq(s, "abed9f8218ab473f77218a6a7d39abf1d21fa46d0700c4898e330ba88309d5ae"));
69 : 4 : s = mfree(s);
70 : :
71 [ - + ]: 4 : assert_se(khash_put(h, "foobar", 6) >= 0);
72 [ - + ]: 4 : assert_se(khash_digest_string(h, &s) >= 0);
73 [ - + ]: 4 : assert_se(streq(s, "33f6c70a60db66007d5325d5d1dea37c371354e5b83347a59ad339ce9f4ba3dc"));
74 : :
75 : 4 : return 0;
76 : : }
|