secret-value: allow empty strings

Passwords and other secrets are allowed to be empty strings, therefore
the check for length != 0 is wrong.

Add tests for empty SecretValue contents.

https://bugzilla.gnome.org/show_bug.cgi?id=694787
This commit is contained in:
Claudio Saavedra 2013-02-27 11:11:48 +02:00
parent b11a7a2f81
commit ab239c7f6b
2 changed files with 26 additions and 3 deletions

View File

@ -97,14 +97,15 @@ secret_value_new (const gchar *secret,
{
gchar *copy;
g_return_val_if_fail (secret == NULL || length != 0, NULL);
g_return_val_if_fail (length == 0 || secret != NULL, NULL);
g_return_val_if_fail (content_type, NULL);
if (length < 0)
length = strlen (secret);
copy = egg_secure_alloc (length + 1);
memcpy (copy, secret, length);
if (secret)
memcpy (copy, secret, length);
copy[length] = 0;
return secret_value_new_full (copy, length, content_type, egg_secure_free);
}
@ -132,7 +133,6 @@ secret_value_new_full (gchar *secret,
{
SecretValue *value;
g_return_val_if_fail (secret == NULL || length != 0, NULL);
g_return_val_if_fail (content_type, NULL);
if (length < 0)

View File

@ -94,6 +94,28 @@ test_new_full_terminated (void)
secret_value_unref (value);
}
static void
test_new_empty (void)
{
SecretValue *value;
const gchar *password;
gsize length;
value = secret_value_new (NULL, 0, "text/plain");
g_assert (value != NULL);
password = secret_value_get (value, &length);
g_assert_cmpuint (length, ==, 0);
g_assert_cmpstr (password, ==, "");
secret_value_unref (value);
value = secret_value_new ("", 0, "text/plain");
g_assert (value != NULL);
password = secret_value_get (value, &length);
g_assert_cmpuint (length, ==, 0);
g_assert_cmpstr (password, ==, "");
secret_value_unref (value);
}
static void
test_ref_unref (void)
{
@ -202,6 +224,7 @@ main (int argc, char **argv)
g_test_add_func ("/value/new-terminated", test_new_terminated);
g_test_add_func ("/value/new-full", test_new_full);
g_test_add_func ("/value/new-full-terminated", test_new_full_terminated);
g_test_add_func ("/value/new-empty", test_new_empty);
g_test_add_func ("/value/ref-unref", test_ref_unref);
g_test_add_func ("/value/boxed", test_boxed);
g_test_add_func ("/value/to-password", test_to_password);