From 7f2554ff251f34a338c1dc40bc0e8362b00c3243 Mon Sep 17 00:00:00 2001
From: Juan Lang <juan.lang@gmail.com>
Date: Fri, 10 Oct 2008 10:03:52 -0700
Subject: [PATCH] Add tests for UuidCreate

---
 dlls/rpcrt4/tests/rpc.c |   56 +++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 56 insertions(+), 0 deletions(-)

diff --git a/dlls/rpcrt4/tests/rpc.c b/dlls/rpcrt4/tests/rpc.c
index 763d387..985e351 100644
--- a/dlls/rpcrt4/tests/rpc.c
+++ b/dlls/rpcrt4/tests/rpc.c
@@ -761,6 +761,61 @@ static void test_RpcStringBindingFromBinding(void)
     ok(status == RPC_S_OK, "RpcBindingFree failed with error %lu\n", status);
 }
 
+static char *printGuid(char *buf, const UUID *guid)
+{
+    sprintf(buf, "{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}\n",
+     guid->Data1, guid->Data2, guid->Data3, guid->Data4[0], guid->Data4[1],
+     guid->Data4[2], guid->Data4[3], guid->Data4[4], guid->Data4[5],
+     guid->Data4[6], guid->Data4[7]);
+    return buf;
+}
+
+static void test_UuidCreate(void)
+{
+    UUID guid;
+    BYTE version;
+
+    UuidCreate(&guid);
+    version = (guid.Data3 & 0xf000) >> 12;
+    if (version == 4)
+    {
+        UUID v4 = { 0, 0, 0x4000, { 0x80,0,0,0,0,0,0,0 } }, temp;
+        RPC_STATUS rslt;
+        int i;
+        char buf[39];
+
+        /* Generate a bunch of UUIDs and and them.  By the end, we expect every
+         * randomly generated bit to have been zero at least once, resulting in
+         * no bits set except those which are not randomly generated:  the
+         * version number and the topmost bits of the Data4 field (treated as
+         * big-endian.)
+         */
+        for (i = 0; i < 1000; i++)
+        {
+            LPBYTE src, dst;
+
+            UuidCreate(&temp);
+            for (src = (LPBYTE)&temp, dst = (LPBYTE)&guid;
+             src - (LPBYTE)&temp < sizeof(temp); src++, dst++)
+                *dst &= *src;
+        }
+        ok(UuidEqual(&guid, &v4, &rslt), "unexpected bits set in V4 UUID: %s\n",
+           printGuid(buf, &guid));
+    }
+    else
+    {
+        /* Older versions of Windows generate V1 UUIDs.  For these, there are
+         * many stable bits, including at least the MAC address if one is
+         * present.  Just check that Data4[0]'s most significant bits are
+         * set as expected.
+         */
+        ok(version == 1, "unexpected version %d\n", version);
+        todo_wine
+        ok((guid.Data4[0] & 0xc0) == 0x80,
+           "unexpected value in Data4[0]: %02x\n", guid.Data4[0] & 0xc0);
+    }
+}
+
 START_TEST( rpc )
 {
     static unsigned char ncacn_np[] = "ncacn_np";
@@ -779,4 +834,5 @@ START_TEST( rpc )
     test_endpoint_mapper(ncacn_np, np_address, np_endpoint);
     test_endpoint_mapper(ncalrpc, NULL, lrpc_endpoint);
     test_RpcStringBindingFromBinding();
+    test_UuidCreate();
 }
-- 
1.5.5.1

