mirror of
https://gitlab.freedesktop.org/libinput/libei.git
synced 2026-05-01 03:27:59 +02:00
test: add a test for the sources utility functions
Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
This commit is contained in:
parent
89408c9e2e
commit
f0a65b3a0f
2 changed files with 151 additions and 0 deletions
|
|
@ -103,6 +103,11 @@ test('iotest',
|
|||
'test/iotest.c',
|
||||
include_directories: 'src',
|
||||
dependencies: munit))
|
||||
test('sourcestest',
|
||||
executable('sourcestest',
|
||||
'test/sourcestest.c',
|
||||
include_directories: 'src',
|
||||
dependencies: [munit, dep_libutil]))
|
||||
|
||||
test('libei-unit-test',
|
||||
executable('libei-unit-test',
|
||||
|
|
|
|||
146
test/sourcestest.c
Normal file
146
test/sourcestest.c
Normal file
|
|
@ -0,0 +1,146 @@
|
|||
/*
|
||||
* Copyright © 2020 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 <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <signal.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <munit.h>
|
||||
|
||||
#include "util-macros.h"
|
||||
#include "util-mem.h"
|
||||
#include "util-sources.h"
|
||||
|
||||
DEFINE_TRIVIAL_CLEANUP_FUNC(struct sink *, sink_unref);
|
||||
#define _cleanup_sink_ _cleanup_(sink_unrefp)
|
||||
|
||||
static MunitResult
|
||||
test_sink(const MunitParameter params[], void *user_data)
|
||||
{
|
||||
struct sink *sink = sink_new();
|
||||
sink_dispatch(sink);
|
||||
sink_dispatch(sink);
|
||||
|
||||
int fd = sink_get_fd(sink);
|
||||
munit_assert_int(fd, !=, -1);
|
||||
|
||||
sink_unref(sink);
|
||||
|
||||
return MUNIT_OK;
|
||||
}
|
||||
|
||||
struct buffer {
|
||||
size_t size;
|
||||
size_t len;
|
||||
char *buffer;
|
||||
};
|
||||
|
||||
static void
|
||||
read_buffer(struct source *source, void *user_data)
|
||||
{
|
||||
struct buffer *buffer = user_data;
|
||||
size_t sz = max(buffer->size, 1024);
|
||||
|
||||
buffer->size = sz;
|
||||
buffer->buffer = xrealloc(buffer->buffer, sz);
|
||||
|
||||
int nread = read(source_get_fd(source), buffer->buffer, sz);
|
||||
munit_assert_int(nread, >=, 0);
|
||||
|
||||
buffer->len = nread;
|
||||
}
|
||||
|
||||
static MunitResult
|
||||
test_source(const MunitParameter params[], void *user_data)
|
||||
{
|
||||
_cleanup_sink_ struct sink *sink = sink_new();
|
||||
|
||||
int fd[2];
|
||||
int rc = pipe2(fd, O_CLOEXEC|O_NONBLOCK);
|
||||
munit_assert_int(rc, !=, -1);
|
||||
|
||||
struct buffer buffer = {0};
|
||||
struct source *s = source_add_autoclose(sink, fd[0], read_buffer, &buffer);
|
||||
s = source_ref(s);
|
||||
|
||||
munit_assert_int(source_get_fd(s), ==, fd[0]);
|
||||
|
||||
/* Nothing to read yet, dispatch is a noop */
|
||||
sink_dispatch(sink);
|
||||
munit_assert_int(buffer.len, ==, 0);
|
||||
|
||||
const char token[] = "foobar";
|
||||
int wrc = write(fd[1], token, sizeof(token));
|
||||
munit_assert_int(wrc, ==, sizeof(token));
|
||||
|
||||
/* haven't called dispatch yet */
|
||||
munit_assert_int(buffer.len, ==, 0);
|
||||
sink_dispatch(sink);
|
||||
munit_assert_int(buffer.len, ==, sizeof(token));
|
||||
munit_assert_string_equal(buffer.buffer, token);
|
||||
|
||||
/* multiple removals shouldn't matter */
|
||||
source_remove(s);
|
||||
source_remove(s);
|
||||
sink_dispatch(sink);
|
||||
source_remove(s);
|
||||
sink_dispatch(sink);
|
||||
|
||||
/* source pipe is already closed */
|
||||
signal(SIGPIPE, SIG_IGN);
|
||||
const char token2[] = "bazbat";
|
||||
wrc = write(fd[1], token2, sizeof(token2));
|
||||
munit_assert_int(wrc, ==, -1);
|
||||
munit_assert_int(errno, ==, EPIPE);
|
||||
|
||||
sink_dispatch(sink);
|
||||
source_unref(s);
|
||||
sink_dispatch(sink);
|
||||
|
||||
free(buffer.buffer);
|
||||
|
||||
return MUNIT_OK;
|
||||
}
|
||||
|
||||
static MunitTest iotest_tests[] = {
|
||||
{ .name = "sink", .test = test_sink},
|
||||
{ .name = "source", .test = test_source},
|
||||
};
|
||||
|
||||
static const MunitSuite iotest_suite = {
|
||||
"sources",
|
||||
iotest_tests,
|
||||
NULL,
|
||||
1,
|
||||
MUNIT_SUITE_OPTION_NONE,
|
||||
};
|
||||
|
||||
int
|
||||
main(int argc, char* argv[MUNIT_ARRAY_PARAM(argc + 1)])
|
||||
{
|
||||
return munit_suite_main(&iotest_suite, NULL, argc, argv);
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue