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