summaryrefslogtreecommitdiff
path: root/unit/test-tester.c
blob: 7cdfc87b81ee57b88d7fa68c47df94e4cb7afa35 (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
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 *
 *  BlueZ - Bluetooth protocol stack for Linux
 *
 *  Copyright (C) 2022  Intel Corporation.
 *
 *
 */

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

#define _GNU_SOURCE
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <fcntl.h>

#include <glib.h>

#include "src/shared/util.h"
#include "src/shared/tester.h"

static void test_basic(const void *data)
{
	tester_test_passed();
}

static bool test_io_recv(struct io *io, void *user_data)
{
	const struct iovec *iov = user_data;
	unsigned char buf[512];
	int fd;
	ssize_t len;

	fd = io_get_fd(io);

	len = read(fd, buf, sizeof(buf));

	g_assert(len > 0);
	g_assert_cmpint(len, ==, iov->iov_len);
	g_assert(memcmp(buf, iov->iov_base, len) == 0);

	tester_test_passed();

	return false;
}

static const struct iovec iov[] = {
	IOV_DATA(0x01),
	IOV_DATA(0x01, 0x02),
};

static void test_setup_io(const void *data)
{
	struct io *io;
	ssize_t len;

	io = tester_setup_io(iov, ARRAY_SIZE(iov));
	g_assert(io);

	io_set_read_handler(io, test_io_recv, (void *)&iov[1], NULL);

	len = io_send(io, (void *)&iov[0], 1);
	g_assert_cmpint(len, ==, iov[0].iov_len);
}

static void test_io_send(const void *data)
{
	struct io *io;

	io = tester_setup_io(iov, ARRAY_SIZE(iov));
	g_assert(io);

	io_set_read_handler(io, test_io_recv, (void *)&iov[0], NULL);

	tester_io_send();
}

int main(int argc, char *argv[])
{
	tester_init(&argc, &argv);

	tester_add("/tester/basic", NULL, NULL, test_basic, NULL);
	tester_add("/tester/setup_io", NULL, NULL, test_setup_io, NULL);
	tester_add("/tester/io_send", NULL, NULL, test_io_send, NULL);

	return tester_run();
}