Hi,
Recently I've created set of unit test for lib/sdp to
increase code coverage for parts of bluez that are not
touched by our functional tests.
I'm sending 7 patches with tests - 22 testcases overall.
If they are worth upstreaming I can send more of them:)
BR,
Radek
2 new test cases sdp_attr_remove from lib/sdp.c
---
unit/test-sdp.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 46 insertions(+), 0 deletions(-)
diff --git a/unit/test-sdp.c b/unit/test-sdp.c
index 9f7dc83..897a147 100644
--- a/unit/test-sdp.c
+++ b/unit/test-sdp.c
@@ -327,6 +327,49 @@ START_TEST(tc_sdp_attr_add_twice_add)
}
END_TEST
+/* sdp_attr_remove testcases */
+START_TEST(tc_sdp_attr_remove_basic)
+{
+ sdp_record_t *rec;
+ sdp_data_t *d;
+ uint16_t attr = SDP_ATTR_SUPPORTED_FEATURES;
+ uint16_t test_uid = 0x0019;
+
+ d = sdp_data_alloc(SDP_UINT16, &test_uid);
+ d->attrId = attr;
+
+ rec = sdp_record_alloc();
+ rec->attrlist = sdp_list_append(rec->attrlist, d);
+ ck_assert(rec->attrlist != NULL);
+
+ sdp_attr_remove(rec, attr);
+
+ /* after removing single entry, list should be empty*/
+ ck_assert(rec->attrlist == NULL);
+
+ /* sdp_record_free will free also internal sdp data (sdp_data_t *d) */
+ sdp_record_free(rec);
+}
+END_TEST
+
+START_TEST(tc_sdp_attr_remove_class_id)
+{
+ sdp_record_t *rec;
+
+ rec = sdp_record_alloc();
+ rec->svclass.type = SDP_UUID16;
+ rec->svclass.value.uuid16 = 0x01;
+
+ sdp_attr_remove(rec, SDP_ATTR_SVCLASS_ID_LIST);
+
+ /* after removing single entry, list should be empty*/
+ ck_assert(rec->svclass.type == 0);
+ ck_assert(rec->svclass.value.uuid16 == 0);
+
+ sdp_record_free(rec);
+}
+END_TEST
+
static void add_test(Suite *s, const char *name, TFun func)
{
TCase *t;
@@ -385,6 +428,9 @@ int main(int argc, char *argv[])
/* sdp_attr_add testcases */
add_test(s, "sdp_attr_add:add twice same value",
tc_sdp_attr_add_twice_add);
+ /* sdp_attr_remove testcases */
+ add_test(s, "sdp_attr_remove: basic", tc_sdp_attr_remove_basic);
+ add_test(s, "sdp_attr_remove: class id", tc_sdp_attr_remove_class_id);
sr = srunner_create(s);
--
1.7.0.4
1 new test case sdp_attr_add from lib/sdp.c code.
---
unit/test-sdp.c | 28 ++++++++++++++++++++++++++++
1 files changed, 28 insertions(+), 0 deletions(-)
diff --git a/unit/test-sdp.c b/unit/test-sdp.c
index 76a8435..9f7dc83 100644
--- a/unit/test-sdp.c
+++ b/unit/test-sdp.c
@@ -302,6 +302,31 @@ START_TEST(tc_sdp_seq_alloc_with_length_zero)
}
END_TEST
+/* sdp_attr_add testcases */
+START_TEST(tc_sdp_attr_add_twice_add)
+{
+ int ret;
+ sdp_record_t *rec;
+ sdp_data_t *d;
+ uint16_t attr = SDP_ATTR_SUPPORTED_FEATURES;
+ uint16_t test_uid = 0x0019;
+
+ d = sdp_data_alloc(SDP_UINT16, &test_uid);
+ d->attrId = attr;
+
+ rec = sdp_record_alloc();
+ rec->attrlist = sdp_list_append(rec->attrlist, d);
+
+ ret = sdp_attr_add(rec, attr, d);
+
+ /* trying to add attribute that already exist, so func should fail
+ * with -1 */
+ ck_assert(ret == -1);
+
+ sdp_record_free(rec);
+}
+END_TEST
+
static void add_test(Suite *s, const char *name, TFun func)
{
TCase *t;
@@ -357,6 +382,9 @@ int main(int argc, char *argv[])
/* sdp_seq_alloc_with_length testcases */
add_test(s, "sdp_seq_alloc_with_length:zero length param",
tc_sdp_seq_alloc_with_length_zero);
+ /* sdp_attr_add testcases */
+ add_test(s, "sdp_attr_add:add twice same value",
+ tc_sdp_attr_add_twice_add);
sr = srunner_create(s);
--
1.7.0.4
1 new test case sdp_seq_alloc_with_length from
lib/sdp.c code.
---
unit/test-sdp.c | 18 ++++++++++++++++++
1 files changed, 18 insertions(+), 0 deletions(-)
diff --git a/unit/test-sdp.c b/unit/test-sdp.c
index 8ce802d..76a8435 100644
--- a/unit/test-sdp.c
+++ b/unit/test-sdp.c
@@ -287,6 +287,21 @@ START_TEST(tc_sdp_data_alloc_null)
}
END_TEST
+/* sdp_seq_alloc_with_length testcases */
+START_TEST(tc_sdp_seq_alloc_with_length_zero)
+{
+ sdp_data_t *ret;
+ int lengths[2] = {0, 0};
+
+ ret = sdp_seq_alloc_with_length(NULL, NULL, lengths, 0);
+
+ ck_assert(ret->val.dataseq == NULL);
+ ck_assert(ret->unitSize == 2 * sizeof(uint8_t));
+
+ sdp_data_free(ret);
+}
+END_TEST
+
static void add_test(Suite *s, const char *name, TFun func)
{
TCase *t;
@@ -339,6 +354,9 @@ int main(int argc, char *argv[])
tc_sdp_data_alloc_with_length_text_unspec);
/* sdp_data_alloc testcases */
add_test(s, "sdp_data_alloc:NULL param", tc_sdp_data_alloc_null);
+ /* sdp_seq_alloc_with_length testcases */
+ add_test(s, "sdp_seq_alloc_with_length:zero length param",
+ tc_sdp_seq_alloc_with_length_zero);
sr = srunner_create(s);
--
1.7.0.4
1 new test case sdp_data_alloc function from
lib/sdp.c code.
---
unit/test-sdp.c | 13 +++++++++++++
1 files changed, 13 insertions(+), 0 deletions(-)
diff --git a/unit/test-sdp.c b/unit/test-sdp.c
index c53ce21..8ce802d 100644
--- a/unit/test-sdp.c
+++ b/unit/test-sdp.c
@@ -276,6 +276,17 @@ START_TEST(tc_sdp_data_alloc_with_length_text_unspec)
}
END_TEST
+/* sdp_data_alloc testcases */
+START_TEST(tc_sdp_data_alloc_null)
+{
+ sdp_data_t *ret;
+
+ ret = sdp_data_alloc(SDP_TEXT_STR16, NULL);
+
+ ck_assert(ret == NULL);
+}
+END_TEST
+
static void add_test(Suite *s, const char *name, TFun func)
{
TCase *t;
@@ -326,6 +337,8 @@ int main(int argc, char *argv[])
tc_sdp_data_alloc_with_length_text_seq32);
add_test(s, "sdp_data_alloc_with_length:indefined param",
tc_sdp_data_alloc_with_length_text_unspec);
+ /* sdp_data_alloc testcases */
+ add_test(s, "sdp_data_alloc:NULL param", tc_sdp_data_alloc_null);
sr = srunner_create(s);
--
1.7.0.4
14 new tests for sdp_data_alloc_with_length function from
lib/sdp.c code
---
unit/test-sdp.c | 223 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 223 insertions(+), 0 deletions(-)
diff --git a/unit/test-sdp.c b/unit/test-sdp.c
index ee7e9b4..c53ce21 100644
--- a/unit/test-sdp.c
+++ b/unit/test-sdp.c
@@ -82,6 +82,200 @@ START_TEST(tc_uuid2strn_undefined)
}
END_TEST
+/* sdp_data_alloc_with_length testcases */
+START_TEST(tc_sdp_data_alloc_with_length_sdp_data_nil)
+{
+ sdp_data_t *ret;
+
+ ret = sdp_data_alloc_with_length(SDP_DATA_NIL, NULL, 0);
+ ck_assert(ret->dtd == SDP_DATA_NIL);
+ ck_assert(ret->unitSize == sizeof(uint8_t));
+
+ sdp_data_free(ret);
+}
+END_TEST
+
+START_TEST(tc_sdp_data_alloc_with_length_int8)
+{
+ sdp_data_t *ret;
+ int8_t test_val = 7;
+
+ ret = sdp_data_alloc_with_length(SDP_INT8, &test_val, 1);
+ ck_assert(ret->dtd == SDP_INT8);
+ ck_assert(ret->unitSize == 2 * sizeof(uint8_t));
+ ck_assert(ret->val.int8 == 7);
+
+ sdp_data_free(ret);
+}
+END_TEST
+
+START_TEST(tc_sdp_data_alloc_with_length_int16)
+{
+ sdp_data_t *ret;
+ int16_t test_val = 32767;
+
+ ret = sdp_data_alloc_with_length(SDP_INT16, &test_val, 1);
+ ck_assert(ret->dtd == SDP_INT16);
+ ck_assert(ret->unitSize == sizeof(int16_t) + sizeof(uint8_t));
+ ck_assert(ret->val.int16 == 32767);
+
+ sdp_data_free(ret);
+}
+END_TEST
+
+START_TEST(tc_sdp_data_alloc_with_length_int32)
+{
+ sdp_data_t *ret;
+ int32_t test_val = -1000000;
+
+ ret = sdp_data_alloc_with_length(SDP_INT32, &test_val, 1);
+ ck_assert(ret->dtd == SDP_INT32);
+ ck_assert(ret->unitSize == sizeof(int32_t) + sizeof(uint8_t));
+ ck_assert(ret->val.int32 == -1000000);
+
+ sdp_data_free(ret);
+}
+END_TEST
+
+START_TEST(tc_sdp_data_alloc_with_length_int64)
+{
+ sdp_data_t *ret;
+ int64_t test_val = -1000000000;
+
+ ret = sdp_data_alloc_with_length(SDP_INT64, &test_val, 1);
+ ck_assert(ret->dtd == SDP_INT64);
+ ck_assert(ret->unitSize == sizeof(int64_t) + sizeof(uint8_t));
+ ck_assert(ret->val.int64 == -1000000000);
+
+ sdp_data_free(ret);
+}
+END_TEST
+
+START_TEST(tc_sdp_data_alloc_with_length_uint64)
+{
+ sdp_data_t *ret;
+ uint64_t test_val = 1000000000;
+
+ ret = sdp_data_alloc_with_length(SDP_UINT64, &test_val, 1);
+ ck_assert(ret->dtd == SDP_UINT64);
+ ck_assert(ret->unitSize == sizeof(uint64_t) + sizeof(uint8_t));
+ ck_assert(ret->val.uint64 == 1000000000);
+
+ sdp_data_free(ret);
+}
+END_TEST
+
+START_TEST(tc_sdp_data_alloc_with_length_uint128)
+{
+ sdp_data_t *ret;
+ uint128_t test_val;
+ int i;
+
+ ret = sdp_data_alloc_with_length(SDP_UINT128, &test_val, 1);
+
+ ck_assert(ret->dtd == SDP_UINT128);
+ ck_assert(ret->unitSize == sizeof(uint128_t) + sizeof(uint8_t));
+
+ for (i = 0; i < 16; ++i)
+ ck_assert(ret->val.uint128.data[i] == test_val.data[i]);
+
+ sdp_data_free(ret);
+}
+END_TEST
+
+START_TEST(tc_sdp_data_alloc_with_length_int128)
+{
+ sdp_data_t *ret;
+ uint128_t test_val;
+ int i;
+
+ ret = sdp_data_alloc_with_length(SDP_INT128, &test_val, 1);
+
+ ck_assert(ret->dtd == SDP_INT128);
+ ck_assert(ret->unitSize == sizeof(uint128_t) + sizeof(uint8_t));
+
+ for (i = 0; i < 16; ++i)
+ ck_assert(ret->val.uint128.data[i] == test_val.data[i]);
+
+ sdp_data_free(ret);
+}
+END_TEST
+
+START_TEST(tc_sdp_data_alloc_with_length_uuid32)
+{
+ sdp_data_t *ret;
+ uint32_t test_val = 1000000;
+
+ ret = sdp_data_alloc_with_length(SDP_UUID32, &test_val, 1);
+
+ ck_assert(ret->dtd == SDP_UUID32);
+ ck_assert(ret->unitSize == sizeof(uint32_t) + sizeof(uint8_t));
+ ck_assert(ret->val.uuid.value.uuid32 == 1000000);
+
+ sdp_data_free(ret);
+}
+END_TEST
+
+START_TEST(tc_sdp_data_alloc_with_length_text_str16_null)
+{
+ sdp_data_t *ret;
+
+ ret = sdp_data_alloc_with_length(SDP_TEXT_STR16, NULL, 0);
+
+ ck_assert(ret == NULL);
+}
+END_TEST
+
+START_TEST(tc_sdp_data_alloc_with_length_text_str16_short_max)
+{
+ sdp_data_t *ret;
+ char *test_data = g_strdup("test data");
+
+ ret = sdp_data_alloc_with_length(SDP_TEXT_STR16, test_data,
+ USHRT_MAX+1);
+
+ ck_assert(ret == NULL);
+
+ g_free(test_data);
+}
+END_TEST
+
+START_TEST(tc_sdp_data_alloc_with_length_text_seq16)
+{
+ sdp_data_t *ret;
+
+ ret = sdp_data_alloc_with_length(SDP_SEQ16, NULL, 0);
+
+ ck_assert(ret->val.dataseq == NULL);
+ ck_assert(ret->unitSize == sizeof(uint16_t) + sizeof(uint8_t));
+
+ sdp_data_free(ret);
+}
+END_TEST
+
+START_TEST(tc_sdp_data_alloc_with_length_text_seq32)
+{
+ sdp_data_t *ret;
+
+ ret = sdp_data_alloc_with_length(SDP_SEQ32, NULL, 0);
+
+ ck_assert(ret->val.dataseq == NULL);
+ ck_assert(ret->unitSize == sizeof(uint32_t) + sizeof(uint8_t));
+
+ sdp_data_free(ret);
+}
+END_TEST
+
+START_TEST(tc_sdp_data_alloc_with_length_text_unspec)
+{
+ sdp_data_t *ret;
+
+ ret = sdp_data_alloc_with_length(SDP_UUID_UNSPEC, NULL, 0);
+
+ ck_assert(ret == NULL);
+}
+END_TEST
+
static void add_test(Suite *s, const char *name, TFun func)
{
TCase *t;
@@ -103,6 +297,35 @@ int main(int argc, char *argv[])
add_test(s, "uuid2strn:NULL param", tc_uuid2strn_null);
add_test(s, "uuid2strn:SDP_UUID32 param", tc_uuid2strn_sdp_uuid_32);
add_test(s, "uuid2strn:undefined param", tc_uuid2strn_undefined);
+ /* sdp_data_alloc_with_length testcases */
+ add_test(s, "sdp_data_alloc_with_length:SDP_DATA_NIL param",
+ tc_sdp_data_alloc_with_length_sdp_data_nil);
+ add_test(s, "sdp_data_alloc_with_length:SDP_INT8 param",
+ tc_sdp_data_alloc_with_length_int8);
+ add_test(s, "sdp_data_alloc_with_length:SDP_INT16 param",
+ tc_sdp_data_alloc_with_length_int16);
+ add_test(s, "sdp_data_alloc_with_length:SDP_INT32 param",
+ tc_sdp_data_alloc_with_length_int32);
+ add_test(s, "sdp_data_alloc_with_length:SDP_INT64 param",
+ tc_sdp_data_alloc_with_length_int64);
+ add_test(s, "sdp_data_alloc_with_length:SDP_UINT64 param",
+ tc_sdp_data_alloc_with_length_uint64);
+ add_test(s, "sdp_data_alloc_with_length:SDP_UINT128 param",
+ tc_sdp_data_alloc_with_length_uint128);
+ add_test(s, "sdp_data_alloc_with_length:SDP_INT128 param",
+ tc_sdp_data_alloc_with_length_int128);
+ add_test(s, "sdp_data_alloc_with_length:SDP_UUID32 param",
+ tc_sdp_data_alloc_with_length_uuid32);
+ add_test(s, "sdp_data_alloc_with_length:SDP_STR16 NULL param",
+ tc_sdp_data_alloc_with_length_text_str16_null);
+ add_test(s, "sdp_data_alloc_with_length:SDP_STR16 too long param",
+ tc_sdp_data_alloc_with_length_text_str16_short_max);
+ add_test(s, "sdp_data_alloc_with_length:SDP_SEQ16 param",
+ tc_sdp_data_alloc_with_length_text_seq16);
+ add_test(s, "sdp_data_alloc_with_length:SDP_SEQ32 param",
+ tc_sdp_data_alloc_with_length_text_seq32);
+ add_test(s, "sdp_data_alloc_with_length:indefined param",
+ tc_sdp_data_alloc_with_length_text_unspec);
sr = srunner_create(s);
--
1.7.0.4
3 new tests for uuid2strn function from lib/sdp.c code.
---
unit/test-sdp.c | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 64 insertions(+), 0 deletions(-)
diff --git a/unit/test-sdp.c b/unit/test-sdp.c
index ea2521b..ee7e9b4 100644
--- a/unit/test-sdp.c
+++ b/unit/test-sdp.c
@@ -32,6 +32,65 @@
#include <bluetooth/hci.h>
#include <bluetooth/sdp.h>
+#include "sdp_lib.h"
+
+#define BUF_SIZE 30
+
+/* uuid2strn testcases */
+START_TEST(tc_uuid2strn_null)
+{
+ char buf[BUF_SIZE];
+ int err;
+
+ err = sdp_uuid2strn(NULL, buf, BUF_SIZE);
+ ck_assert(err == -2);
+ ck_assert_str_eq(buf, "NULL");
+}
+END_TEST
+
+START_TEST(tc_uuid2strn_sdp_uuid_32)
+{
+ char buf[BUF_SIZE];
+ int err;
+ uuid_t *test_uuid;
+
+ test_uuid = g_new0(uuid_t, 1);
+ test_uuid->type = SDP_UUID32;
+
+ err = sdp_uuid2strn(test_uuid, buf, BUF_SIZE);
+ ck_assert(err == 0);
+ ck_assert_str_eq(buf, "00000000");
+
+ g_free(test_uuid);
+}
+END_TEST
+
+START_TEST(tc_uuid2strn_undefined)
+{
+ char buf[BUF_SIZE];
+ int err;
+ uuid_t *test_uuid;
+
+ test_uuid = g_new0(uuid_t, 1);
+ test_uuid->type = SDP_UUID_UNSPEC;
+
+ err = sdp_uuid2strn(test_uuid, buf, BUF_SIZE);
+ ck_assert(err == -1);
+ ck_assert_str_eq(buf, "Type of UUID (18) unknown.");
+
+ g_free(test_uuid);
+}
+END_TEST
+
+static void add_test(Suite *s, const char *name, TFun func)
+{
+ TCase *t;
+
+ t = tcase_create(name);
+ tcase_add_test(t, func);
+ suite_add_tcase(s, t);
+}
+
int main(int argc, char *argv[])
{
int fails;
@@ -40,6 +99,11 @@ int main(int argc, char *argv[])
s = suite_create("SDP");
+ /* uuid2strn testcases */
+ add_test(s, "uuid2strn:NULL param", tc_uuid2strn_null);
+ add_test(s, "uuid2strn:SDP_UUID32 param", tc_uuid2strn_sdp_uuid_32);
+ add_test(s, "uuid2strn:undefined param", tc_uuid2strn_undefined);
+
sr = srunner_create(s);
srunner_run_all(sr, CK_NORMAL);
--
1.7.0.4
Adds dummy test-sdp.c file with skeleton
for unit tests for lib/sdp.c
---
Makefile.am | 7 ++++++-
unit/test-sdp.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 61 insertions(+), 1 deletions(-)
create mode 100644 unit/test-sdp.c
diff --git a/Makefile.am b/Makefile.am
index 102ee62..1764cfb 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -446,7 +446,7 @@ endif
unit_objects =
if TEST
-unit_tests = unit/test-eir
+unit_tests = unit/test-eir unit/test-sdp
noinst_PROGRAMS += $(unit_tests)
@@ -454,6 +454,11 @@ unit_test_eir_SOURCES = unit/test-eir.c src/eir.c src/glib-helper.c
unit_test_eir_LDADD = lib/libbluetooth-private.la @GLIB_LIBS@ @CHECK_LIBS@
unit_test_eir_CFLAGS = $(AM_CFLAGS) @CHECK_CFLAGS@
unit_objects += $(unit_test_eir_OBJECTS)
+
+unit_test_sdp_SOURCES = unit/test-sdp.c
+unit_test_sdp_LDADD = lib/libbluetooth.la @GLIB_LIBS@ @CHECK_LIBS@
+unit_test_sdp_CFLAGS = $(AM_CFLAGS) @CHECK_CFLAGS@
+unit_objects += $(unit_test_sdp_OBJECTS)
else
unit_tests =
endif
diff --git a/unit/test-sdp.c b/unit/test-sdp.c
new file mode 100644
index 0000000..ea2521b
--- /dev/null
+++ b/unit/test-sdp.c
@@ -0,0 +1,55 @@
+/*
+ *
+ * BlueZ - Bluetooth protocol stack for Linux
+ *
+ * Copyright (C) 2012 Nokia Corporation
+ *
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <check.h>
+#include <stdint.h>
+#include <glib.h>
+#include <bluetooth/bluetooth.h>
+#include <bluetooth/hci.h>
+#include <bluetooth/sdp.h>
+
+int main(int argc, char *argv[])
+{
+ int fails;
+ SRunner *sr;
+ Suite *s;
+
+ s = suite_create("SDP");
+
+ sr = srunner_create(s);
+
+ srunner_run_all(sr, CK_NORMAL);
+
+ fails = srunner_ntests_failed(sr);
+
+ srunner_free(sr);
+
+ if (fails > 0)
+ return -1;
+
+ return 0;
+}
--
1.7.0.4