Debian Codegraph @

test/testautomation_guid.c

raw
1 /**
2 * GUID test suite
3 */
4
5 #include "SDL.h"
6 #include "SDL_test.h"
7
8 #ifdef HAVE_STDINT_H
9 #include <stdint.h>
10 #endif
11
12 /* ================= Test Case Implementation ================== */
13
14 /* Helper functions */
15
16 #define NUM_TEST_GUIDS 5
17
18 #ifndef UINT64_C
19 #ifdef _MSC_VER
20 #define UINT64_C(x) x##ui64
21 #elif defined(_LP64)
22 #define UINT64_C(x) x##UL
23 #else
24 #define UINT64_C(x) x##ULL
25 #endif
26 #endif
27
28 static struct
29 {
30 char *str;
31 Uint64 upper, lower;
32 } test_guids[NUM_TEST_GUIDS] = {
33 { "0000000000000000"
34 "ffffffffffffffff",
35 UINT64_C(0x0000000000000000), UINT64_C(0xffffffffffffffff) },
36 { "0011223344556677"
37 "8091a2b3c4d5e6f0",
38 UINT64_C(0x0011223344556677), UINT64_C(0x8091a2b3c4d5e6f0) },
39 { "a011223344556677"
40 "8091a2b3c4d5e6f0",
41 UINT64_C(0xa011223344556677), UINT64_C(0x8091a2b3c4d5e6f0) },
42 { "a011223344556677"
43 "8091a2b3c4d5e6f1",
44 UINT64_C(0xa011223344556677), UINT64_C(0x8091a2b3c4d5e6f1) },
45 { "a011223344556677"
46 "8191a2b3c4d5e6f0",
47 UINT64_C(0xa011223344556677), UINT64_C(0x8191a2b3c4d5e6f0) },
48 };
49
50 static void
51 upper_lower_to_bytestring(Uint8 *out, Uint64 upper, Uint64 lower)
52 {
53 Uint64 values[2];
54 int i, k;
55
56 values[0] = upper;
57 values[1] = lower;
58
59 for (i = 0; i < 2; ++i) {
60 Uint64 v = values[i];
61
62 for (k = 0; k < 8; ++k) {
63 *out++ = v >> 56;
64 v <<= 8;
65 }
66 }
67 }
68
69 /* Test case functions */
70
71 /**
72 * @brief Check String-to-GUID conversion
73 *
74 * @sa SDL_GUIDFromString
75 */
76 static int
77 TestGuidFromString(void *arg)
78 {
79 int i;
80
81 SDLTest_AssertPass("Call to SDL_GUIDFromString");
82 for (i = 0; i < NUM_TEST_GUIDS; ++i) {
83 Uint8 expected[16];
84 SDL_GUID guid;
85
86 upper_lower_to_bytestring(expected,
87 test_guids[i].upper, test_guids[i].lower);
88
89 guid = SDL_GUIDFromString(test_guids[i].str);
90 SDLTest_AssertCheck(SDL_memcmp(expected, guid.data, 16) == 0, "GUID from string, GUID was: '%s'", test_guids[i].str);
91 }
92
93 return TEST_COMPLETED;
94 }
95
96 /**
97 * @brief Check GUID-to-String conversion
98 *
99 * @sa SDL_GUIDToString
100 */
101 static int
102 TestGuidToString(void *arg)
103 {
104 int i;
105
106 SDLTest_AssertPass("Call to SDL_GUIDToString");
107 for (i = 0; i < NUM_TEST_GUIDS; ++i) {
108 const int guid_str_offset = 4;
109 char guid_str_buf[64];
110 char *guid_str = guid_str_buf + guid_str_offset;
111 SDL_GUID guid;
112 int size;
113
114 upper_lower_to_bytestring(guid.data,
115 test_guids[i].upper, test_guids[i].lower);
116
117 /* Serialise to limited-length buffers */
118 for (size = 0; size <= 36; ++size) {
119 const Uint8 fill_char = size + 0xa0;
120 Uint32 expected_prefix;
121 Uint32 actual_prefix;
122 int written_size;
123
124 SDL_memset(guid_str_buf, fill_char, sizeof(guid_str_buf));
125 SDL_GUIDToString(guid, guid_str, size);
126
127 /* Check bytes before guid_str_buf */
128 expected_prefix = fill_char | (fill_char << 8) | (fill_char << 16) | (((Uint32)fill_char) << 24);
129 SDL_memcpy(&actual_prefix, guid_str_buf, 4);
130 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);
131
132 /* Check that we did not overwrite too much */
133 written_size = 0;
134 while ((guid_str[written_size] & 0xff) != fill_char && written_size < 256) {
135 ++written_size;
136 }
137 SDLTest_AssertCheck(written_size <= size, "Output length is within expected bounds, with length %d: wrote %d of %d permitted bytes", size, written_size, size);
138 if (size >= 33) {
139 SDLTest_AssertCheck(SDL_strcmp(guid_str, test_guids[i].str) == 0, "GUID string equality, from string: %s", test_guids[i].str);
140 }
141 }
142 }
143
144 return TEST_COMPLETED;
145 }
146
147 /* ================= Test References ================== */
148
149 /* GUID routine test cases */
150 static const SDLTest_TestCaseReference guidTest1 = {
151 (SDLTest_TestCaseFp)TestGuidFromString, "TestGuidFromString", "Call to SDL_GUIDFromString", TEST_ENABLED
152 };
153
154 static const SDLTest_TestCaseReference guidTest2 = {
155 (SDLTest_TestCaseFp)TestGuidToString, "TestGuidToString", "Call to SDL_GUIDToString", TEST_ENABLED
156 };
157
158 /* Sequence of GUID routine test cases */
159 static const SDLTest_TestCaseReference *guidTests[] = {
160 &guidTest1,
161 &guidTest2,
162 NULL
163 };
164
165 /* GUID routine test suite (global) */
166 SDLTest_TestSuiteReference guidTestSuite = {
167 "GUID",
168 NULL,
169 guidTests,
170 NULL
171 };