diff options
-rw-r--r-- | src/plugins/relay/relay-auth.c | 9 | ||||
-rw-r--r-- | src/plugins/relay/relay-auth.h | 2 | ||||
-rw-r--r-- | src/plugins/relay/relay-client.c | 6 | ||||
-rw-r--r-- | tests/unit/plugins/relay/test-relay-auth.cpp | 43 |
4 files changed, 54 insertions, 6 deletions
diff --git a/src/plugins/relay/relay-auth.c b/src/plugins/relay/relay-auth.c index 4ef43db65..047bc2c24 100644 --- a/src/plugins/relay/relay-auth.c +++ b/src/plugins/relay/relay-auth.c @@ -53,6 +53,9 @@ relay_auth_password_hash_algo_search (const char *name) { int i; + if (!name) + return -1; + for (i = 0; i < RELAY_NUM_PASSWORD_HASH_ALGOS; i++) { if (strcmp (relay_auth_password_hash_algo_name[i], name) == 0) @@ -70,12 +73,12 @@ relay_auth_password_hash_algo_search (const char *name) */ char * -relay_auth_generate_nonce () +relay_auth_generate_nonce (int size) { - int size; char *nonce, *nonce_hexa; - size = weechat_config_integer (relay_config_network_nonce_size); + if (size < 1) + return NULL; nonce = malloc (size); if (!nonce) diff --git a/src/plugins/relay/relay-auth.h b/src/plugins/relay/relay-auth.h index dd3286356..171453091 100644 --- a/src/plugins/relay/relay-auth.h +++ b/src/plugins/relay/relay-auth.h @@ -36,7 +36,7 @@ enum t_relay_auth_password_hash_algo extern char *relay_auth_password_hash_algo_name[]; extern int relay_auth_password_hash_algo_search (const char *name); -extern char *relay_auth_generate_nonce (); +extern char *relay_auth_generate_nonce (int size); extern int relay_auth_check_password_plain (const char *password, const char *relay_password); extern int relay_auth_password (struct t_relay_client *client, diff --git a/src/plugins/relay/relay-client.c b/src/plugins/relay/relay-client.c index 65ee42e4d..d22ccd5c1 100644 --- a/src/plugins/relay/relay-client.c +++ b/src/plugins/relay/relay-client.c @@ -1277,7 +1277,8 @@ relay_client_new (int sock, const char *address, struct t_relay_server *server) new_client->protocol = server->protocol; new_client->protocol_string = (server->protocol_string) ? strdup (server->protocol_string) : NULL; new_client->protocol_args = (server->protocol_args) ? strdup (server->protocol_args) : NULL; - new_client->nonce = relay_auth_generate_nonce (); + new_client->nonce = relay_auth_generate_nonce ( + weechat_config_integer (relay_config_network_nonce_size)); plain_text_password = weechat_string_match_list ( relay_auth_password_hash_algo_name[0], (const char **)relay_config_network_password_hash_algo_list, @@ -1486,7 +1487,8 @@ relay_client_new_with_infolist (struct t_infolist *infolist) if (weechat_infolist_search_var (infolist, "nonce")) new_client->nonce = strdup (weechat_infolist_string (infolist, "nonce")); else - new_client->nonce = relay_auth_generate_nonce (); + new_client->nonce = relay_auth_generate_nonce ( + weechat_config_integer (relay_config_network_nonce_size)); /* "password_hash_algo" is new in WeeChat 2.9 */ if (weechat_infolist_search_var (infolist, "password_hash_algo")) new_client->password_hash_algo = weechat_infolist_integer (infolist, "password_hash_algo"); diff --git a/tests/unit/plugins/relay/test-relay-auth.cpp b/tests/unit/plugins/relay/test-relay-auth.cpp index 15dc07e3b..8c51bfe46 100644 --- a/tests/unit/plugins/relay/test-relay-auth.cpp +++ b/tests/unit/plugins/relay/test-relay-auth.cpp @@ -24,6 +24,8 @@ extern "C" { #include <stdio.h> +#include <string.h> +#include <ctype.h> #include "src/plugins/relay/relay-auth.h" } @@ -57,6 +59,47 @@ TEST_GROUP(RelayAuth) /* * Tests functions: + * relay_auth_password_hash_algo_search + */ + +TEST(RelayAuth, PasswordHashAlgoSearch) +{ + LONGS_EQUAL(-1, relay_auth_password_hash_algo_search (NULL)); + LONGS_EQUAL(-1, relay_auth_password_hash_algo_search ("")); + LONGS_EQUAL(-1, relay_auth_password_hash_algo_search ("zzz")); + + LONGS_EQUAL(0, relay_auth_password_hash_algo_search ("plain")); +} + +/* + * Tests functions: + * relay_auth_generate_nonce + */ + +TEST(RelayAuth, GenerateNonce) +{ + char *nonce; + + POINTERS_EQUAL(NULL, relay_auth_generate_nonce (-1)); + POINTERS_EQUAL(NULL, relay_auth_generate_nonce (0)); + + nonce = relay_auth_generate_nonce (1); + LONGS_EQUAL(2, strlen (nonce)); + CHECK(isxdigit ((int)nonce[0])); + CHECK(isxdigit ((int)nonce[1])); + free (nonce); + + nonce = relay_auth_generate_nonce (2); + LONGS_EQUAL(4, strlen (nonce)); + CHECK(isxdigit ((int)nonce[0])); + CHECK(isxdigit ((int)nonce[1])); + CHECK(isxdigit ((int)nonce[2])); + CHECK(isxdigit ((int)nonce[3])); + free (nonce); +} + +/* + * Tests functions: * relay_auth_parse_sha */ |