VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstRTUuid.cpp@ 91472

Last change on this file since 91472 was 82968, checked in by vboxsync, 5 years ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 10.9 KB
Line 
1/* $Id: tstRTUuid.cpp 82968 2020-02-04 10:35:17Z vboxsync $ */
2/** @file
3 * IPRT Testcase - UUID.
4 */
5
6/*
7 * Copyright (C) 2006-2020 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#include <iprt/utf16.h>
38
39
40int main()
41{
42 RTTEST hTest;
43 int rc = RTTestInitAndCreate("tstRTUuid", &hTest);
44 if (rc)
45 return rc;
46 RTTestBanner(hTest);
47
48
49#define CHECK_RC() \
50 do { if (RT_FAILURE(rc)) { RTTestFailed(hTest, "line %d: rc=%Rrc", __LINE__, rc); } } while (0)
51
52 RTTestSub(hTest, "RTUuidClear & RTUuisIsNull");
53 RTUUID UuidNull;
54 rc = RTUuidClear(&UuidNull); CHECK_RC();
55
56 RTTEST_CHECK(hTest, RTUuidIsNull(&UuidNull));
57 RTTEST_CHECK(hTest, RTUuidCompare(&UuidNull, &UuidNull) == 0);
58
59 RTTestSub(hTest, "RTUuidCreate");
60 RTUUID Uuid;
61 rc = RTUuidCreate(&Uuid); CHECK_RC();
62 RTTEST_CHECK(hTest, !RTUuidIsNull(&Uuid));
63 RTTEST_CHECK(hTest, RTUuidCompare(&Uuid, &Uuid) == 0);
64 RTTEST_CHECK(hTest, RTUuidCompare(&Uuid, &UuidNull) > 0);
65 RTTEST_CHECK(hTest, RTUuidCompare(&UuidNull, &Uuid) < 0);
66
67 RTTestSub(hTest, "RTUuidToStr");
68 char sz[RTUUID_STR_LENGTH];
69 rc = RTUuidToStr(&Uuid, sz, sizeof(sz)); CHECK_RC();
70 RTTEST_CHECK(hTest, strlen(sz) == RTUUID_STR_LENGTH - 1);
71 RTTestPrintf(hTest, RTTESTLVL_INFO, "UUID=%s\n", sz);
72
73 RTTestSub(hTest, "RTUuidFromStr");
74 RTUUID Uuid2;
75 rc = RTUuidFromStr(&Uuid2, sz); CHECK_RC();
76 RTTEST_CHECK(hTest, RTUuidCompare(&Uuid, &Uuid2) == 0);
77
78 char *psz = (char *)RTTestGuardedAllocTail(hTest, RTUUID_STR_LENGTH);
79 if (psz)
80 {
81 RTStrPrintf(psz, RTUUID_STR_LENGTH, "%s", sz);
82 RTTESTI_CHECK_RC(RTUuidFromStr(&Uuid2, psz), VINF_SUCCESS);
83 RTTEST_CHECK(hTest, RTUuidCompare(&Uuid, &Uuid2) == 0);
84 for (unsigned off = 1; off < RTUUID_STR_LENGTH; off++)
85 {
86 char *psz2 = psz + off;
87 RTStrPrintf(psz2, RTUUID_STR_LENGTH - off, "%s", sz);
88 RTTESTI_CHECK_RC(RTUuidFromStr(&Uuid2, psz2), VERR_INVALID_UUID_FORMAT);
89 }
90 RTTestGuardedFree(hTest, psz);
91 }
92
93 RTUuidClear(&Uuid2);
94 char sz2[RTUUID_STR_LENGTH + 2];
95 RTStrPrintf(sz2, sizeof(sz2), "{%s}", sz);
96 rc = RTUuidFromStr(&Uuid2, sz2); CHECK_RC();
97 RTTEST_CHECK(hTest, RTUuidCompare(&Uuid, &Uuid2) == 0);
98
99 psz = (char *)RTTestGuardedAllocTail(hTest, RTUUID_STR_LENGTH + 2);
100 if (psz)
101 {
102 RTStrPrintf(psz, RTUUID_STR_LENGTH + 2, "{%s}", sz);
103 RTTESTI_CHECK_RC(RTUuidFromStr(&Uuid2, psz), VINF_SUCCESS);
104 RTTEST_CHECK(hTest, RTUuidCompare(&Uuid, &Uuid2) == 0);
105 for (unsigned off = 1; off < RTUUID_STR_LENGTH + 2; off++)
106 {
107 char *psz2 = psz + off;
108 RTStrPrintf(psz2, RTUUID_STR_LENGTH + 2 - off, "{%s}", sz);
109 RTTESTI_CHECK_RC(RTUuidFromStr(&Uuid2, psz2), VERR_INVALID_UUID_FORMAT);
110 }
111 RTTestGuardedFree(hTest, psz);
112 }
113
114 RTTestSub(hTest, "RTUuidToUtf16");
115 RTUTF16 wsz[RTUUID_STR_LENGTH];
116 rc = RTUuidToUtf16(&Uuid, wsz, sizeof(wsz)); CHECK_RC();
117 RTTEST_CHECK(hTest, RTUtf16Len(wsz) == RTUUID_STR_LENGTH - 1);
118
119 RTTestSub(hTest, "RTUuidFromUtf16");
120 rc = RTUuidFromUtf16(&Uuid2, wsz); CHECK_RC();
121 RTTEST_CHECK(hTest, RTUuidCompare(&Uuid, &Uuid2) == 0);
122
123 RTUTF16 *pwsz;
124 rc = RTStrToUtf16(sz2, &pwsz);
125 RTTEST_CHECK(hTest, rc == VINF_SUCCESS);
126 if (RT_SUCCESS(rc))
127 {
128 RTTESTI_CHECK_RC(RTUuidFromUtf16(&Uuid2, pwsz), VINF_SUCCESS);
129 RTTEST_CHECK(hTest, RTUuidCompare(&Uuid, &Uuid2) == 0);
130 RTUTF16 *pwsz2 = (RTUTF16*)RTTestGuardedAllocTail(hTest, 2 * (RTUUID_STR_LENGTH + 2));
131 if (pwsz2)
132 {
133 memcpy(pwsz2, pwsz, 2 * (RTUUID_STR_LENGTH + 2));
134 RTTESTI_CHECK_RC(RTUuidFromUtf16(&Uuid2, pwsz2), VINF_SUCCESS);
135 RTTEST_CHECK(hTest, RTUuidCompare(&Uuid, &Uuid2) == 0);
136 for (unsigned off = 1; off < RTUUID_STR_LENGTH + 2; off++)
137 {
138 RTUTF16 *pwsz3 = pwsz2 + off;
139 memcpy(pwsz3, pwsz, 2 * (RTUUID_STR_LENGTH + 1 - off));
140 pwsz3[RTUUID_STR_LENGTH + 1 - off] = 0;
141 RTTESTI_CHECK_RC(RTUuidFromUtf16(&Uuid2, pwsz3), VERR_INVALID_UUID_FORMAT);
142 }
143 RTTestGuardedFree(hTest, pwsz2);
144 }
145 RTUtf16Free(pwsz);
146 }
147
148 RTTestSub(hTest, "RTUuidCompareStr");
149 RTTEST_CHECK(hTest, RTUuidCompareStr(&Uuid, sz) == 0);
150 RTTEST_CHECK(hTest, RTUuidCompareStr(&Uuid, "00000000-0000-0000-0000-000000000000") > 0);
151 RTTEST_CHECK(hTest, RTUuidCompareStr(&UuidNull, "00000000-0000-0000-0000-000000000000") == 0);
152
153 RTTestSub(hTest, "RTUuidCompare2Strs");
154 RTTEST_CHECK(hTest, RTUuidCompare2Strs(sz, sz) == 0);
155 RTTEST_CHECK(hTest, RTUuidCompare2Strs(sz, "00000000-0000-0000-0000-000000000000") > 0);
156 RTTEST_CHECK(hTest, RTUuidCompare2Strs("00000000-0000-0000-0000-000000000000", sz) < 0);
157 RTTEST_CHECK(hTest, RTUuidCompare2Strs("00000000-0000-0000-0000-000000000000", "00000000-0000-0000-0000-000000000000") == 0);
158 RTTEST_CHECK(hTest, RTUuidCompare2Strs("d95d883b-f91d-4ce5-a5c5-d08bb6a85dec", "a56193c7-3e0b-4c03-9d66-56efb45082f7") > 0);
159 RTTEST_CHECK(hTest, RTUuidCompare2Strs("a56193c7-3e0b-4c03-9d66-56efb45082f7", "d95d883b-f91d-4ce5-a5c5-d08bb6a85dec") < 0);
160
161 /*
162 * Check the binary representation.
163 */
164 RTTestSub(hTest, "Binary representation");
165 RTUUID Uuid3;
166 Uuid3.au8[0] = 0x01;
167 Uuid3.au8[1] = 0x23;
168 Uuid3.au8[2] = 0x45;
169 Uuid3.au8[3] = 0x67;
170 Uuid3.au8[4] = 0x89;
171 Uuid3.au8[5] = 0xab;
172 Uuid3.au8[6] = 0xcd;
173 Uuid3.au8[7] = 0x4f;
174 Uuid3.au8[8] = 0x10;
175 Uuid3.au8[9] = 0xb2;
176 Uuid3.au8[10] = 0x54;
177 Uuid3.au8[11] = 0x76;
178 Uuid3.au8[12] = 0x98;
179 Uuid3.au8[13] = 0xba;
180 Uuid3.au8[14] = 0xdc;
181 Uuid3.au8[15] = 0xfe;
182 Uuid3.Gen.u8ClockSeqHiAndReserved = (Uuid3.Gen.u8ClockSeqHiAndReserved & 0x3f) | 0x80;
183 Uuid3.Gen.u16TimeHiAndVersion = (Uuid3.Gen.u16TimeHiAndVersion & 0x0fff) | 0x4000;
184 const char *pszUuid3 = "67452301-ab89-4fcd-90b2-547698badcfe";
185 rc = RTUuidToStr(&Uuid3, sz, sizeof(sz)); CHECK_RC();
186 RTTEST_CHECK(hTest, strcmp(sz, pszUuid3) == 0);
187 rc = RTUuidFromStr(&Uuid, pszUuid3); CHECK_RC();
188 RTTEST_CHECK(hTest, RTUuidCompare(&Uuid, &Uuid3) == 0);
189 RTTEST_CHECK(hTest, memcmp(&Uuid3, &Uuid, sizeof(Uuid)) == 0);
190
191 /*
192 * checking the clock seq and time hi and version bits...
193 */
194 RTTestSub(hTest, "Clock seq, time hi, version bits");
195 RTUUID Uuid4Changes;
196 Uuid4Changes.au64[0] = 0;
197 Uuid4Changes.au64[1] = 0;
198
199 RTUUID Uuid4Prev;
200 RTUuidCreate(&Uuid4Prev);
201
202 for (unsigned i = 0; i < 1024; i++)
203 {
204 RTUUID Uuid4;
205 RTUuidCreate(&Uuid4);
206
207 Uuid4Changes.au64[0] |= Uuid4.au64[0] ^ Uuid4Prev.au64[0];
208 Uuid4Changes.au64[1] |= Uuid4.au64[1] ^ Uuid4Prev.au64[1];
209
210#if 0 /** @todo make a bit string/dumper similar to %Rhxs/d. */
211 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",
212 !!(Uuid4.Gen.u16ClockSeq & RT_BIT(0)),
213 !!(Uuid4.Gen.u16ClockSeq & RT_BIT(1)),
214 !!(Uuid4.Gen.u16ClockSeq & RT_BIT(2)),
215 !!(Uuid4.Gen.u16ClockSeq & RT_BIT(3)),
216 !!(Uuid4.Gen.u16ClockSeq & RT_BIT(4)),
217 !!(Uuid4.Gen.u16ClockSeq & RT_BIT(5)),
218 !!(Uuid4.Gen.u16ClockSeq & RT_BIT(6)),
219 !!(Uuid4.Gen.u16ClockSeq & RT_BIT(7)),
220 !!(Uuid4.Gen.u16ClockSeq & RT_BIT(8)),
221 !!(Uuid4.Gen.u16ClockSeq & RT_BIT(9)),
222 !!(Uuid4.Gen.u16ClockSeq & RT_BIT(10)),
223 !!(Uuid4.Gen.u16ClockSeq & RT_BIT(11)),
224 !!(Uuid4.Gen.u16ClockSeq & RT_BIT(12)),
225 !!(Uuid4.Gen.u16ClockSeq & RT_BIT(13)),
226 !!(Uuid4.Gen.u16ClockSeq & RT_BIT(14)),
227 !!(Uuid4.Gen.u16ClockSeq & RT_BIT(15)),
228
229 !!(Uuid4.Gen.u16TimeHiAndVersion & RT_BIT(0)),
230 !!(Uuid4.Gen.u16TimeHiAndVersion & RT_BIT(1)),
231 !!(Uuid4.Gen.u16TimeHiAndVersion & RT_BIT(2)),
232 !!(Uuid4.Gen.u16TimeHiAndVersion & RT_BIT(3)),
233 !!(Uuid4.Gen.u16TimeHiAndVersion & RT_BIT(4)),
234 !!(Uuid4.Gen.u16TimeHiAndVersion & RT_BIT(5)),
235 !!(Uuid4.Gen.u16TimeHiAndVersion & RT_BIT(6)),
236 !!(Uuid4.Gen.u16TimeHiAndVersion & RT_BIT(7)),
237 !!(Uuid4.Gen.u16TimeHiAndVersion & RT_BIT(8)),
238 !!(Uuid4.Gen.u16TimeHiAndVersion & RT_BIT(9)),
239 !!(Uuid4.Gen.u16TimeHiAndVersion & RT_BIT(10)),
240 !!(Uuid4.Gen.u16TimeHiAndVersion & RT_BIT(11)),
241 !!(Uuid4.Gen.u16TimeHiAndVersion & RT_BIT(12)),
242 !!(Uuid4.Gen.u16TimeHiAndVersion & RT_BIT(13)),
243 !!(Uuid4.Gen.u16TimeHiAndVersion & RT_BIT(14)),
244 !!(Uuid4.Gen.u16TimeHiAndVersion & RT_BIT(15))
245 );
246#endif
247 Uuid4Prev = Uuid4;
248 }
249
250 RTUUID Uuid4Fixed;
251 Uuid4Fixed.au64[0] = ~Uuid4Changes.au64[0];
252 Uuid4Fixed.au64[1] = ~Uuid4Changes.au64[1];
253 RTTestPrintf(hTest, RTTESTLVL_INFO, "fixed bits: %RTuuid (mask)\n", &Uuid4Fixed);
254 RTTestPrintf(hTest, RTTESTLVL_INFO, "tstUuid: raw: %.*Rhxs\n", sizeof(Uuid4Fixed), &Uuid4Fixed);
255
256 Uuid4Prev.au64[0] &= Uuid4Fixed.au64[0];
257 Uuid4Prev.au64[1] &= Uuid4Fixed.au64[1];
258 RTTestPrintf(hTest, RTTESTLVL_INFO, "tstUuid: fixed bits: %RTuuid (value)\n", &Uuid4Prev);
259 RTTestPrintf(hTest, RTTESTLVL_INFO, "tstUuid: raw: %.*Rhxs\n", sizeof(Uuid4Prev), &Uuid4Prev);
260
261 /*
262 * Summary.
263 */
264 return RTTestSummaryAndDestroy(hTest);
265}
266
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette