tstUuid.cpp revision 1c94c0a63ba68be1a7b2c640e70d7a06464e4fca
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync/* $Id$ */
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync/** @file
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync * innotek Portable Runtime Testcase - UUID.
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync */
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync/*
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync * Copyright (C) 2006-2007 Sun Microsystems, Inc.
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync *
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync * available from http://www.virtualbox.org. This file is free software;
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync * you can redistribute it and/or modify it under the terms of the GNU
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync * General Public License (GPL) as published by the Free Software
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync *
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync * The contents of this file may alternatively be used under the terms
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync * of the Common Development and Distribution License Version 1.0
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync * VirtualBox OSE distribution, in which case the provisions of the
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync * CDDL are applicable instead of those of the GPL.
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync *
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync * You may elect to license modified versions of this file under the
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync * terms and conditions of either the GPL or the CDDL or both.
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync *
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync * Clara, CA 95054 USA or visit http://www.sun.com if you need
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync * additional information or have any questions.
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync */
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync/*******************************************************************************
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync* Header Files *
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync*******************************************************************************/
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync#include <iprt/uuid.h>
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync#include <iprt/stream.h>
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync#include <iprt/err.h>
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync#include <iprt/string.h>
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync#include <iprt/initterm.h>
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsyncint main(int argc, char **argv)
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync{
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync int rc;
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync int cErrors = 0;
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync rc = RTR3Init();
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync if (rc)
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync {
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync RTPrintf("RTR3Init failed: %Vrc\n", rc);
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync return 1;
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync }
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync#define CHECK_RC() \
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync do { if (RT_FAILURE(rc)) { RTPrintf("tstUuid(%d): rc=%Vrc!\n", __LINE__, rc); cErrors++; } } while (0)
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync#define CHECK_EXPR(expr) \
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync do { const bool f = !!(expr); if (!f) { RTPrintf("tstUuid(%d): %s!\n", __LINE__, #expr); cErrors++; } } while (0)
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync RTUUID UuidNull;
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync rc = RTUuidClear(&UuidNull); CHECK_RC();
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync CHECK_EXPR(RTUuidIsNull(&UuidNull));
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync CHECK_EXPR(RTUuidCompare(&UuidNull, &UuidNull) == 0);
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync RTUUID Uuid;
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync rc = RTUuidCreate(&Uuid); CHECK_RC();
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync CHECK_EXPR(!RTUuidIsNull(&Uuid));
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync CHECK_EXPR(RTUuidCompare(&Uuid, &Uuid) == 0);
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync CHECK_EXPR(RTUuidCompare(&Uuid, &UuidNull) > 0);
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync CHECK_EXPR(RTUuidCompare(&UuidNull, &Uuid) < 0);
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync char sz[RTUUID_STR_LENGTH];
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync rc = RTUuidToStr(&Uuid, sz, sizeof(sz)); CHECK_RC();
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync RTUUID Uuid2;
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync rc = RTUuidFromStr(&Uuid2, sz); CHECK_RC();
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync CHECK_EXPR(RTUuidCompare(&Uuid, &Uuid2) == 0);
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync RTPrintf("tstUuid: Created {%s}\n", sz);
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync /*
* Check the binary representation.
*/
RTUUID Uuid3;
Uuid3.au8[0] = 0x01;
Uuid3.au8[1] = 0x23;
Uuid3.au8[2] = 0x45;
Uuid3.au8[3] = 0x67;
Uuid3.au8[4] = 0x89;
Uuid3.au8[5] = 0xab;
Uuid3.au8[6] = 0xcd;
Uuid3.au8[7] = 0x4f;
Uuid3.au8[8] = 0x10;
Uuid3.au8[9] = 0xb2;
Uuid3.au8[10] = 0x54;
Uuid3.au8[11] = 0x76;
Uuid3.au8[12] = 0x98;
Uuid3.au8[13] = 0xba;
Uuid3.au8[14] = 0xdc;
Uuid3.au8[15] = 0xfe;
Uuid3.Gen.u16ClockSeq = (Uuid3.Gen.u16ClockSeq & 0x3fff) | 0x8000;
Uuid3.Gen.u16TimeHiAndVersion = (Uuid3.Gen.u16TimeHiAndVersion & 0x0fff) | 0x4000;
const char *pszUuid3 = "67452301-ab89-4fcd-10b2-547698badcfe";
rc = RTUuidToStr(&Uuid3, sz, sizeof(sz)); CHECK_RC();
CHECK_EXPR(strcmp(sz, pszUuid3) == 0);
rc = RTUuidFromStr(&Uuid, pszUuid3); CHECK_RC();
CHECK_EXPR(RTUuidCompare(&Uuid, &Uuid3) == 0);
CHECK_EXPR(memcmp(&Uuid3, &Uuid, sizeof(Uuid)) == 0);
/*
* Summary.
*/
if (!cErrors)
RTPrintf("tstUuid: SUCCESS {%s}\n", sz);
else
RTPrintf("tstUuid: FAILED - %d errors\n", cErrors);
return !!cErrors;
}