| 1 | // |
| 2 | // UUIDGeneratorTest.cpp |
| 3 | // |
| 4 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
| 5 | // and Contributors. |
| 6 | // |
| 7 | // SPDX-License-Identifier: BSL-1.0 |
| 8 | // |
| 9 | |
| 10 | |
| 11 | #include "UUIDGeneratorTest.h" |
| 12 | #include "Poco/CppUnit/TestCaller.h" |
| 13 | #include "Poco/CppUnit/TestSuite.h" |
| 14 | #include "Poco/UUIDGenerator.h" |
| 15 | #include "Poco/UUID.h" |
| 16 | #include "Poco/SHA1Engine.h" |
| 17 | #include <set> |
| 18 | |
| 19 | |
| 20 | using Poco::UUIDGenerator; |
| 21 | using Poco::UUID; |
| 22 | |
| 23 | |
| 24 | UUIDGeneratorTest::UUIDGeneratorTest(const std::string& rName): CppUnit::TestCase(rName) |
| 25 | { |
| 26 | } |
| 27 | |
| 28 | |
| 29 | UUIDGeneratorTest::~UUIDGeneratorTest() |
| 30 | { |
| 31 | } |
| 32 | |
| 33 | |
| 34 | void UUIDGeneratorTest::testTimeBased() |
| 35 | { |
| 36 | UUIDGenerator& gen = UUIDGenerator::defaultGenerator(); |
| 37 | |
| 38 | std::set<UUID> uuids; |
| 39 | for (int i = 0; i < 1000; ++i) |
| 40 | { |
| 41 | UUID uuid = gen.create(); |
| 42 | assertTrue (uuid.version() == UUID::UUID_TIME_BASED); |
| 43 | assertTrue (uuids.find(uuid) == uuids.end()); |
| 44 | uuids.insert(uuid); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | |
| 49 | void UUIDGeneratorTest::testRandom() |
| 50 | { |
| 51 | UUIDGenerator& gen = UUIDGenerator::defaultGenerator(); |
| 52 | |
| 53 | std::set<UUID> uuids; |
| 54 | for (int i = 0; i < 1000; ++i) |
| 55 | { |
| 56 | UUID uuid = gen.createRandom(); |
| 57 | assertTrue (uuid.version() == UUID::UUID_RANDOM); |
| 58 | assertTrue (uuids.find(uuid) == uuids.end()); |
| 59 | uuids.insert(uuid); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | |
| 64 | void UUIDGeneratorTest::testNameBased() |
| 65 | { |
| 66 | UUIDGenerator& gen = UUIDGenerator::defaultGenerator(); |
| 67 | |
| 68 | UUID uuid1 = gen.createFromName(UUID::uri(), "http://www.appinf.com/uuid" ); |
| 69 | assertTrue (uuid1.version() == UUID::UUID_NAME_BASED); |
| 70 | assertTrue (uuid1.variant() == 2); |
| 71 | |
| 72 | UUID uuid2 = gen.createFromName(UUID::uri(), "http://www.appinf.com/uuid2" ); |
| 73 | assertTrue (uuid2 != uuid1); |
| 74 | |
| 75 | UUID uuid3 = gen.createFromName(UUID::dns(), "www.appinf.com" ); |
| 76 | assertTrue (uuid3 != uuid1); |
| 77 | |
| 78 | UUID uuid4 = gen.createFromName(UUID::oid(), "1.3.6.1.4.1" ); |
| 79 | assertTrue (uuid4 != uuid1); |
| 80 | |
| 81 | UUID uuid5 = gen.createFromName(UUID::x500(), "cn=Guenter Obiltschnig, ou=People, o=Applied Informatics, c=at" ); |
| 82 | assertTrue (uuid5 != uuid1); |
| 83 | |
| 84 | UUID uuid6 = gen.createFromName(UUID::uri(), "http://www.appinf.com/uuid" ); |
| 85 | assertTrue (uuid6 == uuid1); |
| 86 | |
| 87 | Poco::SHA1Engine sha1; |
| 88 | UUID uuid7 = gen.createFromName(UUID::uri(), "http://www.appinf.com/uuid" , sha1); |
| 89 | assertTrue (uuid7.version() == UUID::UUID_NAME_BASED_SHA1); |
| 90 | assertTrue (uuid7.variant() == 2); |
| 91 | assertTrue (uuid7 != uuid1); |
| 92 | } |
| 93 | |
| 94 | |
| 95 | void UUIDGeneratorTest::setUp() |
| 96 | { |
| 97 | } |
| 98 | |
| 99 | |
| 100 | void UUIDGeneratorTest::tearDown() |
| 101 | { |
| 102 | } |
| 103 | |
| 104 | |
| 105 | CppUnit::Test* UUIDGeneratorTest::suite() |
| 106 | { |
| 107 | CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("UUIDGeneratorTest" ); |
| 108 | |
| 109 | CppUnit_addTest(pSuite, UUIDGeneratorTest, testTimeBased); |
| 110 | CppUnit_addTest(pSuite, UUIDGeneratorTest, testRandom); |
| 111 | CppUnit_addTest(pSuite, UUIDGeneratorTest, testNameBased); |
| 112 | |
| 113 | return pSuite; |
| 114 | } |
| 115 | |