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