mirror of
https://gitlab.gnome.org/GNOME/libsecret.git
synced 2024-12-22 12:48:51 +00:00
f610c44a92
This makes the internal logic of _secret_attributes_validate public, so applications can check and recover when an invalid attributes table is passed to other libsecret API, such as secret_service_clear.
299 lines
8.6 KiB
C
299 lines
8.6 KiB
C
/* libsecret - GLib wrapper for Secret Service
|
|
*
|
|
* 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.
|
|
*
|
|
* Author: Stef Walter <stefw@gnome.org>
|
|
*/
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#undef G_DISABLE_ASSERT
|
|
|
|
#include "secret-attributes.h"
|
|
#include "secret-private.h"
|
|
|
|
#include "egg/egg-testing.h"
|
|
|
|
#include <glib.h>
|
|
|
|
#include <errno.h>
|
|
#include <stdlib.h>
|
|
|
|
static const SecretSchema MOCK_SCHEMA = {
|
|
"org.mock.Schema",
|
|
SECRET_SCHEMA_DONT_MATCH_NAME,
|
|
{
|
|
{ "number", SECRET_SCHEMA_ATTRIBUTE_INTEGER },
|
|
{ "string", SECRET_SCHEMA_ATTRIBUTE_STRING },
|
|
{ "even", SECRET_SCHEMA_ATTRIBUTE_BOOLEAN },
|
|
{ "bad-type", -1 },
|
|
}
|
|
};
|
|
|
|
static void
|
|
test_build (void)
|
|
{
|
|
GHashTable *attributes;
|
|
|
|
attributes = secret_attributes_build (&MOCK_SCHEMA,
|
|
"number", 4,
|
|
"string", "four",
|
|
"even", TRUE,
|
|
NULL);
|
|
|
|
g_assert_cmpstr (g_hash_table_lookup (attributes, "number"), ==, "4");
|
|
g_assert_cmpstr (g_hash_table_lookup (attributes, "string"), ==, "four");
|
|
g_assert_cmpstr (g_hash_table_lookup (attributes, "even"), ==, "true");
|
|
|
|
g_hash_table_unref (attributes);
|
|
}
|
|
|
|
static void
|
|
test_build_unknown (void)
|
|
{
|
|
GHashTable *attributes;
|
|
|
|
if (g_test_subprocess ()) {
|
|
attributes = secret_attributes_build (&MOCK_SCHEMA,
|
|
"invalid", "whee",
|
|
"string", "four",
|
|
"even", TRUE,
|
|
NULL);
|
|
g_assert_null (attributes);
|
|
return;
|
|
}
|
|
|
|
g_test_trap_subprocess ("/attributes/build-unknown", 0, G_TEST_SUBPROCESS_INHERIT_STDOUT);
|
|
g_test_trap_assert_failed ();
|
|
g_test_trap_assert_stderr ("*was not found in*");
|
|
}
|
|
|
|
static void
|
|
test_build_null_string (void)
|
|
{
|
|
GHashTable *attributes;
|
|
|
|
if (g_test_subprocess ()) {
|
|
attributes = secret_attributes_build (&MOCK_SCHEMA,
|
|
"number", 4,
|
|
"string", NULL,
|
|
"even", TRUE,
|
|
NULL);
|
|
g_assert_null (attributes);
|
|
return;
|
|
}
|
|
|
|
g_test_trap_subprocess ("/attributes/build-null-string", 0, G_TEST_SUBPROCESS_INHERIT_STDOUT);
|
|
g_test_trap_assert_failed ();
|
|
g_test_trap_assert_stderr ("*attribute*NULL*");
|
|
}
|
|
|
|
static void
|
|
test_build_non_utf8_string (void)
|
|
{
|
|
GHashTable *attributes;
|
|
|
|
if (g_test_subprocess ()) {
|
|
attributes = secret_attributes_build (&MOCK_SCHEMA,
|
|
"number", 4,
|
|
"string", "\xfftest",
|
|
"even", TRUE,
|
|
NULL);
|
|
g_assert_null (attributes);
|
|
return;
|
|
}
|
|
|
|
g_test_trap_subprocess ("/attributes/build-non-utf8-string", 0, G_TEST_SUBPROCESS_INHERIT_STDOUT);
|
|
g_test_trap_assert_failed ();
|
|
g_test_trap_assert_stderr ("*attribute*UTF-8*");
|
|
}
|
|
|
|
static void
|
|
test_build_bad_type (void)
|
|
{
|
|
GHashTable *attributes;
|
|
|
|
if (g_test_subprocess ()) {
|
|
attributes = secret_attributes_build (&MOCK_SCHEMA,
|
|
"bad-type", "test",
|
|
NULL);
|
|
g_assert_null (attributes);
|
|
return;
|
|
}
|
|
|
|
g_test_trap_subprocess ("/attributes/build-bad-type", 0, G_TEST_SUBPROCESS_INHERIT_STDOUT);
|
|
g_test_trap_assert_failed ();
|
|
g_test_trap_assert_stderr ("*invalid type*");
|
|
}
|
|
|
|
static void
|
|
test_validate_schema (void)
|
|
{
|
|
GHashTable *attributes;
|
|
gboolean ret;
|
|
|
|
attributes = g_hash_table_new (g_str_hash, g_str_equal);
|
|
g_hash_table_replace (attributes, "number", "1");
|
|
g_hash_table_replace (attributes, "string", "test");
|
|
g_hash_table_replace (attributes, "xdg:schema", "org.mock.Schema");
|
|
|
|
ret = _secret_attributes_validate (&MOCK_SCHEMA, attributes, G_STRFUNC, TRUE);
|
|
g_assert_true (ret);
|
|
|
|
g_hash_table_unref (attributes);
|
|
}
|
|
|
|
static void
|
|
test_validate_schema_empty_ok (void)
|
|
{
|
|
GHashTable *attributes;
|
|
gboolean ret;
|
|
|
|
attributes = g_hash_table_new (g_str_hash, g_str_equal);
|
|
|
|
ret = _secret_attributes_validate (&MOCK_SCHEMA, attributes, G_STRFUNC, FALSE);
|
|
g_assert_true (ret);
|
|
|
|
g_hash_table_unref (attributes);
|
|
}
|
|
|
|
static void
|
|
test_validate_schema_bad_empty_not_ok (void)
|
|
{
|
|
GHashTable *attributes;
|
|
gboolean ret;
|
|
|
|
if (g_test_subprocess ()) {
|
|
attributes = g_hash_table_new (g_str_hash, g_str_equal);
|
|
|
|
ret = _secret_attributes_validate (&MOCK_SCHEMA, attributes, G_STRFUNC, TRUE);
|
|
g_assert_false (ret);
|
|
|
|
g_hash_table_unref (attributes);
|
|
return;
|
|
}
|
|
|
|
g_test_trap_subprocess ("/attributes/validate-schema-bad-empty-not-ok", 0, G_TEST_SUBPROCESS_INHERIT_STDOUT);
|
|
g_test_trap_assert_failed ();
|
|
}
|
|
|
|
static void
|
|
test_validate_schema_bad_mismatched_schema (void)
|
|
{
|
|
GHashTable *attributes;
|
|
gboolean ret;
|
|
|
|
if (g_test_subprocess ()) {
|
|
attributes = g_hash_table_new (g_str_hash, g_str_equal);
|
|
g_hash_table_replace (attributes, "number", "1");
|
|
g_hash_table_replace (attributes, "string", "test");
|
|
g_hash_table_replace (attributes, "xdg:schema", "mismatched.Schema");
|
|
|
|
ret = _secret_attributes_validate (&MOCK_SCHEMA, attributes, G_STRFUNC, TRUE);
|
|
g_assert_false (ret);
|
|
|
|
g_hash_table_unref (attributes);
|
|
return;
|
|
}
|
|
|
|
g_test_trap_subprocess ("/attributes/validate-schema-bad-mismatched-schema", 0, G_TEST_SUBPROCESS_INHERIT_STDOUT);
|
|
g_test_trap_assert_failed ();
|
|
}
|
|
|
|
static void
|
|
test_validate_schema_bad_wrong_type (void)
|
|
{
|
|
GHashTable *attributes;
|
|
gboolean ret;
|
|
char non_utf8_string[] = {(char) 128, '\0'};
|
|
|
|
if (g_test_subprocess ()) {
|
|
attributes = g_hash_table_new (g_str_hash, g_str_equal);
|
|
g_hash_table_replace (attributes, "number", "string_in_wrong_place");
|
|
g_hash_table_replace (attributes, "string", non_utf8_string);
|
|
g_hash_table_replace (attributes, "even", "neither_true_nor_false");
|
|
g_hash_table_replace (attributes, "xdg:schema", "org.mock.Schema");
|
|
|
|
ret = _secret_attributes_validate (&MOCK_SCHEMA, attributes, G_STRFUNC, TRUE);
|
|
g_assert_false (ret);
|
|
|
|
g_hash_table_unref (attributes);
|
|
return;
|
|
}
|
|
|
|
g_test_trap_subprocess ("/attributes/validate-schema-bad-wrong-type", 0, G_TEST_SUBPROCESS_INHERIT_STDOUT);
|
|
g_test_trap_assert_failed ();
|
|
}
|
|
|
|
static void
|
|
test_validate_schema_bad_fake_key (void)
|
|
{
|
|
GHashTable *attributes;
|
|
gboolean ret;
|
|
|
|
if (g_test_subprocess ()) {
|
|
attributes = g_hash_table_new (g_str_hash, g_str_equal);
|
|
g_hash_table_replace (attributes, "number", "1");
|
|
g_hash_table_replace (attributes, "string", "test");
|
|
g_hash_table_replace (attributes, "xdg:schema", "org.mock.Schema");
|
|
g_hash_table_replace (attributes, "made_up_key", "not_valid");
|
|
|
|
ret = _secret_attributes_validate (&MOCK_SCHEMA, attributes, G_STRFUNC, TRUE);
|
|
g_assert_false (ret);
|
|
|
|
g_hash_table_unref (attributes);
|
|
return;
|
|
}
|
|
|
|
g_test_trap_subprocess ("/attributes/validate-schema-bad-fake-key", 0, G_TEST_SUBPROCESS_INHERIT_STDOUT);
|
|
g_test_trap_assert_failed ();
|
|
}
|
|
|
|
static void
|
|
test_validate_libgnomekeyring (void)
|
|
{
|
|
GHashTable *attributes;
|
|
gboolean ret;
|
|
|
|
attributes = g_hash_table_new (g_str_hash, g_str_equal);
|
|
g_hash_table_replace (attributes, "number", "1");
|
|
g_hash_table_replace (attributes, "string", "test");
|
|
g_hash_table_replace (attributes, "gkr:compat", "blah-dee-blah");
|
|
|
|
ret = _secret_attributes_validate (&MOCK_SCHEMA, attributes, G_STRFUNC, TRUE);
|
|
g_assert_true (ret);
|
|
|
|
g_hash_table_unref (attributes);
|
|
}
|
|
|
|
int
|
|
main (int argc, char **argv)
|
|
{
|
|
g_test_init (&argc, &argv, NULL);
|
|
g_set_prgname ("test-attributes");
|
|
|
|
g_test_add_func ("/attributes/build", test_build);
|
|
g_test_add_func ("/attributes/build-unknown", test_build_unknown);
|
|
g_test_add_func ("/attributes/build-null-string", test_build_null_string);
|
|
g_test_add_func ("/attributes/build-non-utf8-string", test_build_non_utf8_string);
|
|
g_test_add_func ("/attributes/build-bad-type", test_build_bad_type);
|
|
|
|
g_test_add_func ("/attributes/validate-schema", test_validate_schema);
|
|
g_test_add_func ("/attributes/validate-schema-empty-ok", test_validate_schema_empty_ok);
|
|
g_test_add_func ("/attributes/validate-schema-bad-empty-not-ok", test_validate_schema_bad_empty_not_ok);
|
|
g_test_add_func ("/attributes/validate-schema-bad-mismatched-schema", test_validate_schema_bad_mismatched_schema);
|
|
g_test_add_func ("/attributes/validate-schema-bad-wrong-type", test_validate_schema_bad_wrong_type);
|
|
g_test_add_func ("/attributes/validate-schema-bad-fake-key", test_validate_schema_bad_fake_key);
|
|
g_test_add_func ("/attributes/validate-libgnomekeyring", test_validate_libgnomekeyring);
|
|
|
|
return g_test_run ();
|
|
}
|