]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
0fe5f3c5 LP |
2 | /*** |
3 | This file is part of systemd. | |
4 | ||
5 | Copyright 2016 Lennart Poettering | |
6 | ||
7 | systemd is free software; you can redistribute it and/or modify it | |
8 | under the terms of the GNU Lesser General Public License as published by | |
9 | the Free Software Foundation; either version 2.1 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | systemd is distributed in the hope that it will be useful, but | |
13 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 | Lesser General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU Lesser General Public License | |
18 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
19 | ***/ | |
20 | ||
2e914f34 MP |
21 | #include <stdio.h> |
22 | ||
0fe5f3c5 LP |
23 | #include "alloc-util.h" |
24 | #include "log.h" | |
25 | #include "string-util.h" | |
26 | #include "khash.h" | |
27 | ||
28 | int main(int argc, char *argv[]) { | |
29 | _cleanup_(khash_unrefp) khash *h = NULL, *copy = NULL; | |
30 | _cleanup_free_ char *s = NULL; | |
2e914f34 | 31 | int r; |
0fe5f3c5 LP |
32 | |
33 | log_set_max_level(LOG_DEBUG); | |
34 | ||
35 | assert_se(khash_new(&h, NULL) == -EINVAL); | |
36 | assert_se(khash_new(&h, "") == -EINVAL); | |
2e914f34 MP |
37 | r = khash_new(&h, "foobar"); |
38 | if (r == -EAFNOSUPPORT) { | |
39 | puts("khash not supported on this kernel, skipping"); | |
40 | return EXIT_TEST_SKIP; | |
41 | } | |
42 | assert_se(r == -EOPNOTSUPP); | |
0fe5f3c5 LP |
43 | |
44 | assert_se(khash_new(&h, "sha256") >= 0); | |
45 | assert_se(khash_get_size(h) == 32); | |
46 | assert_se(streq(khash_get_algorithm(h), "sha256")); | |
47 | ||
48 | assert_se(khash_digest_string(h, &s) >= 0); | |
49 | assert_se(streq(s, "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855")); | |
50 | s = mfree(s); | |
51 | ||
52 | assert_se(khash_put(h, "foobar", 6) >= 0); | |
53 | assert_se(khash_digest_string(h, &s) >= 0); | |
54 | assert_se(streq(s, "c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2")); | |
55 | s = mfree(s); | |
56 | ||
57 | assert_se(khash_put(h, "piep", 4) >= 0); | |
58 | assert_se(khash_digest_string(h, &s) >= 0); | |
59 | assert_se(streq(s, "f114d872b5ea075d3be9040d0b7a429514b3f9324a8e8e3dc3fb24c34ee56bea")); | |
60 | s = mfree(s); | |
61 | ||
62 | assert_se(khash_put(h, "foo", 3) >= 0); | |
63 | assert_se(khash_dup(h, ©) >= 0); | |
64 | ||
65 | assert_se(khash_put(h, "bar", 3) >= 0); | |
66 | assert_se(khash_put(copy, "bar", 3) >= 0); | |
67 | ||
68 | assert_se(khash_digest_string(h, &s) >= 0); | |
69 | assert_se(streq(s, "c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2")); | |
70 | s = mfree(s); | |
71 | ||
72 | assert_se(khash_digest_string(copy, &s) >= 0); | |
73 | assert_se(streq(s, "c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2")); | |
74 | s = mfree(s); | |
75 | ||
76 | h = khash_unref(h); | |
77 | ||
78 | assert_se(khash_new_with_key(&h, "hmac(sha256)", "quux", 4) >= 0); | |
79 | assert_se(khash_get_size(h) == 32); | |
80 | assert_se(streq(khash_get_algorithm(h), "hmac(sha256)")); | |
81 | ||
82 | assert_se(khash_digest_string(h, &s) >= 0); | |
83 | assert_se(streq(s, "abed9f8218ab473f77218a6a7d39abf1d21fa46d0700c4898e330ba88309d5ae")); | |
84 | s = mfree(s); | |
85 | ||
86 | assert_se(khash_put(h, "foobar", 6) >= 0); | |
87 | assert_se(khash_digest_string(h, &s) >= 0); | |
88 | assert_se(streq(s, "33f6c70a60db66007d5325d5d1dea37c371354e5b83347a59ad339ce9f4ba3dc")); | |
89 | ||
90 | return 0; | |
91 | } |