bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2017-2018 Dovecot authors, see the included COPYING file */
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomistatic struct sql_db *driver_test_mysql_init(const char *connect_string);
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomistatic struct sql_db *driver_test_cassandra_init(const char *connect_string);
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomistatic struct sql_db *driver_test_sqlite_init(const char *connect_string);
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomistatic void driver_test_deinit(struct sql_db *_db);
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomistatic int driver_test_connect(struct sql_db *_db);
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomistatic void driver_test_disconnect(struct sql_db *_db);
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomistatic const char *
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomidriver_test_mysql_escape_string(struct sql_db *_db, const char *string);
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomistatic const char *
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomidriver_test_escape_string(struct sql_db *_db, const char *string);
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomistatic void driver_test_exec(struct sql_db *_db, const char *query);
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomistatic void driver_test_query(struct sql_db *_db, const char *query,
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomistatic struct sql_result *
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomidriver_test_query_s(struct sql_db *_db, const char *query);
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomistatic void driver_test_transaction_commit(struct sql_transaction_context *ctx,
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomidriver_test_transaction_commit_s(struct sql_transaction_context *ctx,
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomi const char **error_r);
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomidriver_test_transaction_rollback(struct sql_transaction_context *ctx);
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomidriver_test_update(struct sql_transaction_context *ctx, const char *query,
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomi unsigned int *affected_rows);
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomistatic const char *
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomidriver_test_mysql_escape_blob(struct sql_db *_db, const unsigned char *data,
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomistatic const char *
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomidriver_test_escape_blob(struct sql_db *_db, const unsigned char *data,
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomistatic void driver_test_result_free(struct sql_result *result);
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomistatic int driver_test_result_next_row(struct sql_result *result);
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomistatic unsigned int
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomidriver_test_result_get_fields_count(struct sql_result *result);
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomistatic const char *
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomidriver_test_result_get_field_name(struct sql_result *result, unsigned int idx);
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomidriver_test_result_find_field(struct sql_result *result, const char *field_name);
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomistatic const char *
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomidriver_test_result_get_field_value(struct sql_result *result, unsigned int idx);
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomistatic const unsigned char *
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomidriver_test_result_get_field_value_binary(struct sql_result *result,
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomistatic const char *
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomidriver_test_result_find_field_value(struct sql_result *result,
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomi const char *field_name);
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomistatic const char *const *
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomidriver_test_result_get_values(struct sql_result *result);
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomiconst char *driver_test_result_get_error(struct sql_result *result);
ef597c4619eb021563f659b886c67762fce7a817Timo Sirainen .escape_string = driver_test_mysql_escape_string,
ef597c4619eb021563f659b886c67762fce7a817Timo Sirainen .transaction_begin = driver_test_transaction_begin,
ef597c4619eb021563f659b886c67762fce7a817Timo Sirainen .transaction_commit = driver_test_transaction_commit,
ef597c4619eb021563f659b886c67762fce7a817Timo Sirainen .transaction_commit_s = driver_test_transaction_commit_s,
ef597c4619eb021563f659b886c67762fce7a817Timo Sirainen .transaction_rollback = driver_test_transaction_rollback,
ef597c4619eb021563f659b886c67762fce7a817Timo Sirainen .transaction_begin = driver_test_transaction_begin,
ef597c4619eb021563f659b886c67762fce7a817Timo Sirainen .transaction_commit = driver_test_transaction_commit,
ef597c4619eb021563f659b886c67762fce7a817Timo Sirainen .transaction_commit_s = driver_test_transaction_commit_s,
ef597c4619eb021563f659b886c67762fce7a817Timo Sirainen .transaction_rollback = driver_test_transaction_rollback,
ef597c4619eb021563f659b886c67762fce7a817Timo Sirainen .transaction_begin = driver_test_transaction_begin,
ef597c4619eb021563f659b886c67762fce7a817Timo Sirainen .transaction_commit = driver_test_transaction_commit,
ef597c4619eb021563f659b886c67762fce7a817Timo Sirainen .transaction_commit_s = driver_test_transaction_commit_s,
ef597c4619eb021563f659b886c67762fce7a817Timo Sirainen .transaction_rollback = driver_test_transaction_rollback,
ef597c4619eb021563f659b886c67762fce7a817Timo Sirainen .get_fields_count = driver_test_result_get_fields_count,
ef597c4619eb021563f659b886c67762fce7a817Timo Sirainen .get_field_name = driver_test_result_get_field_name,
ef597c4619eb021563f659b886c67762fce7a817Timo Sirainen .get_field_value = driver_test_result_get_field_value,
ef597c4619eb021563f659b886c67762fce7a817Timo Sirainen .get_field_value_binary = driver_test_result_get_field_value_binary,
ef597c4619eb021563f659b886c67762fce7a817Timo Sirainen .find_field_value = driver_test_result_find_field_value,
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomistatic struct sql_db *driver_test_init(const struct sql_db *driver,
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomi pool_t pool = pool_alloconly_create(MEMPOOL_GROWING" test sql driver", 2048);
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomi struct test_sql_db *ret = p_new(pool, struct test_sql_db, 1);
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomistatic struct sql_db *driver_test_mysql_init(const char *connect_string)
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomi return driver_test_init(&driver_test_mysql_db, connect_string);
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomistatic struct sql_db *driver_test_cassandra_init(const char *connect_string)
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomi return driver_test_init(&driver_test_cassandra_db, connect_string);
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomistatic struct sql_db *driver_test_sqlite_init(const char *connect_string)
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomi return driver_test_init(&driver_test_sqlite_db, connect_string);
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomistatic void driver_test_deinit(struct sql_db *_db ATTR_UNUSED)
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomi struct test_sql_db *db = (struct test_sql_db*)_db;
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomistatic int driver_test_connect(struct sql_db *_db ATTR_UNUSED)
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomistatic void driver_test_disconnect(struct sql_db *_db ATTR_UNUSED)
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomistatic const char *
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomidriver_test_mysql_escape_string(struct sql_db *_db ATTR_UNUSED,
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomi const char *string)
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomi for(const char *ptr = string; *ptr != '\0'; ptr++) {
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomi if (*ptr == '\n' || *ptr == '\r' || *ptr == '\\' ||
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomistatic const char *
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomidriver_test_escape_string(struct sql_db *_db ATTR_UNUSED, const char *string)
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomistatic void driver_test_exec(struct sql_db *_db, const char *query)
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomi struct test_sql_db *db = (struct test_sql_db*)_db;
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomi/* i_debug("DUMMY EXECUTE: %s", query);
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomi i_debug("DUMMY EXPECT : %s", result->queries[result->cur]); */
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomi test_assert(strcmp(result->queries[result->cur], query)==0);
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomi if (strcmp(result->queries[result->cur], query) != 0) {
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomidriver_test_query(struct sql_db *_db, const char *query,
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomi struct sql_result *result = driver_test_query_s(_db, query);
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomistatic struct sql_result *
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomidriver_test_query_s(struct sql_db *_db, const char *query)
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomi struct test_sql_db *db = (struct test_sql_db*)_db;
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomi struct test_sql_result *res = i_new(struct test_sql_result, 1);
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomi res->result = i_new(struct test_driver_result, 1);
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomi /* drop it from array if it's used up */
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomidriver_test_transaction_commit(struct sql_transaction_context *ctx,
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomi res.error_type = driver_test_transaction_commit_s(ctx, &res.error);
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomidriver_test_transaction_commit_s(struct sql_transaction_context *ctx,
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomi const char **error_r)
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomi struct test_sql_db *db = (struct test_sql_db*)ctx->db;
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomidriver_test_transaction_rollback(struct sql_transaction_context *ctx)
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomi struct test_sql_db *db = (struct test_sql_db*)ctx->db;
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomidriver_test_update(struct sql_transaction_context *ctx, const char *query,
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomi unsigned int *affected_rows)
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomi struct test_sql_db *db= (struct test_sql_db*)ctx->db;
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomi /* drop it from array if it's used up */
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomistatic const char *
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomidriver_test_mysql_escape_blob(struct sql_db *_db ATTR_UNUSED,
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomi return t_strdup_printf("X'%s'", binary_to_hex(data,size));
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomistatic const char *
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomidriver_test_escape_blob(struct sql_db *_db ATTR_UNUSED,
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomi return t_strdup_printf("X'%s'", binary_to_hex(data,size));
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomistatic void driver_test_result_free(struct sql_result *result)
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomistatic int driver_test_result_next_row(struct sql_result *result)
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomi if (r == NULL) return 0;
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomistatic unsigned int
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomidriver_test_result_get_fields_count(struct sql_result *result)
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomistatic const char *
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomidriver_test_result_get_field_name(struct sql_result *result, unsigned int idx)
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomidriver_test_result_find_field(struct sql_result *result, const char *field_name)
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomistatic const char *
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomidriver_test_result_get_field_value(struct sql_result *result, unsigned int idx)
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomistatic const unsigned char *
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomidriver_test_result_get_field_value_binary(struct sql_result *result,
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomi const char *value = driver_test_result_get_field_value(result, idx);
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomi /* expect it hex encoded */
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomistatic const char *
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomidriver_test_result_find_field_value(struct sql_result *result,
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomi int idx = driver_test_result_find_field(result, field_name);
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomi return driver_test_result_get_field_value(result, idx);
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomistatic const char *const *
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomidriver_test_result_get_values(struct sql_result *result)
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomiconst char *driver_test_result_get_error(struct sql_result *result)
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomivoid sql_driver_test_add_expected_result(struct sql_db *_db,
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomi struct test_sql_db *db = (struct test_sql_db*)_db;
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomivoid sql_driver_test_clear_expected_results(struct sql_db *_db)
7b1ce34143ee2c47bf4d2bff73f9cd6e763fadc0Aki Tuomi struct test_sql_db *db = (struct test_sql_db*)_db;