Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 :
21 : #include <math.h>
22 : #include <stdio.h>
23 : #include <string.h>
24 :
25 : #include <rtl/uuid.h>
26 : #include <rtl/ustring.h>
27 : #include <rtl/ustring.hxx>
28 : #include <cppunit/TestFixture.h>
29 : #include <cppunit/extensions/HelperMacros.h>
30 : #include <cppunit/plugin/TestPlugIn.h>
31 :
32 : #ifdef WNT
33 : #include <windows.h>
34 : #elif defined UNX
35 : #include <unistd.h>
36 : #include <time.h>
37 : #endif
38 :
39 : #define t_print printf
40 :
41 : using ::rtl::OUString;
42 : using ::rtl::OUStringToOString;
43 : using ::rtl::OString;
44 : /** print a UNI_CODE String. And also print some comments of the string.
45 : */
46 : inline void printUString( const ::rtl::OUString & str, const sal_Char * msg = "" )
47 : {
48 : t_print("#%s #printUString_u# ", msg );
49 : rtl::OString aString;
50 : aString = ::rtl::OUStringToOString( str, RTL_TEXTENCODING_ASCII_US );
51 : t_print("%s\n", (char *)aString.getStr( ) );
52 : }
53 :
54 : /************************************************************************
55 : * For diagnostics( from sal/test/testuuid.cxx )
56 : ************************************************************************/
57 :
58 0 : void printUuid( sal_uInt8 *pNode )
59 : {
60 0 : for( sal_Int32 i1 = 0 ; i1 < 4 ; i1++ )
61 : {
62 0 : for( sal_Int32 i2 = 0 ; i2 < 4 ; i2++ )
63 : {
64 0 : printf( "%02x" , pNode[i1*4 +i2] );
65 : }
66 0 : if( i1 == 3 )
67 0 : break;
68 0 : printf( "-" );
69 : }
70 :
71 0 : printf( "\n# " );
72 0 : }
73 :
74 : namespace rtl_Uuid
75 : {
76 3 : class createUuid : public CppUnit::TestFixture
77 : {
78 : public:
79 : // initialise your test code values here.
80 1 : void setUp()
81 : {
82 1 : }
83 :
84 1 : void tearDown()
85 : {
86 1 : }
87 :
88 : #define TEST_UUID 20
89 1 : void createUuid_001()
90 : {
91 : sal_uInt8 aNode[TEST_UUID][16];
92 : sal_Int32 i,i2;
93 21 : for( i = 0 ; i < TEST_UUID ; i ++ )
94 : {
95 20 : rtl_createUuid( aNode[i], 0, sal_False );
96 : }
97 1 : sal_Bool bRes = sal_True;
98 21 : for( i = 0 ; i < TEST_UUID ; i ++ )
99 : {
100 210 : for( i2 = i+1 ; i2 < TEST_UUID ; i2 ++ )
101 : {
102 190 : if ( rtl_compareUuid( aNode[i] , aNode[i2] ) == 0 )
103 : {
104 0 : bRes = sal_False;
105 0 : break;
106 : }
107 : }
108 20 : if ( bRes == sal_False )
109 0 : break;
110 : }
111 1 : CPPUNIT_ASSERT_MESSAGE("createUuid: every uuid must be different.", bRes == sal_True );
112 1 : }
113 : /*
114 : void createUuid_002()
115 : {
116 : sal_uInt8 pNode[16];
117 : sal_uInt8 aNode[TEST_UUID][16];
118 : sal_Int32 i,i2;
119 : for( i = 0 ; i < TEST_UUID ; i ++ )
120 : {
121 : rtl_createUuid( aNode[i], pNode, sal_True );
122 : }
123 : sal_Bool bRes = sal_True;
124 : for( i = 0 ; i < TEST_UUID ; i ++ )
125 : {
126 : //printUuid( aNode[i] );
127 : for( i2 = i+1 ; i2 < TEST_UUID ; i2 ++ )
128 : {
129 : if ( rtl_compareUuid( aNode[i] , aNode[i2] ) == 0 )
130 : {
131 : bRes = sal_False;
132 : break;
133 : }
134 : }
135 : if ( bRes == sal_False )
136 : break;
137 : }
138 : CPPUNIT_ASSERT_MESSAGE("createUuid: every uuid must be different.", bRes == sal_True );
139 : }*/
140 :
141 2 : CPPUNIT_TEST_SUITE(createUuid);
142 1 : CPPUNIT_TEST(createUuid_001);
143 : //CPPUNIT_TEST(createUuid_002);
144 2 : CPPUNIT_TEST_SUITE_END();
145 : }; // class createUuid
146 :
147 3 : class createNamedUuid : public CppUnit::TestFixture
148 : {
149 : public:
150 : // initialise your test code values here.
151 1 : void setUp()
152 : {
153 1 : }
154 :
155 1 : void tearDown()
156 : {
157 1 : }
158 :
159 1 : void createNamedUuid_001()
160 : {
161 1 : sal_uInt8 NameSpace_DNS[16] = RTL_UUID_NAMESPACE_DNS;
162 1 : sal_uInt8 NameSpace_URL[16] = RTL_UUID_NAMESPACE_URL;
163 : sal_uInt8 pPriorCalculatedUUID[16] = {
164 : 0x52,0xc9,0x30,0xa5,
165 : 0xd1,0x61,0x3b,0x16,
166 : 0x98,0xc5,0xf8,0xd1,
167 1 : 0x10,0x10,0x6d,0x4d };
168 :
169 : sal_uInt8 pNamedUUID[16], pNamedUUID2[16];
170 :
171 : // Same name does generate the same uuid
172 1 : rtl_String *pName = 0;
173 1 : rtl_string_newFromStr( &pName , "this is a bla.blubs.DNS-Name" );
174 1 : rtl_createNamedUuid( pNamedUUID , NameSpace_DNS , pName );
175 1 : rtl_createNamedUuid( pNamedUUID2 , NameSpace_DNS , pName );
176 1 : CPPUNIT_ASSERT_MESSAGE( "Same name should generate the same uuid", ! memcmp( pNamedUUID , pNamedUUID2 , 16 ) && rtl_compareUuid( pNamedUUID , pNamedUUID2 ) == 0 );
177 1 : CPPUNIT_ASSERT_MESSAGE( "Same name should generate the same uuid", ! memcmp( pNamedUUID , pPriorCalculatedUUID , 16 ) );
178 :
179 : // Different names does not generate the same uuid
180 1 : rtl_string_newFromStr( &pName , "this is a bla.blubs.DNS-Namf" );
181 1 : rtl_createNamedUuid( pNamedUUID2 , NameSpace_DNS , pName );
182 1 : CPPUNIT_ASSERT_MESSAGE("Different names does not generate the same uuid.", memcmp( pNamedUUID , pNamedUUID2 , 16 ) );
183 :
184 : // the same name with different namespace uuid produces different uuids
185 1 : rtl_createNamedUuid( pNamedUUID , NameSpace_URL , pName );
186 1 : CPPUNIT_ASSERT_MESSAGE( " same name with different namespace uuid produces different uuids", memcmp( pNamedUUID , pNamedUUID2 , 16 ) && rtl_compareUuid( pNamedUUID , pNamedUUID2 ) != 0);
187 :
188 : //test compareUuid
189 1 : if ( rtl_compareUuid( pNamedUUID , pNamedUUID2 ) > 0 )
190 0 : { CPPUNIT_ASSERT_MESSAGE( " compare uuids", rtl_compareUuid( pNamedUUID2 , pNamedUUID ) < 0);
191 : }
192 : else
193 1 : CPPUNIT_ASSERT_MESSAGE( " compare uuids", rtl_compareUuid( pNamedUUID2 , pNamedUUID ) > 0);
194 :
195 1 : rtl_string_release( pName );
196 1 : }
197 :
198 2 : CPPUNIT_TEST_SUITE(createNamedUuid);
199 1 : CPPUNIT_TEST(createNamedUuid_001);
200 2 : CPPUNIT_TEST_SUITE_END();
201 : }; // class createNamedUuid
202 :
203 1 : CPPUNIT_TEST_SUITE_REGISTRATION(rtl_Uuid::createUuid);
204 1 : CPPUNIT_TEST_SUITE_REGISTRATION(rtl_Uuid::createNamedUuid);
205 : } // namespace rtl_Uuid
206 :
207 : // -----------------------------------------------------------------------------
208 :
209 : // this macro creates an empty function, which will called by the RegisterAllFunctions()
210 : // to let the user the possibility to also register some functions by hand.
211 4 : CPPUNIT_PLUGIN_IMPLEMENT();
212 :
213 :
214 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|