Bump version number
[platform/upstream/libsecret.git] / library / tests / test-vala-lang.vala
1 Secret.Schema schema;
2 Secret.Schema no_name_schema;
3
4 private void test_lookup_sync () {
5   try {
6     string? password = Secret.password_lookup_sync (schema, null, "even", false, "string", "one", "number", 1);
7     GLib.assert (password == "111");
8   } catch ( GLib.Error e ) {
9     GLib.error (e.message);
10   }
11 }
12
13 public async void test_lookup_async_ex () {
14   try {
15     string? password = yield Secret.password_lookup (schema, null, "even", false, "string", "one", "number", 1);
16     GLib.assert (password == "111");
17   } catch ( GLib.Error e ) {
18     GLib.error (e.message);
19   }
20 }
21
22 private void test_lookup_async () {
23   var loop = new GLib.MainLoop ();
24   test_lookup_async_ex.begin ((obj, async_res) => {
25       loop.quit ();
26     });
27   loop.run ();
28 }
29
30 private void test_lookup_no_name () {
31   try {
32     string? password = Secret.password_lookup_sync (schema, null, "number", 5);
33     GLib.assert (password == null);
34
35     password = Secret.password_lookup_sync (no_name_schema, null, "number", 5);
36     GLib.assert (password == "555");
37   } catch ( GLib.Error e ) {
38     GLib.error (e.message);
39   }
40 }
41
42 private void test_store_sync () {
43   try {
44     var attributes = new GLib.HashTable<string,string> (GLib.str_hash, GLib.str_equal);
45     attributes["even"] = "false";
46     attributes["string"] = "nine";
47     attributes["number"] = "9";
48
49     string? password = Secret.password_lookupv_sync (schema, attributes);
50     GLib.assert (password == null);
51
52     bool stored = Secret.password_storev_sync (schema, attributes, Secret.COLLECTION_DEFAULT, "The number ", "999");
53     GLib.assert (stored);
54
55     password = Secret.password_lookupv_sync (schema, attributes);
56     GLib.assert (password == "999");
57   } catch ( GLib.Error e ) {
58     GLib.error (e.message);
59   }
60 }
61
62 private async void test_store_async_ex () {
63   var attributes = new GLib.HashTable<string,string> (GLib.str_hash, GLib.str_equal);
64   attributes["even"] = "true";
65   attributes["string"] = "eight";
66   attributes["number"] = "8";
67
68   try {
69     string? password = yield Secret.password_lookupv (schema, attributes, null);
70     GLib.assert (password == null);
71
72     bool stored = yield Secret.password_storev (schema, attributes, Secret.COLLECTION_DEFAULT, "The number nine", "999", null);
73     GLib.assert (stored);
74
75     password = yield Secret.password_lookupv (schema, attributes, null);
76     GLib.assert (password == "999");
77   } catch ( GLib.Error e ) {
78     GLib.error (e.message);
79   }
80 }
81
82 private void test_store_async () {
83   var loop = new GLib.MainLoop ();
84   test_store_async_ex.begin ((obj, async_res) => {
85       loop.quit ();
86     });
87   loop.run ();
88 }
89
90 private void test_remove_sync () {
91   try {
92     var attributes = new GLib.HashTable<string,string> (GLib.str_hash, GLib.str_equal);
93     attributes["even"] = "false";
94     attributes["string"] = "nine";
95     attributes["number"] = "9";
96
97     string? password = Secret.password_lookupv_sync (schema, attributes);
98     GLib.assert (password == "999");
99
100     bool removed = Secret.password_removev_sync (schema, attributes, null);
101     GLib.assert (removed);
102
103     password = Secret.password_lookupv_sync (schema, attributes);
104     GLib.assert (password == null);
105   } catch ( GLib.Error e ) {
106     GLib.error (e.message);
107   }
108 }
109
110 private async void test_remove_async_ex () {
111   var attributes = new GLib.HashTable<string,string> (GLib.str_hash, GLib.str_equal);
112   attributes["even"] = "true";
113   attributes["string"] = "eight";
114   attributes["number"] = "8";
115
116   try {
117     string? password = yield Secret.password_lookupv (schema, attributes, null);
118     GLib.assert (password == "999");
119
120     bool removed = yield Secret.password_removev (schema, attributes, null);
121     GLib.assert (removed);
122
123     password = yield Secret.password_lookupv (schema, attributes, null);
124     GLib.assert (password == null);
125   } catch ( GLib.Error e ) {
126     GLib.error (e.message);
127   }
128 }
129
130 private void test_remove_async () {
131   var loop = new GLib.MainLoop ();
132   test_remove_async_ex.begin ((obj, async_res) => {
133       loop.quit ();
134     });
135   loop.run ();
136 }
137
138 private static int main (string[] args) {
139   GLib.Test.init (ref args);
140
141   try {
142     MockService.start ("mock-service-normal.py");
143   } catch ( GLib.Error e ) {
144     GLib.error ("Unable to start mock service: %s", e.message);
145   }
146
147   {
148     schema = new Secret.Schema ("org.mock.Schema", Secret.SchemaFlags.NONE,
149                                 "number", Secret.SchemaAttributeType.INTEGER,
150                                 "string", Secret.SchemaAttributeType.STRING,
151                                 "even", Secret.SchemaAttributeType.BOOLEAN);
152
153     no_name_schema = new Secret.Schema ("unused.Schema.Name", Secret.SchemaFlags.DONT_MATCH_NAME,
154                                         "number", Secret.SchemaAttributeType.INTEGER,
155                                         "string", Secret.SchemaAttributeType.STRING);
156   }
157
158   GLib.Test.add_data_func ("/vala/lookup/sync", test_lookup_sync);
159   GLib.Test.add_data_func ("/vala/lookup/async", test_lookup_async);
160   GLib.Test.add_data_func ("/vala/lookup/no-name", test_lookup_no_name);
161   GLib.Test.add_data_func ("/vala/store/sync", test_store_sync);
162   GLib.Test.add_data_func ("/vala/store/async", test_store_async);
163   GLib.Test.add_data_func ("/vala/remove/sync", test_remove_sync);
164   GLib.Test.add_data_func ("/vala/remove/async", test_remove_async);
165
166   var res = GLib.Test.run ();
167
168   MockService.stop ();
169   schema = null;
170
171   return res;
172 }