test_name_generator.cpp 4.6 KB

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