~ubuntu-branches/ubuntu/utopic/telepathy-mission-control-5/utopic-proposed

« back to all changes in this revision

Viewing changes to .pc/git_migration_debug_segfault.patch/tests/account-store.c

  • Committer: Package Import Robot
  • Author(s): Sebastien Bacher
  • Date: 2013-11-13 23:23:05 UTC
  • mfrom: (0.13.7) (0.2.27 sid)
  • Revision ID: package-import@ubuntu.com-20131113232305-sev3nx1lps1xgya6
Tags: 1:5.16.0-1ubuntu1
* Resynchronize on Debian, remaining changes:
* debian/apparmor-profile, debian/control:
  - build-depends on dh-apparmor, suggests apparmor
* debian/rules: 
  - install apparmor and apport files
* debian/telepathy-mission-control-5.apport: 
  - include extra debug informations
* debian/telepathy-mission-control-5.dirs:
  - create apparmor and apport directories

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * MC account storage backend inspector
3
 
 *
4
 
 * Copyright © 2010 Nokia Corporation
5
 
 * Copyright © 2010 Collabora Ltd.
6
 
 *
7
 
 * This library is free software; you can redistribute it and/or
8
 
 * modify it under the terms of the GNU Lesser General Public
9
 
 * License as published by the Free Software Foundation; either
10
 
 * version 2.1 of the License, or (at your option) any later version.
11
 
 *
12
 
 * This library is distributed in the hope that it will be useful,
13
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 
 * Lesser General Public License for more details.
16
 
 *
17
 
 * You should have received a copy of the GNU Lesser General Public
18
 
 * License along with this library; if not, write to the Free Software
19
 
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20
 
 */
21
 
 
22
 
#include "config.h"
23
 
 
24
 
#include <stdlib.h>
25
 
#include <string.h>
26
 
#include <stdio.h>
27
 
#include <glib.h>
28
 
#include <glib-object.h>
29
 
 
30
 
#include "account-store-default.h"
31
 
 
32
 
#define DOCSTRING_A \
33
 
  "%s OP BACKEND ACCOUNT [KEY [VALUE]]\n\n"        \
34
 
  "  OP      := <get | set | del | has | list>\n"  \
35
 
  "  BACKEND := <"
36
 
 
37
 
#define DOCSTRING_B \
38
 
  ">\n"                                                                   \
39
 
  "  ACCOUNT := <MANAGER>/<PROTOCOL>/<ACCOUNT-UID>\n"                    \
40
 
  "  KEY     := <manager | protocol | DisplayName | param-<PARAMETER>>\n" \
41
 
  "  VALUE   := <STRING>\n\n"
42
 
 
43
 
#if ENABLE_LIBACCOUNTS_SSO
44
 
#include "account-store-libaccounts.h"
45
 
#endif
46
 
 
47
 
typedef struct {
48
 
  const gchar *name;
49
 
  gchar *  (*get) (const gchar *account, const gchar *key);
50
 
  gboolean (*set) (const gchar *account, const gchar *key, const gchar *value);
51
 
  gboolean (*delete) (const gchar *account);
52
 
  gboolean (*exists) (const gchar *account);
53
 
  GStrv    (*list) (void);
54
 
  guint    (*count_passwords) (void);
55
 
} Backend;
56
 
 
57
 
typedef enum {
58
 
  OP_UNKNOWN,
59
 
  OP_GET,
60
 
  OP_SET,
61
 
  OP_DELETE,
62
 
  OP_EXISTS,
63
 
  OP_LIST,
64
 
  OP_COUNT_PASSWORDS
65
 
} Operation;
66
 
 
67
 
