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 };
99
run_trusty_unitest(const char * utapp)100 static int run_trusty_unitest(const char* utapp) {
101 int fd;
102 int rc;
103 char rx_buf[1024];
104
105 /* connect to unitest app */
106 fd = tipc_connect(dev_name, utapp);
107 if (fd < 0) {
108 fprintf(stderr, "failed to connect to '%s' app: %s\n", utapp, strerror(-fd));
109 return fd;
110 }
111
112 /* wait for test to complete */
113 for (;;) {
114 rc = read(fd, rx_buf, sizeof(rx_buf));
115 if (rc <= 0 || rc >= (int)sizeof(rx_buf)) {
116 fprintf(stderr, "%s: Read failed: %d\n", __func__, rc);
117 tipc_close(fd);
118 return -1;
119 }
120
121 if (rx_buf[0] == TEST_PASSED) {
122 break;
123 } else if (rx_buf[0] == TEST_FAILED) {
124 break;
125 } else if (rx_buf[0] == TEST_MESSAGE || rx_buf[0] == TEST_TEXT) {
126 write(STDOUT_FILENO, rx_buf + 1, rc - 1);
127 } else {
128 fprintf(stderr, "%s: Bad message header: %d\n", __func__, rx_buf[0]);
129 break;
130 }
131 }
132
133 /* close connection to unitest app */
134 tipc_close(fd);
135
136 return rx_buf[0] == TEST_PASSED ? 0 : -1;
137 }
138
main(int argc,char ** argv)139 int main(int argc, char** argv) {
140 int rc = 0;
141
142 if (argc <= 1) {
143 print_usage_and_exit(argv[0], EXIT_FAILURE, false);
144 }
145
146 parse_options(argc, argv);
147
148 if (!dev_name) {
149 dev_name = TIPC_DEFAULT_DEVNAME;
150 }
151
152 if (!ut_app) {
153 fprintf(stderr, "Unittest app must be specified\n");
154 print_usage_and_exit(argv[0], EXIT_FAILURE, false);
155 }
156
157 rc = run_trusty_unitest(ut_app);
158
159 return rc == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
160 }
161