VirtualBox

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

Last change on this file since 97242 was 96407, checked in by vboxsync, 2 years ago

scm copyright and license note update

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