1 | /* $Id: tstRTUuid.cpp 28800 2010-04-27 08:22:32Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT Testcase - UUID.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 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 | RTTestSub(hTest, "RTUuidToUtf16");
|
---|
78 | RTUTF16 wsz[RTUUID_STR_LENGTH];
|
---|
79 | rc = RTUuidToUtf16(&Uuid, wsz, sizeof(wsz)); CHECK_RC();
|
---|
80 | RTTEST_CHECK(hTest, RTUtf16Len(wsz) == RTUUID_STR_LENGTH - 1);
|
---|
81 |
|
---|
82 | RTTestSub(hTest, "RTUuidFromUtf16");
|
---|
83 | rc = RTUuidFromUtf16(&Uuid2, wsz); CHECK_RC();
|
---|
84 | RTTEST_CHECK(hTest, RTUuidCompare(&Uuid, &Uuid2) == 0);
|
---|
85 |
|
---|
86 | RTTestSub(hTest, "RTUuidCompareStr");
|
---|
87 | RTTEST_CHECK(hTest, RTUuidCompareStr(&Uuid, sz) == 0);
|
---|
88 | RTTEST_CHECK(hTest, RTUuidCompareStr(&Uuid, "00000000-0000-0000-0000-000000000000") > 0);
|
---|
89 | RTTEST_CHECK(hTest, RTUuidCompareStr(&UuidNull, "00000000-0000-0000-0000-000000000000") == 0);
|
---|
90 |
|
---|
91 | RTTestSub(hTest, "RTUuidCompare2Strs");
|
---|
92 | RTTEST_CHECK(hTest, RTUuidCompare2Strs(sz, sz) == 0);
|
---|
93 | RTTEST_CHECK(hTest, RTUuidCompare2Strs(sz, "00000000-0000-0000-0000-000000000000") > 0);
|
---|
94 | RTTEST_CHECK(hTest, RTUuidCompare2Strs("00000000-0000-0000-0000-000000000000", sz) < 0);
|
---|
95 | RTTEST_CHECK(hTest, RTUuidCompare2Strs("00000000-0000-0000-0000-000000000000", "00000000-0000-0000-0000-000000000000") == 0);
|
---|
96 | RTTEST_CHECK(hTest, RTUuidCompare2Strs("d95d883b-f91d-4ce5-a5c5-d08bb6a85dec", "a56193c7-3e0b-4c03-9d66-56efb45082f7") > 0);
|
---|
97 | RTTEST_CHECK(hTest, RTUuidCompare2Strs("a56193c7-3e0b-4c03-9d66-56efb45082f7", "d95d883b-f91d-4ce5-a5c5-d08bb6a85dec") < 0);
|
---|
98 |
|
---|
99 | /*
|
---|
100 | * Check the binary representation.
|
---|
101 | */
|
---|
102 | RTTestSub(hTest, "Binary representation");
|
---|
103 | RTUUID Uuid3;
|
---|
104 | Uuid3.au8[0] = 0x01;
|
---|
105 | Uuid3.au8[1] = 0x23;
|
---|
106 | Uuid3.au8[2] = 0x45;
|
---|
107 | Uuid3.au8[3] = 0x67;
|
---|
108 | Uuid3.au8[4] = 0x89;
|
---|
109 | Uuid3.au8[5] = 0xab;
|
---|
110 | Uuid3.au8[6] = 0xcd;
|
---|
111 | Uuid3.au8[7] = 0x4f;
|
---|
112 | Uuid3.au8[8] = 0x10;
|
---|
113 | Uuid3.au8[9] = 0xb2;
|
---|
114 | Uuid3.au8[10] = 0x54;
|
---|
115 | Uuid3.au8[11] = 0x76;
|
---|
116 | Uuid3.au8[12] = 0x98;
|
---|
117 | Uuid3.au8[13] = 0xba;
|
---|
118 | Uuid3.au8[14] = 0xdc;
|
---|
119 | Uuid3.au8[15] = 0xfe;
|
---|
120 | Uuid3.Gen.u8ClockSeqHiAndReserved = (Uuid3.Gen.u8ClockSeqHiAndReserved & 0x3f) | 0x80;
|
---|
121 | Uuid3.Gen.u16TimeHiAndVersion = (Uuid3.Gen.u16TimeHiAndVersion & 0x0fff) | 0x4000;
|
---|
122 | const char *pszUuid3 = "67452301-ab89-4fcd-90b2-547698badcfe";
|
---|
123 | rc = RTUuidToStr(&Uuid3, sz, sizeof(sz)); CHECK_RC();
|
---|
124 | RTTEST_CHECK(hTest, strcmp(sz, pszUuid3) == 0);
|
---|
125 | rc = RTUuidFromStr(&Uuid, pszUuid3); CHECK_RC();
|
---|
126 | RTTEST_CHECK(hTest, RTUuidCompare(&Uuid, &Uuid3) == 0);
|
---|
127 | RTTEST_CHECK(hTest, memcmp(&Uuid3, &Uuid, sizeof(Uuid)) == 0);
|
---|
128 |
|
---|
129 | /*
|
---|
130 | * checking the clock seq and time hi and version bits...
|
---|
131 | */
|
---|
132 | RTTestSub(hTest, "Clock seq, time hi, version bits");
|
---|
133 | RTUUID Uuid4Changes;
|
---|
134 | Uuid4Changes.au64[0] = 0;
|
---|
135 | Uuid4Changes.au64[1] = 0;
|
---|
136 |
|
---|
137 | RTUUID Uuid4Prev;
|
---|
138 | RTUuidCreate(&Uuid4Prev);
|
---|
139 |
|
---|
140 | for (unsigned i = 0; i < 1024; i++)
|
---|
141 | {
|
---|
142 | RTUUID Uuid4;
|
---|
143 | RTUuidCreate(&Uuid4);
|
---|
144 |
|
---|
145 | Uuid4Changes.au64[0] |= Uuid4.au64[0] ^ Uuid4Prev.au64[0];
|
---|
146 | Uuid4Changes.au64[1] |= Uuid4.au64[1] ^ Uuid4Prev.au64[1];
|
---|
147 |
|
---|
148 | #if 0 /** @todo make a bit string/dumper similar to %Rhxs/d. */
|
---|
149 | 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",
|
---|
150 | !!(Uuid4.Gen.u16ClockSeq & RT_BIT(0)),
|
---|
151 | !!(Uuid4.Gen.u16ClockSeq & RT_BIT(1)),
|
---|
152 | !!(Uuid4.Gen.u16ClockSeq & RT_BIT(2)),
|
---|
153 | !!(Uuid4.Gen.u16ClockSeq & RT_BIT(3)),
|
---|
154 | !!(Uuid4.Gen.u16ClockSeq & RT_BIT(4)),
|
---|
155 | !!(Uuid4.Gen.u16ClockSeq & RT_BIT(5)),
|
---|
156 | !!(Uuid4.Gen.u16ClockSeq & RT_BIT(6)),
|
---|
157 | !!(Uuid4.Gen.u16ClockSeq & RT_BIT(7)),
|
---|
158 | !!(Uuid4.Gen.u16ClockSeq & RT_BIT(8)),
|
---|
159 | !!(Uuid4.Gen.u16ClockSeq & RT_BIT(9)),
|
---|
160 | !!(Uuid4.Gen.u16ClockSeq & RT_BIT(10)),
|
---|
161 | !!(Uuid4.Gen.u16ClockSeq & RT_BIT(11)),
|
---|
162 | !!(Uuid4.Gen.u16ClockSeq & RT_BIT(12)),
|
---|
163 | !!(Uuid4.Gen.u16ClockSeq & RT_BIT(13)),
|
---|
164 | !!(Uuid4.Gen.u16ClockSeq & RT_BIT(14)),
|
---|
165 | !!(Uuid4.Gen.u16ClockSeq & RT_BIT(15)),
|
---|
166 |
|
---|
167 | !!(Uuid4.Gen.u16TimeHiAndVersion & RT_BIT(0)),
|
---|
168 | !!(Uuid4.Gen.u16TimeHiAndVersion & RT_BIT(1)),
|
---|
169 | !!(Uuid4.Gen.u16TimeHiAndVersion & RT_BIT(2)),
|
---|
170 | !!(Uuid4.Gen.u16TimeHiAndVersion & RT_BIT(3)),
|
---|
171 | !!(Uuid4.Gen.u16TimeHiAndVersion & RT_BIT(4)),
|
---|
172 | !!(Uuid4.Gen.u16TimeHiAndVersion & RT_BIT(5)),
|
---|
173 | !!(Uuid4.Gen.u16TimeHiAndVersion & RT_BIT(6)),
|
---|
174 | !!(Uuid4.Gen.u16TimeHiAndVersion & RT_BIT(7)),
|
---|
175 | !!(Uuid4.Gen.u16TimeHiAndVersion & RT_BIT(8)),
|
---|
176 | !!(Uuid4.Gen.u16TimeHiAndVersion & RT_BIT(9)),
|
---|
177 | !!(Uuid4.Gen.u16TimeHiAndVersion & RT_BIT(10)),
|
---|
178 | !!(Uuid4.Gen.u16TimeHiAndVersion & RT_BIT(11)),
|
---|
179 | !!(Uuid4.Gen.u16TimeHiAndVersion & RT_BIT(12)),
|
---|
180 | !!(Uuid4.Gen.u16TimeHiAndVersion & RT_BIT(13)),
|
---|
181 | !!(Uuid4.Gen.u16TimeHiAndVersion & RT_BIT(14)),
|
---|
182 | !!(Uuid4.Gen.u16TimeHiAndVersion & RT_BIT(15))
|
---|
183 | );
|
---|
184 | #endif
|
---|
185 | Uuid4Prev = Uuid4;
|
---|
186 | }
|
---|
187 |
|
---|
188 | RTUUID Uuid4Fixed;
|
---|
189 | Uuid4Fixed.au64[0] = ~Uuid4Changes.au64[0];
|
---|
190 | Uuid4Fixed.au64[1] = ~Uuid4Changes.au64[1];
|
---|
191 | RTTestPrintf(hTest, RTTESTLVL_INFO, "fixed bits: %RTuuid (mask)\n", &Uuid4Fixed);
|
---|
192 | RTTestPrintf(hTest, RTTESTLVL_INFO, "tstUuid: raw: %.*Rhxs\n", sizeof(Uuid4Fixed), &Uuid4Fixed);
|
---|
193 |
|
---|
194 | Uuid4Prev.au64[0] &= Uuid4Fixed.au64[0];
|
---|
195 | Uuid4Prev.au64[1] &= Uuid4Fixed.au64[1];
|
---|
196 | RTTestPrintf(hTest, RTTESTLVL_INFO, "tstUuid: fixed bits: %RTuuid (value)\n", &Uuid4Prev);
|
---|
197 | RTTestPrintf(hTest, RTTESTLVL_INFO, "tstUuid: raw: %.*Rhxs\n", sizeof(Uuid4Prev), &Uuid4Prev);
|
---|
198 |
|
---|
199 | /*
|
---|
200 | * Summary.
|
---|
201 | */
|
---|
202 | return RTTestSummaryAndDestroy(hTest);
|
---|
203 | }
|
---|
204 |
|
---|