]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-hash.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2016 Lennart Poettering
11 #include "alloc-util.h"
13 #include "string-util.h"
16 int main(int argc
, char *argv
[]) {
17 _cleanup_(khash_unrefp
) khash
*h
= NULL
, *copy
= NULL
;
18 _cleanup_free_
char *s
= NULL
;
21 log_set_max_level(LOG_DEBUG
);
23 assert_se(khash_new(&h
, NULL
) == -EINVAL
);
24 assert_se(khash_new(&h
, "") == -EINVAL
);
26 r
= khash_supported();
29 puts("khash not supported on this kernel, skipping");
30 return EXIT_TEST_SKIP
;
33 assert_se(khash_new(&h
, "foobar") == -EOPNOTSUPP
); /* undefined hash function */
35 assert_se(khash_new(&h
, "sha256") >= 0);
36 assert_se(khash_get_size(h
) == 32);
37 assert_se(streq(khash_get_algorithm(h
), "sha256"));
39 assert_se(khash_digest_string(h
, &s
) >= 0);
40 assert_se(streq(s
, "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"));
43 assert_se(khash_put(h
, "foobar", 6) >= 0);
44 assert_se(khash_digest_string(h
, &s
) >= 0);
45 assert_se(streq(s
, "c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2"));
48 assert_se(khash_put(h
, "piep", 4) >= 0);
49 assert_se(khash_digest_string(h
, &s
) >= 0);
50 assert_se(streq(s
, "f114d872b5ea075d3be9040d0b7a429514b3f9324a8e8e3dc3fb24c34ee56bea"));
53 assert_se(khash_put(h
, "foo", 3) >= 0);
54 assert_se(khash_dup(h
, ©
) >= 0);
56 assert_se(khash_put(h
, "bar", 3) >= 0);
57 assert_se(khash_put(copy
, "bar", 3) >= 0);
59 assert_se(khash_digest_string(h
, &s
) >= 0);
60 assert_se(streq(s
, "c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2"));
63 assert_se(khash_digest_string(copy
, &s
) >= 0);
64 assert_se(streq(s
, "c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2"));
69 assert_se(khash_new_with_key(&h
, "hmac(sha256)", "quux", 4) >= 0);
70 assert_se(khash_get_size(h
) == 32);
71 assert_se(streq(khash_get_algorithm(h
), "hmac(sha256)"));
73 assert_se(khash_digest_string(h
, &s
) >= 0);
74 assert_se(streq(s
, "abed9f8218ab473f77218a6a7d39abf1d21fa46d0700c4898e330ba88309d5ae"));
77 assert_se(khash_put(h
, "foobar", 6) >= 0);
78 assert_se(khash_digest_string(h
, &s
) >= 0);
79 assert_se(streq(s
, "33f6c70a60db66007d5325d5d1dea37c371354e5b83347a59ad339ce9f4ba3dc"));