• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 = "fuzzing",
40      .type = FUZZING_TCTI,
41      },
42     {
43      .name = "unknown",
44      .type = UNKNOWN_TCTI,
45      },
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
tcti_type_from_name(char const * tcti_str)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     return UNKNOWN_TCTI;
60 }
61 
62 /*
63  * Convert from an element in the TCTI_TYPE enumeration to a string
64  * representation.
65  */
66 const char *
tcti_name_from_type(TCTI_TYPE tcti_type)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 /*
77  * return 0 if sanity test passes
78  * return 1 if sanity test fails
79  */
80 int
sanity_check_test_opts(test_opts_t * opts)81 sanity_check_test_opts(test_opts_t * opts)
82 {
83     switch (opts->tcti_type) {
84     case DEVICE_TCTI:
85         if (opts->device_file == NULL) {
86             fprintf(stderr, "device-path is NULL, check env\n");
87             return 1;
88         }
89         break;
90     case SOCKET_TCTI:
91         if (opts->socket_address == NULL || opts->socket_port == 0) {
92             fprintf(stderr,
93                     "socket_address or socket_port is NULL, check env\n");
94             return 1;
95         }
96         break;
97     case FUZZING_TCTI:
98         break;
99     default:
100         fprintf(stderr, "unknown TCTI type, check env\n");
101         return 1;
102     }
103     return 0;
104 }
105 
106 /*
107  * Parse command line options from argv extracting test options. These are
108  * returned to the caller in the provided options structure.
109  */
110 int
get_test_opts_from_env(test_opts_t * test_opts)111 get_test_opts_from_env(test_opts_t * test_opts)
112 {
113     char *env_str, *end_ptr;
114 
115     if (test_opts == NULL)
116         return 1;
117     env_str = getenv(ENV_TCTI_NAME);
118     if (env_str != NULL)
119         test_opts->tcti_type = tcti_type_from_name(env_str);
120     env_str = getenv(ENV_DEVICE_FILE);
121     if (env_str != NULL)
122         test_opts->device_file = env_str;
123     env_str = getenv(ENV_SOCKET_ADDRESS);
124     if (env_str != NULL)
125         test_opts->socket_address = env_str;
126     env_str = getenv(ENV_SOCKET_PORT);
127     if (env_str != NULL)
128         test_opts->socket_port = strtol(env_str, &end_ptr, 10);
129     return 0;
130 }
131 
132 /*
133  * Dump the contents of the test_opts_t structure to stdout.
134  */
135 void
dump_test_opts(test_opts_t * opts)136 dump_test_opts(test_opts_t * opts)
137 {
138     printf("test_opts_t:\n");
139     printf("  tcti_type:      %s\n", tcti_name_from_type(opts->tcti_type));
140     printf("  device_file:    %s\n", opts->device_file);
141     printf("  socket_address: %s\n", opts->socket_address);
142     printf("  socket_port:    %d\n", opts->socket_port);
143 }
144