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 : #include <math.h>
21 : #include <stdio.h>
22 : #include <string.h>
23 :
24 : #include <rtl/uuid.h>
25 : #include <rtl/ustring.h>
26 : #include <rtl/ustring.hxx>
27 : #include <cppunit/TestFixture.h>
28 : #include <cppunit/extensions/HelperMacros.h>
29 : #include <cppunit/plugin/TestPlugIn.h>
30 :
31 : #ifdef WNT
32 : #include <windows.h>
33 : #elif defined UNX
34 : #include <unistd.h>
35 : #include <time.h>
36 : #endif
37 :
38 : using ::rtl::OUString;
39 : using ::rtl::OUStringToOString;
40 : using ::rtl::OString;
41 :
42 : namespace rtl_Uuid
43 : {
44 6 : class createUuid : public CppUnit::TestFixture
45 : {
46 : public:
47 : // initialise your test code values here.
48 2 : void setUp() SAL_OVERRIDE
49 : {
50 2 : }
51 :
52 2 : void tearDown() SAL_OVERRIDE
53 : {
54 2 : }
55 :
56 : #define TEST_UUID 20
57 2 : void createUuid_001()
58 : {
59 : sal_uInt8 aNode[TEST_UUID][16];
60 : sal_Int32 i,i2;
61 42 : for( i = 0 ; i < TEST_UUID ; i ++ )
62 : {
63 40 : rtl_createUuid( aNode[i], 0, sal_False );
64 : }
65 2 : bool bRes = true;
66 42 : for( i = 0 ; i < TEST_UUID ; i ++ )
67 : {
68 420 : for( i2 = i+1 ; i2 < TEST_UUID ; i2 ++ )
69 : {
70 380 : if ( rtl_compareUuid( aNode[i] , aNode[i2] ) == 0 )
71 : {
72 0 : bRes = false;
73 0 : break;
74 : }
75 : }
76 40 : if ( !bRes )
77 0 : break;
78 : }
79 2 : CPPUNIT_ASSERT_MESSAGE("createUuid: every uuid must be different.", bRes);
80 2 : }
81 : /*
82 : void createUuid_002()
83 : {
84 : sal_uInt8 pNode[16];
85 : sal_uInt8 aNode[TEST_UUID][16];
86 : sal_Int32 i,i2;
87 : for( i = 0 ; i < TEST_UUID ; i ++ )
88 : {
89 : rtl_createUuid( aNode[i], pNode, sal_True );
90 : }
91 : sal_Bool bRes = sal_True;
92 : for( i = 0 ; i < TEST_UUID ; i ++ )
93 : {
94 : //printUuid( aNode[i] );
95 : for( i2 = i+1 ; i2 < TEST_UUID ; i2 ++ )
96 : {
97 : if ( rtl_compareUuid( aNode[i] , aNode[i2] ) == 0 )
98 : {
99 : bRes = sal_False;
100 : break;
101 : }
102 : }
103 : if ( bRes == sal_False )
104 : break;
105 : }
106 : CPPUNIT_ASSERT_MESSAGE("createUuid: every uuid must be different.", bRes == sal_True );
107 : }*/
108 :
109 4 : CPPUNIT_TEST_SUITE(createUuid);
110 2 : CPPUNIT_TEST(createUuid_001);
111 : //CPPUNIT_TEST(createUuid_002);
112 4 : CPPUNIT_TEST_SUITE_END();
113 : }; // class createUuid
114 :
115 6 : class createNamedUuid : public CppUnit::TestFixture
116 : {
117 : public:
118 : // initialise your test code values here.
119 2 : void setUp() SAL_OVERRIDE
120 : {
121 2 : }
122 :
123 2 : void tearDown() SAL_OVERRIDE
124 : {
125 2 : }
126 :
127 2 : void createNamedUuid_001()
128 : {
129 2 : sal_uInt8 NameSpace_DNS[16] = RTL_UUID_NAMESPACE_DNS;
130 2 : sal_uInt8 NameSpace_URL[16] = RTL_UUID_NAMESPACE_URL;
131 : sal_uInt8 pPriorCalculatedUUID[16] = {
132 : 0x52,0xc9,0x30,0xa5,
133 : 0xd1,0x61,0x3b,0x16,
134 : 0x98,0xc5,0xf8,0xd1,
135 2 : 0x10,0x10,0x6d,0x4d };
136 :
137 : sal_uInt8 pNamedUUID[16], pNamedUUID2[16];
138 :
139 : // Same name does generate the same uuid
140 2 : rtl_String *pName = 0;
141 2 : rtl_string_newFromStr( &pName , "this is a bla.blubs.DNS-Name" );
142 2 : rtl_createNamedUuid( pNamedUUID , NameSpace_DNS , pName );
143 2 : rtl_createNamedUuid( pNamedUUID2 , NameSpace_DNS , pName );
144 2 : CPPUNIT_ASSERT_MESSAGE( "Same name should generate the same uuid", ! memcmp( pNamedUUID , pNamedUUID2 , 16 ) && rtl_compareUuid( pNamedUUID , pNamedUUID2 ) == 0 );
145 2 : CPPUNIT_ASSERT_MESSAGE( "Same name should generate the same uuid", ! memcmp( pNamedUUID , pPriorCalculatedUUID , 16 ) );
146 :
147 : // Different names does not generate the same uuid
148 2 : rtl_string_newFromStr( &pName , "this is a bla.blubs.DNS-Namf" );
149 2 : rtl_createNamedUuid( pNamedUUID2 , NameSpace_DNS , pName );
150 2 : CPPUNIT_ASSERT_MESSAGE("Different names does not generate the same uuid.", memcmp( pNamedUUID , pNamedUUID2 , 16 ) );
151 :
152 : // the same name with different namespace uuid produces different uuids
153 2 : rtl_createNamedUuid( pNamedUUID , NameSpace_URL , pName );
154 2 : CPPUNIT_ASSERT_MESSAGE( " same name with different namespace uuid produces different uuids", memcmp( pNamedUUID , pNamedUUID2 , 16 ) && rtl_compareUuid( pNamedUUID , pNamedUUID2 ) != 0);
155 :
156 : //test compareUuid
157 2 : if ( rtl_compareUuid( pNamedUUID , pNamedUUID2 ) > 0 )
158 0 : { CPPUNIT_ASSERT_MESSAGE( " compare uuids", rtl_compareUuid( pNamedUUID2 , pNamedUUID ) < 0);
159 : }
160 : else
161 2 : CPPUNIT_ASSERT_MESSAGE( " compare uuids", rtl_compareUuid( pNamedUUID2 , pNamedUUID ) > 0);
162 :
163 2 : rtl_string_release( pName );
164 2 : }
165 :
166 4 : CPPUNIT_TEST_SUITE(createNamedUuid);
167 2 : CPPUNIT_TEST(createNamedUuid_001);
168 4 : CPPUNIT_TEST_SUITE_END();
169 : }; // class createNamedUuid
170 :
171 2 : CPPUNIT_TEST_SUITE_REGISTRATION(rtl_Uuid::createUuid);
172 2 : CPPUNIT_TEST_SUITE_REGISTRATION(rtl_Uuid::createNamedUuid);
173 : } // namespace rtl_Uuid
174 :
175 : // this macro creates an empty function, which will called by the RegisterAllFunctions()
176 : // to let the user the possibility to also register some functions by hand.
177 8 : CPPUNIT_PLUGIN_IMPLEMENT();
178 :
179 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|