1 | /* $Id: tstRTUuid.cpp 48935 2013-10-07 21:19:37Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT Testcase - UUID.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2010 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*******************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *******************************************************************************/
|
---|
31 | #include <iprt/uuid.h>
|
---|
32 | #include <iprt/test.h>
|
---|
33 | #include <iprt/stream.h>
|
---|
34 | #include <iprt/err.h>
|
---|
35 | #include <iprt/string.h>
|
---|
36 | #include <iprt/initterm.h>
|
---|
37 |
|
---|
38 |
|
---|
39 | int main(int argc, char **argv)
|
---|
40 | {
|
---|
41 | RTTEST hTest;
|
---|
42 | int rc = RTTestInitAndCreate("tstRTUuid", &hTest);
|
---|
43 | if (rc)
|
---|
44 | return rc;
|
---|
45 | RTTestBanner(hTest);
|
---|
46 |
|
---|
47 |
|
---|
48 | #define CHECK_RC() \
|
---|
49 | do { if (RT_FAILURE(rc)) { RTTestFailed(hTest, "line %d: rc=%Rrc", __LINE__, rc); } } while (0)
|
---|
50 |
|
---|
51 | RTTestSub(hTest, "RTUuidClear & RTUuisIsNull");
|
---|
52 | RTUUID UuidNull;
|
---|
53 | rc = RTUuidClear(&UuidNull); CHECK_RC();
|
---|
54 |
|
---|
55 | RTTEST_CHECK(hTest, RTUuidIsNull(&UuidNull));
|
---|
56 | RTTEST_CHECK(hTest, RTUuidCompare(&UuidNull, &UuidNull) == 0);
|
---|
57 |
|
---|
58 | RTTestSub(hTest, "RTUuidCreate");
|
---|
59 | RTUUID Uuid;
|
---|
60 | rc = RTUuidCreate(&Uuid); CHECK_RC();
|
---|
61 | RTTEST_CHECK(hTest, !RTUuidIsNull(&Uuid));
|
---|
62 | RTTEST_CHECK(hTest, RTUuidCompare(&Uuid, &Uuid) == 0);
|
---|
63 | RTTEST_CHECK(hTest, RTUuidCompare(&Uuid, &UuidNull) > 0);
|
---|
64 | RTTEST_CHECK(hTest, RTUuidCompare(&UuidNull, &Uuid) < 0);
|
---|
65 |
|
---|
66 | RTTestSub(hTest, "RTUuidToStr");
|
---|
67 | char sz[RTUUID_STR_LENGTH];
|
---|
68 | rc = RTUuidToStr(&Uuid, sz, sizeof(sz)); CHECK_RC();
|
---|
69 | RTTEST_CHECK(hTest, strlen(sz) == RTUUID_STR_LENGTH - 1);
|
---|
70 | RTTestPrintf(hTest, RTTESTLVL_INFO, "UUID=%s\n", sz);
|
---|
71 |
|
---|
72 | RTTestSub(hTest, "RTUuidFromStr");
|
---|
73 | RTUUID Uuid2;
|
---|
74 | rc = RTUuidFromStr(&Uuid2, sz); CHECK_RC();
|
---|
75 | RTTEST_CHECK(hTest, RTUuidCompare(&Uuid, &Uuid2) == 0);
|
---|
76 |
|
---|
77 | char *psz = (char *)RTTestGuardedAllocTail(hTest, RTUUID_STR_LENGTH);
|
---|
78 | if (psz)
|
---|
79 | {
|
---|
80 | RTStrPrintf(psz, RTUUID_STR_LENGTH, "%s", sz);
|
---|
81 | RTTESTI_CHECK_RC(RTUuidFromStr(&Uuid2, psz), VINF_SUCCESS);
|
---|
82 | RTTEST_CHECK(hTest, RTUuidCompare(&Uuid, &Uuid2) == 0);
|
---|
83 | for (unsigned off = 1; off < RTUUID_STR_LENGTH; off++)
|
---|
84 | {
|
---|
85 | char *psz2 = psz + off;
|
---|
86 | RTStrPrintf(psz2, RTUUID_STR_LENGTH - off, "%s", sz);
|
---|
87 | RTTESTI_CHECK_RC(RTUuidFromStr(&Uuid2, psz2), VERR_INVALID_UUID_FORMAT);
|
---|
88 | }
|
---|
89 | RTTestGuardedFree(hTest, psz);
|
---|
90 | }
|
---|
91 |
|
---|
92 | RTUuidClear(&Uuid2);
|
---|
93 | char sz2[RTUUID_STR_LENGTH + 2];
|
---|
94 | RTStrPrintf(sz2, sizeof(sz2), "{%s}", sz);
|
---|
95 | rc = RTUuidFromStr(&Uuid2, sz2); CHECK_RC();
|
---|
96 | RTTEST_CHECK(hTest, RTUuidCompare(&Uuid, &Uuid2) == 0);
|
---|
97 |
|
---|
98 | psz = (char *)RTTestGuardedAllocTail(hTest, RTUUID_STR_LENGTH + 2);
|
---|
99 | if (psz)
|
---|
100 | {
|
---|
101 | RTStrPrintf(psz, RTUUID_STR_LENGTH + 2, "{%s}", sz);
|
---|
102 | RTTESTI_CHECK_RC(RTUuidFromStr(&Uuid2, psz), VINF_SUCCESS);
|
---|
103 | RTTEST_CHECK(hTest, RTUuidCompare(&Uuid, &Uuid2) == 0);
|
---|
104 | for (unsigned off = 1; off < RTUUID_STR_LENGTH + 2; off++)
|
---|
105 | {
|
---|
106 | char *psz2 = psz + off;
|
---|
107 | RTStrPrintf(psz2, RTUUID_STR_LENGTH + 2 - off, "{%s}", sz);
|
---|
108 | RTTESTI_CHECK_RC(RTUuidFromStr(&Uuid2, psz2), VERR_INVALID_UUID_FORMAT);
|
---|
109 | }
|
---|
110 | RTTestGuardedFree(hTest, psz);
|
---|
111 | }
|
---|
112 |
|
---|
113 | RTTestSub(hTest, "RTUuidToUtf16");
|
---|
114 | RTUTF16 wsz[RTUUID_STR_LENGTH];
|
---|
115 | rc = RTUuidToUtf16(&Uuid, wsz, sizeof(wsz)); CHECK_RC();
|
---|
116 | RTTEST_CHECK(hTest, RTUtf16Len(wsz) == RTUUID_STR_LENGTH - 1);
|
---|
117 |
|
---|
118 | RTTestSub(hTest, "RTUuidFromUtf16");
|
---|
119 | rc = RTUuidFromUtf16(&Uuid2, wsz); CHECK_RC();
|
---|
120 | RTTEST_CHECK(hTest, RTUuidCompare(&Uuid, &Uuid2) == 0);
|
---|
121 |
|
---|
122 | RTUTF16 *pwsz;
|
---|
123 | rc = RTStrToUtf16(sz2, &pwsz);
|
---|
124 | RTTEST_CHECK(hTest, rc == VINF_SUCCESS);
|
---|
125 | if (RT_SUCCESS(rc))
|
---|
126 | {
|
---|
127 | RTTESTI_CHECK_RC(RTUuidFromUtf16(&Uuid2, pwsz), VINF_SUCCESS);
|
---|
128 | RTTEST_CHECK(hTest, RTUuidCompare(&Uuid, &Uuid2) == 0);
|
---|
129 | RTUTF16 *pwsz2 = (RTUTF16*)RTTestGuardedAllocTail(hTest, 2 * (RTUUID_STR_LENGTH + 2));
|
---|
130 | if (pwsz2)
|
---|
131 | {
|
---|
132 | memcpy(pwsz2, pwsz, 2 * (RTUUID_STR_LENGTH + 2));
|
---|
133 | RTTESTI_CHECK_RC(RTUuidFromUtf16(&Uuid2, pwsz2), VINF_SUCCESS);
|
---|
134 | RTTEST_CHECK(hTest, RTUuidCompare(&Uuid, &Uuid2) == 0);
|
---|
135 | for (unsigned off = 1; off < RTUUID_STR_LENGTH + 2; off++)
|
---|
136 | {
|
---|
137 | RTUTF16 *pwsz3 = pwsz2 + off;
|
---|
138 | memcpy(pwsz3, pwsz, 2 * (RTUUID_STR_LENGTH + 1 - off));
|
---|
139 | pwsz3[RTUUID_STR_LENGTH + 1 - off] = 0;
|
---|
140 | RTTESTI_CHECK_RC(RTUuidFromUtf16(&Uuid2, pwsz3), VERR_INVALID_UUID_FORMAT);
|
---|
141 | }
|
---|
142 | RTTestGuardedFree(hTest, pwsz2);
|
---|
143 | }
|
---|
144 | RTUtf16Free(pwsz);
|
---|
145 | }
|
---|
146 |
|
---|
147 | RTTestSub(hTest, "RTUuidCompareStr");
|
---|
148 | RTTEST_CHECK(hTest, RTUuidCompareStr(&Uuid, sz) == 0);
|
---|
149 | RTTEST_CHECK(hTest, RTUuidCompareStr(&Uuid, "00000000-0000-0000-0000-000000000000") > 0);
|
---|
150 | RTTEST_CHECK(hTest, RTUuidCompareStr(&UuidNull, "00000000-0000-0000-0000-000000000000") == 0);
|
---|
151 |
|
---|
152 | RTTestSub(hTest, "RTUuidCompare2Strs");
|
---|
153 | RTTEST_CHECK(hTest, RTUuidCompare2Strs(sz, sz) == 0);
|
---|
154 | RTTEST_CHECK(hTest, RTUuidCompare2Strs(sz, "00000000-0000-0000-0000-000000000000") > 0);
|
---|
155 | RTTEST_CHECK(hTest, RTUuidCompare2Strs("00000000-0000-0000-0000-000000000000", sz) < 0);
|
---|
156 | RTTEST_CHECK(hTest, RTUuidCompare2Strs("00000000-0000-0000-0000-000000000000", "00000000-0000-0000-0000-000000000000") == 0);
|
---|
157 | RTTEST_CHECK(hTest, RTUuidCompare2Strs("d95d883b-f91d-4ce5-a5c5-d08bb6a85dec", "a56193c7-3e0b-4c03-9d66-56efb45082f7") > 0);
|
---|
158 | RTTEST_CHECK(hTest, RTUuidCompare2Strs("a56193c7-3e0b-4c03-9d66-56efb45082f7", "d95d883b-f91d-4ce5-a5c5-d08bb6a85dec") < 0);
|
---|
159 |
|
---|
160 | /*
|
---|
161 | * Check the binary representation.
|
---|
162 | */
|
---|
163 | RTTestSub(hTest, "Binary representation");
|
---|
164 | RTUUID Uuid3;
|
---|
165 | Uuid3.au8[0] = 0x01;
|
---|
166 | Uuid3.au8[1] = 0x23;
|
---|
167 | Uuid3.au8[2] = 0x45;
|
---|
168 | Uuid3.au8[3] = 0x67;
|
---|
169 | Uuid3.au8[4] = 0x89;
|
---|
170 | Uuid3.au8[5] = 0xab;
|
---|
171 | Uuid3.au8[6] = 0xcd;
|
---|
172 | Uuid3.au8[7] = 0x4f;
|
---|
173 | Uuid3.au8[8] = 0x10;
|
---|
174 | Uuid3.au8[9] = 0xb2;
|
---|
175 | Uuid3.au8[10] = 0x54;
|
---|
176 | Uuid3.au8[11] = 0x76;
|
---|
177 | Uuid3.au8[12] = 0x98;
|
---|
178 | Uuid3.au8[13] = 0xba;
|
---|
179 | Uuid3.au8[14] = 0xdc;
|
---|
180 | Uuid3.au8[15] = 0xfe;
|
---|
181 | Uuid3.Gen.u8ClockSeqHiAndReserved = (Uuid3.Gen.u8ClockSeqHiAndReserved & 0x3f) | 0x80;
|
---|
182 | Uuid3.Gen.u16TimeHiAndVersion = (Uuid3.Gen.u16TimeHiAndVersion & 0x0fff) | 0x4000;
|
---|
183 | const char *pszUuid3 = "67452301-ab89-4fcd-90b2-547698badcfe";
|
---|
184 | rc = RTUuidToStr(&Uuid3, sz, sizeof(sz)); CHECK_RC();
|
---|
185 | RTTEST_CHECK(hTest, strcmp(sz, pszUuid3) == 0);
|
---|
186 | rc = RTUuidFromStr(&Uuid, pszUuid3); CHECK_RC();
|
---|
187 | RTTEST_CHECK(hTest, RTUuidCompare(&Uuid, &Uuid3) == 0);
|
---|
188 | RTTEST_CHECK(hTest, memcmp(&Uuid3, &Uuid, sizeof(Uuid)) == 0);
|
---|
189 |
|
---|
190 | /*
|
---|
191 | * checking the clock seq and time hi and version bits...
|
---|
192 | */
|
---|
193 | RTTestSub(hTest, "Clock seq, time hi, version bits");
|
---|
194 | RTUUID Uuid4Changes;
|
---|
195 | Uuid4Changes.au64[0] = 0;
|
---|
196 | Uuid4Changes.au64[1] = 0;
|
---|
197 |
|
---|
198 | RTUUID Uuid4Prev;
|
---|
199 | RTUuidCreate(&Uuid4Prev);
|
---|
200 |
|
---|
201 | for (unsigned i = 0; i < 1024; i++)
|
---|
202 | {
|
---|
203 | RTUUID Uuid4;
|
---|
204 | RTUuidCreate(&Uuid4);
|
---|
205 |
|
---|
206 | Uuid4Changes.au64[0] |= Uuid4.au64[0] ^ Uuid4Prev.au64[0];
|
---|
207 | Uuid4Changes.au64[1] |= Uuid4.au64[1] ^ Uuid4Prev.au64[1];
|
---|
208 |
|
---|
209 | #if 0 /** @todo make a bit string/dumper similar to %Rhxs/d. */
|
---|
210 | RTPrintf("tstUuid: %d %d %d %d-%d %d %d %d %d %d %d %d-%d %d %d %d ; %d %d %d %d-%d %d %d %d %d %d %d %d-%d %d %d %d\n",
|
---|
211 | !!(Uuid4.Gen.u16ClockSeq & RT_BIT(0)),
|
---|
212 | !!(Uuid4.Gen.u16ClockSeq & RT_BIT(1)),
|
---|
213 | !!(Uuid4.Gen.u16ClockSeq & RT_BIT(2)),
|
---|
214 | !!(Uuid4.Gen.u16ClockSeq & RT_BIT(3)),
|
---|
215 | !!(Uuid4.Gen.u16ClockSeq & RT_BIT(4)),
|
---|
216 | !!(Uuid4.Gen.u16ClockSeq & RT_BIT(5)),
|
---|
217 | !!(Uuid4.Gen.u16ClockSeq & RT_BIT(6)),
|
---|
218 | !!(Uuid4.Gen.u16ClockSeq & RT_BIT(7)),
|
---|
219 | !!(Uuid4.Gen.u16ClockSeq & RT_BIT(8)),
|
---|
220 | !!(Uuid4.Gen.u16ClockSeq & RT_BIT(9)),
|
---|
221 | !!(Uuid4.Gen.u16ClockSeq & RT_BIT(10)),
|
---|
222 | !!(Uuid4.Gen.u16ClockSeq & RT_BIT(11)),
|
---|
223 | !!(Uuid4.Gen.u16ClockSeq & RT_BIT(12)),
|
---|
224 | !!(Uuid4.Gen.u16ClockSeq & RT_BIT(13)),
|
---|
225 | !!(Uuid4.Gen.u16ClockSeq & RT_BIT(14)),
|
---|
226 | !!(Uuid4.Gen.u16ClockSeq & RT_BIT(15)),
|
---|
227 |
|
---|
228 | !!(Uuid4.Gen.u16TimeHiAndVersion & RT_BIT(0)),
|
---|
229 | !!(Uuid4.Gen.u16TimeHiAndVersion & RT_BIT(1)),
|
---|
230 | !!(Uuid4.Gen.u16TimeHiAndVersion & RT_BIT(2)),
|
---|
231 | !!(Uuid4.Gen.u16TimeHiAndVersion & RT_BIT(3)),
|
---|
232 | !!(Uuid4.Gen.u16TimeHiAndVersion & RT_BIT(4)),
|
---|
233 | !!(Uuid4.Gen.u16TimeHiAndVersion & RT_BIT(5)),
|
---|
234 | !!(Uuid4.Gen.u16TimeHiAndVersion & RT_BIT(6)),
|
---|
235 | !!(Uuid4.Gen.u16TimeHiAndVersion & RT_BIT(7)),
|
---|
236 | !!(Uuid4.Gen.u16TimeHiAndVersion & RT_BIT(8)),
|
---|
237 | !!(Uuid4.Gen.u16TimeHiAndVersion & RT_BIT(9)),
|
---|
238 | !!(Uuid4.Gen.u16TimeHiAndVersion & RT_BIT(10)),
|
---|
239 | !!(Uuid4.Gen.u16TimeHiAndVersion & RT_BIT(11)),
|
---|
240 | !!(Uuid4.Gen.u16TimeHiAndVersion & RT_BIT(12)),
|
---|
241 | !!(Uuid4.Gen.u16TimeHiAndVersion & RT_BIT(13)),
|
---|
242 | !!(Uuid4.Gen.u16TimeHiAndVersion & RT_BIT(14)),
|
---|
243 | !!(Uuid4.Gen.u16TimeHiAndVersion & RT_BIT(15))
|
---|
244 | );
|
---|
245 | #endif
|
---|
246 | Uuid4Prev = Uuid4;
|
---|
247 | }
|
---|
248 |
|
---|
249 | RTUUID Uuid4Fixed;
|
---|
250 | Uuid4Fixed.au64[0] = ~Uuid4Changes.au64[0];
|
---|
251 | Uuid4Fixed.au64[1] = ~Uuid4Changes.au64[1];
|
---|
252 | RTTestPrintf(hTest, RTTESTLVL_INFO, "fixed bits: %RTuuid (mask)\n", &Uuid4Fixed);
|
---|
253 | RTTestPrintf(hTest, RTTESTLVL_INFO, "tstUuid: raw: %.*Rhxs\n", sizeof(Uuid4Fixed), &Uuid4Fixed);
|
---|
254 |
|
---|
255 | Uuid4Prev.au64[0] &= Uuid4Fixed.au64[0];
|
---|
256 | Uuid4Prev.au64[1] &= Uuid4Fixed.au64[1];
|
---|
257 | RTTestPrintf(hTest, RTTESTLVL_INFO, "tstUuid: fixed bits: %RTuuid (value)\n", &Uuid4Prev);
|
---|
258 | RTTestPrintf(hTest, RTTESTLVL_INFO, "tstUuid: raw: %.*Rhxs\n", sizeof(Uuid4Prev), &Uuid4Prev);
|
---|
259 |
|
---|
260 | /*
|
---|
261 | * Summary.
|
---|
262 | */
|
---|
263 | return RTTestSummaryAndDestroy(hTest);
|
---|
264 | }
|
---|
265 |
|
---|