changeset 298:a67bc47e6d02

Tests: add test for server-disconnect and cnotice, #559
author David Demelier <markand@malikania.fr>
date Wed, 19 Oct 2016 13:43:29 +0200
parents a76ccf092570
children e60125973197
files libirccd-test/irccd/command-tester.hpp libirccd-test/irccd/server-tester.cpp libirccd-test/irccd/server-tester.hpp libirccd/irccd/cmd-server-cnotice.cpp libirccd/irccd/cmd-server-cnotice.hpp libirccd/irccd/cmd-server-disconnect.cpp libirccd/irccd/cmd-server-disconnect.hpp libirccd/irccd/server.cpp libirccd/irccd/server.hpp tests/CMakeLists.txt tests/cmd-server-cnotice/CMakeLists.txt tests/cmd-server-cnotice/main.cpp tests/cmd-server-disconnect/CMakeLists.txt tests/cmd-server-disconnect/main.cpp
diffstat 14 files changed, 259 insertions(+), 65 deletions(-) [+]
line wrap: on
line diff
--- a/libirccd-test/irccd/command-tester.hpp	Wed Oct 19 13:43:12 2016 +0200
+++ b/libirccd-test/irccd/command-tester.hpp	Wed Oct 19 13:43:29 2016 +0200
@@ -36,7 +36,6 @@
     Irccd m_irccd;
     Irccdctl m_irccdctl;
 
-
 public:
     CommandTester(std::unique_ptr<Command> cmd = nullptr,
                   std::unique_ptr<Server> server = nullptr);
--- a/libirccd-test/irccd/server-tester.cpp	Wed Oct 19 13:43:12 2016 +0200
+++ b/libirccd-test/irccd/server-tester.cpp	Wed Oct 19 13:43:29 2016 +0200
@@ -20,8 +20,8 @@
 
 namespace irccd {
 
-ServerTester::ServerTester()
-    : Server("test")
+ServerTester::ServerTester(std::string name)
+    : Server(std::move(name))
 {
 }
 
--- a/libirccd-test/irccd/server-tester.hpp	Wed Oct 19 13:43:12 2016 +0200
+++ b/libirccd-test/irccd/server-tester.hpp	Wed Oct 19 13:43:29 2016 +0200
@@ -35,9 +35,9 @@
 class IRCCD_EXPORT ServerTester : public Server {
 public:
     /**
-     * Create a server with named 'test'
+     * Create a server with named 'test' by default.
      */
-    ServerTester();
+    ServerTester(std::string name = "test");
 
     /**
      * Overload that is a no-op.
--- a/libirccd/irccd/cmd-server-cnotice.cpp	Wed Oct 19 13:43:12 2016 +0200
+++ b/libirccd/irccd/cmd-server-cnotice.cpp	Wed Oct 19 13:43:29 2016 +0200
@@ -20,44 +20,25 @@
 #include "irccd.hpp"
 #include "server.hpp"
 #include "service-server.hpp"
+#include "transport.hpp"
+#include "util.hpp"
 
 namespace irccd {
 
 namespace command {
 
 ServerChannelNoticeCommand::ServerChannelNoticeCommand()
-    : Command("server-cnotice", "Server", "Send a channel notice")
+    : Command("server-cnotice")
 {
 }
 
-std::vector<Command::Arg> ServerChannelNoticeCommand::args() const
-{
-    return {
-        { "server",     true },
-        { "channel",    true },
-        { "message",    true }
-    };
-}
-
-std::vector<Command::Property> ServerChannelNoticeCommand::properties() const
+void ServerChannelNoticeCommand::exec(Irccd &irccd, TransportClient &client, const nlohmann::json &args)
 {
-    return {
-        { "server",     { nlohmann::json::value_t::string }},
-        { "channel",    { nlohmann::json::value_t::string }},
-        { "message",    { nlohmann::json::value_t::string }}
-    };
-}
-
-nlohmann::json ServerChannelNoticeCommand::exec(Irccd &irccd, const nlohmann::json &request) const
-{
-    Command::exec(irccd, request);
-
-    irccd.servers().require(request["server"].get<std::string>())->cnotice(
-        request["channel"].get<std::string>(),
-        request["message"].get<std::string>()
+    irccd.servers().require(util::json::requireString(args, "server"))->cnotice(
+        util::json::requireString(args, "channel"),
+        util::json::requireString(args, "message")
     );
-
-    return nlohmann::json::object();
+    client.success("server-cnotice");
 }
 
 } // !command
--- a/libirccd/irccd/cmd-server-cnotice.hpp	Wed Oct 19 13:43:12 2016 +0200
+++ b/libirccd/irccd/cmd-server-cnotice.hpp	Wed Oct 19 13:43:29 2016 +0200
@@ -50,19 +50,9 @@
     IRCCD_EXPORT ServerChannelNoticeCommand();
 
     /**
-     * \copydoc Command::args
-     */
-    IRCCD_EXPORT std::vector<Arg> args() const override;
-
-    /**
-     * \copydoc Command::properties
-     */
-    IRCCD_EXPORT std::vector<Property> properties() const override;
-
-    /**
      * \copydoc Command::exec
      */
-    IRCCD_EXPORT nlohmann::json exec(Irccd &irccd, const nlohmann::json &request) const override;
+    IRCCD_EXPORT void exec(Irccd &irccd, TransportClient &client, const nlohmann::json &args) override;
 };
 
 } // !command
