#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(XmlStream, ReadByOne) { boost::asio::io_context context; bool error{}; boost::asio::co_spawn( context, // NOLINTNEXTLINE: Safe [&] -> boost::asio::awaitable { XmlStream stream{impl::MockSocket{context.get_executor(), 1}}; stream.AddReceivedData(kDoc); try { auto doc = co_await stream.ReadRaw(stream.next_layer()); 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.ReadRaw(stream.next_layer()); 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(XmlStream, ReadAll) { boost::asio::io_context context; bool error{}; boost::asio::co_spawn( context, // NOLINTNEXTLINE: Safe [&] -> boost::asio::awaitable { XmlStream stream{impl::MockSocket{context.get_executor(), kDoc.size()}}; stream.AddReceivedData(kDoc); try { auto doc = co_await stream.ReadRaw(stream.next_layer()); 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.ReadRaw(stream.next_layer()); 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(XmlStream, ReadAllWithEnd) { boost::asio::io_context context; bool error{}; boost::asio::co_spawn( context, // NOLINTNEXTLINE: Safe [&] -> boost::asio::awaitable { XmlStream stream{impl::MockSocket{context.get_executor(), kDoc2.size()}}; stream.AddReceivedData(kDoc2); try { auto doc = co_await stream.ReadRaw(stream.next_layer()); 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.ReadRaw(stream.next_layer()); 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(XmlStream, ReadFeatures) { boost::asio::io_context context; bool error{}; boost::asio::co_spawn( context, // NOLINTNEXTLINE: Safe [&] -> boost::asio::awaitable { XmlStream 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(XmlStream, Write) { boost::asio::io_context context; bool error{}; boost::asio::co_spawn( context, // NOLINTNEXTLINE: Safe [&] -> boost::asio::awaitable { XmlStream 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(XmlStream, ReadOneByOne) { boost::asio::io_context context; bool error{}; boost::asio::co_spawn( context, // NOLINTNEXTLINE: Safe [&] -> boost::asio::awaitable { XmlStream stream{impl::MockSocket{context.get_executor(), 1}}; stream.AddReceivedData(kDoc4); try { ServerToUserStream value = (co_await stream.ReadOne(), co_await stream.ReadOne()); EXPECT_EQ(value.id, "68321991947053239"); EXPECT_EQ(value.version, "1.0"); EXPECT_EQ(value.to, BareJid::Parse("test1@localhost")); EXPECT_EQ(value.from, "localhost"); } catch(const std::exception& err) { SPDLOG_ERROR("{}", err.what()); error = true; } }, boost::asio::detached); context.run(); EXPECT_FALSE(error); } } // namespace larra::xmpp