File: support.c

package info (click to toggle)
libsyncml 0.5.4-2.1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 3,276 kB
  • sloc: ansic: 38,414; xml: 355; makefile: 71; sh: 50
file content (219 lines) | stat: -rw-r--r-- 6,089 bytes parent folder | download | duplicates (2)
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
/*
 * libsyncml - A syncml protocol implementation
 * Copyright (C) 2005  Armin Bauer <armin.bauer@opensync.org>
 *
 * This library 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 License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 *
 */
 
#include "support.h"
#include <libsyncml/sml_error_internals.h>
#include <libxml/parser.h>

#ifndef THREAD_SAFE_CHECK
GStaticMutex __libsyncml_check_mutex = G_STATIC_MUTEX_INIT;
#endif

/* FIXME: These settings should be controlled by the test environment.
 * FIXME: The hard coding of the environment creates an unreal test scenario.
 * FIXME: The build host environment must be changed appropriately.
 */
void configure_environment()
{
	/* Several libraries do things which confuse debuggers and
	 * memory checkers like Valgrind. Therefore it is often
	 * possible to configure such libraries via environment
	 * variables to use a more conventional behaviour.
	 *
	 * If the user already set an environment variable
	 * then this variable is not touched.
	 */

	/* The glib uses some memory caching mechanisms for performance
	 * enhancements which confuses memory checkers like Valgrind.
	 * Therefore it is necessary to deactivate these mechanisms for
	 * testing.
	 */
	if (!g_setenv("G_SLICE", "always-malloc", FALSE))
		g_warning("G_SLICE is already set.");
	if (!g_setenv("G_DEBUG", "gc-friendly", FALSE))
		g_warning("G_DEBUG is already set.");

	/* The check library usually forks test suites to safely survive
	 * things like segmentation faults and to have a clean separated
	 * address space for every test suite. This behaviour makes it
	 * difficult for memory checks like Valgrind to detect real
	 * problems correctly. Usually you get tons of false positives.
	 * Therefore forking is disabled.
	 */
	if (!g_setenv("CK_FORK", "no", FALSE))
		g_warning("CK_FORK is already set.");

	xmlInitParser();
}

void cleanup_environment()
{
	xmlCleanupParser();
}

char *olddir = NULL;

char *setup_testbed(char *fkt_name)
{
	char *testbed = NULL;
	setuid(65534);
	
	if (!g_thread_supported ()) g_thread_init (NULL);

#ifndef THREAD_SAFE_CHECK
	/* unlock static mutex lock if locked from earlier error */
	g_static_mutex_trylock(&__libsyncml_check_mutex);
	g_static_mutex_unlock(&__libsyncml_check_mutex);
#endif
	
	if (fkt_name) {
		testbed = g_strdup_printf("%s/testbed.XXXXXX", g_get_tmp_dir());
		if (mkdtemp(testbed)) {
			sml_fail_unless(FALSE, "Cannot setup testbed at %s.", testbed);
		}
		
		char *command = NULL;
		if (fkt_name) {
			command = g_strdup_printf("cp -R %s%sdata/%s/* %s", g_getenv("srcdir") ? g_getenv("srcdir") : "", g_getenv("srcdir") ? "/" : "", fkt_name, testbed);
			if (system(command)) {
				sml_fail_unless(FALSE, "Cannot execute command \"%s\".", command);
			}
			g_free(command);
		}
		
		olddir = g_get_current_dir();
		if (chdir(testbed)) {
			sml_fail_unless(FALSE, "Cannot change to testbed directory %s.", testbed);
		}
		
		smlTrace(TRACE_INTERNAL, "%s: Seting up %s at %s", __func__, VA_STRING(fkt_name), VA_STRING(testbed));
	}
	//printf(".");
	//fflush(NULL);
	return testbed;
}

void destroy_testbed(char *path)
{
	char *command = g_strdup_printf("rm -rf %s", path);
	if (olddir && chdir(olddir)) {
		sml_fail_unless(FALSE, "Cannot change to old directory \"%s\".", olddir);
	}
	if (system(command)) {
		sml_fail_unless(FALSE, "Cannot execute command \"%s\".", command);
	}
	g_free(command);
	smlTrace(TRACE_INTERNAL, "%s: Tearing down %s", __func__, VA_STRING(path));
	g_free(path);
}

void create_case(Suite *s, const char *name, TFun function)
{
	TCase *tc_new = tcase_create(name);
	tcase_set_timeout(tc_new, 60);
	tcase_add_test(tc_new, function);
	suite_add_tcase (s, tc_new);
}

SmlParser *start_parser(const char *data, SmlError **error)
{
	CHECK_ERROR_REF
	setup_testbed(NULL);
	
	SmlParser *parser = smlParserNew(SML_MIMETYPE_XML, 0, error);
	sml_fail_unless(parser != NULL, "%s", smlErrorPrint(error));
	sml_fail_unless(*error == NULL, "%s", smlErrorPrint(error));
	
	if (!smlParserStart(parser, data, strlen(data), error)) {
		sml_fail_unless(*error != NULL, "The parser start failed but the error object was not set.");
		smlParserFree(parser);
		return NULL;
	}
	
	return parser;
}

int sml_sleep(int64_t nanoseconds)
{
	long seconds = nanoseconds / 1000000000;
	nanoseconds = nanoseconds % 1000000000;
	struct timespec timestr;
	timestr.tv_sec  = seconds;
	timestr.tv_nsec = nanoseconds;
	return nanosleep(&timestr, NULL);
}

/* create test case stuff */

static void sml_testsuite_all(Suite *s, struct sml_testcase_s *tc)
{
	unsigned int i;
	for (i=0; tc[i].name; i++)
		create_case(s, tc[i].name, tc[i].func);
}

static unsigned int sml_testsuite_selected(
			Suite *s,
			int argc, char **argv,
			struct sml_testcase_s *tc)
{
	int i, j, n=0;
	/* Also argv[0]! for symlink-ed calls */
	for (i=0; argc > i; i++) {
		for (j=0; tc[j].name; j++) {
			if (strcmp(g_basename(argv[i]), tc[j].name))
					continue;

			create_case(s, tc[j].name, tc[j].func);
			n++;
		}
	}

	return n;
}

int sml_testsuite(
		int argc,
		char **argv,
		const char *unittest,
		struct sml_testcase_s *tc)
{
	configure_environment();

	int nf;
	Suite *s = suite_create(unittest);
	SRunner *sr;

	/* check_env(); */

	if (!sml_testsuite_selected(s, argc, argv, tc))
		sml_testsuite_all(s, tc);

	sr = srunner_create(s);
	srunner_run_all(sr, CK_VERBOSE);
	nf = srunner_ntests_failed(sr);
	srunner_free(sr);

	cleanup_environment();

	return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}