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