xref: /aoo41x/main/sal/qa/rtl/uuid/rtl_Uuid.cxx (revision cdf0e10c)
1*cdf0e10cSrcweir  /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_sal.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include <math.h>
32*cdf0e10cSrcweir #include <stdio.h>
33*cdf0e10cSrcweir 
34*cdf0e10cSrcweir #include <testshl/simpleheader.hxx>
35*cdf0e10cSrcweir #include <rtl/uuid.h>
36*cdf0e10cSrcweir #include <rtl/ustring.h>
37*cdf0e10cSrcweir #include <rtl/ustring.hxx>
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir #ifdef UNX
40*cdf0e10cSrcweir #include <unistd.h>
41*cdf0e10cSrcweir #include <time.h>
42*cdf0e10cSrcweir #endif
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir using namespace rtl;
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir /** print a UNI_CODE String. And also print some comments of the string.
47*cdf0e10cSrcweir */
48*cdf0e10cSrcweir inline void printUString( const ::rtl::OUString & str, const sal_Char * msg = "" )
49*cdf0e10cSrcweir {
50*cdf0e10cSrcweir 	t_print("#%s #printUString_u# ", msg );
51*cdf0e10cSrcweir 	rtl::OString aString;
52*cdf0e10cSrcweir 	aString = ::rtl::OUStringToOString( str, RTL_TEXTENCODING_ASCII_US );
53*cdf0e10cSrcweir 	t_print("%s\n", (char *)aString.getStr( ) );
54*cdf0e10cSrcweir }
55*cdf0e10cSrcweir 
56*cdf0e10cSrcweir /************************************************************************
57*cdf0e10cSrcweir  * For diagnostics( from sal/test/testuuid.cxx )
58*cdf0e10cSrcweir  ************************************************************************/
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir void printUuid( sal_uInt8 *pNode )
61*cdf0e10cSrcweir {
62*cdf0e10cSrcweir 	for( sal_Int32 i1 = 0 ; i1 < 4 ; i1++ )
63*cdf0e10cSrcweir 	{
64*cdf0e10cSrcweir 		for( sal_Int32 i2 = 0 ; i2 < 4 ; i2++ )
65*cdf0e10cSrcweir 		{
66*cdf0e10cSrcweir 			printf( "%02x" , pNode[i1*4 +i2] );
67*cdf0e10cSrcweir 		}
68*cdf0e10cSrcweir 		if( i1 == 3 )
69*cdf0e10cSrcweir 			break;
70*cdf0e10cSrcweir 		printf( "-" );
71*cdf0e10cSrcweir 	}
72*cdf0e10cSrcweir 
73*cdf0e10cSrcweir 	printf( "\n# " );
74*cdf0e10cSrcweir }
75*cdf0e10cSrcweir 
76*cdf0e10cSrcweir namespace rtl_Uuid
77*cdf0e10cSrcweir {
78*cdf0e10cSrcweir class createUuid : public CppUnit::TestFixture
79*cdf0e10cSrcweir {
80*cdf0e10cSrcweir public:
81*cdf0e10cSrcweir     // initialise your test code values here.
82*cdf0e10cSrcweir     void setUp()
83*cdf0e10cSrcweir     {
84*cdf0e10cSrcweir     }
85*cdf0e10cSrcweir 
86*cdf0e10cSrcweir     void tearDown()
87*cdf0e10cSrcweir     {
88*cdf0e10cSrcweir     }
89*cdf0e10cSrcweir 
90*cdf0e10cSrcweir #define TEST_UUID 20
91*cdf0e10cSrcweir     void createUuid_001()
92*cdf0e10cSrcweir     {
93*cdf0e10cSrcweir 	sal_uInt8 aNode[TEST_UUID][16];
94*cdf0e10cSrcweir 	sal_Int32 i,i2;
95*cdf0e10cSrcweir 	for( i = 0 ; i < TEST_UUID ; i ++ )
96*cdf0e10cSrcweir 	{
97*cdf0e10cSrcweir 		rtl_createUuid( aNode[i], 0, sal_False );
98*cdf0e10cSrcweir 	}
99*cdf0e10cSrcweir 	sal_Bool bRes = sal_True;
100*cdf0e10cSrcweir 	for( i = 0 ; i < TEST_UUID ; i ++ )
101*cdf0e10cSrcweir 	{
102*cdf0e10cSrcweir 		for( i2 = i+1 ; i2 < TEST_UUID ; i2 ++ )
103*cdf0e10cSrcweir 		{
104*cdf0e10cSrcweir 			if ( rtl_compareUuid( aNode[i] , aNode[i2] ) == 0  )
105*cdf0e10cSrcweir 			{
106*cdf0e10cSrcweir 				bRes = sal_False;
107*cdf0e10cSrcweir 				break;
108*cdf0e10cSrcweir 			}
109*cdf0e10cSrcweir 		}
110*cdf0e10cSrcweir 		if ( bRes == sal_False )
111*cdf0e10cSrcweir 			break;
112*cdf0e10cSrcweir 	}
113*cdf0e10cSrcweir 	CPPUNIT_ASSERT_MESSAGE("createUuid: every uuid must be different.", bRes == sal_True );
114*cdf0e10cSrcweir     }
115*cdf0e10cSrcweir    /*
116*cdf0e10cSrcweir     void createUuid_002()
117*cdf0e10cSrcweir     {
118*cdf0e10cSrcweir 	sal_uInt8 pNode[16];
119*cdf0e10cSrcweir 	sal_uInt8 aNode[TEST_UUID][16];
120*cdf0e10cSrcweir 	sal_Int32 i,i2;
121*cdf0e10cSrcweir 	for( i = 0 ; i < TEST_UUID ; i ++ )
122*cdf0e10cSrcweir 	{
123*cdf0e10cSrcweir 		rtl_createUuid( aNode[i], pNode, sal_True );
124*cdf0e10cSrcweir 	}
125*cdf0e10cSrcweir 	sal_Bool bRes = sal_True;
126*cdf0e10cSrcweir 	for( i = 0 ; i < TEST_UUID ; i ++ )
127*cdf0e10cSrcweir 	{
128*cdf0e10cSrcweir 		//printUuid( aNode[i] );
129*cdf0e10cSrcweir 		for( i2 = i+1 ; i2 < TEST_UUID ; i2 ++ )
130*cdf0e10cSrcweir 		{
131*cdf0e10cSrcweir 			if ( rtl_compareUuid( aNode[i] , aNode[i2] ) == 0  )
132*cdf0e10cSrcweir 			{
133*cdf0e10cSrcweir 				bRes = sal_False;
134*cdf0e10cSrcweir 				break;
135*cdf0e10cSrcweir 			}
136*cdf0e10cSrcweir 		}
137*cdf0e10cSrcweir 		if ( bRes == sal_False )
138*cdf0e10cSrcweir 			break;
139*cdf0e10cSrcweir 	}
140*cdf0e10cSrcweir 	CPPUNIT_ASSERT_MESSAGE("createUuid: every uuid must be different.", bRes == sal_True );
141*cdf0e10cSrcweir     }*/
142*cdf0e10cSrcweir 
143*cdf0e10cSrcweir     CPPUNIT_TEST_SUITE(createUuid);
144*cdf0e10cSrcweir     CPPUNIT_TEST(createUuid_001);
145*cdf0e10cSrcweir     //CPPUNIT_TEST(createUuid_002);
146*cdf0e10cSrcweir     CPPUNIT_TEST_SUITE_END();
147*cdf0e10cSrcweir }; // class createUuid
148*cdf0e10cSrcweir 
149*cdf0e10cSrcweir namespace ThreadHelper
150*cdf0e10cSrcweir {
151*cdf0e10cSrcweir      void thread_sleep(sal_Int32 _nSec)
152*cdf0e10cSrcweir     {
153*cdf0e10cSrcweir #ifdef WNT      //Windows
154*cdf0e10cSrcweir         Sleep(_nSec * 10 );
155*cdf0e10cSrcweir #endif
156*cdf0e10cSrcweir #if ( defined UNX ) || ( defined OS2 )  //Unix
157*cdf0e10cSrcweir         sleep( _nSec );
158*cdf0e10cSrcweir #endif
159*cdf0e10cSrcweir     }
160*cdf0e10cSrcweir }
161*cdf0e10cSrcweir 
162*cdf0e10cSrcweir class createNamedUuid : public CppUnit::TestFixture
163*cdf0e10cSrcweir {
164*cdf0e10cSrcweir public:
165*cdf0e10cSrcweir     // initialise your test code values here.
166*cdf0e10cSrcweir     void setUp()
167*cdf0e10cSrcweir     {
168*cdf0e10cSrcweir     }
169*cdf0e10cSrcweir 
170*cdf0e10cSrcweir     void tearDown()
171*cdf0e10cSrcweir     {
172*cdf0e10cSrcweir     }
173*cdf0e10cSrcweir 
174*cdf0e10cSrcweir     void createNamedUuid_001()
175*cdf0e10cSrcweir     {
176*cdf0e10cSrcweir 		sal_uInt8 NameSpace_DNS[16] = RTL_UUID_NAMESPACE_DNS;
177*cdf0e10cSrcweir 		sal_uInt8 NameSpace_URL[16] = RTL_UUID_NAMESPACE_URL;
178*cdf0e10cSrcweir 		sal_uInt8 pPriorCalculatedUUID[16] = {
179*cdf0e10cSrcweir 			0x52,0xc9,0x30,0xa5,
180*cdf0e10cSrcweir 			0xd1,0x61,0x3b,0x16,
181*cdf0e10cSrcweir 			0x98,0xc5,0xf8,0xd1,
182*cdf0e10cSrcweir 			0x10,0x10,0x6d,0x4d };
183*cdf0e10cSrcweir 
184*cdf0e10cSrcweir 		sal_uInt8 pNamedUUID[16], pNamedUUID2[16];
185*cdf0e10cSrcweir 
186*cdf0e10cSrcweir 		// Same name does generate the same uuid
187*cdf0e10cSrcweir 		rtl_String *pName = 0;
188*cdf0e10cSrcweir 		rtl_string_newFromStr( &pName , "this is a bla.blubs.DNS-Name" );
189*cdf0e10cSrcweir 		rtl_createNamedUuid( pNamedUUID , NameSpace_DNS , pName );
190*cdf0e10cSrcweir 		rtl_createNamedUuid( pNamedUUID2 , NameSpace_DNS , pName );
191*cdf0e10cSrcweir 		CPPUNIT_ASSERT_MESSAGE( "Same name should generate the same uuid", ! memcmp( pNamedUUID , pNamedUUID2 , 16 ) && rtl_compareUuid( pNamedUUID , pNamedUUID2 ) == 0 );
192*cdf0e10cSrcweir 		CPPUNIT_ASSERT_MESSAGE( "Same name should generate the same uuid", ! memcmp( pNamedUUID  , pPriorCalculatedUUID , 16 ) );
193*cdf0e10cSrcweir 
194*cdf0e10cSrcweir 		// Different names does not generate the same uuid
195*cdf0e10cSrcweir 		rtl_string_newFromStr( &pName , "this is a bla.blubs.DNS-Namf" );
196*cdf0e10cSrcweir 		rtl_createNamedUuid( pNamedUUID2 , NameSpace_DNS , pName );
197*cdf0e10cSrcweir 		CPPUNIT_ASSERT_MESSAGE("Different names does not generate the same uuid.", memcmp( pNamedUUID , pNamedUUID2 , 16 ) );
198*cdf0e10cSrcweir 
199*cdf0e10cSrcweir 		// the same name with different namespace uuid produces different uuids
200*cdf0e10cSrcweir 		rtl_createNamedUuid( pNamedUUID , NameSpace_URL , pName );
201*cdf0e10cSrcweir 		CPPUNIT_ASSERT_MESSAGE( " same name with different namespace uuid produces different uuids", memcmp( pNamedUUID , pNamedUUID2 , 16 ) && rtl_compareUuid( pNamedUUID , pNamedUUID2 ) != 0);
202*cdf0e10cSrcweir 
203*cdf0e10cSrcweir 		//test compareUuid
204*cdf0e10cSrcweir 		if ( rtl_compareUuid( pNamedUUID , pNamedUUID2 ) > 0 )
205*cdf0e10cSrcweir 		{	CPPUNIT_ASSERT_MESSAGE( " compare uuids", rtl_compareUuid( pNamedUUID2 , pNamedUUID ) < 0);
206*cdf0e10cSrcweir 		}
207*cdf0e10cSrcweir 		else
208*cdf0e10cSrcweir 			CPPUNIT_ASSERT_MESSAGE( " compare uuids", rtl_compareUuid( pNamedUUID2 , pNamedUUID ) > 0);
209*cdf0e10cSrcweir 
210*cdf0e10cSrcweir 		rtl_string_release( pName );
211*cdf0e10cSrcweir     }
212*cdf0e10cSrcweir 
213*cdf0e10cSrcweir     CPPUNIT_TEST_SUITE(createNamedUuid);
214*cdf0e10cSrcweir     CPPUNIT_TEST(createNamedUuid_001);
215*cdf0e10cSrcweir     CPPUNIT_TEST_SUITE_END();
216*cdf0e10cSrcweir }; // class createNamedUuid
217*cdf0e10cSrcweir 
218*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_Uuid::createUuid, "rtl_Uuid");
219*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_Uuid::createNamedUuid, "rtl_Uuid");
220*cdf0e10cSrcweir } // namespace rtl_Uuid
221*cdf0e10cSrcweir 
222*cdf0e10cSrcweir // -----------------------------------------------------------------------------
223*cdf0e10cSrcweir 
224*cdf0e10cSrcweir // this macro creates an empty function, which will called by the RegisterAllFunctions()
225*cdf0e10cSrcweir // to let the user the possibility to also register some functions by hand.
226*cdf0e10cSrcweir NOADDITIONAL;
227*cdf0e10cSrcweir 
228*cdf0e10cSrcweir 
229