const Backend backends[] = {
68
 
  { "default",
69
 
    default_get,
70
 
    default_set,
71
 
    default_delete,
72
 
    default_exists,
73
 
    default_list,
74
 
    default_count_passwords },
75
 
 
76
 
#if ENABLE_LIBACCOUNTS_SSO
77
 
  { "libaccounts",
78
 
    libaccounts_get,
79
 
    libaccounts_set,
80
 
    libaccounts_delete,
81
 
    libaccounts_exists,
82
 
    libaccounts_list,
83
 
    NULL },
84
 
#endif
85
 
 
86
 
  { NULL }
87
 
};
88
 
 
89
 
static void usage (const gchar *name, const gchar *fmt,
90
 
    ...) G_GNUC_NORETURN;
91
 
 
92
 
#if ENABLE_GNOME_KEYRING
93
 
#include <gnome-keyring.h>
94
 
 
95
 
static void
96
 
setup_default_keyring (void)
97
 
{
98
 
  GnomeKeyringResult result;
99
 
 
100
 
  g_debug ("Setting default keyring to: %s", g_getenv ("MC_KEYRING_NAME"));
101
 
 
102
 
  if (g_getenv ("MC_KEYRING_NAME") != NULL)
103
 
  {
104
 
      const gchar *keyring_name = g_getenv ("MC_KEYRING_NAME");
105
 
 
106
 
      g_debug ("MC Keyring name: %s", keyring_name);
107
 
 
108
 
      if ((result = gnome_keyring_set_default_keyring_sync (keyring_name)) ==
109
 
           GNOME_KEYRING_RESULT_OK)
110
 
      {
111
 
          g_debug ("Successfully set up temporary keyring %s for tests",
112
 
                   keyring_name);
113
 
      }
114
 
      else
115
 
      {
116
 
          g_warning ("Failed to set %s as the default keyring: %s",
117
 
                     keyring_name, gnome_keyring_result_to_message (result));
118
 
      }
119
 
  }
120
 
}
121
 
#endif
122
 
 
123
 
int main (int argc, char **argv)
124
 
