1 // (C) Copyright Andy Tompkins 2010. Permission to copy, use, modify, sell and
2 // distribute this software is granted provided this copyright notice appears
3 // in all copies. This software is provided "as is" without express or implied
4 // warranty, and with no claim as to its suitability for any purpose.
5
6 // Distributed under the Boost Software License, Version 1.0. (See
7 // accompanying file LICENSE_1_0.txt or copy at
8 // https://www.boost.org/LICENSE_1_0.txt)
9
10 // libs/uuid/test/test_name_generator.cpp -------------------------------//
11
12 #include <boost/uuid/uuid.hpp>
13 #include <boost/uuid/uuid_io.hpp>
14 #include <boost/uuid/name_generator.hpp>
15 #include <boost/uuid/name_generator_md5.hpp>
16 #include <boost/uuid/string_generator.hpp>
17 #include <boost/detail/lightweight_test.hpp>
18 #include <boost/config.hpp>
19 #include <boost/predef/library/c/cloudabi.h>
20
main(int,char * [])21 int main(int, char*[])
22 {
23 using namespace boost::uuids;
24
25 // Verify well-known uuid namespaces
26 BOOST_TEST_EQ(ns::dns(),
27 string_generator()("6ba7b810-9dad-11d1-80b4-00c04fd430c8"));
28 BOOST_TEST_EQ(ns::url(),
29 string_generator()("6ba7b811-9dad-11d1-80b4-00c04fd430c8"));
30 BOOST_TEST_EQ(ns::oid(),
31 string_generator()("6ba7b812-9dad-11d1-80b4-00c04fd430c8"));
32 BOOST_TEST_EQ(ns::x500dn(),
33 string_generator()("6ba7b814-9dad-11d1-80b4-00c04fd430c8"));
34
35 uuid correct_sha1 = {{0x21, 0xf7, 0xf8, 0xde, 0x80, 0x51, 0x5b, 0x89, 0x86, 0x80, 0x01, 0x95, 0xef, 0x79, 0x8b, 0x6a}};
36 uuid wcorrect_sha1 = {{0xc3, 0x15, 0x27, 0x0b, 0xa4, 0x66, 0x58, 0x72, 0xac, 0xa4, 0x96, 0x26, 0xce, 0xc0, 0xf4, 0xbe}};
37 #if !defined(BOOST_UUID_COMPAT_PRE_1_71_MD5)
38 // this value is stable across all endian systems
39 uuid correct_md5 = {{0x06, 0x20, 0x5c, 0xec, 0x25, 0x5b, 0x30, 0x0e, 0xa8, 0xbc, 0xa8, 0x60, 0x5a, 0xb8, 0x24, 0x4e}};
40 #else
41 // code before 1.71 had different results depending on the endianness
42 # if BOOST_ENDIAN_LITTLE_BYTE
43 uuid correct_md5 = {{0xec, 0x5c, 0x20, 0x06, 0x0e, 0x00, 0x3b, 0x25, 0xa0, 0xa8, 0xbc, 0xe8, 0x4e, 0x24, 0xb8, 0x5a}};
44 # else
45 uuid correct_md5 = {{0x06, 0x20, 0x5c, 0xec, 0x25, 0x5b, 0x30, 0x0e, 0xa8, 0xbc, 0xa8, 0x60, 0x5a, 0xb8, 0x24, 0x4e}};
46 # endif
47 #endif
48
49
50 name_generator_sha1 gen(ns::dns());
51
52 uuid u = gen("www.widgets.com");
53 BOOST_TEST_EQ(u, correct_sha1);
54 BOOST_TEST_EQ(u.variant(), boost::uuids::uuid::variant_rfc_4122);
55 BOOST_TEST_EQ(u.version(), boost::uuids::uuid::version_name_based_sha1);
56
57 // RFC 4122 Section 4.3 Bullet 1, same name in same namespace makes the same UUID
58 u = gen(std::string("www.widgets.com"));
59 BOOST_TEST_EQ(u, correct_sha1);
60 BOOST_TEST_EQ(u.variant(), boost::uuids::uuid::variant_rfc_4122);
61
62 // RFC 4122 Section 4.3 Bullet 2, two names in the same namespace makes a different UUID
63 uuid u2 = gen("www.wonka.com");
64 BOOST_TEST_NE(u, u2);
65 BOOST_TEST_EQ(u.variant(), boost::uuids::uuid::variant_rfc_4122);
66
67 u = gen(L"www.widgets.com");
68 BOOST_TEST_EQ(u, wcorrect_sha1);
69 BOOST_TEST_EQ(u.variant(), boost::uuids::uuid::variant_rfc_4122);
70
71 #ifndef BOOST_NO_STD_WSTRING
72 u = gen(std::wstring(L"www.widgets.com"));
73 BOOST_TEST_EQ(u, wcorrect_sha1);
74 BOOST_TEST_EQ(u.variant(), boost::uuids::uuid::variant_rfc_4122);
75 #endif
76
77 char name[] = "www.widgets.com";
78 u = gen(name, 15);
79 BOOST_TEST_EQ(u, correct_sha1);
80 BOOST_TEST_EQ(u.variant(), boost::uuids::uuid::variant_rfc_4122);
81
82 // RFC 4122 Section 4.3 Bullet 3, same name in different namespaces makes a different UUID
83 name_generator_sha1 other(ns::url());
84 uuid u3 = other("www.widgets.com");
85 BOOST_TEST_NE(u, u3);
86
87 #if !BOOST_LIB_C_CLOUDABI
88 // used by documentation
89 uuid udoc = gen("boost.org");
90 std::cout << "boost.org uuid in dns namespace: " << udoc << std::endl;
91 // boost.org uuid in dns namespace: 0043f363-bbb4-5369-840a-322df6ec1926
92 #endif
93
94 // deprecated equality check, make sure boost::uuids::name_generator is a sha1 generator
95 name_generator other2(ns::url());
96 uuid u4 = other2("www.widgets.com");
97 BOOST_TEST_EQ(u3, u4);
98
99 // MD5 generator
100 name_generator_md5 mdgen(ns::url());
101 uuid md = mdgen("www.widgets.com");
102 BOOST_TEST_NE(u3, md);
103 BOOST_TEST_EQ(md.variant(), boost::uuids::uuid::variant_rfc_4122);
104 BOOST_TEST_EQ(md.version(), boost::uuids::uuid::version_name_based_md5);
105 BOOST_TEST_EQ(md, correct_md5);
106
107 // latest generator is SHA1 (test will break on change, which is good)
108 name_generator_latest latestgen(ns::dns());
109 uuid latest = latestgen("www.widgets.com");
110 BOOST_TEST_EQ(latest, correct_sha1);
111
112 return boost::report_errors();
113 }
114