diff tests/rules/main.cpp @ 0:1158cffe5a5e

Initial import
author David Demelier <markand@malikania.fr>
date Mon, 08 Feb 2016 16:43:14 +0100
parents
children f8160d515a76
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/rules/main.cpp	Mon Feb 08 16:43:14 2016 +0100
@@ -0,0 +1,242 @@
+/*
+ * main.cpp -- test irccd rules
+ *
+ * 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 <gtest/gtest.h>
+
+#include <rule.h>
+
+namespace irccd {
+
+/*
+ * Simulate the following rules configuration:
+ *
+ * #
+ * # On all servers, each channel #staff can't use the onCommand event,
+ * # everything else is allowed.
+ * #
+ * [rule]	#1
+ * servers	= ""
+ * channels	= "#staff"
+ * events	= "onCommand"
+ * action	= drop
+ *
+ * #
+ * # However, the same onCommand on #staff is allowed on server "unsafe"
+ * #
+ * [rule]	#2
+ * servers	= "unsafe"
+ * channels	= "#staff"
+ * events	= "onCommand"
+ * action	= accept
+ *
+ * #
+ * # Plugin game is only allowed on server "malikania" and "localhost",
+ * # channel "#games" and events "onMessage, onCommand".
+ * #
+ * # The first rule #3-1 disable the plugin game for every server, it is
+ * # reenabled again with the #3-2.
+ * #
+ * [rule]	#3-1
+ * plugins	= "game"
+ * action	= drop
+ *
+ * [rule]	#3-2
+ * servers	= "malikania localhost"
+ * channels	= "#games"
+ * plugins	= "game"
+ * events	= "onMessage onCommand"
+ * action	= accept
+ */
+class RulesTest : public testing::Test {
+protected:
+	std::vector<Rule> m_rules;
+
+	RulesTest()
+	{
+		// #1
+		{
+			m_rules.push_back({
+				RuleSet{		},	// Servers
+				RuleSet{ "#staff"	},	// Channels
+				RuleSet{		},	// Origins
+				RuleSet{		},	// Plugins
+				RuleSet{ "onCommand"	},	// Events
+				RuleAction::Drop
+			});
+		}
+
+		// #2
+		{
+			m_rules.push_back({
+				RuleSet{ "unsafe"	},
+				RuleSet{ "#staff"	},
+				RuleSet{		},
+				RuleSet{		},
+				RuleSet{ "onCommand"	},
+				RuleAction::Accept
+			});
+		}
+
+		// #3-1
+		{
+			m_rules.push_back({
+				RuleSet{},
+				RuleSet{},
+				RuleSet{},
+				RuleSet{"game"},
+				RuleSet{},
+				RuleAction::Drop
+			});
+		}
+
+		// #3-2
+		{
+			m_rules.push_back({
+				RuleSet{ "malikania", "localhost"	},
+				RuleSet{ "#games"			},
+				RuleSet{ 				},
+				RuleSet{ "game"				},
+				RuleSet{ "onCommand", "onMessage"	},
+				RuleAction::Accept
+			});
+		}
+	}
+
+	~RulesTest()
+	{
+		m_rules.clear();
+	}
+};
+
+TEST_F(RulesTest, basicMatch1)
+{
+	Rule m;
+
+	/*
+	 * [rule]
+	 */
+	ASSERT_TRUE(m.match("freenode", "#test", "a", "", ""));
+	ASSERT_TRUE(m.match("", "", "", "", ""));
+}
+
+TEST_F(RulesTest, basicMatch2)
+{
+	Rule m(RuleSet{"freenode"});
+
+	/*
+	 * [rule]
+	 * servers	= "freenode"
+	 */
+
+	ASSERT_TRUE(m.match("freenode", "#test", "a", "", ""));
+	ASSERT_FALSE(m.match("malikania", "#test", "a", "", ""));
+	ASSERT_TRUE(m.match("freenode", "", "jean", "", "onMessage"));
+}
+
+TEST_F(RulesTest, basicMatch3)
+{
+	Rule m(RuleSet{"freenode"}, RuleSet{"#staff"});
+
+	/*
+	 * [rule]
+	 * servers	= "freenode"
+	 * channels	= "#staff"
+	 */
+
+	ASSERT_TRUE(m.match("freenode", "#staff", "a", "", ""));
+	ASSERT_FALSE(m.match("freenode", "#test", "a", "", ""));
+	ASSERT_FALSE(m.match("malikania", "#staff", "a", "", ""));
+}
+
+TEST_F(RulesTest, basicMatch4)
+{
+	Rule m(RuleSet{"malikania"}, RuleSet{"#staff"}, RuleSet{"a"});
+
+	/*
+	 * [rule]
+	 * servers	= "malikania"
+	 * channels	= "#staff"
+	 * plugins	= "a"
+	 */
+
+	ASSERT_TRUE(m.match("malikania", "#staff", "a", "",""));
+	ASSERT_FALSE(m.match("malikania", "#staff", "b", "", ""));
+	ASSERT_FALSE(m.match("freenode", "#staff", "a", "", ""));
+}
+
+TEST_F(RulesTest, complexMatch1)
+{
+	Rule m(RuleSet{"malikania", "freenode"});
+
+	/*
+	 * [rule]
+	 * servers	= "malikania freenode"
+	 */
+
+	ASSERT_TRUE(m.match("malikania", "", "", "", ""));
+	ASSERT_TRUE(m.match("freenode", "", "", "", ""));
+	ASSERT_FALSE(m.match("no", "", "", "", ""));
+}
+
+TEST_F(RulesTest, basicSolve)
+{
+	/* Allowed */
+	ASSERT_TRUE(Rule::solve(m_rules, "malikania", "#staff", "", "a", "onMessage"));
+
+	/* Allowed */
+	ASSERT_TRUE(Rule::solve(m_rules, "freenode", "#staff", "", "b", "onTopic"));
+
+	/* Not allowed */
+	ASSERT_FALSE(Rule::solve(m_rules, "malikania", "#staff", "", "", "onCommand"));
+
+	/* Not allowed */
+	ASSERT_FALSE(Rule::solve(m_rules, "freenode", "#staff", "", "c", "onCommand"));
+
+	/* Allowed */
+	ASSERT_TRUE(Rule::solve(m_rules, "unsafe", "#staff", "", "c", "onCommand"));
+}
+
+TEST_F(RulesTest, gamesSolve)
+{
+	/* Allowed */
+	ASSERT_TRUE(Rule::solve(m_rules, "malikania", "#games", "", "game", "onMessage"));
+
+	/* Allowed */
+	ASSERT_TRUE(Rule::solve(m_rules, "localhost", "#games", "", "game", "onMessage"));
+
+	/* Allowed */
+	ASSERT_TRUE(Rule::solve(m_rules, "malikania", "#games", "", "game", "onCommand"));
+
+	/* Not allowed */
+	ASSERT_FALSE(Rule::solve(m_rules, "malikania", "#games", "", "game", "onQuery"));
+
+	/* Not allowed */
+	ASSERT_FALSE(Rule::solve(m_rules, "freenode", "#no", "", "game", "onMessage"));
+
+	/* Not allowed */
+	ASSERT_FALSE(Rule::solve(m_rules, "malikania", "#test", "", "game", "onMessage"));
+}
+
+} // !irccd
+
+int main(int argc, char **argv)
+{
+	testing::InitGoogleTest(&argc, argv);
+
+	return RUN_ALL_TESTS();
+}