xref: /aoo42x/main/sal/qa/rtl/uuid/rtl_Uuid.cxx (revision 87d2adbc)
1*87d2adbcSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*87d2adbcSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*87d2adbcSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*87d2adbcSAndrew Rist  * distributed with this work for additional information
6*87d2adbcSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*87d2adbcSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*87d2adbcSAndrew Rist  * "License"); you may not use this file except in compliance
9*87d2adbcSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*87d2adbcSAndrew Rist  *
11*87d2adbcSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*87d2adbcSAndrew Rist  *
13*87d2adbcSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*87d2adbcSAndrew Rist  * software distributed under the License is distributed on an
15*87d2adbcSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*87d2adbcSAndrew Rist  * KIND, either express or implied.  See the License for the
17*87d2adbcSAndrew Rist  * specific language governing permissions and limitations
18*87d2adbcSAndrew Rist  * under the License.
19*87d2adbcSAndrew Rist  *
20*87d2adbcSAndrew Rist  *************************************************************/
21*87d2adbcSAndrew Rist 
22*87d2adbcSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_sal.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <math.h>
28cdf0e10cSrcweir #include <stdio.h>
29cdf0e10cSrcweir 
30cdf0e10cSrcweir #include <testshl/simpleheader.hxx>
31cdf0e10cSrcweir #include <rtl/uuid.h>
32cdf0e10cSrcweir #include <rtl/ustring.h>
33cdf0e10cSrcweir #include <rtl/ustring.hxx>
34cdf0e10cSrcweir 
35cdf0e10cSrcweir #ifdef UNX
36cdf0e10cSrcweir #include <unistd.h>
37cdf0e10cSrcweir #include <time.h>
38cdf0e10cSrcweir #endif
39cdf0e10cSrcweir 
40cdf0e10cSrcweir using namespace rtl;
41cdf0e10cSrcweir 
42cdf0e10cSrcweir /** print a UNI_CODE String. And also print some comments of the string.
43cdf0e10cSrcweir */
44cdf0e10cSrcweir inline void printUString( const ::rtl::OUString & str, const sal_Char * msg = "" )
45cdf0e10cSrcweir {
46cdf0e10cSrcweir 	t_print("#%s #printUString_u# ", msg );
47cdf0e10cSrcweir 	rtl::OString aString;
48cdf0e10cSrcweir 	aString = ::rtl::OUStringToOString( str, RTL_TEXTENCODING_ASCII_US );
49cdf0e10cSrcweir 	t_print("%s\n", (char *)aString.getStr( ) );
50cdf0e10cSrcweir }
51cdf0e10cSrcweir 
52cdf0e10cSrcweir /************************************************************************
53cdf0e10cSrcweir  * For diagnostics( from sal/test/testuuid.cxx )
54cdf0e10cSrcweir  ************************************************************************/
55cdf0e10cSrcweir 
56cdf0e10cSrcweir void printUuid( sal_uInt8 *pNode )
57cdf0e10cSrcweir {
58cdf0e10cSrcweir 	for( sal_Int32 i1 = 0 ; i1 < 4 ; i1++ )
59cdf0e10cSrcweir 	{
60cdf0e10cSrcweir 		for( sal_Int32 i2 = 0 ; i2 < 4 ; i2++ )
61cdf0e10cSrcweir 		{
62cdf0e10cSrcweir 			printf( "%02x" , pNode[i1*4 +i2] );
63cdf0e10cSrcweir 		}
64cdf0e10cSrcweir 		if( i1 == 3 )
65cdf0e10cSrcweir 			break;
66cdf0e10cSrcweir 		printf( "-" );
67cdf0e10cSrcweir 	}
68cdf0e10cSrcweir 
69cdf0e10cSrcweir 	printf( "\n# " );
70cdf0e10cSrcweir }
71cdf0e10cSrcweir 
72cdf0e10cSrcweir namespace rtl_Uuid
73cdf0e10cSrcweir {
74cdf0e10cSrcweir class createUuid : public CppUnit::TestFixture
75cdf0e10cSrcweir {
76cdf0e10cSrcweir public:
77cdf0e10cSrcweir     // initialise your test code values here.
78cdf0e10cSrcweir     void setUp()
79cdf0e10cSrcweir     {
80cdf0e10cSrcweir     }
81cdf0e10cSrcweir 
82cdf0e10cSrcweir     void tearDown()
83cdf0e10cSrcweir     {
84cdf0e10cSrcweir     }
85cdf0e10cSrcweir 
86cdf0e10cSrcweir #define TEST_UUID 20
87cdf0e10cSrcweir     void createUuid_001()
88cdf0e10cSrcweir     {
89cdf0e10cSrcweir 	sal_uInt8 aNode[TEST_UUID][16];
90cdf0e10cSrcweir 	sal_Int32 i,i2;
91cdf0e10cSrcweir 	for( i = 0 ; i < TEST_UUID ; i ++ )
92cdf0e10cSrcweir 	{
93cdf0e10cSrcweir 		rtl_createUuid( aNode[i], 0, sal_False );
94cdf0e10cSrcweir 	}
95cdf0e10cSrcweir 	sal_Bool bRes = sal_True;
96cdf0e10cSrcweir 	for( i = 0 ; i < TEST_UUID ; i ++ )
97cdf0e10cSrcweir 	{
98cdf0e10cSrcweir 		for( i2 = i+1 ; i2 < TEST_UUID ; i2 ++ )
99cdf0e10cSrcweir 		{
100cdf0e10cSrcweir 			if ( rtl_compareUuid( aNode[i] , aNode[i2] ) == 0  )
101cdf0e10cSrcweir 			{
102cdf0e10cSrcweir 				bRes = sal_False;
103cdf0e10cSrcweir 				break;
104cdf0e10cSrcweir 			}
105cdf0e10cSrcweir 		}
106cdf0e10cSrcweir 		if ( bRes == sal_False )
107cdf0e10cSrcweir 			break;
108cdf0e10cSrcweir 	}
109cdf0e10cSrcweir 	CPPUNIT_ASSERT_MESSAGE("createUuid: every uuid must be different.", bRes == sal_True );
110cdf0e10cSrcweir     }
111cdf0e10cSrcweir    /*
112cdf0e10cSrcweir     void createUuid_002()
113cdf0e10cSrcweir     {
114cdf0e10cSrcweir 	sal_uInt8 pNode[16];
115cdf0e10cSrcweir 	sal_uInt8 aNode[TEST_UUID][16];
116cdf0e10cSrcweir 	sal_Int32 i,i2;
117cdf0e10cSrcweir 	for( i = 0 ; i < TEST_UUID ; i ++ )
118cdf0e10cSrcweir 	{
119cdf0e10cSrcweir 		rtl_createUuid( aNode[i], pNode, sal_True );
120cdf0e10cSrcweir 	}
121cdf0e10cSrcweir 	sal_Bool bRes = sal_True;
122cdf0e10cSrcweir 	for( i = 0 ; i < TEST_UUID ; i ++ )
123cdf0e10cSrcweir 	{
124cdf0e10cSrcweir 		//printUuid( aNode[i] );
125cdf0e10cSrcweir 		for( i2 = i+1 ; i2 < TEST_UUID ; i2 ++ )
126cdf0e10cSrcweir 		{
127cdf0e10cSrcweir 			if ( rtl_compareUuid( aNode[i] , aNode[i2] ) == 0  )
128cdf0e10cSrcweir 			{
129cdf0e10cSrcweir 				bRes = sal_False;
130cdf0e10cSrcweir 				break;
131cdf0e10cSrcweir 			}
132cdf0e10cSrcweir 		}
133cdf0e10cSrcweir 		if ( bRes == sal_False )
134cdf0e10cSrcweir 			break;
135cdf0e10cSrcweir 	}
136cdf0e10cSrcweir 	CPPUNIT_ASSERT_MESSAGE("createUuid: every uuid must be different.", bRes == sal_True );
137cdf0e10cSrcweir     }*/
138cdf0e10cSrcweir 
139cdf0e10cSrcweir     CPPUNIT_TEST_SUITE(createUuid);
140cdf0e10cSrcweir     CPPUNIT_TEST(createUuid_001);
141cdf0e10cSrcweir     //CPPUNIT_TEST(createUuid_002);
142cdf0e10cSrcweir     CPPUNIT_TEST_SUITE_END();
143cdf0e10cSrcweir }; // class createUuid
144cdf0e10cSrcweir 
145cdf0e10cSrcweir namespace ThreadHelper
146cdf0e10cSrcweir {
147cdf0e10cSrcweir      void thread_sleep(sal_Int32 _nSec)
148cdf0e10cSrcweir     {
149cdf0e10cSrcweir #ifdef WNT      //Windows
150cdf0e10cSrcweir         Sleep(_nSec * 10 );
151cdf0e10cSrcweir #endif
152cdf0e10cSrcweir #if ( defined UNX ) || ( defined OS2 )  //Unix
153cdf0e10cSrcweir         sleep( _nSec );
154cdf0e10cSrcweir #endif
155cdf0e10cSrcweir     }
156cdf0e10cSrcweir }
157cdf0e10cSrcweir 
158cdf0e10cSrcweir class createNamedUuid : public CppUnit::TestFixture
159cdf0e10cSrcweir {
160cdf0e10cSrcweir public:
161cdf0e10cSrcweir     // initialise your test code values here.
162cdf0e10cSrcweir     void setUp()
163cdf0e10cSrcweir     {
164cdf0e10cSrcweir     }
165cdf0e10cSrcweir 
166cdf0e10cSrcweir     void tearDown()
167cdf0e10cSrcweir     {
168cdf0e10cSrcweir     }
169cdf0e10cSrcweir 
170cdf0e10cSrcweir     void createNamedUuid_001()
171cdf0e10cSrcweir     {
172cdf0e10cSrcweir 		sal_uInt8 NameSpace_DNS[16] = RTL_UUID_NAMESPACE_DNS;
173cdf0e10cSrcweir 		sal_uInt8 NameSpace_URL[16] = RTL_UUID_NAMESPACE_URL;
174cdf0e10cSrcweir 		sal_uInt8 pPriorCalculatedUUID[16] = {
175cdf0e10cSrcweir 			0x52,0xc9,0x30,0xa5,
176cdf0e10cSrcweir 			0xd1,0x61,0x3b,0x16,
177cdf0e10cSrcweir 			0x98,0xc5,0xf8,0xd1,
178cdf0e10cSrcweir 			0x10,0x10,0x6d,0x4d };
179cdf0e10cSrcweir 
180cdf0e10cSrcweir 		sal_uInt8 pNamedUUID[16], pNamedUUID2[16];
181cdf0e10cSrcweir 
182cdf0e10cSrcweir 		// Same name does generate the same uuid
183cdf0e10cSrcweir 		rtl_String *pName = 0;
184cdf0e10cSrcweir 		rtl_string_newFromStr( &pName , "this is a bla.blubs.DNS-Name" );
185cdf0e10cSrcweir 		rtl_createNamedUuid( pNamedUUID , NameSpace_DNS , pName );
186cdf0e10cSrcweir 		rtl_createNamedUuid( pNamedUUID2 , NameSpace_DNS , pName );
187cdf0e10cSrcweir 		CPPUNIT_ASSERT_MESSAGE( "Same name should generate the same uuid", ! memcmp( pNamedUUID , pNamedUUID2 , 16 ) && rtl_compareUuid( pNamedUUID , pNamedUUID2 ) == 0 );
188cdf0e10cSrcweir 		CPPUNIT_ASSERT_MESSAGE( "Same name should generate the same uuid", ! memcmp( pNamedUUID  , pPriorCalculatedUUID , 16 ) );
189cdf0e10cSrcweir 
190cdf0e10cSrcweir 		// Different names does not generate the same uuid
191cdf0e10cSrcweir 		rtl_string_newFromStr( &pName , "this is a bla.blubs.DNS-Namf" );
192cdf0e10cSrcweir 		rtl_createNamedUuid( pNamedUUID2 , NameSpace_DNS , pName );
193cdf0e10cSrcweir 		CPPUNIT_ASSERT_MESSAGE("Different names does not generate the same uuid.", memcmp( pNamedUUID , pNamedUUID2 , 16 ) );
194cdf0e10cSrcweir 
195cdf0e10cSrcweir 		// the same name with different namespace uuid produces different uuids
196cdf0e10cSrcweir 		rtl_createNamedUuid( pNamedUUID , NameSpace_URL , pName );
197cdf0e10cSrcweir 		CPPUNIT_ASSERT_MESSAGE( " same name with different namespace uuid produces different uuids", memcmp( pNamedUUID , pNamedUUID2 , 16 ) && rtl_compareUuid( pNamedUUID , pNamedUUID2 ) != 0);
198cdf0e10cSrcweir 
199cdf0e10cSrcweir 		//test compareUuid
200cdf0e10cSrcweir 		if ( rtl_compareUuid( pNamedUUID , pNamedUUID2 ) > 0 )
201cdf0e10cSrcweir 		{	CPPUNIT_ASSERT_MESSAGE( " compare uuids", rtl_compareUuid( pNamedUUID2 , pNamedUUID ) < 0);
202cdf0e10cSrcweir 		}
203cdf0e10cSrcweir 		else
204cdf0e10cSrcweir 			CPPUNIT_ASSERT_MESSAGE( " compare uuids", rtl_compareUuid( pNamedUUID2 , pNamedUUID ) > 0);
205cdf0e10cSrcweir 
206cdf0e10cSrcweir 		rtl_string_release( pName );
207cdf0e10cSrcweir     }
208cdf0e10cSrcweir 
209cdf0e10cSrcweir     CPPUNIT_TEST_SUITE(createNamedUuid);
210cdf0e10cSrcweir     CPPUNIT_TEST(createNamedUuid_001);
211cdf0e10cSrcweir     CPPUNIT_TEST_SUITE_END();
212cdf0e10cSrcweir }; // class createNamedUuid
213cdf0e10cSrcweir 
214cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_Uuid::createUuid, "rtl_Uuid");
215cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_Uuid::createNamedUuid, "rtl_Uuid");
216cdf0e10cSrcweir } // namespace rtl_Uuid
217cdf0e10cSrcweir 
218cdf0e10cSrcweir // -----------------------------------------------------------------------------
219cdf0e10cSrcweir 
220cdf0e10cSrcweir // this macro creates an empty function, which will called by the RegisterAllFunctions()
221cdf0e10cSrcweir // to let the user the possibility to also register some functions by hand.
222cdf0e10cSrcweir NOADDITIONAL;
223cdf0e10cSrcweir 
224cdf0e10cSrcweir 
225