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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
|
/*
* 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 "gtest-mock-dbus-fixture.h"
#include "backend.h"
#include "backend-dbus/backend-dbus.h"
/***
****
***/
class Users: public GTestMockDBusFixture
{
private:
typedef GTestMockDBusFixture super;
protected:
GCancellable * cancellable;
IndicatorSessionUsers * users;
virtual void SetUp ()
{
super :: SetUp ();
init_event_keys (0);
// init 'users'
cancellable = g_cancellable_new ();
users = 0;
backend_get (cancellable, NULL, &users, NULL);
g_assert (users != 0);
wait_msec (100);
}
virtual void TearDown ()
{
g_cancellable_cancel (cancellable);
g_clear_object (&cancellable);
g_clear_object (&users);
super :: TearDown ();
}
protected:
void compare_user (const MockUser * mu, const IndicatorSessionUser * isu, const std::string& user_state)
{
ASSERT_EQ (user_state, login1_seat->user_state (mu->uid()));
ASSERT_EQ (mu->uid(), isu->uid);
ASSERT_EQ (mu->login_frequency(), isu->login_frequency);
ASSERT_STREQ (mu->username(), isu->user_name);
ASSERT_STREQ (mu->realname(), isu->real_name);
ASSERT_EQ (mu->uid(), isu->uid);
ASSERT_EQ (user_state!="offline", isu->is_logged_in);
ASSERT_EQ (user_state=="active", isu->is_current_user);
}
void compare_user (const MockUser * mu, guint uid, const std::string& user_state)
{
IndicatorSessionUser * isu;
isu = indicator_session_users_get_user (users, uid);
compare_user (mu, isu, user_state);
indicator_session_user_free (isu);
}
void compare_user (guint uid, const std::string& user_state)
{
IndicatorSessionUser * isu = indicator_session_users_get_user (users, uid);
MockUser * mu = accounts->find_by_uid (uid);
compare_user (mu, isu, user_state);
indicator_session_user_free (isu);
}
private:
void init_event_keys (size_t n)
{
expected_event_count = n;
event_keys.clear();
}
static gboolean
wait_for_signals__timeout (gpointer name)
{
g_error ("%s: timed out waiting for signal '%s'", G_STRLOC, (char*)name);
return G_SOURCE_REMOVE;
}
static void
wait_for_signals__event (IndicatorSessionUser * u G_GNUC_UNUSED,
guint uid,
gpointer gself)
{
Users * self = static_cast<Users*>(gself);
self->event_keys.push_back (uid);
if (self->event_keys.size() == self->expected_event_count)
g_main_loop_quit (self->loop);
}
protected:
std::vector<guint> event_keys;
size_t expected_event_count;
void wait_for_signals (gpointer o, const gchar * name, size_t n)
{
const int timeout_seconds = 5; // arbitrary
init_event_keys (n);
guint handler_id = g_signal_connect (o, name,
G_CALLBACK(wait_for_signals__event),
this);
gulong timeout_id = g_timeout_add_seconds (timeout_seconds,
wait_for_signals__timeout,
(gpointer)name);
g_main_loop_run (loop);
g_source_remove (timeout_id);
g_signal_handler_disconnect (o, handler_id);
}
};
/***
****
***/
/**
* Confirm that the fixture's SetUp() and TearDown() work
*/
TEST_F (Users, HelloWorld)
{
ASSERT_TRUE (true);
}
/**
* Confirm that 'users' can get the cached users from our Mock Accounts
*/
TEST_F (Users, InitialUsers)
{
GList * l;
GList * uids = indicator_session_users_get_uids (users);
ASSERT_EQ (12, g_list_length (uids));
for (l=uids; l!=NULL; l=l->next)
{
const guint uid = GPOINTER_TO_UINT (l->data);
compare_user (uid, login1_seat->user_state (uid));
}
g_list_free (uids);
}
/**
* Confirm that 'users' can tell when a new user is added
*/
TEST_F (Users, UserAdded)
{
MockUser * mu;
mu = new MockUser (loop, conn, "pcushing", "Peter Cushing", 2);
accounts->add_user (mu);
ASSERT_EQ (0, event_keys.size());
wait_for_signals (users, INDICATOR_SESSION_USERS_SIGNAL_USER_ADDED, 1);
ASSERT_EQ (1, event_keys.size());
compare_user (mu, event_keys[0], "offline");
}
/**
* Confirm that 'users' can tell when a user is removed
*/
TEST_F (Users, UserRemoved)
{
MockUser * mu = accounts->find_by_username ("pdavison");
/* confirm that users knows about pdavison */
IndicatorSessionUser * isu = indicator_session_users_get_user (users, mu->uid());
ASSERT_TRUE (isu != NULL);
compare_user (mu, isu, "offline");
g_clear_pointer (&isu, indicator_session_user_free);
/* on the bus, remove pdavison. */
accounts->remove_user (mu);
/* now, users should emit a 'user removed' signal... */
ASSERT_EQ (0, event_keys.size());
wait_for_signals (users, INDICATOR_SESSION_USERS_SIGNAL_USER_REMOVED, 1);
ASSERT_EQ (1, event_keys.size());
/* confirm that users won't give us pdavison's info */
isu = indicator_session_users_get_user (users, mu->uid());
ASSERT_TRUE (isu == NULL);
/* confirm that users won't give us pdavison's uid */
GList * uids = indicator_session_users_get_uids (users);
ASSERT_TRUE (g_list_find (uids, GUINT_TO_POINTER(mu->uid())) == NULL);
g_list_free (uids);
delete mu;
}
/**
* Confirm that 'users' notices when a user's real name changes
*/
TEST_F (Users, RealnameChanged)
{
MockUser * mu;
mu = accounts->find_by_username ("pdavison");
const char * const realname = "Peter M. G. Moffett";
mu->set_realname (realname);
ASSERT_NE (mu->realname(), realname);
ASSERT_STREQ (mu->realname(), realname);
wait_for_signals (users, INDICATOR_SESSION_USERS_SIGNAL_USER_CHANGED, 1);
ASSERT_EQ (1, event_keys.size());
compare_user (mu, event_keys[0], "offline");
}
/**
* Confirm that 'users' notices when users log in and out
*/
TEST_F (Users, LogInLogOut)
{
// The fist doctor logs in.
// Confirm that 'users' notices.
MockUser * mu = accounts->find_by_username ("whartnell");
ASSERT_EQ (login1_seat->user_state (mu->uid()), "offline");
const int session_tag = login1_manager->add_session (login1_seat, mu);
wait_for_signals (users, INDICATOR_SESSION_USERS_SIGNAL_USER_CHANGED, 1);
ASSERT_EQ (1, event_keys.size());
compare_user (mu, event_keys[0], "online");
// The first doctor logs out.
// Confirm that 'users' notices.
login1_manager->remove_session (login1_seat, session_tag);
wait_for_signals (users, INDICATOR_SESSION_USERS_SIGNAL_USER_CHANGED, 1);
ASSERT_EQ (1, event_keys.size());
compare_user (mu, event_keys[0], "offline");
}
/**
* Confirm that 'users' notices when the active session changes
*/
TEST_F (Users, ActivateSession)
{
// confirm preconditions: msmith is active, msmith is offline
MockUser * const whartnell = accounts->find_by_username ("whartnell");
ASSERT_EQ (login1_seat->user_state (whartnell->uid()), "offline");
MockUser * const msmith = accounts->find_by_username ("msmith");
ASSERT_EQ (login1_seat->user_state (msmith->uid()), "active");
// whartnell logs in... confirm that 'users' notices
login1_manager->add_session (login1_seat, whartnell);
wait_for_signals (users, INDICATOR_SESSION_USERS_SIGNAL_USER_CHANGED, 1);
ASSERT_EQ (1, event_keys.size());
compare_user (whartnell, event_keys[0], "online");
// activate whartnell's session... confirm that 'users' sees:
// 1. msmith changes from 'active' to 'online'
// 2. whartnell changes from 'online' to 'active'
login1_seat->switch_to_user (whartnell->username());
wait_for_signals (users, INDICATOR_SESSION_USERS_SIGNAL_USER_CHANGED, 2);
ASSERT_EQ (2, event_keys.size());
compare_user (msmith, event_keys[0], "online");
compare_user (whartnell, event_keys[1], "active");
// reverse the test
login1_seat->switch_to_user (msmith->username());
wait_for_signals (users, INDICATOR_SESSION_USERS_SIGNAL_USER_CHANGED, 2);
ASSERT_EQ (2, event_keys.size());
compare_user (whartnell, event_keys[0], "online");
compare_user (msmith, event_keys[1], "active");
}
/**
* Confirm that we can change the active session via users' API.
* This is nearly the same as ActivateSession but uses users' API
*/
TEST_F (Users, ActivateUser)
{
// confirm preconditions: msmith is active, msmith is offline
MockUser * const whartnell = accounts->find_by_username ("whartnell");
ASSERT_EQ (login1_seat->user_state (whartnell->uid()), "offline");
MockUser * const msmith = accounts->find_by_username ("msmith");
ASSERT_EQ (login1_seat->user_state (msmith->uid()), "active");
// whartnell logs in... confirm that 'users' notices
login1_manager->add_session (login1_seat, whartnell);
wait_for_signals (users, INDICATOR_SESSION_USERS_SIGNAL_USER_CHANGED, 1);
ASSERT_EQ (1, event_keys.size());
compare_user (whartnell, event_keys[0], "online");
// activate whartnell's session... confirm that 'users' sees:
// 1. msmith changes from 'active' to 'online'
// 2. whartnell changes from 'online' to 'active'
indicator_session_users_activate_user (users, whartnell->uid());
wait_for_signals (users, INDICATOR_SESSION_USERS_SIGNAL_USER_CHANGED, 2);
ASSERT_EQ (2, event_keys.size());
compare_user (msmith, event_keys[0], "online");
compare_user (whartnell, event_keys[1], "active");
// reverse the test
indicator_session_users_activate_user (users, msmith->uid());
wait_for_signals (users, INDICATOR_SESSION_USERS_SIGNAL_USER_CHANGED, 2);
ASSERT_EQ (2, event_keys.size());
compare_user (whartnell, event_keys[0], "online");
compare_user (msmith, event_keys[1], "active");
}
/**
* Confirm that adding a Guest doesn't show up in the users list
*/
TEST_F (Users, UnwantedGuest)
{
GList * uids;
uids = indicator_session_users_get_uids (users);
const size_t n = g_list_length (uids);
g_list_free (uids);
MockUser * mu = new MockUser (loop, conn, "guest-jjbEVV", "Guest", 1);
mu->set_system_account (true);
accounts->add_user (mu);
wait_msec (50);
uids = indicator_session_users_get_uids (users);
ASSERT_EQ (n, g_list_length (uids));
g_list_free (uids);
}
/**
* Confirm that we can detect live sessions
*/
TEST_F (Users, LiveSession)
{
gboolean b;
// not initially a live session
ASSERT_FALSE (indicator_session_users_is_live_session (users));
g_object_get (users, INDICATOR_SESSION_USERS_PROP_IS_LIVE_SESSION, &b, NULL);
ASSERT_FALSE (b);
// now add the criteria for a live session
MockUser * live_user = new MockUser (loop, conn, "ubuntu", "Ubuntu", 1, 999);
live_user->set_system_account (true);
accounts->add_user (live_user);
const int session_tag = login1_manager->add_session (login1_seat, live_user);
wait_msec (100);
login1_seat->activate_session (session_tag);
wait_for_signal (users, "notify::" INDICATOR_SESSION_USERS_PROP_IS_LIVE_SESSION);
// confirm the backend thinks it's a live session
ASSERT_TRUE (indicator_session_users_is_live_session (users));
g_object_get (users, INDICATOR_SESSION_USERS_PROP_IS_LIVE_SESSION, &b, NULL);
ASSERT_TRUE (b);
}
|