diff options
author | Claudio Saavedra <csaavedra@igalia.com> | 2013-02-27 11:11:48 +0200 |
---|---|---|
committer | Claudio Saavedra <csaavedra@igalia.com> | 2013-03-04 09:33:26 +0200 |
commit | ab239c7f6b05dbe9b62afe77225be95eb41c2e5e (patch) | |
tree | bdc726cfb2df9c255aa98158de38293386271378 /libsecret | |
parent | b11a7a2f81ac63a04875fd38b161ea1b6fb3d174 (diff) | |
download | libsecret-ab239c7f6b05dbe9b62afe77225be95eb41c2e5e.tar.gz |
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
Diffstat (limited to 'libsecret')
-rw-r--r-- | libsecret/secret-value.c | 6 | ||||
-rw-r--r-- | libsecret/tests/test-value.c | 23 |
2 files changed, 26 insertions, 3 deletions
diff --git a/libsecret/secret-value.c b/libsecret/secret-value.c index a6fc5b8..cbfdf6b 100644 --- a/libsecret/secret-value.c +++ b/libsecret/secret-value.c @@ -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) diff --git a/libsecret/tests/test-value.c b/libsecret/tests/test-value.c index ceb2448..5578aef 100644 --- a/libsecret/tests/test-value.c +++ b/libsecret/tests/test-value.c @@ -95,6 +95,28 @@ test_new_full_terminated (void) } 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) { SecretValue *value; @@ -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); |