test_unregistered.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  2. // test_unregistered.cpp
  3. // (C) Copyright 2002 Robert Ramey - http://www.rrsd.com .
  4. // Use, modification and distribution is subject to the Boost Software
  5. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. // should pass compilation and execution
  8. #include <fstream>
  9. #include <cstddef> // NULL
  10. #include <cstdio> // remove
  11. #include <cstring> // strcmp
  12. #include <boost/config.hpp>
  13. #if defined(BOOST_NO_STDC_NAMESPACE)
  14. namespace std{
  15. using ::remove;
  16. }
  17. #endif
  18. #include "test_tools.hpp"
  19. #include <boost/archive/archive_exception.hpp>
  20. #include <boost/serialization/base_object.hpp>
  21. #include <boost/serialization/type_info_implementation.hpp>
  22. #include <boost/core/no_exceptions_support.hpp>
  23. class polymorphic_base
  24. {
  25. friend class boost::serialization::access;
  26. template<class Archive>
  27. void serialize(Archive & /* ar */, const unsigned int /* file_version */){
  28. }
  29. public:
  30. virtual ~polymorphic_base(){};
  31. };
  32. class polymorphic_derived1 : public polymorphic_base
  33. {
  34. friend class boost::serialization::access;
  35. template<class Archive>
  36. void serialize(Archive &ar, const unsigned int /* file_version */){
  37. ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(polymorphic_base);
  38. }
  39. };
  40. class polymorphic_derived2 : public polymorphic_base
  41. {
  42. friend class boost::serialization::access;
  43. template<class Archive>
  44. void serialize(Archive &ar, const unsigned int /* file_version */){
  45. ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(polymorphic_base);
  46. }
  47. };
  48. struct type1 {
  49. template<typename Archive>
  50. void serialize(Archive&, unsigned int ver) {
  51. BOOST_CHECK(ver == 1);
  52. }
  53. };
  54. struct type2 {
  55. template<typename Archive>
  56. void serialize(Archive&, unsigned int ver) {
  57. BOOST_CHECK(ver == 2);
  58. }
  59. };
  60. BOOST_CLASS_VERSION(type1, 1);
  61. BOOST_CLASS_VERSION(type2, 2);
  62. // save unregistered polymorphic classes
  63. void save_unregistered1(const char *testfile)
  64. {
  65. test_ostream os(testfile, TEST_STREAM_FLAGS);
  66. test_oarchive oa(os, TEST_ARCHIVE_FLAGS);
  67. polymorphic_base *rb1 = new polymorphic_derived1;
  68. // registration IS necessary when serializing a polymorphic class
  69. // through pointer to the base class
  70. bool except = false;
  71. BOOST_TRY {
  72. oa << BOOST_SERIALIZATION_NVP(rb1);
  73. }
  74. BOOST_CATCH(boost::archive::archive_exception const& aex){
  75. except = true;
  76. }
  77. BOOST_CATCH_END
  78. BOOST_CHECK_MESSAGE(except, "lack of registration not detected !");
  79. delete rb1;
  80. }
  81. // note: the corresponding save function above will not result in
  82. // valid archive - hence, the following code which attempts to load
  83. // and archive will fail. Leave this as a reminder not to do this
  84. #if 0
  85. // load unregistered polymorphic classes
  86. void load_unregistered1(const char *testfile)
  87. {
  88. std::ifstream is(testfile);
  89. boost::archive::iarchive ia(is);
  90. polymorphic_base *rb1(NULL);
  91. // registration IS necessary when serializing a polymorphic class
  92. // through pointer to the base class
  93. bool except = false;
  94. BOOST_TRY {
  95. ia >> BOOST_SERIALIZATION_NVP(rb1);
  96. }
  97. BOOST_CATCH(boost::archive::archive_exception const& aex){
  98. except = true;
  99. BOOST_CHECK_MESSAGE(
  100. NULL == rb1,
  101. "failed load resulted in a non-null pointer"
  102. );
  103. }
  104. BOOST_CATCH_END
  105. BOOST_CHECK_MESSAGE(except, "lack of registration not detected !");
  106. delete rb1;
  107. }
  108. #endif
  109. // save unregistered polymorphic classes
  110. void save_unregistered2(const char *testfile)
  111. {
  112. test_ostream os(testfile, TEST_STREAM_FLAGS);
  113. test_oarchive oa(os, TEST_ARCHIVE_FLAGS);
  114. polymorphic_derived1 *rd1 = new polymorphic_derived1;
  115. // registration is NOT necessary when serializing a polymorphic class
  116. // through pointer to a derived class
  117. bool except = false;
  118. BOOST_TRY {
  119. oa << BOOST_SERIALIZATION_NVP(rd1);
  120. }
  121. BOOST_CATCH(boost::archive::archive_exception const& aex){
  122. except = true;
  123. }
  124. BOOST_CATCH_END
  125. BOOST_CHECK_MESSAGE(! except, "registration not detected !");
  126. delete rd1;
  127. }
  128. // note: the corresponding save function above will not result in
  129. // valid archive - hence, the following code which attempts to load
  130. // and archive will fail. Leave this as a reminder not to do this
  131. // load unregistered polymorphic classes
  132. void load_unregistered2(const char *testfile)
  133. {
  134. test_istream is(testfile, TEST_STREAM_FLAGS);
  135. test_iarchive ia(is, TEST_ARCHIVE_FLAGS);
  136. polymorphic_derived1 *rd1 = NULL;
  137. // registration is NOT necessary when serializing a polymorphic class
  138. // or through pointer to a derived class
  139. bool except = false;
  140. BOOST_TRY {
  141. ia >> BOOST_SERIALIZATION_NVP(rd1);
  142. }
  143. BOOST_CATCH(boost::archive::archive_exception const& aex){
  144. except = true;
  145. BOOST_CHECK_MESSAGE(
  146. NULL == rd1,
  147. "failed load resulted in a non-null pointer"
  148. );
  149. }
  150. BOOST_CATCH_END
  151. BOOST_CHECK_MESSAGE(! except, "registration not detected !");
  152. delete rd1;
  153. }
  154. // save registered polymorphic class
  155. void save_registered(const char *testfile)
  156. {
  157. test_ostream os(testfile, TEST_STREAM_FLAGS);
  158. test_oarchive oa(os, TEST_ARCHIVE_FLAGS);
  159. polymorphic_base *rb1 = new polymorphic_derived1;
  160. polymorphic_base *rb2 = new polymorphic_derived2;
  161. // registration (forward declaration) will permit correct serialization
  162. // through a pointer to a base class
  163. oa.register_type(static_cast<polymorphic_derived1 *>(NULL));
  164. oa.register_type(static_cast<polymorphic_derived2 *>(NULL));
  165. oa << BOOST_SERIALIZATION_NVP(rb1);
  166. oa << BOOST_SERIALIZATION_NVP(rb2);
  167. delete rb1;
  168. delete rb2;
  169. }
  170. // load registered polymorphic class
  171. void load_registered(const char *testfile)
  172. {
  173. test_istream is(testfile, TEST_STREAM_FLAGS);
  174. test_iarchive ia(is, TEST_ARCHIVE_FLAGS);
  175. polymorphic_base *rb1 = NULL;
  176. polymorphic_base *rb2 = NULL;
  177. // registration (forward declaration) will permit correct serialization
  178. // through a pointer to a base class
  179. ia.register_type(static_cast<polymorphic_derived1 *>(NULL));
  180. ia.register_type(static_cast<polymorphic_derived2 *>(NULL));
  181. ia >> BOOST_SERIALIZATION_NVP(rb1);
  182. BOOST_CHECK_MESSAGE(NULL != rb1, "Load resulted in NULL pointer");
  183. BOOST_CHECK_MESSAGE(
  184. boost::serialization::type_info_implementation<
  185. polymorphic_derived1
  186. >::type::get_const_instance()
  187. ==
  188. * boost::serialization::type_info_implementation<
  189. polymorphic_base
  190. >::type::get_const_instance().get_derived_extended_type_info(*rb1),
  191. "restored pointer b1 not of correct type"
  192. );
  193. ia >> BOOST_SERIALIZATION_NVP(rb2);
  194. BOOST_CHECK_MESSAGE(NULL != rb2, "Load resulted in NULL pointer");
  195. BOOST_CHECK_MESSAGE(
  196. boost::serialization::type_info_implementation<
  197. polymorphic_derived2
  198. >::type::get_const_instance()
  199. ==
  200. * boost::serialization::type_info_implementation<
  201. polymorphic_base
  202. >::type::get_const_instance().get_derived_extended_type_info(*rb2),
  203. "restored pointer b2 not of correct type"
  204. );
  205. delete rb1;
  206. delete rb2;
  207. }
  208. // store a pointer from slot0
  209. void save_unregistered_pointer(const char *testfile)
  210. {
  211. test_ostream os(testfile, TEST_STREAM_FLAGS);
  212. test_oarchive oa(os, TEST_ARCHIVE_FLAGS);
  213. oa.register_type(static_cast<type2 *>(NULL));
  214. type1 instance1;
  215. type2 *pointer2 = new type2;
  216. BOOST_TRY {
  217. oa & BOOST_SERIALIZATION_NVP(instance1) & BOOST_SERIALIZATION_NVP(pointer2);
  218. }
  219. BOOST_CATCH(...) {
  220. BOOST_CHECK_MESSAGE(false, "unexpected exception");
  221. }
  222. BOOST_CATCH_END
  223. delete pointer2;
  224. }
  225. // load a pointer from slot0 which has no pointer serializer
  226. void load_unregistered_pointer(const char *testfile)
  227. {
  228. test_istream is(testfile);
  229. test_iarchive ia(is);
  230. type1 instance1;
  231. type2 *pointer2(NULL);
  232. bool except = false;
  233. BOOST_TRY {
  234. ia & BOOST_SERIALIZATION_NVP(instance1) & BOOST_SERIALIZATION_NVP(pointer2);
  235. }
  236. BOOST_CATCH(boost::archive::archive_exception const& aex){
  237. except = true;
  238. BOOST_CHECK_MESSAGE(
  239. std::strcmp(aex.what(), "unregistered class") == 0,
  240. "incorrect exception"
  241. );
  242. }
  243. BOOST_CATCH_END
  244. BOOST_CHECK_MESSAGE(except, "lack of registration not detected !");
  245. BOOST_CHECK_MESSAGE(NULL == pointer2, "expected failed load");
  246. delete pointer2;
  247. }
  248. int
  249. test_main( int /* argc */, char* /* argv */[] )
  250. {
  251. const char * testfile = boost::archive::tmpnam(NULL);
  252. BOOST_REQUIRE(NULL != testfile);
  253. save_unregistered1(testfile);
  254. // load_unregistered1(testfile);
  255. save_unregistered2(testfile);
  256. load_unregistered2(testfile);
  257. save_registered(testfile);
  258. load_registered(testfile);
  259. save_unregistered_pointer(testfile);
  260. load_unregistered_pointer(testfile);
  261. std::remove(testfile);
  262. return EXIT_SUCCESS;
  263. }
  264. // EOF