Compare commits
34 Commits
bb30c7d86e
...
7e8e085c97
| Author | SHA1 | Date | |
|---|---|---|---|
|
7e8e085c97
|
|||
|
782a9a5977
|
|||
|
34f86651f6
|
|||
|
5dec46df2c
|
|||
|
1b9d89cab3
|
|||
|
a3ad5e7751
|
|||
|
960420df3e
|
|||
|
d71c47f8ce
|
|||
|
425ea57b17
|
|||
|
8822d06091
|
|||
|
9639777814
|
|||
|
929229a1d7
|
|||
|
53bcebc2f0
|
|||
|
b08cf6c29f
|
|||
|
69101eb155
|
|||
|
9d14562f7e
|
|||
|
9b72304963
|
|||
|
1cd9ba8d97
|
|||
|
7e5b6c5343
|
|||
|
c2c5031538
|
|||
|
df3f53bd5c
|
|||
|
e029aae1d3
|
|||
|
b9457c321f
|
|||
|
2201becca4
|
|||
|
7023c51347
|
|||
|
d21f8466b5
|
|||
|
7f919025ee
|
|||
|
1a76b2032e
|
|||
|
722ef65c1f
|
|||
|
317dfcb6e2
|
|||
|
bca7167d03
|
|||
|
3e4e4e5103
|
|||
|
9494e8e2af
|
|||
|
8ffe20c0e8
|
32
.clang-format
Normal file
32
.clang-format
Normal file
@@ -0,0 +1,32 @@
|
||||
# clang-format is fairly limited, and these rules are approximate:
|
||||
# - array initializers can get terribly mangled with clang-format 12.0,
|
||||
# - sometimes it still aligns with space characters,
|
||||
# - struct name NL { NL ... NL } NL name; is unachievable.
|
||||
BasedOnStyle: GNU
|
||||
ColumnLimit: 80
|
||||
IndentWidth: 4
|
||||
TabWidth: 4
|
||||
UseTab: ForContinuationAndIndentation
|
||||
BreakBeforeBraces: Allman
|
||||
SpaceAfterCStyleCast: true
|
||||
AlignAfterOpenBracket: DontAlign
|
||||
AlignOperands: DontAlign
|
||||
AlignConsecutiveMacros: Consecutive
|
||||
AllowAllArgumentsOnNextLine: false
|
||||
AllowAllParametersOfDeclarationOnNextLine: false
|
||||
IndentGotoLabels: false
|
||||
|
||||
# IncludeCategories has some potential, but it may also break the build.
|
||||
# Note that the documentation says the value should be "Never".
|
||||
SortIncludes: false
|
||||
|
||||
# This is a compromise, it generally works out aesthetically better.
|
||||
BinPackArguments: false
|
||||
|
||||
# Unfortunately, this can't be told to align to column 40 or so.
|
||||
SpacesBeforeTrailingComments: 2
|
||||
|
||||
# liberty-specific macro body wrappers.
|
||||
MacroBlockBegin: "BLOCK_START"
|
||||
MacroBlockEnd: "BLOCK_END"
|
||||
ForEachMacros: ["LIST_FOR_EACH"]
|
||||
2
.gitignore
vendored
2
.gitignore
vendored
@@ -7,3 +7,5 @@
|
||||
/liberty.files
|
||||
/liberty.creator*
|
||||
/liberty.includes
|
||||
/liberty.cflags
|
||||
/liberty.cxxflags
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
project (liberty C)
|
||||
cmake_minimum_required (VERSION 2.8.5)
|
||||
cmake_minimum_required (VERSION 2.8.12)
|
||||
|
||||
# Moar warnings
|
||||
if ("${CMAKE_C_COMPILER_ID}" MATCHES "GNU" OR CMAKE_COMPILER_IS_GNUCC)
|
||||
# -Wunused-function is pretty annoying here, as everything is static
|
||||
set (wdisabled "-Wno-unused-function -Wno-implicit-fallthrough")
|
||||
set (wdisabled "-Wno-unused-function")
|
||||
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99 -Wall -Wextra ${wdisabled}")
|
||||
endif ("${CMAKE_C_COMPILER_ID}" MATCHES "GNU" OR CMAKE_COMPILER_IS_GNUCC)
|
||||
endif ()
|
||||
|
||||
# Dependencies
|
||||
set (CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
|
||||
@@ -16,11 +16,9 @@ find_package (PkgConfig REQUIRED)
|
||||
pkg_check_modules (libssl REQUIRED libssl libcrypto)
|
||||
|
||||
if ("${CMAKE_SYSTEM_NAME}" MATCHES "BSD")
|
||||
include_directories (/usr/local/include)
|
||||
link_directories (/usr/local/lib)
|
||||
# Our POSIX version macros make these undefined
|
||||
add_definitions (-D__BSD_VISIBLE=1 -D_BSD_SOURCE=1)
|
||||
endif ("${CMAKE_SYSTEM_NAME}" MATCHES "BSD")
|
||||
endif ()
|
||||
|
||||
set (common_libraries ${libssl_LIBRARIES})
|
||||
include_directories (${libssl_INCLUDE_DIRS})
|
||||
@@ -32,15 +30,25 @@ foreach (extra iconv rt)
|
||||
find_library (extra_lib_${extra} ${extra})
|
||||
if (extra_lib_${extra})
|
||||
list (APPEND common_libraries ${extra})
|
||||
endif (extra_lib_${extra})
|
||||
endforeach (extra)
|
||||
endif ()
|
||||
endforeach ()
|
||||
|
||||
# Build some unit tests
|
||||
include_directories (${PROJECT_SOURCE_DIR})
|
||||
enable_testing ()
|
||||
foreach (name liberty proto)
|
||||
set (tests liberty proto)
|
||||
|
||||
pkg_check_modules (libpulse libpulse)
|
||||
if (libpulse_FOUND)
|
||||
list (APPEND tests pulse)
|
||||
list (APPEND common_libraries ${libpulse_LIBRARIES})
|
||||
include_directories (${libpulse_INCLUDE_DIRS})
|
||||
link_directories (${libpulse_LIBRARY_DIRS})
|
||||
endif ()
|
||||
|
||||
foreach (name ${tests})
|
||||
add_executable (test-${name} tests/${name}.c ${common_sources})
|
||||
add_threads (test-${name})
|
||||
target_link_libraries (test-${name} ${common_libraries})
|
||||
add_test (NAME test-${name} COMMAND test-${name})
|
||||
endforeach (name)
|
||||
endforeach ()
|
||||
|
||||
2
LICENSE
2
LICENSE
@@ -1,4 +1,4 @@
|
||||
Copyright (c) 2014 - 2018, Přemysl Janouch <p@janouch.name>
|
||||
Copyright (c) 2014 - 2020, Přemysl Eric Janouch <p@janouch.name>
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for any
|
||||
purpose with or without fee is hereby granted.
|
||||
|
||||
@@ -1,17 +1,17 @@
|
||||
# Public Domain
|
||||
|
||||
find_package (PkgConfig REQUIRED)
|
||||
pkg_check_modules (NCURSESW QUIET ncursesw)
|
||||
pkg_check_modules (Ncursesw QUIET ncursesw)
|
||||
|
||||
# OpenBSD doesn't provide a pkg-config file
|
||||
set (required_vars NCURSESW_LIBRARIES)
|
||||
if (NOT NCURSESW_FOUND)
|
||||
find_library (NCURSESW_LIBRARIES NAMES ncursesw)
|
||||
find_path (NCURSESW_INCLUDE_DIRS ncurses.h)
|
||||
list (APPEND required_vars NCURSESW_INCLUDE_DIRS)
|
||||
endif (NOT NCURSESW_FOUND)
|
||||
set (required_vars Ncursesw_LIBRARIES)
|
||||
if (NOT Ncursesw_FOUND)
|
||||
find_library (Ncursesw_LIBRARIES NAMES ncursesw)
|
||||
find_path (Ncursesw_INCLUDE_DIRS ncurses.h)
|
||||
list (APPEND required_vars Ncursesw_INCLUDE_DIRS)
|
||||
endif (NOT Ncursesw_FOUND)
|
||||
|
||||
include (FindPackageHandleStandardArgs)
|
||||
FIND_PACKAGE_HANDLE_STANDARD_ARGS (NCURSESW DEFAULT_MSG ${required_vars})
|
||||
FIND_PACKAGE_HANDLE_STANDARD_ARGS (Ncursesw DEFAULT_MSG ${required_vars})
|
||||
|
||||
mark_as_advanced (NCURSESW_LIBRARIES NCURSESW_INCLUDE_DIRS)
|
||||
mark_as_advanced (Ncursesw_LIBRARIES Ncursesw_INCLUDE_DIRS)
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
# Public Domain
|
||||
|
||||
find_path (UNISTRING_INCLUDE_DIRS unistr.h)
|
||||
find_library (UNISTRING_LIBRARIES NAMES unistring libunistring)
|
||||
find_path (Unistring_INCLUDE_DIRS unistr.h)
|
||||
find_library (Unistring_LIBRARIES NAMES unistring libunistring)
|
||||
|
||||
include (FindPackageHandleStandardArgs)
|
||||
FIND_PACKAGE_HANDLE_STANDARD_ARGS (UNISTRING DEFAULT_MSG
|
||||
UNISTRING_INCLUDE_DIRS UNISTRING_LIBRARIES)
|
||||
FIND_PACKAGE_HANDLE_STANDARD_ARGS (Unistring DEFAULT_MSG
|
||||
Unistring_INCLUDE_DIRS Unistring_LIBRARIES)
|
||||
|
||||
mark_as_advanced (UNISTRING_LIBRARIES UNISTRING_INCLUDE_DIRS)
|
||||
mark_as_advanced (Unistring_LIBRARIES Unistring_INCLUDE_DIRS)
|
||||
|
||||
18
fuzz
Executable file
18
fuzz
Executable file
@@ -0,0 +1,18 @@
|
||||
#!/bin/sh
|
||||
# I'm not sure how to make maximum use of this invention
|
||||
# Make sure to have llvm-symbolizer installed
|
||||
clang -g -fsanitize=address,undefined,fuzzer -fno-sanitize-recover=all \
|
||||
tests/fuzz.c -o fuzz-executor
|
||||
|
||||
fuzz () {
|
||||
echo "`tput bold`-- Fuzzing $1`tput sgr0`"
|
||||
mkdir -p /tmp/corpus-$1
|
||||
./fuzz-executor -test=$1 -artifact_prefix=$1- \
|
||||
-max_total_time=600 -timeout=1 /tmp/corpus-$1
|
||||
}
|
||||
|
||||
if [ $# -gt 0 ]; then
|
||||
for test in "$@"; do fuzz $test; done
|
||||
else
|
||||
for test in $(./fuzz-executor); do fuzz $test; done
|
||||
fi
|
||||
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
* liberty-proto.c: the ultimate C unlibrary: protocols
|
||||
*
|
||||
* Copyright (c) 2014 - 2016, Přemysl Janouch <p@janouch.name>
|
||||
* Copyright (c) 2014 - 2016, Přemysl Eric Janouch <p@janouch.name>
|
||||
*
|
||||
* Permission to use, copy, modify, and/or distribute this software for any
|
||||
* purpose with or without fee is hereby granted.
|
||||
@@ -148,7 +148,7 @@ irc_free_message (struct irc_message *msg)
|
||||
|
||||
static void
|
||||
irc_process_buffer (struct str *buf,
|
||||
void (*callback)(const struct irc_message *, const char *, void *),
|
||||
void (*callback) (const struct irc_message *, const char *, void *),
|
||||
void *user_data)
|
||||
{
|
||||
char *start = buf->str, *end = start + buf->len;
|
||||
@@ -213,6 +213,7 @@ irc_fnmatch (const char *pattern, const char *string)
|
||||
char x_pattern[pattern_size], x_string[string_size];
|
||||
irc_strxfrm (x_pattern, pattern, pattern_size);
|
||||
irc_strxfrm (x_string, string, string_size);
|
||||
// FIXME: this supports [], which is not mentioned in RFC 2812
|
||||
return fnmatch (x_pattern, x_string, 0);
|
||||
}
|
||||
|
||||
@@ -663,10 +664,11 @@ scgi_parser_push (struct scgi_parser *self,
|
||||
if (digit == ':')
|
||||
{
|
||||
self->state = SCGI_READING_NAME;
|
||||
str_remove_slice (&self->input, 0, 1);
|
||||
break;
|
||||
}
|
||||
|
||||
if (digit < '0' || digit >= '9')
|
||||
if (digit < '0' || digit > '9')
|
||||
return error_set (e, "invalid header netstring");
|
||||
|
||||
size_t new_len = self->headers_len * 10 + (digit - '0');
|
||||
@@ -699,6 +701,7 @@ scgi_parser_push (struct scgi_parser *self,
|
||||
self->state = SCGI_READING_VALUE;
|
||||
|
||||
str_remove_slice (&self->input, 0, 1);
|
||||
self->headers_len--;
|
||||
break;
|
||||
}
|
||||
case SCGI_READING_VALUE:
|
||||
@@ -727,6 +730,7 @@ scgi_parser_push (struct scgi_parser *self,
|
||||
}
|
||||
|
||||
str_remove_slice (&self->input, 0, 1);
|
||||
self->headers_len--;
|
||||
break;
|
||||
}
|
||||
case SCGI_READING_CONTENT:
|
||||
@@ -791,7 +795,8 @@ enum fcgi_protocol_status
|
||||
|
||||
struct fcgi_parser;
|
||||
|
||||
typedef void (*fcgi_message_fn)
|
||||
/// Message handler, returns false if further processing should be stopped
|
||||
typedef bool (*fcgi_message_fn)
|
||||
(const struct fcgi_parser *parser, void *user_data);
|
||||
|
||||
enum fcgi_parser_state
|
||||
@@ -853,7 +858,7 @@ fcgi_parser_unpack_header (struct fcgi_parser *self)
|
||||
str_remove_slice (&self->input, 0, unpacker.offset);
|
||||
}
|
||||
|
||||
static void
|
||||
static bool
|
||||
fcgi_parser_push (struct fcgi_parser *self, const void *data, size_t len)
|
||||
{
|
||||
// This could be made considerably faster for high-throughput applications
|
||||
@@ -865,14 +870,14 @@ fcgi_parser_push (struct fcgi_parser *self, const void *data, size_t len)
|
||||
{
|
||||
case FCGI_READING_HEADER:
|
||||
if (self->input.len < FCGI_HEADER_LEN)
|
||||
return;
|
||||
return true;
|
||||
|
||||
fcgi_parser_unpack_header (self);
|
||||
self->state = FCGI_READING_CONTENT;
|
||||
break;
|
||||
case FCGI_READING_CONTENT:
|
||||
if (self->input.len < self->content_length)
|
||||
return;
|
||||
return true;
|
||||
|
||||
// Move an appropriate part of the input buffer to the content buffer
|
||||
str_reset (&self->content);
|
||||
@@ -882,10 +887,11 @@ fcgi_parser_push (struct fcgi_parser *self, const void *data, size_t len)
|
||||
break;
|
||||
case FCGI_READING_PADDING:
|
||||
if (self->input.len < self->padding_length)
|
||||
return;
|
||||
return true;
|
||||
|
||||
// Call the callback to further process the message
|
||||
self->on_message (self, self->user_data);
|
||||
if (!self->on_message (self, self->user_data))
|
||||
return false;
|
||||
|
||||
// Remove the padding from the input buffer
|
||||
str_remove_slice (&self->input, 0, self->padding_length);
|
||||
@@ -1188,8 +1194,10 @@ ws_parser_unmask (char *payload, uint64_t len, uint32_t mask)
|
||||
{
|
||||
case 3:
|
||||
payload[end + 2] ^= (mask >> 8) & 0xFF;
|
||||
// Fall-through
|
||||
case 2:
|
||||
payload[end + 1] ^= (mask >> 16) & 0xFF;
|
||||
// Fall-through
|
||||
case 1:
|
||||
payload[end ] ^= (mask >> 24) & 0xFF;
|
||||
}
|
||||
@@ -1313,7 +1321,6 @@ enum mpd_subsystem
|
||||
#define XX(a, b, c) MPD_SUBSYSTEM_ ## a = (1 << b),
|
||||
MPD_SUBSYSTEM_TABLE (XX)
|
||||
#undef XX
|
||||
MPD_SUBSYSTEM_MAX
|
||||
};
|
||||
|
||||
static const char *mpd_subsystem_names[] =
|
||||
@@ -1559,13 +1566,12 @@ mpd_client_parse_line (struct mpd_client *self, const char *line)
|
||||
if (!strcmp (line, "list_OK"))
|
||||
strv_append_owned (&self->data, NULL);
|
||||
else if (mpd_client_parse_response (line, &response))
|
||||
{
|
||||
mpd_client_dispatch (self, &response);
|
||||
free (response.current_command);
|
||||
free (response.message_text);
|
||||
}
|
||||
else
|
||||
strv_append (&self->data, line);
|
||||
|
||||
free (response.current_command);
|
||||
free (response.message_text);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -1738,6 +1744,8 @@ mpd_client_send_command (struct mpd_client *self, const char *command, ...)
|
||||
|
||||
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||
|
||||
/// "On success for all commands, OK is returned. If a command fails, no more
|
||||
/// commands are executed and the appropriate ACK error is returned"
|
||||
static void
|
||||
mpd_client_list_begin (struct mpd_client *self)
|
||||
{
|
||||
|
||||
348
liberty-pulse.c
Normal file
348
liberty-pulse.c
Normal file
@@ -0,0 +1,348 @@
|
||||
/*
|
||||
* liberty-pulse.c: PulseAudio mainloop abstraction
|
||||
*
|
||||
* Copyright (c) 2016 - 2021, Přemysl Eric Janouch <p@janouch.name>
|
||||
*
|
||||
* Permission to use, copy, modify, and/or distribute this software for any
|
||||
* purpose with or without fee is hereby granted.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
||||
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
||||
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
||||
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*
|
||||
*/
|
||||
|
||||
#include <pulse/mainloop.h>
|
||||
|
||||
// --- PulseAudio mainloop abstraction -----------------------------------------
|
||||
|
||||
struct pa_io_event
|
||||
{
|
||||
LIST_HEADER (pa_io_event)
|
||||
|
||||
pa_mainloop_api *api; ///< Parent structure
|
||||
struct poller_fd fd; ///< Underlying FD event
|
||||
|
||||
pa_io_event_cb_t dispatch; ///< Dispatcher
|
||||
pa_io_event_destroy_cb_t free; ///< Destroyer
|
||||
void *user_data; ///< User data
|
||||
};
|
||||
|
||||
struct pa_time_event
|
||||
{
|
||||
LIST_HEADER (pa_time_event)
|
||||
|
||||
pa_mainloop_api *api; ///< Parent structure
|
||||
struct poller_timer timer; ///< Underlying timer event
|
||||
|
||||
pa_time_event_cb_t dispatch; ///< Dispatcher
|
||||
pa_time_event_destroy_cb_t free; ///< Destroyer
|
||||
void *user_data; ///< User data
|
||||
};
|
||||
|
||||
struct pa_defer_event
|
||||
{
|
||||
LIST_HEADER (pa_defer_event)
|
||||
|
||||
pa_mainloop_api *api; ///< Parent structure
|
||||
struct poller_idle idle; ///< Underlying idle event
|
||||
|
||||
pa_defer_event_cb_t dispatch; ///< Dispatcher
|
||||
pa_defer_event_destroy_cb_t free; ///< Destroyer
|
||||
void *user_data; ///< User data
|
||||
};
|
||||
|
||||
struct poller_pa
|
||||
{
|
||||
struct poller *poller; ///< The underlying event loop
|
||||
pa_io_event *io_list; ///< I/O events
|
||||
pa_time_event *time_list; ///< Timer events
|
||||
pa_defer_event *defer_list; ///< Deferred events
|
||||
};
|
||||
|
||||
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||
|
||||
static short
|
||||
poller_pa_flags_to_events (pa_io_event_flags_t flags)
|
||||
{
|
||||
short result = 0;
|
||||
if (flags & PA_IO_EVENT_ERROR) result |= POLLERR;
|
||||
if (flags & PA_IO_EVENT_HANGUP) result |= POLLHUP;
|
||||
if (flags & PA_IO_EVENT_INPUT) result |= POLLIN;
|
||||
if (flags & PA_IO_EVENT_OUTPUT) result |= POLLOUT;
|
||||
return result;
|
||||
}
|
||||
|
||||
static pa_io_event_flags_t
|
||||
poller_pa_events_to_flags (short events)
|
||||
{
|
||||
pa_io_event_flags_t result = 0;
|
||||
if (events & POLLERR) result |= PA_IO_EVENT_ERROR;
|
||||
if (events & POLLHUP) result |= PA_IO_EVENT_HANGUP;
|
||||
if (events & POLLIN) result |= PA_IO_EVENT_INPUT;
|
||||
if (events & POLLOUT) result |= PA_IO_EVENT_OUTPUT;
|
||||
return result;
|
||||
}
|
||||
|
||||
static struct timeval
|
||||
poller_pa_get_current_time (void)
|
||||
{
|
||||
struct timeval tv;
|
||||
#ifdef _POSIX_TIMERS
|
||||
struct timespec tp;
|
||||
hard_assert (clock_gettime (CLOCK_REALTIME, &tp) != -1);
|
||||
tv.tv_sec = tp.tv_sec;
|
||||
tv.tv_usec = tp.tv_nsec / 1000;
|
||||
#else
|
||||
gettimeofday (&tv, NULL);
|
||||
#endif
|
||||
return tv;
|
||||
}
|
||||
|
||||
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||
|
||||
static void
|
||||
poller_pa_io_dispatcher (const struct pollfd *pfd, void *user_data)
|
||||
{
|
||||
pa_io_event *self = user_data;
|
||||
self->dispatch (self->api, self,
|
||||
pfd->fd, poller_pa_events_to_flags (pfd->revents), self->user_data);
|
||||
}
|
||||
|
||||
static void
|
||||
poller_pa_io_enable (pa_io_event *self, pa_io_event_flags_t events)
|
||||
{
|
||||
struct poller_fd *fd = &self->fd;
|
||||
if (events)
|
||||
poller_fd_set (fd, poller_pa_flags_to_events (events));
|
||||
else
|
||||
poller_fd_reset (fd);
|
||||
}
|
||||
|
||||
static pa_io_event *
|
||||
poller_pa_io_new (pa_mainloop_api *api, int fd_, pa_io_event_flags_t events,
|
||||
pa_io_event_cb_t cb, void *userdata)
|
||||
{
|
||||
pa_io_event *self = xcalloc (1, sizeof *self);
|
||||
self->api = api;
|
||||
self->dispatch = cb;
|
||||
self->user_data = userdata;
|
||||
|
||||
struct poller_pa *data = api->userdata;
|
||||
self->fd = poller_fd_make (data->poller, fd_);
|
||||
self->fd.user_data = self;
|
||||
self->fd.dispatcher = poller_pa_io_dispatcher;
|
||||
|
||||
// FIXME: under x2go PA tries to register twice for the same FD,
|
||||
// which fails with our curent poller implementation;
|
||||
// we could maintain a list of { poller_fd, listeners } structures;
|
||||
// or maybe we're doing something wrong, which is yet to be determined
|
||||
poller_pa_io_enable (self, events);
|
||||
LIST_PREPEND (data->io_list, self);
|
||||
return self;
|
||||
}
|
||||
|
||||
static void
|
||||
poller_pa_io_free (pa_io_event *self)
|
||||
{
|
||||
if (self->free)
|
||||
self->free (self->api, self, self->user_data);
|
||||
|
||||
struct poller_pa *data = self->api->userdata;
|
||||
poller_fd_reset (&self->fd);
|
||||
LIST_UNLINK (data->io_list, self);
|
||||
free (self);
|
||||
}
|
||||
|
||||
static void
|
||||
poller_pa_io_set_destroy (pa_io_event *self, pa_io_event_destroy_cb_t cb)
|
||||
{
|
||||
self->free = cb;
|
||||
}
|
||||
|
||||
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||
|
||||
static void
|
||||
poller_pa_time_dispatcher (void *user_data)
|
||||
{
|
||||
pa_time_event *self = user_data;
|
||||
// XXX: the meaning of the time argument is undocumented,
|
||||
// so let's just put current Unix time in there
|
||||
struct timeval now = poller_pa_get_current_time ();
|
||||
self->dispatch (self->api, self, &now, self->user_data);
|
||||
}
|
||||
|
||||
static void
|
||||
poller_pa_time_restart (pa_time_event *self, const struct timeval *tv)
|
||||
{
|
||||
struct poller_timer *timer = &self->timer;
|
||||
if (tv)
|
||||
{
|
||||
struct timeval now = poller_pa_get_current_time ();
|
||||
poller_timer_set (timer,
|
||||
(tv->tv_sec - now.tv_sec) * 1000 +
|
||||
(tv->tv_usec - now.tv_usec) / 1000);
|
||||
}
|
||||
else
|
||||
poller_timer_reset (timer);
|
||||
}
|
||||
|
||||
static pa_time_event *
|
||||
poller_pa_time_new (pa_mainloop_api *api, const struct timeval *tv,
|
||||
pa_time_event_cb_t cb, void *userdata)
|
||||
{
|
||||
pa_time_event *self = xcalloc (1, sizeof *self);
|
||||
self->api = api;
|
||||
self->dispatch = cb;
|
||||
self->user_data = userdata;
|
||||
|
||||
struct poller_pa *data = api->userdata;
|
||||
self->timer = poller_timer_make (data->poller);
|
||||
self->timer.user_data = self;
|
||||
self->timer.dispatcher = poller_pa_time_dispatcher;
|
||||
|
||||
poller_pa_time_restart (self, tv);
|
||||
LIST_PREPEND (data->time_list, self);
|
||||
return self;
|
||||
}
|
||||
|
||||
static void
|
||||
poller_pa_time_free (pa_time_event *self)
|
||||
{
|
||||
if (self->free)
|
||||
self->free (self->api, self, self->user_data);
|
||||
|
||||
struct poller_pa *data = self->api->userdata;
|
||||
poller_timer_reset (&self->timer);
|
||||
LIST_UNLINK (data->time_list, self);
|
||||
free (self);
|
||||
}
|
||||
|
||||
static void
|
||||
poller_pa_time_set_destroy (pa_time_event *self, pa_time_event_destroy_cb_t cb)
|
||||
{
|
||||
self->free = cb;
|
||||
}
|
||||
|
||||
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||
|
||||
static void
|
||||
poller_pa_defer_dispatcher (void *user_data)
|
||||
{
|
||||
pa_defer_event *self = user_data;
|
||||
self->dispatch (self->api, self, self->user_data);
|
||||
}
|
||||
|
||||
static pa_defer_event *
|
||||
poller_pa_defer_new (pa_mainloop_api *api,
|
||||
pa_defer_event_cb_t cb, void *userdata)
|
||||
{
|
||||
pa_defer_event *self = xcalloc (1, sizeof *self);
|
||||
self->api = api;
|
||||
self->dispatch = cb;
|
||||
self->user_data = userdata;
|
||||
|
||||
struct poller_pa *data = api->userdata;
|
||||
self->idle = poller_idle_make (data->poller);
|
||||
self->idle.user_data = self;
|
||||
self->idle.dispatcher = poller_pa_defer_dispatcher;
|
||||
|
||||
poller_idle_set (&self->idle);
|
||||
LIST_PREPEND (data->defer_list, self);
|
||||
return self;
|
||||
}
|
||||
|
||||
static void
|
||||
poller_pa_defer_enable (pa_defer_event *self, int enable)
|
||||
{
|
||||
struct poller_idle *idle = &self->idle;
|
||||
if (enable)
|
||||
poller_idle_set (idle);
|
||||
else
|
||||
poller_idle_reset (idle);
|
||||
}
|
||||
|
||||
static void
|
||||
poller_pa_defer_free (pa_defer_event *self)
|
||||
{
|
||||
if (self->free)
|
||||
self->free (self->api, self, self->user_data);
|
||||
|
||||
struct poller_pa *data = self->api->userdata;
|
||||
poller_idle_reset (&self->idle);
|
||||
LIST_UNLINK (data->defer_list, self);
|
||||
free (self);
|
||||
}
|
||||
|
||||
static void
|
||||
poller_pa_defer_set_destroy (pa_defer_event *self,
|
||||
pa_defer_event_destroy_cb_t cb)
|
||||
{
|
||||
self->free = cb;
|
||||
}
|
||||
|
||||
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||
|
||||
static void
|
||||
poller_pa_quit (pa_mainloop_api *api, int retval)
|
||||
{
|
||||
(void) api;
|
||||
(void) retval;
|
||||
|
||||
// This is not called from within libpulse
|
||||
hard_assert (!"quitting the libpulse event loop is unimplemented");
|
||||
}
|
||||
|
||||
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||
|
||||
static struct pa_mainloop_api g_poller_pa_template =
|
||||
{
|
||||
.io_new = poller_pa_io_new,
|
||||
.io_enable = poller_pa_io_enable,
|
||||
.io_free = poller_pa_io_free,
|
||||
.io_set_destroy = poller_pa_io_set_destroy,
|
||||
|
||||
.time_new = poller_pa_time_new,
|
||||
.time_restart = poller_pa_time_restart,
|
||||
.time_free = poller_pa_time_free,
|
||||
.time_set_destroy = poller_pa_time_set_destroy,
|
||||
|
||||
.defer_new = poller_pa_defer_new,
|
||||
.defer_enable = poller_pa_defer_enable,
|
||||
.defer_free = poller_pa_defer_free,
|
||||
.defer_set_destroy = poller_pa_defer_set_destroy,
|
||||
|
||||
.quit = poller_pa_quit,
|
||||
};
|
||||
|
||||
static struct pa_mainloop_api *
|
||||
poller_pa_new (struct poller *self)
|
||||
{
|
||||
struct poller_pa *data = xcalloc (1, sizeof *data);
|
||||
data->poller = self;
|
||||
|
||||
struct pa_mainloop_api *api = xmalloc (sizeof *api);
|
||||
*api = g_poller_pa_template;
|
||||
api->userdata = data;
|
||||
return api;
|
||||
}
|
||||
|
||||
static void
|
||||
poller_pa_destroy (struct pa_mainloop_api *api)
|
||||
{
|
||||
struct poller_pa *data = api->userdata;
|
||||
|
||||
LIST_FOR_EACH (pa_io_event, iter, data->io_list)
|
||||
poller_pa_io_free (iter);
|
||||
LIST_FOR_EACH (pa_time_event, iter, data->time_list)
|
||||
poller_pa_time_free (iter);
|
||||
LIST_FOR_EACH (pa_defer_event, iter, data->defer_list)
|
||||
poller_pa_defer_free (iter);
|
||||
|
||||
free (data);
|
||||
free (api);
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
* liberty-tui.c: the ultimate C unlibrary: TUI
|
||||
*
|
||||
* Copyright (c) 2016 - 2017, Přemysl Janouch <p@janouch.name>
|
||||
* Copyright (c) 2016 - 2017, Přemysl Eric Janouch <p@janouch.name>
|
||||
*
|
||||
* Permission to use, copy, modify, and/or distribute this software for any
|
||||
* purpose with or without fee is hereby granted.
|
||||
|
||||
207
liberty.c
207
liberty.c
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
* liberty.c: the ultimate C unlibrary
|
||||
*
|
||||
* Copyright (c) 2014 - 2018, Přemysl Janouch <p@janouch.name>
|
||||
* Copyright (c) 2014 - 2020, Přemysl Eric Janouch <p@janouch.name>
|
||||
*
|
||||
* Permission to use, copy, modify, and/or distribute this software for any
|
||||
* purpose with or without fee is hereby granted.
|
||||
@@ -37,6 +37,7 @@
|
||||
#include <sys/wait.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/time.h>
|
||||
#include <sys/uio.h>
|
||||
#include <fcntl.h>
|
||||
#include <poll.h>
|
||||
#include <signal.h>
|
||||
@@ -116,6 +117,9 @@ extern char **environ;
|
||||
#define CONTAINER_OF(pointer, type, member) \
|
||||
((type *) ((char *) pointer - offsetof (type, member)))
|
||||
|
||||
char *liberty = "They who can give up essential liberty to obtain a little "
|
||||
"temporary safety deserve neither liberty nor safety.";
|
||||
|
||||
// --- Logging -----------------------------------------------------------------
|
||||
|
||||
static void
|
||||
@@ -727,6 +731,21 @@ set_blocking (int fd, bool blocking)
|
||||
return prev;
|
||||
}
|
||||
|
||||
static bool
|
||||
xwrite (int fd, const char *data, size_t len, struct error **e)
|
||||
{
|
||||
size_t written = 0;
|
||||
while (written < len)
|
||||
{
|
||||
ssize_t res = write (fd, data + written, len - written);
|
||||
if (res >= 0)
|
||||
written += res;
|
||||
else if (errno != EINTR)
|
||||
return error_set (e, "%s", strerror (errno));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static void
|
||||
xclose (int fd)
|
||||
{
|
||||
@@ -1092,7 +1111,7 @@ struct async
|
||||
LIST_HEADER (struct async)
|
||||
struct async_manager *manager; ///< Our manager object
|
||||
|
||||
// "cancelled" may not be accesed or modified by the worker thread
|
||||
// "cancelled" may not be accessed or modified by the worker thread
|
||||
|
||||
pthread_t worker; ///< Worker thread ID
|
||||
bool started; ///< Worker thread ID is valid
|
||||
@@ -1353,6 +1372,7 @@ struct poller_idle
|
||||
|
||||
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||
|
||||
// The heap could definitely be made faster but we'll prefer simplicity
|
||||
struct poller_timers
|
||||
{
|
||||
struct poller_timer **heap; ///< Min-heap of timers
|
||||
@@ -1729,10 +1749,9 @@ poller_run (struct poller *self)
|
||||
self->revents_len = 0;
|
||||
}
|
||||
|
||||
#elif defined (BSD)
|
||||
|
||||
// Mac OS X's kqueue is fatally broken, or so I've been told; leaving it out.
|
||||
// Otherwise this is sort of similar to the epoll version.
|
||||
// Sort of similar to the epoll version. Let's hope Darwin isn't broken,
|
||||
// that'd mean reimplementing this in terms of select() just because of Crapple.
|
||||
#elif defined (BSD) || defined (__APPLE__)
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/event.h>
|
||||
@@ -2668,6 +2687,12 @@ strncasecmp_ascii (const char *a, const char *b, size_t n)
|
||||
return 0;
|
||||
}
|
||||
|
||||
static bool
|
||||
iscntrl_ascii (int c)
|
||||
{
|
||||
return (c >= 0 && c < 32) || c == 0x7f;
|
||||
}
|
||||
|
||||
static bool
|
||||
isalpha_ascii (int c)
|
||||
{
|
||||
@@ -2696,63 +2721,67 @@ isspace_ascii (int c)
|
||||
|
||||
// --- UTF-8 -------------------------------------------------------------------
|
||||
|
||||
/// Return a pointer to the next UTF-8 character, or NULL on error
|
||||
static const char *
|
||||
utf8_next (const char *s, size_t len, int32_t *codepoint)
|
||||
/// Return the value of the UTF-8 character at `*s` and advance the pointer
|
||||
/// to the next one. Returns -2 if there is only a partial but possibly valid
|
||||
/// character sequence, or -1 on other errors. Either way, `*s` is untouched.
|
||||
static int32_t
|
||||
utf8_decode (const char **s, size_t len)
|
||||
{
|
||||
// End of string, we go no further
|
||||
if (!len)
|
||||
return NULL;
|
||||
return -1;
|
||||
|
||||
// Find out how long the sequence is (0 for ASCII)
|
||||
unsigned mask = 0x80;
|
||||
unsigned sequence_len = 0;
|
||||
|
||||
const uint8_t *p = (const uint8_t *) s;
|
||||
const uint8_t *p = (const uint8_t *) *s, *end = p + len;
|
||||
while ((*p & mask) == mask)
|
||||
{
|
||||
// Invalid start of sequence
|
||||
if (mask == 0xFE)
|
||||
return NULL;
|
||||
return -1;
|
||||
|
||||
mask |= mask >> 1;
|
||||
sequence_len++;
|
||||
}
|
||||
|
||||
// In the middle of a character or the input is too short
|
||||
if (sequence_len == 1 || sequence_len > len)
|
||||
return NULL;
|
||||
// In the middle of a character
|
||||
// or an overlong sequence (subset, possibly MUTF-8, not supported)
|
||||
if (sequence_len == 1 || *p == 0xC0 || *p == 0xC1)
|
||||
return -1;
|
||||
|
||||
// Check the rest of the sequence
|
||||
uint32_t cp = *p++ & ~mask;
|
||||
while (sequence_len && --sequence_len)
|
||||
{
|
||||
if (p == end)
|
||||
return -2;
|
||||
if ((*p & 0xC0) != 0x80)
|
||||
return NULL;
|
||||
return -1;
|
||||
cp = cp << 6 | (*p++ & 0x3F);
|
||||
}
|
||||
if (codepoint)
|
||||
*codepoint = cp;
|
||||
return (const char *) p;
|
||||
*s = (const char *) p;
|
||||
return cp;
|
||||
}
|
||||
|
||||
static inline bool
|
||||
utf8_validate_cp (int32_t cp)
|
||||
{
|
||||
// RFC 3629, CESU-8 not allowed
|
||||
return cp >= 0 && cp <= 0x10FFFF && (cp < 0xD800 || cp > 0xDFFF);
|
||||
}
|
||||
|
||||
/// Very rough UTF-8 validation, just makes sure codepoints can be iterated
|
||||
static bool
|
||||
utf8_validate (const char *s, size_t len)
|
||||
{
|
||||
const char *next;
|
||||
while (len)
|
||||
{
|
||||
int32_t codepoint;
|
||||
// TODO: better validations
|
||||
if (!(next = utf8_next (s, len, &codepoint))
|
||||
|| codepoint > 0x10FFFF)
|
||||
return false;
|
||||
|
||||
len -= next - s;
|
||||
s = next;
|
||||
}
|
||||
return true;
|
||||
const char *end = s + len;
|
||||
int32_t codepoint;
|
||||
while ((codepoint = utf8_decode (&s, end - s)) >= 0
|
||||
&& utf8_validate_cp (codepoint))
|
||||
;
|
||||
return s == end;
|
||||
}
|
||||
|
||||
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||
@@ -2776,12 +2805,12 @@ utf8_iter_next (struct utf8_iter *self, size_t *len)
|
||||
return -1;
|
||||
|
||||
const char *old = self->s;
|
||||
int32_t codepoint;
|
||||
if (!soft_assert ((self->s = utf8_next (old, self->len, &codepoint))))
|
||||
int32_t codepoint = utf8_decode (&self->s, self->len);
|
||||
if (!soft_assert (codepoint >= 0))
|
||||
{
|
||||
// Invalid UTF-8
|
||||
self->len = 0;
|
||||
return -1;
|
||||
return codepoint;
|
||||
}
|
||||
|
||||
size_t advance = self->s - old;
|
||||
@@ -2913,6 +2942,13 @@ base64_encode (const void *data, size_t len, struct str *output)
|
||||
|
||||
// --- Utilities ---------------------------------------------------------------
|
||||
|
||||
static void
|
||||
cstr_set (char **s, char *new)
|
||||
{
|
||||
free (*s);
|
||||
*s = new;
|
||||
}
|
||||
|
||||
static void
|
||||
cstr_split (const char *s, const char *delimiters, bool ignore_empty,
|
||||
struct strv *out)
|
||||
@@ -2942,10 +2978,10 @@ cstr_strip_in_place (char *s, const char *stripped_chars)
|
||||
}
|
||||
|
||||
static void
|
||||
cstr_transform (char *s, int (*tolower) (int c))
|
||||
cstr_transform (char *s, int (*xform) (int c))
|
||||
{
|
||||
for (; *s; s++)
|
||||
*s = tolower (*s);
|
||||
*s = xform (*s);
|
||||
}
|
||||
|
||||
static char *
|
||||
@@ -2992,6 +3028,7 @@ iconv_xstrdup (iconv_t conv, char *in, size_t in_len, size_t *out_len)
|
||||
|
||||
char *in_ptr = in;
|
||||
if (in_len == (size_t) -1)
|
||||
// XXX: out_len will be one character longer than the string!
|
||||
in_len = strlen (in) + 1;
|
||||
|
||||
while (iconv (conv, (char **) &in_ptr, &in_len,
|
||||
@@ -3209,7 +3246,7 @@ get_xdg_config_dirs (struct strv *out)
|
||||
str_free (&config_home);
|
||||
|
||||
const char *xdg_config_dirs;
|
||||
if (!(xdg_config_dirs = getenv ("XDG_CONFIG_DIRS")))
|
||||
if (!(xdg_config_dirs = getenv ("XDG_CONFIG_DIRS")) || !*xdg_config_dirs)
|
||||
xdg_config_dirs = "/etc/xdg";
|
||||
cstr_split (xdg_config_dirs, ":", true, out);
|
||||
}
|
||||
@@ -3234,7 +3271,7 @@ get_xdg_data_dirs (struct strv *out)
|
||||
str_free (&data_home);
|
||||
|
||||
const char *xdg_data_dirs;
|
||||
if (!(xdg_data_dirs = getenv ("XDG_DATA_DIRS")))
|
||||
if (!(xdg_data_dirs = getenv ("XDG_DATA_DIRS")) || !*xdg_data_dirs)
|
||||
xdg_data_dirs = "/usr/local/share/:/usr/share/";
|
||||
cstr_split (xdg_data_dirs, ":", true, out);
|
||||
}
|
||||
@@ -3251,16 +3288,8 @@ resolve_relative_data_filename (const char *filename)
|
||||
}
|
||||
|
||||
static char *
|
||||
resolve_relative_runtime_filename (const char *filename)
|
||||
resolve_relative_runtime_filename_finish (struct str path)
|
||||
{
|
||||
struct str path = str_make ();
|
||||
const char *runtime_dir = getenv ("XDG_RUNTIME_DIR");
|
||||
if (runtime_dir && *runtime_dir == '/')
|
||||
str_append (&path, runtime_dir);
|
||||
else
|
||||
get_xdg_home_dir (&path, "XDG_DATA_HOME", ".local/share");
|
||||
str_append_printf (&path, "/%s/%s", PROGRAM_NAME, filename);
|
||||
|
||||
// Try to create the file's ancestors;
|
||||
// typically the user will want to immediately create a file in there
|
||||
const char *last_slash = strrchr (path.str, '/');
|
||||
@@ -3273,6 +3302,41 @@ resolve_relative_runtime_filename (const char *filename)
|
||||
return str_steal (&path);
|
||||
}
|
||||
|
||||
static char *
|
||||
resolve_relative_runtime_filename (const char *filename)
|
||||
{
|
||||
struct str path = str_make ();
|
||||
const char *runtime_dir = getenv ("XDG_RUNTIME_DIR");
|
||||
if (runtime_dir && *runtime_dir == '/')
|
||||
str_append (&path, runtime_dir);
|
||||
else
|
||||
get_xdg_home_dir (&path, "XDG_DATA_HOME", ".local/share");
|
||||
|
||||
str_append_printf (&path, "/%s/%s", PROGRAM_NAME, filename);
|
||||
return resolve_relative_runtime_filename_finish (path);
|
||||
}
|
||||
|
||||
/// This differs from resolve_relative_runtime_filename() in that we expect
|
||||
/// the filename to be something like a pattern for mkstemp(), so the resulting
|
||||
/// path can reside in a system-wide directory with no risk of a conflict.
|
||||
/// However, we have to take care about permissions. Do we even need this?
|
||||
static char *
|
||||
resolve_relative_runtime_template (const char *template)
|
||||
{
|
||||
struct str path = str_make ();
|
||||
const char *runtime_dir = getenv ("XDG_RUNTIME_DIR");
|
||||
const char *tmpdir = getenv ("TMPDIR");
|
||||
if (runtime_dir && *runtime_dir == '/')
|
||||
str_append_printf (&path, "%s/%s", runtime_dir, PROGRAM_NAME);
|
||||
else if (tmpdir && *tmpdir == '/')
|
||||
str_append_printf (&path, "%s/%s.%d", tmpdir, PROGRAM_NAME, geteuid ());
|
||||
else
|
||||
str_append_printf (&path, "/tmp/%s.%d", PROGRAM_NAME, geteuid ());
|
||||
|
||||
str_append_printf (&path, "/%s", template);
|
||||
return resolve_relative_runtime_filename_finish (path);
|
||||
}
|
||||
|
||||
static char *
|
||||
try_expand_tilde (const char *filename)
|
||||
{
|
||||
@@ -3725,7 +3789,7 @@ opt_handler_usage (const struct opt_handler *self, FILE *stream)
|
||||
str_append_printf (&row, "--%s", opt->long_name);
|
||||
if (opt->arg_hint)
|
||||
str_append_printf (&row, (opt->flags & OPT_OPTIONAL_ARG)
|
||||
? " [%s]" : " %s", opt->arg_hint);
|
||||
? "[=%s]" : " %s", opt->arg_hint);
|
||||
|
||||
// TODO: keep the indent if there are multiple lines
|
||||
if (row.len + 2 <= OPT_USAGE_ALIGNMENT_COLUMN)
|
||||
@@ -4352,7 +4416,7 @@ socket_io_try_write (int socket_fd, struct str *wb)
|
||||
// char = [\0-\177] # or any Unicode codepoint in the UTF-8 encoding
|
||||
// escape = [\\"abfnrtv] / [xX][0-9A-Fa-f][0-9A-Fa-f]? / [0-7][0-7]?[0-7]?
|
||||
//
|
||||
// integer = lws '-'? [0-9]+ # whatever strtoll() accepts on your system
|
||||
// integer = lws [-+]? [0-9]+ # whatever strtoll() accepts on your system
|
||||
// null = lws 'null'
|
||||
// boolean = lws 'yes' / lws 'YES' / lws 'no' / lws 'NO'
|
||||
// / lws 'on' / lws 'ON' / lws 'off' / lws 'OFF'
|
||||
@@ -4643,13 +4707,15 @@ config_item_write_string (struct str *output, const struct str *s)
|
||||
for (size_t i = 0; i < s->len; i++)
|
||||
{
|
||||
unsigned char c = s->str[i];
|
||||
if (c == '\n') str_append (output, "\\n");
|
||||
else if (c == '\r') str_append (output, "\\r");
|
||||
else if (c == '\t') str_append (output, "\\t");
|
||||
else if (c == '\\') str_append (output, "\\\\");
|
||||
else if (c == '"') str_append (output, "\\\"");
|
||||
else if (c < 32) str_append_printf (output, "\\x%02x", c);
|
||||
else str_append_c (output, c);
|
||||
if (c == '\n') str_append (output, "\\n");
|
||||
else if (c == '\r') str_append (output, "\\r");
|
||||
else if (c == '\t') str_append (output, "\\t");
|
||||
else if (c == '\\') str_append (output, "\\\\");
|
||||
else if (c == '"') str_append (output, "\\\"");
|
||||
else if (iscntrl_ascii (c))
|
||||
str_append_printf (output, "\\x%02x", c);
|
||||
else
|
||||
str_append_c (output, c);
|
||||
}
|
||||
str_append_c (output, '"');
|
||||
}
|
||||
@@ -5026,18 +5092,21 @@ config_tokenizer_next (struct config_tokenizer *self, struct error **e)
|
||||
return CONFIG_T_STRING;
|
||||
}
|
||||
|
||||
char *end;
|
||||
// Our input doesn't need to be NUL-terminated but we want to use strtoll()
|
||||
char buf[48] = "", *end = buf;
|
||||
size_t buf_len = MIN (sizeof buf - 1, self->len);
|
||||
|
||||
errno = 0;
|
||||
self->integer = strtoll (self->p, &end, 10);
|
||||
self->integer = strtoll (strncpy (buf, self->p, buf_len), &end, 10);
|
||||
if (errno == ERANGE)
|
||||
{
|
||||
config_tokenizer_error (self, e, "integer out of range");
|
||||
return CONFIG_T_ABORT;
|
||||
}
|
||||
if (end != self->p)
|
||||
if (end != buf)
|
||||
{
|
||||
self->len -= end - self->p;
|
||||
self->p = end;
|
||||
self->len -= end - buf;
|
||||
self->p += end - buf;
|
||||
return CONFIG_T_INTEGER;
|
||||
}
|
||||
|
||||
@@ -5050,7 +5119,7 @@ config_tokenizer_next (struct config_tokenizer *self, struct error **e)
|
||||
str_reset (&self->string);
|
||||
do
|
||||
str_append_c (&self->string, config_tokenizer_advance (self));
|
||||
while (config_tokenizer_is_word_char (*self->p));
|
||||
while (self->len && config_tokenizer_is_word_char (*self->p));
|
||||
|
||||
if (!strcmp (self->string.str, "null"))
|
||||
return CONFIG_T_NULL;
|
||||
@@ -5253,7 +5322,9 @@ static struct config_item *
|
||||
config_item_parse (const char *script, size_t len,
|
||||
bool single_value_only, struct error **e)
|
||||
{
|
||||
struct config_parser parser = config_parser_make (script, len);
|
||||
volatile struct config_parser parser = config_parser_make (script, len);
|
||||
struct config_parser *volatile self = (struct config_parser *) &parser;
|
||||
|
||||
struct config_item *volatile object = NULL;
|
||||
jmp_buf err;
|
||||
|
||||
@@ -5275,13 +5346,13 @@ config_item_parse (const char *script, size_t len,
|
||||
// This is really only intended for in-program configuration
|
||||
// and telling the line number would look awkward
|
||||
parser.tokenizer.report_line = false;
|
||||
object = config_parser_parse_value (&parser, err);
|
||||
object = config_parser_parse_value (self, err);
|
||||
}
|
||||
else
|
||||
object = config_parser_parse_object (&parser, err);
|
||||
config_parser_expect (&parser, CONFIG_T_ABORT, err);
|
||||
object = config_parser_parse_object (self, err);
|
||||
config_parser_expect (self, CONFIG_T_ABORT, err);
|
||||
end:
|
||||
config_parser_free (&parser);
|
||||
config_parser_free (self);
|
||||
return object;
|
||||
}
|
||||
|
||||
|
||||
@@ -82,4 +82,3 @@ siphash (const unsigned char key[16], const unsigned char *m, size_t len)
|
||||
|
||||
return v0 ^ v1 ^ v2 ^ v3;
|
||||
}
|
||||
|
||||
|
||||
297
tests/fuzz.c
Normal file
297
tests/fuzz.c
Normal file
@@ -0,0 +1,297 @@
|
||||
/*
|
||||
* tests/fuzz.c
|
||||
*
|
||||
* Copyright (c) 2020, Přemysl Eric Janouch <p@janouch.name>
|
||||
*
|
||||
* Permission to use, copy, modify, and/or distribute this software for any
|
||||
* purpose with or without fee is hereby granted.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
||||
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
||||
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
||||
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*
|
||||
*/
|
||||
|
||||
#define PROGRAM_NAME "fuzz"
|
||||
#define PROGRAM_VERSION "0"
|
||||
|
||||
#define LIBERTY_WANT_SSL
|
||||
// The MPD client is a full wrapper and needs the network
|
||||
#define LIBERTY_WANT_POLLER
|
||||
#define LIBERTY_WANT_ASYNC
|
||||
|
||||
#define LIBERTY_WANT_PROTO_IRC
|
||||
#define LIBERTY_WANT_PROTO_HTTP
|
||||
#define LIBERTY_WANT_PROTO_SCGI
|
||||
#define LIBERTY_WANT_PROTO_FASTCGI
|
||||
#define LIBERTY_WANT_PROTO_WS
|
||||
#define LIBERTY_WANT_PROTO_MPD
|
||||
|
||||
#include "../liberty.c"
|
||||
#include "../liberty-tui.c"
|
||||
|
||||
static bool
|
||||
app_is_character_in_locale (ucs4_t ch)
|
||||
{
|
||||
return ch < 128;
|
||||
}
|
||||
|
||||
// --- UTF-8 -------------------------------------------------------------------
|
||||
|
||||
static void
|
||||
test_utf8_validate (const uint8_t *data, size_t size)
|
||||
{
|
||||
utf8_validate ((const char *) data, size);
|
||||
}
|
||||
|
||||
// --- Base 64 -----------------------------------------------------------------
|
||||
|
||||
static void
|
||||
test_base64_decode (const uint8_t *data, size_t size)
|
||||
{
|
||||
struct str wrap = str_make ();
|
||||
str_append_data (&wrap, data, size);
|
||||
|
||||
struct str out = str_make ();
|
||||
base64_decode (wrap.str, true /* ignore_ws */, &out);
|
||||
str_free (&out);
|
||||
|
||||
str_free (&wrap);
|
||||
}
|
||||
|
||||
// --- IRC ---------------------------------------------------------------------
|
||||
|
||||
static void
|
||||
test_irc_parse_message (const uint8_t *data, size_t size)
|
||||
{
|
||||
struct str wrap = str_make ();
|
||||
str_append_data (&wrap, data, size);
|
||||
|
||||
struct irc_message msg;
|
||||
irc_parse_message (&msg, wrap.str);
|
||||
irc_free_message (&msg);
|
||||
|
||||
str_free (&wrap);
|
||||
}
|
||||
|
||||
// --- HTTP --------------------------------------------------------------------
|
||||
|
||||
static void
|
||||
test_http_parse_media_type (const uint8_t *data, size_t size)
|
||||
{
|
||||
struct str wrap = str_make ();
|
||||
str_append_data (&wrap, data, size);
|
||||
|
||||
char *type = NULL;
|
||||
char *subtype = NULL;
|
||||
struct str_map parameters = str_map_make (free);
|
||||
http_parse_media_type (wrap.str, &type, &subtype, ¶meters);
|
||||
free (type);
|
||||
free (subtype);
|
||||
str_map_free (¶meters);
|
||||
|
||||
str_free (&wrap);
|
||||
}
|
||||
|
||||
static void
|
||||
test_http_parse_upgrade (const uint8_t *data, size_t size)
|
||||
{
|
||||
struct str wrap = str_make ();
|
||||
str_append_data (&wrap, data, size);
|
||||
|
||||
struct http_protocol *protocols = NULL;
|
||||
http_parse_upgrade (wrap.str, &protocols);
|
||||
LIST_FOR_EACH (struct http_protocol, iter, protocols)
|
||||
http_protocol_destroy (iter);
|
||||
|
||||
str_free (&wrap);
|
||||
}
|
||||
|
||||
// --- SCGI --------------------------------------------------------------------
|
||||
|
||||
static bool
|
||||
test_scgi_parser_on_headers_read (void *user_data)
|
||||
{
|
||||
(void) user_data;
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool
|
||||
test_scgi_parser_on_content (void *user_data, const void *data, size_t len)
|
||||
{
|
||||
(void) user_data;
|
||||
(void) data;
|
||||
(void) len;
|
||||
return true;
|
||||
}
|
||||
|
||||
static void
|
||||
test_scgi_parser_push (const uint8_t *data, size_t size)
|
||||
{
|
||||
struct scgi_parser parser = scgi_parser_make ();
|
||||
parser.on_headers_read = test_scgi_parser_on_headers_read;
|
||||
parser.on_content = test_scgi_parser_on_content;
|
||||
|
||||
scgi_parser_push (&parser, data, size, NULL);
|
||||
scgi_parser_free (&parser);
|
||||
}
|
||||
|
||||
// --- WebSockets --------------------------------------------------------------
|
||||
|
||||
static bool
|
||||
test_ws_parser_on_frame_header (void *user_data, const struct ws_parser *self)
|
||||
{
|
||||
(void) user_data;
|
||||
(void) self;
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool
|
||||
test_ws_parser_on_frame (void *user_data, const struct ws_parser *self)
|
||||
{
|
||||
(void) user_data;
|
||||
(void) self;
|
||||
return true;
|
||||
}
|
||||
|
||||
static void
|
||||
test_ws_parser_push (const uint8_t *data, size_t size)
|
||||
{
|
||||
struct ws_parser parser = ws_parser_make ();
|
||||
parser.on_frame_header = test_ws_parser_on_frame_header;
|
||||
parser.on_frame = test_ws_parser_on_frame;
|
||||
|
||||
ws_parser_push (&parser, data, size);
|
||||
ws_parser_free (&parser);
|
||||
}
|
||||
|
||||
// --- FastCGI -----------------------------------------------------------------
|
||||
|
||||
static bool
|
||||
test_fcgi_parser_on_message (const struct fcgi_parser *parser, void *user_data)
|
||||
{
|
||||
(void) parser;
|
||||
(void) user_data;
|
||||
return true;
|
||||
}
|
||||
|
||||
static void
|
||||
test_fcgi_parser_push (const uint8_t *data, size_t size)
|
||||
{
|
||||
struct fcgi_parser parser = fcgi_parser_make ();
|
||||
parser.on_message = test_fcgi_parser_on_message;
|
||||
fcgi_parser_push (&parser, data, size);
|
||||
fcgi_parser_free (&parser);
|
||||
}
|
||||
|
||||
static void
|
||||
test_fcgi_nv_parser_push (const uint8_t *data, size_t size)
|
||||
{
|
||||
struct str_map values = str_map_make (free);
|
||||
struct fcgi_nv_parser nv_parser = fcgi_nv_parser_make ();
|
||||
nv_parser.output = &values;
|
||||
|
||||
fcgi_nv_parser_push (&nv_parser, data, size);
|
||||
fcgi_nv_parser_free (&nv_parser);
|
||||
str_map_free (&values);
|
||||
}
|
||||
|
||||
// --- Config ------------------------------------------------------------------
|
||||
|
||||
static void
|
||||
test_config_item_parse (const uint8_t *data, size_t size)
|
||||
{
|
||||
struct config_item *item =
|
||||
config_item_parse ((const char *) data, size, false, NULL);
|
||||
if (item)
|
||||
config_item_destroy (item);
|
||||
}
|
||||
|
||||
// --- TUI ---------------------------------------------------------------------
|
||||
|
||||
static void
|
||||
test_attrs_decode (const uint8_t *data, size_t size)
|
||||
{
|
||||
struct str wrap = str_make ();
|
||||
str_append_data (&wrap, data, size);
|
||||
|
||||
attrs_decode (wrap.str);
|
||||
|
||||
str_free (&wrap);
|
||||
}
|
||||
|
||||
// --- MPD ---------------------------------------------------------------------
|
||||
|
||||
static void
|
||||
test_mpd_client_process_input (const uint8_t *data, size_t size)
|
||||
{
|
||||
struct poller poller;
|
||||
poller_init (&poller);
|
||||
|
||||
struct mpd_client mpd = mpd_client_make (&poller);
|
||||
str_append_data (&mpd.read_buffer, data, size);
|
||||
mpd_client_process_input (&mpd);
|
||||
mpd_client_free (&mpd);
|
||||
|
||||
poller_free (&poller);
|
||||
}
|
||||
|
||||
// --- Main --------------------------------------------------------------------
|
||||
|
||||
typedef void (*fuzz_test_fn) (const uint8_t *data, size_t size);
|
||||
static fuzz_test_fn generator = NULL;
|
||||
|
||||
void
|
||||
LLVMFuzzerTestOneInput (const uint8_t *data, size_t size)
|
||||
{
|
||||
generator (data, size);
|
||||
}
|
||||
|
||||
int
|
||||
LLVMFuzzerInitialize (int *argcp, char ***argvp)
|
||||
{
|
||||
struct str_map targets = str_map_make (NULL);
|
||||
#define REGISTER(name) str_map_set (&targets, #name, test_ ## name);
|
||||
REGISTER (utf8_validate)
|
||||
REGISTER (base64_decode)
|
||||
REGISTER (irc_parse_message)
|
||||
REGISTER (http_parse_media_type)
|
||||
REGISTER (http_parse_upgrade)
|
||||
REGISTER (scgi_parser_push)
|
||||
REGISTER (ws_parser_push)
|
||||
REGISTER (fcgi_parser_push)
|
||||
REGISTER (fcgi_nv_parser_push)
|
||||
REGISTER (config_item_parse)
|
||||
REGISTER (attrs_decode)
|
||||
REGISTER (mpd_client_process_input)
|
||||
|
||||
char **argv = *argvp, *option = "-test=", *name = NULL;
|
||||
for (int i = 1; i < *argcp; i++)
|
||||
if (!strncmp (argv[i], option, strlen (option)))
|
||||
{
|
||||
name = argv[i] + strlen (option);
|
||||
memmove (argv + i, argv + i + 1, (*argcp - i) * sizeof *argv);
|
||||
(*argcp)--;
|
||||
}
|
||||
|
||||
if (!name)
|
||||
{
|
||||
struct str_map_iter iter = str_map_iter_make (&targets);
|
||||
while (str_map_iter_next (&iter))
|
||||
printf ("%s\n", iter.link->key);
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
|
||||
if (!(generator = str_map_find (&targets, name)))
|
||||
{
|
||||
fprintf (stderr, "Unknown test: %s\n", name);
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
|
||||
str_map_free (&targets);
|
||||
return 0;
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
* tests/liberty.c
|
||||
*
|
||||
* Copyright (c) 2015 - 2016, Přemysl Janouch <p@janouch.name>
|
||||
* Copyright (c) 2015 - 2016, Přemysl Eric Janouch <p@janouch.name>
|
||||
*
|
||||
* Permission to use, copy, modify, and/or distribute this software for any
|
||||
* purpose with or without fee is hereby granted.
|
||||
@@ -326,10 +326,19 @@ test_str_map (void)
|
||||
static void
|
||||
test_utf8 (void)
|
||||
{
|
||||
const char valid [] = "2H₂ + O₂ ⇌ 2H₂O, R = 4.7 kΩ, ⌀ 200 mm";
|
||||
const char invalid[] = "\xf0\x90\x28\xbc";
|
||||
soft_assert ( utf8_validate (valid, sizeof valid));
|
||||
soft_assert (!utf8_validate (invalid, sizeof invalid));
|
||||
const char *full = "\xc5\x99", *partial = full, *empty = full;
|
||||
soft_assert (utf8_decode (&full, 2) == 0x0159);
|
||||
soft_assert (utf8_decode (&partial, 1) == -2);
|
||||
soft_assert (utf8_decode (&empty, 0) == -1);
|
||||
|
||||
const char valid_1[] = "2H₂ + O₂ ⇌ 2H₂O, R = 4.7 kΩ, ⌀ 200 mm";
|
||||
const char valid_2[] = "\xf0\x93\x82\xb9";
|
||||
const char invalid_1[] = "\xf0\x90\x28\xbc";
|
||||
const char invalid_2[] = "\xc0\x80";
|
||||
soft_assert ( utf8_validate (valid_1, sizeof valid_1));
|
||||
soft_assert ( utf8_validate (valid_2, sizeof valid_2));
|
||||
soft_assert (!utf8_validate (invalid_1, sizeof invalid_1));
|
||||
soft_assert (!utf8_validate (invalid_2, sizeof invalid_2));
|
||||
|
||||
struct utf8_iter iter = utf8_iter_make ("fóọ");
|
||||
size_t ch_len;
|
||||
@@ -604,6 +613,78 @@ test_connector (const void *user_data, struct test_connector_fixture *self)
|
||||
connector_free (&connector);
|
||||
}
|
||||
|
||||
// --- Configuration -----------------------------------------------------------
|
||||
|
||||
static void
|
||||
on_test_config_foo_change (struct config_item *item)
|
||||
{
|
||||
*(bool *) item->user_data = item->value.boolean;
|
||||
}
|
||||
|
||||
static bool
|
||||
test_config_validate_nonnegative
|
||||
(const struct config_item *item, struct error **e)
|
||||
{
|
||||
if (item->type == CONFIG_ITEM_NULL)
|
||||
return true;
|
||||
|
||||
hard_assert (item->type == CONFIG_ITEM_INTEGER);
|
||||
if (item->value.integer >= 0)
|
||||
return true;
|
||||
|
||||
error_set (e, "must be non-negative");
|
||||
return false;
|
||||
}
|
||||
|
||||
static struct config_schema g_config_test[] =
|
||||
{
|
||||
{ .name = "foo",
|
||||
.comment = "baz",
|
||||
.type = CONFIG_ITEM_BOOLEAN,
|
||||
.default_ = "off",
|
||||
.on_change = on_test_config_foo_change },
|
||||
{ .name = "bar",
|
||||
.type = CONFIG_ITEM_INTEGER,
|
||||
.validate = test_config_validate_nonnegative,
|
||||
.default_ = "1" },
|
||||
{ .name = "foobar",
|
||||
.type = CONFIG_ITEM_STRING,
|
||||
.default_ = "\"qux\\x01\"" },
|
||||
{}
|
||||
};
|
||||
|
||||
static void
|
||||
test_config_load (struct config_item *subtree, void *user_data)
|
||||
{
|
||||
config_schema_apply_to_object (g_config_test, subtree, user_data);
|
||||
}
|
||||
|
||||
static void
|
||||
test_config (void)
|
||||
{
|
||||
struct config config = config_make ();
|
||||
|
||||
bool b = true;
|
||||
config_register_module (&config, "top", test_config_load, &b);
|
||||
config_load (&config, config_item_object ());
|
||||
config_schema_call_changed (config.root);
|
||||
hard_assert (b == false);
|
||||
|
||||
struct config_item *invalid = config_item_integer (-1);
|
||||
hard_assert (!config_item_set_from (config_item_get (config.root,
|
||||
"top.bar", NULL), invalid, NULL));
|
||||
config_item_destroy (invalid);
|
||||
|
||||
struct str s = str_make ();
|
||||
config_item_write (config.root, true, &s);
|
||||
struct config_item *parsed = config_item_parse (s.str, s.len, false, NULL);
|
||||
hard_assert (parsed);
|
||||
config_item_destroy (parsed);
|
||||
str_free (&s);
|
||||
|
||||
config_free (&config);
|
||||
}
|
||||
|
||||
// --- Main --------------------------------------------------------------------
|
||||
|
||||
int
|
||||
@@ -622,6 +703,7 @@ main (int argc, char *argv[])
|
||||
test_add_simple (&test, "/utf-8", NULL, test_utf8);
|
||||
test_add_simple (&test, "/base64", NULL, test_base64);
|
||||
test_add_simple (&test, "/async", NULL, test_async);
|
||||
test_add_simple (&test, "/config", NULL, test_config);
|
||||
|
||||
test_add (&test, "/connector", struct test_connector_fixture, NULL,
|
||||
test_connector_fixture_init,
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
* tests/proto.c
|
||||
*
|
||||
* Copyright (c) 2015, Přemysl Janouch <p@janouch.name>
|
||||
* Copyright (c) 2015, Přemysl Eric Janouch <p@janouch.name>
|
||||
*
|
||||
* Permission to use, copy, modify, and/or distribute this software for any
|
||||
* purpose with or without fee is hereby granted.
|
||||
@@ -77,7 +77,7 @@ test_irc (void)
|
||||
static void
|
||||
test_http_parser (void)
|
||||
{
|
||||
struct str_map parameters = str_map_make (NULL);
|
||||
struct str_map parameters = str_map_make (free);
|
||||
parameters.key_xfrm = tolower_ascii_strxfrm;
|
||||
|
||||
char *type = NULL;
|
||||
@@ -88,9 +88,11 @@ test_http_parser (void)
|
||||
soft_assert (!strcasecmp_ascii (subtype, "html"));
|
||||
soft_assert (parameters.len == 1);
|
||||
soft_assert (!strcmp (str_map_find (¶meters, "charset"), "utf-8"));
|
||||
free (type);
|
||||
free (subtype);
|
||||
str_map_free (¶meters);
|
||||
|
||||
struct http_protocol *protocols;
|
||||
struct http_protocol *protocols = NULL;
|
||||
soft_assert (http_parse_upgrade ("websocket, HTTP/2.0, , ", &protocols));
|
||||
|
||||
soft_assert (!strcmp (protocols->name, "websocket"));
|
||||
@@ -105,10 +107,20 @@ test_http_parser (void)
|
||||
http_protocol_destroy (iter);
|
||||
}
|
||||
|
||||
struct scgi_fixture
|
||||
{
|
||||
struct scgi_parser parser;
|
||||
bool seen_headers;
|
||||
bool seen_content;
|
||||
};
|
||||
|
||||
static bool
|
||||
test_scgi_parser_on_headers_read (void *user_data)
|
||||
{
|
||||
struct scgi_parser *parser = user_data;
|
||||
struct scgi_fixture *fixture = user_data;
|
||||
struct scgi_parser *parser = &fixture->parser;
|
||||
fixture->seen_headers = true;
|
||||
|
||||
soft_assert (parser->headers.len == 4);
|
||||
soft_assert (!strcmp (str_map_find (&parser->headers,
|
||||
"CONTENT_LENGTH"), "27"));
|
||||
@@ -124,7 +136,9 @@ test_scgi_parser_on_headers_read (void *user_data)
|
||||
static bool
|
||||
test_scgi_parser_on_content (void *user_data, const void *data, size_t len)
|
||||
{
|
||||
(void) user_data;
|
||||
struct scgi_fixture *fixture = user_data;
|
||||
fixture->seen_content = true;
|
||||
|
||||
soft_assert (!strncmp (data, "What is the answer to life?", len));
|
||||
return true;
|
||||
}
|
||||
@@ -132,10 +146,12 @@ test_scgi_parser_on_content (void *user_data, const void *data, size_t len)
|
||||
static void
|
||||
test_scgi_parser (void)
|
||||
{
|
||||
struct scgi_parser parser = scgi_parser_make ();
|
||||
parser.on_headers_read = test_scgi_parser_on_headers_read;
|
||||
parser.on_content = test_scgi_parser_on_content;
|
||||
parser.user_data = &parser;
|
||||
struct scgi_fixture fixture = { scgi_parser_make(), false, false };
|
||||
struct scgi_parser *parser = &fixture.parser;
|
||||
|
||||
parser->on_headers_read = test_scgi_parser_on_headers_read;
|
||||
parser->on_content = test_scgi_parser_on_content;
|
||||
parser->user_data = &fixture;
|
||||
|
||||
// This is an example straight from the specification
|
||||
const char example[] =
|
||||
@@ -147,8 +163,9 @@ test_scgi_parser (void)
|
||||
","
|
||||
"What is the answer to life?";
|
||||
|
||||
soft_assert (scgi_parser_push (&parser, example, sizeof example, NULL));
|
||||
scgi_parser_free (&parser);
|
||||
soft_assert (scgi_parser_push (parser, example, sizeof example, NULL));
|
||||
soft_assert (fixture.seen_headers && fixture.seen_content);
|
||||
scgi_parser_free (parser);
|
||||
}
|
||||
|
||||
static bool
|
||||
|
||||
127
tests/pulse.c
Normal file
127
tests/pulse.c
Normal file
@@ -0,0 +1,127 @@
|
||||
/*
|
||||
* tests/pulse.c
|
||||
*
|
||||
* Copyright (c) 2021, Přemysl Eric Janouch <p@janouch.name>
|
||||
*
|
||||
* Permission to use, copy, modify, and/or distribute this software for any
|
||||
* purpose with or without fee is hereby granted.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
||||
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
||||
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
||||
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*
|
||||
*/
|
||||
|
||||
#define PROGRAM_NAME "test"
|
||||
#define PROGRAM_VERSION "0"
|
||||
|
||||
#define LIBERTY_WANT_POLLER
|
||||
|
||||
#include "../liberty.c"
|
||||
#include "../liberty-pulse.c"
|
||||
|
||||
// --- Tests -------------------------------------------------------------------
|
||||
|
||||
enum
|
||||
{
|
||||
EVENT_IO = 1 << 0,
|
||||
EVENT_TIME = 1 << 1,
|
||||
EVENT_DEFER = 1 << 2,
|
||||
EVENT_ALL = (1 << 3) - 1
|
||||
};
|
||||
|
||||
static intptr_t g_events = 0;
|
||||
static intptr_t g_destroys = 0;
|
||||
|
||||
static void
|
||||
io_event_cb (pa_mainloop_api *a,
|
||||
pa_io_event *e, int fd, pa_io_event_flags_t events, void *userdata)
|
||||
{
|
||||
(void) a; (void) e; (void) fd; (void) events;
|
||||
g_events |= (intptr_t) userdata;
|
||||
}
|
||||
|
||||
static void
|
||||
io_event_destroy_cb (pa_mainloop_api *a, pa_io_event *e, void *userdata)
|
||||
{
|
||||
(void) a; (void) e;
|
||||
g_destroys += (intptr_t) userdata;
|
||||
}
|
||||
|
||||
static void
|
||||
time_event_cb (pa_mainloop_api *a,
|
||||
pa_time_event *e, const struct timeval *tv, void *userdata)
|
||||
{
|
||||
(void) a; (void) e; (void) tv;
|
||||
g_events |= (intptr_t) userdata;
|
||||
}
|
||||
|
||||
static void
|
||||
time_event_destroy_cb (pa_mainloop_api *a, pa_time_event *e, void *userdata)
|
||||
{
|
||||
(void) a; (void) e;
|
||||
g_destroys += (intptr_t) userdata;
|
||||
}
|
||||
|
||||
static void
|
||||
defer_event_cb (pa_mainloop_api *a, pa_defer_event *e, void *userdata)
|
||||
{
|
||||
(void) a; (void) e;
|
||||
g_events |= (intptr_t) userdata;
|
||||
}
|
||||
|
||||
static void
|
||||
defer_event_destroy_cb (pa_mainloop_api *a, pa_defer_event *e, void *userdata)
|
||||
{
|
||||
(void) a; (void) e;
|
||||
g_destroys += (intptr_t) userdata;
|
||||
}
|
||||
|
||||
static void
|
||||
test_pulse (void)
|
||||
{
|
||||
struct poller poller;
|
||||
poller_init (&poller);
|
||||
|
||||
// Let's just get this over with, not aiming for high test coverage here
|
||||
pa_mainloop_api *api = poller_pa_new (&poller);
|
||||
|
||||
pa_io_event *ie = api->io_new (api, STDOUT_FILENO, PA_IO_EVENT_OUTPUT,
|
||||
io_event_cb, (void *) EVENT_IO);
|
||||
api->io_set_destroy (ie, io_event_destroy_cb);
|
||||
|
||||
const struct timeval tv = poller_pa_get_current_time ();
|
||||
pa_time_event *te = api->time_new (api, &tv,
|
||||
time_event_cb, (void *) EVENT_TIME);
|
||||
api->time_set_destroy (te, time_event_destroy_cb);
|
||||
api->time_restart (te, &tv);
|
||||
|
||||
pa_defer_event *de = api->defer_new (api,
|
||||
defer_event_cb, (void *) EVENT_DEFER);
|
||||
api->defer_set_destroy (de, defer_event_destroy_cb);
|
||||
api->defer_enable (api->defer_new (api,
|
||||
defer_event_cb, (void *) EVENT_DEFER), false);
|
||||
|
||||
alarm (1);
|
||||
while (g_events != EVENT_ALL)
|
||||
poller_run (&poller);
|
||||
|
||||
poller_pa_destroy (api);
|
||||
soft_assert (g_destroys == EVENT_ALL);
|
||||
poller_free (&poller);
|
||||
}
|
||||
|
||||
// --- Main --------------------------------------------------------------------
|
||||
|
||||
int
|
||||
main (int argc, char *argv[])
|
||||
{
|
||||
struct test test;
|
||||
test_init (&test, argc, argv);
|
||||
test_add_simple (&test, "/pulse", NULL, test_pulse);
|
||||
return test_run (&test);
|
||||
}
|
||||
Reference in New Issue
Block a user