Skip to content

Commit 85739c3

Browse files
committed
Make test_sasl work
Signed-off-by: Steffen Jaeckel <s@jaeckel.eu>
1 parent d772a3c commit 85739c3

2 files changed

Lines changed: 86 additions & 8 deletions

File tree

Makefile.am

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ STATIC_TESTS = \
189189
tests/test_hash \
190190
tests/test_jid \
191191
tests/test_ctx \
192+
tests/test_sasl \
192193
tests/test_send_queue \
193194
tests/test_serialize_sm \
194195
tests/test_string \
@@ -265,6 +266,10 @@ tests_test_resolver_LDFLAGS = -static
265266
tests_test_rand_SOURCES = tests/test_rand.c tests/test.c src/sha1.c
266267
tests_test_rand_CFLAGS = $(STROPHE_FLAGS) -I$(top_srcdir)/src
267268

269+
tests_test_sasl_SOURCES = tests/test_sasl.c src/sasl.c \
270+
src/crypto.c src/hash.c src/jid.c src/md5.c src/scram.c src/sha1.c src/sha256.c src/sha512.c src/snprintf.c src/util.c
271+
tests_test_sasl_CFLAGS = -I$(top_srcdir)/src
272+
268273
tests_test_scram_SOURCES = tests/test_scram.c tests/test.c src/sha1.c \
269274
src/sha256.c src/sha512.c
270275
tests_test_scram_CFLAGS = $(STROPHE_FLAGS) -I$(top_srcdir)/src

tests/test_sasl.c

Lines changed: 81 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@
1010
** This program is dual licensed under the MIT or GPLv3 licenses.
1111
*/
1212

13+
#include <assert.h>
1314
#include <stdio.h>
1415
#include <string.h>
1516

17+
#include "test.h"
18+
1619
#include "strophe.h"
1720
#include "common.h"
1821
#include "sasl.h"
@@ -31,7 +34,53 @@ static const char response_md5[] =
3134
"cG9uc2U9NGVhNmU4N2JjMDkzMzUwNzQzZGIyOGQ3MDIwOGNhZmIsY2hhcnNl"
3235
"dD11dGYtOA==";
3336

34-
int test_plain(xmpp_ctx_t *ctx)
37+
/* stubs to build test without whole libstrophe */
38+
void *strophe_alloc(const xmpp_ctx_t *ctx, size_t size)
39+
{
40+
(void)ctx;
41+
return malloc(size);
42+
}
43+
void *strophe_realloc(const xmpp_ctx_t *ctx, void *p, size_t size)
44+
{
45+
(void)ctx;
46+
return realloc(p, size);
47+
}
48+
49+
void strophe_free(const xmpp_ctx_t *ctx, void *p)
50+
{
51+
(void)ctx;
52+
free(p);
53+
}
54+
55+
xmpp_ctx_t *xmpp_ctx_new(const xmpp_mem_t *mem, const xmpp_log_t *log)
56+
{
57+
return calloc(1, sizeof(struct _xmpp_ctx_t));
58+
}
59+
60+
void xmpp_ctx_free(xmpp_ctx_t *ctx)
61+
{
62+
free(ctx);
63+
}
64+
65+
void xmpp_rand_nonce(xmpp_rand_t *rand, char *output, size_t len)
66+
{
67+
const char *nonce = "00DEADBEEF00";
68+
memcpy(output, nonce, len);
69+
}
70+
71+
void xmpp_disconnect(xmpp_conn_t *conn) {}
72+
73+
void strophe_error(const xmpp_ctx_t *ctx,
74+
const char *area,
75+
const char *fmt,
76+
...)
77+
{
78+
(void)ctx;
79+
(void)area;
80+
(void)fmt;
81+
}
82+
83+
static int test_plain(xmpp_ctx_t *ctx)
3584
{
3685
char *result;
3786

@@ -49,19 +98,43 @@ int test_plain(xmpp_ctx_t *ctx)
4998
return 0;
5099
}
51100

52-
int test_digest_md5(xmpp_ctx_t *ctx)
101+
static int test_digest_md5(xmpp_ctx_t *ctx)
53102
{
54-
char *result;
55-
56-
result =
103+
int ret = 0;
104+
char *result =
57105
sasl_digest_md5(ctx, challenge_md5, "somenode@somerealm", "secret");
58-
printf("response:\n%s\n", result);
106+
assert(result != NULL);
59107
if (strcmp(response_md5, result)) {
60108
/* generated incorrect response to challenge */
61-
return 1;
109+
size_t n = 0;
110+
char *should, *is;
111+
printf("\nshould: %s\n", response_md5);
112+
printf(" is: %s\n", result);
113+
printf(" ");
114+
while (response_md5[n] == result[n]) {
115+
putchar(' ');
116+
n++;
117+
}
118+
printf("^\n");
119+
should =
120+
xmpp_base64_decode_str(ctx, response_md5, strlen(response_md5));
121+
is = xmpp_base64_decode_str(ctx, result, strlen(result));
122+
printf("\nshould: %s\n", should);
123+
printf(" is: %s\n", is);
124+
printf(" ");
125+
n = 0;
126+
while (should[n] == is[n]) {
127+
putchar(' ');
128+
n++;
129+
}
130+
strophe_free(ctx, is);
131+
strophe_free(ctx, should);
132+
printf("^\n");
133+
ret = 1;
62134
}
135+
strophe_free(ctx, result);
63136

64-
return 0;
137+
return ret;
65138
}
66139

67140
int main()

0 commit comments

Comments
 (0)