2012-02-01 12:34:08 +00:00
|
|
|
/* libsecret - GLib wrapper for Secret Service
|
2012-01-23 16:20:18 +00:00
|
|
|
*
|
|
|
|
* Copyright 2012 Red Hat Inc.
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Lesser General Public License as published
|
|
|
|
* by the Free Software Foundation; either version 2 of the licence or (at
|
|
|
|
* your option) any later version.
|
|
|
|
*
|
|
|
|
* See the included COPYING file for more information.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2012-02-01 12:34:08 +00:00
|
|
|
#include "secret-value.h"
|
|
|
|
#include "secret-private.h"
|
2012-01-23 16:20:18 +00:00
|
|
|
|
|
|
|
#include "egg/egg-testing.h"
|
|
|
|
#include "egg/egg-secure-memory.h"
|
|
|
|
|
|
|
|
#include <glib.h>
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
EGG_SECURE_DECLARE (test_value);
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_new (void)
|
|
|
|
{
|
2012-02-01 12:34:08 +00:00
|
|
|
SecretValue *value;
|
2012-01-23 16:20:18 +00:00
|
|
|
gsize length;
|
|
|
|
|
2012-02-01 12:34:08 +00:00
|
|
|
value = secret_value_new ("blahblah", 4, "text/plain");
|
2012-01-23 16:20:18 +00:00
|
|
|
|
2012-02-01 12:34:08 +00:00
|
|
|
g_assert_cmpstr (secret_value_get (value, &length), ==, "blah");
|
2012-01-23 16:20:18 +00:00
|
|
|
g_assert_cmpuint (length, ==, 4);
|
|
|
|
|
2012-02-01 12:34:08 +00:00
|
|
|
g_assert_cmpstr (secret_value_get_content_type (value), ==, "text/plain");
|
2012-01-23 16:20:18 +00:00
|
|
|
|
2012-02-01 12:34:08 +00:00
|
|
|
secret_value_unref (value);
|
2012-01-23 16:20:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_new_terminated (void)
|
|
|
|
{
|
2012-02-01 12:34:08 +00:00
|
|
|
SecretValue *value;
|
2012-01-23 16:20:18 +00:00
|
|
|
gsize length;
|
|
|
|
|
2012-02-01 12:34:08 +00:00
|
|
|
value = secret_value_new ("blah", -1, "text/plain");
|
2012-01-23 16:20:18 +00:00
|
|
|
|
2012-02-01 12:34:08 +00:00
|
|
|
g_assert_cmpstr (secret_value_get (value, &length), ==, "blah");
|
2012-01-23 16:20:18 +00:00
|
|
|
g_assert_cmpuint (length, ==, 4);
|
|
|
|
|
2012-02-01 12:34:08 +00:00
|
|
|
g_assert_cmpstr (secret_value_get_content_type (value), ==, "text/plain");
|
2012-01-23 16:20:18 +00:00
|
|
|
|
2012-02-01 12:34:08 +00:00
|
|
|
secret_value_unref (value);
|
2012-01-23 16:20:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_new_full (void)
|
|
|
|
{
|
2012-02-01 12:34:08 +00:00
|
|
|
SecretValue *value;
|
2012-01-23 16:20:18 +00:00
|
|
|
gchar *data = g_strdup ("blah");
|
|
|
|
gsize length;
|
|
|
|
|
2012-02-01 12:34:08 +00:00
|
|
|
value = secret_value_new_full (data, 4, "text/plain", g_free);
|
2012-01-23 16:20:18 +00:00
|
|
|
|
2012-02-01 12:34:08 +00:00
|
|
|
g_assert_cmpstr (secret_value_get (value, &length), ==, "blah");
|
2012-01-23 16:20:18 +00:00
|
|
|
g_assert_cmpuint (length, ==, 4);
|
|
|
|
|
|
|
|
/* No copy done here */
|
2012-02-01 12:34:08 +00:00
|
|
|
g_assert (secret_value_get (value, NULL) == data);
|
2012-01-23 16:20:18 +00:00
|
|
|
|
2012-02-01 12:34:08 +00:00
|
|
|
secret_value_unref (value);
|
2012-01-23 16:20:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_new_full_terminated (void)
|
|
|
|
{
|
2012-02-01 12:34:08 +00:00
|
|
|
SecretValue *value;
|
2012-01-23 16:20:18 +00:00
|
|
|
gchar *data = g_strdup ("blah");
|
|
|
|
gsize length;
|
|
|
|
|
2012-02-01 12:34:08 +00:00
|
|
|
value = secret_value_new_full (data, -1, "text/plain", g_free);
|
2012-01-23 16:20:18 +00:00
|
|
|
|
2012-02-01 12:34:08 +00:00
|
|
|
g_assert_cmpstr (secret_value_get (value, &length), ==, "blah");
|
2012-01-23 16:20:18 +00:00
|
|
|
g_assert_cmpuint (length, ==, 4);
|
|
|
|
|
|
|
|
/* No copy done here */
|
2012-02-01 12:34:08 +00:00
|
|
|
g_assert (secret_value_get (value, NULL) == data);
|
2012-01-23 16:20:18 +00:00
|
|
|
|
2012-02-01 12:34:08 +00:00
|
|
|
secret_value_unref (value);
|
2012-01-23 16:20:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_ref_unref (void)
|
|
|
|
{
|
2012-02-01 12:34:08 +00:00
|
|
|
SecretValue *value;
|
|
|
|
SecretValue *value2;
|
2012-01-23 16:20:18 +00:00
|
|
|
gsize length;
|
|
|
|
|
2012-02-01 12:34:08 +00:00
|
|
|
value = secret_value_new ("blah", 4, "text/plain");
|
|
|
|
value2 = secret_value_ref(value);
|
|
|
|
secret_value_unref (value);
|
2012-01-23 16:20:18 +00:00
|
|
|
|
2012-02-01 12:34:08 +00:00
|
|
|
g_assert_cmpstr (secret_value_get (value2, &length), ==, "blah");
|
2012-01-23 16:20:18 +00:00
|
|
|
g_assert_cmpuint (length, ==, 4);
|
|
|
|
|
2012-02-01 12:34:08 +00:00
|
|
|
secret_value_unref (value2);
|
2012-01-23 16:20:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_boxed (void)
|
|
|
|
{
|
2012-02-01 12:34:08 +00:00
|
|
|
SecretValue *value;
|
|
|
|
SecretValue *value2;
|
2012-01-23 16:20:18 +00:00
|
|
|
gsize length;
|
|
|
|
|
2012-02-01 12:34:08 +00:00
|
|
|
value = secret_value_new ("blah", 4, "text/plain");
|
|
|
|
value2 = g_boxed_copy (SECRET_TYPE_VALUE, value);
|
|
|
|
g_boxed_free (SECRET_TYPE_VALUE, value);
|
2012-01-23 16:20:18 +00:00
|
|
|
|
2012-02-01 12:34:08 +00:00
|
|
|
g_assert_cmpstr (secret_value_get (value2, &length), ==, "blah");
|
2012-01-23 16:20:18 +00:00
|
|
|
g_assert_cmpuint (length, ==, 4);
|
|
|
|
|
2012-02-01 12:34:08 +00:00
|
|
|
g_boxed_free (SECRET_TYPE_VALUE, value2);
|
2012-01-23 16:20:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_to_password (void)
|
|
|
|
{
|
2012-02-01 12:34:08 +00:00
|
|
|
SecretValue *value;
|
2012-01-23 16:20:18 +00:00
|
|
|
gchar *password;
|
|
|
|
|
2012-02-01 12:34:08 +00:00
|
|
|
value = secret_value_new_full (egg_secure_strdup ("blah"), -1,
|
2012-01-23 16:20:18 +00:00
|
|
|
"text/plain", egg_secure_free);
|
|
|
|
|
2012-02-01 12:34:08 +00:00
|
|
|
password = _secret_value_unref_to_password (value);
|
2012-01-23 16:20:18 +00:00
|
|
|
g_assert_cmpstr (password, ==, "blah");
|
|
|
|
|
|
|
|
egg_secure_free (password);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_to_password_bad_destroy (void)
|
|
|
|
{
|
2012-02-01 12:34:08 +00:00
|
|
|
SecretValue *value;
|
2012-01-23 16:20:18 +00:00
|
|
|
gchar *password;
|
|
|
|
|
2012-02-01 12:34:08 +00:00
|
|
|
value = secret_value_new_full (g_strdup ("blah"), -1,
|
2012-01-23 16:20:18 +00:00
|
|
|
"text/plain", g_free);
|
|
|
|
|
2012-02-01 12:34:08 +00:00
|
|
|
password = _secret_value_unref_to_password (value);
|
2012-01-23 16:20:18 +00:00
|
|
|
g_assert_cmpstr (password, ==, "blah");
|
|
|
|
|
|
|
|
egg_secure_free (password);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_to_password_bad_content (void)
|
|
|
|
{
|
2012-02-01 12:34:08 +00:00
|
|
|
SecretValue *value;
|
2012-01-23 16:20:18 +00:00
|
|
|
gchar *password;
|
|
|
|
|
2012-02-01 12:34:08 +00:00
|
|
|
value = secret_value_new_full (g_strdup ("wooowhee"), -1,
|
2012-01-23 16:20:18 +00:00
|
|
|
"application/octet-stream", g_free);
|
|
|
|
|
2012-02-01 12:34:08 +00:00
|
|
|
password = _secret_value_unref_to_password (value);
|
2012-01-23 16:20:18 +00:00
|
|
|
g_assert_cmpstr (password, ==, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_to_password_extra_ref (void)
|
|
|
|
{
|
2012-02-01 12:34:08 +00:00
|
|
|
SecretValue *value;
|
2012-01-23 16:20:18 +00:00
|
|
|
gchar *password;
|
|
|
|
|
2012-02-01 12:34:08 +00:00
|
|
|
value = secret_value_new_full (egg_secure_strdup ("blah"), -1,
|
2012-01-23 16:20:18 +00:00
|
|
|
"text/plain", egg_secure_free);
|
2012-02-01 12:34:08 +00:00
|
|
|
secret_value_ref (value);
|
2012-01-23 16:20:18 +00:00
|
|
|
|
2012-02-01 12:34:08 +00:00
|
|
|
password = _secret_value_unref_to_password (value);
|
2012-01-23 16:20:18 +00:00
|
|
|
g_assert_cmpstr (password, ==, "blah");
|
|
|
|
|
|
|
|
egg_secure_free (password);
|
2012-02-01 12:34:08 +00:00
|
|
|
secret_value_unref (value);
|
2012-01-23 16:20:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main (int argc, char **argv)
|
|
|
|
{
|
|
|
|
g_test_init (&argc, &argv, NULL);
|
|
|
|
g_set_prgname ("test-value");
|
|
|
|
g_type_init ();
|
|
|
|
|
|
|
|
g_test_add_func ("/value/new", test_new);
|
|
|
|
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/ref-unref", test_ref_unref);
|
|
|
|
g_test_add_func ("/value/boxed", test_boxed);
|
|
|
|
g_test_add_func ("/value/to-password", test_to_password);
|
|
|
|
g_test_add_func ("/value/to-password-bad-destroy", test_to_password_bad_destroy);
|
|
|
|
g_test_add_func ("/value/to-password-bad-content", test_to_password_bad_content);
|
|
|
|
g_test_add_func ("/value/to-password-extra-ref", test_to_password_extra_ref);
|
|
|
|
|
|
|
|
return egg_tests_run_with_loop ();
|
|
|
|
}
|