1 | /* $Id: tstUuid.cpp 13837 2008-11-05 02:54:02Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT Testcase - UUID.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 Sun Microsystems, Inc.
|
---|
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 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
27 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
28 | * additional information or have any questions.
|
---|
29 | */
|
---|
30 |
|
---|
31 |
|
---|
32 | /*******************************************************************************
|
---|
33 | * Header Files *
|
---|
34 | *******************************************************************************/
|
---|
35 | #include <iprt/uuid.h>
|
---|
36 | #include <iprt/stream.h>
|
---|
37 | #include <iprt/err.h>
|
---|
38 | #include <iprt/string.h>
|
---|
39 | #include <iprt/initterm.h>
|
---|
40 |
|
---|
41 |
|
---|
42 | int main(int argc, char **argv)
|
---|
43 | {
|
---|
44 | int rc;
|
---|
45 | int cErrors = 0;
|
---|
46 |
|
---|
47 | rc = RTR3Init();
|
---|
48 | if (rc)
|
---|
49 | {
|
---|
50 | RTPrintf("RTR3Init failed: %Rrc\n", rc);
|
---|
51 | return 1;
|
---|
52 | }
|
---|
53 |
|
---|
54 | #define CHECK_RC() \
|
---|
55 | do { if (RT_FAILURE(rc)) { RTPrintf("tstUuid(%d): rc=%Rrc!\n", __LINE__, rc); cErrors++; } } while (0)
|
---|
56 | #define CHECK_EXPR(expr) \
|
---|
57 | do { const bool f = !!(expr); if (!f) { RTPrintf("tstUuid(%d): %s!\n", __LINE__, #expr); cErrors++; } } while (0)
|
---|
58 |
|
---|
59 | RTUUID UuidNull;
|
---|
60 | rc = RTUuidClear(&UuidNull); CHECK_RC();
|
---|
61 | CHECK_EXPR(RTUuidIsNull(&UuidNull));
|
---|
62 | CHECK_EXPR(RTUuidCompare(&UuidNull, &UuidNull) == 0);
|
---|
63 |
|
---|
64 | RTUUID Uuid;
|
---|
65 | rc = RTUuidCreate(&Uuid); CHECK_RC();
|
---|
66 | CHECK_EXPR(!RTUuidIsNull(&Uuid));
|
---|
67 | CHECK_EXPR(RTUuidCompare(&Uuid, &Uuid) == 0);
|
---|
68 | CHECK_EXPR(RTUuidCompare(&Uuid, &UuidNull) > 0);
|
---|
69 | CHECK_EXPR(RTUuidCompare(&UuidNull, &Uuid) < 0);
|
---|
70 |
|
---|
71 | char sz[RTUUID_STR_LENGTH];
|
---|
72 | rc = RTUuidToStr(&Uuid, sz, sizeof(sz)); CHECK_RC();
|
---|
73 | RTUUID Uuid2;
|
---|
74 | rc = RTUuidFromStr(&Uuid2, sz); CHECK_RC();
|
---|
75 | CHECK_EXPR(RTUuidCompare(&Uuid, &Uuid2) == 0);
|
---|
76 |
|
---|
77 | RTPrintf("tstUuid: Created {%s}\n", sz);
|
---|
78 |
|
---|
79 | /*
|
---|
80 | * Check the binary representation.
|
---|
81 | */
|
---|
82 | RTUUID Uuid3;
|
---|
83 | Uuid3.au8[0] = 0x01;
|
---|
84 | Uuid3.au8[1] = 0x23;
|
---|
85 | Uuid3.au8[2] = 0x45;
|
---|
86 | Uuid3.au8[3] = 0x67;
|
---|
87 | Uuid3.au8[4] = 0x89;
|
---|
88 | Uuid3.au8[5] = 0xab;
|
---|
89 | Uuid3.au8[6] = 0xcd;
|
---|
90 | Uuid3.au8[7] = 0x4f;
|
---|
91 | Uuid3.au8[8] = 0x10;
|
---|
92 | Uuid3.au8[9] = 0xb2;
|
---|
93 | Uuid3.au8[10] = 0x54;
|
---|
94 | Uuid3.au8[11] = 0x76;
|
---|
95 | Uuid3.au8[12] = 0x98;
|
---|
96 | Uuid3.au8[13] = 0xba;
|
---|
97 | Uuid3.au8[14] = 0xdc;
|
---|
98 | Uuid3.au8[15] = 0xfe;
|
---|
99 | Uuid3.Gen.u8ClockSeqHiAndReserved = (Uuid3.Gen.u8ClockSeqHiAndReserved & 0x3f) | 0x80;
|
---|
100 | Uuid3.Gen.u16TimeHiAndVersion = (Uuid3.Gen.u16TimeHiAndVersion & 0x0fff) | 0x4000;
|
---|
101 | const char *pszUuid3 = "67452301-ab89-4fcd-90b2-547698badcfe";
|
---|
102 | rc = RTUuidToStr(&Uuid3, sz, sizeof(sz)); CHECK_RC();
|
---|
103 | CHECK_EXPR(strcmp(sz, pszUuid3) == 0);
|
---|
104 | rc = RTUuidFromStr(&Uuid, pszUuid3); CHECK_RC();
|
---|
105 | CHECK_EXPR(RTUuidCompare(&Uuid, &Uuid3) == 0);
|
---|
106 | CHECK_EXPR(memcmp(&Uuid3, &Uuid, sizeof(Uuid)) == 0);
|
---|
107 |
|
---|
108 | /*
|
---|
109 | * checking the clock seq and time hi and version bits...
|
---|
110 | */
|
---|
111 | RTUUID Uuid4Changes;
|
---|
112 | Uuid4Changes.au64[0] = 0;
|
---|
113 | Uuid4Changes.au64[1] = 0;
|
---|
114 |
|
---|
115 | RTUUID Uuid4Prev;
|
---|
116 | RTUuidCreate(&Uuid4Prev);
|
---|
117 |
|
---|
118 | for (unsigned i = 0; i < 1024; i++)
|
---|
119 | {
|
---|
120 | RTUUID Uuid4;
|
---|
121 | RTUuidCreate(&Uuid4);
|
---|
122 |
|
---|
123 | Uuid4Changes.au64[0] |= Uuid4.au64[0] ^ Uuid4Prev.au64[0];
|
---|
124 | Uuid4Changes.au64[1] |= Uuid4.au64[1] ^ Uuid4Prev.au64[1];
|
---|
125 |
|
---|
126 | #if 0 /** @todo make a bit string/dumper similar to %Rhxs/d. */
|
---|
127 | 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",
|
---|
128 | !!(Uuid4.Gen.u16ClockSeq & RT_BIT(0)),
|
---|
129 | !!(Uuid4.Gen.u16ClockSeq & RT_BIT(1)),
|
---|
130 | !!(Uuid4.Gen.u16ClockSeq & RT_BIT(2)),
|
---|
131 | !!(Uuid4.Gen.u16ClockSeq & RT_BIT(3)),
|
---|
132 | !!(Uuid4.Gen.u16ClockSeq & RT_BIT(4)),
|
---|
133 | !!(Uuid4.Gen.u16ClockSeq & RT_BIT(5)),
|
---|
134 | !!(Uuid4.Gen.u16ClockSeq & RT_BIT(6)),
|
---|
135 | !!(Uuid4.Gen.u16ClockSeq & RT_BIT(7)),
|
---|
136 | !!(Uuid4.Gen.u16ClockSeq & RT_BIT(8)),
|
---|
137 | !!(Uuid4.Gen.u16ClockSeq & RT_BIT(9)),
|
---|
138 | !!(Uuid4.Gen.u16ClockSeq & RT_BIT(10)),
|
---|
139 | !!(Uuid4.Gen.u16ClockSeq & RT_BIT(11)),
|
---|
140 | !!(Uuid4.Gen.u16ClockSeq & RT_BIT(12)),
|
---|
141 | !!(Uuid4.Gen.u16ClockSeq & RT_BIT(13)),
|
---|
142 | !!(Uuid4.Gen.u16ClockSeq & RT_BIT(14)),
|
---|
143 | !!(Uuid4.Gen.u16ClockSeq & RT_BIT(15)),
|
---|
144 |
|
---|
145 | !!(Uuid4.Gen.u16TimeHiAndVersion & RT_BIT(0)),
|
---|
146 | !!(Uuid4.Gen.u16TimeHiAndVersion & RT_BIT(1)),
|
---|
147 | !!(Uuid4.Gen.u16TimeHiAndVersion & RT_BIT(2)),
|
---|
148 | !!(Uuid4.Gen.u16TimeHiAndVersion & RT_BIT(3)),
|
---|
149 | !!(Uuid4.Gen.u16TimeHiAndVersion & RT_BIT(4)),
|
---|
150 | !!(Uuid4.Gen.u16TimeHiAndVersion & RT_BIT(5)),
|
---|
151 | !!(Uuid4.Gen.u16TimeHiAndVersion & RT_BIT(6)),
|
---|
152 | !!(Uuid4.Gen.u16TimeHiAndVersion & RT_BIT(7)),
|
---|
153 | !!(Uuid4.Gen.u16TimeHiAndVersion & RT_BIT(8)),
|
---|
154 | !!(Uuid4.Gen.u16TimeHiAndVersion & RT_BIT(9)),
|
---|
155 | !!(Uuid4.Gen.u16TimeHiAndVersion & RT_BIT(10)),
|
---|
156 | !!(Uuid4.Gen.u16TimeHiAndVersion & RT_BIT(11)),
|
---|
157 | !!(Uuid4.Gen.u16TimeHiAndVersion & RT_BIT(12)),
|
---|
158 | !!(Uuid4.Gen.u16TimeHiAndVersion & RT_BIT(13)),
|
---|
159 | !!(Uuid4.Gen.u16TimeHiAndVersion & RT_BIT(14)),
|
---|
160 | !!(Uuid4.Gen.u16TimeHiAndVersion & RT_BIT(15))
|
---|
161 | );
|
---|
162 | #endif
|
---|
163 | Uuid4Prev = Uuid4;
|
---|
164 | }
|
---|
165 |
|
---|
166 | RTUUID Uuid4Fixed;
|
---|
167 | Uuid4Fixed.au64[0] = ~Uuid4Changes.au64[0];
|
---|
168 | Uuid4Fixed.au64[1] = ~Uuid4Changes.au64[1];
|
---|
169 | RTPrintf("tstUuid: fixed bits: %RTuuid (mask)\n", &Uuid4Fixed);
|
---|
170 | RTPrintf("tstUuid: raw: %.*Rhxs\n", sizeof(Uuid4Fixed), &Uuid4Fixed);
|
---|
171 |
|
---|
172 | Uuid4Prev.au64[0] &= Uuid4Fixed.au64[0];
|
---|
173 | Uuid4Prev.au64[1] &= Uuid4Fixed.au64[1];
|
---|
174 | RTPrintf("tstUuid: fixed bits: %RTuuid (value)\n", &Uuid4Prev);
|
---|
175 | RTPrintf("tstUuid: raw: %.*Rhxs\n", sizeof(Uuid4Prev), &Uuid4Prev);
|
---|
176 |
|
---|
177 | /*
|
---|
178 | * Summary.
|
---|
179 | */
|
---|
180 | if (!cErrors)
|
---|
181 | RTPrintf("tstUuid: SUCCESS {%s}\n", sz);
|
---|
182 | else
|
---|
183 | RTPrintf("tstUuid: FAILED - %d errors\n", cErrors);
|
---|
184 | return !!cErrors;
|
---|
185 | }
|
---|
186 |
|
---|