bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2002-2018 Dovecot authors, see the included COPYING file
8177c14444271eeec2bc3d965f7898322e16e358Aki Tuomistatic const char *TEST_SOCKET = "program-client-test.sock";
0a017e924aa2490f6a54096d11e534b2a7554ebeAki Tuomistatic const char *pclient_test_io_string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.\n"
0a017e924aa2490f6a54096d11e534b2a7554ebeAki Tuomi "Praesent vehicula ac leo vel placerat. Nullam placerat \n"
0a017e924aa2490f6a54096d11e534b2a7554ebeAki Tuomi "volutpat leo, sed ultricies felis pulvinar quis. Nam \n"
0a017e924aa2490f6a54096d11e534b2a7554ebeAki Tuomi "tempus, augue ut tempor cursus, neque felis commodo lacus, \n"
0a017e924aa2490f6a54096d11e534b2a7554ebeAki Tuomi "sit amet tincidunt arcu justo vel augue. Proin dapibus \n"
0a017e924aa2490f6a54096d11e534b2a7554ebeAki Tuomi "vulputate maximus. Mauris congue lacus felis, sed varius \n"
0a017e924aa2490f6a54096d11e534b2a7554ebeAki Tuomi "leo finibus sagittis. Cum sociis natoque penatibus et magnis \n"
0a017e924aa2490f6a54096d11e534b2a7554ebeAki Tuomi "dis parturient montes, nascetur ridiculus mus. Aliquam \n"
0a017e924aa2490f6a54096d11e534b2a7554ebeAki Tuomi "laoreet arcu a hendrerit consequat. Duis vitae erat tellus.";
0a017e924aa2490f6a54096d11e534b2a7554ebeAki Tuomivoid test_program_client_destroy(struct test_client **_client)
0a017e924aa2490f6a54096d11e534b2a7554ebeAki Tuomi i_error("output error: %s", o_stream_get_error(client->out));
4755f8d37a2abb18f3783b4102c26fcd11a9f379Timo Sirainenint test_program_input_handle(struct test_client *client, const char *line)
fba2690b7b40d123ecc7271d041b1571358d177dStephan Bosch test_assert((cmp = strncmp(line, "VERSION\tscript\t", 15)) == 0);
0a017e924aa2490f6a54096d11e534b2a7554ebeAki Tuomi if (cmp == 0) {
8177c14444271eeec2bc3d965f7898322e16e358Aki Tuomi client->os_body = iostream_temp_create_named(".dovecot.test.", 0, "test_program_input body");
0a017e924aa2490f6a54096d11e534b2a7554ebeAki Tuomi switch(o_stream_send_istream(client->os_body, client->in)) {
0a017e924aa2490f6a54096d11e534b2a7554ebeAki Tuomi client->body = iostream_temp_finish(&client->os_body, -1);
870bf394d43e653dd7669093a12bafbfc0b8fe92Timo Sirainen if (strcmp(args[0], "test_program_success")==0) {
0a017e924aa2490f6a54096d11e534b2a7554ebeAki Tuomi /* return hello world */
0a017e924aa2490f6a54096d11e534b2a7554ebeAki Tuomi o_stream_nsend_str(client->out, t_strdup_printf("%s %s\n+\n",
870bf394d43e653dd7669093a12bafbfc0b8fe92Timo Sirainen } else if (strcmp(args[0], "test_program_io")==0) {
870bf394d43e653dd7669093a12bafbfc0b8fe92Timo Sirainen } else if (strcmp(args[0], "test_program_failure")==0) {
0a017e924aa2490f6a54096d11e534b2a7554ebeAki Tuomivoid test_program_input(struct test_client *client)
1c85a443bba5caca70c19dc05b609c29aef9049fStephan Bosch ret = test_program_input_handle(client, NULL);
1c85a443bba5caca70c19dc05b609c29aef9049fStephan Bosch (line=i_stream_read_next_line(client->in)) != NULL) {
1c85a443bba5caca70c19dc05b609c29aef9049fStephan Bosch if (test_program_input_handle(client, line) < 0) {
1c85a443bba5caca70c19dc05b609c29aef9049fStephan Bosch i_warning("Client sent invalid line: %s", line);
1c85a443bba5caca70c19dc05b609c29aef9049fStephan Bosch /* incur slight delay to check if the connection gets
1c85a443bba5caca70c19dc05b609c29aef9049fStephan Bosch prematurely closed */
1c85a443bba5caca70c19dc05b609c29aef9049fStephan Bosch test_globals.to = timeout_add_short(100, test_program_run, client);
0a017e924aa2490f6a54096d11e534b2a7554ebeAki Tuomivoid test_program_connected(struct test_server *server)
0a017e924aa2490f6a54096d11e534b2a7554ebeAki Tuomi fd = net_accept(server->listen_fd, NULL, NULL); /* makes no sense on unix */
0a017e924aa2490f6a54096d11e534b2a7554ebeAki Tuomi pool_t pool = pool_alloconly_create("test_program client", 1024);
0a017e924aa2490f6a54096d11e534b2a7554ebeAki Tuomi struct test_client *client = p_new(pool, struct test_client, 1);
0a017e924aa2490f6a54096d11e534b2a7554ebeAki Tuomi client->io = io_add_istream(client->in, test_program_input, client);
0a017e924aa2490f6a54096d11e534b2a7554ebeAki Tuomi /* create listener */
0a017e924aa2490f6a54096d11e534b2a7554ebeAki Tuomi test_globals.listen_fd = net_listen_unix_unlink_stale(TEST_SOCKET, 100);
0a017e924aa2490f6a54096d11e534b2a7554ebeAki Tuomi test_globals.io = io_add(test_globals.listen_fd, IO_READ, test_program_connected, &test_globals);
0a017e924aa2490f6a54096d11e534b2a7554ebeAki Tuomi test_program_client_destroy(&test_globals.client);
0a017e924aa2490f6a54096d11e534b2a7554ebeAki Tuomivoid test_program_async_callback(int result, int *ret)
0a017e924aa2490f6a54096d11e534b2a7554ebeAki Tuomi program_client_unix_create(TEST_SOCKET, args, &pc_set, FALSE);
0a017e924aa2490f6a54096d11e534b2a7554ebeAki Tuomi buffer_t *output = buffer_create_dynamic(default_pool, 16);
0a017e924aa2490f6a54096d11e534b2a7554ebeAki Tuomi program_client_run_async(pc, test_program_async_callback, &ret);
0a017e924aa2490f6a54096d11e534b2a7554ebeAki Tuomi test_assert(strcmp(str_c(output), "hello world\n") == 0);
0a017e924aa2490f6a54096d11e534b2a7554ebeAki Tuomi program_client_unix_create(TEST_SOCKET, args, &pc_set, FALSE);
0a017e924aa2490f6a54096d11e534b2a7554ebeAki Tuomi struct istream *is = test_istream_create(pclient_test_io_string);
0a017e924aa2490f6a54096d11e534b2a7554ebeAki Tuomi buffer_t *output = buffer_create_dynamic(default_pool, 16);
0a017e924aa2490f6a54096d11e534b2a7554ebeAki Tuomi program_client_run_async(pc, test_program_async_callback, &ret);
0a017e924aa2490f6a54096d11e534b2a7554ebeAki Tuomi test_assert(strcmp(str_c(output), pclient_test_io_string) == 0);
0a017e924aa2490f6a54096d11e534b2a7554ebeAki Tuomi program_client_unix_create(TEST_SOCKET, args, &pc_set, FALSE);
0a017e924aa2490f6a54096d11e534b2a7554ebeAki Tuomi buffer_t *output = buffer_create_dynamic(default_pool, 16);
0a017e924aa2490f6a54096d11e534b2a7554ebeAki Tuomi program_client_run_async(pc, test_program_async_callback, &ret);
0a017e924aa2490f6a54096d11e534b2a7554ebeAki Tuomi program_client_unix_create(TEST_SOCKET, args, &pc_set, TRUE);
0a017e924aa2490f6a54096d11e534b2a7554ebeAki Tuomi program_client_run_async(pc, test_program_async_callback, &ret);
0a017e924aa2490f6a54096d11e534b2a7554ebeAki Tuomi void (*tests[])(void) = {