]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib-program-client: Reformat test-program-client-net.c.
authorStephan Bosch <stephan.bosch@dovecot.fi>
Sun, 25 Feb 2018 09:34:16 +0000 (10:34 +0100)
committerAki Tuomi <aki.tuomi@open-xchange.com>
Tue, 25 Jun 2019 09:22:29 +0000 (12:22 +0300)
src/lib-program-client/test-program-client-net.c

index 85f1bffbffd4277577747ab6e6e99b286a2337a8..f6cc468897c1bbaa8826d4b42a44003795f318cc 100644 (file)
@@ -1,5 +1,4 @@
-/* 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 *pclient_test_io_string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.\r\n"
-                                           "Praesent vehicula ac leo vel placerat. Nullam placerat \r\n"
-                                           "volutpat leo, sed ultricies felis pulvinar quis. Nam \r\n"
-                                           "tempus, augue ut tempor cursus, neque felis commodo lacus, \r\n"
-                                           "sit amet tincidunt arcu justo vel augue. Proin dapibus \r\n"
-                                           "vulputate maximus. Mauris congue lacus felis, sed varius \r\n"
-                                           "leo finibus sagittis. Cum sociis natoque penatibus et magnis \r\n"
-                                           "dis parturient montes, nascetur ridiculus mus. Aliquam \r\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.\r\n"
+       "Praesent vehicula ac leo vel placerat. Nullam placerat \r\n"
+       "volutpat leo, sed ultricies felis pulvinar quis. Nam \r\n"
+       "tempus, augue ut tempor cursus, neque felis commodo lacus, \r\n"
+       "sit amet tincidunt arcu justo vel augue. Proin dapibus \r\n"
+       "vulputate maximus. Mauris congue lacus felis, sed varius \r\n"
+       "leo finibus sagittis. Cum sociis natoque penatibus et magnis \r\n"
+       "dis parturient montes, nascetur ridiculus mus. Aliquam \r\n"
+       "laoreet arcu a hendrerit consequat. Duis vitae erat tellus.";
+
+static struct program_client_settings pc_set = {
        .client_connect_timeout_msecs = 5000,
        .input_idle_timeout_msecs = 10000,
        .debug = TRUE,
 };
 
-static
-struct test_server {
+static struct test_server {
        struct ioloop *ioloop;
        struct io *io;
        struct timeout *to;
@@ -62,22 +60,19 @@ struct test_client {
        } state;
 };
 
-static
-void test_program_io_loop_run(void)
+static void test_program_io_loop_run(void)
 {
        if (test_globals.io_loop_ref++ == 0)
                io_loop_run(current_ioloop);
 }
 
-static
-void test_program_io_loop_stop(void)
+static void test_program_io_loop_stop(void)
 {
        if (--test_globals.io_loop_ref == 0)
                io_loop_stop(current_ioloop);
 }
 
-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;
@@ -96,8 +91,8 @@ void test_program_client_destroy(struct test_client **_client)
        test_program_io_loop_stop();
 }
 
-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 = -1;
        const char *arg;
@@ -105,7 +100,7 @@ int test_program_input_handle(struct test_client *client, const char *line)
 
        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
@@ -122,7 +117,7 @@ int test_program_input_handle(struct test_client *client, const char *line)
                        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;
@@ -131,9 +126,8 @@ int test_program_input_handle(struct test_client *client, const char *line)
                array_push_back(&client->args, &arg);
                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");
                is = client->in;
                client->in = i_stream_create_dot(is, FALSE);
                i_stream_unref(&is);
@@ -145,8 +139,8 @@ int test_program_input_handle(struct test_client *client, const char *line)
                case OSTREAM_SEND_ISTREAM_RESULT_WAIT_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");
@@ -156,8 +150,7 @@ int test_program_input_handle(struct test_client *client, const char *line)
        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;
@@ -170,19 +163,20 @@ void test_program_run(struct test_client *client)
                args = array_get(&client->args, &count);
                test_assert(count > 0);
                if (count > 0) {
-                       if (strcmp(args[0], "test_program_success")==0) {
+                       if (strcmp(args[0], "test_program_success") == 0) {
                                /* return hello world */
                                i_assert(count >= 3);
-                               o_stream_nsend_str(client->out, t_strdup_printf(
-                                                  "%s %s\r\n.\n+\n",
-                                                  args[1], args[2]));
-                       } else if (strcmp(args[0], "test_program_io")==0) {
+                               o_stream_nsend_str(client->out,
+                                       t_strdup_printf("%s %s\r\n.\n+\n",
+                                                       args[1], args[2]));
+                       } else if (strcmp(args[0], "test_program_io") == 0) {
                                os = o_stream_create_dot(client->out, FALSE);
                                o_stream_nsend_istream(os, client->body);
                                test_assert(o_stream_finish(os) > 0);
                                o_stream_unref(&os);
                                o_stream_nsend_str(client->out, "+\n");
-                       } else if (strcmp(args[0], "test_program_failure")==0) {
+                       } else if (strcmp(args[0],
+                                         "test_program_failure") == 0) {
                                o_stream_nsend_str(client->out, ".\n-\n");
                        }
                } else
@@ -191,8 +185,7 @@ void test_program_run(struct test_client *client)
        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;
