Skip to content

Commit c9abf1f

Browse files
committed
Add initial tests
1 parent 9638c29 commit c9abf1f

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed

tests/test_sasl2.c

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/* SPDX-License-Identifier: MIT OR GPL-3.0-only */
2+
/* test_sasl.c
3+
** libstrophe XMPP client library -- test routines for the SASL implementation
4+
**
5+
** Copyright (C) 2005-2009 Collecta, Inc.
6+
**
7+
** This software is provided AS-IS with no warranty, either express
8+
** or implied.
9+
**
10+
** This program is dual licensed under the MIT or GPLv3 licenses.
11+
*/
12+
13+
#include <stdio.h>
14+
#include <string.h>
15+
16+
#include "strophe.h"
17+
#include "common.h"
18+
#include "sasl.h"
19+
20+
static const unsigned char jid[] = "[email protected]";
21+
static const char password[] = "secret";
22+
static const char response_plain[] = "AGZvb0BiYXIuY29tAHNlY3JldA==";
23+
24+
static const char challenge_md5[] =
25+
"cmVhbG09InNvbWVyZWFsbSIsbm9uY2U9Ik9BNk1HOXRFUUdtMmhoIixxb3A9ImF1dGgi"
26+
"LGNoYXJzZXQ9dXRmLTgsYWxnb3JpdGhtPW1kNS1zZXNzCg==";
27+
static const char response_md5[] =
28+
"dXNlcm5hbWU9InNvbWVub2RlIixyZWFsbT0ic29tZXJlYWxtIixub25jZT0i"
29+
"T0E2TUc5dEVRR20yaGgiLGNub25jZT0iMDBERUFEQkVFRjAwIixuYz0wMDAw"
30+
"MDAwMSxxb3A9YXV0aCxkaWdlc3QtdXJpPSJ4bXBwL3NvbWVyZWFsbSIscmVz"
31+
"cG9uc2U9NGVhNmU4N2JjMDkzMzUwNzQzZGIyOGQ3MDIwOGNhZmIsY2hhcnNl"
32+
"dD11dGYtOA==";
33+
34+
int test_plain(xmpp_ctx_t *ctx)
35+
{
36+
char *result;
37+
38+
result = sasl_plain(ctx, jid, password);
39+
if (result == NULL) {
40+
/* SASL PLAIN internal failure! */
41+
return 1;
42+
}
43+
if (strncmp(response_plain, result, strlen(response_plain))) {
44+
/* SASL PLAIN returned incorrect string! */
45+
return 2;
46+
}
47+
strophe_free(ctx, result);
48+
49+
return 0;
50+
}
51+
52+
int test_digest_md5(xmpp_ctx_t *ctx)
53+
{
54+
char *result;
55+
56+
result =
57+
sasl_digest_md5(ctx, challenge_md5, "somenode@somerealm", "secret");
58+
printf("response:\n%s\n", result);
59+
if (strcmp(response_md5, result)) {
60+
/* generated incorrect response to challenge */
61+
return 1;
62+
}
63+
64+
return 0;
65+
}
66+
67+
int main()
68+
{
69+
xmpp_ctx_t *ctx;
70+
int ret;
71+
72+
printf("allocating context... ");
73+
ctx = xmpp_ctx_new(NULL, NULL);
74+
if (ctx == NULL)
75+
printf("failed to create context\n");
76+
if (ctx == NULL)
77+
return -1;
78+
printf("ok.\n");
79+
80+
printf("testing SASL PLAIN... ");
81+
ret = test_plain(ctx);
82+
if (ret)
83+
printf("failed!\n");
84+
if (ret)
85+
return ret;
86+
printf("ok.\n");
87+
88+
printf("testing SASL DIGEST-MD5... ");
89+
ret = test_digest_md5(ctx);
90+
if (ret)
91+
printf("failed!\n");
92+
if (ret)
93+
return ret;
94+
printf("ok.\n");
95+
96+
printf("freeing context... ");
97+
xmpp_ctx_free(ctx);
98+
printf("ok.\n");
99+
100+
return ret;
101+
}

0 commit comments

Comments
 (0)