1 | /* $Id: tstUuid.cpp 8245 2008-04-21 17:24:28Z 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: %Vrc\n", rc);
|
---|
51 | return 1;
|
---|
52 | }
|
---|
53 |
|
---|
54 | #define CHECK_RC() \
|
---|
55 | do { if (RT_FAILURE(rc)) { RTPrintf("tstUuid(%d): rc=%Vrc!\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.u16ClockSeq = (Uuid3.Gen.u16ClockSeq & 0x3fff) | 0x8000;
|
---|
100 | Uuid3.Gen.u16TimeHiAndVersion = (Uuid3.Gen.u16TimeHiAndVersion & 0x0fff) | 0x4000;
|
---|
101 | const char *pszUuid3 = "67452301-ab89-4fcd-10b2-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 | * Summary.
|
---|
110 | */
|
---|
111 | if (!cErrors)
|
---|
112 | RTPrintf("tstUuid: SUCCESS {%s}\n", sz);
|
---|
113 | else
|
---|
114 | RTPrintf("tstUuid: FAILED - %d errors\n", cErrors);
|
---|
115 | return !!cErrors;
|
---|
116 | }
|
---|
117 |
|
---|