| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415 |
- // SPDX-License-Identifier: GPL-2.0-or-later
- /*
- *
- * OBEX library with GLib integration
- *
- * Copyright (C) 2012 Intel Corporation.
- *
- */
- #ifdef HAVE_CONFIG_H
- #include <config.h>
- #endif
- #include <stdint.h>
- #include <string.h>
- #include "gobex/gobex.h"
- #include "gobex/gobex-apparam.h"
- #include "util.h"
- #define TAG_U8 0x00
- #define TAG_U16 0x01
- #define TAG_U32 0x02
- #define TAG_U64 0x03
- #define TAG_STRING 0x04
- #define TAG_BYTES 0x05
- static uint8_t tag_nval_short[] = { TAG_U8 };
- static uint8_t tag_nval_data[] = { TAG_U8, 0x01 };
- static uint8_t tag_nval2_short[] = { TAG_U8, 0x01, 0x1, TAG_U16 };
- static uint8_t tag_nval2_data[] = { TAG_U8, 0x01, 0x1, TAG_U16, 0x02 };
- static uint8_t tag_uint8[] = { TAG_U8, 0x01, 0x01 };
- static uint8_t tag_uint16[] = { TAG_U16, 0x02, 0x01, 0x02 };
- static uint8_t tag_uint32[] = { TAG_U32, 0x04, 0x01, 0x02, 0x03, 0x04 };
- static uint8_t tag_uint64[] = { TAG_U64, 0x08, 0x01, 0x02, 0x03, 0x04,
- 0x05, 0x06, 0x07, 0x08 };
- static uint8_t tag_string[] = { TAG_STRING, 0x04, 'A', 'B', 'C', '\0' };
- static uint8_t tag_bytes[257] = { TAG_BYTES, 0xFF };
- static uint8_t tag_multi[] = { TAG_U8, 0x01, 0x01,
- TAG_U16, 0x02, 0x01, 0x02,
- TAG_U32, 0x04, 0x01, 0x02, 0x03, 0x04,
- TAG_U64, 0x08, 0x01, 0x02, 0x03, 0x04,
- 0x05, 0x06, 0x07, 0x08,
- TAG_STRING, 0x04, 'A', 'B', 'C', '\0' };
- static GObexApparam *parse_and_decode(const void *data, gsize size)
- {
- GObexApparam *apparam;
- guint8 encoded[1024];
- gsize len;
- apparam = g_obex_apparam_decode(data, size);
- g_assert(apparam != NULL);
- len = g_obex_apparam_encode(apparam, encoded, sizeof(encoded));
- assert_memequal(data, size, encoded, len);
- return apparam;
- }
- static void test_apparam_nval_short(void)
- {
- GObexApparam *apparam;
- apparam = g_obex_apparam_decode(tag_nval_short,
- sizeof(tag_nval_short));
- g_assert(apparam == NULL);
- }
- static void test_apparam_nval_data(void)
- {
- GObexApparam *apparam;
- apparam = g_obex_apparam_decode(tag_nval_data,
- sizeof(tag_nval_data));
- g_assert(apparam == NULL);
- }
- static void test_apparam_nval2_short(void)
- {
- GObexApparam *apparam;
- apparam = g_obex_apparam_decode(tag_nval2_short,
- sizeof(tag_nval2_short));
- g_assert(apparam == NULL);
- }
- static void test_apparam_nval2_data(void)
- {
- GObexApparam *apparam;
- apparam = g_obex_apparam_decode(tag_nval2_data,
- sizeof(tag_nval2_data));
- g_assert(apparam == NULL);
- }
- static void test_apparam_get_uint8(void)
- {
- GObexApparam *apparam;
- guint8 data;
- gboolean ret;
- apparam = parse_and_decode(tag_uint8, sizeof(tag_uint8));
- ret = g_obex_apparam_get_uint8(apparam, TAG_U8, &data);
- g_assert(ret == TRUE);
- g_assert(data == 0x01);
- g_obex_apparam_free(apparam);
- }
- static void test_apparam_get_uint16(void)
- {
- GObexApparam *apparam;
- uint16_t data;
- gboolean ret;
- apparam = parse_and_decode(tag_uint16, sizeof(tag_uint16));
- ret = g_obex_apparam_get_uint16(apparam, TAG_U16, &data);
- g_assert(ret == TRUE);
- g_assert(data == 0x0102);
- g_obex_apparam_free(apparam);
- }
- static void test_apparam_get_uint32(void)
- {
- GObexApparam *apparam;
- uint32_t data;
- gboolean ret;
- apparam = parse_and_decode(tag_uint32, sizeof(tag_uint32));
- ret = g_obex_apparam_get_uint32(apparam, TAG_U32, &data);
- g_assert(ret == TRUE);
- g_assert(data == 0x01020304);
- g_obex_apparam_free(apparam);
- }
- static void test_apparam_get_uint64(void)
- {
- GObexApparam *apparam;
- uint64_t data;
- gboolean ret;
- apparam = parse_and_decode(tag_uint64, sizeof(tag_uint64));
- ret = g_obex_apparam_get_uint64(apparam, TAG_U64, &data);
- g_assert(ret == TRUE);
- g_assert(data == 0x0102030405060708);
- g_obex_apparam_free(apparam);
- }
- static void test_apparam_get_string(void)
- {
- GObexApparam *apparam;
- char *string;
- apparam = parse_and_decode(tag_string, sizeof(tag_string));
- string = g_obex_apparam_get_string(apparam, TAG_STRING);
- g_assert(string != NULL);
- g_assert_cmpstr(string, ==, "ABC");
- g_free(string);
- g_obex_apparam_free(apparam);
- }
- static void test_apparam_get_bytes(void)
- {
- GObexApparam *apparam;
- const uint8_t *data;
- gsize len;
- gboolean ret;
- apparam = parse_and_decode(tag_bytes, sizeof(tag_bytes));
- ret = g_obex_apparam_get_bytes(apparam, TAG_BYTES, &data, &len);
- g_assert(ret == TRUE);
- assert_memequal(tag_bytes + 2, sizeof(tag_bytes) - 2, data, len);
- g_obex_apparam_free(apparam);
- }
- static void test_apparam_get_multi(void)
- {
- GObexApparam *apparam;
- char *string;
- uint8_t data8;
- uint16_t data16;
- uint32_t data32;
- uint64_t data64;
- gboolean ret;
- apparam = g_obex_apparam_decode(tag_multi, sizeof(tag_multi));
- g_assert(apparam != NULL);
- ret = g_obex_apparam_get_uint8(apparam, TAG_U8, &data8);
- g_assert(ret == TRUE);
- g_assert(data8 == 0x01);
- ret = g_obex_apparam_get_uint16(apparam, TAG_U16, &data16);
- g_assert(ret == TRUE);
- g_assert(data16 == 0x0102);
- ret = g_obex_apparam_get_uint32(apparam, TAG_U32, &data32);
- g_assert(ret == TRUE);
- g_assert(data32 == 0x01020304);
- ret = g_obex_apparam_get_uint64(apparam, TAG_U64, &data64);
- g_assert(ret == TRUE);
- g_assert(data64 == 0x0102030405060708);
- string = g_obex_apparam_get_string(apparam, TAG_STRING);
- g_assert(string != NULL);
- g_assert_cmpstr(string, ==, "ABC");
- g_free(string);
- g_obex_apparam_free(apparam);
- }
- static void test_apparam_set_uint8(void)
- {
- GObexApparam *apparam;
- guint8 buf[1024];
- gsize len;
- apparam = g_obex_apparam_set_uint8(NULL, TAG_U8, 0x01);
- g_assert(apparam != NULL);
- len = g_obex_apparam_encode(apparam, buf, sizeof(buf));
- assert_memequal(tag_uint8, sizeof(tag_uint8), buf, len);
- g_obex_apparam_free(apparam);
- }
- static void test_apparam_set_uint16(void)
- {
- GObexApparam *apparam;
- guint8 buf[1024];
- gsize len;
- apparam = g_obex_apparam_set_uint16(NULL, TAG_U16, 0x0102);
- g_assert(apparam != NULL);
- len = g_obex_apparam_encode(apparam, buf, sizeof(buf));
- assert_memequal(tag_uint16, sizeof(tag_uint16), buf, len);
- g_obex_apparam_free(apparam);
- }
- static void test_apparam_set_uint32(void)
- {
- GObexApparam *apparam;
- guint8 buf[1024];
- gsize len;
- apparam = g_obex_apparam_set_uint32(NULL, TAG_U32, 0x01020304);
- g_assert(apparam != NULL);
- len = g_obex_apparam_encode(apparam, buf, sizeof(buf));
- assert_memequal(tag_uint32, sizeof(tag_uint32), buf, len);
- g_obex_apparam_free(apparam);
- }
- static void test_apparam_set_uint64(void)
- {
- GObexApparam *apparam;
- guint8 buf[1024];
- gsize len;
- apparam = g_obex_apparam_set_uint64(NULL, TAG_U64, 0x0102030405060708);
- g_assert(apparam != NULL);
- len = g_obex_apparam_encode(apparam, buf, sizeof(buf));
- assert_memequal(tag_uint64, sizeof(tag_uint64), buf, len);
- g_obex_apparam_free(apparam);
- }
- static void test_apparam_set_string(void)
- {
- GObexApparam *apparam;
- guint8 buf[1024];
- gsize len;
- apparam = g_obex_apparam_set_string(NULL, TAG_STRING, "ABC");
- g_assert(apparam != NULL);
- len = g_obex_apparam_encode(apparam, buf, sizeof(buf));
- assert_memequal(tag_string, sizeof(tag_string), buf, len);
- g_obex_apparam_free(apparam);
- }
- static void test_apparam_set_bytes(void)
- {
- GObexApparam *apparam;
- guint8 buf[1024];
- gsize len;
- apparam = g_obex_apparam_set_bytes(NULL, TAG_BYTES, tag_bytes + 2, 255);
- g_assert(apparam != NULL);
- len = g_obex_apparam_encode(apparam, buf, sizeof(buf));
- assert_memequal(tag_bytes, sizeof(tag_bytes), buf, len);
- g_obex_apparam_free(apparam);
- }
- static void test_apparam_set_multi(void)
- {
- GObexApparam *apparam;
- guint8 buf[1024];
- gsize len;
- apparam = g_obex_apparam_set_uint8(NULL, TAG_U8, 0x01);
- g_assert(apparam != NULL);
- apparam = g_obex_apparam_set_uint16(apparam, TAG_U16, 0x0102);
- g_assert(apparam != NULL);
- apparam = g_obex_apparam_set_uint32(apparam, TAG_U32, 0x01020304);
- g_assert(apparam != NULL);
- apparam = g_obex_apparam_set_uint64(apparam, TAG_U64,
- 0x0102030405060708);
- g_assert(apparam != NULL);
- apparam = g_obex_apparam_set_string(apparam, TAG_STRING, "ABC");
- g_assert(apparam != NULL);
- len = g_obex_apparam_encode(apparam, buf, sizeof(buf));
- g_assert_cmpuint(len, ==, sizeof(tag_multi));
- g_obex_apparam_free(apparam);
- }
- int main(int argc, char *argv[])
- {
- g_test_init(&argc, &argv, NULL);
- g_test_add_func("/gobex/test_apparam_nval_short",
- test_apparam_nval_short);
- g_test_add_func("/gobex/test_apparam_nval_data",
- test_apparam_nval_data);
- g_test_add_func("/gobex/test_apparam_nval2_short",
- test_apparam_nval2_short);
- g_test_add_func("/gobex/test_apparam_nval2_data",
- test_apparam_nval2_data);
- g_test_add_func("/gobex/test_apparam_get_uint8",
- test_apparam_get_uint8);
- g_test_add_func("/gobex/test_apparam_get_uint16",
- test_apparam_get_uint16);
- g_test_add_func("/gobex/test_apparam_get_uint32",
- test_apparam_get_uint32);
- g_test_add_func("/gobex/test_apparam_get_uint64",
- test_apparam_get_uint64);
- g_test_add_func("/gobex/test_apparam_get_string",
- test_apparam_get_string);
- g_test_add_func("/gobex/test_apparam_get_bytes",
- test_apparam_get_bytes);
- g_test_add_func("/gobex/test_apparam_get_multi",
- test_apparam_get_multi);
- g_test_add_func("/gobex/test_apparam_set_uint8",
- test_apparam_set_uint8);
- g_test_add_func("/gobex/test_apparam_set_uint16",
- test_apparam_set_uint16);
- g_test_add_func("/gobex/test_apparam_set_uint32",
- test_apparam_set_uint32);
- g_test_add_func("/gobex/test_apparam_set_uint64",
- test_apparam_set_uint64);
- g_test_add_func("/gobex/test_apparam_set_string",
- test_apparam_set_string);
- g_test_add_func("/gobex/test_apparam_set_bytes",
- test_apparam_set_bytes);
- g_test_add_func("/gobex/test_apparam_set_multi",
- test_apparam_set_multi);
- return g_test_run();
- }
|