From f2b4c7bc5d2b3d789520765425c109259aee20ca Mon Sep 17 00:00:00 2001 From: Johan Hedberg Date: Wed, 29 Jun 2011 13:36:54 +0300 Subject: [PATCH] gobex: Add basic packet encoding test --- unit/test-gobex-packet.c | 37 ++++++++++++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/unit/test-gobex-packet.c b/unit/test-gobex-packet.c index fcbe398e2..ba6abbca2 100644 --- a/unit/test-gobex-packet.c +++ b/unit/test-gobex-packet.c @@ -25,6 +25,10 @@ #include "util.h" +static uint8_t pkt_put_action[] = { G_OBEX_OP_PUT, 0x00, 0x05, + G_OBEX_HDR_ID_ACTION, 0xab }; +static uint8_t pkt_put[] = { G_OBEX_OP_PUT, 0x00, 0x03 }; + static void test_pkt(void) { GObexPacket *pkt; @@ -39,10 +43,10 @@ static void test_pkt(void) static void test_decode_pkt(void) { GObexPacket *pkt; - uint8_t buf[] = { G_OBEX_OP_PUT, 0x00, 0x03 }; GError *err = NULL; - pkt = g_obex_packet_decode(buf, sizeof(buf), 0, G_OBEX_DATA_REF, &err); + pkt = g_obex_packet_decode(pkt_put, sizeof(pkt_put), 0, + G_OBEX_DATA_REF, &err); g_assert_no_error(err); g_obex_packet_free(pkt); @@ -54,11 +58,10 @@ static void test_decode_pkt_header(void) GObexHeader *header; GError *err = NULL; gboolean ret; - uint8_t buf[] = { G_OBEX_OP_PUT, 0x00, 0x05, - G_OBEX_HDR_ID_ACTION, 0xab }; guint8 val; - pkt = g_obex_packet_decode(buf, sizeof(buf), 0, G_OBEX_DATA_REF, &err); + pkt = g_obex_packet_decode(pkt_put_action, sizeof(pkt_put_action), + 0, G_OBEX_DATA_REF, &err); g_assert_no_error(err); header = g_obex_packet_get_header(pkt, G_OBEX_HDR_ID_ACTION); @@ -71,6 +74,28 @@ static void test_decode_pkt_header(void) g_obex_packet_free(pkt); } +static void test_decode_encode(void) +{ + GObexPacket *pkt; + GError *err = NULL; + uint8_t buf[255]; + gssize len; + + pkt = g_obex_packet_decode(pkt_put_action, sizeof(pkt_put_action), + 0, G_OBEX_DATA_REF, &err); + g_assert_no_error(err); + + len = g_obex_packet_encode(pkt, buf, sizeof(buf)); + if (len < 0) { + g_printerr("Encoding failed: %s\n", g_strerror(-len)); + g_assert_not_reached(); + } + + assert_memequal(pkt_put_action, sizeof(pkt_put_action), buf, len); + + g_obex_packet_free(pkt); +} + int main(int argc, char *argv[]) { g_test_init(&argc, &argv, NULL); @@ -80,6 +105,8 @@ int main(int argc, char *argv[]) g_test_add_func("/gobex/test_decode_pkt_header", test_decode_pkt_header); + g_test_add_func("/gobex/test_encode_pkt", test_decode_encode); + g_test_run(); return 0; -- 2.47.3