xref: /aosp_15_r20/system/core/trusty/utils/trusty-ut-ctrl/ut-ctrl.c (revision 00c7fec1bb09f3284aad6a6f96d2f63dfc3650ad)
1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <errno.h>
18 #include <getopt.h>
19 #include <stdbool.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <sys/uio.h>
24 #include <unistd.h>
25 
26 #include <trusty/tipc.h>
27 
28 #define TIPC_DEFAULT_DEVNAME "/dev/trusty-ipc-dev0"
29 
30 static const char* dev_name = NULL;
31 static const char* ut_app = NULL;
32 
33 static const char* _sopts = "hD:";
34 static const struct option _lopts[] = {
35         {"help", no_argument, 0, 'h'},
36         {"dev", required_argument, 0, 'D'},
37         {0, 0, 0, 0},
38 };
39 
40 static const char* usage =
41         "Usage: %s [options] unittest-app\n"
42         "\n"
43         "options:\n"
44         "  -h, --help            prints this message and exit\n"
45         "  -D, --dev name        Trusty device name\n"
46         "\n";
47 
48 static const char* usage_long = "\n";
49 
50 static bool opt_silent = false;
51 
print_usage_and_exit(const char * prog,int code,bool verbose)52 static void print_usage_and_exit(const char* prog, int code, bool verbose) {
53     fprintf(stderr, usage, prog);
54     if (verbose) {
55         fprintf(stderr, "%s", usage_long);
56     }
57     exit(code);
58 }
59 
parse_options(int argc,char ** argv)60 static void parse_options(int argc, char** argv) {
61     int c;
62     int oidx = 0;
63 
64     while (1) {
65         c = getopt_long(argc, argv, _sopts, _lopts, &oidx);
66         if (c == -1) {
67             break; /* done */
68         }
69 
70         switch (c) {
71             case 'D':
72                 dev_name = strdup(optarg);
73                 break;
74 
75             case 's':
76                 opt_silent = true;
77                 break;
78 
79             case 'h':
80                 print_usage_and_exit(argv[0], EXIT_SUCCESS, true);
81                 break;
82 
83             default:
84                 print_usage_and_exit(argv[0], EXIT_FAILURE, false);
85         }
86     }
87 
88     if (optind < argc) {
89         ut_app = strdup(argv[optind]);
90     }
91 }
92 
93 enum test_message_header {
94     TEST_PASSED = 0,
95     TEST_FAILED = 1,
96     TEST_MESSAGE = 2,
97     TEST_TEXT = 3,
98     TEST_OPCODE_COUNT,
99 };
100 
get_msg_len(const char * buf,int max_buf_len)101 static int get_msg_len(const char* buf, int max_buf_len) {
102     int buf_len;
103     for (buf_len = 0; buf_len < max_buf_len; buf_len++) {
104         if ((unsigned char)buf[buf_len] < TEST_OPCODE_COUNT) {
105             break;
106         }
107     }
108     return buf_len;
109 }
110 
run_trusty_unitest(const char * utapp)111 static int run_trusty_unitest(const char* utapp) {
112     int fd;
113     char read_buf[1024];
114     int read_len;
115     char* rx_buf;
116     int rx_buf_len;
117     int cmd = -1;
118 
119     /* connect to unitest app */
120     fd = tipc_connect(dev_name, utapp);
121     if (fd < 0) {
122         fprintf(stderr, "failed to connect to '%s' app: %s\n", utapp, strerror(-fd));
123         return fd;
124     }
125 
126     /* wait for test to complete */
127     rx_buf_len = 0;
128     for (;;) {
129         if (rx_buf_len == 0) {
130             read_len = read(fd, read_buf, sizeof(read_buf));
131             if (read_len <= 0 || read_len > (int)sizeof(read_buf)) {
132                 fprintf(stderr, "%s: Read failed: %d, %s\n", __func__, read_len,
133                         read_len < 0 ? strerror(errno) : "");
134                 tipc_close(fd);
135                 return -1;
136             }
137             rx_buf = read_buf;
138             rx_buf_len = read_len;
139         }
140 
141         int msg_len = get_msg_len(rx_buf, rx_buf_len);
142         if (msg_len == 0) {
143             cmd = rx_buf[0];
144             rx_buf++;
145             rx_buf_len--;
146         }
147 
148         if (cmd == TEST_PASSED) {
149             break;
150         } else if (cmd == TEST_FAILED) {
151             break;
152         } else if (cmd == TEST_MESSAGE || cmd == TEST_TEXT) {
153             if (msg_len) {
154                 write(STDOUT_FILENO, rx_buf, msg_len);
155                 rx_buf += msg_len;
156                 rx_buf_len -= msg_len;
157             }
158         } else {
159             fprintf(stderr, "%s: Bad message header: %d\n", __func__, cmd);
160             break;
161         }
162     }
163 
164     /* close connection to unitest app */
165     tipc_close(fd);
166 
167     return cmd == TEST_PASSED ? 0 : -1;
168 }
169 
main(int argc,char ** argv)170 int main(int argc, char** argv) {
171     int rc = 0;
172 
173     if (argc <= 1) {
174         print_usage_and_exit(argv[0], EXIT_FAILURE, false);
175     }
176 
177     parse_options(argc, argv);
178 
179     if (!dev_name) {
180         dev_name = TIPC_DEFAULT_DEVNAME;
181     }
182 
183     if (!ut_app) {
184         fprintf(stderr, "Unittest app must be specified\n");
185         print_usage_and_exit(argv[0], EXIT_FAILURE, false);
186     }
187 
188     rc = run_trusty_unitest(ut_app);
189 
190     return rc == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
191 }
192