mirror of
https://gitlab.freedesktop.org/libinput/libei.git
synced 2026-01-06 04:50:12 +01:00
160 lines
3.9 KiB
C
160 lines
3.9 KiB
C
/*
|
|
* 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 "util-macros.h"
|
|
#include "util-mem.h"
|
|
#include "util-munit.h"
|
|
#include "util-sources.h"
|
|
|
|
DEFINE_TRIVIAL_CLEANUP_FUNC(struct sink *, sink_unref);
|
|
#define _cleanup_sink_ _cleanup_(sink_unrefp)
|
|
DEFINE_TRIVIAL_CLEANUP_FUNC(struct source *, source_unref);
|
|
#define _cleanup_source_ _cleanup_(source_unrefp)
|
|
|
|
|
|
MUNIT_TEST(test_sink)
|
|
{
|
|
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;
|
|
}
|
|
|
|
MUNIT_TEST(test_source)
|
|
{
|
|
_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_new(fd[0], read_buffer, &buffer);
|
|
|
|
munit_assert_int(source_get_fd(s), ==, fd[0]);
|
|
|
|
sink_add_source(sink, s);
|
|
|
|
/* 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 void
|
|
drain_data(struct source *source, void *user_data)
|
|
{
|
|
char buf[1024] = {0};
|
|
read(source_get_fd(source), buf, sizeof(buf));
|
|
}
|
|
|
|
MUNIT_TEST(test_source_readd)
|
|
{
|
|
_cleanup_sink_ struct sink *sink = sink_new();
|
|
|
|
int fd[2];
|
|
int rc = pipe2(fd, O_CLOEXEC|O_NONBLOCK);
|
|
munit_assert_int(rc, !=, -1);
|
|
|
|
_cleanup_source_ struct source *s = source_new(fd[0], drain_data, NULL);
|
|
sink_add_source(sink, s);
|
|
sink_dispatch(sink);
|
|
/* remove and re-add without calling dispatch */
|
|
source_remove(s);
|
|
sink_add_source(sink, s);
|
|
source_remove(s);
|
|
|
|
return MUNIT_OK;
|
|
}
|
|
|
|
int
|
|
main(int argc, char **argv)
|
|
{
|
|
return munit_tests_run(argc, argv);
|
|
}
|