#include #include #include #include #include #include #include #include #include namespace larra::xmpp { constexpr std::string_view kDoc = "\n\n"; constexpr std::string_view kDoc2 = "\n"; constexpr std::string_view kDoc3 = "PLAINSCRAM-SHA-256X-OAUTH2"; constexpr std::string_view kDoc4 = R"()"; TEST(RawXmlStream, ReadByOne) { boost::asio::io_context context; bool error{}; boost::asio::co_spawn( context, // NOLINTNEXTLINE: Safe [&] -> boost::asio::awaitable { RawXmlStream stream{impl::MockSocket{context.get_executor(), 1}}; stream.AddReceivedData(kDoc); try { auto doc = co_await stream.Read(); auto node = doc->get_root_node(); EXPECT_EQ(node->get_name(), std::string_view{"doc"}); EXPECT_FALSE(node->has_child_text()); auto doc2 = co_await stream.Read(); auto node2 = doc2->get_root_node(); EXPECT_EQ(node2->get_name(), std::string_view{"doc2"}); EXPECT_FALSE(node2->has_child_text()); } catch(const std::exception& err) { SPDLOG_ERROR("{}", err.what()); error = true; } }, boost::asio::detached); context.run(); EXPECT_FALSE(error); } TEST(RawXmlStream, ReadAll) { boost::asio::io_context context; bool error{}; boost::asio::co_spawn( context, // NOLINTNEXTLINE: Safe [&] -> boost::asio::awaitable { RawXmlStream stream{impl::MockSocket{context.get_executor(), kDoc.size()}}; stream.AddReceivedData(kDoc); try { auto doc = co_await stream.Read(); auto node = doc->get_root_node(); EXPECT_EQ(node->get_name(), std::string_view{"doc"}); EXPECT_FALSE(node->has_child_text()); auto doc2 = co_await stream.Read(); auto node2 = doc2->get_root_node(); EXPECT_EQ(node2->get_name(), std::string_view{"doc2"}); EXPECT_FALSE(node2->has_child_text()); } catch(const std::exception& err) { SPDLOG_ERROR("{}", err.what()); error = true; } }, boost::asio::detached); context.run(); EXPECT_FALSE(error); } TEST(RawXmlStream, ReadAllWithEnd) { boost::asio::io_context context; bool error{}; boost::asio::co_spawn( context, // NOLINTNEXTLINE: Safe [&] -> boost::asio::awaitable { RawXmlStream stream{impl::MockSocket{context.get_executor(), kDoc2.size()}}; stream.AddReceivedData(kDoc2); try { auto doc = co_await stream.Read(); auto node = doc->get_root_node(); EXPECT_EQ(node->get_name(), std::string_view{"doc"}); EXPECT_FALSE(node->has_child_text()); auto doc2 = co_await stream.Read(); auto node2 = doc2->get_root_node(); EXPECT_EQ(node2->get_name(), std::string_view{"doc2"}); EXPECT_FALSE(node2->has_child_text()); } catch(const std::exception& err) { SPDLOG_ERROR("{}", err.what()); error = true; } }, boost::asio::detached); context.run(); EXPECT_FALSE(error); } TEST(RawXmlStream, ReadFeatures) { boost::asio::io_context context; bool error{}; boost::asio::co_spawn( context, // NOLINTNEXTLINE: Safe [&] -> boost::asio::awaitable { RawXmlStream stream{impl::MockSocket{context.get_executor(), kDoc3.size()}}; stream.AddReceivedData(kDoc3); try { auto features = co_await stream.template Read(); } catch(const std::exception& err) { SPDLOG_ERROR("{}", err.what()); error = true; } }, boost::asio::detached); context.run(); EXPECT_FALSE(error); } struct SomeStruct { static constexpr auto kDefaultName = "some"; static constexpr auto kDefaultNamespace = "namespace"; static constexpr auto kPrefix = "prefix"; friend auto operator<<(xmlpp::Element* node, const SomeStruct&) { node->add_child_text("text"); } }; TEST(RawXmlStream, Write) { boost::asio::io_context context; bool error{}; boost::asio::co_spawn( context, // NOLINTNEXTLINE: Safe [&] -> boost::asio::awaitable { RawXmlStream stream1{impl::MockSocket{context.get_executor()}}; auto stream = std::move(stream1); try { co_await stream.Send(SomeStruct{}); EXPECT_EQ(stream.GetSentData(), std::string_view{"text"}); } catch(const std::exception& err) { SPDLOG_ERROR("{}", err.what()); error = true; } }, boost::asio::detached); context.run(); EXPECT_FALSE(error); } TEST(RawXmlStream, ReadOneByOne) { boost::asio::io_context context; bool error{}; boost::asio::co_spawn( context, // NOLINTNEXTLINE: Safe [&] -> boost::asio::awaitable { RawXmlStream stream{impl::MockSocket{context.get_executor(), 1}}; stream.AddReceivedData(kDoc4); try { auto doc = (co_await stream.ReadOne(), co_await stream.ReadOne()); auto node = doc->get_root_node(); EXPECT_TRUE(node); if(!node) { co_return; } auto stream = ServerToUserStream::Parse(node); } catch(const std::exception& err) { SPDLOG_ERROR("{}", err.what()); error = true; } }, boost::asio::detached); context.run(); EXPECT_FALSE(error); } } // namespace larra::xmpp