TestBaseSafeIntLib.cpp 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274
  1. /** @file
  2. UEFI OS based application for unit testing the SafeIntLib.
  3. Copyright (c) Microsoft Corporation.<BR>
  4. Copyright (c) 2018 - 2022, Intel Corporation. All rights reserved.<BR>
  5. SPDX-License-Identifier: BSD-2-Clause-Patent
  6. **/
  7. #include <gtest/gtest.h>
  8. extern "C" {
  9. #include <Base.h>
  10. #include <Library/SafeIntLib.h>
  11. }
  12. //
  13. // Conversion function tests:
  14. //
  15. TEST(ConversionTestSuite, TestSafeInt8ToUint8) {
  16. RETURN_STATUS Status;
  17. INT8 Operand;
  18. UINT8 Result;
  19. //
  20. // Positive UINT8 should result in just a cast
  21. //
  22. Operand = 0x5b;
  23. Result = 0;
  24. Status = SafeInt8ToUint8 (Operand, &Result);
  25. ASSERT_EQ (Status, RETURN_SUCCESS);
  26. ASSERT_EQ (0x5b, Result);
  27. //
  28. // Negative number should result in an error status
  29. //
  30. Operand = (-56);
  31. Status = SafeInt8ToUint8 (Operand, &Result);
  32. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  33. }
  34. TEST(ConversionTestSuite, TestSafeInt8ToUint16) {
  35. RETURN_STATUS Status;
  36. INT8 Operand;
  37. UINT16 Result;
  38. //
  39. // Positive UINT8 should result in just a cast
  40. //
  41. Operand = 0x5b;
  42. Result = 0;
  43. Status = SafeInt8ToUint16 (Operand, &Result);
  44. ASSERT_EQ (Status, RETURN_SUCCESS);
  45. ASSERT_EQ (0x5b, Result);
  46. //
  47. // Negative number should result in an error status
  48. //
  49. Operand = (-56);
  50. Status = SafeInt8ToUint16 (Operand, &Result);
  51. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  52. }
  53. TEST(ConversionTestSuite, TestSafeInt8ToUint32) {
  54. RETURN_STATUS Status;
  55. INT8 Operand;
  56. UINT32 Result;
  57. //
  58. // Positive UINT8 should result in just a cast
  59. //
  60. Operand = 0x5b;
  61. Result = 0;
  62. Status = SafeInt8ToUint32 (Operand, &Result);
  63. ASSERT_EQ (Status, RETURN_SUCCESS);
  64. ASSERT_EQ ((UINT32)0x5b, Result);
  65. //
  66. // Negative number should result in an error status
  67. //
  68. Operand = (-56);
  69. Status = SafeInt8ToUint32 (Operand, &Result);
  70. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  71. }
  72. TEST(ConversionTestSuite, TestSafeInt8ToUintn) {
  73. RETURN_STATUS Status;
  74. INT8 Operand;
  75. UINTN Result;
  76. //
  77. // Positive UINT8 should result in just a cast
  78. //
  79. Operand = 0x5b;
  80. Result = 0;
  81. Status = SafeInt8ToUintn (Operand, &Result);
  82. ASSERT_EQ (Status, RETURN_SUCCESS);
  83. ASSERT_EQ ((UINTN)0x5b, Result);
  84. //
  85. // Negative number should result in an error status
  86. //
  87. Operand = (-56);
  88. Status = SafeInt8ToUintn (Operand, &Result);
  89. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  90. }
  91. TEST(ConversionTestSuite, TestSafeInt8ToUint64) {
  92. RETURN_STATUS Status;
  93. INT8 Operand;
  94. UINT64 Result;
  95. //
  96. // Positive UINT8 should result in just a cast
  97. //
  98. Operand = 0x5b;
  99. Result = 0;
  100. Status = SafeInt8ToUint64 (Operand, &Result);
  101. ASSERT_EQ (Status, RETURN_SUCCESS);
  102. ASSERT_EQ ((UINT64)0x5b, Result);
  103. //
  104. // Negative number should result in an error status
  105. //
  106. Operand = (-56);
  107. Status = SafeInt8ToUint64 (Operand, &Result);
  108. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  109. }
  110. TEST(ConversionTestSuite, TestSafeUint8ToInt8) {
  111. RETURN_STATUS Status;
  112. UINT8 Operand;
  113. INT8 Result;
  114. //
  115. // Operand <= 0x7F (MAX_INT8) should result in a cast
  116. //
  117. Operand = 0x5b;
  118. Result = 0;
  119. Status = SafeUint8ToInt8 (Operand, &Result);
  120. ASSERT_EQ (Status, RETURN_SUCCESS);
  121. ASSERT_EQ (0x5b, Result);
  122. //
  123. // Operand larger than 0x7f should result in an error status
  124. //
  125. Operand = 0xaf;
  126. Status = SafeUint8ToInt8 (Operand, &Result);
  127. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  128. }
  129. TEST(ConversionTestSuite, TestSafeUint8ToChar8) {
  130. RETURN_STATUS Status;
  131. UINT8 Operand;
  132. CHAR8 Result;
  133. //
  134. // CHAR8 is typedefed as char, which by default is signed, thus
  135. // CHAR8 is same as INT8, so same tests as above:
  136. //
  137. //
  138. // Operand <= 0x7F (MAX_INT8) should result in a cast
  139. //
  140. Operand = 0x5b;
  141. Result = 0;
  142. Status = SafeUint8ToChar8 (Operand, &Result);
  143. ASSERT_EQ (Status, RETURN_SUCCESS);
  144. ASSERT_EQ (0x5b, Result);
  145. //
  146. // Operand larger than 0x7f should result in an error status
  147. //
  148. Operand = 0xaf;
  149. Status = SafeUint8ToChar8 (Operand, &Result);
  150. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  151. }
  152. TEST(ConversionTestSuite, TestSafeInt16ToInt8) {
  153. RETURN_STATUS Status;
  154. INT16 Operand;
  155. INT8 Result;
  156. //
  157. // If Operand is between MIN_INT8 and MAX_INT8 inclusive, then it's a cast
  158. //
  159. Operand = 0x5b;
  160. Result = 0;
  161. Status = SafeInt16ToInt8 (Operand, &Result);
  162. ASSERT_EQ (Status, RETURN_SUCCESS);
  163. ASSERT_EQ (0x5b, Result);
  164. Operand = (-35);
  165. Status = SafeInt16ToInt8 (Operand, &Result);
  166. ASSERT_EQ (Status, RETURN_SUCCESS);
  167. ASSERT_EQ ((-35), Result);
  168. //
  169. // Otherwise should result in an error status
  170. //
  171. Operand = 0x1234;
  172. Status = SafeInt16ToInt8 (Operand, &Result);
  173. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  174. Operand = (-17835);
  175. Status = SafeInt16ToInt8 (Operand, &Result);
  176. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  177. }
  178. TEST(ConversionTestSuite, TestSafeInt16ToChar8) {
  179. RETURN_STATUS Status;
  180. INT16 Operand;
  181. CHAR8 Result;
  182. //
  183. // CHAR8 is typedefed as char, which may be signed or unsigned based
  184. // on the compiler. Thus, for compatibility CHAR8 should be between 0 and MAX_INT8.
  185. //
  186. //
  187. // If Operand is between 0 and MAX_INT8 inclusive, then it's a cast
  188. //
  189. Operand = 0x5b;
  190. Result = 0;
  191. Status = SafeInt16ToChar8 (Operand, &Result);
  192. ASSERT_EQ (Status, RETURN_SUCCESS);
  193. ASSERT_EQ (0x5b, Result);
  194. Operand = 0;
  195. Result = 0;
  196. Status = SafeInt16ToChar8 (Operand, &Result);
  197. ASSERT_EQ (Status, RETURN_SUCCESS);
  198. ASSERT_EQ (0, Result);
  199. Operand = MAX_INT8;
  200. Result = 0;
  201. Status = SafeInt16ToChar8 (Operand, &Result);
  202. ASSERT_EQ (Status, RETURN_SUCCESS);
  203. ASSERT_EQ (MAX_INT8, Result);
  204. //
  205. // Otherwise should result in an error status
  206. //
  207. Operand = (-35);
  208. Status = SafeInt16ToChar8 (Operand, &Result);
  209. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  210. Operand = 0x1234;
  211. Status = SafeInt16ToChar8 (Operand, &Result);
  212. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  213. Operand = (-17835);
  214. Status = SafeInt16ToChar8 (Operand, &Result);
  215. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  216. }
  217. TEST(ConversionTestSuite, TestSafeInt16ToUint8) {
  218. RETURN_STATUS Status;
  219. INT16 Operand;
  220. UINT8 Result;
  221. //
  222. // If Operand is between 0 and MAX_INT8 inclusive, then it's a cast
  223. //
  224. Operand = 0x5b;
  225. Result = 0;
  226. Status = SafeInt16ToUint8 (Operand, &Result);
  227. ASSERT_EQ (Status, RETURN_SUCCESS);
  228. ASSERT_EQ (0x5b, Result);
  229. //
  230. // Otherwise should result in an error status
  231. //
  232. Operand = 0x1234;
  233. Status = SafeInt16ToUint8 (Operand, &Result);
  234. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  235. Operand = (-17835);
  236. Status = SafeInt16ToUint8 (Operand, &Result);
  237. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  238. }
  239. TEST(ConversionTestSuite, TestSafeInt16ToUint16) {
  240. RETURN_STATUS Status;
  241. INT16 Operand = 0x5b5b;
  242. UINT16 Result = 0;
  243. //
  244. // If Operand is non-negative, then it's a cast
  245. //
  246. Status = SafeInt16ToUint16 (Operand, &Result);
  247. ASSERT_EQ (Status, RETURN_SUCCESS);
  248. ASSERT_EQ (0x5b5b, Result);
  249. //
  250. // Otherwise should result in an error status
  251. //
  252. Operand = (-17835);
  253. Status = SafeInt16ToUint16 (Operand, &Result);
  254. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  255. }
  256. TEST(ConversionTestSuite, TestSafeInt16ToUint32) {
  257. RETURN_STATUS Status;
  258. INT16 Operand;
  259. UINT32 Result;
  260. //
  261. // If Operand is non-negative, then it's a cast
  262. //
  263. Operand = 0x5b5b;
  264. Result = 0;
  265. Status = SafeInt16ToUint32 (Operand, &Result);
  266. ASSERT_EQ (Status, RETURN_SUCCESS);
  267. ASSERT_EQ ((UINT32)0x5b5b, Result);
  268. //
  269. // Otherwise should result in an error status
  270. //
  271. Operand = (-17835);
  272. Status = SafeInt16ToUint32 (Operand, &Result);
  273. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  274. }
  275. TEST(ConversionTestSuite, TestSafeInt16ToUintn) {
  276. RETURN_STATUS Status;
  277. INT16 Operand;
  278. UINTN Result;
  279. //
  280. // If Operand is non-negative, then it's a cast
  281. //
  282. Operand = 0x5b5b;
  283. Result = 0;
  284. Status = SafeInt16ToUintn (Operand, &Result);
  285. ASSERT_EQ (Status, RETURN_SUCCESS);
  286. ASSERT_EQ ((UINTN)0x5b5b, Result);
  287. //
  288. // Otherwise should result in an error status
  289. //
  290. Operand = (-17835);
  291. Status = SafeInt16ToUintn (Operand, &Result);
  292. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  293. }
  294. TEST(ConversionTestSuite, TestSafeInt16ToUint64) {
  295. RETURN_STATUS Status;
  296. INT16 Operand;
  297. UINT64 Result;
  298. //
  299. // If Operand is non-negative, then it's a cast
  300. //
  301. Operand = 0x5b5b;
  302. Result = 0;
  303. Status = SafeInt16ToUint64 (Operand, &Result);
  304. ASSERT_EQ (Status, RETURN_SUCCESS);
  305. ASSERT_EQ ((UINT64)0x5b5b, Result);
  306. //
  307. // Otherwise should result in an error status
  308. //
  309. Operand = (-17835);
  310. Status = SafeInt16ToUint64 (Operand, &Result);
  311. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  312. }
  313. TEST(ConversionTestSuite, TestSafeUint16ToInt8) {
  314. RETURN_STATUS Status;
  315. UINT16 Operand;
  316. INT8 Result;
  317. //
  318. // If Operand is <= MAX_INT8, it's a cast
  319. //
  320. Operand = 0x5b;
  321. Result = 0;
  322. Status = SafeUint16ToInt8 (Operand, &Result);
  323. ASSERT_EQ (Status, RETURN_SUCCESS);
  324. ASSERT_EQ (0x5b, Result);
  325. //
  326. // Otherwise should result in an error status
  327. //
  328. Operand = (0x5b5b);
  329. Status = SafeUint16ToInt8 (Operand, &Result);
  330. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  331. }
  332. TEST(ConversionTestSuite, TestSafeUint16ToChar8) {
  333. RETURN_STATUS Status;
  334. UINT16 Operand;
  335. CHAR8 Result;
  336. // CHAR8 is typedefed as char, which by default is signed, thus
  337. // CHAR8 is same as INT8, so same tests as above:
  338. //
  339. // If Operand is <= MAX_INT8, it's a cast
  340. //
  341. Operand = 0x5b;
  342. Result = 0;
  343. Status = SafeUint16ToChar8 (Operand, &Result);
  344. ASSERT_EQ (Status, RETURN_SUCCESS);
  345. ASSERT_EQ (0x5b, Result);
  346. //
  347. // Otherwise should result in an error status
  348. //
  349. Operand = (0x5b5b);
  350. Status = SafeUint16ToChar8 (Operand, &Result);
  351. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  352. }
  353. TEST(ConversionTestSuite, TestSafeUint16ToUint8) {
  354. RETURN_STATUS Status;
  355. UINT16 Operand;
  356. UINT8 Result;
  357. //
  358. // If Operand is <= MAX_UINT8 (0xff), it's a cast
  359. //
  360. Operand = 0xab;
  361. Result = 0;
  362. Status = SafeUint16ToUint8 (Operand, &Result);
  363. ASSERT_EQ (Status, RETURN_SUCCESS);
  364. ASSERT_EQ (0xab, Result);
  365. //
  366. // Otherwise should result in an error status
  367. //
  368. Operand = (0x5b5b);
  369. Status = SafeUint16ToUint8 (Operand, &Result);
  370. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  371. }
  372. TEST(ConversionTestSuite, TestSafeUint16ToInt16) {
  373. RETURN_STATUS Status;
  374. UINT16 Operand;
  375. INT16 Result;
  376. //
  377. // If Operand is <= MAX_INT16 (0x7fff), it's a cast
  378. //
  379. Operand = 0x5b5b;
  380. Result = 0;
  381. Status = SafeUint16ToInt16 (Operand, &Result);
  382. ASSERT_EQ (Status, RETURN_SUCCESS);
  383. ASSERT_EQ (0x5b5b, Result);
  384. //
  385. // Otherwise should result in an error status
  386. //
  387. Operand = (0xabab);
  388. Status = SafeUint16ToInt16 (Operand, &Result);
  389. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  390. }
  391. TEST(ConversionTestSuite, TestSafeInt32ToInt8) {
  392. RETURN_STATUS Status;
  393. INT32 Operand;
  394. INT8 Result;
  395. //
  396. // If Operand is between MIN_INT8 and MAX_INT8 inclusive, then it's a cast
  397. //
  398. Operand = 0x5b;
  399. Result = 0;
  400. Status = SafeInt32ToInt8 (Operand, &Result);
  401. ASSERT_EQ (Status, RETURN_SUCCESS);
  402. ASSERT_EQ (0x5b, Result);
  403. Operand = (-57);
  404. Status = SafeInt32ToInt8 (Operand, &Result);
  405. ASSERT_EQ (Status, RETURN_SUCCESS);
  406. ASSERT_EQ ((-57), Result);
  407. //
  408. // Otherwise should result in an error status
  409. //
  410. Operand = (0x5bababab);
  411. Status = SafeInt32ToInt8 (Operand, &Result);
  412. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  413. Operand = (-1537977259);
  414. Status = SafeInt32ToInt8 (Operand, &Result);
  415. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  416. }
  417. TEST(ConversionTestSuite, TestSafeInt32ToChar8) {
  418. RETURN_STATUS Status;
  419. INT32 Operand;
  420. CHAR8 Result;
  421. //
  422. // CHAR8 is typedefed as char, which may be signed or unsigned based
  423. // on the compiler. Thus, for compatibility CHAR8 should be between 0 and MAX_INT8.
  424. //
  425. //
  426. // If Operand is between 0 and MAX_INT8 inclusive, then it's a cast
  427. //
  428. Operand = 0x5b;
  429. Result = 0;
  430. Status = SafeInt32ToChar8 (Operand, &Result);
  431. ASSERT_EQ (Status, RETURN_SUCCESS);
  432. ASSERT_EQ (0x5b, Result);
  433. Operand = 0;
  434. Result = 0;
  435. Status = SafeInt32ToChar8 (Operand, &Result);
  436. ASSERT_EQ (Status, RETURN_SUCCESS);
  437. ASSERT_EQ (0, Result);
  438. Operand = MAX_INT8;
  439. Result = 0;
  440. Status = SafeInt32ToChar8 (Operand, &Result);
  441. ASSERT_EQ (Status, RETURN_SUCCESS);
  442. ASSERT_EQ (MAX_INT8, Result);
  443. //
  444. // Otherwise should result in an error status
  445. //
  446. Operand = (-57);
  447. Status = SafeInt32ToChar8 (Operand, &Result);
  448. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  449. Operand = (0x5bababab);
  450. Status = SafeInt32ToChar8 (Operand, &Result);
  451. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  452. Operand = (-1537977259);
  453. Status = SafeInt32ToChar8 (Operand, &Result);
  454. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  455. }
  456. TEST(ConversionTestSuite, TestSafeInt32ToUint8) {
  457. RETURN_STATUS Status;
  458. INT32 Operand;
  459. UINT8 Result;
  460. //
  461. // If Operand is between 0 and MAX_INT8 inclusive, then it's a cast
  462. //
  463. Operand = 0x5b;
  464. Result = 0;
  465. Status = SafeInt32ToUint8 (Operand, &Result);
  466. ASSERT_EQ (Status, RETURN_SUCCESS);
  467. ASSERT_EQ (0x5b, Result);
  468. //
  469. // Otherwise should result in an error status
  470. //
  471. Operand = (-57);
  472. Status = SafeInt32ToUint8 (Operand, &Result);
  473. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  474. Operand = (0x5bababab);
  475. Status = SafeInt32ToUint8 (Operand, &Result);
  476. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  477. Operand = (-1537977259);
  478. Status = SafeInt32ToUint8 (Operand, &Result);
  479. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  480. }
  481. TEST(ConversionTestSuite, TestSafeInt32ToInt16) {
  482. RETURN_STATUS Status;
  483. INT32 Operand;
  484. INT16 Result;
  485. //
  486. // If Operand is between MIN_INT16 and MAX_INT16 inclusive, then it's a cast
  487. //
  488. Operand = 0x5b5b;
  489. Result = 0;
  490. Status = SafeInt32ToInt16 (Operand, &Result);
  491. ASSERT_EQ (Status, RETURN_SUCCESS);
  492. ASSERT_EQ (0x5b5b, Result);
  493. Operand = (-17857);
  494. Status = SafeInt32ToInt16 (Operand, &Result);
  495. ASSERT_EQ (Status, RETURN_SUCCESS);
  496. ASSERT_EQ ((-17857), Result);
  497. //
  498. // Otherwise should result in an error status
  499. //
  500. Operand = (0x5bababab);
  501. Status = SafeInt32ToInt16 (Operand, &Result);
  502. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  503. Operand = (-1537977259);
  504. Status = SafeInt32ToInt16 (Operand, &Result);
  505. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  506. }
  507. TEST(ConversionTestSuite, TestSafeInt32ToUint16) {
  508. RETURN_STATUS Status;
  509. INT32 Operand;
  510. UINT16 Result;
  511. //
  512. // If Operand is between 0 and MAX_UINT16 inclusive, then it's a cast
  513. //
  514. Operand = 0xabab;
  515. Result = 0;
  516. Status = SafeInt32ToUint16 (Operand, &Result);
  517. ASSERT_EQ (Status, RETURN_SUCCESS);
  518. ASSERT_EQ (0xabab, Result);
  519. //
  520. // Otherwise should result in an error status
  521. //
  522. Operand = (-17857);
  523. Status = SafeInt32ToUint16 (Operand, &Result);
  524. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  525. Operand = (0x5bababab);
  526. Status = SafeInt32ToUint16 (Operand, &Result);
  527. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  528. Operand = (-1537977259);
  529. Status = SafeInt32ToUint16 (Operand, &Result);
  530. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  531. }
  532. TEST(ConversionTestSuite, TestSafeInt32ToUint32) {
  533. RETURN_STATUS Status;
  534. INT32 Operand;
  535. UINT32 Result;
  536. //
  537. // If Operand is non-negative, then it's a cast
  538. //
  539. Operand = 0x5bababab;
  540. Result = 0;
  541. Status = SafeInt32ToUint32 (Operand, &Result);
  542. ASSERT_EQ (Status, RETURN_SUCCESS);
  543. ASSERT_EQ ((UINT32)0x5bababab, Result);
  544. //
  545. // Otherwise should result in an error status
  546. //
  547. Operand = (-1537977259);
  548. Status = SafeInt32ToUint32 (Operand, &Result);
  549. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  550. }
  551. TEST(ConversionTestSuite, TestSafeInt32ToUint64) {
  552. RETURN_STATUS Status;
  553. INT32 Operand;
  554. UINT64 Result;
  555. //
  556. // If Operand is non-negative, then it's a cast
  557. //
  558. Operand = 0x5bababab;
  559. Result = 0;
  560. Status = SafeInt32ToUint64 (Operand, &Result);
  561. ASSERT_EQ (Status, RETURN_SUCCESS);
  562. ASSERT_EQ ((UINT64)0x5bababab, Result);
  563. //
  564. // Otherwise should result in an error status
  565. //
  566. Operand = (-1537977259);
  567. Status = SafeInt32ToUint64 (Operand, &Result);
  568. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  569. }
  570. TEST(ConversionTestSuite, TestSafeUint32ToInt8) {
  571. RETURN_STATUS Status;
  572. UINT32 Operand;
  573. INT8 Result;
  574. //
  575. // If Operand is <= MAX_INT8, then it's a cast
  576. //
  577. Operand = 0x5b;
  578. Result = 0;
  579. Status = SafeUint32ToInt8 (Operand, &Result);
  580. ASSERT_EQ (Status, RETURN_SUCCESS);
  581. ASSERT_EQ (0x5b, Result);
  582. //
  583. // Otherwise should result in an error status
  584. //
  585. Operand = (0x5bababab);
  586. Status = SafeUint32ToInt8 (Operand, &Result);
  587. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  588. }
  589. TEST(ConversionTestSuite, TestSafeUint32ToChar8) {
  590. RETURN_STATUS Status;
  591. UINT32 Operand;
  592. CHAR8 Result;
  593. // CHAR8 is typedefed as char, which by default is signed, thus
  594. // CHAR8 is same as INT8, so same tests as above:
  595. //
  596. // If Operand is <= MAX_INT8, then it's a cast
  597. //
  598. Operand = 0x5b;
  599. Result = 0;
  600. Status = SafeUint32ToChar8 (Operand, &Result);
  601. ASSERT_EQ (Status, RETURN_SUCCESS);
  602. ASSERT_EQ (0x5b, Result);
  603. //
  604. // Otherwise should result in an error status
  605. //
  606. Operand = (0x5bababab);
  607. Status = SafeUint32ToChar8 (Operand, &Result);
  608. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  609. }
  610. TEST(ConversionTestSuite, TestSafeUint32ToUint8) {
  611. RETURN_STATUS Status;
  612. UINT32 Operand;
  613. UINT8 Result;
  614. //
  615. // If Operand is <= MAX_UINT8, then it's a cast
  616. //
  617. Operand = 0xab;
  618. Result = 0;
  619. Status = SafeUint32ToUint8 (Operand, &Result);
  620. ASSERT_EQ (Status, RETURN_SUCCESS);
  621. ASSERT_EQ (0xab, Result);
  622. //
  623. // Otherwise should result in an error status
  624. //
  625. Operand = (0xabababab);
  626. Status = SafeUint32ToUint8 (Operand, &Result);
  627. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  628. }
  629. TEST(ConversionTestSuite, TestSafeUint32ToInt16) {
  630. RETURN_STATUS Status;
  631. UINT32 Operand;
  632. INT16 Result;
  633. //
  634. // If Operand is <= MAX_INT16, then it's a cast
  635. //
  636. Operand = 0x5bab;
  637. Result = 0;
  638. Status = SafeUint32ToInt16 (Operand, &Result);
  639. ASSERT_EQ (Status, RETURN_SUCCESS);
  640. ASSERT_EQ (0x5bab, Result);
  641. //
  642. // Otherwise should result in an error status
  643. //
  644. Operand = (0xabababab);
  645. Status = SafeUint32ToInt16 (Operand, &Result);
  646. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  647. }
  648. TEST(ConversionTestSuite, TestSafeUint32ToUint16) {
  649. RETURN_STATUS Status;
  650. UINT32 Operand;
  651. UINT16 Result;
  652. //
  653. // If Operand is <= MAX_UINT16, then it's a cast
  654. //
  655. Operand = 0xabab;
  656. Result = 0;
  657. Status = SafeUint32ToUint16 (Operand, &Result);
  658. ASSERT_EQ (Status, RETURN_SUCCESS);
  659. ASSERT_EQ (0xabab, Result);
  660. //
  661. // Otherwise should result in an error status
  662. //
  663. Operand = (0xabababab);
  664. Status = SafeUint32ToUint16 (Operand, &Result);
  665. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  666. }
  667. TEST(ConversionTestSuite, TestSafeUint32ToInt32) {
  668. RETURN_STATUS Status;
  669. UINT32 Operand;
  670. INT32 Result;
  671. //
  672. // If Operand is <= MAX_INT32, then it's a cast
  673. //
  674. Operand = 0x5bababab;
  675. Result = 0;
  676. Status = SafeUint32ToInt32 (Operand, &Result);
  677. ASSERT_EQ (Status, RETURN_SUCCESS);
  678. ASSERT_EQ (0x5bababab, Result);
  679. //
  680. // Otherwise should result in an error status
  681. //
  682. Operand = (0xabababab);
  683. Status = SafeUint32ToInt32 (Operand, &Result);
  684. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  685. }
  686. TEST(ConversionTestSuite, TestSafeIntnToInt8) {
  687. RETURN_STATUS Status;
  688. INTN Operand;
  689. INT8 Result;
  690. //
  691. // If Operand is between MIN_INT8 and MAX_INT8 inclusive, then it's a cast
  692. //
  693. Operand = 0x5b;
  694. Result = 0;
  695. Status = SafeIntnToInt8 (Operand, &Result);
  696. ASSERT_EQ (Status, RETURN_SUCCESS);
  697. ASSERT_EQ (0x5b, Result);
  698. Operand = (-53);
  699. Status = SafeIntnToInt8 (Operand, &Result);
  700. ASSERT_EQ (Status, RETURN_SUCCESS);
  701. ASSERT_EQ ((-53), Result);
  702. //
  703. // Otherwise should result in an error status
  704. //
  705. Operand = (0x5bababab);
  706. Status = SafeIntnToInt8 (Operand, &Result);
  707. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  708. Operand = (-1537977259);
  709. Status = SafeIntnToInt8 (Operand, &Result);
  710. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  711. }
  712. TEST(ConversionTestSuite, TestSafeIntnToChar8) {
  713. RETURN_STATUS Status;
  714. INTN Operand;
  715. CHAR8 Result;
  716. //
  717. // CHAR8 is typedefed as char, which may be signed or unsigned based
  718. // on the compiler. Thus, for compatibility CHAR8 should be between 0 and MAX_INT8.
  719. //
  720. //
  721. // If Operand is between MIN_INT8 and MAX_INT8 inclusive, then it's a cast
  722. //
  723. Operand = 0x5b;
  724. Result = 0;
  725. Status = SafeIntnToChar8 (Operand, &Result);
  726. ASSERT_EQ (Status, RETURN_SUCCESS);
  727. ASSERT_EQ (0x5b, Result);
  728. Operand = 0;
  729. Result = 0;
  730. Status = SafeIntnToChar8 (Operand, &Result);
  731. ASSERT_EQ (Status, RETURN_SUCCESS);
  732. ASSERT_EQ (0, Result);
  733. Operand = MAX_INT8;
  734. Result = 0;
  735. Status = SafeIntnToChar8 (Operand, &Result);
  736. ASSERT_EQ (Status, RETURN_SUCCESS);
  737. ASSERT_EQ (MAX_INT8, Result);
  738. //
  739. // Otherwise should result in an error status
  740. //
  741. Operand = (-53);
  742. Status = SafeIntnToChar8 (Operand, &Result);
  743. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  744. Operand = (0x5bababab);
  745. Status = SafeIntnToChar8 (Operand, &Result);
  746. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  747. Operand = (-1537977259);
  748. Status = SafeIntnToChar8 (Operand, &Result);
  749. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  750. }
  751. TEST(ConversionTestSuite, TestSafeIntnToUint8) {
  752. RETURN_STATUS Status;
  753. INTN Operand;
  754. UINT8 Result;
  755. //
  756. // If Operand is between 0 and MAX_UINT8 inclusive, then it's a cast
  757. //
  758. Operand = 0xab;
  759. Result = 0;
  760. Status = SafeIntnToUint8 (Operand, &Result);
  761. ASSERT_EQ (Status, RETURN_SUCCESS);
  762. ASSERT_EQ (0xab, Result);
  763. //
  764. // Otherwise should result in an error status
  765. //
  766. Operand = (0x5bababab);
  767. Status = SafeIntnToUint8 (Operand, &Result);
  768. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  769. Operand = (-1537977259);
  770. Status = SafeIntnToUint8 (Operand, &Result);
  771. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  772. }
  773. TEST(ConversionTestSuite, TestSafeIntnToInt16) {
  774. RETURN_STATUS Status;
  775. INTN Operand;
  776. INT16 Result;
  777. //
  778. // If Operand is between MIN_INT16 and MAX_INT16 inclusive, then it's a cast
  779. //
  780. Operand = 0x5bab;
  781. Result = 0;
  782. Status = SafeIntnToInt16 (Operand, &Result);
  783. ASSERT_EQ (Status, RETURN_SUCCESS);
  784. ASSERT_EQ (0x5bab, Result);
  785. Operand = (-23467);
  786. Status = SafeIntnToInt16 (Operand, &Result);
  787. ASSERT_EQ (Status, RETURN_SUCCESS);
  788. ASSERT_EQ ((-23467), Result);
  789. //
  790. // Otherwise should result in an error status
  791. //
  792. Operand = (0x5bababab);
  793. Status = SafeIntnToInt16 (Operand, &Result);
  794. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  795. Operand = (-1537977259);
  796. Status = SafeIntnToInt16 (Operand, &Result);
  797. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  798. }
  799. TEST(ConversionTestSuite, TestSafeIntnToUint16) {
  800. RETURN_STATUS Status;
  801. INTN Operand;
  802. UINT16 Result;
  803. //
  804. // If Operand is between 0 and MAX_UINT16 inclusive, then it's a cast
  805. //
  806. Operand = 0xabab;
  807. Result = 0;
  808. Status = SafeIntnToUint16 (Operand, &Result);
  809. ASSERT_EQ (Status, RETURN_SUCCESS);
  810. ASSERT_EQ (0xabab, Result);
  811. //
  812. // Otherwise should result in an error status
  813. //
  814. Operand = (0x5bababab);
  815. Status = SafeIntnToUint16 (Operand, &Result);
  816. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  817. Operand = (-1537977259);
  818. Status = SafeIntnToUint16 (Operand, &Result);
  819. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  820. }
  821. TEST(ConversionTestSuite, TestSafeIntnToUintn) {
  822. RETURN_STATUS Status;
  823. INTN Operand;
  824. UINTN Result;
  825. //
  826. // If Operand is non-negative, then it's a cast
  827. //
  828. Operand = 0x5bababab;
  829. Result = 0;
  830. Status = SafeIntnToUintn (Operand, &Result);
  831. ASSERT_EQ (Status, RETURN_SUCCESS);
  832. ASSERT_EQ ((UINTN)0x5bababab, Result);
  833. //
  834. // Otherwise should result in an error status
  835. //
  836. Operand = (-1537977259);
  837. Status = SafeIntnToUintn (Operand, &Result);
  838. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  839. }
  840. TEST(ConversionTestSuite, TestSafeIntnToUint64) {
  841. RETURN_STATUS Status;
  842. INTN Operand;
  843. UINT64 Result;
  844. //
  845. // If Operand is non-negative, then it's a cast
  846. //
  847. Operand = 0x5bababab;
  848. Result = 0;
  849. Status = SafeIntnToUint64 (Operand, &Result);
  850. ASSERT_EQ (Status, RETURN_SUCCESS);
  851. ASSERT_EQ ((UINT64)0x5bababab, Result);
  852. //
  853. // Otherwise should result in an error status
  854. //
  855. Operand = (-1537977259);
  856. Status = SafeIntnToUint64 (Operand, &Result);
  857. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  858. }
  859. TEST(ConversionTestSuite, TestSafeUintnToInt8) {
  860. RETURN_STATUS Status;
  861. UINTN Operand;
  862. INT8 Result;
  863. //
  864. // If Operand is <= MAX_INT8, then it's a cast
  865. //
  866. Operand = 0x5b;
  867. Result = 0;
  868. Status = SafeUintnToInt8 (Operand, &Result);
  869. ASSERT_EQ (Status, RETURN_SUCCESS);
  870. ASSERT_EQ (0x5b, Result);
  871. //
  872. // Otherwise should result in an error status
  873. //
  874. Operand = (0xabab);
  875. Status = SafeUintnToInt8 (Operand, &Result);
  876. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  877. }
  878. TEST(ConversionTestSuite, TestSafeUintnToChar8) {
  879. RETURN_STATUS Status;
  880. UINTN Operand;
  881. CHAR8 Result;
  882. // CHAR8 is typedefed as char, which by default is signed, thus
  883. // CHAR8 is same as INT8, so same tests as above:
  884. //
  885. // If Operand is <= MAX_INT8, then it's a cast
  886. //
  887. Operand = 0x5b;
  888. Result = 0;
  889. Status = SafeUintnToChar8 (Operand, &Result);
  890. ASSERT_EQ (Status, RETURN_SUCCESS);
  891. ASSERT_EQ (0x5b, Result);
  892. //
  893. // Otherwise should result in an error status
  894. //
  895. Operand = (0xabab);
  896. Status = SafeUintnToChar8 (Operand, &Result);
  897. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  898. }
  899. TEST(ConversionTestSuite, TestSafeUintnToUint8) {
  900. RETURN_STATUS Status;
  901. UINTN Operand;
  902. UINT8 Result;
  903. //
  904. // If Operand is <= MAX_UINT8, then it's a cast
  905. //
  906. Operand = 0xab;
  907. Result = 0;
  908. Status = SafeUintnToUint8 (Operand, &Result);
  909. ASSERT_EQ (Status, RETURN_SUCCESS);
  910. ASSERT_EQ (0xab, Result);
  911. //
  912. // Otherwise should result in an error status
  913. //
  914. Operand = (0xabab);
  915. Status = SafeUintnToUint8 (Operand, &Result);
  916. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  917. }
  918. TEST(ConversionTestSuite, TestSafeUintnToInt16) {
  919. RETURN_STATUS Status;
  920. UINTN Operand;
  921. INT16 Result;
  922. //
  923. // If Operand is <= MAX_INT16, then it's a cast
  924. //
  925. Operand = 0x5bab;
  926. Result = 0;
  927. Status = SafeUintnToInt16 (Operand, &Result);
  928. ASSERT_EQ (Status, RETURN_SUCCESS);
  929. ASSERT_EQ (0x5bab, Result);
  930. //
  931. // Otherwise should result in an error status
  932. //
  933. Operand = (0xabab);
  934. Status = SafeUintnToInt16 (Operand, &Result);
  935. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  936. }
  937. TEST(ConversionTestSuite, TestSafeUintnToUint16) {
  938. RETURN_STATUS Status;
  939. UINTN Operand;
  940. UINT16 Result;
  941. //
  942. // If Operand is <= MAX_UINT16, then it's a cast
  943. //
  944. Operand = 0xabab;
  945. Result = 0;
  946. Status = SafeUintnToUint16 (Operand, &Result);
  947. ASSERT_EQ (Status, RETURN_SUCCESS);
  948. ASSERT_EQ (0xabab, Result);
  949. //
  950. // Otherwise should result in an error status
  951. //
  952. Operand = (0xabababab);
  953. Status = SafeUintnToUint16 (Operand, &Result);
  954. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  955. }
  956. TEST(ConversionTestSuite, TestSafeUintnToInt32) {
  957. RETURN_STATUS Status;
  958. UINTN Operand;
  959. INT32 Result;
  960. //
  961. // If Operand is <= MAX_INT32, then it's a cast
  962. //
  963. Operand = 0x5bababab;
  964. Result = 0;
  965. Status = SafeUintnToInt32 (Operand, &Result);
  966. ASSERT_EQ (Status, RETURN_SUCCESS);
  967. ASSERT_EQ (0x5bababab, Result);
  968. //
  969. // Otherwise should result in an error status
  970. //
  971. Operand = (0xabababab);
  972. Status = SafeUintnToInt32 (Operand, &Result);
  973. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  974. }
  975. TEST(ConversionTestSuite, TestSafeInt64ToInt8) {
  976. RETURN_STATUS Status;
  977. INT64 Operand;
  978. INT8 Result;
  979. //
  980. // If Operand is between MIN_INT8 and MAX_INT8 inclusive, then it's a cast
  981. //
  982. Operand = 0x5b;
  983. Result = 0;
  984. Status = SafeInt64ToInt8 (Operand, &Result);
  985. ASSERT_EQ (Status, RETURN_SUCCESS);
  986. ASSERT_EQ (0x5b, Result);
  987. Operand = (-37);
  988. Status = SafeInt64ToInt8 (Operand, &Result);
  989. ASSERT_EQ (Status, RETURN_SUCCESS);
  990. ASSERT_EQ ((-37), Result);
  991. //
  992. // Otherwise should result in an error status
  993. //
  994. Operand = (0x5babababefefefef);
  995. Status = SafeInt64ToInt8 (Operand, &Result);
  996. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  997. Operand = (-6605562033422200815);
  998. Status = SafeInt64ToInt8 (Operand, &Result);
  999. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  1000. }
  1001. TEST(ConversionTestSuite, TestSafeInt64ToChar8) {
  1002. RETURN_STATUS Status;
  1003. INT64 Operand;
  1004. CHAR8 Result;
  1005. //
  1006. // CHAR8 is typedefed as char, which may be signed or unsigned based
  1007. // on the compiler. Thus, for compatibility CHAR8 should be between 0 and MAX_INT8.
  1008. //
  1009. //
  1010. // If Operand is between MIN_INT8 and MAX_INT8 inclusive, then it's a cast
  1011. //
  1012. Operand = 0x5b;
  1013. Result = 0;
  1014. Status = SafeInt64ToChar8 (Operand, &Result);
  1015. ASSERT_EQ (Status, RETURN_SUCCESS);
  1016. ASSERT_EQ (0x5b, Result);
  1017. Operand = 0;
  1018. Result = 0;
  1019. Status = SafeInt64ToChar8 (Operand, &Result);
  1020. ASSERT_EQ (Status, RETURN_SUCCESS);
  1021. ASSERT_EQ (0, Result);
  1022. Operand = MAX_INT8;
  1023. Result = 0;
  1024. Status = SafeInt64ToChar8 (Operand, &Result);
  1025. ASSERT_EQ (Status, RETURN_SUCCESS);
  1026. ASSERT_EQ (MAX_INT8, Result);
  1027. //
  1028. // Otherwise should result in an error status
  1029. //
  1030. Operand = (-37);
  1031. Status = SafeInt64ToChar8 (Operand, &Result);
  1032. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  1033. Operand = (0x5babababefefefef);
  1034. Status = SafeInt64ToChar8 (Operand, &Result);
  1035. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  1036. Operand = (-6605562033422200815);
  1037. Status = SafeInt64ToChar8 (Operand, &Result);
  1038. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  1039. }
  1040. TEST(ConversionTestSuite, TestSafeInt64ToUint8) {
  1041. RETURN_STATUS Status;
  1042. INT64 Operand;
  1043. UINT8 Result;
  1044. //
  1045. // If Operand is between 0 and MAX_UINT8 inclusive, then it's a cast
  1046. //
  1047. Operand = 0xab;
  1048. Result = 0;
  1049. Status = SafeInt64ToUint8 (Operand, &Result);
  1050. ASSERT_EQ (Status, RETURN_SUCCESS);
  1051. ASSERT_EQ (0xab, Result);
  1052. //
  1053. // Otherwise should result in an error status
  1054. //
  1055. Operand = (0x5babababefefefef);
  1056. Status = SafeInt64ToUint8 (Operand, &Result);
  1057. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  1058. Operand = (-6605562033422200815);
  1059. Status = SafeInt64ToUint8 (Operand, &Result);
  1060. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  1061. }
  1062. TEST(ConversionTestSuite, TestSafeInt64ToInt16) {
  1063. RETURN_STATUS Status;
  1064. INT64 Operand;
  1065. INT16 Result;
  1066. //
  1067. // If Operand is between MIN_INT16 and MAX_INT16 inclusive, then it's a cast
  1068. //
  1069. Operand = 0x5bab;
  1070. Result = 0;
  1071. Status = SafeInt64ToInt16 (Operand, &Result);
  1072. ASSERT_EQ (Status, RETURN_SUCCESS);
  1073. ASSERT_EQ (0x5bab, Result);
  1074. Operand = (-23467);
  1075. Status = SafeInt64ToInt16 (Operand, &Result);
  1076. ASSERT_EQ (Status, RETURN_SUCCESS);
  1077. ASSERT_EQ ((-23467), Result);
  1078. //
  1079. // Otherwise should result in an error status
  1080. //
  1081. Operand = (0x5babababefefefef);
  1082. Status = SafeInt64ToInt16 (Operand, &Result);
  1083. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  1084. Operand = (-6605562033422200815);
  1085. Status = SafeInt64ToInt16 (Operand, &Result);
  1086. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  1087. }
  1088. TEST(ConversionTestSuite, TestSafeInt64ToUint16) {
  1089. RETURN_STATUS Status;
  1090. INT64 Operand;
  1091. UINT16 Result;
  1092. //
  1093. // If Operand is between 0 and MAX_UINT16 inclusive, then it's a cast
  1094. //
  1095. Operand = 0xabab;
  1096. Result = 0;
  1097. Status = SafeInt64ToUint16 (Operand, &Result);
  1098. ASSERT_EQ (Status, RETURN_SUCCESS);
  1099. ASSERT_EQ (0xabab, Result);
  1100. //
  1101. // Otherwise should result in an error status
  1102. //
  1103. Operand = (0x5babababefefefef);
  1104. Status = SafeInt64ToUint16 (Operand, &Result);
  1105. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  1106. Operand = (-6605562033422200815);
  1107. Status = SafeInt64ToUint16 (Operand, &Result);
  1108. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  1109. }
  1110. TEST(ConversionTestSuite, TestSafeInt64ToInt32) {
  1111. RETURN_STATUS Status;
  1112. INT64 Operand;
  1113. INT32 Result;
  1114. //
  1115. // If Operand is between MIN_INT32 and MAX_INT32 inclusive, then it's a cast
  1116. //
  1117. Operand = 0x5bababab;
  1118. Result = 0;
  1119. Status = SafeInt64ToInt32 (Operand, &Result);
  1120. ASSERT_EQ (Status, RETURN_SUCCESS);
  1121. ASSERT_EQ (0x5bababab, Result);
  1122. Operand = (-1537977259);
  1123. Status = SafeInt64ToInt32 (Operand, &Result);
  1124. ASSERT_EQ (Status, RETURN_SUCCESS);
  1125. ASSERT_EQ ((-1537977259), Result);
  1126. //
  1127. // Otherwise should result in an error status
  1128. //
  1129. Operand = (0x5babababefefefef);
  1130. Status = SafeInt64ToInt32 (Operand, &Result);
  1131. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  1132. Operand = (-6605562033422200815);
  1133. Status = SafeInt64ToInt32 (Operand, &Result);
  1134. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  1135. }
  1136. TEST(ConversionTestSuite, TestSafeInt64ToUint32) {
  1137. RETURN_STATUS Status;
  1138. INT64 Operand;
  1139. UINT32 Result;
  1140. //
  1141. // If Operand is between 0 and MAX_UINT32 inclusive, then it's a cast
  1142. //
  1143. Operand = 0xabababab;
  1144. Result = 0;
  1145. Status = SafeInt64ToUint32 (Operand, &Result);
  1146. ASSERT_EQ (Status, RETURN_SUCCESS);
  1147. ASSERT_EQ (0xabababab, Result);
  1148. //
  1149. // Otherwise should result in an error status
  1150. //
  1151. Operand = (0x5babababefefefef);
  1152. Status = SafeInt64ToUint32 (Operand, &Result);
  1153. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  1154. Operand = (-6605562033422200815);
  1155. Status = SafeInt64ToUint32 (Operand, &Result);
  1156. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  1157. }
  1158. TEST(ConversionTestSuite, TestSafeInt64ToUint64) {
  1159. RETURN_STATUS Status;
  1160. INT64 Operand;
  1161. UINT64 Result;
  1162. //
  1163. // If Operand is non-negative, then it's a cast
  1164. //
  1165. Operand = 0x5babababefefefef;
  1166. Result = 0;
  1167. Status = SafeInt64ToUint64 (Operand, &Result);
  1168. ASSERT_EQ (Status, RETURN_SUCCESS);
  1169. ASSERT_EQ ((UINT64)0x5babababefefefef, Result);
  1170. //
  1171. // Otherwise should result in an error status
  1172. //
  1173. Operand = (-6605562033422200815);
  1174. Status = SafeInt64ToUint64 (Operand, &Result);
  1175. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  1176. }
  1177. TEST(ConversionTestSuite, TestSafeUint64ToInt8) {
  1178. RETURN_STATUS Status;
  1179. UINT64 Operand;
  1180. INT8 Result;
  1181. //
  1182. // If Operand is <= MAX_INT8, then it's a cast
  1183. //
  1184. Operand = 0x5b;
  1185. Result = 0;
  1186. Status = SafeUint64ToInt8 (Operand, &Result);
  1187. ASSERT_EQ (Status, RETURN_SUCCESS);
  1188. ASSERT_EQ (0x5b, Result);
  1189. //
  1190. // Otherwise should result in an error status
  1191. //
  1192. Operand = (0xababababefefefef);
  1193. Status = SafeUint64ToInt8 (Operand, &Result);
  1194. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  1195. }
  1196. TEST(ConversionTestSuite, TestSafeUint64ToChar8) {
  1197. RETURN_STATUS Status;
  1198. UINT64 Operand;
  1199. CHAR8 Result;
  1200. // CHAR8 is typedefed as char, which by default is signed, thus
  1201. // CHAR8 is same as INT8, so same tests as above:
  1202. //
  1203. // If Operand is <= MAX_INT8, then it's a cast
  1204. //
  1205. Operand = 0x5b;
  1206. Result = 0;
  1207. Status = SafeUint64ToChar8 (Operand, &Result);
  1208. ASSERT_EQ (Status, RETURN_SUCCESS);
  1209. ASSERT_EQ (0x5b, Result);
  1210. //
  1211. // Otherwise should result in an error status
  1212. //
  1213. Operand = (0xababababefefefef);
  1214. Status = SafeUint64ToChar8 (Operand, &Result);
  1215. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  1216. }
  1217. TEST(ConversionTestSuite, TestSafeUint64ToUint8) {
  1218. RETURN_STATUS Status;
  1219. UINT64 Operand;
  1220. UINT8 Result;
  1221. //
  1222. // If Operand is <= MAX_UINT8, then it's a cast
  1223. //
  1224. Operand = 0xab;
  1225. Result = 0;
  1226. Status = SafeUint64ToUint8 (Operand, &Result);
  1227. ASSERT_EQ (Status, RETURN_SUCCESS);
  1228. ASSERT_EQ (0xab, Result);
  1229. //
  1230. // Otherwise should result in an error status
  1231. //
  1232. Operand = (0xababababefefefef);
  1233. Status = SafeUint64ToUint8 (Operand, &Result);
  1234. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  1235. }
  1236. TEST(ConversionTestSuite, TestSafeUint64ToInt16) {
  1237. RETURN_STATUS Status;
  1238. UINT64 Operand;
  1239. INT16 Result;
  1240. //
  1241. // If Operand is <= MAX_INT16, then it's a cast
  1242. //
  1243. Operand = 0x5bab;
  1244. Result = 0;
  1245. Status = SafeUint64ToInt16 (Operand, &Result);
  1246. ASSERT_EQ (Status, RETURN_SUCCESS);
  1247. ASSERT_EQ (0x5bab, Result);
  1248. //
  1249. // Otherwise should result in an error status
  1250. //
  1251. Operand = (0xababababefefefef);
  1252. Status = SafeUint64ToInt16 (Operand, &Result);
  1253. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  1254. }
  1255. TEST(ConversionTestSuite, TestSafeUint64ToUint16) {
  1256. RETURN_STATUS Status;
  1257. UINT64 Operand;
  1258. UINT16 Result;
  1259. //
  1260. // If Operand is <= MAX_UINT16, then it's a cast
  1261. //
  1262. Operand = 0xabab;
  1263. Result = 0;
  1264. Status = SafeUint64ToUint16 (Operand, &Result);
  1265. ASSERT_EQ (Status, RETURN_SUCCESS);
  1266. ASSERT_EQ (0xabab, Result);
  1267. //
  1268. // Otherwise should result in an error status
  1269. //
  1270. Operand = (0xababababefefefef);
  1271. Status = SafeUint64ToUint16 (Operand, &Result);
  1272. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  1273. }
  1274. TEST(ConversionTestSuite, TestSafeUint64ToInt32) {
  1275. RETURN_STATUS Status;
  1276. UINT64 Operand;
  1277. INT32 Result;
  1278. //
  1279. // If Operand is <= MAX_INT32, then it's a cast
  1280. //
  1281. Operand = 0x5bababab;
  1282. Result = 0;
  1283. Status = SafeUint64ToInt32 (Operand, &Result);
  1284. ASSERT_EQ (Status, RETURN_SUCCESS);
  1285. ASSERT_EQ (0x5bababab, Result);
  1286. //
  1287. // Otherwise should result in an error status
  1288. //
  1289. Operand = (0xababababefefefef);
  1290. Status = SafeUint64ToInt32 (Operand, &Result);
  1291. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  1292. }
  1293. TEST(ConversionTestSuite, TestSafeUint64ToUint32) {
  1294. RETURN_STATUS Status;
  1295. UINT64 Operand;
  1296. UINT32 Result;
  1297. //
  1298. // If Operand is <= MAX_UINT32, then it's a cast
  1299. //
  1300. Operand = 0xabababab;
  1301. Result = 0;
  1302. Status = SafeUint64ToUint32 (Operand, &Result);
  1303. ASSERT_EQ (Status, RETURN_SUCCESS);
  1304. ASSERT_EQ (0xabababab, Result);
  1305. //
  1306. // Otherwise should result in an error status
  1307. //
  1308. Operand = (0xababababefefefef);
  1309. Status = SafeUint64ToUint32 (Operand, &Result);
  1310. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  1311. }
  1312. TEST(ConversionTestSuite, TestSafeUint64ToInt64) {
  1313. RETURN_STATUS Status;
  1314. UINT64 Operand;
  1315. INT64 Result;
  1316. //
  1317. // If Operand is <= MAX_INT64, then it's a cast
  1318. //
  1319. Operand = 0x5babababefefefef;
  1320. Result = 0;
  1321. Status = SafeUint64ToInt64 (Operand, &Result);
  1322. ASSERT_EQ (Status, RETURN_SUCCESS);
  1323. ASSERT_EQ (0x5babababefefefef, Result);
  1324. //
  1325. // Otherwise should result in an error status
  1326. //
  1327. Operand = (0xababababefefefef);
  1328. Status = SafeUint64ToInt64 (Operand, &Result);
  1329. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  1330. }
  1331. //
  1332. // Addition function tests:
  1333. //
  1334. TEST(AdditionSubtractionTestSuite, TestSafeUint8Add) {
  1335. RETURN_STATUS Status;
  1336. UINT8 Augend;
  1337. UINT8 Addend;
  1338. UINT8 Result;
  1339. //
  1340. // If the result of addition doesn't overflow MAX_UINT8, then it's addition
  1341. //
  1342. Augend = 0x3a;
  1343. Addend = 0x3a;
  1344. Result = 0;
  1345. Status = SafeUint8Add (Augend, Addend, &Result);
  1346. ASSERT_EQ (Status, RETURN_SUCCESS);
  1347. ASSERT_EQ (0x74, Result);
  1348. //
  1349. // Otherwise should result in an error status
  1350. //
  1351. Augend = 0xab;
  1352. Addend = 0xbc;
  1353. Status = SafeUint8Add (Augend, Addend, &Result);
  1354. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  1355. }
  1356. TEST(AdditionSubtractionTestSuite, TestSafeUint16Add) {
  1357. RETURN_STATUS Status;
  1358. UINT16 Augend = 0x3a3a;
  1359. UINT16 Addend = 0x3a3a;
  1360. UINT16 Result = 0;
  1361. //
  1362. // If the result of addition doesn't overflow MAX_UINT16, then it's addition
  1363. //
  1364. Status = SafeUint16Add (Augend, Addend, &Result);
  1365. ASSERT_EQ (Status, RETURN_SUCCESS);
  1366. ASSERT_EQ (0x7474, Result);
  1367. //
  1368. // Otherwise should result in an error status
  1369. //
  1370. Augend = 0xabab;
  1371. Addend = 0xbcbc;
  1372. Status = SafeUint16Add (Augend, Addend, &Result);
  1373. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  1374. }
  1375. TEST(AdditionSubtractionTestSuite, TestSafeUint32Add) {
  1376. RETURN_STATUS Status;
  1377. UINT32 Augend;
  1378. UINT32 Addend;
  1379. UINT32 Result;
  1380. //
  1381. // If the result of addition doesn't overflow MAX_UINT32, then it's addition
  1382. //
  1383. Augend = 0x3a3a3a3a;
  1384. Addend = 0x3a3a3a3a;
  1385. Result = 0;
  1386. Status = SafeUint32Add (Augend, Addend, &Result);
  1387. ASSERT_EQ (Status, RETURN_SUCCESS);
  1388. ASSERT_EQ ((UINT32)0x74747474, Result);
  1389. //
  1390. // Otherwise should result in an error status
  1391. //
  1392. Augend = 0xabababab;
  1393. Addend = 0xbcbcbcbc;
  1394. Status = SafeUint32Add (Augend, Addend, &Result);
  1395. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  1396. }
  1397. TEST(AdditionSubtractionTestSuite, TestSafeUint64Add) {
  1398. RETURN_STATUS Status;
  1399. UINT64 Augend;
  1400. UINT64 Addend;
  1401. UINT64 Result;
  1402. //
  1403. // If the result of addition doesn't overflow MAX_UINT64, then it's addition
  1404. //
  1405. Augend = 0x3a3a3a3a12121212;
  1406. Addend = 0x3a3a3a3a12121212;
  1407. Result = 0;
  1408. Status = SafeUint64Add (Augend, Addend, &Result);
  1409. ASSERT_EQ (Status, RETURN_SUCCESS);
  1410. ASSERT_EQ ((UINT64)0x7474747424242424, Result);
  1411. //
  1412. // Otherwise should result in an error status
  1413. //
  1414. Augend = 0xababababefefefef;
  1415. Addend = 0xbcbcbcbcdededede;
  1416. Status = SafeUint64Add (Augend, Addend, &Result);
  1417. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  1418. }
  1419. TEST(AdditionSubtractionTestSuite, TestSafeInt8Add) {
  1420. RETURN_STATUS Status;
  1421. INT8 Augend;
  1422. INT8 Addend;
  1423. INT8 Result;
  1424. //
  1425. // If the result of addition doesn't overflow MAX_INT8
  1426. // and doesn't underflow MIN_INT8, then it's addition
  1427. //
  1428. Augend = 0x3a;
  1429. Addend = 0x3a;
  1430. Result = 0;
  1431. Status = SafeInt8Add (Augend, Addend, &Result);
  1432. ASSERT_EQ (Status, RETURN_SUCCESS);
  1433. ASSERT_EQ (0x74, Result);
  1434. Augend = (-58);
  1435. Addend = (-58);
  1436. Status = SafeInt8Add (Augend, Addend, &Result);
  1437. ASSERT_EQ (Status, RETURN_SUCCESS);
  1438. ASSERT_EQ ((-116), Result);
  1439. //
  1440. // Otherwise should result in an error status
  1441. //
  1442. Augend = 0x5a;
  1443. Addend = 0x5a;
  1444. Status = SafeInt8Add (Augend, Addend, &Result);
  1445. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  1446. Augend = (-90);
  1447. Addend = (-90);
  1448. Status = SafeInt8Add (Augend, Addend, &Result);
  1449. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  1450. }
  1451. TEST(AdditionSubtractionTestSuite, TestSafeInt16Add) {
  1452. RETURN_STATUS Status;
  1453. INT16 Augend;
  1454. INT16 Addend;
  1455. INT16 Result;
  1456. //
  1457. // If the result of addition doesn't overflow MAX_INT16
  1458. // and doesn't underflow MIN_INT16, then it's addition
  1459. //
  1460. Augend = 0x3a3a;
  1461. Addend = 0x3a3a;
  1462. Result = 0;
  1463. Status = SafeInt16Add (Augend, Addend, &Result);
  1464. ASSERT_EQ (Status, RETURN_SUCCESS);
  1465. ASSERT_EQ (0x7474, Result);
  1466. Augend = (-14906);
  1467. Addend = (-14906);
  1468. Status = SafeInt16Add (Augend, Addend, &Result);
  1469. ASSERT_EQ (Status, RETURN_SUCCESS);
  1470. ASSERT_EQ ((-29812), Result);
  1471. //
  1472. // Otherwise should result in an error status
  1473. //
  1474. Augend = 0x5a5a;
  1475. Addend = 0x5a5a;
  1476. Status = SafeInt16Add (Augend, Addend, &Result);
  1477. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  1478. Augend = (-23130);
  1479. Addend = (-23130);
  1480. Status = SafeInt16Add (Augend, Addend, &Result);
  1481. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  1482. }
  1483. TEST(AdditionSubtractionTestSuite, TestSafeInt32Add) {
  1484. RETURN_STATUS Status;
  1485. INT32 Augend;
  1486. INT32 Addend;
  1487. INT32 Result;
  1488. //
  1489. // If the result of addition doesn't overflow MAX_INT32
  1490. // and doesn't underflow MIN_INT32, then it's addition
  1491. //
  1492. Augend = 0x3a3a3a3a;
  1493. Addend = 0x3a3a3a3a;
  1494. Result = 0;
  1495. Status = SafeInt32Add (Augend, Addend, &Result);
  1496. ASSERT_EQ (Status, RETURN_SUCCESS);
  1497. ASSERT_EQ (0x74747474, Result);
  1498. Augend = (-976894522);
  1499. Addend = (-976894522);
  1500. Status = SafeInt32Add (Augend, Addend, &Result);
  1501. ASSERT_EQ (Status, RETURN_SUCCESS);
  1502. ASSERT_EQ ((-1953789044), Result);
  1503. //
  1504. // Otherwise should result in an error status
  1505. //
  1506. Augend = 0x5a5a5a5a;
  1507. Addend = 0x5a5a5a5a;
  1508. Status = SafeInt32Add (Augend, Addend, &Result);
  1509. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  1510. Augend = (-1515870810);
  1511. Addend = (-1515870810);
  1512. Status = SafeInt32Add (Augend, Addend, &Result);
  1513. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  1514. }
  1515. TEST(AdditionSubtractionTestSuite, TestSafeInt64Add) {
  1516. RETURN_STATUS Status;
  1517. INT64 Augend;
  1518. INT64 Addend;
  1519. INT64 Result;
  1520. //
  1521. // If the result of addition doesn't overflow MAX_INT64
  1522. // and doesn't underflow MIN_INT64, then it's addition
  1523. //
  1524. Augend = 0x3a3a3a3a3a3a3a3a;
  1525. Addend = 0x3a3a3a3a3a3a3a3a;
  1526. Result = 0;
  1527. Status = SafeInt64Add (Augend, Addend, &Result);
  1528. ASSERT_EQ (Status, RETURN_SUCCESS);
  1529. ASSERT_EQ (0x7474747474747474, Result);
  1530. Augend = (-4195730024608447034);
  1531. Addend = (-4195730024608447034);
  1532. Status = SafeInt64Add (Augend, Addend, &Result);
  1533. ASSERT_EQ (Status, RETURN_SUCCESS);
  1534. ASSERT_EQ ((-8391460049216894068), Result);
  1535. //
  1536. // Otherwise should result in an error status
  1537. //
  1538. Augend = 0x5a5a5a5a5a5a5a5a;
  1539. Addend = 0x5a5a5a5a5a5a5a5a;
  1540. Status = SafeInt64Add (Augend, Addend, &Result);
  1541. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  1542. Augend = (-6510615555426900570);
  1543. Addend = (-6510615555426900570);
  1544. Status = SafeInt64Add (Augend, Addend, &Result);
  1545. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  1546. }
  1547. //
  1548. // Subtraction function tests:
  1549. //
  1550. TEST(AdditionSubtractionTestSuite, TestSafeUint8Sub) {
  1551. RETURN_STATUS Status;
  1552. UINT8 Minuend;
  1553. UINT8 Subtrahend;
  1554. UINT8 Result;
  1555. //
  1556. // If Minuend >= Subtrahend, then it's subtraction
  1557. //
  1558. Minuend = 0x5a;
  1559. Subtrahend = 0x3b;
  1560. Result = 0;
  1561. Status = SafeUint8Sub (Minuend, Subtrahend, &Result);
  1562. ASSERT_EQ (Status, RETURN_SUCCESS);
  1563. ASSERT_EQ (0x1f, Result);
  1564. //
  1565. // Otherwise should result in an error status
  1566. //
  1567. Minuend = 0x5a;
  1568. Subtrahend = 0x6d;
  1569. Status = SafeUint8Sub (Minuend, Subtrahend, &Result);
  1570. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  1571. }
  1572. TEST(AdditionSubtractionTestSuite, TestSafeUint16Sub) {
  1573. RETURN_STATUS Status;
  1574. UINT16 Minuend;
  1575. UINT16 Subtrahend;
  1576. UINT16 Result;
  1577. //
  1578. // If Minuend >= Subtrahend, then it's subtraction
  1579. //
  1580. Minuend = 0x5a5a;
  1581. Subtrahend = 0x3b3b;
  1582. Result = 0;
  1583. Status = SafeUint16Sub (Minuend, Subtrahend, &Result);
  1584. ASSERT_EQ (Status, RETURN_SUCCESS);
  1585. ASSERT_EQ (0x1f1f, Result);
  1586. //
  1587. // Otherwise should result in an error status
  1588. //
  1589. Minuend = 0x5a5a;
  1590. Subtrahend = 0x6d6d;
  1591. Status = SafeUint16Sub (Minuend, Subtrahend, &Result);
  1592. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  1593. }
  1594. TEST(AdditionSubtractionTestSuite, TestSafeUint32Sub) {
  1595. RETURN_STATUS Status;
  1596. UINT32 Minuend;
  1597. UINT32 Subtrahend;
  1598. UINT32 Result;
  1599. //
  1600. // If Minuend >= Subtrahend, then it's subtraction
  1601. //
  1602. Minuend = 0x5a5a5a5a;
  1603. Subtrahend = 0x3b3b3b3b;
  1604. Result = 0;
  1605. Status = SafeUint32Sub (Minuend, Subtrahend, &Result);
  1606. ASSERT_EQ (Status, RETURN_SUCCESS);
  1607. ASSERT_EQ ((UINT32)0x1f1f1f1f, Result);
  1608. //
  1609. // Otherwise should result in an error status
  1610. //
  1611. Minuend = 0x5a5a5a5a;
  1612. Subtrahend = 0x6d6d6d6d;
  1613. Status = SafeUint32Sub (Minuend, Subtrahend, &Result);
  1614. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  1615. }
  1616. TEST(AdditionSubtractionTestSuite, TestSafeUint64Sub) {
  1617. RETURN_STATUS Status;
  1618. UINT64 Minuend;
  1619. UINT64 Subtrahend;
  1620. UINT64 Result;
  1621. //
  1622. // If Minuend >= Subtrahend, then it's subtraction
  1623. //
  1624. Minuend = 0x5a5a5a5a5a5a5a5a;
  1625. Subtrahend = 0x3b3b3b3b3b3b3b3b;
  1626. Result = 0;
  1627. Status = SafeUint64Sub (Minuend, Subtrahend, &Result);
  1628. ASSERT_EQ (Status, RETURN_SUCCESS);
  1629. ASSERT_EQ ((UINT64)0x1f1f1f1f1f1f1f1f, Result);
  1630. //
  1631. // Otherwise should result in an error status
  1632. //
  1633. Minuend = 0x5a5a5a5a5a5a5a5a;
  1634. Subtrahend = 0x6d6d6d6d6d6d6d6d;
  1635. Status = SafeUint64Sub (Minuend, Subtrahend, &Result);
  1636. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  1637. }
  1638. TEST(AdditionSubtractionTestSuite, TestSafeInt8Sub) {
  1639. RETURN_STATUS Status;
  1640. INT8 Minuend;
  1641. INT8 Subtrahend;
  1642. INT8 Result;
  1643. //
  1644. // If the result of subtractions doesn't overflow MAX_INT8 or
  1645. // underflow MIN_INT8, then it's subtraction
  1646. //
  1647. Minuend = 0x5a;
  1648. Subtrahend = 0x3a;
  1649. Result = 0;
  1650. Status = SafeInt8Sub (Minuend, Subtrahend, &Result);
  1651. ASSERT_EQ (Status, RETURN_SUCCESS);
  1652. ASSERT_EQ (0x20, Result);
  1653. Minuend = 58;
  1654. Subtrahend = 78;
  1655. Status = SafeInt8Sub (Minuend, Subtrahend, &Result);
  1656. ASSERT_EQ (Status, RETURN_SUCCESS);
  1657. ASSERT_EQ ((-20), Result);
  1658. //
  1659. // Otherwise should result in an error status
  1660. //
  1661. Minuend = (-80);
  1662. Subtrahend = 80;
  1663. Status = SafeInt8Sub (Minuend, Subtrahend, &Result);
  1664. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  1665. Minuend = (80);
  1666. Subtrahend = (-80);
  1667. Status = SafeInt8Sub (Minuend, Subtrahend, &Result);
  1668. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  1669. }
  1670. TEST(AdditionSubtractionTestSuite, TestSafeInt16Sub) {
  1671. RETURN_STATUS Status;
  1672. INT16 Minuend;
  1673. INT16 Subtrahend;
  1674. INT16 Result;
  1675. //
  1676. // If the result of subtractions doesn't overflow MAX_INT16 or
  1677. // underflow MIN_INT16, then it's subtraction
  1678. //
  1679. Minuend = 0x5a5a;
  1680. Subtrahend = 0x3a3a;
  1681. Result = 0;
  1682. Status = SafeInt16Sub (Minuend, Subtrahend, &Result);
  1683. ASSERT_EQ (Status, RETURN_SUCCESS);
  1684. ASSERT_EQ (0x2020, Result);
  1685. Minuend = 0x3a3a;
  1686. Subtrahend = 0x5a5a;
  1687. Status = SafeInt16Sub (Minuend, Subtrahend, &Result);
  1688. ASSERT_EQ (Status, RETURN_SUCCESS);
  1689. ASSERT_EQ ((-8224), Result);
  1690. //
  1691. // Otherwise should result in an error status
  1692. //
  1693. Minuend = (-31354);
  1694. Subtrahend = 31354;
  1695. Status = SafeInt16Sub (Minuend, Subtrahend, &Result);
  1696. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  1697. Minuend = (31354);
  1698. Subtrahend = (-31354);
  1699. Status = SafeInt16Sub (Minuend, Subtrahend, &Result);
  1700. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  1701. }
  1702. TEST(AdditionSubtractionTestSuite, TestSafeInt32Sub) {
  1703. RETURN_STATUS Status;
  1704. INT32 Minuend;
  1705. INT32 Subtrahend;
  1706. INT32 Result;
  1707. //
  1708. // If the result of subtractions doesn't overflow MAX_INT32 or
  1709. // underflow MIN_INT32, then it's subtraction
  1710. //
  1711. Minuend = 0x5a5a5a5a;
  1712. Subtrahend = 0x3a3a3a3a;
  1713. Result = 0;
  1714. Status = SafeInt32Sub (Minuend, Subtrahend, &Result);
  1715. ASSERT_EQ (Status, RETURN_SUCCESS);
  1716. ASSERT_EQ (0x20202020, Result);
  1717. Minuend = 0x3a3a3a3a;
  1718. Subtrahend = 0x5a5a5a5a;
  1719. Status = SafeInt32Sub (Minuend, Subtrahend, &Result);
  1720. ASSERT_EQ (Status, RETURN_SUCCESS);
  1721. ASSERT_EQ ((-538976288), Result);
  1722. //
  1723. // Otherwise should result in an error status
  1724. //
  1725. Minuend = (-2054847098);
  1726. Subtrahend = 2054847098;
  1727. Status = SafeInt32Sub (Minuend, Subtrahend, &Result);
  1728. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  1729. Minuend = (2054847098);
  1730. Subtrahend = (-2054847098);
  1731. Status = SafeInt32Sub (Minuend, Subtrahend, &Result);
  1732. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  1733. }
  1734. TEST(AdditionSubtractionTestSuite, TestSafeInt64Sub) {
  1735. RETURN_STATUS Status;
  1736. INT64 Minuend;
  1737. INT64 Subtrahend;
  1738. INT64 Result;
  1739. //
  1740. // If the result of subtractions doesn't overflow MAX_INT64 or
  1741. // underflow MIN_INT64, then it's subtraction
  1742. //
  1743. Minuend = 0x5a5a5a5a5a5a5a5a;
  1744. Subtrahend = 0x3a3a3a3a3a3a3a3a;
  1745. Result = 0;
  1746. Status = SafeInt64Sub (Minuend, Subtrahend, &Result);
  1747. ASSERT_EQ (Status, RETURN_SUCCESS);
  1748. ASSERT_EQ (0x2020202020202020, Result);
  1749. Minuend = 0x3a3a3a3a3a3a3a3a;
  1750. Subtrahend = 0x5a5a5a5a5a5a5a5a;
  1751. Status = SafeInt64Sub (Minuend, Subtrahend, &Result);
  1752. ASSERT_EQ (Status, RETURN_SUCCESS);
  1753. ASSERT_EQ ((-2314885530818453536), Result);
  1754. //
  1755. // Otherwise should result in an error status
  1756. //
  1757. Minuend = (-8825501086245354106);
  1758. Subtrahend = 8825501086245354106;
  1759. Status = SafeInt64Sub (Minuend, Subtrahend, &Result);
  1760. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  1761. Minuend = (8825501086245354106);
  1762. Subtrahend = (-8825501086245354106);
  1763. Status = SafeInt64Sub (Minuend, Subtrahend, &Result);
  1764. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  1765. }
  1766. //
  1767. // Multiplication function tests:
  1768. //
  1769. TEST(MultiplicationTestSuite, TestSafeUint8Mult) {
  1770. RETURN_STATUS Status;
  1771. UINT8 Multiplicand;
  1772. UINT8 Multiplier;
  1773. UINT8 Result;
  1774. //
  1775. // If the result of multiplication doesn't overflow MAX_UINT8, it will succeed
  1776. //
  1777. Multiplicand = 0x12;
  1778. Multiplier = 0xa;
  1779. Result = 0;
  1780. Status = SafeUint8Mult (Multiplicand, Multiplier, &Result);
  1781. ASSERT_EQ (Status, RETURN_SUCCESS);
  1782. ASSERT_EQ (0xb4, Result);
  1783. //
  1784. // Otherwise should result in an error status
  1785. //
  1786. Multiplicand = 0x12;
  1787. Multiplier = 0x23;
  1788. Status = SafeUint8Mult (Multiplicand, Multiplier, &Result);
  1789. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  1790. }
  1791. TEST(MultiplicationTestSuite, TestSafeUint16Mult) {
  1792. RETURN_STATUS Status;
  1793. UINT16 Multiplicand;
  1794. UINT16 Multiplier;
  1795. UINT16 Result;
  1796. //
  1797. // If the result of multiplication doesn't overflow MAX_UINT16, it will succeed
  1798. //
  1799. Multiplicand = 0x212;
  1800. Multiplier = 0x7a;
  1801. Result = 0;
  1802. Status = SafeUint16Mult (Multiplicand, Multiplier, &Result);
  1803. ASSERT_EQ (Status, RETURN_SUCCESS);
  1804. ASSERT_EQ (0xfc94, Result);
  1805. //
  1806. // Otherwise should result in an error status
  1807. //
  1808. Multiplicand = 0x1234;
  1809. Multiplier = 0x213;
  1810. Status = SafeUint16Mult (Multiplicand, Multiplier, &Result);
  1811. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  1812. }
  1813. TEST(MultiplicationTestSuite, TestSafeUint32Mult) {
  1814. RETURN_STATUS Status;
  1815. UINT32 Multiplicand;
  1816. UINT32 Multiplier;
  1817. UINT32 Result;
  1818. //
  1819. // If the result of multiplication doesn't overflow MAX_UINT32, it will succeed
  1820. //
  1821. Multiplicand = 0xa122a;
  1822. Multiplier = 0xd23;
  1823. Result = 0;
  1824. Status = SafeUint32Mult (Multiplicand, Multiplier, &Result);
  1825. ASSERT_EQ (Status, RETURN_SUCCESS);
  1826. ASSERT_EQ (0x844c9dbe, Result);
  1827. //
  1828. // Otherwise should result in an error status
  1829. //
  1830. Multiplicand = 0xa122a;
  1831. Multiplier = 0xed23;
  1832. Status = SafeUint32Mult (Multiplicand, Multiplier, &Result);
  1833. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  1834. }
  1835. TEST(MultiplicationTestSuite, TestSafeUint64Mult) {
  1836. RETURN_STATUS Status;
  1837. UINT64 Multiplicand;
  1838. UINT64 Multiplier;
  1839. UINT64 Result;
  1840. //
  1841. // If the result of multiplication doesn't overflow MAX_UINT64, it will succeed
  1842. //
  1843. Multiplicand = 0x123456789a;
  1844. Multiplier = 0x1234567;
  1845. Result = 0;
  1846. Status = SafeUint64Mult (Multiplicand, Multiplier, &Result);
  1847. ASSERT_EQ (Status, RETURN_SUCCESS);
  1848. ASSERT_EQ ((UINT64)0x14b66db9745a07f6, Result);
  1849. //
  1850. // Otherwise should result in an error status
  1851. //
  1852. Multiplicand = 0x123456789a;
  1853. Multiplier = 0x12345678;
  1854. Status = SafeUint64Mult (Multiplicand, Multiplier, &Result);
  1855. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  1856. }
  1857. TEST(MultiplicationTestSuite, TestSafeInt8Mult) {
  1858. RETURN_STATUS Status;
  1859. INT8 Multiplicand;
  1860. INT8 Multiplier;
  1861. INT8 Result;
  1862. //
  1863. // If the result of multiplication doesn't overflow MAX_INT8 and doesn't
  1864. // underflow MIN_UINT8, it will succeed
  1865. //
  1866. Multiplicand = 0x12;
  1867. Multiplier = 0x7;
  1868. Result = 0;
  1869. Status = SafeInt8Mult (Multiplicand, Multiplier, &Result);
  1870. ASSERT_EQ (Status, RETURN_SUCCESS);
  1871. ASSERT_EQ (0x7e, Result);
  1872. //
  1873. // Otherwise should result in an error status
  1874. //
  1875. Multiplicand = 0x12;
  1876. Multiplier = 0xa;
  1877. Status = SafeInt8Mult (Multiplicand, Multiplier, &Result);
  1878. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  1879. }
  1880. TEST(MultiplicationTestSuite, TestSafeInt16Mult) {
  1881. RETURN_STATUS Status;
  1882. INT16 Multiplicand;
  1883. INT16 Multiplier;
  1884. INT16 Result;
  1885. //
  1886. // If the result of multiplication doesn't overflow MAX_INT16 and doesn't
  1887. // underflow MIN_UINT16, it will succeed
  1888. //
  1889. Multiplicand = 0x123;
  1890. Multiplier = 0x67;
  1891. Result = 0;
  1892. Status = SafeInt16Mult (Multiplicand, Multiplier, &Result);
  1893. ASSERT_EQ (Status, RETURN_SUCCESS);
  1894. ASSERT_EQ (0x7515, Result);
  1895. //
  1896. // Otherwise should result in an error status
  1897. //
  1898. Multiplicand = 0x123;
  1899. Multiplier = 0xab;
  1900. Status = SafeInt16Mult (Multiplicand, Multiplier, &Result);
  1901. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  1902. }
  1903. TEST(MultiplicationTestSuite, TestSafeInt32Mult) {
  1904. RETURN_STATUS Status;
  1905. INT32 Multiplicand;
  1906. INT32 Multiplier;
  1907. INT32 Result;
  1908. //
  1909. // If the result of multiplication doesn't overflow MAX_INT32 and doesn't
  1910. // underflow MIN_UINT32, it will succeed
  1911. //
  1912. Multiplicand = 0x123456;
  1913. Multiplier = 0x678;
  1914. Result = 0;
  1915. Status = SafeInt32Mult (Multiplicand, Multiplier, &Result);
  1916. ASSERT_EQ (Status, RETURN_SUCCESS);
  1917. ASSERT_EQ (0x75c28c50, Result);
  1918. //
  1919. // Otherwise should result in an error status
  1920. //
  1921. Multiplicand = 0x123456;
  1922. Multiplier = 0xabc;
  1923. Status = SafeInt32Mult (Multiplicand, Multiplier, &Result);
  1924. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  1925. }
  1926. TEST(MultiplicationTestSuite, TestSafeInt64Mult) {
  1927. RETURN_STATUS Status;
  1928. INT64 Multiplicand;
  1929. INT64 Multiplier;
  1930. INT64 Result;
  1931. //
  1932. // If the result of multiplication doesn't overflow MAX_INT64 and doesn't
  1933. // underflow MIN_UINT64, it will succeed
  1934. //
  1935. Multiplicand = 0x123456789;
  1936. Multiplier = 0x6789abcd;
  1937. Result = 0;
  1938. Status = SafeInt64Mult (Multiplicand, Multiplier, &Result);
  1939. ASSERT_EQ (Status, RETURN_SUCCESS);
  1940. ASSERT_EQ (0x75cd9045220d6bb5, Result);
  1941. //
  1942. // Otherwise should result in an error status
  1943. //
  1944. Multiplicand = 0x123456789;
  1945. Multiplier = 0xa789abcd;
  1946. Status = SafeInt64Mult (Multiplicand, Multiplier, &Result);
  1947. ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
  1948. }
  1949. int main(int argc, char* argv[]) {
  1950. testing::InitGoogleTest(&argc, argv);
  1951. return RUN_ALL_TESTS();
  1952. }