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