1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  *   Licensed to the Apache Software Foundation (ASF) under one or more
12  *   contributor license agreements. See the NOTICE file distributed
13  *   with this work for additional information regarding copyright
14  *   ownership. The ASF licenses this file to you under the Apache
15  *   License, Version 2.0 (the "License"); you may not use this file
16  *   except in compliance with the License. You may obtain a copy of
17  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #include <math.h>
21 #include <stdio.h>
22 #include <string.h>
23 
24 #include <rtl/uuid.h>
25 #include <rtl/ustring.h>
26 #include <rtl/ustring.hxx>
27 #include <cppunit/TestFixture.h>
28 #include <cppunit/extensions/HelperMacros.h>
29 #include <cppunit/plugin/TestPlugIn.h>
30 
31 #ifdef _WIN32
32 #if !defined WIN32_LEAN_AND_MEAN
33 # define WIN32_LEAN_AND_MEAN
34 #endif
35 #include <windows.h>
36 #elif defined UNX
37 #include <unistd.h>
38 #include <time.h>
39 #endif
40 
41 
42 namespace rtl_Uuid
43 {
44 class createUuid : public CppUnit::TestFixture
45 {
46 public:
47 #define TEST_UUID 20
createUuid_001()48     void createUuid_001()
49     {
50     sal_uInt8 aNode[TEST_UUID][16];
51     sal_Int32 i,i2;
52     for( i = 0 ; i < TEST_UUID ; i ++ )
53     {
54         rtl_createUuid( aNode[i], nullptr, false );
55     }
56     bool bRes = true;
57     for( i = 0 ; i < TEST_UUID ; i ++ )
58     {
59         for( i2 = i+1 ; i2 < TEST_UUID ; i2 ++ )
60         {
61             if ( rtl_compareUuid( aNode[i] , aNode[i2] ) == 0  )
62             {
63                 bRes = false;
64                 break;
65             }
66         }
67         if ( !bRes )
68             break;
69     }
70     CPPUNIT_ASSERT_MESSAGE("createUuid: every uuid must be different.", bRes);
71     }
72    /*
73     void createUuid_002()
74     {
75     sal_uInt8 pNode[16];
76     sal_uInt8 aNode[TEST_UUID][16];
77     sal_Int32 i,i2;
78     for( i = 0 ; i < TEST_UUID ; i ++ )
79     {
80         rtl_createUuid( aNode[i], pNode, sal_True );
81     }
82     sal_Bool bRes = sal_True;
83     for( i = 0 ; i < TEST_UUID ; i ++ )
84     {
85         //printUuid( aNode[i] );
86         for( i2 = i+1 ; i2 < TEST_UUID ; i2 ++ )
87         {
88             if ( rtl_compareUuid( aNode[i] , aNode[i2] ) == 0  )
89             {
90                 bRes = sal_False;
91                 break;
92             }
93         }
94         if ( bRes == sal_False )
95             break;
96     }
97     CPPUNIT_ASSERT_MESSAGE("createUuid: every uuid must be different.", bRes == sal_True );
98     }*/
99 
100     CPPUNIT_TEST_SUITE(createUuid);
101     CPPUNIT_TEST(createUuid_001);
102     //CPPUNIT_TEST(createUuid_002);
103     CPPUNIT_TEST_SUITE_END();
104 }; // class createUuid
105 
106 class createNamedUuid : public CppUnit::TestFixture
107 {
108 public:
createNamedUuid_001()109     void createNamedUuid_001()
110     {
111         sal_uInt8 NameSpace_DNS[16] = RTL_UUID_NAMESPACE_DNS;
112         sal_uInt8 NameSpace_URL[16] = RTL_UUID_NAMESPACE_URL;
113         sal_uInt8 pPriorCalculatedUUID[16] = {
114             0x52,0xc9,0x30,0xa5,
115             0xd1,0x61,0x3b,0x16,
116             0x98,0xc5,0xf8,0xd1,
117             0x10,0x10,0x6d,0x4d };
118 
119         sal_uInt8 pNamedUUID[16], pNamedUUID2[16];
120 
121         // Same name does generate the same uuid
122         rtl_String *pName = nullptr;
123         rtl_string_newFromStr( &pName , "this is a bla.blubs.DNS-Name" );
124         rtl_createNamedUuid( pNamedUUID , NameSpace_DNS , pName );
125         rtl_createNamedUuid( pNamedUUID2 , NameSpace_DNS , pName );
126         CPPUNIT_ASSERT_MESSAGE( "Same name should generate the same uuid", ! memcmp( pNamedUUID , pNamedUUID2 , 16 ) && rtl_compareUuid( pNamedUUID , pNamedUUID2 ) == 0 );
127         CPPUNIT_ASSERT_MESSAGE( "Same name should generate the same uuid", ! memcmp( pNamedUUID  , pPriorCalculatedUUID , 16 ) );
128 
129         // Different names does not generate the same uuid
130         rtl_string_newFromStr( &pName , "this is a bla.blubs.DNS-Namf" );
131         rtl_createNamedUuid( pNamedUUID2 , NameSpace_DNS , pName );
132         CPPUNIT_ASSERT_MESSAGE("Different names does not generate the same uuid.", memcmp( pNamedUUID , pNamedUUID2 , 16 ) );
133 
134         // the same name with different namespace uuid produces different uuids
135         rtl_createNamedUuid( pNamedUUID , NameSpace_URL , pName );
136         CPPUNIT_ASSERT_MESSAGE( " same name with different namespace uuid produces different uuids", memcmp( pNamedUUID , pNamedUUID2 , 16 ) && rtl_compareUuid( pNamedUUID , pNamedUUID2 ) != 0);
137 
138         //test compareUuid
139         if ( rtl_compareUuid( pNamedUUID , pNamedUUID2 ) > 0 )
140         {   CPPUNIT_ASSERT_MESSAGE( " compare uuids", rtl_compareUuid( pNamedUUID2 , pNamedUUID ) < 0);
141         }
142         else
143             CPPUNIT_ASSERT_MESSAGE( " compare uuids", rtl_compareUuid( pNamedUUID2 , pNamedUUID ) > 0);
144 
145         rtl_string_release( pName );
146     }
147 
148     CPPUNIT_TEST_SUITE(createNamedUuid);
149     CPPUNIT_TEST(createNamedUuid_001);
150     CPPUNIT_TEST_SUITE_END();
151 }; // class createNamedUuid
152 
153 CPPUNIT_TEST_SUITE_REGISTRATION(rtl_Uuid::createUuid);
154 CPPUNIT_TEST_SUITE_REGISTRATION(rtl_Uuid::createNamedUuid);
155 } // namespace rtl_Uuid
156 
157 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
158