test_largeobject.cxx 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #include <iostream>
  2. #include <sstream>
  3. #include <pqxx/largeobject>
  4. #include <pqxx/transaction>
  5. #include "../test_helpers.hxx"
  6. namespace
  7. {
  8. void test_stream_large_object()
  9. {
  10. pqxx::connection conn;
  11. // Construct a really nasty string. (Don't just construct a std::string from
  12. // a char[] constant, because it'll terminate at the embedded zero.)
  13. //
  14. // The crucial thing is the "ff" byte at the beginning. It tests for
  15. // possible conflation between "eof" (-1) and a char which just happens to
  16. // have the same bit pattern as an 8-bit value of -1. This conflation can be
  17. // a problem when it occurs at buffer boundaries.
  18. constexpr char bytes[]{"\xff\0end"};
  19. std::string const contents{bytes, std::size(bytes)};
  20. pqxx::work tx{conn};
  21. #include "pqxx/internal/ignore-deprecated-pre.hxx"
  22. pqxx::largeobject new_obj{tx};
  23. pqxx::olostream write{tx, new_obj};
  24. write << contents;
  25. write.flush();
  26. pqxx::largeobjectaccess check{tx, new_obj, std::ios::in | std::ios::binary};
  27. std::array<char, 50> buf;
  28. std::size_t const len{
  29. static_cast<std::size_t>(check.read(std::data(buf), std::size(buf)))};
  30. PQXX_CHECK_EQUAL(len, std::size(contents), "olostream truncated data.");
  31. std::string const check_str{std::data(buf), len};
  32. PQXX_CHECK_EQUAL(check_str, contents, "olostream mangled data.");
  33. pqxx::ilostream read{tx, new_obj};
  34. std::string read_back;
  35. std::string chunk;
  36. while (read >> chunk) read_back += chunk;
  37. new_obj.remove(tx);
  38. PQXX_CHECK_EQUAL(read_back, contents, "Got wrong data from ilostream.");
  39. PQXX_CHECK_EQUAL(
  40. std::size(read_back), std::size(contents), "ilostream truncated data.");
  41. PQXX_CHECK_EQUAL(
  42. std::size(read_back), std::size(bytes), "ilostream truncated data.");
  43. #include "pqxx/internal/ignore-deprecated-post.hxx"
  44. }
  45. PQXX_REGISTER_TEST(test_stream_large_object);
  46. } // namespace