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