SDL_test_fuzzer.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2024 Sam Lantinga <[email protected]>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. /**
  19. * Fuzzer functions of SDL test framework.
  20. *
  21. * This code is a part of the SDL test library, not the main SDL library.
  22. */
  23. /*
  24. Data generators for fuzzing test data in a reproducible way.
  25. */
  26. #ifndef SDL_test_fuzzer_h_
  27. #define SDL_test_fuzzer_h_
  28. #include <SDL3/SDL_begin_code.h>
  29. /* Set up for C function definitions, even when using C++ */
  30. #ifdef __cplusplus
  31. extern "C" {
  32. #endif
  33. /*
  34. Based on GSOC code by Markus Kauppila <[email protected]>
  35. */
  36. /**
  37. * Note: The fuzzer implementation uses a static instance of random context
  38. * internally which makes it thread-UNsafe.
  39. */
  40. /**
  41. * Initializes the fuzzer for a test
  42. *
  43. * \param execKey Execution "Key" that initializes the random number generator uniquely for the test.
  44. *
  45. */
  46. void SDLTest_FuzzerInit(Uint64 execKey);
  47. /**
  48. * Returns a random Uint8
  49. *
  50. * \returns a generated integer
  51. */
  52. Uint8 SDLTest_RandomUint8(void);
  53. /**
  54. * Returns a random Sint8
  55. *
  56. * \returns a generated signed integer
  57. */
  58. Sint8 SDLTest_RandomSint8(void);
  59. /**
  60. * Returns a random Uint16
  61. *
  62. * \returns a generated integer
  63. */
  64. Uint16 SDLTest_RandomUint16(void);
  65. /**
  66. * Returns a random Sint16
  67. *
  68. * \returns a generated signed integer
  69. */
  70. Sint16 SDLTest_RandomSint16(void);
  71. /**
  72. * Returns a random integer
  73. *
  74. * \returns a generated integer
  75. */
  76. Sint32 SDLTest_RandomSint32(void);
  77. /**
  78. * Returns a random positive integer
  79. *
  80. * \returns a generated integer
  81. */
  82. Uint32 SDLTest_RandomUint32(void);
  83. /**
  84. * Returns random Uint64.
  85. *
  86. * \returns a generated integer
  87. */
  88. Uint64 SDLTest_RandomUint64(void);
  89. /**
  90. * Returns random Sint64.
  91. *
  92. * \returns a generated signed integer
  93. */
  94. Sint64 SDLTest_RandomSint64(void);
  95. /**
  96. * \returns a random float in range [0.0 - 1.0]
  97. */
  98. float SDLTest_RandomUnitFloat(void);
  99. /**
  100. * \returns a random double in range [0.0 - 1.0]
  101. */
  102. double SDLTest_RandomUnitDouble(void);
  103. /**
  104. * \returns a random float.
  105. *
  106. */
  107. float SDLTest_RandomFloat(void);
  108. /**
  109. * \returns a random double.
  110. *
  111. */
  112. double SDLTest_RandomDouble(void);
  113. /**
  114. * Returns a random boundary value for Uint8 within the given boundaries.
  115. * Boundaries are inclusive, see the usage examples below. If validDomain
  116. * is true, the function will only return valid boundaries, otherwise non-valid
  117. * boundaries are also possible.
  118. * If boundary1 > boundary2, the values are swapped
  119. *
  120. * Usage examples:
  121. * RandomUint8BoundaryValue(10, 20, SDL_TRUE) returns 10, 11, 19 or 20
  122. * RandomUint8BoundaryValue(1, 20, SDL_FALSE) returns 0 or 21
  123. * RandomUint8BoundaryValue(0, 99, SDL_FALSE) returns 100
  124. * RandomUint8BoundaryValue(0, 255, SDL_FALSE) returns 0 (error set)
  125. *
  126. * \param boundary1 Lower boundary limit
  127. * \param boundary2 Upper boundary limit
  128. * \param validDomain Should the generated boundary be valid (=within the bounds) or not?
  129. *
  130. * \returns a random boundary value for the given range and domain or 0 with error set
  131. */
  132. Uint8 SDLTest_RandomUint8BoundaryValue(Uint8 boundary1, Uint8 boundary2, SDL_bool validDomain);
  133. /**
  134. * Returns a random boundary value for Uint16 within the given boundaries.
  135. * Boundaries are inclusive, see the usage examples below. If validDomain
  136. * is true, the function will only return valid boundaries, otherwise non-valid
  137. * boundaries are also possible.
  138. * If boundary1 > boundary2, the values are swapped
  139. *
  140. * Usage examples:
  141. * RandomUint16BoundaryValue(10, 20, SDL_TRUE) returns 10, 11, 19 or 20
  142. * RandomUint16BoundaryValue(1, 20, SDL_FALSE) returns 0 or 21
  143. * RandomUint16BoundaryValue(0, 99, SDL_FALSE) returns 100
  144. * RandomUint16BoundaryValue(0, 0xFFFF, SDL_FALSE) returns 0 (error set)
  145. *
  146. * \param boundary1 Lower boundary limit
  147. * \param boundary2 Upper boundary limit
  148. * \param validDomain Should the generated boundary be valid (=within the bounds) or not?
  149. *
  150. * \returns a random boundary value for the given range and domain or 0 with error set
  151. */
  152. Uint16 SDLTest_RandomUint16BoundaryValue(Uint16 boundary1, Uint16 boundary2, SDL_bool validDomain);
  153. /**
  154. * Returns a random boundary value for Uint32 within the given boundaries.
  155. * Boundaries are inclusive, see the usage examples below. If validDomain
  156. * is true, the function will only return valid boundaries, otherwise non-valid
  157. * boundaries are also possible.
  158. * If boundary1 > boundary2, the values are swapped
  159. *
  160. * Usage examples:
  161. * RandomUint32BoundaryValue(10, 20, SDL_TRUE) returns 10, 11, 19 or 20
  162. * RandomUint32BoundaryValue(1, 20, SDL_FALSE) returns 0 or 21
  163. * RandomUint32BoundaryValue(0, 99, SDL_FALSE) returns 100
  164. * RandomUint32BoundaryValue(0, 0xFFFFFFFF, SDL_FALSE) returns 0 (with error set)
  165. *
  166. * \param boundary1 Lower boundary limit
  167. * \param boundary2 Upper boundary limit
  168. * \param validDomain Should the generated boundary be valid (=within the bounds) or not?
  169. *
  170. * \returns a random boundary value for the given range and domain or 0 with error set
  171. */
  172. Uint32 SDLTest_RandomUint32BoundaryValue(Uint32 boundary1, Uint32 boundary2, SDL_bool validDomain);
  173. /**
  174. * Returns a random boundary value for Uint64 within the given boundaries.
  175. * Boundaries are inclusive, see the usage examples below. If validDomain
  176. * is true, the function will only return valid boundaries, otherwise non-valid
  177. * boundaries are also possible.
  178. * If boundary1 > boundary2, the values are swapped
  179. *
  180. * Usage examples:
  181. * RandomUint64BoundaryValue(10, 20, SDL_TRUE) returns 10, 11, 19 or 20
  182. * RandomUint64BoundaryValue(1, 20, SDL_FALSE) returns 0 or 21
  183. * RandomUint64BoundaryValue(0, 99, SDL_FALSE) returns 100
  184. * RandomUint64BoundaryValue(0, 0xFFFFFFFFFFFFFFFF, SDL_FALSE) returns 0 (with error set)
  185. *
  186. * \param boundary1 Lower boundary limit
  187. * \param boundary2 Upper boundary limit
  188. * \param validDomain Should the generated boundary be valid (=within the bounds) or not?
  189. *
  190. * \returns a random boundary value for the given range and domain or 0 with error set
  191. */
  192. Uint64 SDLTest_RandomUint64BoundaryValue(Uint64 boundary1, Uint64 boundary2, SDL_bool validDomain);
  193. /**
  194. * Returns a random boundary value for Sint8 within the given boundaries.
  195. * Boundaries are inclusive, see the usage examples below. If validDomain
  196. * is true, the function will only return valid boundaries, otherwise non-valid
  197. * boundaries are also possible.
  198. * If boundary1 > boundary2, the values are swapped
  199. *
  200. * Usage examples:
  201. * RandomSint8BoundaryValue(-10, 20, SDL_TRUE) returns -11, -10, 19 or 20
  202. * RandomSint8BoundaryValue(-100, -10, SDL_FALSE) returns -101 or -9
  203. * RandomSint8BoundaryValue(SINT8_MIN, 99, SDL_FALSE) returns 100
  204. * RandomSint8BoundaryValue(SINT8_MIN, SINT8_MAX, SDL_FALSE) returns SINT8_MIN (== error value) with error set
  205. *
  206. * \param boundary1 Lower boundary limit
  207. * \param boundary2 Upper boundary limit
  208. * \param validDomain Should the generated boundary be valid (=within the bounds) or not?
  209. *
  210. * \returns a random boundary value for the given range and domain or SINT8_MIN with error set
  211. */
  212. Sint8 SDLTest_RandomSint8BoundaryValue(Sint8 boundary1, Sint8 boundary2, SDL_bool validDomain);
  213. /**
  214. * Returns a random boundary value for Sint16 within the given boundaries.
  215. * Boundaries are inclusive, see the usage examples below. If validDomain
  216. * is true, the function will only return valid boundaries, otherwise non-valid
  217. * boundaries are also possible.
  218. * If boundary1 > boundary2, the values are swapped
  219. *
  220. * Usage examples:
  221. * RandomSint16BoundaryValue(-10, 20, SDL_TRUE) returns -11, -10, 19 or 20
  222. * RandomSint16BoundaryValue(-100, -10, SDL_FALSE) returns -101 or -9
  223. * RandomSint16BoundaryValue(SINT16_MIN, 99, SDL_FALSE) returns 100
  224. * RandomSint16BoundaryValue(SINT16_MIN, SINT16_MAX, SDL_FALSE) returns SINT16_MIN (== error value) with error set
  225. *
  226. * \param boundary1 Lower boundary limit
  227. * \param boundary2 Upper boundary limit
  228. * \param validDomain Should the generated boundary be valid (=within the bounds) or not?
  229. *
  230. * \returns a random boundary value for the given range and domain or SINT16_MIN with error set
  231. */
  232. Sint16 SDLTest_RandomSint16BoundaryValue(Sint16 boundary1, Sint16 boundary2, SDL_bool validDomain);
  233. /**
  234. * Returns a random boundary value for Sint32 within the given boundaries.
  235. * Boundaries are inclusive, see the usage examples below. If validDomain
  236. * is true, the function will only return valid boundaries, otherwise non-valid
  237. * boundaries are also possible.
  238. * If boundary1 > boundary2, the values are swapped
  239. *
  240. * Usage examples:
  241. * RandomSint32BoundaryValue(-10, 20, SDL_TRUE) returns -11, -10, 19 or 20
  242. * RandomSint32BoundaryValue(-100, -10, SDL_FALSE) returns -101 or -9
  243. * RandomSint32BoundaryValue(SINT32_MIN, 99, SDL_FALSE) returns 100
  244. * RandomSint32BoundaryValue(SINT32_MIN, SINT32_MAX, SDL_FALSE) returns SINT32_MIN (== error value)
  245. *
  246. * \param boundary1 Lower boundary limit
  247. * \param boundary2 Upper boundary limit
  248. * \param validDomain Should the generated boundary be valid (=within the bounds) or not?
  249. *
  250. * \returns a random boundary value for the given range and domain or SINT32_MIN with error set
  251. */
  252. Sint32 SDLTest_RandomSint32BoundaryValue(Sint32 boundary1, Sint32 boundary2, SDL_bool validDomain);
  253. /**
  254. * Returns a random boundary value for Sint64 within the given boundaries.
  255. * Boundaries are inclusive, see the usage examples below. If validDomain
  256. * is true, the function will only return valid boundaries, otherwise non-valid
  257. * boundaries are also possible.
  258. * If boundary1 > boundary2, the values are swapped
  259. *
  260. * Usage examples:
  261. * RandomSint64BoundaryValue(-10, 20, SDL_TRUE) returns -11, -10, 19 or 20
  262. * RandomSint64BoundaryValue(-100, -10, SDL_FALSE) returns -101 or -9
  263. * RandomSint64BoundaryValue(SINT64_MIN, 99, SDL_FALSE) returns 100
  264. * RandomSint64BoundaryValue(SINT64_MIN, SINT64_MAX, SDL_FALSE) returns SINT64_MIN (== error value) and error set
  265. *
  266. * \param boundary1 Lower boundary limit
  267. * \param boundary2 Upper boundary limit
  268. * \param validDomain Should the generated boundary be valid (=within the bounds) or not?
  269. *
  270. * \returns a random boundary value for the given range and domain or SINT64_MIN with error set
  271. */
  272. Sint64 SDLTest_RandomSint64BoundaryValue(Sint64 boundary1, Sint64 boundary2, SDL_bool validDomain);
  273. /**
  274. * Returns integer in range [min, max] (inclusive).
  275. * Min and max values can be negative values.
  276. * If Max in smaller than min, then the values are swapped.
  277. * Min and max are the same value, that value will be returned.
  278. *
  279. * \param min Minimum inclusive value of returned random number
  280. * \param max Maximum inclusive value of returned random number
  281. *
  282. * \returns a generated random integer in range
  283. */
  284. Sint32 SDLTest_RandomIntegerInRange(Sint32 min, Sint32 max);
  285. /**
  286. * Generates random null-terminated string. The minimum length for
  287. * the string is 1 character, maximum length for the string is 255
  288. * characters and it can contain ASCII characters from 32 to 126.
  289. *
  290. * Note: Returned string needs to be deallocated.
  291. *
  292. * \returns a newly allocated random string; or NULL if length was invalid or string could not be allocated.
  293. */
  294. char *SDLTest_RandomAsciiString(void);
  295. /**
  296. * Generates random null-terminated string. The maximum length for
  297. * the string is defined by the maxLength parameter.
  298. * String can contain ASCII characters from 32 to 126.
  299. *
  300. * Note: Returned string needs to be deallocated.
  301. *
  302. * \param maxLength The maximum length of the generated string.
  303. *
  304. * \returns a newly allocated random string; or NULL if maxLength was invalid or string could not be allocated.
  305. */
  306. char *SDLTest_RandomAsciiStringWithMaximumLength(int maxLength);
  307. /**
  308. * Generates random null-terminated string. The length for
  309. * the string is defined by the size parameter.
  310. * String can contain ASCII characters from 32 to 126.
  311. *
  312. * Note: Returned string needs to be deallocated.
  313. *
  314. * \param size The length of the generated string
  315. *
  316. * \returns a newly allocated random string; or NULL if size was invalid or string could not be allocated.
  317. */
  318. char *SDLTest_RandomAsciiStringOfSize(int size);
  319. /**
  320. * Get the invocation count for the fuzzer since last ...FuzzerInit.
  321. *
  322. * \returns the invocation count.
  323. */
  324. int SDLTest_GetFuzzerInvocationCount(void);
  325. /* Ends C function definitions when using C++ */
  326. #ifdef __cplusplus
  327. }
  328. #endif
  329. #include <SDL3/SDL_close_code.h>
  330. #endif /* SDL_test_fuzzer_h_ */