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