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