bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2002-2018 Dovecot authors, see the included COPYING file
a371ea8bd48d45548cd7aa16d4f5aeb38ba48c91Aki Tuomistatic const char *pclient_test_io_string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.\r\n"
a371ea8bd48d45548cd7aa16d4f5aeb38ba48c91Aki Tuomi "Praesent vehicula ac leo vel placerat. Nullam placerat \r\n"
a371ea8bd48d45548cd7aa16d4f5aeb38ba48c91Aki Tuomi "volutpat leo, sed ultricies felis pulvinar quis. Nam \r\n"
a371ea8bd48d45548cd7aa16d4f5aeb38ba48c91Aki Tuomi "tempus, augue ut tempor cursus, neque felis commodo lacus, \r\n"
a371ea8bd48d45548cd7aa16d4f5aeb38ba48c91Aki Tuomi "sit amet tincidunt arcu justo vel augue. Proin dapibus \r\n"
a371ea8bd48d45548cd7aa16d4f5aeb38ba48c91Aki Tuomi "vulputate maximus. Mauris congue lacus felis, sed varius \r\n"
a371ea8bd48d45548cd7aa16d4f5aeb38ba48c91Aki Tuomi "leo finibus sagittis. Cum sociis natoque penatibus et magnis \r\n"
a371ea8bd48d45548cd7aa16d4f5aeb38ba48c91Aki Tuomi "dis parturient montes, nascetur ridiculus mus. Aliquam \r\n"
3f67fea2e5a12390d280315c37c80302a9dab683Aki Tuomi "laoreet arcu a hendrerit consequat. Duis vitae erat tellus.";
3f67fea2e5a12390d280315c37c80302a9dab683Aki Tuomivoid test_program_client_destroy(struct test_client **_client)
3f67fea2e5a12390d280315c37c80302a9dab683Aki Tuomi i_error("output error: %s", o_stream_get_error(client->out));
3f67fea2e5a12390d280315c37c80302a9dab683Aki Tuomiint test_program_input_handle(struct test_client *client, const char *line)
fba2690b7b40d123ecc7271d041b1571358d177dStephan Bosch test_assert((cmp = strncmp(line, "VERSION\tscript\t", 15)) == 0);
3f67fea2e5a12390d280315c37c80302a9dab683Aki Tuomi if (cmp == 0) {
a371ea8bd48d45548cd7aa16d4f5aeb38ba48c91Aki Tuomi "test_program_input body");
3f67fea2e5a12390d280315c37c80302a9dab683Aki Tuomi switch(o_stream_send_istream(client->os_body, client->in)) {
a371ea8bd48d45548cd7aa16d4f5aeb38ba48c91Aki Tuomi client->body = iostream_temp_finish(&client->os_body,
870bf394d43e653dd7669093a12bafbfc0b8fe92Timo Sirainen if (strcmp(args[0], "test_program_success")==0) {
a371ea8bd48d45548cd7aa16d4f5aeb38ba48c91Aki Tuomi /* return hello world */
a371ea8bd48d45548cd7aa16d4f5aeb38ba48c91Aki Tuomi "%s %s\r\n.\n+\n",
870bf394d43e653dd7669093a12bafbfc0b8fe92Timo Sirainen } else if (strcmp(args[0], "test_program_io")==0) {
870bf394d43e653dd7669093a12bafbfc0b8fe92Timo Sirainen } else if (strcmp(args[0], "test_program_failure")==0) {
3f67fea2e5a12390d280315c37c80302a9dab683Aki Tuomivoid test_program_input(struct test_client *client)
a371ea8bd48d45548cd7aa16d4f5aeb38ba48c91Aki Tuomi while((line = i_stream_read_next_line(client->in)) != NULL) {
a371ea8bd48d45548cd7aa16d4f5aeb38ba48c91Aki Tuomi if (ret != 0) break;
3f67fea2e5a12390d280315c37c80302a9dab683Aki Tuomi /* incur short delay to make sure the client did not disconnect
3f67fea2e5a12390d280315c37c80302a9dab683Aki Tuomi prematurely */
3f67fea2e5a12390d280315c37c80302a9dab683Aki Tuomi test_globals.to = timeout_add_short(100, test_program_run, client);
3f67fea2e5a12390d280315c37c80302a9dab683Aki Tuomivoid test_program_connected(struct test_server *server)
3f67fea2e5a12390d280315c37c80302a9dab683Aki Tuomi fd = net_accept(server->listen_fd, NULL, NULL); /* makes no sense on net */
3f67fea2e5a12390d280315c37c80302a9dab683Aki Tuomi pool_t pool = pool_alloconly_create("test_program client", 1024);
3f67fea2e5a12390d280315c37c80302a9dab683Aki Tuomi struct test_client *client = p_new(pool, struct test_client, 1);
3f67fea2e5a12390d280315c37c80302a9dab683Aki Tuomi client->io = io_add_istream(client->in, test_program_input, client);
3f67fea2e5a12390d280315c37c80302a9dab683Aki Tuomi /* create listener */
3f67fea2e5a12390d280315c37c80302a9dab683Aki Tuomi test_globals.listen_fd = net_listen(&ip, &test_globals.port, 1);
3f67fea2e5a12390d280315c37c80302a9dab683Aki Tuomi test_globals.io = io_add(test_globals.listen_fd, IO_READ,
3f67fea2e5a12390d280315c37c80302a9dab683Aki Tuomi test_program_client_destroy(&test_globals.client);
3f67fea2e5a12390d280315c37c80302a9dab683Aki Tuomivoid test_program_async_callback(int result, int *ret)
a371ea8bd48d45548cd7aa16d4f5aeb38ba48c91Aki Tuomi program_client_net_create("127.0.0.1", test_globals.port, args,
3f67fea2e5a12390d280315c37c80302a9dab683Aki Tuomi buffer_t *output = buffer_create_dynamic(default_pool, 16);
3f67fea2e5a12390d280315c37c80302a9dab683Aki Tuomi program_client_run_async(pc, test_program_async_callback, &ret);
a371ea8bd48d45548cd7aa16d4f5aeb38ba48c91Aki Tuomi test_assert(strcmp(str_c(output), "hello world") == 0);
3f67fea2e5a12390d280315c37c80302a9dab683Aki Tuomi program_client_net_create("127.0.0.1", test_globals.port, args,
3f67fea2e5a12390d280315c37c80302a9dab683Aki Tuomi struct istream *is = test_istream_create(pclient_test_io_string);
3f67fea2e5a12390d280315c37c80302a9dab683Aki Tuomi buffer_t *output = buffer_create_dynamic(default_pool, 16);
3f67fea2e5a12390d280315c37c80302a9dab683Aki Tuomi program_client_run_async(pc, test_program_async_callback, &ret);
3f67fea2e5a12390d280315c37c80302a9dab683Aki Tuomi test_assert(strcmp(str_c(output), pclient_test_io_string) == 0);
3f67fea2e5a12390d280315c37c80302a9dab683Aki Tuomi program_client_net_create("127.0.0.1", test_globals.port, args,
3f67fea2e5a12390d280315c37c80302a9dab683Aki Tuomi buffer_t *output = buffer_create_dynamic(default_pool, 16);
3f67fea2e5a12390d280315c37c80302a9dab683Aki Tuomi program_client_run_async(pc, test_program_async_callback, &ret);
3f67fea2e5a12390d280315c37c80302a9dab683Aki Tuomi program_client_net_create("127.0.0.1", test_globals.port, args,
3f67fea2e5a12390d280315c37c80302a9dab683Aki Tuomi program_client_run_async(pc, test_program_async_callback, &ret);
3f67fea2e5a12390d280315c37c80302a9dab683Aki Tuomi program_client_net_create_ips(ips, N_ELEMENTS(ips),
3f67fea2e5a12390d280315c37c80302a9dab683Aki Tuomi program_client_run_async(pc, test_program_async_callback, &ret);
3f67fea2e5a12390d280315c37c80302a9dab683Aki Tuomi void (*tests[])(void) = {