testautomation_guid.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /**
  2. * GUID test suite
  3. */
  4. #include <SDL3/SDL.h>
  5. #include <SDL3/SDL_test.h>
  6. #include "testautomation_suites.h"
  7. /* ================= Test Case Implementation ================== */
  8. /* Helper functions */
  9. #define NUM_TEST_GUIDS 5
  10. static struct
  11. {
  12. char *str;
  13. Uint64 upper, lower;
  14. } test_guids[NUM_TEST_GUIDS] = {
  15. { "0000000000000000"
  16. "ffffffffffffffff",
  17. 0x0000000000000000, 0xfffffffffffffffflu },
  18. { "0011223344556677"
  19. "8091a2b3c4d5e6f0",
  20. 0x0011223344556677lu, 0x8091a2b3c4d5e6f0lu },
  21. { "a011223344556677"
  22. "8091a2b3c4d5e6f0",
  23. 0xa011223344556677lu, 0x8091a2b3c4d5e6f0lu },
  24. { "a011223344556677"
  25. "8091a2b3c4d5e6f1",
  26. 0xa011223344556677lu, 0x8091a2b3c4d5e6f1lu },
  27. { "a011223344556677"
  28. "8191a2b3c4d5e6f0",
  29. 0xa011223344556677lu, 0x8191a2b3c4d5e6f0lu },
  30. };
  31. static void
  32. upper_lower_to_bytestring(Uint8 *out, Uint64 upper, Uint64 lower)
  33. {
  34. Uint64 values[2];
  35. int i, k;
  36. values[0] = upper;
  37. values[1] = lower;
  38. for (i = 0; i < 2; ++i) {
  39. Uint64 v = values[i];
  40. for (k = 0; k < 8; ++k) {
  41. *out++ = v >> 56;
  42. v <<= 8;
  43. }
  44. }
  45. }
  46. /* Test case functions */
  47. /**
  48. * \brief Check String-to-GUID conversion
  49. *
  50. * \sa SDL_GUIDFromString
  51. */
  52. static int
  53. TestGuidFromString(void *arg)
  54. {
  55. int i;
  56. SDLTest_AssertPass("Call to SDL_GUIDFromString");
  57. for (i = 0; i < NUM_TEST_GUIDS; ++i) {
  58. Uint8 expected[16];
  59. SDL_GUID guid;
  60. upper_lower_to_bytestring(expected,
  61. test_guids[i].upper, test_guids[i].lower);
  62. guid = SDL_GUIDFromString(test_guids[i].str);
  63. SDLTest_AssertCheck(SDL_memcmp(expected, guid.data, 16) == 0, "GUID from string, GUID was: '%s'", test_guids[i].str);
  64. }
  65. return TEST_COMPLETED;
  66. }
  67. /**
  68. * \brief Check GUID-to-String conversion
  69. *
  70. * \sa SDL_GUIDToString
  71. */
  72. static int
  73. TestGuidToString(void *arg)
  74. {
  75. int i;
  76. SDLTest_AssertPass("Call to SDL_GUIDToString");
  77. for (i = 0; i < NUM_TEST_GUIDS; ++i) {
  78. const int guid_str_offset = 4;
  79. char guid_str_buf[64];
  80. char *guid_str = guid_str_buf + guid_str_offset;
  81. SDL_GUID guid;
  82. int size;
  83. upper_lower_to_bytestring(guid.data,
  84. test_guids[i].upper, test_guids[i].lower);
  85. /* Serialise to limited-length buffers */
  86. for (size = 0; size <= 36; ++size) {
  87. const Uint8 fill_char = size + 0xa0;
  88. Uint32 expected_prefix;
  89. Uint32 actual_prefix;
  90. int written_size;
  91. SDL_memset(guid_str_buf, fill_char, sizeof(guid_str_buf));
  92. SDL_GUIDToString(guid, guid_str, size);
  93. /* Check bytes before guid_str_buf */
  94. expected_prefix = fill_char | (fill_char << 8) | (fill_char << 16) | (fill_char << 24);
  95. SDL_memcpy(&actual_prefix, guid_str_buf, 4);
  96. SDLTest_AssertCheck(expected_prefix == actual_prefix, "String buffer memory before output untouched, expected: %" SDL_PRIu32 ", got: %" SDL_PRIu32 ", at size=%d", expected_prefix, actual_prefix, size);
  97. /* Check that we did not overwrite too much */
  98. written_size = 0;
  99. while ((guid_str[written_size] & 0xff) != fill_char && written_size < 256) {
  100. ++written_size;
  101. }
  102. SDLTest_AssertCheck(written_size <= size, "Output length is within expected bounds, with length %d: wrote %d of %d permitted bytes", size, written_size, size);
  103. if (size >= 33) {
  104. SDLTest_AssertCheck(SDL_strcmp(guid_str, test_guids[i].str) == 0, "GUID string equality, from string: %s", test_guids[i].str);
  105. }
  106. }
  107. }
  108. return TEST_COMPLETED;
  109. }
  110. /* ================= Test References ================== */
  111. /* GUID routine test cases */
  112. static const SDLTest_TestCaseReference guidTest1 = {
  113. (SDLTest_TestCaseFp)TestGuidFromString, "TestGuidFromString", "Call to SDL_GUIDFromString", TEST_ENABLED
  114. };
  115. static const SDLTest_TestCaseReference guidTest2 = {
  116. (SDLTest_TestCaseFp)TestGuidToString, "TestGuidToString", "Call to SDL_GUIDToString", TEST_ENABLED
  117. };
  118. /* Sequence of GUID routine test cases */
  119. static const SDLTest_TestCaseReference *guidTests[] = {
  120. &guidTest1,
  121. &guidTest2,
  122. NULL
  123. };
  124. /* GUID routine test suite (global) */
  125. SDLTest_TestSuiteReference guidTestSuite = {
  126. "GUID",
  127. NULL,
  128. guidTests,
  129. NULL
  130. };