Remove useless comparison
[pidgin-git.git] / libpurple / tests / test_jabber_scram.c
blobb81b3db767cd57398e1220d2efcafad140b27eca
1 #include <string.h>
3 #include "tests.h"
4 #include "../util.h"
5 #include "../protocols/jabber/auth_scram.h"
6 #include "../protocols/jabber/jutil.h"
8 static JabberScramHash sha1_mech = { "-SHA-1", "sha1", 20 };
10 #define assert_pbkdf2_equal(password, salt, count, expected) { \
11 GString *p = g_string_new(password); \
12 GString *s = g_string_new(salt); \
13 guchar *result = jabber_scram_hi(&sha1_mech, p, s, count); \
14 fail_if(result == NULL, "Hi() returned NULL"); \
15 fail_if(0 != memcmp(result, expected, 20), "Hi() returned invalid result"); \
16 g_string_free(s, TRUE); \
17 g_string_free(p, TRUE); \
20 START_TEST(test_pbkdf2)
22 assert_pbkdf2_equal("password", "salt", 1, "\x0c\x60\xc8\x0f\x96\x1f\x0e\x71\xf3\xa9\xb5\x24\xaf\x60\x12\x06\x2f\xe0\x37\xa6");
23 assert_pbkdf2_equal("password", "salt", 2, "\xea\x6c\x01\x4d\xc7\x2d\x6f\x8c\xcd\x1e\xd9\x2a\xce\x1d\x41\xf0\xd8\xde\x89\x57");
24 assert_pbkdf2_equal("password", "salt", 4096, "\x4b\x00\x79\x01\xb7\x65\x48\x9a\xbe\xad\x49\xd9\x26\xf7\x21\xd0\x65\xa4\x29\xc1");
26 #if 0
27 /* This causes libcheck to time out :-D */
28 assert_pbkdf2_equal("password", "salt", 16777216, "\xee\xfe\x3d\x61\xcd\x4d\xa4\xe4\xe9\x94\x5b\x3d\x6b\xa2\x15\x8c\x26\x34\xe9\x84");
29 #endif
31 END_TEST
33 START_TEST(test_proofs)
35 JabberScramData *data = g_new0(JabberScramData, 1);
36 gboolean ret;
37 GString *salt;
38 const char *client_proof;
39 /* const char *server_signature; */
41 data->hash = &sha1_mech;
42 data->password = g_strdup("password");
43 data->auth_message = g_string_new("n=username@jabber.org,r=8jLxB5515dhFxBil5A0xSXMH,"
44 "r=8jLxB5515dhFxBil5A0xSXMHabc,s=c2FsdA==,i=1,"
45 "c=biws,r=8jLxB5515dhFxBil5A0xSXMHabc");
46 client_proof = "\x48\x61\x30\xa5\x61\x0b\xae\xb9\xe4\x11\xa8\xfd\xa5\xcd\x34\x1d\x8a\x3c\x28\x17";
48 salt = g_string_new("salt");
49 ret = jabber_scram_calc_proofs(data, salt, 1);
50 fail_if(ret == FALSE, "Failed to calculate SCRAM proofs!");
52 fail_unless(0 == memcmp(client_proof, data->client_proof->str, 20));
53 g_string_free(salt, TRUE);
55 jabber_scram_data_destroy(data);
57 END_TEST
59 #define assert_successful_exchange(pw, nonce, start_data, challenge1, response1, success) { \
60 JabberScramData *data = g_new0(JabberScramData, 1); \
61 gboolean ret; \
62 gchar *out; \
64 data->step = 1; \
65 data->hash = &sha1_mech; \
66 data->password = jabber_saslprep(pw); \
67 fail_if(data->password == NULL); \
68 data->cnonce = g_strdup(nonce); \
69 data->auth_message = g_string_new(start_data); \
71 ret = jabber_scram_feed_parser(data, challenge1, &out); \
72 fail_unless(ret == TRUE); \
73 fail_unless(purple_strequal(out, response1), "Got unexpected response to challenge. Expected %s, got %s", response1, out); \
74 g_free(out); \
76 data->step = 2; \
77 ret = jabber_scram_feed_parser(data, success, &out); \
78 fail_unless(ret == TRUE); \
79 fail_unless(out == NULL); \
81 jabber_scram_data_destroy(data); \
84 START_TEST(test_mech)
86 assert_successful_exchange("password", "H7yDYKAWBCrM2Fa5SxGa4iez",
87 "n=paul,r=H7yDYKAWBCrM2Fa5SxGa4iez",
88 "r=H7yDYKAWBCrM2Fa5SxGa4iezFPVDPpDUcGxPkH3RzP,s=3rXeErP/os7jUNqU,i=4096",
89 "c=biws,r=H7yDYKAWBCrM2Fa5SxGa4iezFPVDPpDUcGxPkH3RzP,p=pXkak78EuwwOEwk2/h/OzD7NkEI=",
90 "v=ldX4EBNnOgDnNTOCmbSfBHAUCOs=");
92 #ifdef USE_IDN
93 assert_successful_exchange("pass½word", "GNb2HsNI7VnTv8ABsE5AnY8W",
94 "n=paul,r=GNb2HsNI7VnTv8ABsE5AnY8W",
95 "r=GNb2HsNI7VnTv8ABsE5AnY8W/w/I3eRKM0I7jxFWOH,s=ysAriUjPzFqOXnMQ,i=4096",
96 "c=biws,r=GNb2HsNI7VnTv8ABsE5AnY8W/w/I3eRKM0I7jxFWOH,p=n/CtgdWjOYnLQ4m9Na+wPn9D2uY=",
97 "v=4TkZwKWy6JHNmrUbU2+IdAaXtos=");
98 #endif
100 END_TEST
102 Suite *
103 jabber_scram_suite(void)
105 Suite *s = suite_create("Jabber SASL SCRAM functions");
107 TCase *tc = tcase_create("PBKDF2 Functionality");
108 tcase_add_test(tc, test_pbkdf2);
109 suite_add_tcase(s, tc);
111 tc = tcase_create("SCRAM Proofs");
112 tcase_add_test(tc, test_proofs);
113 suite_add_tcase(s, tc);
115 tc = tcase_create("SCRAM exchange");
116 tcase_add_test(tc, test_mech);
117 suite_add_tcase(s, tc);
118 return s;