summaryrefslogtreecommitdiff
path: root/unit
diff options
context:
space:
mode:
authorJohan Hedberg <johan.hedberg@intel.com>2011-07-06 17:30:38 +0200
committerMarcel Holtmann <marcel@holtmann.org>2012-12-04 22:22:00 +0100
commit98703e92d1bcfe6c5d0129dad14697ad649997d2 (patch)
tree64955fb37e2a0ada07cb9fcebf9057322b8f2647 /unit
parent6abdccbf94dd16c3c9d4619fd969826a02564105 (diff)
downloadbluez-98703e92d1bcfe6c5d0129dad14697ad649997d2.tar.gz
gobex: Add g_obex_get_rsp
Diffstat (limited to 'unit')
-rw-r--r--unit/test-gobex-transfer.c125
1 files changed, 123 insertions, 2 deletions
diff --git a/unit/test-gobex-transfer.c b/unit/test-gobex-transfer.c
index a94a62d29..6039dc091 100644
--- a/unit/test-gobex-transfer.c
+++ b/unit/test-gobex-transfer.c
@@ -158,7 +158,7 @@ static void transfer_complete(GObex *obex, GError *err, gpointer user_data)
g_main_loop_quit(mainloop);
}
-static gssize put_provide_data(void *buf, gsize len, gpointer user_data)
+static gssize provide_data(void *buf, gsize len, gpointer user_data)
{
struct test_data *d = user_data;
@@ -194,7 +194,7 @@ static void test_put_req(void)
timer_id = g_timeout_add_seconds(1, test_timeout, &d);
- g_obex_put_req(obex, "foo/bar", "file.txt", put_provide_data,
+ g_obex_put_req(obex, "foo/bar", "file.txt", provide_data,
transfer_complete, &d, &d.err);
g_assert_no_error(d.err);
@@ -444,6 +444,126 @@ static void test_get_req(void)
g_assert_no_error(d.err);
}
+static gboolean get_cli(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;
+
+ d->count++;
+
+ if (d->count > 1) {
+ expect = (const char *) get_rsp_last;
+ expect_len = sizeof(get_rsp_last);
+ send_buf = NULL;
+ send_buf_len = 0;
+ } else {
+ expect = (const char *) get_rsp_first;
+ expect_len = sizeof(get_rsp_first);
+ send_buf = (const char *) get_req_last;
+ send_buf_len = sizeof(get_req_last);
+ }
+
+ status = g_io_channel_read_chars(io, buf, sizeof(buf), &rbytes, NULL);
+ if (status != G_IO_STATUS_NORMAL) {
+ g_print("get_cli 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("get_cli 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("get_cli 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;
+ }
+
+ if (send_buf == NULL)
+ return TRUE;
+
+ g_io_channel_write_chars(io, send_buf, send_buf_len, &bytes_written,
+ NULL);
+ if (bytes_written != send_buf_len) {
+ g_print("get_cli 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(mainloop);
+ return FALSE;
+}
+
+static void handle_get(GObex *obex, GObexPacket *req, gpointer user_data)
+{
+ struct test_data *d = user_data;
+ guint8 op = g_obex_packet_get_operation(req, NULL);
+ guint id;
+
+ if (op != G_OBEX_OP_GET) {
+ d->err = g_error_new(TEST_ERROR, TEST_ERROR_UNEXPECTED,
+ "Unexpected opcode 0x%02x", op);
+ g_main_loop_quit(mainloop);
+ return;
+ }
+
+ id = g_obex_get_rsp(obex, req, provide_data, transfer_complete,
+ d, &d->err);
+ if (id == 0)
+ g_main_loop_quit(mainloop);
+}
+
+static void test_get_rsp(void)
+{
+ GIOChannel *io;
+ GIOCondition cond;
+ guint io_id, timer_id;
+ GObex *obex;
+ struct test_data d = { 0, NULL };
+
+ create_endpoints(&obex, &io, SOCK_STREAM);
+
+ cond = G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL;
+ io_id = g_io_add_watch(io, cond, get_cli, &d);
+
+ mainloop = g_main_loop_new(NULL, FALSE);
+
+ timer_id = g_timeout_add_seconds(1, test_timeout, &d);
+
+ g_obex_add_request_function(obex, G_OBEX_OP_GET, handle_get, &d);
+
+ g_io_channel_write_chars(io, (char *) get_req_first,
+ sizeof(get_req_first), NULL, &d.err);
+ g_assert_no_error(d.err);
+
+ g_main_loop_run(mainloop);
+
+ g_main_loop_unref(mainloop);
+ mainloop = NULL;
+
+ g_source_remove(timer_id);
+ g_io_channel_unref(io);
+ g_source_remove(io_id);
+ g_obex_unref(obex);
+
+ g_assert_no_error(d.err);
+}
+
int main(int argc, char *argv[])
{
g_test_init(&argc, &argv, NULL);
@@ -452,6 +572,7 @@ int main(int argc, char *argv[])
g_test_add_func("/gobex/test_put_rsp", test_put_rsp);
g_test_add_func("/gobex/test_get_req", test_get_req);
+ g_test_add_func("/gobex/test_get_rsp", test_get_rsp);
g_test_run();