1 | /* |
---|
2 | * SPDX-License-Identifier: BSD-2-Clause |
---|
3 | * |
---|
4 | * Copyright (C) 2018, 2019 embedded brains GmbH |
---|
5 | * |
---|
6 | * Redistribution and use in source and binary forms, with or without |
---|
7 | * modification, are permitted provided that the following conditions |
---|
8 | * are met: |
---|
9 | * 1. Redistributions of source code must retain the above copyright |
---|
10 | * notice, this list of conditions and the following disclaimer. |
---|
11 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
12 | * notice, this list of conditions and the following disclaimer in the |
---|
13 | * documentation and/or other materials provided with the distribution. |
---|
14 | * |
---|
15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
---|
19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
25 | * POSSIBILITY OF SUCH DAMAGE. |
---|
26 | */ |
---|
27 | |
---|
28 | #include <t.h> |
---|
29 | |
---|
30 | #include <sys/time.h> |
---|
31 | #include <string.h> |
---|
32 | |
---|
33 | #include <rtems.h> |
---|
34 | #include <rtems/bspIo.h> |
---|
35 | #include <rtems/timecounter.h> |
---|
36 | #include <rtems/sysinit.h> |
---|
37 | |
---|
38 | #include "t-self-test.h" |
---|
39 | |
---|
40 | #include <tmacros.h> |
---|
41 | |
---|
42 | const char rtems_test_name[] = "TTEST 1"; |
---|
43 | |
---|
44 | #define test_assert(e) (e) ? (void)0 : test_failed(__LINE__, #e) |
---|
45 | |
---|
46 | RTEMS_LINKER_ROSET(t_self_test, const char *); |
---|
47 | |
---|
48 | typedef enum { |
---|
49 | CENSOR_PASS_THROUGH, |
---|
50 | CENSOR_DISCARD |
---|
51 | } censor_state; |
---|
52 | |
---|
53 | typedef struct { |
---|
54 | const char *c; |
---|
55 | size_t case_begin_count; |
---|
56 | size_t case_end_count; |
---|
57 | struct timecounter tc; |
---|
58 | T_putchar putchar; |
---|
59 | void *putchar_arg; |
---|
60 | const char *censor_c; |
---|
61 | censor_state censor_state; |
---|
62 | } test_context; |
---|
63 | |
---|
64 | static test_context test_instance; |
---|
65 | |
---|
66 | static void |
---|
67 | test_failed(int line, const char *e) |
---|
68 | { |
---|
69 | printk("FAILED:%i:%s\n", line, e); |
---|
70 | rtems_test_exit(1); |
---|
71 | } |
---|
72 | |
---|
73 | static void |
---|
74 | test_putchar(int c, void *arg) |
---|
75 | { |
---|
76 | test_context *ctx; |
---|
77 | |
---|
78 | ctx = arg; |
---|
79 | |
---|
80 | if (c != '\r' && ctx->c != NULL) { |
---|
81 | test_assert(*ctx->c == c); |
---|
82 | ++ctx->c; |
---|
83 | } |
---|
84 | |
---|
85 | rtems_putc((char)c); |
---|
86 | } |
---|
87 | |
---|
88 | static void |
---|
89 | case_early(const char *name) |
---|
90 | { |
---|
91 | test_context *ctx; |
---|
92 | const char **item; |
---|
93 | ssize_t n; |
---|
94 | |
---|
95 | ctx = &test_instance; |
---|
96 | ++ctx->case_begin_count; |
---|
97 | n = strlen(name); |
---|
98 | |
---|
99 | RTEMS_LINKER_SET_FOREACH(t_self_test, item) { |
---|
100 | const char *to; |
---|
101 | |
---|
102 | to = *item; |
---|
103 | |
---|
104 | if (strncmp(name, to, n) == 0 && to[n] == ':') { |
---|
105 | ctx->c = to + n + 1; |
---|
106 | return; |
---|
107 | } |
---|
108 | } |
---|
109 | |
---|
110 | test_assert(0); |
---|
111 | } |
---|
112 | |
---|
113 | static void |
---|
114 | case_late(const char *name) |
---|
115 | { |
---|
116 | test_context *ctx; |
---|
117 | |
---|
118 | ctx = &test_instance; |
---|
119 | ++ctx->case_end_count; |
---|
120 | test_assert(ctx->c != NULL); |
---|
121 | test_assert(*ctx->c == '\0'); |
---|
122 | ctx->c = NULL; |
---|
123 | } |
---|
124 | |
---|
125 | static const char censored_init[] = "A:ttest01\n" |
---|
126 | "S:Platform:RTEMS\n" |
---|
127 | "S:Compiler:*" |
---|
128 | "S:Version:*" |
---|
129 | "S:BSP:*" |
---|
130 | "S:RTEMS_DEBUG:*" |
---|
131 | "S:RTEMS_MULTIPROCESSING:*" |
---|
132 | "S:RTEMS_POSIX_API:*" |
---|
133 | "S:RTEMS_PROFILING:*" |
---|
134 | "S:RTEMS_SMP:*"; |
---|
135 | |
---|
136 | static void |
---|
137 | censor_putchar(int c, void *arg) |
---|
138 | { |
---|
139 | test_context *ctx; |
---|
140 | |
---|
141 | ctx = arg; |
---|
142 | |
---|
143 | if (*ctx->censor_c == '\0') { |
---|
144 | T_putchar discard_putchar; |
---|
145 | void *discard_putchar_arg; |
---|
146 | |
---|
147 | (*ctx->putchar)(c, ctx->putchar_arg); |
---|
148 | T_set_putchar(ctx->putchar, ctx->putchar_arg, &discard_putchar, |
---|
149 | &discard_putchar_arg); |
---|
150 | return; |
---|
151 | } |
---|
152 | |
---|
153 | switch (ctx->censor_state) { |
---|
154 | case CENSOR_PASS_THROUGH: |
---|
155 | if (*ctx->censor_c == '*') { |
---|
156 | (*ctx->putchar)('*', ctx->putchar_arg); |
---|
157 | ctx->censor_state = CENSOR_DISCARD; |
---|
158 | } else if (c == *ctx->censor_c) { |
---|
159 | (*ctx->putchar)(c, ctx->putchar_arg); |
---|
160 | ++ctx->censor_c; |
---|
161 | } |
---|
162 | break; |
---|
163 | case CENSOR_DISCARD: |
---|
164 | if (c == '\n') { |
---|
165 | (*ctx->putchar)(c, ctx->putchar_arg); |
---|
166 | ctx->censor_state = CENSOR_PASS_THROUGH; |
---|
167 | ++ctx->censor_c; |
---|
168 | } |
---|
169 | break; |
---|
170 | } |
---|
171 | } |
---|
172 | |
---|
173 | static void |
---|
174 | run_initialize(void) |
---|
175 | { |
---|
176 | test_context *ctx; |
---|
177 | |
---|
178 | ctx = &test_instance; |
---|
179 | ctx->censor_c = censored_init; |
---|
180 | T_set_putchar(censor_putchar, ctx, &ctx->putchar, &ctx->putchar_arg); |
---|
181 | } |
---|
182 | |
---|
183 | static const char expected_final[] = "Z:ttest01:C:341:N:1316:F:790:D:0.682999\n" |
---|
184 | "Y:ReportHash:SHA256:62d6f3b37299137932ea2c2f0505c8b8f12b95749c81d5af19570e9470203475\n"; |
---|
185 | |
---|
186 | static void |
---|
187 | run_finalize(void) |
---|
188 | { |
---|
189 | test_context *ctx; |
---|
190 | |
---|
191 | ctx = &test_instance; |
---|
192 | ctx->c = expected_final; |
---|
193 | } |
---|
194 | |
---|
195 | static void |
---|
196 | test_action(T_event event, const char *name) |
---|
197 | { |
---|
198 | (void)name; |
---|
199 | |
---|
200 | switch (event) { |
---|
201 | case T_EVENT_CASE_EARLY: |
---|
202 | case_early(name); |
---|
203 | break; |
---|
204 | case T_EVENT_CASE_LATE: |
---|
205 | case_late(name); |
---|
206 | break; |
---|
207 | case T_EVENT_RUN_INITIALIZE_EARLY: |
---|
208 | run_initialize(); |
---|
209 | break; |
---|
210 | case T_EVENT_RUN_FINALIZE_EARLY: |
---|
211 | run_finalize(); |
---|
212 | break; |
---|
213 | default: |
---|
214 | break; |
---|
215 | }; |
---|
216 | } |
---|
217 | |
---|
218 | static Atomic_Uint counter = ATOMIC_INITIALIZER_UINT(0); |
---|
219 | |
---|
220 | static T_time |
---|
221 | now(void) |
---|
222 | { |
---|
223 | T_time t; |
---|
224 | |
---|
225 | t = _Atomic_Fetch_add_uint(&counter, 1, ATOMIC_ORDER_RELAXED); |
---|
226 | return t * SBT_1MS; |
---|
227 | } |
---|
228 | |
---|
229 | static uint32_t |
---|
230 | get_timecount(struct timecounter *tc) |
---|
231 | { |
---|
232 | return 0; |
---|
233 | } |
---|
234 | |
---|
235 | static void |
---|
236 | install_timecounter(void) |
---|
237 | { |
---|
238 | test_context *ctx; |
---|
239 | |
---|
240 | ctx = &test_instance; |
---|
241 | ctx->tc.tc_get_timecount = get_timecount; |
---|
242 | ctx->tc.tc_counter_mask = 0xffffffff; |
---|
243 | ctx->tc.tc_frequency = 1000000000; |
---|
244 | ctx->tc.tc_quality = RTEMS_TIMECOUNTER_QUALITY_CLOCK_DRIVER + 1; |
---|
245 | rtems_timecounter_install(&ctx->tc); |
---|
246 | } |
---|
247 | |
---|
248 | RTEMS_SYSINIT_ITEM(install_timecounter, RTEMS_SYSINIT_DEVICE_DRIVERS, |
---|
249 | RTEMS_SYSINIT_ORDER_FIRST); |
---|
250 | |
---|
251 | static char buffer[512]; |
---|
252 | |
---|
253 | static const T_action actions[] = { |
---|
254 | T_report_hash_sha256, |
---|
255 | test_action, |
---|
256 | T_check_file_descriptors, |
---|
257 | T_check_rtems_barriers, |
---|
258 | T_check_rtems_extensions, |
---|
259 | T_check_rtems_message_queues, |
---|
260 | T_check_rtems_partitions, |
---|
261 | T_check_rtems_periods, |
---|
262 | T_check_rtems_regions, |
---|
263 | T_check_rtems_semaphores, |
---|
264 | T_check_rtems_tasks, |
---|
265 | T_check_rtems_timers, |
---|
266 | T_check_posix_keys |
---|
267 | }; |
---|
268 | |
---|
269 | static const T_config config = { |
---|
270 | .name = "ttest01", |
---|
271 | .buf = buffer, |
---|
272 | .buf_size = sizeof(buffer), |
---|
273 | .putchar = test_putchar, |
---|
274 | .putchar_arg = &test_instance, |
---|
275 | .verbosity = T_VERBOSE, |
---|
276 | .now = now, |
---|
277 | .action_count = T_ARRAY_SIZE(actions), |
---|
278 | .actions = actions |
---|
279 | }; |
---|
280 | |
---|
281 | static void |
---|
282 | Init(rtems_task_argument arg) |
---|
283 | { |
---|
284 | test_context *ctx; |
---|
285 | int exit_code; |
---|
286 | size_t case_count; |
---|
287 | |
---|
288 | (void)arg; |
---|
289 | TEST_BEGIN(); |
---|
290 | ctx = &test_instance; |
---|
291 | test_assert(!T_is_runner()); |
---|
292 | T_register(); |
---|
293 | test_assert(!T_is_runner()); |
---|
294 | exit_code = T_main(&config); |
---|
295 | test_assert(exit_code == 1); |
---|
296 | test_assert(!T_is_runner()); |
---|
297 | case_count = RTEMS_LINKER_SET_ITEM_COUNT(t_self_test); |
---|
298 | test_assert(ctx->case_begin_count == case_count); |
---|
299 | test_assert(ctx->case_end_count == case_count); |
---|
300 | TEST_END(); |
---|
301 | rtems_test_exit(0); |
---|
302 | } |
---|
303 | |
---|
304 | #define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER |
---|
305 | |
---|
306 | #define CONFIGURE_LIBIO_MAXIMUM_FILE_DESCRIPTORS 4 |
---|
307 | |
---|
308 | #define CONFIGURE_MAXIMUM_BARRIERS 1 |
---|
309 | #define CONFIGURE_MAXIMUM_MESSAGE_QUEUES 1 |
---|
310 | #define CONFIGURE_MAXIMUM_PARTITIONS 1 |
---|
311 | #define CONFIGURE_MAXIMUM_PERIODS 1 |
---|
312 | #define CONFIGURE_MAXIMUM_REGIONS 1 |
---|
313 | #define CONFIGURE_MAXIMUM_SEMAPHORES 1 |
---|
314 | #define CONFIGURE_MAXIMUM_TASKS 2 |
---|
315 | #define CONFIGURE_MAXIMUM_TIMERS 1 |
---|
316 | #define CONFIGURE_MAXIMUM_USER_EXTENSIONS 1 |
---|
317 | |
---|
318 | #define CONFIGURE_MAXIMUM_POSIX_KEYS 1 |
---|
319 | |
---|
320 | #define CONFIGURE_RTEMS_INIT_TASKS_TABLE |
---|
321 | |
---|
322 | #define CONFIGURE_INIT |
---|
323 | |
---|
324 | #include <rtems/confdefs.h> |
---|