diff options
Diffstat (limited to 'src/service/reclaim/test_did_helper.c')
-rw-r--r-- | src/service/reclaim/test_did_helper.c | 137 |
1 files changed, 137 insertions, 0 deletions
diff --git a/src/service/reclaim/test_did_helper.c b/src/service/reclaim/test_did_helper.c new file mode 100644 index 000000000..c11f47540 --- /dev/null +++ b/src/service/reclaim/test_did_helper.c | |||
@@ -0,0 +1,137 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet | ||
3 | Copyright (C) 2010-2015 GNUnet e.V. | ||
4 | |||
5 | GNUnet is free software: you can redistribute it and/or modify it | ||
6 | under the terms of the GNU Affero General Public License as published | ||
7 | by the Free Software Foundation, either version 3 of the License, | ||
8 | or (at your option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | Affero General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU Affero General Public License | ||
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
17 | |||
18 | SPDX-License-Identifier: AGPL3.0-or-later | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file reclaim/test_did_helper.c | ||
23 | * @brief Unit tests for the helper library for DID related functions | ||
24 | * @author Tristan Schwieren | ||
25 | */ | ||
26 | |||
27 | #include "platform.h" | ||
28 | #include "gnunet_util_lib.h" | ||
29 | #include "gnunet_namestore_service.h" | ||
30 | #include "gnunet_gns_service.h" | ||
31 | #include "gnunet_gnsrecord_lib.h" | ||
32 | #include "did_helper.h" | ||
33 | #include "jansson.h" | ||
34 | |||
35 | static const char test_skey_bytes[32] = { | ||
36 | 0x9b, 0x93, 0x7b, 0x81, 0x32, 0x2d, 0x81, 0x6c, | ||
37 | 0xfa, 0xb9, 0xd5, 0xa3, 0xba, 0xac, 0xc9, 0xb2, | ||
38 | 0xa5, 0xfe, 0xbe, 0x4b, 0x14, 0x9f, 0x12, 0x6b, | ||
39 | 0x36, 0x30, 0xf9, 0x3a, 0x29, 0x52, 0x70, 0x17 | ||
40 | }; | ||
41 | |||
42 | // TODO: Create a did manual from private key / independet of implementation | ||
43 | static const char *test_did = | ||
44 | "did:gns:000G0509BYD1MPAXVSTNV0KRD1JAT0YZMPJFQNM869B66S72PSF17K4Y8G"; | ||
45 | |||
46 | static const char *test_multibase_key = | ||
47 | "u7QEJX5oaWV3edV2CeGhkrQPfpaT71ogyVmNk4rZeE8yeRA"; | ||
48 | |||
49 | static const char *test_did_document_format_str = | ||
50 | "{\"@context\":[\"https://www.w3.org/ns/did/v1\",\ | ||
51 | \"https://w3id.org/security/suites/ed25519-2020/v1\"],\ | ||
52 | \"id\":\"%s\",\ | ||
53 | \"verificationMethod\":[{\ | ||
54 | \"id\":\"%s#key-1\",\ | ||
55 | \"type\":\"Ed25519VerificationKey2020\",\ | ||
56 | \"controller\":\"%s\",\ | ||
57 | \"publicKeyMultibase\":\"%s\"}],\ | ||
58 | \"authentication\":[\"#key-1\"],\ | ||
59 | \"assertionMethod\":[\"#key-1\"]}"; | ||
60 | |||
61 | static struct GNUNET_CRYPTO_PrivateKey test_skey; | ||
62 | static struct GNUNET_CRYPTO_PublicKey test_pkey; | ||
63 | static struct json_t *test_did_document; | ||
64 | static char *test_did_document_str; | ||
65 | |||
66 | void | ||
67 | test_GNUNET_DID_pkey_to_did () | ||
68 | { | ||
69 | char *str_did; | ||
70 | str_did = DID_pkey_to_did (&test_pkey); | ||
71 | GNUNET_assert (strcmp ((char *) test_did, str_did) == 0); | ||
72 | GNUNET_free (str_did); | ||
73 | } | ||
74 | |||
75 | void | ||
76 | test_GNUNET_DID_did_to_pkey () | ||
77 | { | ||
78 | struct GNUNET_CRYPTO_PublicKey pkey; | ||
79 | DID_did_to_pkey ((char *) test_did, &pkey); | ||
80 | |||
81 | GNUNET_assert (test_pkey.type = pkey.type); | ||
82 | GNUNET_assert (memcmp (&pkey.eddsa_key, | ||
83 | &test_pkey.eddsa_key, | ||
84 | sizeof (test_pkey.eddsa_key)) == 0); | ||
85 | } | ||
86 | |||
87 | // void | ||
88 | // test_GNUNET_DID_key_convert_multibase_base64_to_gnunet (); | ||
89 | |||
90 | void | ||
91 | test_GNUNET_DID_key_convert_gnunet_to_multibase_base64 () | ||
92 | { | ||
93 | char *multibase_key; | ||
94 | multibase_key = DID_key_convert_gnunet_to_multibase_base64 (&test_pkey); | ||
95 | |||
96 | GNUNET_assert (strcmp (test_multibase_key, multibase_key) == 0); | ||
97 | GNUNET_free (multibase_key); | ||
98 | } | ||
99 | |||
100 | void | ||
101 | test_GNUNET_DID_pkey_to_did_document () | ||
102 | { | ||
103 | struct json_t *did_document; | ||
104 | char *did_document_str = DID_pkey_to_did_document (&test_pkey); | ||
105 | did_document = json_loads (did_document_str, JSON_DECODE_ANY, NULL); | ||
106 | GNUNET_assert (json_equal (test_did_document, did_document) == 1); | ||
107 | json_decref (did_document); | ||
108 | GNUNET_free (did_document_str); | ||
109 | } | ||
110 | |||
111 | int | ||
112 | main () | ||
113 | { | ||
114 | // Setup key | ||
115 | test_skey.type = htonl (GNUNET_PUBLIC_KEY_TYPE_EDDSA); | ||
116 | memcpy (&(test_skey.eddsa_key), | ||
117 | test_skey_bytes, | ||
118 | sizeof(struct GNUNET_CRYPTO_EddsaPrivateKey)); | ||
119 | GNUNET_CRYPTO_key_get_public (&test_skey, &test_pkey); | ||
120 | |||
121 | // Setup did document | ||
122 | GNUNET_asprintf (&test_did_document_str, | ||
123 | test_did_document_format_str, | ||
124 | test_did, | ||
125 | test_did, | ||
126 | test_did, | ||
127 | test_multibase_key); | ||
128 | test_did_document = json_loads (test_did_document_str, JSON_DECODE_ANY, NULL); | ||
129 | |||
130 | // Do tests | ||
131 | test_GNUNET_DID_pkey_to_did (); | ||
132 | test_GNUNET_DID_did_to_pkey (); | ||
133 | test_GNUNET_DID_pkey_to_did_document (); | ||
134 | test_GNUNET_DID_key_convert_gnunet_to_multibase_base64 (); | ||
135 | json_decref (test_did_document); | ||
136 | return 0; | ||
137 | } | ||