sys-test-options.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /* SPDX-License-Identifier: BSD-2-Clause */
  2. /***********************************************************************
  3. * Copyright (c) 2017-2018, Intel Corporation
  4. *
  5. * All rights reserved.
  6. ***********************************************************************/
  7. #ifdef HAVE_CONFIG_H
  8. #include <config.h>
  9. #endif
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include "test-options.h"
  14. /*
  15. * A structure to map a string name to an element in the TCTI_TYPE
  16. * enumeration.
  17. */
  18. typedef struct {
  19. char *name;
  20. TCTI_TYPE type;
  21. } tcti_map_entry_t;
  22. /*
  23. * A table of tcti_map_entry_t structures. This is how we map a string
  24. * provided on the command line to the enumeration.
  25. */
  26. tcti_map_entry_t tcti_map_table[] = {
  27. {
  28. .name = "device",
  29. .type = DEVICE_TCTI,
  30. },
  31. {
  32. .name = "socket",
  33. .type = SOCKET_TCTI,
  34. },
  35. {
  36. .name = "swtpm",
  37. .type = SWTPM_TCTI,
  38. },
  39. {
  40. .name = "fuzzing",
  41. .type = FUZZING_TCTI,
  42. },
  43. {
  44. .name = "unknown",
  45. .type = UNKNOWN_TCTI,
  46. },
  47. };
  48. /*
  49. * Convert from a string to an element in the TCTI_TYPE enumeration.
  50. * An unknown name / string will map to UNKNOWN_TCTI.
  51. */
  52. TCTI_TYPE
  53. tcti_type_from_name(char const *tcti_str)
  54. {
  55. int i;
  56. for (i = 0; i < N_TCTI; ++i)
  57. if (strcmp(tcti_str, tcti_map_table[i].name) == 0)
  58. return tcti_map_table[i].type;
  59. fprintf(stderr, "Unknown tcti %s.\n", tcti_str);
  60. return UNKNOWN_TCTI;
  61. }
  62. /*
  63. * Convert from an element in the TCTI_TYPE enumeration to a string
  64. * representation.
  65. */
  66. const char *
  67. tcti_name_from_type(TCTI_TYPE tcti_type)
  68. {
  69. int i;
  70. for (i = 0; i < N_TCTI; ++i)
  71. if (tcti_type == tcti_map_table[i].type)
  72. return tcti_map_table[i].name;
  73. return NULL;
  74. }
  75. /*
  76. * return 0 if sanity test passes
  77. * return 1 if sanity test fails
  78. */
  79. int
  80. sanity_check_test_opts(test_opts_t * opts)
  81. {
  82. switch (opts->tcti_type) {
  83. case DEVICE_TCTI:
  84. if (opts->device_file == NULL) {
  85. fprintf(stderr, "device-path is NULL, check env\n");
  86. return 1;
  87. }
  88. break;
  89. case SOCKET_TCTI:
  90. if (opts->socket_address == NULL || opts->socket_port == 0) {
  91. fprintf(stderr,
  92. "socket_address or socket_port is NULL, check env\n");
  93. return 1;
  94. }
  95. break;
  96. case SWTPM_TCTI:
  97. if (opts->socket_address == NULL || opts->socket_port == 0) {
  98. fprintf(stderr,
  99. "socket_address or socket_port is NULL, check env\n");
  100. return 1;
  101. }
  102. break;
  103. case FUZZING_TCTI:
  104. break;
  105. default:
  106. fprintf(stderr, "unknown TCTI type, check env\n");
  107. return 1;
  108. }
  109. return 0;
  110. }
  111. /*
  112. * Parse command line options from argv extracting test options. These are
  113. * returned to the caller in the provided options structure.
  114. */
  115. int
  116. get_test_opts_from_env(test_opts_t * test_opts)
  117. {
  118. char *env_str, *end_ptr;
  119. if (test_opts == NULL)
  120. return 1;
  121. env_str = getenv(ENV_TCTI_NAME);
  122. if (env_str != NULL)
  123. test_opts->tcti_type = tcti_type_from_name(env_str);
  124. env_str = getenv(ENV_DEVICE_FILE);
  125. if (env_str != NULL)
  126. test_opts->device_file = env_str;
  127. env_str = getenv(ENV_SOCKET_ADDRESS);
  128. if (env_str != NULL)
  129. test_opts->socket_address = env_str;
  130. env_str = getenv(ENV_SOCKET_PORT);
  131. if (env_str != NULL)
  132. test_opts->socket_port = strtol(env_str, &end_ptr, 10);
  133. return 0;
  134. }
  135. /*
  136. * Dump the contents of the test_opts_t structure to stdout.
  137. */
  138. void
  139. dump_test_opts(test_opts_t * opts)
  140. {
  141. printf("test_opts_t:\n");
  142. printf(" tcti_type: %s\n", tcti_name_from_type(opts->tcti_type));
  143. printf(" device_file: %s\n", opts->device_file);
  144. printf(" socket_address: %s\n", opts->socket_address);
  145. printf(" socket_port: %d\n", opts->socket_port);
  146. }