summaryrefslogtreecommitdiffstats
path: root/test/test-example.c
blob: 05f97794b14a53078e848c57f2a72a999af5c76f (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
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
/* PipeWire
 *
 * Copyright © 2021 Red Hat, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */



#include "config.h"

#include <signal.h>
#include <unistd.h>

#include <pipewire/pipewire.h>

#include "pwtest.h"

/* The simplest test example (test passes)  */
PWTEST(successful_test)
{
	int x = 10, y = 20, z = 10;
	bool t = true, f = false;
	const char *a = "foo", *b = "bar", *c = "baz";

	pwtest_int_lt(x, y);
	pwtest_int_le(x, y);
	pwtest_int_gt(y, x);
	pwtest_int_ge(y, x);
	pwtest_int_eq(x, z);
	pwtest_int_ne(y, z);

	pwtest_bool_true(t);
	pwtest_bool_false(f);
	pwtest_bool_eq(t, !f);
	pwtest_bool_ne(t, f);

	pwtest_str_eq(a, a);
	pwtest_str_ne(a, b);
	pwtest_str_eq_n(b, c, 2);
	pwtest_str_ne_n(b, c, 3);

	return PWTEST_PASS;
}

/* Demo failure of an integer comparison (test will fail) */
PWTEST(failing_test_int)
{
	int x = 10, y = 20;
	pwtest_int_gt(x, y);
	return PWTEST_PASS;
}

/* Demo failure of a bool comparison (test will fail) */
PWTEST(failing_test_bool)
{
	bool oops = true;
	pwtest_bool_false(oops);
	return PWTEST_PASS;
}

/* Demo failure of a string comparison (test will fail) */
PWTEST(failing_test_string)
{
	const char *what = "yes";
	pwtest_str_eq(what, "no");
	return PWTEST_PASS;
}

/* Demo custom failure (test will fail) */
PWTEST(general_fail_test)
{
	/* pwtest_fail(); */
	pwtest_fail_with_msg("Some condition wasn't met");
	return PWTEST_PASS;
}

/* Demo failure (test will fail) */
PWTEST(failing_test_if_reached)
{
	pwtest_fail_if_reached();
	return PWTEST_SYSTEM_ERROR;
}

/* Demo a system error (test will fail) */
PWTEST(system_error_test)
{
	return PWTEST_SYSTEM_ERROR;
}

/* Demo signal handling of SIGSEGV (test will fail) */
PWTEST(catch_segfault_test)
{
	int *x = NULL;
	*x = 20;
	return PWTEST_PASS;
}

/* Demo signal handling of abort (test will fail) */
PWTEST(catch_abort_signal_test)
{
	abort();
	return PWTEST_PASS;
}

/* Demo a timeout (test will fail with default timeout of 30) */
PWTEST(timeout_test)
{
	/* run with --timeout=1 to make this less annoying */
	sleep(60);
	return PWTEST_PASS;
}

/* Demo a ranged test (test passes, skips the last 2)  */
PWTEST(ranged_test)
{
	struct pwtest_test *t = current_test;
	int iteration = pwtest_get_iteration(t);

	pwtest_int_lt(iteration, 10); /* see pwtest_add */

	printf("Ranged test iteration %d\n", iteration);
	if (iteration >= 8)
		return PWTEST_SKIP;

	return PWTEST_PASS;
}

/* Demo the properties passed to tests (test passes)  */
PWTEST(property_test)
{
	struct pwtest_test *t = current_test;
	struct pw_properties *p = pwtest_get_props(t);

	pwtest_ptr_notnull(p);
	pwtest_str_eq(pw_properties_get(p, "myprop"), "somevalue");
	pwtest_str_eq(pw_properties_get(p, "prop2"), "other");

	return PWTEST_PASS;
}

/* Demo the environment passed to tests (test passes)  */
PWTEST(env_test)
{
	pwtest_str_eq(getenv("myenv"), "envval");
	pwtest_str_eq(getenv("env2"), "val");

	/* Set by pwtest */
	pwtest_str_eq(getenv("PWTEST"), "1");

	return PWTEST_PASS;
}

/* Demo the environment passed to tests (test passes)  */
PWTEST(env_reset_test)
{
	/* If run after env_test even with --no-fork this test should
	 * succeed */
	pwtest_str_eq(getenv("myenv"), NULL);
	pwtest_str_eq(getenv("env2"), NULL);

	return PWTEST_PASS;
}

PWTEST(default_env_test)
{
	/* This one is set automatically */
	pwtest_str_eq(getenv("PWTEST"), "1");
	/* Default value */
	pwtest_str_eq(getenv("PIPEWIRE_REMOTE"), "test-has-no-daemon");

	return PWTEST_PASS;
}

PWTEST(daemon_test)
{
	struct pw_context *ctx;
        struct pw_core *core;
	struct pw_loop *loop;

	pwtest_str_eq_n(getenv("PIPEWIRE_REMOTE"), "pwtest-pw-", 10);

	pw_init(0, NULL);
	loop = pw_loop_new(NULL);
	ctx = pw_context_new(loop, NULL, 0);
	pwtest_ptr_notnull(ctx);
        core = pw_context_connect(ctx, NULL, 0);
	pwtest_ptr_notnull(core);

	pw_loop_iterate(loop, -1);
	pw_core_disconnect(core);
        pw_context_destroy(ctx);
	pw_loop_destroy(loop);

	return PWTEST_PASS;
}

/* If not started with a daemon, we can't connect to a daemon (test will fail)  */
PWTEST(daemon_test_without_daemon)
{
	struct pw_context *ctx;
        struct pw_core *core;
	struct pw_loop *loop;

	pw_init(0, NULL);
	loop = pw_loop_new(NULL);
	ctx = pw_context_new(loop, NULL, 0);
	pwtest_ptr_notnull(ctx);
        core = pw_context_connect(ctx, NULL, 0);

	pwtest_ptr_notnull(core); /* Expect this to fail because we don't have a daemon */

	pw_loop_iterate(loop, -1);
	pw_core_disconnect(core);
        pw_context_destroy(ctx);
	pw_loop_destroy(loop);

	return PWTEST_PASS;
}

PWTEST_SUITE(example_tests)
{
	pwtest_add(successful_test, PWTEST_NOARG);
	pwtest_add(failing_test_int, PWTEST_NOARG);
	pwtest_add(failing_test_bool, PWTEST_NOARG);
	pwtest_add(failing_test_string, PWTEST_NOARG);
	pwtest_add(failing_test_if_reached, PWTEST_NOARG);
	pwtest_add(general_fail_test, PWTEST_NOARG);
	pwtest_add(system_error_test, PWTEST_NOARG);
	pwtest_add(catch_segfault_test, PWTEST_NOARG);
	pwtest_add(catch_abort_signal_test, PWTEST_ARG_SIGNAL, SIGABRT);
	pwtest_add(ranged_test, PWTEST_ARG_RANGE, 0, 10);
	pwtest_add(property_test,
		   PWTEST_ARG_PROP, "myprop", "somevalue",
		   PWTEST_ARG_PROP, "prop2", "other");
	pwtest_add(env_test,
		   PWTEST_ARG_ENV, "env2", "val",
		   PWTEST_ARG_ENV, "myenv", "envval");
	pwtest_add(env_reset_test, PWTEST_NOARG);
	pwtest_add(default_env_test, PWTEST_NOARG);
	pwtest_add(daemon_test, PWTEST_ARG_DAEMON);
	pwtest_add(daemon_test_without_daemon, PWTEST_NOARG);

	/* Run this one last so it doesn't matter if we forget --timeout */
	pwtest_add(timeout_test, PWTEST_NOARG);

	return PWTEST_PASS;
}