1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2014, STMicroelectronics International N.V.
4  */
5 
6 /*************************************************************************
7  * 1. Includes
8  ************************************************************************/
9 #include "adbg_int.h"
10 
11 /*************************************************************************
12  * 2. Definition of external constants and variables
13  ************************************************************************/
14 
15 /*************************************************************************
16  * 3. File scope types, constants and variables
17  ************************************************************************/
18 
19 /*************************************************************************
20  * 4. Declaration of file local functions
21  ************************************************************************/
22 
23 static bool ADBG_AssertHelper(ADBG_Case_t *const Case_p,
24 			      const char *const FileName_p,
25 			      const int LineNumber, const bool ExpressionOK);
26 
27 static const char *ADBG_GetFileBase(const char *const FileName_p);
28 
29 /*************************************************************************
30  * 5. Definition of external functions
31  ************************************************************************/
Do_ADBG_Expect(ADBG_Case_t * const Case_p,const char * const FileName_p,const int LineNumber,const int Expected,const int Got,const char * const GotVarName_p,const ADBG_EnumTable_t * const EnumTable_p)32 bool Do_ADBG_Expect(
33 	ADBG_Case_t *const Case_p,
34 	const char *const FileName_p,
35 	const int LineNumber,
36 	const int Expected,
37 	const int Got,
38 	const char *const GotVarName_p,
39 	const ADBG_EnumTable_t *const EnumTable_p
40 	)
41 {
42 	if (ADBG_AssertHelper(Case_p, FileName_p, LineNumber, Expected == Got))
43 		return true;
44 
45 	if (EnumTable_p != NULL) {
46 		Do_ADBG_Log("%s:%d: %s has an unexpected value: 0x%x = %s, "
47 			    "expected 0x%x = %s",
48 			    ADBG_GetFileBase(FileName_p), LineNumber,
49 			    GotVarName_p,
50 			    Got, Do_ADBG_GetEnumName(Got, EnumTable_p),
51 			    Expected,
52 			    Do_ADBG_GetEnumName(Expected, EnumTable_p));
53 	} else {
54 		Do_ADBG_Log(
55 			"%s:%d: %s has an unexpected value: 0x%x, expected 0x%x",
56 			ADBG_GetFileBase(FileName_p), LineNumber,
57 			GotVarName_p, Got, Expected);
58 	}
59 
60 	return false;
61 }
62 
Do_ADBG_ExpectNot(ADBG_Case_t * const Case_p,const char * const FileName_p,const int LineNumber,const int NotExpected,const int Got,const char * const GotVarName_p,const ADBG_EnumTable_t * const EnumTable_p)63 bool Do_ADBG_ExpectNot(
64 	ADBG_Case_t *const Case_p,
65 	const char *const FileName_p,
66 	const int LineNumber,
67 	const int NotExpected,
68 	const int Got,
69 	const char *const GotVarName_p,
70 	const ADBG_EnumTable_t *const EnumTable_p
71 	)
72 {
73 	if (ADBG_AssertHelper(Case_p, FileName_p, LineNumber, NotExpected !=
74 			      Got))
75 		return true;
76 
77 	if (EnumTable_p != NULL) {
78 		Do_ADBG_Log("%s:%d: %s has an unexpected value: 0x%x = %s, "
79 			    "expected 0x%x = %s",
80 			    ADBG_GetFileBase(FileName_p), LineNumber,
81 			    GotVarName_p,
82 			    Got, Do_ADBG_GetEnumName(Got, EnumTable_p),
83 			    NotExpected,
84 			    Do_ADBG_GetEnumName(NotExpected, EnumTable_p));
85 	} else {
86 		Do_ADBG_Log(
87 			"%s:%d: %s has an unexpected value: 0x%zu, expected 0x%zu",
88 			ADBG_GetFileBase(FileName_p), LineNumber,
89 			GotVarName_p, (size_t)Got, (size_t)NotExpected);
90 	}
91 
92 	return false;
93 }
94 
Do_ADBG_ExpectBuffer(ADBG_Case_t * const Case_p,const char * const FileName_p,const int LineNumber,const void * const ExpectedBuffer_p,const size_t ExpectedBufferLength,const char * const GotBufferName_p,const void * const GotBuffer_p,const char * const GotBufferLengthName_p,const size_t GotBufferLength)95 bool Do_ADBG_ExpectBuffer(
96 	ADBG_Case_t *const Case_p,
97 	const char *const FileName_p,
98 	const int LineNumber,
99 	const void *const ExpectedBuffer_p,
100 	const size_t ExpectedBufferLength,
101 	const char *const GotBufferName_p,
102 	const void *const GotBuffer_p,
103 	const char *const GotBufferLengthName_p,
104 	const size_t GotBufferLength
105 	)
106 {
107 	if (!ADBG_AssertHelper(Case_p, FileName_p, LineNumber,
108 			       ExpectedBufferLength == GotBufferLength)) {
109 		Do_ADBG_Log(
110 			"%s:%d: %s has an unexpected value: %zu, expected %zu",
111 			ADBG_GetFileBase(
112 				FileName_p), LineNumber,
113 			GotBufferLengthName_p, GotBufferLength,
114 			ExpectedBufferLength);
115 		return false;
116 	}
117 
118 	if (!ADBG_AssertHelper(Case_p, FileName_p, LineNumber,
119 			       memcmp(ExpectedBuffer_p, GotBuffer_p,
120 				      ExpectedBufferLength) == 0)) {
121 		Do_ADBG_Log("%s:%d: %s has an unexpected content:",
122 			    ADBG_GetFileBase(
123 				    FileName_p), LineNumber, GotBufferName_p);
124 		Do_ADBG_Log("Got");
125 		Do_ADBG_HexLog(GotBuffer_p, GotBufferLength, 16);
126 		Do_ADBG_Log("Expected");
127 		Do_ADBG_HexLog(ExpectedBuffer_p, ExpectedBufferLength, 16);
128 		return false;
129 	}
130 
131 	return true;
132 }
133 
Do_ADBG_ExpectPointer(ADBG_Case_t * const Case_p,const char * const FileName_p,const int LineNumber,const void * Expected_p,const void * Got_p,const char * const GotVarName_p)134 bool Do_ADBG_ExpectPointer(
135 	ADBG_Case_t *const Case_p,
136 	const char *const FileName_p,
137 	const int LineNumber,
138 	const void *Expected_p,
139 	const void *Got_p,
140 	const char *const GotVarName_p
141 	)
142 {
143 	if (ADBG_AssertHelper(Case_p, FileName_p, LineNumber, Expected_p ==
144 			      Got_p))
145 		return true;
146 
147 	Do_ADBG_Log("%s:%d: %s has an unexpected value: %p, expected %p",
148 		    ADBG_GetFileBase(FileName_p), LineNumber,
149 		    GotVarName_p, Got_p, Expected_p);
150 
151 	return false;
152 }
153 
154 
155 
Do_ADBG_ExpectPointerNotNULL(ADBG_Case_t * const Case_p,const char * const FileName_p,const int LineNumber,const void * Got_p,const char * const GotVarName_p)156 bool Do_ADBG_ExpectPointerNotNULL(
157 	ADBG_Case_t *const Case_p,
158 	const char *const FileName_p,
159 	const int LineNumber,
160 	const void *Got_p,
161 	const char *const GotVarName_p
162 	)
163 {
164 	if (ADBG_AssertHelper(Case_p, FileName_p, LineNumber, Got_p != NULL))
165 		return true;
166 
167 	Do_ADBG_Log("%s:%d: %s has an unexpected value: %p, expected not NULL",
168 		    ADBG_GetFileBase(FileName_p), LineNumber,
169 		    GotVarName_p, Got_p);
170 
171 	return false;
172 }
173 
Do_ADBG_ExpectCompareSigned(ADBG_Case_t * const Case_p,const char * const FileName_p,const int LineNumber,const long Value1,const long Value2,const bool Result,const char * const Value1Str_p,const char * const ComparStr_p,const char * const Value2Str_p)174 bool Do_ADBG_ExpectCompareSigned(
175 	ADBG_Case_t *const Case_p,
176 	const char *const FileName_p,
177 	const int LineNumber,
178 	const long Value1,
179 	const long Value2,
180 	const bool Result,
181 	const char *const Value1Str_p,
182 	const char *const ComparStr_p,
183 	const char *const Value2Str_p
184 	)
185 {
186 	if (!ADBG_AssertHelper(Case_p, FileName_p, LineNumber, Result)) {
187 		Do_ADBG_Log(
188 			"%s:%d: Expression \"%s %s %s\" (%ld %s %ld) is false",
189 			ADBG_GetFileBase(FileName_p), LineNumber,
190 			Value1Str_p, ComparStr_p, Value2Str_p,
191 			Value1, ComparStr_p, Value2);
192 	}
193 	return Result;
194 }
195 
Do_ADBG_ExpectCompareUnsigned(ADBG_Case_t * const Case_p,const char * const FileName_p,const int LineNumber,const unsigned long Value1,const unsigned long Value2,const bool Result,const char * const Value1Str_p,const char * const ComparStr_p,const char * const Value2Str_p)196 bool Do_ADBG_ExpectCompareUnsigned(
197 	ADBG_Case_t *const Case_p,
198 	const char *const FileName_p,
199 	const int LineNumber,
200 	const unsigned long Value1,
201 	const unsigned long Value2,
202 	const bool Result,
203 	const char *const Value1Str_p,
204 	const char *const ComparStr_p,
205 	const char *const Value2Str_p
206 	)
207 {
208 	if (!ADBG_AssertHelper(Case_p, FileName_p, LineNumber, Result)) {
209 		Do_ADBG_Log(
210 			"%s:%d: Expression \"%s %s %s\" (%lu %s %lu) is false",
211 			ADBG_GetFileBase(FileName_p), LineNumber,
212 			Value1Str_p, ComparStr_p, Value2Str_p,
213 			Value1, ComparStr_p, Value2);
214 	}
215 	return Result;
216 }
217 
218 
Do_ADBG_ExpectComparePointer(ADBG_Case_t * const Case_p,const char * const FileName_p,const int LineNumber,const void * const Value1_p,const void * const Value2_p,const bool Result,const char * const Value1Str_p,const char * const ComparStr_p,const char * const Value2Str_p)219 bool Do_ADBG_ExpectComparePointer(
220 	ADBG_Case_t *const Case_p,
221 	const char *const FileName_p,
222 	const int LineNumber,
223 	const void *const Value1_p,
224 	const void *const Value2_p,
225 	const bool Result,
226 	const char *const Value1Str_p,
227 	const char *const ComparStr_p,
228 	const char *const Value2Str_p
229 	)
230 {
231 	if (!ADBG_AssertHelper(Case_p, FileName_p, LineNumber, Result)) {
232 		Do_ADBG_Log(
233 			"%s:%d: Expression \"%s %s %s\" (%p %s %p) is false",
234 			ADBG_GetFileBase(FileName_p), LineNumber,
235 			Value1Str_p, ComparStr_p, Value2Str_p,
236 			Value1_p, ComparStr_p, Value2_p);
237 	}
238 	return Result;
239 }
240 
241 /*************************************************************************
242  * 6. Definitions of internal functions
243  ************************************************************************/
ADBG_AssertHelper(ADBG_Case_t * const Case_p,const char * const FileName_p,const int LineNumber,const bool ExpressionOK)244 static bool ADBG_AssertHelper(
245 	ADBG_Case_t *const Case_p,
246 	const char *const FileName_p,
247 	const int LineNumber,
248 	const bool ExpressionOK
249 	)
250 {
251 	ADBG_SubCase_t *SubCase_p = Case_p->CurrentSubCase_p;
252 
253 	IDENTIFIER_NOT_USED(Case_p)
254 
255 	SubCase_p->Result.NumTests++;
256 
257 	if (!ExpressionOK) {
258 		SubCase_p->Result.NumFailedTests++;
259 		if (SubCase_p->Result.FirstFailedRow == 0) {
260 			SubCase_p->Result.FirstFailedRow = LineNumber;
261 			SubCase_p->Result.FirstFailedFile_p = ADBG_GetFileBase(
262 				FileName_p);
263 		}
264 	}
265 
266 	return ExpressionOK;
267 }
268 
ADBG_GetFileBase(const char * const FileName_p)269 static const char *ADBG_GetFileBase(const char *const FileName_p)
270 {
271 	const char *Ch_p = FileName_p;
272 	const char *Base_p = FileName_p;
273 
274 	while (*Ch_p != '\0') {
275 		if (*Ch_p == '\\')
276 			Base_p = Ch_p + 1;
277 
278 		Ch_p++;
279 	}
280 
281 	return Base_p;
282 }
283