From a3924a27a0314abd66adcd567909001ff3ade5e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Helleu?= Date: Sat, 13 Nov 2021 09:06:34 +0100 Subject: [PATCH] tests: add tests on some IRC SASL functions --- doc/en/weechat_dev.en.adoc | 2 +- doc/fr/weechat_dev.fr.adoc | 1 + doc/ja/weechat_dev.ja.adoc | 2 + doc/sr/weechat_dev.sr.adoc | 3 +- src/plugins/irc/irc-sasl.c | 9 ++ tests/CMakeLists.txt | 1 + tests/Makefile.am | 1 + tests/unit/plugins/irc/test-irc-sasl.cpp | 110 +++++++++++++++++++++++ 8 files changed, 127 insertions(+), 2 deletions(-) create mode 100644 tests/unit/plugins/irc/test-irc-sasl.cpp diff --git a/doc/en/weechat_dev.en.adoc b/doc/en/weechat_dev.en.adoc index 2db1d94d8..2f9bfa91a 100644 --- a/doc/en/weechat_dev.en.adoc +++ b/doc/en/weechat_dev.en.adoc @@ -434,6 +434,7 @@ WeeChat "core" is located in following directories: |             test-irc-mode.cpp | Tests: IRC modes. |             test-irc-nick.cpp | Tests: IRC nicks. |             test-irc-protocol.cpp | Tests: IRC protocol. +|             test-irc-sasl.cpp | Tests: SASL authentication with IRC protocol. |             test-irc-server.cpp | Tests: IRC server. |          trigger/ | Root of unit tests for trigger plugin. |             test-trigger.cpp | Tests: triggers. @@ -443,7 +444,6 @@ WeeChat "core" is located in following directories: |             test-typing-status.cpp | Tests: typing status. |          relay/ | Root of unit tests for Relay plugin. |             test-relay-auth.cpp | Tests: clients authentication. - |=== [[documentation_translations]] diff --git a/doc/fr/weechat_dev.fr.adoc b/doc/fr/weechat_dev.fr.adoc index 34ae68cd8..380357def 100644 --- a/doc/fr/weechat_dev.fr.adoc +++ b/doc/fr/weechat_dev.fr.adoc @@ -436,6 +436,7 @@ Le cœur de WeeChat est situé dans les répertoires suivants : |             test-irc-mode.cpp | Tests : modes IRC. |             test-irc-nick.cpp | Tests : pseudos IRC. |             test-irc-protocol.cpp | Tests : protocole IRC. +|             test-irc-sasl.cpp | Tests : authentification SASL avec le protocole IRC. |             test-irc-server.cpp | Tests : serveur IRC. |          trigger/ | Racine des tests unitaires pour l'extension trigger. |             test-trigger.cpp | Tests : triggers. diff --git a/doc/ja/weechat_dev.ja.adoc b/doc/ja/weechat_dev.ja.adoc index 26d7c39ca..5abdad3d9 100644 --- a/doc/ja/weechat_dev.ja.adoc +++ b/doc/ja/weechat_dev.ja.adoc @@ -468,6 +468,8 @@ WeeChat "core" は以下のディレクトリに配置されています: |             test-irc-nick.cpp | Tests: IRC nicks. |             test-irc-protocol.cpp | テスト: IRC プロトコル // TRANSLATION MISSING +|             test-irc-sasl.cpp | Tests: SASL authentication with IRC protocol. +// TRANSLATION MISSING |             test-irc-server.cpp | Tests: IRC server. // TRANSLATION MISSING |          trigger/ | Root of unit tests for trigger plugin. diff --git a/doc/sr/weechat_dev.sr.adoc b/doc/sr/weechat_dev.sr.adoc index c5c0da6c5..ce29351b8 100644 --- a/doc/sr/weechat_dev.sr.adoc +++ b/doc/sr/weechat_dev.sr.adoc @@ -436,6 +436,8 @@ WeeChat „језгро” се налази у следећим директо |             test-irc-mode.cpp | Тестови: IRC режими. |             test-irc-nick.cpp | Тестови: IRC надимци. |             test-irc-protocol.cpp | Тестови: IRC протокол. +// TRANSLATION MISSING +|             test-irc-sasl.cpp | Tests: SASL authentication with IRC protocol. |             test-irc-server.cpp | Тестови: IRC сервер. |          trigger/ | Корен unit тестова за окидач додатак. |             test-trigger.cpp | Тестови: окидачи. @@ -446,7 +448,6 @@ WeeChat „језгро” се налази у следећим директо |             test-typing-status.cpp | Тестови: typing статус. |          relay/ | Корен unit тестова за Релеј додатак. |             test-relay-auth.cpp | Тестови: аутентификација клијената. - |=== [[documentation_translations]] diff --git a/src/plugins/irc/irc-sasl.c b/src/plugins/irc/irc-sasl.c index 3795d3486..e96323e10 100644 --- a/src/plugins/irc/irc-sasl.c +++ b/src/plugins/irc/irc-sasl.c @@ -58,6 +58,9 @@ irc_sasl_mechanism_plain (const char *sasl_username, const char *sasl_password) char *answer_base64, *string; int length_username, length; + if (!sasl_username || !sasl_password) + return NULL; + answer_base64 = NULL; length_username = strlen (sasl_username); length = ((length_username + 1) * 2) + strlen (sasl_password) + 1; @@ -117,6 +120,12 @@ irc_sasl_mechanism_scram (struct t_irc_server *server, int server_key_size, server_signature_size, verifier_size; long number; + if (!server || !hash_algo || !data_base64 || !sasl_username + || !sasl_password) + { + return NULL; + } + answer_base64 = NULL; string = NULL; length = 0; diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index a620dd862..68dd7e036 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -63,6 +63,7 @@ if(ENABLE_IRC) unit/plugins/irc/test-irc-mode.cpp unit/plugins/irc/test-irc-nick.cpp unit/plugins/irc/test-irc-protocol.cpp + unit/plugins/irc/test-irc-sasl.cpp unit/plugins/irc/test-irc-server.cpp unit/plugins/irc/test-irc-tag.cpp ) diff --git a/tests/Makefile.am b/tests/Makefile.am index 82761a498..cdad617f4 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -78,6 +78,7 @@ tests_irc = unit/plugins/irc/test-irc-buffer.cpp \ unit/plugins/irc/test-irc-mode.cpp \ unit/plugins/irc/test-irc-nick.cpp \ unit/plugins/irc/test-irc-protocol.cpp \ + unit/plugins/irc/test-irc-sasl.cpp \ unit/plugins/irc/test-irc-server.cpp \ unit/plugins/irc/test-irc-tag.cpp endif diff --git a/tests/unit/plugins/irc/test-irc-sasl.cpp b/tests/unit/plugins/irc/test-irc-sasl.cpp new file mode 100644 index 000000000..cf5e7e72f --- /dev/null +++ b/tests/unit/plugins/irc/test-irc-sasl.cpp @@ -0,0 +1,110 @@ +/* + * test-irc-sasl.cpp - test IRC SASL functions + * + * Copyright (C) 2021 Sébastien Helleu + * + * 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 . + */ + +#include "CppUTest/TestHarness.h" + +extern "C" +{ +#include +#include "src/core/wee-string.h" +#include "src/plugins/plugin.h" +#include "src/plugins/irc/irc-sasl.h" +#include "src/plugins/irc/irc-server.h" +} + +TEST_GROUP(IrcSasl) +{ +}; + +/* + * Tests functions: + * irc_sasl_mechanism_plain + */ + +TEST(IrcSasl, MechanismPlain) +{ + POINTERS_EQUAL(NULL, irc_sasl_mechanism_plain (NULL, NULL)); + POINTERS_EQUAL(NULL, irc_sasl_mechanism_plain (NULL, "")); + POINTERS_EQUAL(NULL, irc_sasl_mechanism_plain ("", NULL)); + + STRCMP_EQUAL("AAA=", irc_sasl_mechanism_plain ("", "")); + + /* "alice\0alice\0" */ + STRCMP_EQUAL("YWxpY2UAYWxpY2UA", + irc_sasl_mechanism_plain ("alice", "")); + + /* "alice\0alice\0secret" */ + STRCMP_EQUAL("YWxpY2UAYWxpY2UAc2VjcmV0", + irc_sasl_mechanism_plain ("alice", "secret")); + + /* "\0\0secret" */ + STRCMP_EQUAL("AABzZWNyZXQ=", + irc_sasl_mechanism_plain ("", "secret")); +} + +/* + * Tests functions: + * irc_sasl_mechanism_scram + */ + +TEST(IrcSasl, MechanismScram) +{ + struct t_irc_server *server; + char *str, str_decoded[1024], *error; + + POINTERS_EQUAL(NULL, irc_sasl_mechanism_scram (NULL, NULL, NULL, NULL, + NULL, NULL)); + + server = irc_server_alloc ("my_ircd"); + + /* decoded returned value is like: n,,n=user,r=rOprNGfwEbeRWgbNEkqO */ + error = NULL; + str = irc_sasl_mechanism_scram (server, "sha256", "+", + "user1", "secret", &error); + POINTERS_EQUAL(NULL, error); + CHECK(string_base64_decode (str, str_decoded) > 0); + CHECK(strncmp (str_decoded, "n,,n=user1,r=", 13) == 0); + free (str); + + /* TODO: complete tests */ + + irc_server_free (server); +} + +/* + * Tests functions: + * irc_sasl_get_key_content + */ + +TEST(IrcSasl, GetKeyContent) +{ + /* TODO: write tests */ +} + +/* + * Tests functions: + * irc_sasl_mechanism_ecdsa_nist256p_challenge + */ + +TEST(IrcSasl, MechanismEcdsaNist256pChallenge) +{ + /* TODO: write tests */ +}