{
125
 
  int i;
126
 
  const gchar *op_name = NULL;
127
 
  const gchar *backend = NULL;
128
 
  const gchar *account = NULL;
129
 
  const gchar *setting = NULL;
130
 
  const gchar *value = NULL;
131
 
  const Backend *store = NULL;
132
 
  Operation op = OP_UNKNOWN;
133
 
  gchar *output = NULL;
134
 
  gboolean success = FALSE;
135
 
 
136
 
  g_type_init ();
137
 
  g_set_application_name (argv[0]);
138
 
 
139
 
#if ENABLE_GNOME_KEYRING
140
 
  setup_default_keyring ();
141
 
#endif
142
 
 
143
 
  if (argc < 3)
144
 
    usage (argv[0], "");
145
 
 
146
 
  op_name = argv[1];
147
 
  backend = argv[2];
148
 
 
149
 
  for (i = 0; backends[i].name != NULL; i++)
150
 
    {
151
 
      if (g_str_equal (backends[i].name, backend))
152
 
        {
153
 
          store = &backends[i];
154
 
          break;
155
 
        }
156
 
    }
157
 
 
158
 
  if (store == NULL)
159
 
    usage (argv[0], "No such backend %s", backend);
160
 
 
161
 
  if (g_str_equal (op_name, "get"))
162
 
    op = OP_GET;
163
 
  else if (g_str_equal (op_name, "set"))
164
 
    op = OP_SET;
165
 
  else if (g_str_equal (op_name, "del"))
166
 
    op = OP_DELETE;
167
 
  else if (g_str_equal (op_name, "has"))
168
 
    op = OP_EXISTS;
169
 
  else if (g_str_equal (op_name, "list"))
170
 
    op = OP_LIST;
171
 
  else if (g_str_equal (op_name, "count-passwords"))
172
 
    op = OP_COUNT_PASSWORDS;
173
 
 
174
 
  switch (op)
175
 
    {
176
 
      case OP_SET:
177
 
 
178
 
        if (argc >= 6)
179
 
          value = argv[5];
180
 
 
181
 
      case OP_GET:
182
 
 
183
 
        if (argc < 5)
184
 
          usage (argv[0], "op '%s' requires an account and key", op_name);
185
 
 
186
 
        account = argv[3];
187
 
        setting = argv[4];
188
 
 
189
 
        if (account == NULL || *account == '\0')
190
 
          usage (argv[0], "op '%s' requires an account", op_name);
191
 
 
192
 
        if (setting == NULL || *setting == '\0')
193
 
          usage (argv[0], "op '%s' requires a key", op_name);
194
 
 
195
 
        break;
196
 
 
197
 
      case OP_DELETE:
198
 
      case OP_EXISTS:
199
 
 
200
 
        if (argc < 4)
201
 
          usage (argv[0], "op '%s' requires an account", op_name);
202
 
 
203
 
        account = argv[3];
204
 
        break;
205
 
 
206
 
      case OP_LIST:
207
 
        if (argc < 3)
208
 
          usage (argv[0], "op '%s' requires an backend", op_name);
209
 
        break;
210
 
 
211
 
      case OP_COUNT_PASSWORDS:
212
 
        if (argc < 3)
213
 
          usage (argv[0], "op '%s' requires an backend", op_name);
214
 
        break;
215
 
 
216
 
      case OP_UNKNOWN:
217
 
        usage (argv[0], "Unknown operation: %s", op_name);
218
 
    }
219
 
 
220
 
  /* if we got this far, we have all the args we need: */
221
 
  switch (op)
222
 
    {
223
 
      GStrv list;
224
 
 
225
 
      case OP_GET:
226
 
        output = store->get (account, setting);
227
 
        success = output != NULL;
228
 
        break;
229
 
 
230
 
      case OP_SET:
231
 
        success = store->set (account, setting, value);
232
 
        output = g_strdup_printf ("%s.%s set to '%s' in %s",
233
 
            account, setting, value, store->name);
234
 
        break;
235
 
 
236
 
      case OP_DELETE:
237
 
        success = store->delete (account);
238
 
        output = g_strdup_printf ("%s deleted from %s", account, store->name);
239
 
        break;
240
 
 
241
 
      case OP_EXISTS:
242
 
        success = store->exists (account);
243
 
        if (success)
244
 
          output = g_strdup_printf ("Exists in %s", store->name);
245
 
        break;
246
 
 
247
 
      case OP_LIST:
248
 
        list = store->list ();
249
 
        output = g_strjoinv ("\n", list);
250
 
        g_strfreev (list);
251
 
        break;
252
 
 
253
 
      case OP_COUNT_PASSWORDS:
254
 
        if (store->count_passwords == NULL)
255
 
          {
256
 
            g_printerr ("Password-counting is unimplemented\n");
257
 
          }
258
 
        else
259
 
          {
260
 
            guint n = store->count_passwords ();
261
 
            output = g_strdup_printf ("%u", n);
262
 
            success = TRUE;
263
 
          }
264
 
        break;
265
 
 
266
 
      default:
267
 
        output = g_strdup ("Unknown operation");
268
 
    }
269
 
 
270
 
  if (output != NULL)
271
 
    printf ("%s\n", output);
272
 
 
273
 
  g_free (output);
274
 
 
275
 
  return success ? 0 : 1;
276
 
}
277
 
 
278
 
static void
279
 
usage (const gchar *name, const gchar *fmt, ...)
280
 
{
281
 
  guint i;
282
 
  va_list ap;
283
 
 
284
 
  fprintf (stderr, DOCSTRING_A, name);
285
 
 
286
 
  fprintf (stderr, "%s", backends[0].name);
287
 
 
288
 
  for (i = 1; backends[i].name != NULL; i++)
289
 
    fprintf (stderr, " | %s", backends[i].name);
290
 
 
291
 
  fprintf (stderr, DOCSTRING_B);
292
 
 
293
 
  va_start (ap, fmt);
294
 
  vfprintf (stderr, fmt, ap);
295
 
  va_end (ap);
296
 
 
297
 
  exit (1);
298
 
}