diff options
author | Johan Hedberg <johan.hedberg@intel.com> | 2011-07-09 11:06:31 +0200 |
---|---|---|
committer | Marcel Holtmann <marcel@holtmann.org> | 2012-12-04 22:22:00 +0100 |
commit | b1d071bd5cf91e227cec46e1e590dcbe9676d0e8 (patch) | |
tree | 9cb60a65afbda6546dcd155c16bc5fb9b802e10e /unit/util.c | |
parent | 60263d9a33d58baad3feb56b936ecceaedfe7f29 (diff) | |
download | bluez-b1d071bd5cf91e227cec46e1e590dcbe9676d0e8.tar.gz |
gobex: Refactor common unit test functions to util.c
Diffstat (limited to 'unit/util.c')
-rw-r--r-- | unit/util.c | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/unit/util.c b/unit/util.c index 7e23860e5..630a70ec0 100644 --- a/unit/util.c +++ b/unit/util.c @@ -112,3 +112,72 @@ void create_endpoints(GObex **obex, GIOChannel **io, int sock_type) g_io_channel_set_buffered(*io, FALSE); g_io_channel_set_close_on_unref(*io, TRUE); } + +gboolean test_timeout(gpointer user_data) +{ + struct test_data *d = user_data; + + if (!g_main_loop_is_running(d->mainloop)) + return FALSE; + + d->err = g_error_new(TEST_ERROR, TEST_ERROR_TIMEOUT, "Timed out"); + + g_main_loop_quit(d->mainloop); + + return FALSE; +} + +gboolean test_io_cb(GIOChannel *io, GIOCondition cond, gpointer user_data) +{ + struct test_data *d = user_data; + GIOStatus status; + gsize bytes_written, rbytes, send_buf_len, expect_len; + char buf[255]; + const char *send_buf, *expect; + + expect = d->recv[d->count].data; + expect_len = d->recv[d->count].len; + send_buf = d->send[d->count].data; + send_buf_len = d->send[d->count].len; + + d->count++; + + status = g_io_channel_read_chars(io, buf, sizeof(buf), &rbytes, NULL); + if (status != G_IO_STATUS_NORMAL) { + g_print("io_cb count %u\n", d->count); + g_set_error(&d->err, TEST_ERROR, TEST_ERROR_UNEXPECTED, + "Reading data failed with status %d", status); + goto failed; + } + + if (rbytes < expect_len) { + g_print("io_cb count %u\n", d->count); + dump_bufs(expect, expect_len, buf, rbytes); + g_set_error(&d->err, TEST_ERROR, TEST_ERROR_UNEXPECTED, + "Not enough data from socket"); + goto failed; + } + + if (memcmp(buf, expect, expect_len) != 0) { + g_print("io_cb count %u\n", d->count); + dump_bufs(expect, expect_len, buf, rbytes); + g_set_error(&d->err, TEST_ERROR, TEST_ERROR_UNEXPECTED, + "Received data is not correct"); + goto failed; + } + + g_io_channel_write_chars(io, send_buf, send_buf_len, &bytes_written, + NULL); + if (bytes_written != send_buf_len) { + g_print("io_cb count %u\n", d->count); + g_set_error(&d->err, TEST_ERROR, TEST_ERROR_UNEXPECTED, + "Unable to write to socket"); + goto failed; + } + + return TRUE; + +failed: + g_main_loop_quit(d->mainloop); + return FALSE; +} |