aboutsummaryrefslogtreecommitdiff
path: root/tests/backend-dbus/mock-accounts.cc
blob: 2a0e7e76101cafd81b138d753eb7d70f0d53674e (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
/*
 * Copyright 2013 Canonical Ltd.
 *
 * Authors:
 *   Charles Kerr <charles.kerr@canonical.com>
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 3, as published
 * by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranties of
 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "mock-accounts.h"
#include "mock-user.h"

namespace
{
  const char * const DBUS_ACCOUNTS_NAME = "org.freedesktop.Accounts";

  const char * const DBUS_ACCOUNTS_PATH = "/org/freedesktop/Accounts";
}

/***
****
***/

void
MockAccounts :: add_user (MockUser * user)
{
  g_assert (my_users.count(user) == 0);

  my_users.insert (user);
  my_uid_to_user[user->uid()] = user;
  my_path_to_user[user->path()] = user;
  my_username_to_user[user->username()] = user;

  accounts_emit_user_added (my_skeleton, user->path());
}

void
MockAccounts :: remove_user (MockUser * user)
{
  g_assert (my_users.count(user) == 1);

  my_users.erase (user);
  my_uid_to_user.erase (user->uid());
  my_path_to_user.erase (user->path());
  my_username_to_user.erase (user->username());

  accounts_emit_user_deleted (my_skeleton, user->path());
}

MockUser *
MockAccounts :: find_by_uid (guint64 uid)
{
  const uid_to_user_t::iterator it (my_uid_to_user.find(uid));

  if (it != my_uid_to_user.end())
    return it->second;

  g_warn_if_reached ();
  return 0;
}

MockUser *
MockAccounts :: find_by_path (const char * path)
{
  const path_to_user_t::iterator it (my_path_to_user.find(path));

  if (it != my_path_to_user.end())
    return it->second;

  g_warn_if_reached ();
  return 0;
}

MockUser *
MockAccounts :: find_by_username (const char * username)
{
  const username_to_user_t::iterator it (my_username_to_user.find(username));

  if (it != my_path_to_user.end())
    return it->second;

  g_warn_if_reached ();
  return 0;
}

/***
****
***/

gboolean
MockAccounts :: on_find_user_by_id_static (Accounts              * a,
                                           GDBusMethodInvocation * invocation,
                                           guint64                 uid,
                                           gpointer                gself)
{
  MockUser * user = static_cast<MockAccounts*>(gself)->find_by_uid (uid);
  accounts_complete_find_user_by_id (a, invocation, user ? user->path() : "");
  return true;
}

gboolean
MockAccounts :: on_list_cached_users_static (Accounts              * a,
                                             GDBusMethodInvocation * invocation,
                                             gpointer                gself)
{
  int i;
  const char ** paths;
  const users_t& users = static_cast<MockAccounts*>(gself)->my_users;

  i = 0;
  paths = g_new0 (const char*, users.size() + 1);
  for (users_t::iterator it(users.begin()),
                        end(users.end()); it!=end; ++it)
   paths[i++] = (*it)->path();
  accounts_complete_list_cached_users (a, invocation, paths);
  g_free (paths);

  return true;
}

/***
****
***/

MockAccounts :: MockAccounts (GMainLoop       * loop,
                              GDBusConnection * bus_connection):
  MockObject (loop, bus_connection, DBUS_ACCOUNTS_NAME, DBUS_ACCOUNTS_PATH),
  my_skeleton (accounts_skeleton_new ())
{
  g_signal_connect (my_skeleton, "handle-list-cached-users",
                    G_CALLBACK(on_list_cached_users_static), this);
  g_signal_connect (my_skeleton, "handle-find-user-by-id",
                    G_CALLBACK(on_find_user_by_id_static), this);

  set_skeleton (G_DBUS_INTERFACE_SKELETON(my_skeleton));
}

MockAccounts :: ~MockAccounts ()
{
  for (users_t::iterator it(my_users.begin()),
                        end(my_users.end()); it!=end; ++it)
    delete *it;

  g_signal_handlers_disconnect_by_data (my_skeleton, this);
  g_clear_object (&my_skeleton);
}