collection: Have gobject-introspection and glib-mkenums recognize flags
[platform/upstream/libsecret.git] / libsecret / tests / test-vala-lang.vala
1 /*
2  * Copyright 2012 Red Hat Inc.
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published
6  * by the Free Software Foundation; either version 2.1 of the licence or (at
7  * your option) any later version.
8  *
9  * See the included COPYING file for more information.
10  */
11
12 Secret.Schema schema;
13 Secret.Schema no_name_schema;
14
15 private void test_lookup_sync () {
16   try {
17     string? password = Secret.password_lookup_sync (schema, null, "even", false, "string", "one", "number", 1);
18     GLib.assert (password == "111");
19   } catch ( GLib.Error e ) {
20     GLib.error (e.message);
21   }
22 }
23
24 public async void test_lookup_async_ex () {
25   try {
26     string? password = yield Secret.password_lookup (schema, null, "even", false, "string", "one", "number", 1);
27     GLib.assert (password == "111");
28   } catch ( GLib.Error e ) {
29     GLib.error (e.message);
30   }
31 }
32
33 private void test_lookup_async () {
34   var loop = new GLib.MainLoop ();
35   test_lookup_async_ex.begin ((obj, async_res) => {
36       loop.quit ();
37     });
38   loop.run ();
39 }
40
41 private void test_lookup_no_name () {
42   try {
43     string? password = Secret.password_lookup_sync (schema, null, "number", 5);
44     GLib.assert (password == null);
45
46     password = Secret.password_lookup_sync (no_name_schema, null, "number", 5);
47     GLib.assert (password == "555");
48   } catch ( GLib.Error e ) {
49     GLib.error (e.message);
50   }
51 }
52
53 private void test_store_sync () {
54   try {
55     var attributes = new GLib.HashTable<string,string> (GLib.str_hash, GLib.str_equal);
56     attributes["even"] = "false";
57     attributes["string"] = "nine";
58     attributes["number"] = "9";
59
60     string? password = Secret.password_lookupv_sync (schema, attributes);
61     GLib.assert (password == null);
62
63     bool stored = Secret.password_storev_sync (schema, attributes, Secret.COLLECTION_DEFAULT, "The number ", "999");
64     GLib.assert (stored);
65
66     password = Secret.password_lookupv_sync (schema, attributes);
67     GLib.assert (password == "999");
68   } catch ( GLib.Error e ) {
69     GLib.error (e.message);
70   }
71 }
72
73 private async void test_store_async_ex () {
74   var attributes = new GLib.HashTable<string,string> (GLib.str_hash, GLib.str_equal);
75   attributes["even"] = "true";
76   attributes["string"] = "eight";
77   attributes["number"] = "8";
78
79   try {
80     string? password = yield Secret.password_lookupv (schema, attributes, null);
81     GLib.assert (password == null);
82
83     bool stored = yield Secret.password_storev (schema, attributes, Secret.COLLECTION_DEFAULT, "The number nine", "999", null);
84     GLib.assert (stored);
85
86     password = yield Secret.password_lookupv (schema, attributes, null);
87     GLib.assert (password == "999");
88   } catch ( GLib.Error e ) {
89     GLib.error (e.message);
90   }
91 }
92
93 private void test_store_async () {
94   var loop = new GLib.MainLoop ();
95   test_store_async_ex.begin ((obj, async_res) => {
96       loop.quit ();
97     });
98   loop.run ();
99 }
100
101 private void test_clear_sync () {
102   try {
103     var attributes = new GLib.HashTable<string,string> (GLib.str_hash, GLib.str_equal);
104     attributes["even"] = "false";
105     attributes["string"] = "nine";
106     attributes["number"] = "9";
107
108     string? password = Secret.password_lookupv_sync (schema, attributes);
109     GLib.assert (password == "999");
110
111     bool removed = Secret.password_clearv_sync (schema, attributes, null);
112     GLib.assert (removed);
113
114     password = Secret.password_lookupv_sync (schema, attributes);
115     GLib.assert (password == null);
116   } catch ( GLib.Error e ) {
117     GLib.error (e.message);
118   }
119 }
120
121 private async void test_clear_async_ex () {
122   var attributes = new GLib.HashTable<string,string> (GLib.str_hash, GLib.str_equal);
123   attributes["even"] = "true";
124   attributes["string"] = "eight";
125   attributes["number"] = "8";
126
127   try {
128     string? password = yield Secret.password_lookupv (schema, attributes, null);
129     GLib.assert (password == "999");
130
131     bool removed = yield Secret.password_clearv (schema, attributes, null);
132     GLib.assert (removed);
133
134     password = yield Secret.password_lookupv (schema, attributes, null);
135     GLib.assert (password == null);
136   } catch ( GLib.Error e ) {
137     GLib.error (e.message);
138   }
139 }
140
141 private void test_clear_async () {
142   var loop = new GLib.MainLoop ();
143   test_clear_async_ex.begin ((obj, async_res) => {
144       loop.quit ();
145     });
146   loop.run ();
147 }
148
149 private static int main (string[] args) {
150   GLib.Test.init (ref args);
151
152   try {
153     MockService.start ("mock-service-normal.py");
154   } catch ( GLib.Error e ) {
155     GLib.error ("Unable to start mock service: %s", e.message);
156   }
157
158   {
159     schema = new Secret.Schema ("org.mock.Schema", Secret.SchemaFlags.NONE,
160                                 "number", Secret.SchemaAttributeType.INTEGER,
161                                 "string", Secret.SchemaAttributeType.STRING,
162                                 "even", Secret.SchemaAttributeType.BOOLEAN);
163
164     no_name_schema = new Secret.Schema ("unused.Schema.Name", Secret.SchemaFlags.DONT_MATCH_NAME,
165                                         "number", Secret.SchemaAttributeType.INTEGER,
166                                         "string", Secret.SchemaAttributeType.STRING);
167   }
168
169   GLib.Test.add_data_func ("/vala/lookup/sync", test_lookup_sync);
170   GLib.Test.add_data_func ("/vala/lookup/async", test_lookup_async);
171   GLib.Test.add_data_func ("/vala/lookup/no-name", test_lookup_no_name);
172   GLib.Test.add_data_func ("/vala/store/sync", test_store_sync);
173   GLib.Test.add_data_func ("/vala/store/async", test_store_async);
174   GLib.Test.add_data_func ("/vala/clear/sync", test_clear_sync);
175   GLib.Test.add_data_func ("/vala/clear/async", test_clear_async);
176
177   var res = GLib.Test.run ();
178
179   Secret.Service.disconnect ();
180   MockService.stop ();
181   schema = null;
182
183   return res;
184 }