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