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