summaryrefslogtreecommitdiff
path: root/libsecret/test-vala-lang.vala
blob: ead58177135fe5667b6edd9fef5e95b812535187 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
 * 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.1 of the licence or (at
 * your option) any later version.
 *
 * See the included COPYING file for more information.
 */

Secret.Schema schema;
Secret.Schema no_name_schema;

private void test_lookup_sync () {
  try {
    string? password = Secret.password_lookup_sync (schema, null, "even", false, "string", "one", "number", 1);
    GLib.assert (password == "111");
  } catch ( GLib.Error e ) {
    GLib.error (e.message);
  }
}

public async void test_lookup_async_ex () {
  try {
    string? password = yield Secret.password_lookup (schema, null, "even", false, "string", "one", "number", 1);
    GLib.assert (password == "111");
  } catch ( GLib.Error e ) {
    GLib.error (e.message);
  }
}

private void test_lookup_async () {
  var loop = new GLib.MainLoop ();
  test_lookup_async_ex.begin ((obj, async_res) => {
      loop.quit ();
    });
  loop.run ();
}

private void test_lookup_no_name () {
  try {
    string? password = Secret.password_lookup_sync (schema, null, "number", 5);
    GLib.assert (password == null);

    password = Secret.password_lookup_sync (no_name_schema, null, "number", 5);
    GLib.assert (password == "555");
  } catch ( GLib.Error e ) {
    GLib.error (e.message);
  }
}

private void test_store_sync () {
  try {
    var attributes = new GLib.HashTable<string,string> (GLib.str_hash, GLib.str_equal);
    attributes["even"] = "false";
    attributes["string"] = "nine";
    attributes["number"] = "9";

    string? password = Secret.password_lookupv_sync (schema, attributes);
    GLib.assert (password == null);

    bool stored = Secret.password_storev_sync (schema, attributes, Secret.COLLECTION_DEFAULT, "The number ", "999");
    GLib.assert (stored);

    password = Secret.password_lookupv_sync (schema, attributes);
    GLib.assert (password == "999");
  } catch ( GLib.Error e ) {
    GLib.error (e.message);
  }
}

private async void test_store_async_ex () {
  var attributes = new GLib.HashTable<string,string> (GLib.str_hash, GLib.str_equal);
  attributes["even"] = "true";
  attributes["string"] = "eight";
  attributes["number"] = "8";

  try {
    string? password = yield Secret.password_lookupv (schema, attributes, null);
    GLib.assert (password == null);

    bool stored = yield Secret.password_storev (schema, attributes, Secret.COLLECTION_DEFAULT, "The number nine", "999", null);
    GLib.assert (stored);

    password = yield Secret.password_lookupv (schema, attributes, null);
    GLib.assert (password == "999");
  } catch ( GLib.Error e ) {
    GLib.error (e.message);
  }
}

private void test_store_async () {
  var loop = new GLib.MainLoop ();
  test_store_async_ex.begin ((obj, async_res) => {
      loop.quit ();
    });
  loop.run ();
}

private void test_clear_sync () {
  try {
    var attributes = new GLib.HashTable<string,string> (GLib.str_hash, GLib.str_equal);
    attributes["even"] = "false";
    attributes["string"] = "nine";
    attributes["number"] = "9";

    string? password = Secret.password_lookupv_sync (schema, attributes);
    GLib.assert (password == "999");

    bool removed = Secret.password_clearv_sync (schema, attributes, null);
    GLib.assert (removed);

    password = Secret.password_lookupv_sync (schema, attributes);
    GLib.assert (password == null);
  } catch ( GLib.Error e ) {
    GLib.error (e.message);
  }
}

private async void test_clear_async_ex () {
  var attributes = new GLib.HashTable<string,string> (GLib.str_hash, GLib.str_equal);
  attributes["even"] = "true";
  attributes["string"] = "eight";
  attributes["number"] = "8";

  try {
    string? password = yield Secret.password_lookupv (schema, attributes, null);
    GLib.assert (password == "999");

    bool removed = yield Secret.password_clearv (schema, attributes, null);
    GLib.assert (removed);

    password = yield Secret.password_lookupv (schema, attributes, null);
    GLib.assert (password == null);
  } catch ( GLib.Error e ) {
    GLib.error (e.message);
  }
}

private void test_clear_async () {
  var loop = new GLib.MainLoop ();
  test_clear_async_ex.begin ((obj, async_res) => {
      loop.quit ();
    });
  loop.run ();
}

private static int main (string[] args) {
  GLib.Test.init (ref args);

  try {
    MockService.start ("mock-service-normal.py");
  } catch ( GLib.Error e ) {
    GLib.error ("Unable to start mock service: %s", e.message);
  }

  {
    schema = new Secret.Schema ("org.mock.Schema", Secret.SchemaFlags.NONE,
                                "number", Secret.SchemaAttributeType.INTEGER,
                                "string", Secret.SchemaAttributeType.STRING,
                                "even", Secret.SchemaAttributeType.BOOLEAN);

    no_name_schema = new Secret.Schema ("unused.Schema.Name", Secret.SchemaFlags.DONT_MATCH_NAME,
                                        "number", Secret.SchemaAttributeType.INTEGER,
                                        "string", Secret.SchemaAttributeType.STRING);
  }

  GLib.Test.add_data_func ("/vala/lookup/sync", test_lookup_sync);
  GLib.Test.add_data_func ("/vala/lookup/async", test_lookup_async);
  GLib.Test.add_data_func ("/vala/lookup/no-name", test_lookup_no_name);
  GLib.Test.add_data_func ("/vala/store/sync", test_store_sync);
  GLib.Test.add_data_func ("/vala/store/async", test_store_async);
  GLib.Test.add_data_func ("/vala/clear/sync", test_clear_sync);
  GLib.Test.add_data_func ("/vala/clear/async", test_clear_async);

  var res = GLib.Test.run ();

  Secret.Service.disconnect ();
  MockService.stop ();
  schema = null;

  return res;
}