49
49
#include <sysexits.h> /* EX_USAGE, EX_OSERR, EX_OSFILE */
50
50
#include <errno.h> /* errno, error_t, EACCES,
51
51
ENAMETOOLONG, ENOENT, ENOTDIR,
52
ENOMEM, EEXIST, ECHILD, EPERM,
52
EEXIST, ECHILD, EPERM, ENOMEM,
53
53
EAGAIN, EINTR, ENOBUFS, EADDRINUSE,
54
54
ECONNREFUSED, ECONNRESET,
55
55
ETOOMANYREFS, EMSGSIZE, EBADF,
96
95
IN_EXCL_UNLINK, IN_ONLYDIR,
97
96
struct inotify_event */
98
97
#include <fnmatch.h> /* fnmatch(), FNM_FILE_NAME */
99
#include <stdio.h> /* asprintf(), FILE, stderr, fopen(),
100
fclose(), getline(), sscanf(),
101
feof(), ferror(), rename(),
102
fdopen(), fprintf(), fscanf() */
98
#include <stdio.h> /* asprintf(), FILE, fopen(),
99
getline(), sscanf(), feof(),
100
ferror(), fclose(), stderr,
101
rename(), fdopen(), fprintf(),
103
103
#include <glib.h> /* GKeyFile, g_key_file_free(), g_key_file_new(),
104
104
GError, g_key_file_load_from_file(),
105
105
G_KEY_FILE_NONE, TRUE, G_FILE_ERROR_NOENT,
652
652
__attribute__((nonnull, warn_unused_result))
653
653
bool add_to_queue(task_queue *const queue, const task_context task){
654
if((queue->length + 1) > (SIZE_MAX / sizeof(task_context))){
656
error(0, ENOMEM, "Failed to allocate %" PRIuMAX
657
" tasks for queue->tasks", (uintmax_t)(queue->length + 1));
661
654
const size_t needed_size = sizeof(task_context)*(queue->length + 1);
662
655
if(needed_size > (queue->allocated)){
663
656
task_context *const new_tasks = realloc(queue->tasks,
1899
1884
g_assert_true(queue->tasks[0].func == dummy_func);
1902
static void test_add_to_queue_overflow(__attribute__((unused))
1903
test_fixture *fixture,
1904
__attribute__((unused))
1905
gconstpointer user_data){
1906
__attribute__((cleanup(cleanup_queue)))
1907
task_queue *queue = create_queue();
1908
g_assert_nonnull(queue);
1909
g_assert_true(queue->length == 0);
1910
queue->length = SIZE_MAX / sizeof(task_context); /* fake max size */
1912
FILE *real_stderr = stderr;
1913
FILE *devnull = fopen("/dev/null", "we");
1914
g_assert_nonnull(devnull);
1916
const bool ret = add_to_queue(queue,
1917
(task_context){ .func=dummy_func });
1918
g_assert_true(errno == ENOMEM);
1919
g_assert_false(ret);
1920
stderr = real_stderr;
1921
g_assert_cmpint(fclose(devnull), ==, 0);
1922
queue->length = 0; /* Restore real size */
1925
1887
static void dummy_func(__attribute__((unused))
1926
1888
const task_context task,
1927
1889
__attribute__((unused))
5967
5925
char *const filename = strdup("/nonexistent/socket");
5968
5926
__attribute__((cleanup(string_set_clear)))
5969
5927
string_set cancelled_filenames = {};
5972
/* Find a message size which triggers EMSGSIZE */
5973
__attribute__((cleanup(cleanup_string)))
5974
char *message_buffer = NULL;
5975
size_t message_size = PIPE_BUF + 1;
5976
for(ssize_t ssret = 0; ssret >= 0; message_size += 1024){
5977
if(message_size >= 1024*1024*1024){ /* 1 GiB */
5978
g_test_skip("Skipping EMSGSIZE test: Will not try 1GiB");
5981
free(message_buffer);
5982
message_buffer = malloc(message_size);
5983
if(message_buffer == NULL){
5984
g_test_skip("Skipping EMSGSIZE test");
5985
g_test_message("Failed to malloc() %" PRIuMAX " bytes",
5986
(uintmax_t)message_size);
5989
/* Fill buffer with 'x' */
5990
memset(message_buffer, 'x', message_size);
5991
/* Create a new socketpair for each message size to avoid having
5992
to empty the pipe by reading the message to a separate buffer
5994
g_assert_cmpint(socketpair(PF_LOCAL, SOCK_DGRAM
5995
| SOCK_NONBLOCK | SOCK_CLOEXEC, 0,
5997
ssret = send(socketfds[1], message_buffer, message_size,
5999
error_t saved_errno = errno;
6000
g_assert_cmpint(close(socketfds[0]), ==, 0);
6001
g_assert_cmpint(close(socketfds[1]), ==, 0);
6004
if(saved_errno != EMSGSIZE) {
6005
g_test_skip("Skipping EMSGSIZE test");
6006
g_test_message("Error on send(): %s", strerror(saved_errno));
6009
} else if(ssret != (ssize_t)message_size){
6010
g_test_skip("Skipping EMSGSIZE test");
6011
g_test_message("Partial send(): %" PRIuMAX " of %" PRIdMAX
6012
" bytes", (uintmax_t)ssret,
6013
(intmax_t)message_size);
6017
g_test_message("EMSGSIZE triggered by %" PRIdMAX " bytes",
6018
(intmax_t)message_size);
6021
.data=message_buffer,
6022
.length=message_size - 2, /* Compensate for added '+' and NUL */
6023
.allocated=message_size,
5928
const size_t oversized = 1024*1024; /* Limit seems to be 212960 */
5929
__attribute__((cleanup(cleanup_buffer)))
5931
.data=malloc(oversized),
5933
.allocated=oversized,
5935
g_assert_nonnull(password.data);
6025
5936
if(mlock(password.data, password.allocated) != 0){
6026
5937
g_assert_true(errno == EPERM or errno == ENOMEM);
5939
/* Construct test password buffer */
5940
/* Start with + since that is what the real procotol uses */
5941
password.data[0] = '+';
5942
/* Set a special character at string end just to mark the end */
5943
password.data[oversized-3] = 'y';
5944
/* Set NUL at buffer end, as suggested by the protocol */
5945
password.data[oversized-2] = '\0';
5946
/* Fill rest of password with 'x' */
5947
memset(password.data+1, 'x', oversized-3);
6029
5949
__attribute__((cleanup(cleanup_queue)))
6030
5950
task_queue *queue = create_queue();
6031
5951
g_assert_nonnull(queue);
6032
5953
g_assert_cmpint(socketpair(PF_LOCAL, SOCK_DGRAM
6033
5954
| SOCK_NONBLOCK | SOCK_CLOEXEC, 0,
6034
5955
socketfds), ==, 0);
7941
7862
test_add("/parse_arguments/mixed", test_parse_arguments_mixed);
7942
7863
test_add("/queue/create", test_create_queue);
7943
7864
test_add("/queue/add", test_add_to_queue);
7944
test_add("/queue/add/overflow", test_add_to_queue_overflow);
7945
7865
test_add("/queue/has_question/empty",
7946
7866
test_queue_has_question_empty);
7947
7867
test_add("/queue/has_question/false",
8172
8092
g_option_context_set_help_enabled(context, FALSE);
8173
8093
g_option_context_set_ignore_unknown_options(context, TRUE);
8175
gboolean should_run_tests = FALSE;
8095
gboolean run_tests = FALSE;
8176
8096
GOptionEntry entries[] = {
8177
8097
{ "test", 0, 0, G_OPTION_ARG_NONE,
8178
&should_run_tests, "Run tests", NULL },
8098
&run_tests, "Run tests", NULL },
8181
8101
g_option_context_add_main_entries(context, entries, NULL);