~ubuntu-branches/ubuntu/quantal/libsecret/quantal-proposed

« back to all changes in this revision

Viewing changes to libsecret/tests/test-attributes.c

  • Committer: Package Import Robot
  • Author(s): Marc Deslauriers
  • Date: 2012-10-31 10:18:13 UTC
  • mfrom: (1.1.4)
  • Revision ID: package-import@ubuntu.com-20121031101813-eiwj20mut8suteyg
Tags: 0.11-0ubuntu0.12.10.1
New upstream release to fix segfault in vinagre from using an invalid
attribute. (LP: #1071055)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* libsecret - GLib wrapper for Secret Service
 
2
 *
 
3
 * Copyright 2012 Red Hat Inc.
 
4
 *
 
5
 * This program is free software: you can redistribute it and/or modify
 
6
 * it under the terms of the GNU Lesser General Public License as published
 
7
 * by the Free Software Foundation; either version 2 of the licence or (at
 
8
 * your option) any later version.
 
9
 *
 
10
 * See the included COPYING file for more information.
 
11
 *
 
12
 * Author: Stef Walter <stefw@gnome.org>
 
13
 */
 
14
 
 
15
 
 
16
#include "config.h"
 
17
 
 
18
#include "secret-attributes.h"
 
19
 
 
20
#include "egg/egg-testing.h"
 
21
 
 
22
#include <glib.h>
 
23
 
 
24
#include <errno.h>
 
25
#include <stdlib.h>
 
26
 
 
27
static const SecretSchema MOCK_SCHEMA = {
 
28
        "org.mock.Schema",
 
29
        SECRET_SCHEMA_NONE,
 
30
        {
 
31
                { "number", SECRET_SCHEMA_ATTRIBUTE_INTEGER },
 
32
                { "string", SECRET_SCHEMA_ATTRIBUTE_STRING },
 
33
                { "even", SECRET_SCHEMA_ATTRIBUTE_BOOLEAN },
 
34
                { "bad-type", -1 },
 
35
        }
 
36
};
 
37
 
 
38
static void
 
39
test_build (void)
 
40
{
 
41
        GHashTable *attributes;
 
42
 
 
43
        attributes = secret_attributes_build (&MOCK_SCHEMA,
 
44
                                              "number", 4,
 
45
                                              "string", "four",
 
46
                                              "even", TRUE,
 
47
                                              NULL);
 
48
 
 
49
        g_assert_cmpstr (g_hash_table_lookup (attributes, "number"), ==, "4");
 
50
        g_assert_cmpstr (g_hash_table_lookup (attributes, "string"), ==, "four");
 
51
        g_assert_cmpstr (g_hash_table_lookup (attributes, "even"), ==, "true");
 
52
 
 
53
        g_hash_table_unref (attributes);
 
54
}
 
55
 
 
56
static void
 
57
test_build_unknown (void)
 
58
{
 
59
        GHashTable *attributes;
 
60
 
 
61
        if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR)) {
 
62
                attributes = secret_attributes_build (&MOCK_SCHEMA,
 
63
                                                      "invalid", "whee",
 
64
                                                      "string", "four",
 
65
                                                      "even", TRUE,
 
66
                                                      NULL);
 
67
                g_assert (attributes == NULL);
 
68
        }
 
69
 
 
70
        g_test_trap_assert_failed ();
 
71
        g_test_trap_assert_stderr ("*was not found in*");
 
72
}
 
73
 
 
74
static void
 
75
test_build_null_string (void)
 
76
{
 
77
        GHashTable *attributes;
 
78
 
 
79
        if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR)) {
 
80
                attributes = secret_attributes_build (&MOCK_SCHEMA,
 
81
                                                      "number", 4,
 
82
                                                      "string", NULL,
 
83
                                                      "even", TRUE,
 
84
                                                      NULL);
 
85
                g_assert (attributes == NULL);
 
86
        }
 
87
 
 
88
        g_test_trap_assert_failed ();
 
89
        g_test_trap_assert_stderr ("*attribute*NULL*");
 
90
}
 
91
 
 
92
static void
 
93
test_build_non_utf8_string (void)
 
94
{
 
95
        GHashTable *attributes;
 
96
 
 
97
        if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR)) {
 
98
                attributes = secret_attributes_build (&MOCK_SCHEMA,
 
99
                                                      "number", 4,
 
100
                                                      "string", "\xfftest",
 
101
                                                      "even", TRUE,
 
102
                                                      NULL);
 
103
                g_assert (attributes == NULL);
 
104
        }
 
105
 
 
106
        g_test_trap_assert_failed ();
 
107
        g_test_trap_assert_stderr ("*attribute*UTF-8*");
 
108
}
 
109
 
 
110
static void
 
111
test_build_bad_type (void)
 
112
{
 
113
        GHashTable *attributes;
 
114
 
 
115
        if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR)) {
 
116
                attributes = secret_attributes_build (&MOCK_SCHEMA,
 
117
                                                      "bad-type", "test",
 
118
                                                      NULL);
 
119
                g_assert (attributes == NULL);
 
120
        }
 
121
 
 
122
        g_test_trap_assert_failed ();
 
123
        g_test_trap_assert_stderr ("*invalid type*");
 
124
}
 
125
 
 
126
int
 
127
main (int argc, char **argv)
 
128
{
 
129
        g_test_init (&argc, &argv, NULL);
 
130
        g_set_prgname ("test-attributes");
 
131
        g_type_init ();
 
132
 
 
133
        g_test_add_func ("/attributes/build", test_build);
 
134
        g_test_add_func ("/attributes/build-unknown", test_build_unknown);
 
135
        g_test_add_func ("/attributes/build-null-string", test_build_null_string);
 
136
        g_test_add_func ("/attributes/build-non-utf8-string", test_build_non_utf8_string);
 
137
        g_test_add_func ("/attributes/build-bad-type", test_build_bad_type);
 
138
 
 
139
        return g_test_run ();
 
140
}