Debian Codegraph @

test/UT/US5418/us5418h.c

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 /*------------------------------------------------------------------
2 * us5418h.c - Unit Tests for User Story 5418 - Performance Timers
3 *
4 * September, 2019
5 *
6 * Copyright (c) 2019 by cisco Systems, Inc.
7 * All rights reserved.
8 *------------------------------------------------------------------
9 */
10 #include "us5418.h"
11 #include <pthread.h>
12
13 static unsigned char *cacerts = NULL;
14 static int cacerts_len = 0;
15
16 static char *cssl_emulator_path = NULL;
17
18 extern EST_CTX *ectx;
19 static char temp_dir[MAX_FILENAME_LEN + 1];
20 static FILE *fh;
21 static EVP_PKEY *client_pkey = NULL;
22 static X509 *client_cert = NULL;
23 static unsigned char *ca_chain_bytes;
24 static int ca_chain_bytes_len = 0;
25 pthread_mutex_t lock;
26 static pid_t pid;
27
28 /* Auth fails on first req */
29 #define US5418H_SIMPLE_ENROLL_LINE_COUNT HEADER_LINE_COUNT + 14
30 #define US5418H_GET_CACERTS_LINE_COUNT HEADER_LINE_COUNT + 3
31 #define US5418H_CSR_ATTRS_LINE_COUNT HEADER_LINE_COUNT + 3
32 #define US5418H_SERVER_KEYGEN_LINE_COUNT HEADER_LINE_COUNT + 15
33 #define US5418H_SIMPLE_REENROLL_LINE_COUNT HEADER_LINE_COUNT + 14
34
35 typedef EST_ERROR (*setup_cb_t) (EST_CTX **ctx);
36
37 static void us5418h_clean (void) {}
38
39 static int us5418h_start_server (int manual_enroll, int nid)
40 {
41 int rv;
42 if (pthread_mutex_init(&lock, NULL) != 0)
43 {
44 printf("\n mutex init has failed\n");
45 return 1;
46 }
47 rv = st_start(US5418_SERVER_PORT, US5418_SERVER_CERT_AND_KEY,
48 US5418_SERVER_CERT_AND_KEY, "US5418 test realm",
49 US5418_CACERTS, US5418_TRUSTED_CERT, US5418_OPENSSL_CNF,
50 manual_enroll, 0, nid);
51 if (rv != EST_ERR_NONE) {
52 printf("Failed to start CoAP st_server\n");
53 }
54
55 st_set_default_est_event_callbacks();
56
57 return rv;
58 }
59
60 /*
61 * This routine is called when CUnit initializes this test
62 * suite. This can be used to allocate data or open any
63 * resources required for all the test cases.
64 */
65 static int us5418h_init_suite (void)
66 {
67 int rv;
68
69 est_init_logger(EST_LOG_LVL_INFO, NULL);
70
71 /*
72 * Read in the CA certificates
73 */
74 cacerts_len = read_binary_file(US5418_CACERTS, &cacerts);
75 if (cacerts_len <= 0) {
76 return 1;
77 }
78
79 us5418h_clean();
80
81 /*
82 * Start an instance of the EST server with
83 * automatic enrollment enabled.
84 */
85 rv = us5418h_start_server(0, 0);
86 if (rv) {
87 printf("The st server for US5418 could not be started.\n");
88 return rv;
89 }
90 /* Build out temp directory */
91 #ifdef WIN32
92 snprintf(temp_dir, MAX_FILENAME_LEN, "%s\\", getenv("TEMP"));
93 #else
94 snprintf(temp_dir, MAX_FILENAME_LEN, "/tmp/");
95 #endif
96 /* Need this since python now depends upon ciscossl */
97 cssl_emulator_path = getenv("COAP_EMU_SSL");
98
99 read_x509_cert_and_key_file(US5418_CLIENT_CERTKEY, US5418_CLIENT_CERTKEY,
100 &client_cert, &client_pkey);
101
102 ca_chain_bytes_len = read_binary_file(US5418_CACERTS, &ca_chain_bytes);
103 if (ca_chain_bytes_len <= 0) {
104 printf("The ca chain file could not be read or had zero len.\n");
105 return 1;
106 }
107 pid = getpid();
108
109 return rv;
110 }
111
112 /*
113 * This routine is called when CUnit uninitializes this test
114 * suite. This can be used to deallocate data or close any
115 * resources that were used for the test cases.
116 */
117 static int us5418h_destroy_suite (void)
118 {
119 st_stop();
120 if (client_pkey) {
121 EVP_PKEY_free(client_pkey);
122 }
123 if (ca_chain_bytes) {
124 free(ca_chain_bytes);
125 }
126 if (client_cert) {
127 X509_free(client_cert);
128 }
129 if (cacerts) {
130 free(cacerts);
131 }
132 printf("Finishing up. Destroying Suite US5418\n");
133 return 0;
134 }
135
136 /*
137 * This is a simple callback used to override the default
138 * logging facility in libest.
139 * Used to test the logging API's.
140 *
141 * These are the log levels defined in libest/src/est.h
142 * EST_LOG_LVL_ERR = 1,
143 * EST_LOG_LVL_WARN,
144 * EST_LOG_LVL_INFO
145 */
146 static void us5418h_logger_file (char *format, va_list l)
147 {
148 pthread_mutex_lock(&lock);
149 vfprintf(fh, format, l);
150 fflush(fh);
151 pthread_mutex_unlock(&lock);
152 }
153
154 /*
155 * Test Timer Enable/Disable API
156 * Test the enable and disable perf timers API functions
157 * Tests:
158 * - Enable with a NULL ctx (should fail)
159 * - Enable with st_server ctx (should pass)
160 * - Enable with st_server ctx again (should pass)
161 * - Disable with st_server ctx (should pass)
162 * - Disable with st_server ctx again (should pass)
163 * - Disable with a NULL ctx (should fail)
164 */
165 static void us5418h_test1 (void)
166 {
167 PRINT_START;
168 EST_ERROR est_err = EST_ERR_UNKNOWN;
169 est_err = est_enable_performance_timers(NULL);
170 CU_ASSERT_5418(est_err == EST_ERR_NO_CTX);
171 est_err = est_enable_performance_timers(ectx);
172 CU_ASSERT_5418(est_err == EST_ERR_NONE);
173 est_err = est_enable_performance_timers(ectx);
174 CU_ASSERT_5418(est_err == EST_ERR_NONE);
175 est_err = est_disable_performance_timers(ectx);
176 CU_ASSERT_5418(est_err == EST_ERR_NONE);
177 est_err = est_disable_performance_timers(ectx);
178 CU_ASSERT_5418(est_err == EST_ERR_NONE);
179 est_err = est_disable_performance_timers(NULL);
180 CU_ASSERT_5418(est_err == EST_ERR_NO_CTX);
181 PRINT_END;
182 }
183
184 EST_ERROR setup_client_ctx(EST_CTX **cctx) {
185 EST_ERROR est_err;
186 *cctx = est_client_init(ca_chain_bytes, ca_chain_bytes_len,
187 EST_CERT_FORMAT_PEM, NULL);
188 if (!(*cctx)) {
189 /* No success variable in here so can't use CU_FAIL_5418 */
190 printf("The client context was not properly initialized.\n");
191 CU_FAIL("The client context was not properly initialized.");
192 return EST_ERR_NO_CTX;
193 }
194 est_err = est_client_set_server(*cctx, US5418_SERVER_IP, US5418_SERVER_PORT, NULL);
195 if (est_err != EST_ERR_NONE) {
•196 printf("EST_ERROR %s occurred while setting the server in the est client context\n",
197 EST_ERR_NUM_TO_STR(est_err));
•198 CU_FAIL("EST error occurred while setting the server in the est client context");
199 return est_err;
200 }
201 est_err = est_client_set_auth(*cctx, "estuser", "estpwd", client_cert, client_pkey);
202 if (est_err != EST_ERR_NONE) {
•203 printf("EST_ERROR %s occurred while setting the auth in the est client context\n",
204 EST_ERR_NUM_TO_STR(est_err));
•205 CU_FAIL("EST error occurred while setting the auth in the est client context");
206 return est_err;
207 }
208 return EST_ERR_NONE;
209 }
210
211 static int get_num_entries (REQUESTS request_type)
212 {
213 switch (request_type) {
214 case SIMPLE_ENROLL:
215 return US5418H_SIMPLE_ENROLL_LINE_COUNT;
216 case GET_CACERTS:
217 return US5418H_GET_CACERTS_LINE_COUNT;
218 case CSR_ATTRS:
219 return US5418H_CSR_ATTRS_LINE_COUNT;
220 case SERVER_KEYGEN:
221 return US5418H_SERVER_KEYGEN_LINE_COUNT;
222 case SIMPLE_REENROLL:
223 return US5418H_SIMPLE_REENROLL_LINE_COUNT;
224 default:
225 return -1;
226 }
227 }
228
229 static EST_ERROR perform_est_request (REQUESTS request_type, EVP_PKEY *pkey,
230 X509 *reenroll_cert, setup_cb_t setup_cb)
231 {
232 EST_CTX *cctx = NULL;
233 EST_ERROR est_err;
234 int pkcs7_len = 0;
235 int cacerts_len = 0;
236 unsigned char *csr_attrs_buf;
237 int csr_attrs_len = 0;
238 int newkey_len = 0;
239
240 est_err = setup_cb(&cctx);
241 if (est_err != EST_ERR_NONE) {
242 /* No success variable in here so can't use CU_FAIL_5418 */
•243 printf("EST_ERROR %s occurred during setup of client context\n",
244 EST_ERR_NUM_TO_STR(est_err));
•245 CU_FAIL("EST error occurred during setup of client context");
246 if (cctx) {
247 est_destroy(cctx);
248 }
249 return est_err;
250 }
251 switch (request_type) {
252 case SIMPLE_ENROLL:
253 est_err = est_client_enroll(cctx, "testcert", &pkcs7_len, pkey);
254 if (est_err != EST_ERR_NONE) {
•255 printf("EST_ERROR %s occurred during enrollment request\n",
256 EST_ERR_NUM_TO_STR(est_err));
•257 CU_FAIL("EST error occurred during enrollment request");
258 }
259 break;
260 case GET_CACERTS:
261 est_err = est_client_get_cacerts(cctx, &cacerts_len);
262 if (est_err != EST_ERR_NONE) {
•263 printf("EST_ERROR %s occurred during get cacerts request\n",
264 EST_ERR_NUM_TO_STR(est_err));
•265 CU_FAIL("EST error occurred during get cacerts request");
266 }
267 break;
268 case CSR_ATTRS:
269 est_err = est_client_get_csrattrs(cctx, &csr_attrs_buf, &csr_attrs_len);
270 if (est_err != EST_ERR_NONE) {
•271 printf("EST_ERROR %s occurred during get csr attrs request\n",
272 EST_ERR_NUM_TO_STR(est_err));
•273 CU_FAIL("EST error occurred during get csr attrs request");
274 }
275 break;
276 case SERVER_KEYGEN:
277 est_err = est_client_server_keygen_enroll(cctx, "testcert", &pkcs7_len,
278 &newkey_len, pkey);
279 if (est_err != EST_ERR_NONE) {
•280 printf("EST_ERROR %s occurred during get server keygen request\n",
281 EST_ERR_NUM_TO_STR(est_err));
•282 CU_FAIL("EST error occurred during get server keygen request");
283 }
284 break;
285 case SIMPLE_REENROLL:
286 est_err = est_client_reenroll(cctx, reenroll_cert, &pkcs7_len, pkey);
287 if (est_err != EST_ERR_NONE) {
•288 printf("EST_ERROR %s occurred during reenroll request\n",
289 EST_ERR_NUM_TO_STR(est_err));
•290 CU_FAIL("EST error occurred during reenroll request");
291 }
292 break;
293 default:
•294 printf("EST_ERROR %s occurred during get csr attrs request\n",
295 EST_ERR_NUM_TO_STR(est_err));
•296 CU_FAIL("EST error occurred during get csr attrs request");
297 est_err = EST_ERR_INVALID_PARAMETERS;
298 }
299 if (cctx) {
300 est_destroy(cctx);
301 }
302 return est_err;
303 }
304 /*
305 * This test contains the logic for testing the logging of timers for all the
306 * different forms of requests. Simply enter in the type of request as a
307 * parameter and use the returned boolean value to determine if the test
308 * succeeded (1 success, 0 failure) for logging purposes The request type
309 * strings are the following:
310 * - SIMPLE_ENROLL
311 * - GET_CACERTS
312 * - CSR_ATTRS
313 * - SERVER_KEYGEN
314 * - SIMPLE_REENROLL
315 */
316 static char perform_timer_logging_test (REQUESTS request_type)
317 {
318 char cmd[EST_UT_MAX_CMD_LEN];
319 char est_client_logs[MAX_FILENAME_LEN];
320 char est_server_logs[MAX_FILENAME_LEN];
321 char csv_filename[MAX_FILENAME_LEN];
322 FILE *fh_csv;
323 char c;
324 int exit_code = 0;
325 int line_count = 0;
326 EST_ERROR est_err = EST_ERR_UNKNOWN;
327 char success = 1;
328
329 snprintf(est_server_logs, MAX_FILENAME_LEN, "%sus5418h_estserver_%ld.log",
330 temp_dir, (unsigned long)pid);
331 if (strnlen(est_server_logs, MAX_FILENAME_LEN) >= MAX_FILENAME_LEN) {
332 CU_FAIL_5418("The logfile for the est_coap_client.py emulator was "
333 "too long. Add symbolic links to make paths shorter.");
334 goto end;
335 }
336
337 if ((fh = fopen(est_server_logs, "w")) == NULL) {
338 CU_FAIL_5418("Could not open server log file");
339 goto end;
340 }
341
342 pthread_mutex_lock(&lock);
343 est_err = est_init_logger(EST_LOG_LVL_INFO, us5418h_logger_file);
344 pthread_mutex_unlock(&lock);
345 if (est_err != EST_ERR_NONE) {
346 CU_FAIL_5418("Couldn't init the libest logger");
347 goto end;
348 }
349
350 snprintf(est_client_logs, MAX_FILENAME_LEN, "%sus5418h_estclient_%ld.log",
351 temp_dir, (unsigned long)pid);
352 if (strnlen(est_client_logs, MAX_FILENAME_LEN) >= MAX_FILENAME_LEN) {
353 CU_FAIL_5418("The logfile for the est_coap_client.py emulator was "
354 "too long. Add symbolic links to make paths shorter.");
355 goto end;
356 }
357 est_err = est_enable_performance_timers(ectx);
358 CU_ASSERT_5418(est_err == EST_ERR_NONE);
359 /* Perform EST request */
360 est_err = perform_est_request(request_type, client_pkey, client_cert,
361 (setup_cb_t)setup_client_ctx);
362 CU_ASSERT_5418(est_err == EST_ERR_NONE);
363 est_err = est_disable_performance_timers(ectx);
364 CU_ASSERT_5418(est_err == EST_ERR_NONE);
365 /* Wait for logging lock to change the logging */
366
367 pthread_mutex_lock(&lock);
368 est_err = est_init_logger(EST_LOG_LVL_INFO, NULL);
369 pthread_mutex_unlock(&lock);
370 if (est_err != EST_ERR_NONE) {
371 CU_FAIL_5418("Couldn't reset the libest logger");
372 goto end;
373 }
374 fclose(fh);
375 snprintf(csv_filename, MAX_FILENAME_LEN, "%sus5418h_timer_%ld.csv",
376 temp_dir, (unsigned long)pid);
377 if (strnlen(csv_filename, MAX_FILENAME_LEN) >= MAX_FILENAME_LEN) {
378 CU_FAIL_5418("The path of the output csv for the est timers is "
379 "too long. Add symbolic links to make paths shorter.");
380 goto end;
381 }
382 snprintf(cmd, EST_UT_MAX_CMD_LEN, "LD_LIBRARY_PATH=%s/lib python %s %s %s",
383 cssl_emulator_path, PARSE_TIMER_PATH, est_server_logs,
384 csv_filename);
385 if (strnlen(cmd, EST_UT_MAX_CMD_LEN) >= EST_UT_MAX_CMD_LEN) {
386 CU_FAIL_5418("Commmand for generating the timing csv file is too long. "
387 "Add symbolic links to make paths shorter.");
388 goto end;
389 }
390 exit_code = system(cmd);
391 CU_ASSERT_5418(!exit_code);
392 // Open the file
393 fh_csv = fopen(csv_filename, "r");
394
395 // Check if file exists
396 if (fh_csv == NULL) {
397 printf("Could not open file %s", csv_filename);
398 CU_FAIL_5418("Could not open output csv file");
399 goto end;
400 }
401
402 // Extract characters from file and store in character c
403 for (c = getc(fh_csv); c != EOF; c = getc(fh_csv)) {
404 if (c == '\n') { // Increment count if this character is newline
405 line_count += 1;
406 }
407 }
408 // Close the file
409 fclose(fh_csv);
410 remove(est_server_logs);
411 remove(est_client_logs);
412 remove(csv_filename);
413 CU_ASSERT_5418(line_count == get_num_entries(request_type));
414 end:
415 return success;
416 }
417
418 /*
419 * Test Timer with Simple Enroll
420 * This test checks to make sure the logging and python script for generating a
421 * csv of the logged timings is working on a simple enroll request. The test
422 * consists of the following steps:
423 * - enable timer logging in st server and write logs to a file
424 * - use est client to perform request
425 * - disable timer logging, close logging file, and reset logging
426 * - launch python csv generator and ensure it has entries
427 */
428 static void us5418h_test2 (void)
429 {
430 /*
431 * success is defined in the PRINT_START define and is used for logging
432 * success or failure of the test
433 */
434 PRINT_START;
435 success = perform_timer_logging_test(SIMPLE_ENROLL);
436 PRINT_END;
437 }
438 /*
439 * Test Timer with Get CA Certs
440 * This test checks to make sure the logging and python script for generating a
441 * csv of the logged timings is working on a simple enroll request. The test
442 * consists of the following steps:
443 * - enable timer logging in st server and write logs to a file
444 * - use est client to perform request
445 * - disable timer logging, close logging file, and reset logging
446 * - launch python csv generator and ensure it has entries
447 */
448 static void us5418h_test3 (void)
449 {
450 /*
451 * success is defined in the PRINT_START define and is used for logging
452 * success or failure of the test
453 */
454 PRINT_START;
455 success = perform_timer_logging_test(GET_CACERTS);
456 PRINT_END;
457 }
458
459 /*
460 * Test Timer with Get CSR Attrs
461 * This test checks to make sure the logging and python script for generating a
462 * csv of the logged timings is working on a simple enroll request. The test
463 * consists of the following steps:
464 * - enable timer logging in st server and write logs to a file
465 * - use est client to perform request
466 * - disable timer logging, close logging file, and reset logging
467 * - launch python csv generator and ensure it has entries
468 */
469 static void us5418h_test4 (void)
470 {
471 /*
472 * success is defined in the PRINT_START define and is used for logging
473 * success or failure of the test
474 */
475 PRINT_START;
476 success = perform_timer_logging_test(CSR_ATTRS);
477 PRINT_END;
478 }
479
480 /*
481 * Test Timer with Server Keygen
482 * This test checks to make sure the logging and python script for generating a
483 * csv of the logged timings is working on a simple enroll request. The test
484 * consists of the following steps:
485 * - enable timer logging in st server and write logs to a file
486 * - use est client to perform request
487 * - disable timer logging, close logging file, and reset logging
488 * - launch python csv generator and ensure it has entries
489 */
490 static void us5418h_test5 (void)
491 {
492 /*
493 * success is defined in the PRINT_START define and is used for logging
494 * success or failure of the test
495 */
496 PRINT_START;
497 success = perform_timer_logging_test(SERVER_KEYGEN);
498 PRINT_END;
499 }
500
501 /*
502 * Test Timer with Simple Re-enroll
503 * This test checks to make sure the logging and python script for generating a
504 * csv of the logged timings is working on a simple enroll request. The test
505 * consists of the following steps:
506 * - enable timer logging in st server and write logs to a file
507 * - use est client to perform request
508 * - disable timer logging, close logging file, and reset logging
509 * - launch python csv generator and ensure it has entries
510 */
511 static void us5418h_test6 (void)
512 {
513 /*
514 * success is defined in the PRINT_START define and is used for logging
515 * success or failure of the test
516 */
517 PRINT_START;
518 success = perform_timer_logging_test(SIMPLE_REENROLL);
519 PRINT_END;
520 }
521
522 /* The main() function for setting up and running the tests.
523 * Returns a CUE_SUCCESS on successful running, another
524 * CUnit error code on failure.
525 */
526 int us5418h_add_suite (void)
527 {
528 #ifdef HAVE_CUNIT
529 CU_pSuite pSuite = NULL;
530
531 /* add a suite to the registry */
532 pSuite = CU_add_suite("us5418h_Performace_Timers_HTTP", us5418h_init_suite,
533 us5418h_destroy_suite);
534 if (NULL == pSuite) {
535 CU_cleanup_registry();
536 return CU_get_error();
537 }
538
539 /* add the tests to the suite */
540 if ((NULL ==
541 CU_add_test(pSuite, "Test Timer Enable/Disable API", us5418h_test1))) {
542 CU_cleanup_registry();
543 return CU_get_error();
544 }
545 if ((NULL ==
546 CU_add_test(pSuite, "Test Timer with Simple Enroll", us5418h_test2))) {
547 CU_cleanup_registry();
548 return CU_get_error();
549 }
550 if ((NULL ==
551 CU_add_test(pSuite, "Test Timer with Get CA Certs", us5418h_test3))) {
552 CU_cleanup_registry();
553 return CU_get_error();
554 }
555 if ((NULL ==
556 CU_add_test(pSuite, "Test Timer with Get CSR Attrs", us5418h_test4))) {
557 CU_cleanup_registry();
558 return CU_get_error();
559 }
560 if ((NULL ==
561 CU_add_test(pSuite, "Test Timer with Server Keygen", us5418h_test5))) {
562 CU_cleanup_registry();
563 return CU_get_error();
564 }
565 if ((NULL == CU_add_test(pSuite, "Test Timer with Simple Re-enroll",
566 us5418h_test6))) {
567 CU_cleanup_registry();
568 return CU_get_error();
569 }
570 return CUE_SUCCESS;
571 #endif
572 }