--- a/libirccd/irccd/cmd-server-disconnect.cpp	Wed Oct 19 13:43:12 2016 +0200
+++ b/libirccd/irccd/cmd-server-disconnect.cpp	Wed Oct 19 13:43:29 2016 +0200
@@ -20,31 +20,27 @@
 #include "irccd.hpp"
 #include "server.hpp"
 #include "service-server.hpp"
+#include "transport.hpp"
 
 namespace irccd {
 
 namespace command {
 
 ServerDisconnectCommand::ServerDisconnectCommand()
-    : Command("server-disconnect", "Server", "Disconnect one or more servers")
+    : Command("server-disconnect")
 {
 }
 
-std::vector<Command::Arg> ServerDisconnectCommand::args() const
+void ServerDisconnectCommand::exec(Irccd &irccd, TransportClient &client, const nlohmann::json &args)
 {
-    return {{ "server", false }};
-}
+    auto it = args.find("server");
 
-nlohmann::json ServerDisconnectCommand::exec(Irccd &irccd, const nlohmann::json &request) const
-{
-    auto it = request.find("server");
-
-    if (it == request.end())
+    if (it == args.end())
         irccd.servers().clear();
     else
         irccd.servers().remove(*it);
 
-    return Command::exec(irccd, request);
+    client.success("server-disconnect");
 }
 
 } // !command
--- a/libirccd/irccd/cmd-server-disconnect.hpp	Wed Oct 19 13:43:12 2016 +0200
+++ b/libirccd/irccd/cmd-server-disconnect.hpp	Wed Oct 19 13:43:29 2016 +0200
@@ -25,6 +25,7 @@
  */
 
 #include "command.hpp"
+#include "sysconfig.hpp"
 
 namespace irccd {
 
@@ -33,24 +34,17 @@
 /**
  * \brief Implementation of server-disconnect transport command.
  */
-class ServerDisconnectCommand : public Command {
+class IRCCD_EXPORT ServerDisconnectCommand : public Command {
 public:
     /**
      * Constructor.
      */
-    IRCCD_EXPORT ServerDisconnectCommand();
-
-    /**
-     * Get list of arguments required.
-     *
-     * \return the arguments required
-     */
-    IRCCD_EXPORT std::vector<Arg> args() const override;
+    ServerDisconnectCommand();
 
     /**
      * \copydoc Command::exec
      */
-    IRCCD_EXPORT nlohmann::json exec(Irccd &irccd, const nlohmann::json &request) const override;
+    void exec(Irccd &irccd, TransportClient &client, const nlohmann::json &args) override;
 };
 
 } // !command
--- a/libirccd/irccd/server.cpp	Wed Oct 19 13:43:12 2016 +0200
+++ b/libirccd/irccd/server.cpp	Wed Oct 19 13:43:29 2016 +0200
@@ -588,6 +588,11 @@
     m_stateNext = std::move(state);
 }
 
+std::string Server::status() const noexcept
+{
+    return !m_state ? "null" : m_state->ident();
+}
+
 void Server::update() noexcept
 {
     if (m_stateNext) {
--- a/libirccd/irccd/server.hpp	Wed Oct 19 13:43:12 2016 +0200
+++ b/libirccd/irccd/server.hpp	Wed Oct 19 13:43:29 2016 +0200
@@ -797,6 +797,13 @@
     IRCCD_EXPORT void next(std::unique_ptr<State> state) noexcept;
 
     /**
+     * Get the state current id.
+     *
+     * \return the state id
+     */
+    IRCCD_EXPORT std::string status() const noexcept;
+
+    /**
      * Switch to next state if it has.
      */
     IRCCD_EXPORT void update() noexcept;
@@ -816,7 +823,7 @@
      *
      * \warning Not thread-safe
      */
-    IRCCD_EXPORT void prepare(fd_set &setinput, fd_set &setoutput, net::Handle &maxfd) noexcept;
+    IRCCD_EXPORT virtual void prepare(fd_set &setinput, fd_set &setoutput, net::Handle &maxfd) noexcept;
 
     /**
      * Process incoming/outgoing data after selection.
@@ -825,7 +832,7 @@
      * \param setoutput
      * \throw any exception that have been throw from user functions
      */
-    IRCCD_EXPORT void sync(fd_set &setinput, fd_set &setoutput);
+    IRCCD_EXPORT virtual void sync(fd_set &setinput, fd_set &setoutput);
 
     /**
      * Determine if the nickname is the bot itself.
--- a/tests/CMakeLists.txt	Wed Oct 19 13:43:12 2016 +0200
+++ b/tests/CMakeLists.txt	Wed Oct 19 13:43:29 2016 +0200
@@ -21,6 +21,8 @@
 
 if (WITH_TESTS)
     add_subdirectory(cmd-server-cmode)
+    add_subdirectory(cmd-server-cnotice)
+    add_subdirectory(cmd-server-disconnect)
     add_subdirectory(cmd-server-message)
 
 #    # Misc
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/cmd-server-cnotice/CMakeLists.txt	Wed Oct 19 13:43:29 2016 +0200
@@ -0,0 +1,24 @@
+#
+# CMakeLists.txt -- CMake build system for irccd
+#
+# Copyright (c) 2013-2016 David Demelier <markand@malikania.fr>
+#
+# Permission to use, copy, modify, and/or distribute this software for any
+# purpose with or without fee is hereby granted, provided that the above
+# copyright notice and this permission notice appear in all copies.
+#
+# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+#
+
+irccd_define_test(
+    NAME cmd-server-cnotice
+    SOURCES main.cpp
+    LIBRARIES libirccd libirccdctl
+)
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/cmd-server-cnotice/main.cpp	Wed Oct 19 13:43:29 2016 +0200
@@ -0,0 +1,76 @@
+/*
+ * main.cpp -- test server-cnotice remote command
+ *
+ * Copyright (c) 2013-2016 David Demelier <markand@malikania.fr>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <cmd-server-cnotice.hpp>
+#include <command-tester.hpp>
+#include <server-tester.hpp>
+
+using namespace irccd;
+using namespace irccd::command;
+
+namespace {
+
+std::string channel;
+std::string message;
+
+} // !namespace
+
+class ServerChannelNoticeTest : public ServerTester {
+public:
+    virtual void cnotice(std::string channel, std::string message) override
+    {
+        ::channel = channel;
+        ::message = message;
+    }
+};
+
+class ServerChannelNoticeCommandTest : public CommandTester {
+public:
+    ServerChannelNoticeCommandTest()
+        : CommandTester(std::make_unique<ServerChannelNoticeCommand>(),
+                        std::make_unique<ServerChannelNoticeTest>())
+    {
+        m_irccdctl.client().request({
+            { "command",    "server-cnotice"    },
+            { "server",     "test"              },
+            { "channel",    "#staff"            },
+            { "message",    "silence"           }
+        });
+    }
+};
+
+TEST_F(ServerChannelNoticeCommandTest, basic)
+{
+    try {
+        poll([&] () {
+            return !channel.empty() && !message.empty();
+        });
+
+        ASSERT_EQ("#staff", channel);
+        ASSERT_EQ("silence", message);
+    } catch (const std::exception &ex) {
+        FAIL() << ex.what();
+    }
+}
+
+int main(int argc, char **argv)
+{
+    testing::InitGoogleTest(&argc, argv);
+
+    return RUN_ALL_TESTS();
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/cmd-server-disconnect/CMakeLists.txt	Wed Oct 19 13:43:29 2016 +0200
@@ -0,0 +1,24 @@
+#
+# CMakeLists.txt -- CMake build system for irccd
+#
+# Copyright (c) 2013-2016 David Demelier <markand@malikania.fr>
+#
+# Permission to use, copy, modify, and/or distribute this software for any
+# purpose with or without fee is hereby granted, provided that the above
+# copyright notice and this permission notice appear in all copies.
+#
+# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+#
+
+irccd_define_test(
+    NAME cmd-server-disconnect
+    SOURCES main.cpp
+    LIBRARIES libirccd libirccdctl
+)
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/cmd-server-disconnect/main.cpp	Wed Oct 19 13:43:29 2016 +0200
@@ -0,0 +1,96 @@
+/*
+ * main.cpp -- test server-disconnect remote command
+ *
+ * Copyright (c) 2013-2016 David Demelier <markand@malikania.fr>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <cmd-server-disconnect.hpp>
+#include <command-tester.hpp>
+#include <server-tester.hpp>
+#include <service-server.hpp>
+#include <server.hpp>
+
+using namespace irccd;
+using namespace irccd::command;
+
+class ServerDisconnectCommandTest : public CommandTester {
+public:
+    ServerDisconnectCommandTest()
+        : CommandTester(std::make_unique<ServerDisconnectCommand>())
+    {
+    }
+};
+
+TEST_F(ServerDisconnectCommandTest, one)
+{
+    bool response = false;
+
+    try {
+        m_irccd.servers().add(std::make_unique<ServerTester>("s1"));
+        m_irccd.servers().add(std::make_unique<ServerTester>("s2"));
+        m_irccdctl.client().onMessage.connect([&] (const auto &msg) {
+            auto it = msg.find("command");
+
+            if (it != msg.end())
+                response = it->is_string() && *it == "server-disconnect";
+        });
+        m_irccdctl.client().request({
+            { "command",    "server-disconnect" },
+            { "server",     "s1"                }
+        });
+
+        poll([&] () { return response; });
+
+        ASSERT_TRUE(response);
+        ASSERT_FALSE(m_irccd.servers().has("s1"));
+        ASSERT_TRUE(m_irccd.servers().has("s2"));
+    } catch (const std::exception &ex) {
+        FAIL() << ex.what();
+    }
+}
+
+TEST_F(ServerDisconnectCommandTest, all)
+{
+    bool response = false;
+
+    try {
+        m_irccd.servers().add(std::make_unique<ServerTester>("s1"));
+        m_irccd.servers().add(std::make_unique<ServerTester>("s2"));
+        m_irccdctl.client().onMessage.connect([&] (const auto &msg) {
+            auto it = msg.find("command");
+
+            if (it != msg.end())
+                response = it->is_string() && *it == "server-disconnect";
+        });
+        m_irccdctl.client().request({
+            { "command",    "server-disconnect" }
+        });
+
+        poll([&] () { return response; });
+
+        ASSERT_TRUE(response);
+        ASSERT_FALSE(m_irccd.servers().has("s1"));
+        ASSERT_FALSE(m_irccd.servers().has("s2"));
+    } catch (const std::exception &ex) {
+        FAIL() << ex.what();
+    }
+}
+
+int main(int argc, char **argv)
+{
+    testing::InitGoogleTest(&argc, argv);
+
+    return RUN_ALL_TESTS();
+}