testutil.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * Copyright 2014-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include "testutil.h"
  10. #include <assert.h>
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include "e_os.h"
  15. /*
  16. * Declares the structures needed to register each test case function.
  17. */
  18. typedef struct test_info {
  19. const char *test_case_name;
  20. int (*test_fn) ();
  21. int (*param_test_fn)(int idx);
  22. int num;
  23. } TEST_INFO;
  24. static TEST_INFO all_tests[1024];
  25. static int num_tests = 0;
  26. /*
  27. * A parameterised tests runs a loop of test cases.
  28. * |num_test_cases| counts the total number of test cases
  29. * across all tests.
  30. */
  31. static int num_test_cases = 0;
  32. void add_test(const char *test_case_name, int (*test_fn) ())
  33. {
  34. assert(num_tests != OSSL_NELEM(all_tests));
  35. all_tests[num_tests].test_case_name = test_case_name;
  36. all_tests[num_tests].test_fn = test_fn;
  37. all_tests[num_tests].num = -1;
  38. ++num_test_cases;
  39. ++num_tests;
  40. }
  41. void add_all_tests(const char *test_case_name, int(*test_fn)(int idx),
  42. int num)
  43. {
  44. assert(num_tests != OSSL_NELEM(all_tests));
  45. all_tests[num_tests].test_case_name = test_case_name;
  46. all_tests[num_tests].param_test_fn = test_fn;
  47. all_tests[num_tests].num = num;
  48. ++num_tests;
  49. num_test_cases += num;
  50. }
  51. int run_tests(const char *test_prog_name)
  52. {
  53. int num_failed = 0;
  54. int i, j;
  55. printf("%s: %d test case%s\n", test_prog_name, num_test_cases,
  56. num_test_cases == 1 ? "" : "s");
  57. for (i = 0; i != num_tests; ++i) {
  58. if (all_tests[i].num == -1) {
  59. if (!all_tests[i].test_fn()) {
  60. printf("** %s failed **\n--------\n",
  61. all_tests[i].test_case_name);
  62. ++num_failed;
  63. }
  64. } else {
  65. for (j = 0; j < all_tests[i].num; j++) {
  66. if (!all_tests[i].param_test_fn(j)) {
  67. printf("** %s failed test %d\n--------\n",
  68. all_tests[i].test_case_name, j);
  69. ++num_failed;
  70. }
  71. }
  72. }
  73. }
  74. if (num_failed != 0) {
  75. printf("%s: %d test%s failed (out of %d)\n", test_prog_name,
  76. num_failed, num_failed != 1 ? "s" : "", num_test_cases);
  77. return EXIT_FAILURE;
  78. }
  79. printf(" All tests passed.\n");
  80. return EXIT_SUCCESS;
  81. }
  82. static const char *print_string_maybe_null(const char *s)
  83. {
  84. return s == NULL ? "(NULL)" : s;
  85. }
  86. int strings_equal(const char *desc, const char *s1, const char *s2)
  87. {
  88. if (s1 == NULL && s2 == NULL)
  89. return 1;
  90. if (s1 == NULL || s2 == NULL || strcmp(s1, s2) != 0) {
  91. fprintf(stderr, "%s mismatch: %s vs %s\n", desc, print_string_maybe_null(s1),
  92. print_string_maybe_null(s2));
  93. return 0;
  94. }
  95. return 1;
  96. }