SafeIntLibUintnIntnUnitTests32.cpp 11 KB

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