testguid.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. Copyright (C) 1997-2022 Sam Lantinga <[email protected]>
  3. This software is provided 'as-is', without any express or implied
  4. warranty. In no event will the authors be held liable for any damages
  5. arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it
  8. freely.
  9. */
  10. /**
  11. * Automated tests for GUID processing
  12. */
  13. #include <stdio.h>
  14. #include "SDL.h"
  15. /* Helpers */
  16. static int _error_count = 0;
  17. static int
  18. _require_eq(Uint64 expected, Uint64 actual, int line, char *msg)
  19. {
  20. if (expected != actual) {
  21. _error_count += 1;
  22. SDL_LogError(SDL_LOG_CATEGORY_ERROR, "[%s, L%d] %s: Actual %ld (0x%lx) != expected %ld (0x%lx)",
  23. __FILE__, line, msg, actual, actual, expected, expected);
  24. return 0;
  25. }
  26. return 1;
  27. }
  28. #define ASSERT_EQ(MSG, EXPECTED, ACTUAL) _require_eq((EXPECTED), (ACTUAL), __LINE__, (MSG))
  29. /* Helpers */
  30. #define NUM_TEST_GUIDS 5
  31. static struct {
  32. char *str;
  33. Uint64 upper, lower;
  34. } test_guids[NUM_TEST_GUIDS] = {
  35. { "0000000000000000" "ffffffffffffffff",
  36. 0x0000000000000000, 0xfffffffffffffffflu },
  37. { "0011223344556677" "8091a2b3c4d5e6f0",
  38. 0x0011223344556677lu, 0x8091a2b3c4d5e6f0lu },
  39. { "a011223344556677" "8091a2b3c4d5e6f0",
  40. 0xa011223344556677lu, 0x8091a2b3c4d5e6f0lu },
  41. { "a011223344556677" "8091a2b3c4d5e6f1",
  42. 0xa011223344556677lu, 0x8091a2b3c4d5e6f1lu },
  43. { "a011223344556677" "8191a2b3c4d5e6f0",
  44. 0xa011223344556677lu, 0x8191a2b3c4d5e6f0lu },
  45. };
  46. static void
  47. upper_lower_to_bytestring(Uint8* out, Uint64 upper, Uint64 lower)
  48. {
  49. Uint64 values[2];
  50. int i, k;
  51. values[0] = upper;
  52. values [1] = lower;
  53. for (i = 0; i < 2; ++i) {
  54. Uint64 v = values[i];
  55. for (k = 0; k < 8; ++k) {
  56. *out++ = v >> 56;
  57. v <<= 8;
  58. }
  59. }
  60. }
  61. /* ================= Test Case Implementation ================== */
  62. /**
  63. * @brief Check String-to-GUID conversion
  64. *
  65. * @sa SDL_GUIDFromString
  66. */
  67. static void
  68. TestGuidFromString(void)
  69. {
  70. int i;
  71. for (i = 0; i < NUM_TEST_GUIDS; ++i) {
  72. Uint8 expected[16];
  73. SDL_GUID guid;
  74. upper_lower_to_bytestring(expected,
  75. test_guids[i].upper, test_guids[i].lower);
  76. guid = SDL_GUIDFromString(test_guids[i].str);
  77. if (!ASSERT_EQ("GUID from string", 0, SDL_memcmp(expected, guid.data, 16))) {
  78. SDL_Log(" GUID was: '%s'", test_guids[i].str);
  79. }
  80. }
  81. }
  82. /**
  83. * @brief Check GUID-to-String conversion
  84. *
  85. * @sa SDL_GUIDToString
  86. */
  87. static void
  88. TestGuidToString(void)
  89. {
  90. int i;
  91. for (i = 0; i < NUM_TEST_GUIDS; ++i) {
  92. const int guid_str_offset = 4;
  93. char guid_str_buf[64];
  94. char *guid_str = guid_str_buf + guid_str_offset;
  95. SDL_GUID guid;
  96. int size;
  97. upper_lower_to_bytestring(guid.data,
  98. test_guids[i].upper, test_guids[i].lower);
  99. /* Serialise to limited-length buffers */
  100. for (size = 0; size <= 36; ++size) {
  101. const Uint8 fill_char = size + 0xa0;
  102. Uint32 expected_prefix;
  103. Uint32 actual_prefix;
  104. int written_size;
  105. SDL_memset(guid_str_buf, fill_char, sizeof(guid_str_buf));
  106. SDL_GUIDToString(guid, guid_str, size);
  107. /* Check bytes before guid_str_buf */
  108. expected_prefix = fill_char | (fill_char << 8) | (fill_char << 16) | (fill_char << 24);
  109. SDL_memcpy(&actual_prefix, guid_str_buf, 4);
  110. if (!ASSERT_EQ("String buffer memory before output untouched: ", expected_prefix, actual_prefix)) {
  111. SDL_Log(" at size=%d", size);
  112. }
  113. /* Check that we did not overwrite too much */
  114. written_size = 0;
  115. while ((guid_str[written_size] & 0xff) != fill_char && written_size < 256) {
  116. ++written_size;
  117. }
  118. if (!ASSERT_EQ("Output length is within expected bounds", 1, written_size <= size)) {
  119. SDL_Log(" with length %d: wrote %d of %d permitted bytes",
  120. size, written_size, size);
  121. }
  122. if (size >= 33) {
  123. if (!ASSERT_EQ("GUID string equality", 0, SDL_strcmp(guid_str, test_guids[i].str))) {
  124. SDL_Log(" from string: %s", test_guids[i].str);
  125. }
  126. }
  127. }
  128. }
  129. }
  130. int
  131. main(int argc, char *argv[])
  132. {
  133. /* Enable standard application logging */
  134. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  135. TestGuidFromString();
  136. TestGuidToString();
  137. return _error_count > 0;
  138. }