1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the test suite of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU
19** General Public License version 3 as published by the Free Software
20** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21** included in the packaging of this file. Please review the following
22** information to ensure the GNU General Public License requirements will
23** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24**
25** $QT_END_LICENSE$
26**
27****************************************************************************/
28
29
30#include <QtTest/QtTest>
31
32#include <qcoreapplication.h>
33#include <quuid.h>
34
35class tst_QUuid : public QObject
36{
37 Q_OBJECT
38
39private slots:
40 void initTestCase();
41
42 void fromChar();
43 void toString();
44 void fromString_data();
45 void fromString();
46 void toByteArray();
47 void fromByteArray();
48 void toRfc4122();
49 void fromRfc4122();
50 void createUuidV3OrV5();
51 void check_QDataStream();
52 void isNull();
53 void equal();
54 void notEqual();
55 void cpp11();
56
57 // Only in Qt > 3.2.x
58 void generate();
59 void less();
60 void more();
61 void variants();
62 void versions();
63
64 void threadUniqueness();
65 void processUniqueness();
66
67 void hash();
68
69 void qvariant();
70 void qvariant_conversion();
71
72 void darwinTypes();
73
74public:
75 // Variables
76 QUuid uuidNS;
77 QUuid uuidA;
78 QUuid uuidB;
79 QUuid uuidC;
80 QUuid uuidD;
81};
82
83void tst_QUuid::initTestCase()
84{
85 //It's NameSpace_DNS in RFC4122
86 //"{6ba7b810-9dad-11d1-80b4-00c04fd430c8}";
87 uuidNS = QUuid(0x6ba7b810, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8);
88
89 //"{fc69b59e-cc34-4436-a43c-ee95d128b8c5}";
90 uuidA = QUuid(0xfc69b59e, 0xcc34, 0x4436, 0xa4, 0x3c, 0xee, 0x95, 0xd1, 0x28, 0xb8, 0xc5);
91
92 //"{1ab6e93a-b1cb-4a87-ba47-ec7e99039a7b}";
93 uuidB = QUuid(0x1ab6e93a, 0xb1cb, 0x4a87, 0xba, 0x47, 0xec, 0x7e, 0x99, 0x03, 0x9a, 0x7b);
94
95#if QT_CONFIG(process)
96 // chdir to the directory containing our testdata, then refer to it with relative paths
97#ifdef Q_OS_ANDROID
98 QString testdata_dir = QCoreApplication::applicationDirPath();
99#else // !Q_OS_ANDROID
100 QString testdata_dir = QFileInfo(QFINDTESTDATA("testProcessUniqueness")).absolutePath();
101#endif
102 QVERIFY2(QDir::setCurrent(testdata_dir), qPrintable("Could not chdir to " + testdata_dir));
103#endif
104
105 //"{3d813cbb-47fb-32ba-91df-831e1593ac29}"; http://www.rfc-editor.org/errata_search.php?rfc=4122&eid=1352
106 uuidC = QUuid(0x3d813cbb, 0x47fb, 0x32ba, 0x91, 0xdf, 0x83, 0x1e, 0x15, 0x93, 0xac, 0x29);
107
108 //"{21f7f8de-8051-5b89-8680-0195ef798b6a}";
109 uuidD = QUuid(0x21f7f8de, 0x8051, 0x5b89, 0x86, 0x80, 0x01, 0x95, 0xef, 0x79, 0x8b, 0x6a);
110}
111
112void tst_QUuid::fromChar()
113{
114 QCOMPARE(uuidA, QUuid("{fc69b59e-cc34-4436-a43c-ee95d128b8c5}"));
115 QCOMPARE(uuidA, QUuid("fc69b59e-cc34-4436-a43c-ee95d128b8c5}"));
116 QCOMPARE(uuidA, QUuid("{fc69b59e-cc34-4436-a43c-ee95d128b8c5"));
117 QCOMPARE(uuidA, QUuid("fc69b59e-cc34-4436-a43c-ee95d128b8c5"));
118 QCOMPARE(QUuid(), QUuid("{fc69b59e-cc34-4436-a43c-ee95d128b8c"));
119 QCOMPARE(QUuid(), QUuid("{fc69b59e-cc34"));
120 QCOMPARE(QUuid(), QUuid("fc69b59e-cc34-"));
121 QCOMPARE(QUuid(), QUuid("fc69b59e-cc34"));
122 QCOMPARE(QUuid(), QUuid("cc34"));
123 QCOMPARE(QUuid(), QUuid(NULL));
124
125 QCOMPARE(uuidB, QUuid(QString("{1ab6e93a-b1cb-4a87-ba47-ec7e99039a7b}")));
126}
127
128void tst_QUuid::toString()
129{
130 QCOMPARE(uuidA.toString(), QString("{fc69b59e-cc34-4436-a43c-ee95d128b8c5}"));
131 QCOMPARE(uuidA.toString(QUuid::WithoutBraces),
132 QString("fc69b59e-cc34-4436-a43c-ee95d128b8c5"));
133 QCOMPARE(uuidA.toString(QUuid::Id128),
134 QString("fc69b59ecc344436a43cee95d128b8c5"));
135
136 QCOMPARE(uuidB.toString(), QString("{1ab6e93a-b1cb-4a87-ba47-ec7e99039a7b}"));
137 QCOMPARE(uuidB.toString(QUuid::WithoutBraces),
138 QString("1ab6e93a-b1cb-4a87-ba47-ec7e99039a7b"));
139 QCOMPARE(uuidB.toString(QUuid::Id128),
140 QString("1ab6e93ab1cb4a87ba47ec7e99039a7b"));
141}
142
143void tst_QUuid::fromString_data()
144{
145 QTest::addColumn<QUuid>(name: "expected");
146 QTest::addColumn<QString>(name: "input");
147
148 QUuid invalid = {};
149
150#define ROW(which, string) \
151 QTest::addRow("%-38s -> %s", string, #which) << which << string
152 ROW(uuidA, "{fc69b59e-cc34-4436-a43c-ee95d128b8c5}");
153 ROW(uuidA, "fc69b59e-cc34-4436-a43c-ee95d128b8c5}");
154 ROW(uuidA, "{fc69b59e-cc34-4436-a43c-ee95d128b8c5" );
155 ROW(uuidA, "fc69b59e-cc34-4436-a43c-ee95d128b8c5" );
156
157 ROW(uuidA, "{fc69b59e-cc34-4436-a43c-ee95d128b8c56"); // too long (not an error!)
158 ROW(invalid, "{fc69b59e-cc34-4436-a43c-ee95d128b8c" ); // premature end (within length limits)
159 ROW(invalid, " fc69b59e-cc34-4436-a43c-ee95d128b8c5}"); // leading space
160 ROW(uuidA, "{fc69b59e-cc34-4436-a43c-ee95d128b8c5 "); // trailing space (not an error!)
161 ROW(invalid, "{gc69b59e-cc34-4436-a43c-ee95d128b8c5}"); // non-hex digit in 1st group
162 ROW(invalid, "{fc69b59e-cp34-4436-a43c-ee95d128b8c5}"); // non-hex digit in 2nd group
163 ROW(invalid, "{fc69b59e-cc34-44r6-a43c-ee95d128b8c5}"); // non-hex digit in 3rd group
164 ROW(invalid, "{fc69b59e-cc34-4436-a4yc-ee95d128b8c5}"); // non-hex digit in 4th group
165 ROW(invalid, "{fc69b59e-cc34-4436-a43c-ee95d128j8c5}"); // non-hex digit in last group
166 ROW(invalid, "(fc69b59e-cc34-4436-a43c-ee95d128b8c5}"); // wrong initial character
167 ROW(invalid, "{fc69b59e+cc34-4436-a43c-ee95d128b8c5}"); // wrong 1st separator
168 ROW(invalid, "{fc69b59e-cc34*4436-a43c-ee95d128b8c5}"); // wrong 2nd separator
169 ROW(invalid, "{fc69b59e-cc34-44366a43c-ee95d128b8c5}"); // wrong 3rd separator
170 ROW(invalid, "{fc69b59e-cc34-4436-a43c\303\244ee95d128b8c5}"); // wrong 4th separator (&auml;)
171 ROW(uuidA, "{fc69b59e-cc34-4436-a43c-ee95d128b8c5)"); // wrong final character (not an error!)
172
173 ROW(uuidB, "{1ab6e93a-b1cb-4a87-ba47-ec7e99039a7b}");
174#undef ROW
175}
176
177void tst_QUuid::fromString()
178{
179 QFETCH(const QUuid, expected);
180 QFETCH(const QString, input);
181
182 const auto inputL1 = input.toLatin1();
183 const auto inputU8 = input.toUtf8();
184
185 QCOMPARE(expected, QUuid(input));
186 QCOMPARE(expected, QUuid(inputU8));
187 QCOMPARE(expected, QUuid(inputL1));
188
189 QCOMPARE(expected, QUuid::fromString(input));
190
191 // for QLatin1String, construct one whose data() is not NUL-terminated:
192 const auto longerInputL1 = inputL1 + '5'; // the '5' makes the premature end check incorrectly succeed
193 const auto inputL1S = QLatin1String(longerInputL1.data(), inputL1.size());
194 QCOMPARE(expected, QUuid::fromString(inputL1S));
195}
196
197void tst_QUuid::toByteArray()
198{
199 QCOMPARE(uuidA.toByteArray(), QByteArray("{fc69b59e-cc34-4436-a43c-ee95d128b8c5}"));
200 QCOMPARE(uuidA.toByteArray(QUuid::WithoutBraces),
201 QByteArray("fc69b59e-cc34-4436-a43c-ee95d128b8c5"));
202 QCOMPARE(uuidA.toByteArray(QUuid::Id128),
203 QByteArray("fc69b59ecc344436a43cee95d128b8c5"));
204
205 QCOMPARE(uuidB.toByteArray(), QByteArray("{1ab6e93a-b1cb-4a87-ba47-ec7e99039a7b}"));
206 QCOMPARE(uuidB.toByteArray(QUuid::WithoutBraces),
207 QByteArray("1ab6e93a-b1cb-4a87-ba47-ec7e99039a7b"));
208 QCOMPARE(uuidB.toByteArray(QUuid::Id128),
209 QByteArray("1ab6e93ab1cb4a87ba47ec7e99039a7b"));
210}
211
212void tst_QUuid::fromByteArray()
213{
214 QCOMPARE(uuidA, QUuid(QByteArray("{fc69b59e-cc34-4436-a43c-ee95d128b8c5}")));
215 QCOMPARE(uuidA, QUuid(QByteArray("fc69b59e-cc34-4436-a43c-ee95d128b8c5}")));
216 QCOMPARE(uuidA, QUuid(QByteArray("{fc69b59e-cc34-4436-a43c-ee95d128b8c5")));
217 QCOMPARE(uuidA, QUuid(QByteArray("fc69b59e-cc34-4436-a43c-ee95d128b8c5")));
218 QCOMPARE(QUuid(), QUuid(QByteArray("{fc69b59e-cc34-4436-a43c-ee95d128b8c")));
219
220 QCOMPARE(uuidB, QUuid(QByteArray("{1ab6e93a-b1cb-4a87-ba47-ec7e99039a7b}")));
221}
222
223void tst_QUuid::toRfc4122()
224{
225 QCOMPARE(uuidA.toRfc4122(), QByteArray::fromHex("fc69b59ecc344436a43cee95d128b8c5"));
226
227 QCOMPARE(uuidB.toRfc4122(), QByteArray::fromHex("1ab6e93ab1cb4a87ba47ec7e99039a7b"));
228}
229
230void tst_QUuid::fromRfc4122()
231{
232 QCOMPARE(uuidA, QUuid::fromRfc4122(QByteArray::fromHex("fc69b59ecc344436a43cee95d128b8c5")));
233
234 QCOMPARE(uuidB, QUuid::fromRfc4122(QByteArray::fromHex("1ab6e93ab1cb4a87ba47ec7e99039a7b")));
235}
236
237void tst_QUuid::createUuidV3OrV5()
238{
239 //"www.widgets.com" is also from RFC4122
240 QCOMPARE(uuidC, QUuid::createUuidV3(uuidNS, QByteArray("www.widgets.com")));
241 QCOMPARE(uuidC, QUuid::createUuidV3(uuidNS, QString("www.widgets.com")));
242
243 QCOMPARE(uuidD, QUuid::createUuidV5(uuidNS, QByteArray("www.widgets.com")));
244 QCOMPARE(uuidD, QUuid::createUuidV5(uuidNS, QString("www.widgets.com")));
245}
246
247void tst_QUuid::check_QDataStream()
248{
249 QUuid tmp;
250 QByteArray ar;
251 {
252 QDataStream out(&ar,QIODevice::WriteOnly);
253 out.setByteOrder(QDataStream::BigEndian);
254 out << uuidA;
255 }
256 {
257 QDataStream in(&ar,QIODevice::ReadOnly);
258 in.setByteOrder(QDataStream::BigEndian);
259 in >> tmp;
260 QCOMPARE(uuidA, tmp);
261 }
262 {
263 QDataStream out(&ar,QIODevice::WriteOnly);
264 out.setByteOrder(QDataStream::LittleEndian);
265 out << uuidA;
266 }
267 {
268 QDataStream in(&ar,QIODevice::ReadOnly);
269 in.setByteOrder(QDataStream::LittleEndian);
270 in >> tmp;
271 QCOMPARE(uuidA, tmp);
272 }
273}
274
275void tst_QUuid::isNull()
276{
277 QVERIFY( !uuidA.isNull() );
278
279 QUuid should_be_null_uuid;
280 QVERIFY( should_be_null_uuid.isNull() );
281}
282
283
284void tst_QUuid::equal()
285{
286 QVERIFY( !(uuidA == uuidB) );
287
288 QUuid copy(uuidA);
289 QCOMPARE(uuidA, copy);
290
291 QUuid assigned;
292 assigned = uuidA;
293 QCOMPARE(uuidA, assigned);
294}
295
296
297void tst_QUuid::notEqual()
298{
299 QVERIFY( uuidA != uuidB );
300}
301
302void tst_QUuid::cpp11() {
303#ifdef Q_COMPILER_UNIFORM_INIT
304 // "{fc69b59e-cc34-4436-a43c-ee95d128b8c5}" cf, initTestCase
305 Q_DECL_CONSTEXPR QUuid u1{0xfc69b59e, 0xcc34, 0x4436, 0xa4, 0x3c, 0xee, 0x95, 0xd1, 0x28, 0xb8, 0xc5};
306 Q_DECL_CONSTEXPR QUuid u2 = {0xfc69b59e, 0xcc34, 0x4436, 0xa4, 0x3c, 0xee, 0x95, 0xd1, 0x28, 0xb8, 0xc5};
307 Q_UNUSED(u1);
308 Q_UNUSED(u2);
309#else
310 QSKIP("This compiler is not in C++11 mode or it doesn't support uniform initialization");
311#endif
312}
313
314void tst_QUuid::generate()
315{
316 QUuid shouldnt_be_null_uuidA = QUuid::createUuid();
317 QUuid shouldnt_be_null_uuidB = QUuid::createUuid();
318 QVERIFY( !shouldnt_be_null_uuidA.isNull() );
319 QVERIFY( !shouldnt_be_null_uuidB.isNull() );
320 QVERIFY( shouldnt_be_null_uuidA != shouldnt_be_null_uuidB );
321}
322
323
324void tst_QUuid::less()
325{
326 QVERIFY( uuidB < uuidA);
327 QVERIFY( uuidB <= uuidA);
328 QVERIFY(!(uuidA < uuidB) );
329 QVERIFY(!(uuidA <= uuidB));
330
331 QUuid null_uuid;
332 QVERIFY(null_uuid < uuidA); // Null uuid is always less than a valid one
333 QVERIFY(null_uuid <= uuidA);
334
335 QVERIFY(null_uuid <= null_uuid);
336 QVERIFY(uuidA <= uuidA);
337}
338
339
340void tst_QUuid::more()
341{
342 QVERIFY( uuidA > uuidB);
343 QVERIFY( uuidA >= uuidB);
344 QVERIFY(!(uuidB > uuidA));
345 QVERIFY(!(uuidB >= uuidA));
346
347 QUuid null_uuid;
348 QVERIFY(!(null_uuid > uuidA)); // Null uuid is always less than a valid one
349 QVERIFY(!(null_uuid >= uuidA));
350
351 QVERIFY(null_uuid >= null_uuid);
352 QVERIFY(uuidA >= uuidA);
353}
354
355
356void tst_QUuid::variants()
357{
358 QVERIFY( uuidA.variant() == QUuid::DCE );
359 QVERIFY( uuidB.variant() == QUuid::DCE );
360
361 QUuid NCS = "{3a2f883c-4000-000d-0000-00fb40000000}";
362 QVERIFY( NCS.variant() == QUuid::NCS );
363}
364
365
366void tst_QUuid::versions()
367{
368 QVERIFY( uuidA.version() == QUuid::Random );
369 QVERIFY( uuidB.version() == QUuid::Random );
370
371 QUuid DCE_time= "{406c45a0-3b7e-11d0-80a3-0000c08810a7}";
372 QVERIFY( DCE_time.version() == QUuid::Time );
373
374 QUuid NCS = "{3a2f883c-4000-000d-0000-00fb40000000}";
375 QVERIFY( NCS.version() == QUuid::VerUnknown );
376}
377
378class UuidThread : public QThread
379{
380public:
381 QUuid uuid;
382
383 void run()
384 {
385 uuid = QUuid::createUuid();
386 }
387};
388
389void tst_QUuid::threadUniqueness()
390{
391 QVector<UuidThread *> threads(qMax(a: 2, b: QThread::idealThreadCount()));
392 for (int i = 0; i < threads.count(); ++i)
393 threads[i] = new UuidThread;
394 for (int i = 0; i < threads.count(); ++i)
395 threads[i]->start();
396 for (int i = 0; i < threads.count(); ++i)
397 QVERIFY(threads[i]->wait(1000));
398 for (int i = 1; i < threads.count(); ++i)
399 QVERIFY(threads[0]->uuid != threads[i]->uuid);
400 qDeleteAll(c: threads);
401}
402
403void tst_QUuid::processUniqueness()
404{
405#if !QT_CONFIG(process)
406 QSKIP("No qprocess support", SkipAll);
407#else
408#ifdef Q_OS_ANDROID
409 QSKIP("This test crashes on Android");
410#endif
411 QProcess process;
412 QString processOneOutput;
413 QString processTwoOutput;
414
415 // Start it once
416#ifdef Q_OS_MAC
417 process.start("testProcessUniqueness/testProcessUniqueness.app");
418#elif defined(Q_OS_ANDROID)
419 process.start("libtestProcessUniqueness.so");
420#else
421 process.start(command: "testProcessUniqueness/testProcessUniqueness");
422#endif
423 QVERIFY(process.waitForFinished());
424 processOneOutput = process.readAllStandardOutput();
425
426 // Start it twice
427#ifdef Q_OS_MAC
428 process.start("testProcessUniqueness/testProcessUniqueness.app");
429#elif defined(Q_OS_ANDROID)
430 process.start("libtestProcessUniqueness.so");
431#else
432 process.start(command: "testProcessUniqueness/testProcessUniqueness");
433#endif
434 QVERIFY(process.waitForFinished());
435 processTwoOutput = process.readAllStandardOutput();
436
437 // They should be *different*!
438 QVERIFY(processOneOutput != processTwoOutput);
439#endif
440}
441
442void tst_QUuid::hash()
443{
444 uint h = qHash(uuid: uuidA);
445 QCOMPARE(qHash(uuidA), h);
446 QCOMPARE(qHash(QUuid(uuidA.toString())), h);
447}
448
449void tst_QUuid::qvariant()
450{
451 QUuid uuid = QUuid::createUuid();
452 QVariant v = QVariant::fromValue(value: uuid);
453 QVERIFY(!v.isNull());
454 QCOMPARE(v.type(), QVariant::Uuid);
455
456 QUuid uuid2 = v.value<QUuid>();
457 QVERIFY(!uuid2.isNull());
458 QCOMPARE(uuid, uuid2);
459}
460
461void tst_QUuid::qvariant_conversion()
462{
463 QUuid uuid = QUuid::createUuid();
464 QVariant v = QVariant::fromValue(value: uuid);
465
466 // QUuid -> QString
467 QVERIFY(v.canConvert<QString>());
468 QCOMPARE(v.toString(), uuid.toString());
469 QCOMPARE(v.value<QString>(), uuid.toString());
470
471 // QUuid -> QByteArray
472 QVERIFY(v.canConvert<QByteArray>());
473 QCOMPARE(v.toByteArray(), uuid.toByteArray());
474 QCOMPARE(v.value<QByteArray>(), uuid.toByteArray());
475
476 QVERIFY(!v.canConvert<int>());
477 QVERIFY(!v.canConvert<QStringList>());
478
479 // try reverse conversion QString -> QUuid
480 QVariant sv = QVariant::fromValue(value: uuid.toString());
481 QCOMPARE(sv.type(), QVariant::String);
482 QVERIFY(sv.canConvert<QUuid>());
483 QCOMPARE(sv.value<QUuid>(), uuid);
484
485 // QString -> QUuid
486 {
487 QVariant sv = QVariant::fromValue(value: uuid.toByteArray());
488 QCOMPARE(sv.type(), QVariant::ByteArray);
489 QVERIFY(sv.canConvert<QUuid>());
490 QCOMPARE(sv.value<QUuid>(), uuid);
491 }
492}
493
494void tst_QUuid::darwinTypes()
495{
496#ifndef Q_OS_DARWIN
497 QSKIP("This is a Darwin-only test");
498#else
499 extern void tst_QUuid_darwinTypes(); // in tst_quuid_darwin.mm
500 tst_QUuid_darwinTypes();
501#endif
502}
503
504QTEST_MAIN(tst_QUuid)
505#include "tst_quuid.moc"
506

source code of qtbase/tests/auto/corelib/plugin/quuid/tst_quuid.cpp