-/* Copyright (c) 2002-2018 Dovecot authors, see the included COPYING file
- */
+/* Copyright (c) 2002-2018 Dovecot authors, see the included COPYING file */
#include "lib.h"
#include "test-lib.h"
#include <unistd.h>
static const char *TEST_SOCKET = "program-client-test.sock";
-static const char *pclient_test_io_string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.\n"
- "Praesent vehicula ac leo vel placerat. Nullam placerat \n"
- "volutpat leo, sed ultricies felis pulvinar quis. Nam \n"
- "tempus, augue ut tempor cursus, neque felis commodo lacus, \n"
- "sit amet tincidunt arcu justo vel augue. Proin dapibus \n"
- "vulputate maximus. Mauris congue lacus felis, sed varius \n"
- "leo finibus sagittis. Cum sociis natoque penatibus et magnis \n"
- "dis parturient montes, nascetur ridiculus mus. Aliquam \n"
- "laoreet arcu a hendrerit consequat. Duis vitae erat tellus.";
-
-static
-struct program_client_settings pc_set = {
+static const char *pclient_test_io_string =
+ "Lorem ipsum dolor sit amet, consectetur adipiscing elit.\n"
+ "Praesent vehicula ac leo vel placerat. Nullam placerat \n"
+ "volutpat leo, sed ultricies felis pulvinar quis. Nam \n"
+ "tempus, augue ut tempor cursus, neque felis commodo lacus, \n"
+ "sit amet tincidunt arcu justo vel augue. Proin dapibus \n"
+ "vulputate maximus. Mauris congue lacus felis, sed varius \n"
+ "leo finibus sagittis. Cum sociis natoque penatibus et magnis \n"
+ "dis parturient montes, nascetur ridiculus mus. Aliquam \n"
+ "laoreet arcu a hendrerit consequat. Duis vitae erat tellus.";
+
+static struct program_client_settings pc_set = {
.client_connect_timeout_msecs = 1000,
.input_idle_timeout_msecs = 5000,
.debug = TRUE,
};
-static
-struct test_server {
+static struct test_server {
struct ioloop *ioloop;
struct io *io;
struct timeout *to;
} state;
};
-static
-void test_program_client_destroy(struct test_client **_client)
+static void test_program_client_destroy(struct test_client **_client)
{
struct test_client *client = *_client;
*_client = NULL;
test_globals.client = NULL;
}
-static
-int test_program_input_handle(struct test_client *client, const char *line)
+static int
+test_program_input_handle(struct test_client *client, const char *line)
{
int cmp;
const char *arg;
switch(client->state) {
case CLIENT_STATE_INIT:
- test_assert((cmp = strncmp(line, "VERSION\tscript\t", 15)) == 0);
+ test_assert((cmp=strncmp(line, "VERSION\tscript\t", 15)) == 0);
if (cmp == 0) {
client->state = CLIENT_STATE_VERSION;
} else
return -1;
break;
case CLIENT_STATE_ARGS:
- if (strcmp(line, "") == 0) {
+ if (strcmp(line, "") == 0) {
array_append_zero(&client->args);
client->state = CLIENT_STATE_BODY;
return 0;
array_append(&client->args, &arg, 1);
break;
case CLIENT_STATE_BODY:
- client->os_body = iostream_temp_create_named(".dovecot.test.", 0, "test_program_input body");
+ client->os_body = iostream_temp_create_named(
+ ".dovecot.test.", 0, "test_program_input body");
switch(o_stream_send_istream(client->os_body, client->in)) {
case OSTREAM_SEND_ISTREAM_RESULT_ERROR_OUTPUT:
i_panic("Cannot write to ostream-temp: %s",
o_stream_get_error(client->os_body));
case OSTREAM_SEND_ISTREAM_RESULT_ERROR_INPUT:
i_warning("Client stream error: %s",
- i_stream_get_error(client->in));
+ i_stream_get_error(client->in));
return -1;
case OSTREAM_SEND_ISTREAM_RESULT_WAIT_INPUT:
i_debug("waiting for input");
break;
case OSTREAM_SEND_ISTREAM_RESULT_FINISHED:
- client->body = iostream_temp_finish(&client->os_body, -1);
+ client->body =
+ iostream_temp_finish(&client->os_body, -1);
return 1;
case OSTREAM_SEND_ISTREAM_RESULT_WAIT_OUTPUT:
i_panic("Cannot write to ostream-temp");
return 0;
}
-static
-void test_program_run(struct test_client *client)
+static void test_program_run(struct test_client *client)
{
const char *const *args;
unsigned int count;
test_program_client_destroy(&client);
}
-static
-void test_program_input(struct test_client *client)
-
+static void test_program_input(struct test_client *client)
{
const char *line = "";
int ret = 0;
if (client->state == CLIENT_STATE_BODY) {
ret = test_program_input_handle(client, NULL);
break;
- } else {
- while (client->state != CLIENT_STATE_BODY &&
- (line=i_stream_read_next_line(client->in)) != NULL) {
- if (test_program_input_handle(client, line) < 0) {
- i_warning("Client sent invalid line: %s", line);
- break;
- }
+ }
+ while (client->state != CLIENT_STATE_BODY &&
+ (line=i_stream_read_next_line(client->in)) != NULL) {
+ if (test_program_input_handle(client, line) < 0) {
+ i_warning("Client sent invalid line: %s", line);
+ break;
}
}
}
test_globals.to = timeout_add_short(100, test_program_run, client);
}
-static
-void test_program_connected(struct test_server *server)
+static void test_program_connected(struct test_server *server)
{
+ struct test_client *client;
int fd;
+
i_assert(server->client == NULL);
fd = net_accept(server->listen_fd, NULL, NULL); /* makes no sense on unix */
if (fd < 0)
i_fatal("Failed to accept connection: %m");
pool_t pool = pool_alloconly_create("test_program client", 1024);
- struct test_client *client = p_new(pool, struct test_client, 1);
+ client = p_new(pool, struct test_client, 1);
client->pool = pool;
client->fd = fd;
client->in = i_stream_create_fd(fd, -1);
server->client = client;
}
-static
-void test_program_setup(void) {
+static void test_program_setup(void)
+{
test_begin("test_program_setup");
+
test_globals.ioloop = io_loop_create();
io_loop_set_current(test_globals.ioloop);
if (test_globals.listen_fd < 0)
i_fatal("Cannot create unix listener: %m");
- test_globals.io = io_add(test_globals.listen_fd, IO_READ, test_program_connected, &test_globals);
+ test_globals.io = io_add(test_globals.listen_fd, IO_READ,
+ test_program_connected, &test_globals);
test_end();
}
-static
-void test_program_teardown(void)
+static void test_program_teardown(void)
{
test_begin("test_program_teardown");
+
if (test_globals.client != NULL)
test_program_client_destroy(&test_globals.client);
+
io_remove(&test_globals.io);
i_close_fd(&test_globals.listen_fd);
io_loop_destroy(&test_globals.ioloop);
test_end();
}
-static
-void test_program_async_callback(int result, int *ret)
+static void test_program_async_callback(int result, int *ret)
{
*ret = result;
io_loop_stop(current_ioloop);
}
-static
-void test_program_success(void) {
- test_begin("test_program_success");
+static void test_program_success(void)
+{
+ struct program_client *pc;
int ret;
const char *const args[] = {
"test_program_success", "hello", "world", NULL
};
- struct program_client *pc =
- program_client_unix_create(TEST_SOCKET, args, &pc_set, FALSE);
+ test_begin("test_program_success");
+
+ pc = program_client_unix_create(TEST_SOCKET, args, &pc_set, FALSE);
buffer_t *output = buffer_create_dynamic(default_pool, 16);
struct ostream *os = test_ostream_create(output);
test_end();
}
-static
-void test_program_io(void) {
- test_begin("test_program_io (async)");
-
+static void test_program_io(void)
+{
+ struct program_client *pc;
int ret;
const char *const args[] = {
"test_program_io", NULL
};
- struct program_client *pc =
- program_client_unix_create(TEST_SOCKET, args, &pc_set, FALSE);
+ test_begin("test_program_io (async)");
+
+ pc = program_client_unix_create(TEST_SOCKET, args, &pc_set, FALSE);
struct istream *is = test_istream_create(pclient_test_io_string);
program_client_set_input(pc, is);
test_end();
}
-static
-void test_program_failure(void) {
- test_begin("test_program_failure");
-
+static void test_program_failure(void)
+{
+ struct program_client *pc;
int ret;
const char *const args[] = {
"test_program_failure", NULL
};
- struct program_client *pc =
- program_client_unix_create(TEST_SOCKET, args, &pc_set, FALSE);
+ test_begin("test_program_failure");
+
+ pc = program_client_unix_create(TEST_SOCKET, args, &pc_set, FALSE);
buffer_t *output = buffer_create_dynamic(default_pool, 16);
struct ostream *os = test_ostream_create(output);
test_end();
}
-static
-void test_program_noreply(void) {
- test_begin("test_program_noreply");
-
+static void test_program_noreply(void)
+{
+ struct program_client *pc;
int ret;
const char *const args[] = {
"test_program_success", "hello", "world", NULL
};
- struct program_client *pc =
- program_client_unix_create(TEST_SOCKET, args, &pc_set, TRUE);
+ test_begin("test_program_noreply");
+
+ pc = program_client_unix_create(TEST_SOCKET, args, &pc_set, TRUE);
program_client_run_async(pc, test_program_async_callback, &ret);