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