[libsecret] secret-value: allow empty strings



commit ab239c7f6b05dbe9b62afe77225be95eb41c2e5e
Author: Claudio Saavedra <csaavedra igalia com>
Date:   Wed Feb 27 11:11:48 2013 +0200

    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

 libsecret/secret-value.c     |    6 +++---
 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);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]