]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Test/GoogleTest/Library/BaseSafeIntLib/SafeIntLibUintnIntnUnitTests32.cpp
MdePkg/Test: Add port of BaseSafeIntLib unit tests to GoogleTest
[mirror_edk2.git] / MdePkg / Test / GoogleTest / Library / BaseSafeIntLib / SafeIntLibUintnIntnUnitTests32.cpp
1 /** @file
2 IA32-specific functions for unit-testing INTN and UINTN functions in
3 SafeIntLib.
4
5 Copyright (c) Microsoft Corporation.<BR>
6 Copyright (c) 2019 - 2020, Intel Corporation. All rights reserved.<BR>
7 SPDX-License-Identifier: BSD-2-Clause-Patent
8
9 **/
10
11 #include <gtest/gtest.h>
12 extern "C" {
13 #include <Base.h>
14 #include <Library/SafeIntLib.h>
15 }
16
17 TEST(ConversionTestSuite, TestSafeInt32ToUintn) {
18 RETURN_STATUS Status;
19 INT32 Operand;
20 UINTN Result;
21
22 //
23 // If Operand is non-negative, then it's a cast
24 //
25 Operand = 0x5bababab;
26 Result = 0;
27 Status = SafeInt32ToUintn (Operand, &Result);
28 ASSERT_EQ (Status, RETURN_SUCCESS);
29 ASSERT_EQ ((UINTN)0x5bababab, Result);
30
31 //
32 // Otherwise should result in an error status
33 //
34 Operand = (-1537977259);
35 Status = SafeInt32ToUintn (Operand, &Result);
36 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
37 }
38
39 TEST(ConversionTestSuite, TestSafeUint32ToIntn) {
40 RETURN_STATUS Status;
41 UINT32 Operand;
42 INTN Result;
43
44 //
45 // If Operand is <= MAX_INTN, then it's a cast
46 //
47 Operand = 0x5bababab;
48 Result = 0;
49 Status = SafeUint32ToIntn (Operand, &Result);
50 ASSERT_EQ (Status, RETURN_SUCCESS);
51 ASSERT_EQ (0x5bababab, Result);
52
53 //
54 // Otherwise should result in an error status
55 //
56 Operand = (0xabababab);
57 Status = SafeUint32ToIntn (Operand, &Result);
58 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
59 }
60
61 TEST(ConversionTestSuite, TestSafeIntnToInt32) {
62 RETURN_STATUS Status;
63 INTN Operand;
64 INT32 Result;
65
66 //
67 // INTN is same as INT32 in IA32, so this is just a cast
68 //
69 Operand = 0x5bababab;
70 Result = 0;
71 Status = SafeIntnToInt32 (Operand, &Result);
72 ASSERT_EQ (Status, RETURN_SUCCESS);
73 ASSERT_EQ (0x5bababab, Result);
74 }
75
76 TEST(ConversionTestSuite, TestSafeIntnToUint32) {
77 RETURN_STATUS Status;
78 INTN Operand;
79 UINT32 Result;
80
81 //
82 // If Operand is non-negative, then it's a cast
83 //
84 Operand = 0x5bababab;
85 Result = 0;
86 Status = SafeIntnToUint32 (Operand, &Result);
87 ASSERT_EQ (Status, RETURN_SUCCESS);
88 ASSERT_EQ ((UINT32)0x5bababab, Result);
89
90 //
91 // Otherwise should result in an error status
92 //
93 Operand = (-1537977259);
94 Status = SafeIntnToUint32 (Operand, &Result);
95 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
96 }
97
98 TEST(ConversionTestSuite, TestSafeUintnToUint32) {
99 RETURN_STATUS Status;
100 UINTN Operand;
101 UINT32 Result;
102
103 //
104 // UINTN is same as UINT32 in IA32, so this is just a cast
105 //
106 Operand = 0xabababab;
107 Result = 0;
108 Status = SafeUintnToUint32 (Operand, &Result);
109 ASSERT_EQ (Status, RETURN_SUCCESS);
110 ASSERT_EQ (0xabababab, Result);
111 }
112
113 TEST(ConversionTestSuite, TestSafeUintnToIntn) {
114 RETURN_STATUS Status;
115 UINTN Operand;
116 INTN Result;
117
118 //
119 // If Operand is <= MAX_INTN, then it's a cast
120 //
121 Operand = 0x5bababab;
122 Result = 0;
123 Status = SafeUintnToIntn (Operand, &Result);
124 ASSERT_EQ (Status, RETURN_SUCCESS);
125 ASSERT_EQ (0x5bababab, Result);
126
127 //
128 // Otherwise should result in an error status
129 //
130 Operand = (0xabababab);
131 Status = SafeUintnToIntn (Operand, &Result);
132 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
133 }
134
135 TEST(ConversionTestSuite, TestSafeUintnToInt64) {
136 RETURN_STATUS Status;
137 UINTN Operand;
138 INT64 Result;
139
140 //
141 // UINTN is same as UINT32 in IA32, and UINT32 is a subset of
142 // INT64, so this is just a cast
143 //
144 Operand = 0xabababab;
145 Result = 0;
146 Status = SafeUintnToInt64 (Operand, &Result);
147 ASSERT_EQ (Status, RETURN_SUCCESS);
148 ASSERT_EQ (0xabababab, Result);
149 }
150
151 TEST(ConversionTestSuite, TestSafeInt64ToIntn) {
152 RETURN_STATUS Status;
153 INT64 Operand;
154 INTN Result;
155
156 //
157 // If Operand is between MIN_INTN and MAX_INTN2 inclusive, then it's a cast
158 //
159 Operand = 0x5bababab;
160 Result = 0;
161 Status = SafeInt64ToIntn (Operand, &Result);
162 ASSERT_EQ (Status, RETURN_SUCCESS);
163 ASSERT_EQ (0x5bababab, Result);
164
165 Operand = (-1537977259);
166 Status = SafeInt64ToIntn (Operand, &Result);
167 ASSERT_EQ (Status, RETURN_SUCCESS);
168 ASSERT_EQ ((-1537977259), Result);
169
170 //
171 // Otherwise should result in an error status
172 //
173 Operand = (0x5babababefefefef);
174 Status = SafeInt64ToIntn (Operand, &Result);
175 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
176
177 Operand = (-6605562033422200815);
178 Status = SafeInt64ToIntn (Operand, &Result);
179 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
180 }
181
182 TEST(ConversionTestSuite, TestSafeInt64ToUintn) {
183 RETURN_STATUS Status;
184 INT64 Operand;
185 UINTN Result;
186
187 //
188 // If Operand is between 0 and MAX_UINTN inclusive, then it's a cast
189 //
190 Operand = 0xabababab;
191 Result = 0;
192 Status = SafeInt64ToUintn (Operand, &Result);
193 ASSERT_EQ (Status, RETURN_SUCCESS);
194 ASSERT_EQ (0xabababab, Result);
195
196 //
197 // Otherwise should result in an error status
198 //
199 Operand = (0x5babababefefefef);
200 Status = SafeInt64ToUintn (Operand, &Result);
201 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
202
203 Operand = (-6605562033422200815);
204 Status = SafeInt64ToUintn (Operand, &Result);
205 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
206 }
207
208 TEST(ConversionTestSuite, TestSafeUint64ToIntn) {
209 RETURN_STATUS Status;
210 UINT64 Operand;
211 INTN Result;
212
213 //
214 // If Operand is <= MAX_INTN, then it's a cast
215 //
216 Operand = 0x5bababab;
217 Result = 0;
218 Status = SafeUint64ToIntn (Operand, &Result);
219 ASSERT_EQ (Status, RETURN_SUCCESS);
220 ASSERT_EQ (0x5bababab, Result);
221
222 //
223 // Otherwise should result in an error status
224 //
225 Operand = (0xababababefefefef);
226 Status = SafeUint64ToIntn (Operand, &Result);
227 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
228 }
229
230 TEST(ConversionTestSuite, TestSafeUint64ToUintn) {
231 RETURN_STATUS Status;
232 UINT64 Operand;
233 UINTN Result;
234
235 //
236 // If Operand is <= MAX_UINTN, then it's a cast
237 //
238 Operand = 0xabababab;
239 Result = 0;
240 Status = SafeUint64ToUintn (Operand, &Result);
241 ASSERT_EQ (Status, RETURN_SUCCESS);
242 ASSERT_EQ (0xabababab, Result);
243
244 //
245 // Otherwise should result in an error status
246 //
247 Operand = (0xababababefefefef);
248 Status = SafeUint64ToUintn (Operand, &Result);
249 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
250 }
251
252 TEST(AdditionSubtractionTestSuite, TestSafeUintnAdd) {
253 RETURN_STATUS Status;
254 UINTN Augend;
255 UINTN Addend;
256 UINTN Result;
257
258 //
259 // If the result of addition doesn't overflow MAX_UINTN, then it's addition
260 //
261 Augend = 0x3a3a3a3a;
262 Addend = 0x3a3a3a3a;
263 Result = 0;
264 Status = SafeUintnAdd (Augend, Addend, &Result);
265 ASSERT_EQ (Status, RETURN_SUCCESS);
266 ASSERT_EQ ((UINTN)0x74747474, Result);
267
268 //
269 // Otherwise should result in an error status
270 //
271 Augend = 0xabababab;
272 Addend = 0xbcbcbcbc;
273 Status = SafeUintnAdd (Augend, Addend, &Result);
274 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
275 }
276
277 TEST(AdditionSubtractionTestSuite, TestSafeIntnAdd) {
278 RETURN_STATUS Status;
279 INTN Augend;
280 INTN Addend;
281 INTN Result;
282
283 //
284 // If the result of addition doesn't overflow MAX_INTN
285 // and doesn't underflow MIN_INTN, then it's addition
286 //
287 Augend = 0x3a3a3a3a;
288 Addend = 0x3a3a3a3a;
289 Result = 0;
290 Status = SafeIntnAdd (Augend, Addend, &Result);
291 ASSERT_EQ (Status, RETURN_SUCCESS);
292 ASSERT_EQ (0x74747474, Result);
293
294 Augend = (-976894522);
295 Addend = (-976894522);
296 Status = SafeIntnAdd (Augend, Addend, &Result);
297 ASSERT_EQ (Status, RETURN_SUCCESS);
298 ASSERT_EQ ((-1953789044), Result);
299
300 //
301 // Otherwise should result in an error status
302 //
303 Augend = 0x5a5a5a5a;
304 Addend = 0x5a5a5a5a;
305 Status = SafeIntnAdd (Augend, Addend, &Result);
306 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
307
308 Augend = (-1515870810);
309 Addend = (-1515870810);
310 Status = SafeIntnAdd (Augend, Addend, &Result);
311 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
312 }
313
314 TEST(AdditionSubtractionTestSuite, TestSafeUintnSub) {
315 RETURN_STATUS Status;
316 UINTN Minuend;
317 UINTN Subtrahend;
318 UINTN Result;
319
320 //
321 // If Minuend >= Subtrahend, then it's subtraction
322 //
323 Minuend = 0x5a5a5a5a;
324 Subtrahend = 0x3b3b3b3b;
325 Result = 0;
326 Status = SafeUintnSub (Minuend, Subtrahend, &Result);
327 ASSERT_EQ (Status, RETURN_SUCCESS);
328 ASSERT_EQ ((UINTN)0x1f1f1f1f, Result);
329
330 //
331 // Otherwise should result in an error status
332 //
333 Minuend = 0x5a5a5a5a;
334 Subtrahend = 0x6d6d6d6d;
335 Status = SafeUintnSub (Minuend, Subtrahend, &Result);
336 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
337 }
338
339 TEST(AdditionSubtractionTestSuite, TestSafeIntnSub) {
340 RETURN_STATUS Status;
341 INTN Minuend;
342 INTN Subtrahend;
343 INTN Result;
344
345 //
346 // If the result of subtractions doesn't overflow MAX_INTN or
347 // underflow MIN_INTN, then it's subtraction
348 //
349 Minuend = 0x5a5a5a5a;
350 Subtrahend = 0x3a3a3a3a;
351 Result = 0;
352 Status = SafeIntnSub (Minuend, Subtrahend, &Result);
353 ASSERT_EQ (Status, RETURN_SUCCESS);
354 ASSERT_EQ (0x20202020, Result);
355
356 Minuend = 0x3a3a3a3a;
357 Subtrahend = 0x5a5a5a5a;
358 Status = SafeIntnSub (Minuend, Subtrahend, &Result);
359 ASSERT_EQ (Status, RETURN_SUCCESS);
360 ASSERT_EQ ((-538976288), Result);
361
362 //
363 // Otherwise should result in an error status
364 //
365 Minuend = (-2054847098);
366 Subtrahend = 2054847098;
367 Status = SafeIntnSub (Minuend, Subtrahend, &Result);
368 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
369
370 Minuend = (2054847098);
371 Subtrahend = (-2054847098);
372 Status = SafeIntnSub (Minuend, Subtrahend, &Result);
373 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
374 }
375
376 TEST(MultiplicationTestSuite, TestSafeUintnMult) {
377 RETURN_STATUS Status;
378 UINTN Multiplicand;
379 UINTN Multiplier;
380 UINTN Result;
381
382 //
383 // If the result of multiplication doesn't overflow MAX_UINTN, it will succeed
384 //
385 Multiplicand = 0xa122a;
386 Multiplier = 0xd23;
387 Result = 0;
388 Status = SafeUintnMult (Multiplicand, Multiplier, &Result);
389 ASSERT_EQ (Status, RETURN_SUCCESS);
390 ASSERT_EQ (0x844c9dbe, Result);
391
392 //
393 // Otherwise should result in an error status
394 //
395 Multiplicand = 0xa122a;
396 Multiplier = 0xed23;
397 Status = SafeUintnMult (Multiplicand, Multiplier, &Result);
398 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
399 }
400
401 TEST(MultiplicationTestSuite, TestSafeIntnMult) {
402 RETURN_STATUS Status;
403 INTN Multiplicand;
404 INTN Multiplier;
405 INTN Result;
406
407 //
408 // If the result of multiplication doesn't overflow MAX_INTN and doesn't
409 // underflow MIN_UINTN, it will succeed
410 //
411 Multiplicand = 0x123456;
412 Multiplier = 0x678;
413 Result = 0;
414 Status = SafeIntnMult (Multiplicand, Multiplier, &Result);
415 ASSERT_EQ (Status, RETURN_SUCCESS);
416 ASSERT_EQ (0x75c28c50, Result);
417
418 //
419 // Otherwise should result in an error status
420 //
421 Multiplicand = 0x123456;
422 Multiplier = 0xabc;
423 Status = SafeIntnMult (Multiplicand, Multiplier, &Result);
424 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
425 }