2
0

main.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Copyright 2016-2017 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 "internal/nelem.h"
  11. #include "output.h"
  12. #include "tu_local.h"
  13. #include <string.h>
  14. static size_t arg_count;
  15. static char **args;
  16. static unsigned char arg_used[1000];
  17. static void check_arg_usage(void)
  18. {
  19. size_t i, n = arg_count < OSSL_NELEM(arg_used) ? arg_count
  20. : OSSL_NELEM(arg_used);
  21. for (i = 0; i < n; i++)
  22. if (!arg_used[i+1])
  23. test_printf_stderr("Warning ignored command-line argument %d: %s\n",
  24. i, args[i+1]);
  25. if (i < arg_count)
  26. test_printf_stderr("Warning arguments %zu and later unchecked\n", i);
  27. }
  28. int main(int argc, char *argv[])
  29. {
  30. int ret = EXIT_FAILURE;
  31. test_open_streams();
  32. if (!global_init()) {
  33. test_printf_stderr("Global init failed - aborting\n");
  34. return ret;
  35. }
  36. arg_count = argc - 1;
  37. args = argv;
  38. setup_test_framework();
  39. if (setup_tests())
  40. ret = run_tests(argv[0]);
  41. cleanup_tests();
  42. check_arg_usage();
  43. ret = pulldown_test_framework(ret);
  44. test_close_streams();
  45. return ret;
  46. }
  47. const char *test_get_program_name(void)
  48. {
  49. return args[0];
  50. }
  51. char *test_get_argument(size_t n)
  52. {
  53. if (n > arg_count)
  54. return NULL;
  55. if (n + 1 < OSSL_NELEM(arg_used))
  56. arg_used[n + 1] = 1;
  57. return args[n + 1];
  58. }
  59. size_t test_get_argument_count(void)
  60. {
  61. return arg_count;
  62. }
  63. int test_has_option(const char *option)
  64. {
  65. size_t i;
  66. for (i = 1; i <= arg_count; i++)
  67. if (strcmp(args[i], option) == 0) {
  68. arg_used[i] = 1;
  69. return 1;
  70. }
  71. return 0;
  72. }
  73. const char *test_get_option_argument(const char *option)
  74. {
  75. size_t i, n = strlen(option);
  76. for (i = 1; i <= arg_count; i++)
  77. if (strncmp(args[i], option, n) == 0) {
  78. arg_used[i] = 1;
  79. if (args[i][n] == '\0' && i + 1 < arg_count) {
  80. arg_used[++i] = 1;
  81. return args[i];
  82. }
  83. return args[i] + n;
  84. }
  85. return NULL;
  86. }