Add missing libxml2-tools dependency
[archive/platform/upstream/libvirt.git] / tests / vircryptotest.c
1 /*
2  * vircryptotest.c: cryptographic helper test suite
3  *
4  * Copyright (C) 2014 Red Hat, Inc.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library 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 GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library.  If not, see
18  * <http://www.gnu.org/licenses/>.
19  */
20
21 #include <config.h>
22
23 #include "vircrypto.h"
24
25 #include "testutils.h"
26
27
28 struct testCryptoHashData {
29     virCryptoHash hash;
30     const char *input;
31     const char *output;
32 };
33
34 static int
35 testCryptoHash(const void *opaque)
36 {
37     const struct testCryptoHashData *data = opaque;
38     char *actual = NULL;
39     int ret = -1;
40
41     if (virCryptoHashString(data->hash, data->input, &actual) < 0) {
42         fprintf(stderr, "Failed to generate crypto hash\n");
43         goto cleanup;
44     }
45
46     if (STRNEQ_NULLABLE(data->output, actual)) {
47         fprintf(stderr, "Expected hash '%s' but got '%s'\n",
48                 data->output, NULLSTR(actual));
49         goto cleanup;
50     }
51
52     ret = 0;
53  cleanup:
54     VIR_FREE(actual);
55     return ret;
56 }
57
58
59 static int
60 mymain(void)
61 {
62     int ret = 0;
63
64 #define VIR_CRYPTO_HASH(h, i, o)                \
65     do {                                        \
66         struct testCryptoHashData data = {      \
67             .hash = h,                          \
68             .input = i,                         \
69             .output = o,                        \
70         };                                      \
71         if (virtTestRun("Hash " i, testCryptoHash, &data) < 0) \
72             ret = -1;                                          \
73     } while (0)
74
75     VIR_CRYPTO_HASH(VIR_CRYPTO_HASH_MD5, "", "d41d8cd98f00b204e9800998ecf8427e");
76     VIR_CRYPTO_HASH(VIR_CRYPTO_HASH_SHA256, "", "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
77
78     VIR_CRYPTO_HASH(VIR_CRYPTO_HASH_MD5, " ", "7215ee9c7d9dc229d2921a40e899ec5f");
79     VIR_CRYPTO_HASH(VIR_CRYPTO_HASH_SHA256, " ", "36a9e7f1c95b82ffb99743e0c5c4ce95d83c9a430aac59f84ef3cbfab6145068");
80
81     VIR_CRYPTO_HASH(VIR_CRYPTO_HASH_MD5, "\n", "68b329da9893e34099c7d8ad5cb9c940");
82     VIR_CRYPTO_HASH(VIR_CRYPTO_HASH_SHA256, "\n", "01ba4719c80b6fe911b091a7c05124b64eeece964e09c058ef8f9805daca546b");
83
84     VIR_CRYPTO_HASH(VIR_CRYPTO_HASH_MD5, "The quick brown fox", "a2004f37730b9445670a738fa0fc9ee5");
85     VIR_CRYPTO_HASH(VIR_CRYPTO_HASH_SHA256, "The quick brown fox", "5cac4f980fedc3d3f1f99b4be3472c9b30d56523e632d151237ec9309048bda9");
86
87     return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
88 }
89
90 VIRT_TEST_MAIN(mymain)