Skip to content

Commit

Permalink
Cleaning up tests, guarding against invalid inputs.
Browse files Browse the repository at this point in the history
  • Loading branch information
tjsaunders committed Aug 5, 2015
1 parent 98bd30a commit 8a7f16b
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 6 deletions.
32 changes: 28 additions & 4 deletions lib/proxy/db.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ int proxy_db_exec_stmt(pool *p, const char *stmt, const char **errstr) {
char *ptr = NULL;
unsigned int nretries = 0;

if (stmt == NULL) {
if (p == NULL ||
stmt == NULL) {
errno = EINVAL;
return -1;
}
Expand Down Expand Up @@ -141,7 +142,8 @@ int proxy_db_bind_stmt(pool *p, const char *stmt, int idx, int type,
sqlite3_stmt *pstmt;
int res;

if (stmt == NULL) {
if (p == NULL ||
stmt == NULL) {
errno = EINVAL;
return -1;
}
Expand Down Expand Up @@ -169,6 +171,11 @@ int proxy_db_bind_stmt(pool *p, const char *stmt, int idx, int type,
case PROXY_DB_BIND_TYPE_INT: {
int i;

if (data == NULL) {
errno = EINVAL;
return -1;
}

i = *((int *) data);
res = sqlite3_bind_int(pstmt, idx, i);
if (res != SQLITE_OK) {
Expand All @@ -182,6 +189,11 @@ int proxy_db_bind_stmt(pool *p, const char *stmt, int idx, int type,
case PROXY_DB_BIND_TYPE_LONG: {
long l;

if (data == NULL) {
errno = EINVAL;
return -1;
}

l = *((long *) data);
res = sqlite3_bind_int(pstmt, idx, l);
if (res != SQLITE_OK) {
Expand All @@ -195,6 +207,11 @@ int proxy_db_bind_stmt(pool *p, const char *stmt, int idx, int type,
case PROXY_DB_BIND_TYPE_TEXT: {
const char *text;

if (data == NULL) {
errno = EINVAL;
return -1;
}

text = (const char *) data;
res = sqlite3_bind_text(pstmt, idx, text, -1, NULL);
if (res != SQLITE_OK) {
Expand Down Expand Up @@ -228,7 +245,8 @@ int proxy_db_finish_stmt(pool *p, const char *stmt) {
sqlite3_stmt *pstmt;
int res;

if (stmt == NULL) {
if (p == NULL ||
stmt == NULL) {
errno = EINVAL;
return -1;
}
Expand Down Expand Up @@ -265,7 +283,8 @@ array_header *proxy_db_exec_prepared_stmt(pool *p, const char *stmt,
int readonly = FALSE, res;
array_header *results = NULL;

if (stmt == NULL) {
if (p == NULL ||
stmt == NULL) {
errno = EINVAL;
return NULL;
}
Expand Down Expand Up @@ -441,6 +460,11 @@ int proxy_db_open(pool *p, const char *table_path) {
}

int proxy_db_close(pool *p) {
if (p == NULL) {
errno = EINVAL;
return -1;
}

if (proxy_dbh != NULL) {
sqlite3_stmt *pstmt;
int res;
Expand Down
4 changes: 2 additions & 2 deletions t/api/conn.c
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ START_TEST (conn_get_hostport_test) {
}
END_TEST

START_TEST (conn_get_url_test) {
START_TEST (conn_get_uri_test) {
struct proxy_conn *pconn;
const char *pconn_url, *url;

Expand Down Expand Up @@ -176,7 +176,7 @@ Suite *tests_get_conn_suite(void) {
tcase_add_test(testcase, conn_create_test);
tcase_add_test(testcase, conn_get_addr_test);
tcase_add_test(testcase, conn_get_hostport_test);
tcase_add_test(testcase, conn_get_url_test);
tcase_add_test(testcase, conn_get_uri_test);

suite_add_tcase(suite, testcase);
return suite;
Expand Down
58 changes: 58 additions & 0 deletions t/api/db.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,26 +58,84 @@ START_TEST (db_open_test) {
END_TEST

START_TEST (db_close_test) {
int res;

res = proxy_db_close(NULL);
fail_unless(res == -1, "Failed to handle null arguments");
fail_unless(errno == EINVAL, "Failed to set errno to EINVAL, got %s (%d)",
strerror(errno), errno);
}
END_TEST

START_TEST (db_prepare_stmt_test) {
int res;

res = proxy_db_prepare_stmt(NULL, NULL);
fail_unless(res == -1, "Failed to handle null arguments");
fail_unless(errno == EINVAL, "Failed to set errno to EINVAL, got %s (%d)",
strerror(errno), errno);
}
END_TEST

START_TEST (db_finish_stmt_test) {
int res;
const char *stmt;

res = proxy_db_finish_stmt(NULL, NULL);
fail_unless(res == -1, "Failed to handle null arguments");
fail_unless(errno == EINVAL, "Failed to set errno to EINVAL, got %s (%d)",
strerror(errno), errno);

stmt = "SELECT COUNT(*) FROM table";
res = proxy_db_finish_stmt(p, stmt);
fail_unless(res == -1, "Failed to handle unprepared statement");
fail_unless(errno == ENOENT, "Failed to set errno to ENOENT, got %s (%d)",
strerror(errno), errno);
}
END_TEST

START_TEST (db_bind_stmt_test) {
int res;
const char *stmt;
int idx, type;

res = proxy_db_bind_stmt(NULL, NULL, -1, -1, NULL);
fail_unless(res == -1, "Failed to handle null arguments");
fail_unless(errno == EINVAL, "Failed to set errno to EINVAL, got %s (%d)",
strerror(errno), errno);

stmt = "SELECT COUNT(*) FROM table";
idx = -1;
res = proxy_db_bind_stmt(p, stmt, idx, PROXY_DB_BIND_TYPE_INT, NULL);
fail_unless(res == -1, "Failed to handle invalid index %d", idx);
fail_unless(errno == EINVAL, "Failed to set errno to EINVAL, got %s (%d)",
strerror(errno), errno);

idx = 1;
res = proxy_db_bind_stmt(p, stmt, idx, PROXY_DB_BIND_TYPE_INT, NULL);
fail_unless(res == -1, "Failed to handle unprepared statement");
fail_unless(errno == ENOENT, "Failed to set errno to ENOENT, got %s (%d)",
strerror(errno), errno);
}
END_TEST

START_TEST (db_exec_stmt_test) {
int res;

res = proxy_db_exec_stmt(NULL, NULL, NULL);
fail_unless(res == -1, "Failed to handle null arguments");
fail_unless(errno == EINVAL, "Failed to set errno to EINVAL, got %s (%d)",
strerror(errno), errno);
}
END_TEST

START_TEST (db_exec_prepared_stmt_test) {
array_header *res;

res = proxy_db_exec_prepared_stmt(NULL, NULL, NULL);
fail_unless(res == NULL, "Failed to handle null arguments");
fail_unless(errno == EINVAL, "Failed to set errno to EINVAL, got %s (%d)",
strerror(errno), errno);
}
END_TEST

Expand Down

0 comments on commit 8a7f16b

Please sign in to comment.