summaryrefslogtreecommitdiff
path: root/unit/utils.c
blob: 3c8eaae78f1a0b9d41dcab50fb86f62cb149af96 (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
/*
 *
 *  Connection Manager
 *
 *  Copyright (C) 2011  BWM CarIT GmbH. All rights reserved.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 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 warranty of
 *  MERCHANTABILITY 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, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdlib.h>

#include "gdbus/gdbus.h"

#include "test-connman.h"

#define ENABLE_WRAPPER 1

gboolean util_quit_loop(gpointer data)
{
	struct test_fix *fix = data;

	g_main_loop_quit(fix->main_loop);

	return FALSE;
}

guint util_idle_call(struct test_fix *fix, GSourceFunc func,
			GDestroyNotify notify)
{
	GSource *source;
	guint id;

	source = g_idle_source_new();
	g_source_set_callback(source, func, fix, notify);
	id = g_source_attach(source, g_main_loop_get_context(fix->main_loop));
	g_source_unref(source);

	return id;
}

guint util_call(struct test_fix *fix, GSourceFunc func,
		GDestroyNotify notify)
{
	GSource *source;
	guint id;

	source = g_timeout_source_new(0);
	g_source_set_callback(source, func, fix, notify);
	id = g_source_attach(source, g_main_loop_get_context(fix->main_loop));
	g_source_unref(source);

	return id;
}

void util_setup(struct test_fix *fix, gconstpointer data)
{
	fix->main_loop = g_main_loop_new(NULL, FALSE);
	fix->main_connection = g_dbus_setup_private(DBUS_BUS_SYSTEM,
							NULL, NULL);
}

void util_teardown(struct test_fix *fix, gconstpointer data)
{
	dbus_connection_close(fix->main_connection);
	dbus_connection_unref(fix->main_connection);

	g_main_loop_unref(fix->main_loop);
}

static void util_wrapper(struct test_fix *fix, gconstpointer data)
{
	GSourceFunc func = data;
#if ENABLE_WRAPPER
	if (g_test_trap_fork(60 * 1000 * 1000, 0) == TRUE) {
		util_call(fix, func, NULL);
		g_main_loop_run(fix->main_loop);
		exit(0);
	}

	g_test_trap_assert_passed();
#else
	util_call(fix, func, NULL);
	g_main_loop_run(fix->main_loop);
#endif
}

void util_test_add(const char *test_name, GSourceFunc test_func,
			util_test_setup_cb setup_cb,
			util_test_teardown_cb teardown_cb)
{
	g_test_add(test_name, struct test_fix, test_func,
		setup_cb, util_wrapper, teardown_cb);
}

void util_session_create(struct test_fix *fix, unsigned int max_sessions)
{
	unsigned int i;

	fix->max_sessions = max_sessions;
	fix->session = g_try_new0(struct test_session, max_sessions);

	for (i = 0; i < max_sessions; i++) {
		fix->session[i].fix = fix;
		fix->session[i].info = g_try_new0(struct test_session_info, 1);
		fix->session[i].connection = g_dbus_setup_private(
						DBUS_BUS_SYSTEM, NULL, NULL);
	}
}

void util_session_destroy(gpointer data)
{
	struct test_fix *fix = data;

	unsigned int i;

	for (i = 0; i < fix->max_sessions; i++) {
		dbus_connection_close(fix->session[i].connection);
		g_free(fix->session[i].info);
	}

	g_free(fix->session);
}