summaryrefslogtreecommitdiff
path: root/tests/unit
diff options
context:
space:
mode:
authorSébastien Helleu <flashcode@flashtux.org>2021-06-21 21:26:45 +0200
committerSébastien Helleu <flashcode@flashtux.org>2021-06-24 20:59:21 +0200
commit23c46c3f2bfa735d30e815b9ff45c8008adbcbc5 (patch)
tree48e7f338dd1ceb40df928b3558110ab25512c394 /tests/unit
parentb3b4ef648b0a858c4183dba28071b2c84ef31a7c (diff)
downloadweechat-23c46c3f2bfa735d30e815b9ff45c8008adbcbc5.zip
irc: escape/unescape IRC message tags values (issue #1654)
Spec: https://ircv3.net/specs/extensions/message-tags#escaping-values
Diffstat (limited to 'tests/unit')
-rw-r--r--tests/unit/plugins/irc/test-irc-protocol.cpp34
-rw-r--r--tests/unit/plugins/irc/test-irc-tag.cpp147
2 files changed, 147 insertions, 34 deletions
diff --git a/tests/unit/plugins/irc/test-irc-protocol.cpp b/tests/unit/plugins/irc/test-irc-protocol.cpp
index eff9e3346..04eaf6b4b 100644
--- a/tests/unit/plugins/irc/test-irc-protocol.cpp
+++ b/tests/unit/plugins/irc/test-irc-protocol.cpp
@@ -42,7 +42,6 @@ extern const char *irc_protocol_nick_address (struct t_irc_server *server,
struct t_irc_nick *nick,
const char *nickname,
const char *address);
-extern struct t_hashtable *irc_protocol_get_message_tags (const char *tags);
extern char *irc_protocol_cap_to_enable (const char *capabilities,
int sasl_requested);
}
@@ -171,39 +170,6 @@ TEST(IrcProtocol, Tags)
/*
* Tests functions:
- * irc_protocol_get_message_tags
- */
-
-TEST(IrcProtocol, GetMessageTags)
-{
- struct t_hashtable *hashtable;
-
- POINTERS_EQUAL(NULL, irc_protocol_get_message_tags (NULL));
- POINTERS_EQUAL(NULL, irc_protocol_get_message_tags (""));
-
- hashtable = irc_protocol_get_message_tags ("abc");
- CHECK(hashtable);
- LONGS_EQUAL(1, hashtable->items_count);
- POINTERS_EQUAL(NULL, (const char *)hashtable_get (hashtable, "abc"));
- hashtable_free (hashtable);
-
- hashtable = irc_protocol_get_message_tags ("abc=def");
- CHECK(hashtable);
- LONGS_EQUAL(1, hashtable->items_count);
- STRCMP_EQUAL("def", (const char *)hashtable_get (hashtable, "abc"));
- hashtable_free (hashtable);
-
- hashtable = irc_protocol_get_message_tags ("aaa=bbb;ccc;example.com/ddd=eee");
- CHECK(hashtable);
- LONGS_EQUAL(3, hashtable->items_count);
- STRCMP_EQUAL("bbb", (const char *)hashtable_get (hashtable, "aaa"));
- POINTERS_EQUAL(NULL, (const char *)hashtable_get (hashtable, "ccc"));
- STRCMP_EQUAL("eee", (const char *)hashtable_get (hashtable, "example.com/ddd"));
- hashtable_free (hashtable);
-}
-
-/*
- * Tests functions:
* irc_protocol_parse_time
*/
diff --git a/tests/unit/plugins/irc/test-irc-tag.cpp b/tests/unit/plugins/irc/test-irc-tag.cpp
new file mode 100644
index 000000000..50796ac37
--- /dev/null
+++ b/tests/unit/plugins/irc/test-irc-tag.cpp
@@ -0,0 +1,147 @@
+/*
+ * test-irc-tag.cpp - test IRC message tags functions
+ *
+ * Copyright (C) 2021 Sébastien Helleu <flashcode@flashtux.org>
+ *
+ * This file is part of WeeChat, the extensible chat client.
+ *
+ * WeeChat is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * WeeChat is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with WeeChat. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "CppUTest/TestHarness.h"
+
+extern "C"
+{
+#include <stdio.h>
+#include "src/core/wee-hashtable.h"
+#include "src/core/wee-hook.h"
+#include "src/plugins/irc/irc-tag.h"
+}
+
+#define WEE_CHECK_ESCAPE_VALUE(__result, __string) \
+ escaped = irc_tag_escape_value (__string); \
+ STRCMP_EQUAL(__result, escaped); \
+ free (escaped);
+
+#define WEE_CHECK_UNESCAPE_VALUE(__result, __string) \
+ unescaped = irc_tag_unescape_value (__string); \
+ STRCMP_EQUAL(__result, unescaped); \
+ free (unescaped);
+
+TEST_GROUP(IrcTag)
+{
+};
+
+/*
+ * Tests functions:
+ * irc_tag_escape_value
+ */
+
+TEST(IrcTag, EscapeValue)
+{
+ char *escaped;
+
+ /* NULL/empty string */
+ POINTERS_EQUAL(NULL, irc_tag_escape_value (NULL));
+ WEE_CHECK_ESCAPE_VALUE("", "");
+
+ WEE_CHECK_ESCAPE_VALUE("test", "test");
+ WEE_CHECK_ESCAPE_VALUE("test\\:abc", "test;abc");
+ WEE_CHECK_ESCAPE_VALUE("test\\sabc", "test abc");
+ WEE_CHECK_ESCAPE_VALUE("test_\\\\_abc", "test_\\_abc");
+ WEE_CHECK_ESCAPE_VALUE("test_\\r_abc", "test_\r_abc");
+ WEE_CHECK_ESCAPE_VALUE("test_\\n_abc", "test_\n_abc");
+ WEE_CHECK_ESCAPE_VALUE("test_\xf0\xa4\xad\xa2_abc",
+ "test_\xf0\xa4\xad\xa2_abc");
+ WEE_CHECK_ESCAPE_VALUE("\\:\\s\\\\\\r\\n", "; \\\r\n");
+}
+
+/*
+ * Tests functions:
+ * irc_tag_unescape_value
+ */
+
+TEST(IrcTag, UnescapeValue)
+{
+ char *unescaped;
+
+ /* NULL/empty string */
+ POINTERS_EQUAL(NULL, irc_tag_unescape_value (NULL));
+ WEE_CHECK_UNESCAPE_VALUE("", "");
+
+ WEE_CHECK_UNESCAPE_VALUE("test", "test");
+ WEE_CHECK_UNESCAPE_VALUE("test", "test\\");
+ WEE_CHECK_UNESCAPE_VALUE("test;abc", "test\\:abc");
+ WEE_CHECK_UNESCAPE_VALUE("test abc", "test\\sabc");
+ WEE_CHECK_UNESCAPE_VALUE("test_\\_abc", "test_\\\\_abc");
+ WEE_CHECK_UNESCAPE_VALUE("test_\r_abc", "test_\\r_abc");
+ WEE_CHECK_UNESCAPE_VALUE("test_\n_abc", "test_\\n_abc");
+ WEE_CHECK_UNESCAPE_VALUE("test_a_abc", "test_\\a_abc");
+ WEE_CHECK_UNESCAPE_VALUE("test_\xf0\xa4\xad\xa2_abc",
+ "test_\\\xf0\xa4\xad\xa2_abc");
+ WEE_CHECK_UNESCAPE_VALUE("; \\\r\n", "\\:\\s\\\\\\r\\n");
+}
+
+/*
+ * Tests functions:
+ * irc_tag_modifier_cb
+ */
+
+TEST(IrcTag, ModifierCallback)
+{
+ char *result;
+
+ /* modifier "irc_tag_escape_value" */
+ result = hook_modifier_exec (NULL, "irc_tag_escape_value", NULL, "test");
+ STRCMP_EQUAL("test", result);
+ free (result);
+
+ /* modifier "irc_tag_unescape_value" */
+ result = hook_modifier_exec (NULL, "irc_tag_unescape_value", NULL, "test");
+ STRCMP_EQUAL("test", result);
+ free (result);
+}
+
+/*
+ * Tests functions:
+ * irc_tag_parse
+ */
+
+TEST(IrcTag, Parse)
+{
+ struct t_hashtable *hashtable;
+
+ POINTERS_EQUAL(NULL, irc_tag_parse (NULL));
+ POINTERS_EQUAL(NULL, irc_tag_parse (""));
+
+ hashtable = irc_tag_parse ("abc");
+ CHECK(hashtable);
+ LONGS_EQUAL(1, hashtable->items_count);
+ POINTERS_EQUAL(NULL, (const char *)hashtable_get (hashtable, "abc"));
+ hashtable_free (hashtable);
+
+ hashtable = irc_tag_parse ("abc=def");
+ CHECK(hashtable);
+ LONGS_EQUAL(1, hashtable->items_count);
+ STRCMP_EQUAL("def", (const char *)hashtable_get (hashtable, "abc"));
+ hashtable_free (hashtable);
+
+ hashtable = irc_tag_parse ("aaa=bbb;ccc;example.com/ddd=eee");
+ CHECK(hashtable);
+ LONGS_EQUAL(3, hashtable->items_count);
+ STRCMP_EQUAL("bbb", (const char *)hashtable_get (hashtable, "aaa"));
+ POINTERS_EQUAL(NULL, (const char *)hashtable_get (hashtable, "ccc"));
+ STRCMP_EQUAL("eee", (const char *)hashtable_get (hashtable, "example.com/ddd"));
+ hashtable_free (hashtable);
+}