diff options
author | Christian Grothoff <christian@grothoff.org> | 2013-10-16 20:14:02 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2013-10-16 20:14:02 +0000 |
commit | 3d670727232e79b7e49a1df7ba9260db4e5798a0 (patch) | |
tree | 093f7b5aec28134dc6c5fe34cd2d8bfb1e1445ad /src/gnsrecord/test_gnsrecord_crypto.c | |
parent | 2de26776993a5e591e487321a5dc1d62b86d9cd4 (diff) | |
download | gnunet-3d670727232e79b7e49a1df7ba9260db4e5798a0.tar.gz gnunet-3d670727232e79b7e49a1df7ba9260db4e5798a0.zip |
-moving namestore_common functions to gnsrecord library
Diffstat (limited to 'src/gnsrecord/test_gnsrecord_crypto.c')
-rw-r--r-- | src/gnsrecord/test_gnsrecord_crypto.c | 145 |
1 files changed, 145 insertions, 0 deletions
diff --git a/src/gnsrecord/test_gnsrecord_crypto.c b/src/gnsrecord/test_gnsrecord_crypto.c new file mode 100644 index 000000000..345bf353b --- /dev/null +++ b/src/gnsrecord/test_gnsrecord_crypto.c | |||
@@ -0,0 +1,145 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2013 Christian Grothoff (and other contributing authors) | ||
4 | |||
5 | GNUnet is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published | ||
7 | by the Free Software Foundation; either version 3, or (at your | ||
8 | 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 | General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with GNUnet; see the file COPYING. If not, write to the | ||
17 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | /** | ||
21 | * @file gnsrecord/test_gnsrecord_crypto.c | ||
22 | * @brief testcase for block creation, verification and decryption | ||
23 | */ | ||
24 | #include "platform.h" | ||
25 | #include "gnunet_util_lib.h" | ||
26 | #include "gnunet_gnsrecord_lib.h" | ||
27 | |||
28 | #define RECORDS 5 | ||
29 | |||
30 | #define TEST_RECORD_TYPE 1234 | ||
31 | |||
32 | #define TEST_RECORD_DATALEN 123 | ||
33 | |||
34 | #define TEST_RECORD_DATA 'a' | ||
35 | |||
36 | #define TEST_REMOVE_RECORD_TYPE 4321 | ||
37 | |||
38 | #define TEST_REMOVE_RECORD_DATALEN 255 | ||
39 | |||
40 | #define TEST_REMOVE_RECORD_DATA 'b' | ||
41 | |||
42 | |||
43 | static struct GNUNET_CRYPTO_EcdsaPrivateKey * privkey; | ||
44 | |||
45 | static struct GNUNET_NAMESTORE_RecordData *s_rd; | ||
46 | |||
47 | static char *s_name; | ||
48 | |||
49 | static int res; | ||
50 | |||
51 | |||
52 | static struct GNUNET_NAMESTORE_RecordData * | ||
53 | create_record (int count) | ||
54 | { | ||
55 | unsigned int c; | ||
56 | struct GNUNET_NAMESTORE_RecordData * rd; | ||
57 | |||
58 | rd = GNUNET_malloc (count * sizeof (struct GNUNET_NAMESTORE_RecordData)); | ||
59 | for (c = 0; c < count; c++) | ||
60 | { | ||
61 | rd[c].expiration_time = GNUNET_TIME_absolute_get().abs_value_us; | ||
62 | rd[c].record_type = TEST_RECORD_TYPE; | ||
63 | rd[c].data_size = TEST_RECORD_DATALEN; | ||
64 | rd[c].data = GNUNET_malloc(TEST_RECORD_DATALEN); | ||
65 | memset ((char *) rd[c].data, TEST_RECORD_DATA, TEST_RECORD_DATALEN); | ||
66 | } | ||
67 | return rd; | ||
68 | } | ||
69 | |||
70 | |||
71 | static void | ||
72 | rd_decrypt_cb (void *cls, | ||
73 | unsigned int rd_count, | ||
74 | const struct GNUNET_NAMESTORE_RecordData *rd) | ||
75 | { | ||
76 | char rd_cmp_data[TEST_RECORD_DATALEN]; | ||
77 | |||
78 | int c; | ||
79 | |||
80 | GNUNET_assert (RECORDS == rd_count); | ||
81 | GNUNET_assert (NULL != rd); | ||
82 | |||
83 | memset (rd_cmp_data, 'a', TEST_RECORD_DATALEN); | ||
84 | |||
85 | for (c = 0; c < rd_count; c++) | ||
86 | { | ||
87 | GNUNET_assert (TEST_RECORD_TYPE == rd[c].record_type); | ||
88 | GNUNET_assert (TEST_RECORD_DATALEN == rd[c].data_size); | ||
89 | GNUNET_assert (0 == memcmp (&rd_cmp_data, rd[c].data, TEST_RECORD_DATALEN)); | ||
90 | } | ||
91 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
92 | "Block was decrypted successfully \n"); | ||
93 | res = 0; | ||
94 | |||
95 | } | ||
96 | |||
97 | static void | ||
98 | run (void *cls, char *const *args, const char *cfgfile, | ||
99 | const struct GNUNET_CONFIGURATION_Handle *cfg) | ||
100 | { | ||
101 | struct GNUNET_NAMESTORE_Block *block; | ||
102 | struct GNUNET_CRYPTO_EcdsaPublicKey pubkey; | ||
103 | |||
104 | /* load privat key */ | ||
105 | char *hostkey_file; | ||
106 | GNUNET_asprintf(&hostkey_file,"zonefiles%s%s",DIR_SEPARATOR_STR, | ||
107 | "N0UJMP015AFUNR2BTNM3FKPBLG38913BL8IDMCO2H0A1LIB81960.zkey"); | ||
108 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file); | ||
109 | privkey = GNUNET_CRYPTO_ecdsa_key_create_from_file(hostkey_file); | ||
110 | GNUNET_free (hostkey_file); | ||
111 | GNUNET_assert (privkey != NULL); | ||
112 | struct GNUNET_TIME_Absolute expire = GNUNET_TIME_absolute_get(); | ||
113 | /* get public key */ | ||
114 | GNUNET_CRYPTO_ecdsa_key_get_public(privkey, &pubkey); | ||
115 | |||
116 | /* create record */ | ||
117 | s_name = "DUMMY.dummy.gnunet"; | ||
118 | s_rd = create_record (RECORDS); | ||
119 | |||
120 | /* Create block */ | ||
121 | GNUNET_assert (NULL != (block = GNUNET_NAMESTORE_block_create (privkey, expire,s_name, s_rd, RECORDS))); | ||
122 | GNUNET_assert (GNUNET_OK == GNUNET_NAMESTORE_block_verify (block)); | ||
123 | GNUNET_assert (GNUNET_OK == GNUNET_NAMESTORE_block_decrypt (block, &pubkey, s_name, &rd_decrypt_cb, s_name)); | ||
124 | |||
125 | GNUNET_free (block); | ||
126 | } | ||
127 | |||
128 | |||
129 | int | ||
130 | main (int argc, char *argv[]) | ||
131 | { | ||
132 | static char *const argvx[] = { "test-gnsrecord-crypto", | ||
133 | NULL | ||
134 | }; | ||
135 | static struct GNUNET_GETOPT_CommandLineOption options[] = { | ||
136 | GNUNET_GETOPT_OPTION_END | ||
137 | }; | ||
138 | |||
139 | res = 1; | ||
140 | GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1, argvx, "test-namestore-api", | ||
141 | "nohelp", options, &run, &res); | ||
142 | return res; | ||
143 | } | ||
144 | |||
145 | /* end of test_gnsrecord_crypto.c */ | ||