blob: 44ed1ba2d313adcf02579b574df9b471f1f4b461 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Joe Hershberger3a77be52015-05-20 14:27:27 -05002/*
3 * Simple unit test library
4 *
5 * Copyright (c) 2013 Google, Inc
Joe Hershberger3a77be52015-05-20 14:27:27 -05006 */
7
8#include <common.h>
Simon Glassd856e912020-01-27 08:49:56 -07009#include <console.h>
Simon Glass19920d72019-12-29 21:19:23 -070010#include <malloc.h>
Simon Glassb8f00822020-11-08 21:08:43 -070011#ifdef CONFIG_SANDBOX
12#include <asm/state.h>
13#endif
Joe Hershberger3a77be52015-05-20 14:27:27 -050014#include <test/test.h>
15#include <test/ut.h>
16
Simon Glassfe6d12a2015-11-08 23:47:50 -070017DECLARE_GLOBAL_DATA_PTR;
18
Joe Hershberger3a77be52015-05-20 14:27:27 -050019void ut_fail(struct unit_test_state *uts, const char *fname, int line,
20 const char *func, const char *cond)
21{
Simon Glassfe6d12a2015-11-08 23:47:50 -070022 gd->flags &= ~(GD_FLG_SILENT | GD_FLG_RECORD);
Joe Hershberger3a77be52015-05-20 14:27:27 -050023 printf("%s:%d, %s(): %s\n", fname, line, func, cond);
24 uts->fail_count++;
25}
26
27void ut_failf(struct unit_test_state *uts, const char *fname, int line,
28 const char *func, const char *cond, const char *fmt, ...)
29{
30 va_list args;
31
Simon Glassfe6d12a2015-11-08 23:47:50 -070032 gd->flags &= ~(GD_FLG_SILENT | GD_FLG_RECORD);
Joe Hershberger3a77be52015-05-20 14:27:27 -050033 printf("%s:%d, %s(): %s: ", fname, line, func, cond);
34 va_start(args, fmt);
35 vprintf(fmt, args);
36 va_end(args);
37 putc('\n');
38 uts->fail_count++;
39}
Simon Glass19920d72019-12-29 21:19:23 -070040
41ulong ut_check_free(void)
42{
43 struct mallinfo info = mallinfo();
44
45 return info.uordblks;
46}
47
48long ut_check_delta(ulong last)
49{
50 return ut_check_free() - last;
51}
52
Simon Glassd856e912020-01-27 08:49:56 -070053int ut_check_console_line(struct unit_test_state *uts, const char *fmt, ...)
54{
55 va_list args;
56
57 va_start(args, fmt);
58 vsnprintf(uts->expect_str, sizeof(uts->expect_str), fmt, args);
59 va_end(args);
60 console_record_readline(uts->actual_str, sizeof(uts->actual_str));
61
62 return strcmp(uts->expect_str, uts->actual_str);
63}
64
Simon Glassc963df92020-07-28 19:41:10 -060065int ut_check_console_linen(struct unit_test_state *uts, const char *fmt, ...)
66{
67 va_list args;
68
69 va_start(args, fmt);
70 vsnprintf(uts->expect_str, sizeof(uts->expect_str), fmt, args);
71 va_end(args);
72 console_record_readline(uts->actual_str, sizeof(uts->actual_str));
73
74 return strncmp(uts->expect_str, uts->actual_str,
75 strlen(uts->expect_str));
76}
77
78int ut_check_skipline(struct unit_test_state *uts)
79{
80 if (!console_record_avail())
81 return -ENFILE;
82 console_record_readline(uts->actual_str, sizeof(uts->actual_str));
83
84 return 0;
85}
86
Simon Glassd856e912020-01-27 08:49:56 -070087int ut_check_console_end(struct unit_test_state *uts)
88{
89 if (!console_record_avail())
90 return 0;
91
92 console_record_readline(uts->actual_str, sizeof(uts->actual_str));
93
94 return 1;
95}
96
97int ut_check_console_dump(struct unit_test_state *uts, int total_bytes)
98{
99 char *str = uts->actual_str;
100 int upto;
101
102 /* Handle empty dump */
103 if (!total_bytes)
104 return 0;
105
106 for (upto = 0; upto < total_bytes;) {
107 int len;
108 int bytes;
109
110 len = console_record_readline(str, sizeof(uts->actual_str));
111 if (str[8] != ':' || str[9] != ' ')
112 return 1;
113
114 bytes = len - 8 - 2 - 3 * 16 - 4;
115 upto += bytes;
116 }
117
118 return upto == total_bytes ? 0 : 1;
119}
Simon Glassb8f00822020-11-08 21:08:43 -0700120
121void ut_silence_console(struct unit_test_state *uts)
122{
123#ifdef CONFIG_SANDBOX
124 struct sandbox_state *state = state_get_current();
125
126 if (!state->show_test_output)
127 gd->flags |= GD_FLG_SILENT;
128#endif
129}
130
131void ut_unsilence_console(struct unit_test_state *uts)
132{
133 gd->flags &= ~(GD_FLG_SILENT | GD_FLG_RECORD);
134}