hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/* SPDX-License-Identifier: BSD-2-Clause */
/*
 * Copyright (c) 2014, STMicroelectronics International N.V.
 * All rights reserved.
 */
 
#ifndef TB_ASSERTS_H
#define TB_ASSERTS_H
 
#include <trace.h>
#include "tb_macros.h"
 
/*
 * TB_ASSERT_MSG general assert function with a message.
 */
#define TB_ASSERT_MSG(cond, str)                                   \
do {                                                               \
   if (!(cond)) {                                             \
       EMSG("Assertion failed at line %d in file:\n%s\n", \
           __LINE__, __FILE__);                       \
       EMSG("Message: %s\n", str);                        \
       HALT;                                              \
   }; \
} while (0)
 
/*
 * TB_ASSERT general assert function.
 */
#define TB_ASSERT(cond)                                            \
do {                                                               \
   if (!(cond)) {                                             \
       EMSG("Assertion failed at line %d in file:\n%s\n", \
           __LINE__, __FILE__);                       \
       HALT;                                              \
   };                                                         \
} while (0)
 
/*
 * TB_ASSERT_EQ_SHORT checks that src equals the short value.
 */
#define TB_ASSERT_EQ_SHORT(src, short)                                         \
do {                                                                           \
   if (((short) == 0) && (__mpanum_size((mpanum)src) != 0)) {             \
       EMSG("Assertion failed at line %d in file:\n%s\n",             \
           __LINE__, __FILE__);                                   \
       EMSG("short == 0, but size != 0\n");                           \
       HALT;                                                          \
   } else if (__mpanum_size((mpanum)src) > 1) {                           \
       EMSG("Assertion failed at line %d in file:\n%s\n",             \
           __LINE__, __FILE__);                                   \
       EMSG("size > 1, cannot be equal to a short.\n");               \
       HALT;                                                          \
   } else if (                                                            \
       (int)(__mpanum_lsw((mpanum)src)*__mpanum_sign((mpanum)src)) != \
           (int)(short)) {                                        \
       EMSG("Assertion failed at line %d in file:\n%s\n",             \
               __LINE__, __FILE__);                           \
       EMSG("short == %d, but src == %d\n", (short),                  \
           (int)(__mpanum_lsw((mpanum)src)                        \
           *__mpanum_sign((mpanum)src)));                         \
       HALT;                                                          \
   };                                                                     \
} while (0)
 
/*
 * TB_ASSERT_STR_EQ checks that the two strings a and b are equal.
 */
#define TB_ASSERT_STR_EQ(a, b)                                  \
do {                                                            \
   if (my_strcmp((a), (b)) != 0) {                         \
       EMSG("Assertion failed %s != %s\n", (a), (b));  \
       HALT;                                           \
   };                                                      \
} while (0)
 
/*
 * TB_ASSERT_HEX_VALUE checks that a prints to the string v in hex.
 */
#define TB_ASSERT_HEX_PRINT_VALUE(a, v)                \
do {                                                   \
   char *_str_;                                   \
   _str_ = TEE_BigIntConvertToString(NULL,        \
       TEE_STRING_MODE_HEX_UC, (a));          \
   TB_ASSERT_STR_EQ(_str_, (v));                  \
   TEE_Free(_str_);                               \
} while (0)
 
/*
 * TB_ASSERT_POINTER_NULL(p) checks that p is null
 */
#define TB_ASSERT_POINTER_NULL(p)                                  \
do {                                                               \
   if ((p) != 0) {                                            \
       EMSG("Assertion failed, pointer was not null.\n"); \
       HALT;                                              \
   };                                                         \
} while (0)
 
/*
 * TB_ASSERT_POINTERS_EQ checks that p, q are pointing to the same element
 */
#define TB_ASSERT_POINTERS_EQ(p, q)                                  \
do {                                                                 \
   if ((p) != (q)) {                                            \
       EMSG("Assertion failed, pointers are not equal.\n"); \
       HALT;                                                \
   };                                                           \
} while (0)
 
/*
 * TB_ASSERT_POINTERS_NEQ checks that p, q are not pointing to the same element
 */
#define TB_ASSERT_POINTERS_NEQ(p, q)                             \
do {                                                             \
   if ((p) == (q)) {                                        \
       EMSG("Assertion failed, pointers are equal.\n"); \
       HALT;                                            \
   };                                                       \
} while (0)
 
/*
 * TB_ASSERT_BIGINT_EQ Checks that a and b are equal
 */
#define TB_ASSERT_BIGINT_EQ(a, b)                                   \
do {                                                                \
   if (TEE_BigIntCmp((a), (b)) != 0) {                         \
       EMSG("Assertion failed, numbers are not equal.\n"); \
       HALT;                                               \
   };                                                          \
} while (0)
 
/*
 * TB_ASSERT_BIGINT_NEQ Checks that a and b are different
 */
#define TB_ASSERT_BIGINT_NEQ(a, b)                              \
do {                                                            \
   if (TEE_BigIntCmp((a), (b)) == 0) {                     \
       EMSG("Assertion failed, numbers are equal.\n"); \
       HALT;                                           \
   };                                                      \
} while (0)
 
/*
 * TB_ASSERT_BIGINT_LESS Checks that a < b
 */
#define TB_ASSERT_BIGINT_LESS(a, b)                                         \
do {                                                                        \
   if (TEE_BigIntCmp((a), (b)) >= 0) {                                 \
       EMSG("Assertion failed, first is not less than second.\n"); \
       HALT;                                                       \
   }; \
} while (0)
 
/*
 * TB_ASSERT_INT_EQ Checks that a and be are equal
 */
#define TB_ASSERT_INT_EQ(a, b)                                      \
do {                                                                \
   if ((a) != (b)) {                                           \
       EMSG("Assertion failed, numbers are not equal.\n"); \
       HALT;                                               \
   };                                                          \
} while (0)
 
#endif