@@ -210,8 +203,7 @@ void test_program_input(struct test_client *client)
                }
 
                if ((line == NULL && !client->in->eof) ||
-                   (line != NULL &&
-                    ret == 0))
+                   (line != NULL && ret == 0))
                        return;
        }
 
@@ -228,17 +220,18 @@ void test_program_input(struct test_client *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 net */
        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);
@@ -250,14 +243,16 @@ void test_program_connected(struct test_server *server)
        test_program_io_loop_run();
 }
 
-static
-void test_program_setup(void) {
+static void test_program_setup(void)
+{
+       struct ip_addr ip;
+
        test_begin("test_program_setup");
+
        test_globals.ioloop = io_loop_create();
        io_loop_set_current(test_globals.ioloop);
 
        /* create listener */
-       struct ip_addr ip;
        test_globals.port = 0;
        test_assert(net_addr2ip("127.0.0.1", &ip) == 0);
 
@@ -271,10 +266,10 @@ void test_program_setup(void) {
        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);
@@ -283,25 +278,25 @@ void test_program_teardown(void)
        test_end();
 }
 
-static
-void test_program_async_callback(int result, int *ret)
+static void test_program_async_callback(int result, int *ret)
 {
        *ret = result;
        test_program_io_loop_stop();
 }
 
-static
-void test_program_success(void) {
-       test_begin("test_program_success");
+static void test_program_success(void)
+{
+       struct program_client *pc;
        int ret = -2;
 
        const char *const args[] = {
                "test_program_success", "hello", "world", NULL
        };
 
-       struct program_client *pc =
-               program_client_net_create("127.0.0.1", test_globals.port, args,
-                                         &pc_set, FALSE);
+       test_begin("test_program_success");     
+
+       pc = program_client_net_create("127.0.0.1", test_globals.port, args,
+                                      &pc_set, FALSE);
 
        buffer_t *output = buffer_create_dynamic(default_pool, 16);
        struct ostream *os = test_ostream_create(output);
@@ -325,19 +320,19 @@ void test_program_success(void) {
        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 = -2;
 
        const char *const args[] = {
                "test_program_io", NULL
        };
 
-       struct program_client *pc =
-               program_client_net_create("127.0.0.1", test_globals.port, args,
-                                         &pc_set, FALSE);
+       test_begin("test_program_io (async)");
+
+       pc = program_client_net_create("127.0.0.1", test_globals.port, args,
+                                      &pc_set, FALSE);
 
        struct istream *is = test_istream_create(pclient_test_io_string);
        program_client_set_input(pc, is);
@@ -365,19 +360,19 @@ void test_program_io(void) {
        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 = -2;
 
        const char *const args[] = {
                "test_program_failure", NULL
        };
 
-       struct program_client *pc =
-               program_client_net_create("127.0.0.1", test_globals.port, args,
-                                         &pc_set, FALSE);
+       test_begin("test_program_failure");
+
+       pc = program_client_net_create("127.0.0.1", test_globals.port, args,
+                                      &pc_set, FALSE);
 
        buffer_t *output = buffer_create_dynamic(default_pool, 16);
        struct ostream *os = test_ostream_create(output);
@@ -400,19 +395,19 @@ void test_program_failure(void) {
        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 = -2;
 
        const char *const args[] = {
                "test_program_success", "hello", "world", NULL
        };
 
-       struct program_client *pc =
-               program_client_net_create("127.0.0.1", test_globals.port, args,
-                                         &pc_set, TRUE);
+       test_begin("test_program_noreply");
+
+       pc = program_client_net_create("127.0.0.1", test_globals.port, args,
+                                      &pc_set, TRUE);
 
        program_client_run_async(pc, test_program_async_callback, &ret);
 
@@ -428,28 +423,28 @@ void test_program_noreply(void) {
        test_end();
 }
 
-static
-void test_program_refused(void) {
-       test_begin("test_program_refused");
-
+static void test_program_refused(void)
+{
+       struct program_client *pc;
+       struct ip_addr ips[4];
        int ret = -2;
 
        const char *const args[] = {
                "test_program_success", "hello", "world", NULL
        };
 
-       struct ip_addr ips[4];
+       test_begin("test_program_refused");
+
        if (net_addr2ip("::1", &ips[0]) < 0 ||
            net_addr2ip("127.0.0.3", &ips[1]) < 0 ||
            net_addr2ip("127.0.0.2", &ips[2]) < 0 ||
            net_addr2ip("127.0.0.1", &ips[3]) < 0) {
                i_fatal("Cannot convert addresses");
        }
-
-       struct program_client *pc =
-               program_client_net_create_ips(ips, N_ELEMENTS(ips),
-                                             test_globals.port, args,
-                                             &pc_set, TRUE);
+       
+       pc = program_client_net_create_ips(ips, N_ELEMENTS(ips),
+                                          test_globals.port, args,
+                                          &pc_set, TRUE);
 
        test_expect_errors(N_ELEMENTS(ips)-1);
        program_client_run_async(pc, test_program_async_callback, &ret);
@@ -464,7 +459,6 @@ void test_program_refused(void) {
        test_end();
 }
 
-
 int main(void)
 {
        void (*tests[])(void) = {