diff options
author | Christophe Genevey Metat <genevey.christophe@gmail.com> | 2016-06-07 13:50:08 +0000 |
---|---|---|
committer | Christophe Genevey Metat <genevey.christophe@gmail.com> | 2016-06-07 13:50:08 +0000 |
commit | f12ac33d46fad89af94831f16dcdebd436a851da (patch) | |
tree | 0e79ed94a2348be62f8c683421251d69bcc159f3 /src/my/test_my.c | |
parent | ec817d5981e88f06f9f153cd423d13860fba4b57 (diff) | |
download | gnunet-f12ac33d46fad89af94831f16dcdebd436a851da.tar.gz gnunet-f12ac33d46fad89af94831f16dcdebd436a851da.zip |
written function cleanup
Diffstat (limited to 'src/my/test_my.c')
-rw-r--r-- | src/my/test_my.c | 395 |
1 files changed, 162 insertions, 233 deletions
diff --git a/src/my/test_my.c b/src/my/test_my.c index f0766ac2c..f7e6ae68a 100644 --- a/src/my/test_my.c +++ b/src/my/test_my.c | |||
@@ -38,44 +38,44 @@ static int | |||
38 | run_queries (struct GNUNET_MYSQL_Context *context) | 38 | run_queries (struct GNUNET_MYSQL_Context *context) |
39 | { | 39 | { |
40 | struct GNUNET_CRYPTO_RsaPublicKey *pub; | 40 | struct GNUNET_CRYPTO_RsaPublicKey *pub; |
41 | struct GNUNET_CRYPTO_RsaPublicKey *pub2 = NULL; | 41 | struct GNUNET_CRYPTO_RsaPublicKey *pub2 = NULL; |
42 | struct GNUNET_CRYPTO_RsaSignature *sig; | 42 | struct GNUNET_CRYPTO_RsaSignature *sig; |
43 | struct GNUNET_CRYPTO_RsaSignature *sig2 = NULL; | 43 | struct GNUNET_CRYPTO_RsaSignature *sig2 = NULL; |
44 | struct GNUNET_TIME_Absolute abs_time = GNUNET_TIME_absolute_get (); | 44 | struct GNUNET_TIME_Absolute abs_time = GNUNET_TIME_absolute_get (); |
45 | struct GNUNET_TIME_Absolute abs_time2; | 45 | struct GNUNET_TIME_Absolute abs_time2; |
46 | struct GNUNET_TIME_Absolute forever = GNUNET_TIME_UNIT_FOREVER_ABS; | 46 | struct GNUNET_TIME_Absolute forever = GNUNET_TIME_UNIT_FOREVER_ABS; |
47 | struct GNUNET_TIME_Absolute forever2; | 47 | struct GNUNET_TIME_Absolute forever2; |
48 | struct GNUNET_HashCode hc; | 48 | struct GNUNET_HashCode hc; |
49 | struct GNUNET_HashCode hc2; | 49 | struct GNUNET_HashCode hc2; |
50 | const char msg[] = "hello"; | 50 | const char msg[] = "hello"; |
51 | void *msg2; | 51 | void *msg2; |
52 | size_t msg2_len; | 52 | size_t msg2_len; |
53 | 53 | ||
54 | uint16_t u16; | 54 | uint16_t u16; |
55 | uint16_t u162; | 55 | uint16_t u162; |
56 | uint32_t u32; | 56 | uint32_t u32; |
57 | uint32_t u322; | 57 | uint32_t u322; |
58 | uint64_t u64; | 58 | uint64_t u64; |
59 | uint64_t u642; | 59 | uint64_t u642; |
60 | 60 | ||
61 | int ret; | 61 | int ret; |
62 | 62 | ||
63 | struct GNUNET_MYSQL_StatementHandle *statements_handle_insert; | 63 | struct GNUNET_MYSQL_StatementHandle *statements_handle_insert; |
64 | struct GNUNET_MYSQL_StatementHandle *statements_handle_select; | 64 | struct GNUNET_MYSQL_StatementHandle *statements_handle_select; |
65 | 65 | ||
66 | struct GNUNET_CRYPTO_RsaPrivateKey *priv; | 66 | struct GNUNET_CRYPTO_RsaPrivateKey *priv; |
67 | struct GNUNET_HashCode hmsg; | 67 | struct GNUNET_HashCode hmsg; |
68 | 68 | ||
69 | priv = GNUNET_CRYPTO_rsa_private_key_create (1024); | 69 | priv = GNUNET_CRYPTO_rsa_private_key_create (1024); |
70 | pub = GNUNET_CRYPTO_rsa_private_key_get_public (priv); | 70 | pub = GNUNET_CRYPTO_rsa_private_key_get_public (priv); |
71 | memset (&hmsg, 42, sizeof(hmsg)); | 71 | memset (&hmsg, 42, sizeof(hmsg)); |
72 | sig = GNUNET_CRYPTO_rsa_sign_fdh (priv, | 72 | sig = GNUNET_CRYPTO_rsa_sign_fdh (priv, |
73 | &hmsg); | 73 | &hmsg); |
74 | u16 = 16; | 74 | u16 = 16; |
75 | u32 = 32; | 75 | u32 = 32; |
76 | u64 = 64; | 76 | u64 = 64; |
77 | 77 | ||
78 | statements_handle_insert = GNUNET_MYSQL_statement_prepare (context, | 78 | statements_handle_insert = GNUNET_MYSQL_statement_prepare (context, |
79 | "INSERT INTO test_my2 (" | 79 | "INSERT INTO test_my2 (" |
80 | " pub" | 80 | " pub" |
81 | ",sig" | 81 | ",sig" |
@@ -89,190 +89,146 @@ run_queries (struct GNUNET_MYSQL_Context *context) | |||
89 | ") VALUES " | 89 | ") VALUES " |
90 | "( ?, ?, ?, ?, ?, ?, ?, ?, ?)"); | 90 | "( ?, ?, ?, ?, ?, ?, ?, ?, ?)"); |
91 | 91 | ||
92 | if (NULL == statements_handle_insert) | 92 | if (NULL == statements_handle_insert) |
93 | { | 93 | { |
94 | fprintf (stderr, "Failed to prepared statement INSERT\n"); | 94 | fprintf (stderr, "Failed to prepared statement INSERT\n"); |
95 | return 1; | 95 | return 1; |
96 | } | 96 | } |
97 | 97 | ||
98 | struct GNUNET_MY_QueryParam params_insert[] = { | 98 | struct GNUNET_MY_QueryParam params_insert[] = { |
99 | GNUNET_MY_query_param_rsa_public_key (pub), | 99 | GNUNET_MY_query_param_rsa_public_key (pub), |
100 | GNUNET_MY_query_param_rsa_signature (sig), | 100 | GNUNET_MY_query_param_rsa_signature (sig), |
101 | GNUNET_MY_query_param_absolute_time (&abs_time), | 101 | GNUNET_MY_query_param_absolute_time (&abs_time), |
102 | GNUNET_MY_query_param_absolute_time (&forever), | 102 | GNUNET_MY_query_param_absolute_time (&forever), |
103 | GNUNET_MY_query_param_auto_from_type (&hc), | 103 | GNUNET_MY_query_param_auto_from_type (&hc), |
104 | GNUNET_MY_query_param_fixed_size (msg, strlen (msg)), | 104 | GNUNET_MY_query_param_fixed_size (msg, strlen (msg)), |
105 | GNUNET_MY_query_param_uint16 (&u16), | 105 | GNUNET_MY_query_param_uint16 (&u16), |
106 | GNUNET_MY_query_param_uint32 (&u32), | 106 | GNUNET_MY_query_param_uint32 (&u32), |
107 | GNUNET_MY_query_param_uint64 (&u64), | 107 | GNUNET_MY_query_param_uint64 (&u64), |
108 | GNUNET_MY_query_param_end | 108 | GNUNET_MY_query_param_end |
109 | }; | 109 | }; |
110 | 110 | ||
111 | /* statements_handle_insert = GNUNET_MYSQL_statement_prepare (context, | 111 | if (GNUNET_OK != GNUNET_MY_exec_prepared(context, |
112 | "INSERT INTO test_my2 (" | 112 | statements_handle_insert, |
113 | " abs_time" | 113 | params_insert)) |
114 | ",forever" | 114 | { |
115 | ",hash" | 115 | fprintf (stderr, "Failed to execute prepared statement INSERT\n"); |
116 | ",u16" | 116 | return 1; |
117 | ",u32" | 117 | } |
118 | ",u64" | 118 | |
119 | ") VALUES " | 119 | statements_handle_select = GNUNET_MYSQL_statement_prepare (context, |
120 | "( ?, ?, ?, ?, ?, ?)"); | 120 | "SELECT" |
121 | 121 | " pub" | |
122 | struct GNUNET_MY_QueryParam params_insert[] = { | 122 | ",sig" |
123 | GNUNET_MY_query_param_absolute_time (&abs_time), | 123 | ",abs_time" |
124 | GNUNET_MY_query_param_absolute_time (&forever), | 124 | ",forever" |
125 | GNUNET_MY_query_param_auto_from_type (&hc), | 125 | ",hash" |
126 | GNUNET_MY_query_param_uint16 (&u16), | 126 | ",vsize" |
127 | GNUNET_MY_query_param_uint32 (&u32), | 127 | ",u16" |
128 | GNUNET_MY_query_param_uint64 (&u64), | 128 | ",u32" |
129 | GNUNET_MY_query_param_end | 129 | ",u64" |
130 | }; | 130 | " FROM test_my2"); |
131 | */ | 131 | |
132 | if (GNUNET_OK != GNUNET_MY_exec_prepared(context, | 132 | if (NULL == statements_handle_select) |
133 | statements_handle_insert, | 133 | { |
134 | params_insert)) | 134 | fprintf(stderr, "Failed to prepared statement SELECT\n"); |
135 | { | 135 | return 1; |
136 | fprintf (stderr, "Failed to execute prepared statement INSERT\n"); | 136 | } |
137 | return 1; | 137 | |
138 | } | 138 | struct GNUNET_MY_QueryParam params_select[] = { |
139 | 139 | GNUNET_MY_query_param_end | |
140 | 140 | }; | |
141 | 141 | ||
142 | /* statements_handle_select = GNUNET_MYSQL_statement_prepare (context, | 142 | if (GNUNET_OK != GNUNET_MY_exec_prepared (context, |
143 | "SELECT" | 143 | statements_handle_select, |
144 | " pub" | 144 | params_select)) |
145 | ",sig" | 145 | { |
146 | ",abs_time" | ||
147 | ",forever" | ||
148 | ",hash" | ||
149 | ",vsize" | ||
150 | ",u16" | ||
151 | ",u32" | ||
152 | ",u64" | ||
153 | " FROM test_my" | ||
154 | " ORDER BY abs_time DESC " | ||
155 | " LIMIT 1;"); | ||
156 | |||
157 | */ | ||
158 | statements_handle_select = GNUNET_MYSQL_statement_prepare (context, | ||
159 | "SELECT" | ||
160 | " pub" | ||
161 | ",sig" | ||
162 | ",abs_time" | ||
163 | ",forever" | ||
164 | ",hash" | ||
165 | ",vsize" | ||
166 | ",u16" | ||
167 | ",u32" | ||
168 | ",u64" | ||
169 | " FROM test_my2"); | ||
170 | |||
171 | if (NULL == statements_handle_select) | ||
172 | { | ||
173 | fprintf(stderr, "Failed to prepared statement SELECT\n"); | ||
174 | return 1; | ||
175 | } | ||
176 | |||
177 | struct GNUNET_MY_QueryParam params_select[] = { | ||
178 | GNUNET_MY_query_param_end | ||
179 | }; | ||
180 | |||
181 | if (GNUNET_OK != GNUNET_MY_exec_prepared (context, | ||
182 | statements_handle_select, | ||
183 | params_select)) | ||
184 | { | ||
185 | fprintf (stderr, "Failed to execute prepared statement SELECT\n"); | 146 | fprintf (stderr, "Failed to execute prepared statement SELECT\n"); |
186 | return 1; | 147 | return 1; |
187 | } | 148 | } |
188 | 149 | ||
189 | /* | 150 | struct GNUNET_MY_ResultSpec results_select[] = { |
190 | struct GNUNET_MY_ResultSpec results_select[] = { | 151 | GNUNET_MY_result_spec_rsa_public_key (&pub2), |
191 | GNUNET_MY_result_spec_rsa_public_key (&pub2), | 152 | GNUNET_MY_result_spec_rsa_signature (&sig2), |
192 | GNUNET_MY_result_spec_rsa_signature (&sig2), | 153 | GNUNET_MY_result_spec_absolute_time (&abs_time2), |
193 | GNUNET_MY_result_spec_absolute_time (&abs_time2), | 154 | GNUNET_MY_result_spec_absolute_time (&forever2), |
194 | GNUNET_MY_result_spec_absolute_time (&forever2), | 155 | GNUNET_MY_result_spec_auto_from_type (&hc2), |
195 | GNUNET_MY_result_spec_auto_from_type (&hc2), | 156 | GNUNET_MY_result_spec_variable_size (&msg2, &msg2_len), |
196 | GNUNET_MY_result_spec_variable_size (&msg2, &msg2_len), | 157 | GNUNET_MY_result_spec_uint16 (&u162), |
197 | GNUNET_MY_result_spec_uint16 (&u162), | 158 | GNUNET_MY_result_spec_uint32 (&u322), |
198 | GNUNET_MY_result_spec_uint32 (&u322), | 159 | GNUNET_MY_result_spec_uint64 (&u642), |
199 | GNUNET_MY_result_spec_uint64 (&u642), | 160 | GNUNET_MY_result_spec_end |
200 | GNUNET_MY_result_spec_end | 161 | }; |
201 | }; | 162 | |
202 | */ | 163 | ret = GNUNET_MY_extract_result (statements_handle_select, |
203 | struct GNUNET_MY_ResultSpec results_select[] = { | 164 | results_select); |
204 | GNUNET_MY_result_spec_rsa_public_key (&pub2), | 165 | |
205 | GNUNET_MY_result_spec_rsa_signature (&sig2), | 166 | GNUNET_break (GNUNET_YES == ret); |
206 | GNUNET_MY_result_spec_absolute_time (&abs_time2), | 167 | GNUNET_break (abs_time.abs_value_us == abs_time2.abs_value_us); |
207 | GNUNET_MY_result_spec_absolute_time (&forever2), | 168 | GNUNET_break (forever.abs_value_us == forever2.abs_value_us); |
208 | GNUNET_MY_result_spec_auto_from_type (&hc2), | 169 | GNUNET_break (0 == |
209 | GNUNET_MY_result_spec_variable_size (&msg2, &msg2_len), | 170 | memcmp (&hc, |
210 | GNUNET_MY_result_spec_uint16 (&u162), | ||
211 | GNUNET_MY_result_spec_uint32 (&u322), | ||
212 | GNUNET_MY_result_spec_uint64 (&u642), | ||
213 | GNUNET_MY_result_spec_end | ||
214 | }; | ||
215 | |||
216 | ret = GNUNET_MY_extract_result (statements_handle_select, | ||
217 | results_select); | ||
218 | |||
219 | GNUNET_break (GNUNET_YES == ret); | ||
220 | GNUNET_break (abs_time.abs_value_us == abs_time2.abs_value_us); | ||
221 | GNUNET_break (forever.abs_value_us == forever2.abs_value_us); | ||
222 | GNUNET_break (0 == | ||
223 | memcmp (&hc, | ||
224 | &hc2, | 171 | &hc2, |
225 | sizeof (struct GNUNET_HashCode))); | 172 | sizeof (struct GNUNET_HashCode))); |
226 | 173 | ||
227 | GNUNET_break (strlen (msg) == msg2_len); | 174 | GNUNET_break (0 == |
228 | GNUNET_break (0 == | 175 | GNUNET_CRYPTO_rsa_signature_cmp (sig, |
176 | sig2)); | ||
177 | GNUNET_break (0 == | ||
178 | GNUNET_CRYPTO_rsa_public_key_cmp (pub, | ||
179 | pub2)); | ||
180 | |||
181 | GNUNET_break (strlen (msg) == msg2_len); | ||
182 | GNUNET_break (0 == | ||
229 | strncmp (msg, | 183 | strncmp (msg, |
230 | msg2, | 184 | msg2, |
231 | msg2_len)); | 185 | msg2_len)); |
232 | fprintf(stderr, "msg2 : %d\n", strlen(msg2)); | 186 | GNUNET_break (16 == u162); |
233 | GNUNET_break (16 == u162); | 187 | GNUNET_break (32 == u322); |
234 | GNUNET_break (32 == u322); | 188 | GNUNET_break (64 == u642); |
235 | GNUNET_break (64 == u642); | 189 | |
236 | 190 | GNUNET_MY_cleanup_result (results_select); | |
237 | if (GNUNET_OK != ret) | ||
238 | { | ||
239 | fprintf(stderr, "Failed to extract result\n"); | ||
240 | return 1; | ||
241 | } | ||
242 | 191 | ||
243 | return 0; | 192 | GNUNET_CRYPTO_rsa_signature_free (sig); |
193 | GNUNET_CRYPTO_rsa_private_key_free (priv); | ||
194 | GNUNET_CRYPTO_rsa_public_key_free (pub); | ||
195 | |||
196 | if (GNUNET_OK != ret) | ||
197 | return 1; | ||
198 | |||
199 | return 0; | ||
244 | } | 200 | } |
245 | 201 | ||
246 | 202 | ||
247 | int | 203 | int |
248 | main (int argc, const char * const argv[]) | 204 | main (int argc, const char * const argv[]) |
249 | { | 205 | { |
250 | struct GNUNET_CONFIGURATION_Handle *config; | 206 | struct GNUNET_CONFIGURATION_Handle *config; |
251 | struct GNUNET_MYSQL_Context *context; | 207 | struct GNUNET_MYSQL_Context *context; |
252 | int ret; | 208 | int ret; |
253 | 209 | ||
254 | GNUNET_log_setup ( "test-my", | 210 | GNUNET_log_setup ( "test-my", |
255 | "WARNING", | 211 | "WARNING", |
256 | NULL); | 212 | NULL); |
257 | 213 | ||
258 | config = GNUNET_CONFIGURATION_create (); | 214 | config = GNUNET_CONFIGURATION_create (); |
259 | if (GNUNET_OK != GNUNET_CONFIGURATION_parse (config, "test_my.conf")) | 215 | if (GNUNET_OK != GNUNET_CONFIGURATION_parse (config, "test_my.conf")) |
260 | { | 216 | { |
261 | fprintf (stderr, "Failed to parse configuaration\n"); | 217 | fprintf (stderr, "Failed to parse configuaration\n"); |
262 | return 1; | 218 | return 1; |
263 | } | 219 | } |
264 | 220 | ||
265 | context = GNUNET_MYSQL_context_create (config, | 221 | context = GNUNET_MYSQL_context_create (config, |
266 | "datastore-mysql"); | 222 | "datastore-mysql"); |
267 | if (NULL == context) | 223 | if (NULL == context) |
268 | { | 224 | { |
269 | fprintf(stderr, "Failed to connect to database\n"); | 225 | fprintf(stderr, "Failed to connect to database\n"); |
270 | return 77; | 226 | return 77; |
271 | } | 227 | } |
272 | 228 | ||
273 | (void) GNUNET_MYSQL_statement_run (context, | 229 | (void) GNUNET_MYSQL_statement_run (context, |
274 | "DROP TABLE test_my2;"); | 230 | "DROP TABLE test_my2;"); |
275 | if (GNUNET_OK != GNUNET_MYSQL_statement_run (context, | 231 | if (GNUNET_OK != GNUNET_MYSQL_statement_run (context, |
276 | "CREATE TABLE IF NOT EXISTS test_my2(" | 232 | "CREATE TABLE IF NOT EXISTS test_my2(" |
277 | " pub BLOB NOT NULL" | 233 | " pub BLOB NOT NULL" |
278 | ",sig BLOB NOT NULL" | 234 | ",sig BLOB NOT NULL" |
@@ -284,43 +240,16 @@ main (int argc, const char * const argv[]) | |||
284 | ",u32 INT NOT NULL" | 240 | ",u32 INT NOT NULL" |
285 | ",u64 BIGINT NOT NULL" | 241 | ",u64 BIGINT NOT NULL" |
286 | ")")) | 242 | ")")) |
287 | { | 243 | { |
288 | fprintf (stderr, | 244 | fprintf (stderr, |
289 | "Failed to create table \n"); | 245 | "Failed to create table \n"); |
290 | GNUNET_MYSQL_statements_invalidate (context); | 246 | GNUNET_MYSQL_statements_invalidate (context); |
291 | GNUNET_MYSQL_context_destroy (context); | 247 | GNUNET_MYSQL_context_destroy (context); |
292 | |||
293 | return 1; | ||
294 | } | ||
295 | |||
296 | /* if (GNUNET_OK != GNUNET_MYSQL_statement_run (context, | ||
297 | "CREATE TABLE test_my2(" | ||
298 | " abs_time BIGINT NOT NULL" | ||
299 | ", forever BIGINT NOT NULL" | ||
300 | ", hash VARCHAR(32) NOT NULL CHECK(LENGTH(hash)=64)" | ||
301 | ", u16 SMALLINT NOT NULL" | ||
302 | ", u32 INT NOT NULL" | ||
303 | ", u64 BIGINT NOT NULL" | ||
304 | ")")) | ||
305 | { | ||
306 | fprintf (stderr, | ||
307 | "Failed to create table \n"); | ||
308 | GNUNET_MYSQL_statements_invalidate (context); | ||
309 | GNUNET_MYSQL_context_destroy (context); | ||
310 | 248 | ||
311 | return 1; | 249 | return 1; |
312 | } | 250 | } |
313 | */ | ||
314 | ret = run_queries (context); | ||
315 | 251 | ||
316 | /* if(GNUNET_OK != GNUNET_MYSQL_statement_run (context, | 252 | ret = run_queries (context); |
317 | "DROP TABLE test_my2")) | ||
318 | { | ||
319 | fprintf (stderr, "Failed to drop table test_my\n"); | ||
320 | GNUNET_MYSQL_statements_invalidate (context); | ||
321 | } | ||
322 | 253 | ||
323 | GNUNET_MYSQL_context_destroy (context); | 254 | return ret; |
324 | */ | ||
325 | return ret; | ||
326 | } | 255 | } |