From c9abf1ff095240a2dcc8c074bb0cc9c0f9240787 Mon Sep 17 00:00:00 2001 From: Stephen Paul Weber Date: Mon, 18 Nov 2024 10:44:12 -0500 Subject: [PATCH] Add initial tests --- tests/test_sasl2.c | 101 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 tests/test_sasl2.c diff --git a/tests/test_sasl2.c b/tests/test_sasl2.c new file mode 100644 index 00000000..adf52a96 --- /dev/null +++ b/tests/test_sasl2.c @@ -0,0 +1,101 @@ +/* SPDX-License-Identifier: MIT OR GPL-3.0-only */ +/* test_sasl.c +** libstrophe XMPP client library -- test routines for the SASL implementation +** +** Copyright (C) 2005-2009 Collecta, Inc. +** +** This software is provided AS-IS with no warranty, either express +** or implied. +** +** This program is dual licensed under the MIT or GPLv3 licenses. +*/ + +#include +#include + +#include "strophe.h" +#include "common.h" +#include "sasl.h" + +static const unsigned char jid[] = "foo@bar.com"; +static const char password[] = "secret"; +static const char response_plain[] = "AGZvb0BiYXIuY29tAHNlY3JldA=="; + +static const char challenge_md5[] = + "cmVhbG09InNvbWVyZWFsbSIsbm9uY2U9Ik9BNk1HOXRFUUdtMmhoIixxb3A9ImF1dGgi" + "LGNoYXJzZXQ9dXRmLTgsYWxnb3JpdGhtPW1kNS1zZXNzCg=="; +static const char response_md5[] = + "dXNlcm5hbWU9InNvbWVub2RlIixyZWFsbT0ic29tZXJlYWxtIixub25jZT0i" + "T0E2TUc5dEVRR20yaGgiLGNub25jZT0iMDBERUFEQkVFRjAwIixuYz0wMDAw" + "MDAwMSxxb3A9YXV0aCxkaWdlc3QtdXJpPSJ4bXBwL3NvbWVyZWFsbSIscmVz" + "cG9uc2U9NGVhNmU4N2JjMDkzMzUwNzQzZGIyOGQ3MDIwOGNhZmIsY2hhcnNl" + "dD11dGYtOA=="; + +int test_plain(xmpp_ctx_t *ctx) +{ + char *result; + + result = sasl_plain(ctx, jid, password); + if (result == NULL) { + /* SASL PLAIN internal failure! */ + return 1; + } + if (strncmp(response_plain, result, strlen(response_plain))) { + /* SASL PLAIN returned incorrect string! */ + return 2; + } + strophe_free(ctx, result); + + return 0; +} + +int test_digest_md5(xmpp_ctx_t *ctx) +{ + char *result; + + result = + sasl_digest_md5(ctx, challenge_md5, "somenode@somerealm", "secret"); + printf("response:\n%s\n", result); + if (strcmp(response_md5, result)) { + /* generated incorrect response to challenge */ + return 1; + } + + return 0; +} + +int main() +{ + xmpp_ctx_t *ctx; + int ret; + + printf("allocating context... "); + ctx = xmpp_ctx_new(NULL, NULL); + if (ctx == NULL) + printf("failed to create context\n"); + if (ctx == NULL) + return -1; + printf("ok.\n"); + + printf("testing SASL PLAIN... "); + ret = test_plain(ctx); + if (ret) + printf("failed!\n"); + if (ret) + return ret; + printf("ok.\n"); + + printf("testing SASL DIGEST-MD5... "); + ret = test_digest_md5(ctx); + if (ret) + printf("failed!\n"); + if (ret) + return ret; + printf("ok.\n"); + + printf("freeing context... "); + xmpp_ctx_free(ctx); + printf("ok.\n"); + + return ret; +}