1 /*
2 * Copyright (C) 2022 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 #define TLOG_LEVEL TLOG_LEVEL_DEBUG
18 #define TLOG_TAG "stats-test"
19
20 #include <inttypes.h>
21 #include <lk/macros.h>
22 #include <stddef.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/mman.h>
27
28 #include <lib/tipc/tipc.h>
29 #include <lib/unittest/unittest.h>
30 #include <trusty/memref.h>
31 #include <trusty/sys/mman.h>
32 #include <trusty/time.h>
33 #include <trusty/uuid.h>
34 #include <trusty_log.h>
35 #include <interface/metrics/consumer.h>
36 #include <trusty_unittest.h>
37 #include <uapi/err.h>
38
39 static handle_t metrics_chan;
40
consumer_connect(handle_t * chan)41 static int consumer_connect(handle_t* chan) {
42 return tipc_connect(chan, METRICS_CONSUMER_PORT);
43 }
44
45 enum TrustyAtoms : int32_t {
46 TrustyAppCrashed = 100072,
47 TrustyError = 100145,
48 TrustyStorageError = 100146
49 };
50
51 typedef struct {
52 } stats_test_t;
53
TEST_F_SETUP(stats_test)54 TEST_F_SETUP(stats_test) {
55 int rc = consumer_connect(&metrics_chan);
56 ASSERT_EQ(rc, 0);
57 test_abort:;
58 }
59
TEST_F_TEARDOWN(stats_test)60 TEST_F_TEARDOWN(stats_test) {
61 close(metrics_chan);
62 }
63
TEST_F(stats_test,atom_trusty_app_crashed)64 TEST_F(stats_test, atom_trusty_app_crashed) {
65 int rc;
66 struct metrics_req req;
67 struct metrics_report_crash_req args;
68
69 char test_uuid[37] = "5247d19b-cf09-4272-a450-3ef20dbefc14";
70
71 memcpy(args.app_id, test_uuid, 37);
72
73 req.cmd = METRICS_CMD_REPORT_CRASH;
74 args.crash_reason = 0xdeafd00f;
75
76 struct iovec iovs[] = {
77 {
78 .iov_base = &req,
79 .iov_len = sizeof(req),
80 },
81 {
82 .iov_base = &args,
83 .iov_len = sizeof(args),
84 },
85 };
86 struct ipc_msg msg = {
87 .num_iov = countof(iovs),
88 .iov = iovs,
89 };
90
91 int total_len = sizeof(req) + sizeof(args);
92 rc = send_msg(metrics_chan, &msg);
93 if (rc != (int)total_len) {
94 TLOGE("failed (%d) to send storage event\n", rc);
95 }
96 test_abort:;
97 }
98
TEST_F(stats_test,atom_second_app_crashed)99 TEST_F(stats_test, atom_second_app_crashed) {
100 int rc;
101 struct metrics_req req;
102 struct metrics_report_crash_req args;
103
104 char test_uuid[37] = "5247d19b-cf09-4272-a450-3ef20dbefc14";
105
106 memcpy(args.app_id, test_uuid, 37);
107
108 req.cmd = METRICS_CMD_REPORT_CRASH;
109 args.crash_reason = 0xdeafd00f;
110
111 struct iovec iovs[] = {
112 {
113 .iov_base = &req,
114 .iov_len = sizeof(req),
115 },
116 {
117 .iov_base = &args,
118 .iov_len = sizeof(args),
119 },
120 };
121 struct ipc_msg msg = {
122 .num_iov = countof(iovs),
123 .iov = iovs,
124 };
125
126 int total_len = sizeof(req) + sizeof(args);
127 rc = send_msg(metrics_chan, &msg);
128 if (rc != (int)total_len) {
129 TLOGE("failed (%d) to send storage event\n", rc);
130 }
131 test_abort:;
132 }
133
134 PORT_TEST(stats_test, "com.android.trusty.stats.test")
135