xref: /aoo42x/main/sal/qa/rtl/uuid/rtl_Uuid.cxx (revision a03c9fa9)
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 <rtl/uuid.h>
31 #include <rtl/ustring.h>
32 #include <rtl/ustring.hxx>
33 
34 #ifdef WNT
35 #define WIN32_LEAN_AND_MEAN
36 #include <tools/prewin.h>
37 #include <windows.h>
38 #include <tools/postwin.h>
39 #endif
40 
41 #ifdef UNX
42 #include <unistd.h>
43 #include <time.h>
44 #endif
45 
46 #include "gtest/gtest.h"
47 
48 using namespace rtl;
49 
50 /** print a UNI_CODE String. And also print some comments of the string.
51 */
52 inline void printUString( const ::rtl::OUString & str, const sal_Char * msg = "" )
53 {
54 	printf("#%s #printUString_u# ", msg );
55 	rtl::OString aString;
56 	aString = ::rtl::OUStringToOString( str, RTL_TEXTENCODING_ASCII_US );
57 	printf("%s\n", (char *)aString.getStr( ) );
58 }
59 
60 /************************************************************************
61  * For diagnostics( from sal/test/testuuid.cxx )
62  ************************************************************************/
63 
64 void printUuid( sal_uInt8 *pNode )
65 {
66 	for( sal_Int32 i1 = 0 ; i1 < 4 ; i1++ )
67 	{
68 		for( sal_Int32 i2 = 0 ; i2 < 4 ; i2++ )
69 		{
70 			printf( "%02x" , pNode[i1*4 +i2] );
71 		}
72 		if( i1 == 3 )
73 			break;
74 		printf( "-" );
75 	}
76 
77 	printf( "\n# " );
78 }
79 
80 namespace rtl_Uuid
81 {
82 class createUuid : public ::testing::Test
83 {
84 public:
85     // initialise your test code values here.
86     void SetUp()
87     {
88     }
89 
90     void TearDown()
91     {
92     }
93 
94 }; // class createUuid
95 
96 #define TEST_UUID 20
97 TEST_F(createUuid, createUuid_001)
98 {
99     sal_uInt8 aNode[TEST_UUID][16];
100     sal_Int32 i,i2;
101     for( i = 0 ; i < TEST_UUID ; i ++ )
102     {
103         rtl_createUuid( aNode[i], 0, sal_False );
104     }
105     sal_Bool bRes = sal_True;
106     for( i = 0 ; i < TEST_UUID ; i ++ )
107     {
108         for( i2 = i+1 ; i2 < TEST_UUID ; i2 ++ )
109         {
110             if ( rtl_compareUuid( aNode[i] , aNode[i2] ) == 0  )
111             {
112                 bRes = sal_False;
113                 break;
114             }
115         }
116         if ( bRes == sal_False )
117             break;
118     }
119     ASSERT_TRUE(bRes == sal_True) << "createUuid: every uuid must be different.";
120 }
121 
122 /*
123 TEST_F(createUuid, createUuid_002)
124 {
125     sal_uInt8 pNode[16];
126     sal_uInt8 aNode[TEST_UUID][16];
127     sal_Int32 i,i2;
128     for( i = 0 ; i < TEST_UUID ; i ++ )
129     {
130         rtl_createUuid( aNode[i], pNode, sal_True );
131     }
132     sal_Bool bRes = sal_True;
133     for( i = 0 ; i < TEST_UUID ; i ++ )
134     {
135         //printUuid( aNode[i] );
136         for( i2 = i+1 ; i2 < TEST_UUID ; i2 ++ )
137         {
138             if ( rtl_compareUuid( aNode[i] , aNode[i2] ) == 0  )
139             {
140                 bRes = sal_False;
141                 break;
142             }
143         }
144         if ( bRes == sal_False )
145             break;
146     }
147     ASSERT_TRUE(bRes == sal_True) << "createUuid: every uuid must be different.";
148 }*/
149 
150 namespace ThreadHelper
151 {
152      void thread_sleep(sal_Int32 _nSec)
153     {
154 #ifdef WNT      //Windows
155         Sleep(_nSec * 10 );
156 #endif
157 #if ( defined UNX ) || ( defined OS2 )  //Unix
158         sleep( _nSec );
159 #endif
160     }
161 }
162 
163 class createNamedUuid : public ::testing::Test
164 {
165 public:
166     // initialise your test code values here.
167     void SetUp()
168     {
169     }
170 
171     void TearDown()
172     {
173     }
174 }; // class createNamedUuid
175 
176 TEST_F(createNamedUuid, createNamedUuid_001)
177 {
178     sal_uInt8 NameSpace_DNS[16] = RTL_UUID_NAMESPACE_DNS;
179     sal_uInt8 NameSpace_URL[16] = RTL_UUID_NAMESPACE_URL;
180     sal_uInt8 pPriorCalculatedUUID[16] = {
181         0x52,0xc9,0x30,0xa5,
182         0xd1,0x61,0x3b,0x16,
183         0x98,0xc5,0xf8,0xd1,
184         0x10,0x10,0x6d,0x4d };
185 
186     sal_uInt8 pNamedUUID[16], pNamedUUID2[16];
187 
188     // Same name does generate the same uuid
189     rtl_String *pName = 0;
190     rtl_string_newFromStr( &pName , "this is a bla.blubs.DNS-Name" );
191     rtl_createNamedUuid( pNamedUUID , NameSpace_DNS , pName );
192     rtl_createNamedUuid( pNamedUUID2 , NameSpace_DNS , pName );
193     ASSERT_TRUE(! memcmp( pNamedUUID , pNamedUUID2 , 16 ) && rtl_compareUuid( pNamedUUID , pNamedUUID2 ) == 0)
194         <<  "Same name should generate the same uuid";
195     ASSERT_TRUE(! memcmp( pNamedUUID  , pPriorCalculatedUUID , 16 )) <<  "Same name should generate the same uuid";
196 
197     // Different names does not generate the same uuid
198     rtl_string_newFromStr( &pName , "this is a bla.blubs.DNS-Namf" );
199     rtl_createNamedUuid( pNamedUUID2 , NameSpace_DNS , pName );
200     ASSERT_TRUE(memcmp( pNamedUUID , pNamedUUID2 , 16 )) << "Different names does not generate the same uuid.";
201 
202     // the same name with different namespace uuid produces different uuids
203     rtl_createNamedUuid( pNamedUUID , NameSpace_URL , pName );
204     ASSERT_TRUE(memcmp( pNamedUUID , pNamedUUID2 , 16 ) && rtl_compareUuid( pNamedUUID , pNamedUUID2 ) != 0)
205         <<  " same name with different namespace uuid produces different uuids";
206 
207     //test compareUuid
208     if ( rtl_compareUuid( pNamedUUID , pNamedUUID2 ) > 0 )
209     {   ASSERT_TRUE(rtl_compareUuid( pNamedUUID2 , pNamedUUID ) < 0) <<  " compare uuids";
210     }
211     else
212         ASSERT_TRUE(rtl_compareUuid( pNamedUUID2 , pNamedUUID ) > 0) <<  " compare uuids";
213 
214     rtl_string_release( pName );
215 }
216 
217 } // namespace rtl_Uuid
218 
219 int main(int argc, char **argv)
220 {
221     ::testing::InitGoogleTest(&argc, argv);
222     return RUN_ALL_TESTS();
223 }
224