summaryrefslogtreecommitdiffstats
path: root/testsuite/unittests/test_background_send.c
blob: ea654287305a5a0d32f88253819c322276a85d5e (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
/*
 * Unix SMB/CIFS implementation.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * 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, see <http://www.gnu.org/licenses/>.
 */

#include "replace.h"
#include <assert.h>
#include "source3/lib/background.h"
#include "source3/include/messages.h"
#include "lib/util/talloc_stack.h"
#include "source3/param/loadparm.h"
#include "dynconfig/dynconfig.h"

static int bg_trigger(void *private_data)
{
	return 1;
}

static void test_background_send(void)
{
	TALLOC_CTX *frame = talloc_stackframe();
	struct tevent_context *ev = NULL;
	struct messaging_context *msg_ctx = NULL;
	struct tevent_req *req = NULL;
	uint32_t ping_msg = MSG_PING;

	ev = tevent_context_init(frame);
	assert(ev != NULL);

	msg_ctx = messaging_init(frame, ev);
	assert(msg_ctx != NULL);

	req = background_job_send(
		frame, ev, msg_ctx, &ping_msg, 1, 0, bg_trigger, NULL);
	assert(req != NULL);

	/*
	 * Here's the core of this test: TALLOC_FREE msg_ctx before
	 * req. This happens if you use background_job_send() smbd and
	 * don't manually TALLOC_FREE req before exit_server()
	 */
	TALLOC_FREE(msg_ctx);
	TALLOC_FREE(req);

	TALLOC_FREE(frame);
}

int main(int argc, const char *argv[])
{
	const char testname[] = "test_background_send";
	bool ok;

	if (argc != 2) {
		fprintf(stderr, "usage: %s <configfile>\n", argv[0]);
		return 1;
	}

	printf("test: %s\n", testname);

	ok = lp_load_initial_only(argv[1]);
	if (!ok) {
		fprintf(stderr, "lp_load_initial_only(%s) failed\n", argv[1]);
		return 1;
	}

	test_background_send();	/* crashes on failure */

	printf("success: %s\n", testname);
	return 0;
}