Skip to contentSkip to conversation

unit/test-queue: Add /queue/foreach_remove_backward test

PATCH Applied Luiz Augusto von Dentz 1 participant 11 messages Dec 10, 2014 Bluetooth drivers

lore
Share & tools
lore

Luiz Augusto von Dentz

author maintainer
#0 lore raw

PATCH v2 3/13 unit/test-queue: Add /queue/foreach_remove_backward test

From: Luiz Augusto von Dentz <[email protected]>

This tests queue_remove from queue_foreach callback in backward order.

---
 unit/test-queue.c | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)
unit/test-queue.c +24 -0
index 6261612..cb85605 100644
@@ -120,6 +120,28 @@ static void test_foreach_remove_all(void)
 	queue_destroy(queue, NULL);
 }
 
+static void foreach_remove_backward(void *data, void *user_data)
+{
+	struct queue *queue = user_data;
+
+	queue_remove(queue, UINT_TO_PTR(2));
+	queue_remove(queue, UINT_TO_PTR(1));
+}
+
+static void test_foreach_remove_backward(void)
+{
+	struct queue *queue;
+
+	queue = queue_new();
+	g_assert(queue != NULL);
+
+	queue_push_tail(queue, UINT_TO_PTR(1));
+	queue_push_tail(queue, UINT_TO_PTR(2));
+
+	queue_foreach(queue, foreach_remove_backward, queue);
+	queue_destroy(queue, NULL);
+}
+
 static struct queue *static_queue;
 
 static void destroy_remove(void *user_data)
@@ -200,6 +222,8 @@ int main(int argc, char *argv[])
 	g_test_add_func("/queue/foreach_destroy", test_foreach_destroy);
 	g_test_add_func("/queue/foreach_remove", test_foreach_remove);
 	g_test_add_func("/queue/foreach_remove_all", test_foreach_remove_all);
+	g_test_add_func("/queue/foreach_remove_backward",
+						test_foreach_remove_backward);
 	g_test_add_func("/queue/destroy_remove", test_destroy_remove);
 	g_test_add_func("/queue/push_after", test_push_after);
 
Signature · 1 line
1.9.3
author maintainer
#1 lore raw

PATCH v2 4/13 shared/queue: Add reference counting to entries

From: Luiz Augusto von Dentz <[email protected]>

This makes queue_foreach much more efficient when tracking if the next entry was remove by the callback.

---
 src/shared/queue.c | 77 ++++++++++++++++++++++++++++++++++--------------------
 1 file changed, 48 insertions(+), 29 deletions(-)
src/shared/queue.c +48 -29
index 9efb665..a5155e7 100644
@@ -29,6 +29,7 @@
 #include "src/shared/queue.h"
 
 struct queue_entry {
+	int ref_count;
 	void *data;
 	struct queue_entry *next;
 };
@@ -83,6 +84,37 @@ void queue_destroy(struct queue *queue, queue_destroy_func_t destroy)
 	queue_unref(queue);
 }
 
+static struct queue_entry *queue_entry_ref(struct queue_entry *entry)
+{
+	if (!entry)
+		return NULL;
+
+	__sync_fetch_and_add(&entry->ref_count, 1);
+
+	return entry;
+}
+
+static void queue_entry_unref(struct queue_entry *entry)
+{
+	if (__sync_sub_and_fetch(&entry->ref_count, 1))
+		return;
+
+	free(entry);
+}
+
+static struct queue_entry *queue_entry_new(void *data)
+{
+	struct queue_entry *entry;
+
+	entry = new0(struct queue_entry, 1);
+	if (!entry)
+		return NULL;
+
+	entry->data = data;
+
+	return queue_entry_ref(entry);
+}
+
 bool queue_push_tail(struct queue *queue, void *data)
 {
 	struct queue_entry *entry;
@@ -90,13 +122,10 @@ bool queue_push_tail(struct queue *queue, void *data)
 	if (!queue)
 		return false;
 
-	entry = new0(struct queue_entry, 1);
+	entry = queue_entry_new(data);
 	if (!entry)
 		return false;
 
-	entry->data = data;
-	entry->next = NULL;
-
 	if (queue->tail)
 		queue->tail->next = entry;
 
@@ -117,11 +146,10 @@ bool queue_push_head(struct queue *queue, void *data)
 	if (!queue)
 		return false;
 
-	entry = new0(struct queue_entry, 1);
+	entry = queue_entry_new(data);
 	if (!entry)
 		return false;
 
-	entry->data = data;
 	entry->next = queue->head;
 
 	queue->head = entry;
@@ -153,11 +181,10 @@ bool queue_push_after(struct queue *queue, void *entry, void *data)
 	if (!qentry)
 		return false;
 
-	new_entry = new0(struct queue_entry, 1);
+	new_entry = queue_entry_new(data);
 	if (!new_entry)
 		return false;
 
-	new_entry->data = data;
 	new_entry->next = qentry->next;
 
 	if (!qentry->next)
@@ -187,7 +214,7 @@ void *queue_pop_head(struct queue *queue)
 
 	data = entry->data;
 
-	free(entry);
+	queue_entry_unref(entry);
 	queue->entries--;
 
 	return data;
@@ -209,17 +236,6 @@ void *queue_peek_tail(struct queue *queue)
 	return queue->tail->data;
 }
 
-static bool queue_find_entry(struct queue *queue, const void *data)
-{
-	struct queue_entry *entry;
-
-	for (entry = queue->head; entry; entry = entry->next)
-		if (entry == data)
-			return true;
-
-	return false;
-}
-
 void queue_foreach(struct queue *queue, queue_foreach_func_t function,
 							void *user_data)
 {
@@ -233,15 +249,18 @@ void queue_foreach(struct queue *queue, queue_foreach_func_t function,
 		return;
 
 	queue_ref(queue);
-	while (entry && queue->ref_count > 1) {
-		struct queue_entry *tmp = entry;
+	while (entry && queue->head && queue->ref_count > 1) {
+		struct queue_entry *next;
 
-		entry = tmp->next;
+		queue_entry_ref(entry);
 
-		function(tmp->data, user_data);
+		function(entry->data, user_data);
 
-		if (!queue_find_entry(queue, entry))
-			break;
+		next = entry->next;
+
+		queue_entry_unref(entry);
+
+		entry = next;
 	}
 	queue_unref(queue);
 }
@@ -289,7 +308,7 @@ bool queue_remove(struct queue *queue, void *data)
 		if (!entry->next)
 			queue->tail = prev;
 
-		free(entry);
+		queue_entry_unref(entry);
 		queue->entries--;
 
 		return true;
@@ -322,7 +341,7 @@ void *queue_remove_if(struct queue *queue, queue_match_func_t function,
 
 			data = entry->data;
 
-			free(entry);
+			queue_entry_unref(entry);
 			queue->entries--;
 
 			return data;
@@ -374,7 +393,7 @@ unsigned int queue_remove_all(struct queue *queue, queue_match_func_t function,
 			if (destroy)
 				destroy(tmp->data);
 
-			free(tmp);
+			queue_entry_unref(tmp);
 			count++;
 		}
 	}
Signature · 1 line
1.9.3
author maintainer
#2 lore raw

PATCH v2 5/13 unit/test-mgmt: Add /mgmt/response/1 test

In reply to Luiz Augusto von Dentz

From: Luiz Augusto von Dentz <[email protected]>

This tests if a success response can be received.

---
 unit/test-mgmt.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 50 insertions(+), 2 deletions(-)
unit/test-mgmt.c +50 -2
index f9dcb00..ead088c 100644
@@ -46,11 +46,14 @@ struct context {
 enum action {
 	ACTION_PASSED,
 	ACTION_IGNORE,
+	ACTION_RESPOND,
 };
 
 struct handler {
 	const void *cmd_data;
 	uint16_t cmd_size;
+	const void *rsp_data;
+	uint16_t rsp_size;
 	bool match_prefix;
 	enum action action;
 };
@@ -67,7 +70,7 @@ static void context_quit(struct context *context)
 	g_main_loop_quit(context->main_loop);
 }
 
-static void check_actions(struct context *context,
+static void check_actions(struct context *context, int fd,
 					const void *data, uint16_t size)
 {
 	GList *list;
@@ -75,6 +78,7 @@ static void check_actions(struct context *context,
 	for (list = g_list_first(context->handler_list); list;
 						list = g_list_next(list)) {
 		struct handler *handler = list->data;
+		int ret;
 
 		if (handler->match_prefix) {
 			if (size < handler->cmd_size)
@@ -91,6 +95,10 @@ static void check_actions(struct context *context,
 		case ACTION_PASSED:
 			context_quit(context);
 			return;
+		case ACTION_RESPOND:
+			ret = write(fd, handler->rsp_data, handler->rsp_size);
+			g_assert(ret >= 0);
+			return;
 		case ACTION_IGNORE:
 			return;
 		}
@@ -117,7 +125,7 @@ static gboolean server_handler(GIOChannel *channel, GIOCondition cond,
 	if (result < 0)
 		return FALSE;
 
-	check_actions(context, buf, result);
+	check_actions(context, fd, buf, result);
 
 	return TRUE;
 }
@@ -176,12 +184,15 @@ static void execute_context(struct context *context)
 
 static void add_action(struct context *context,
 				const void *cmd_data, uint16_t cmd_size,
+				const void *rsp_data, uint16_t rsp_size,
 				bool match_prefix, enum action action)
 {
 	struct handler *handler = g_new0(struct handler, 1);
 
 	handler->cmd_data = cmd_data;
 	handler->cmd_size = cmd_size;
+	handler->rsp_data = rsp_data;
+	handler->rsp_size = rsp_size;
 	handler->match_prefix = match_prefix;
 	handler->action = action;
 
@@ -195,16 +206,23 @@ struct command_test_data {
 	const void *param;
 	const void *cmd_data;
 	uint16_t cmd_size;
+	const void *rsp_data;
+	uint16_t rsp_size;
 };
 
 static const unsigned char read_version_command[] =
 				{ 0x01, 0x00, 0xff, 0xff, 0x00, 0x00 };
+static const unsigned char read_version_response[] =
+				{ 0x01, 0x00, 0xff, 0xff, 0x06, 0x00,
+				0x01, 0x00, 0x00, 0x01, 0x06, 0x00 };
 
 static const struct command_test_data command_test_1 = {
 	.opcode = MGMT_OP_READ_VERSION,
 	.index = MGMT_INDEX_NONE,
 	.cmd_data = read_version_command,
 	.cmd_size = sizeof(read_version_command),
+	.rsp_data = read_version_response,
+	.rsp_size = sizeof(read_version_response),
 };
 
 static const unsigned char read_info_command[] =
@@ -223,6 +241,7 @@ static void test_command(gconstpointer data)
 	struct context *context = create_context();
 
 	add_action(context, test->cmd_data, test->cmd_size,
+						test->rsp_data, test->rsp_size,
 						false, ACTION_PASSED);
 
 	mgmt_send(context->mgmt_client, test->opcode, test->index,
@@ -232,6 +251,32 @@ static void test_command(gconstpointer data)
 	execute_context(context);
 }
 
+static void success_cb(uint8_t status, uint16_t length, const void *param,
+							void *user_data)
+{
+	struct context *context = user_data;
+
+	g_assert_cmpint(status, ==, MGMT_STATUS_SUCCESS);
+
+	context_quit(context);
+}
+
+static void test_response(gconstpointer data)
+{
+	const struct command_test_data *test = data;
+	struct context *context = create_context();
+
+	add_action(context, test->cmd_data, test->cmd_size,
+						test->rsp_data, test->rsp_size,
+						false, ACTION_RESPOND);
+
+	mgmt_send(context->mgmt_client, test->opcode, test->index,
+					test->length, test->param,
+					success_cb, context, NULL);
+
+	execute_context(context);
+}
+
 int main(int argc, char *argv[])
 {
 	g_test_init(&argc, &argv, NULL);
@@ -239,5 +284,8 @@ int main(int argc, char *argv[])
 	g_test_add_data_func("/mgmt/command/1", &command_test_1, test_command);
 	g_test_add_data_func("/mgmt/command/2", &command_test_2, test_command);
 
+	g_test_add_data_func("/mgmt/response/1", &command_test_1,
+								test_response);
+
 	return g_test_run();
 }
Signature · 1 line
1.9.3
author maintainer
#3 lore raw

PATCH v2 6/13 unit/test-mgmt: Add /mgmt/response/2 test

In reply to Luiz Augusto von Dentz

From: Luiz Augusto von Dentz <[email protected]>

This tests if an error can be received.

---
 unit/test-mgmt.c | 38 ++++++++++++++++++++++++++++++--------
 1 file changed, 30 insertions(+), 8 deletions(-)
unit/test-mgmt.c +30 -8
index ead088c..7bff828 100644
@@ -54,6 +54,7 @@ struct handler {
 	uint16_t cmd_size;
 	const void *rsp_data;
 	uint16_t rsp_size;
+	uint8_t rsp_status;
 	bool match_prefix;
 	enum action action;
 };
@@ -185,7 +186,8 @@ static void execute_context(struct context *context)
 static void add_action(struct context *context,
 				const void *cmd_data, uint16_t cmd_size,
 				const void *rsp_data, uint16_t rsp_size,
-				bool match_prefix, enum action action)
+				uint8_t rsp_status, bool match_prefix,
+				enum action action)
 {
 	struct handler *handler = g_new0(struct handler, 1);
 
@@ -193,6 +195,7 @@ static void add_action(struct context *context,
 	handler->cmd_size = cmd_size;
 	handler->rsp_data = rsp_data;
 	handler->rsp_size = rsp_size;
+	handler->rsp_status = rsp_status;
 	handler->match_prefix = match_prefix;
 	handler->action = action;
 
@@ -208,6 +211,7 @@ struct command_test_data {
 	uint16_t cmd_size;
 	const void *rsp_data;
 	uint16_t rsp_size;
+	uint8_t rsp_status;
 };
 
 static const unsigned char read_version_command[] =
@@ -223,6 +227,7 @@ static const struct command_test_data command_test_1 = {
 	.cmd_size = sizeof(read_version_command),
 	.rsp_data = read_version_response,
 	.rsp_size = sizeof(read_version_response),
+	.rsp_status = MGMT_STATUS_SUCCESS,
 };
 
 static const unsigned char read_info_command[] =
@@ -235,14 +240,28 @@ static const struct command_test_data command_test_2 = {
 	.cmd_size = sizeof(read_info_command),
 };
 
+static const unsigned char invalid_index_response[] =
+				{ 0x02, 0x00, 0xff, 0xff, 0x03, 0x00,
+				0x01, 0x00, 0x11 };
+
+static const struct command_test_data command_test_3 = {
+	.opcode = MGMT_OP_READ_VERSION,
+	.index = MGMT_INDEX_NONE,
+	.cmd_data = read_version_command,
+	.cmd_size = sizeof(read_version_command),
+	.rsp_data = invalid_index_response,
+	.rsp_size = sizeof(invalid_index_response),
+	.rsp_status = MGMT_STATUS_INVALID_INDEX,
+};
+
 static void test_command(gconstpointer data)
 {
 	const struct command_test_data *test = data;
 	struct context *context = create_context();
 
 	add_action(context, test->cmd_data, test->cmd_size,
-						test->rsp_data, test->rsp_size,
-						false, ACTION_PASSED);
+			test->rsp_data, test->rsp_size, test->rsp_status,
+			false, ACTION_PASSED);
 
 	mgmt_send(context->mgmt_client, test->opcode, test->index,
 					test->length, test->param,
@@ -251,12 +270,13 @@ static void test_command(gconstpointer data)
 	execute_context(context);
 }
 
-static void success_cb(uint8_t status, uint16_t length, const void *param,
+static void response_cb(uint8_t status, uint16_t length, const void *param,
 							void *user_data)
 {
 	struct context *context = user_data;
+	struct handler *handler = context->handler_list->data;
 
-	g_assert_cmpint(status, ==, MGMT_STATUS_SUCCESS);
+	g_assert_cmpint(status, ==, handler->rsp_status);
 
 	context_quit(context);
 }
@@ -267,12 +287,12 @@ static void test_response(gconstpointer data)
 	struct context *context = create_context();
 
 	add_action(context, test->cmd_data, test->cmd_size,
-						test->rsp_data, test->rsp_size,
-						false, ACTION_RESPOND);
+			test->rsp_data, test->rsp_size, test->rsp_status,
+			false, ACTION_RESPOND);
 
 	mgmt_send(context->mgmt_client, test->opcode, test->index,
 					test->length, test->param,
-					success_cb, context, NULL);
+					response_cb, context, NULL);
 
 	execute_context(context);
 }
@@ -286,6 +306,8 @@ int main(int argc, char *argv[])
 
 	g_test_add_data_func("/mgmt/response/1", &command_test_1,
 								test_response);
+	g_test_add_data_func("/mgmt/response/2", &command_test_3,
+								test_response);
 
 	return g_test_run();
 }
Signature · 1 line
1.9.3
author maintainer
#4 lore raw

PATCH v2 7/13 unit/test-mgmt: Add /mgmt/event/1 test

In reply to Luiz Augusto von Dentz

From: Luiz Augusto von Dentz <[email protected]>

This tests if an event can be received.

---
 unit/test-mgmt.c | 36 ++++++++++++++++++++++++++++++++++++
 1 file changed, 36 insertions(+)
unit/test-mgmt.c +36 -0
index 7bff828..a610d1a 100644
@@ -38,6 +38,7 @@
 
 struct context {
 	GMainLoop *main_loop;
+	int fd;
 	struct mgmt *mgmt_client;
 	guint server_source;
 	GList *handler_list;
@@ -143,6 +144,7 @@ static struct context *create_context(void)
 	err = socketpair(AF_UNIX, SOCK_SEQPACKET | SOCK_CLOEXEC, 0, sv);
 	g_assert(err == 0);
 
+	context->fd = sv[0];
 	channel = g_io_channel_unix_new(sv[0]);
 
 	g_io_channel_set_close_on_unref(channel, TRUE);
@@ -254,6 +256,16 @@ static const struct command_test_data command_test_3 = {
 	.rsp_status = MGMT_STATUS_INVALID_INDEX,
 };
 
+static const unsigned char event_index_added[] =
+				{ 0x04, 0x00, 0x01, 0x00, 0x00, 0x00 };
+
+static const struct command_test_data event_test_1 = {
+	.opcode = MGMT_EV_INDEX_ADDED,
+	.index = MGMT_INDEX_NONE,
+	.cmd_data = event_index_added,
+	.cmd_size = sizeof(event_index_added),
+};
+
 static void test_command(gconstpointer data)
 {
 	const struct command_test_data *test = data;
@@ -297,6 +309,28 @@ static void test_response(gconstpointer data)
 	execute_context(context);
 }
 
+static void event_cb(uint16_t index, uint16_t length, const void *param,
+							void *user_data)
+{
+	struct context *context = user_data;
+
+	context_quit(context);
+}
+
+static void test_event(gconstpointer data)
+{
+	const struct command_test_data *test = data;
+	struct context *context = create_context();
+
+	mgmt_register(context->mgmt_client, test->opcode, test->index,
+						event_cb, context, NULL);
+
+	g_assert_cmpint(write(context->fd, test->cmd_data, test->cmd_size), ==,
+								test->cmd_size);
+
+	execute_context(context);
+}
+
 int main(int argc, char *argv[])
 {
 	g_test_init(&argc, &argv, NULL);
@@ -309,5 +343,7 @@ int main(int argc, char *argv[])
 	g_test_add_data_func("/mgmt/response/2", &command_test_3,
 								test_response);
 
+	g_test_add_data_func("/mgmt/event/1", &event_test_1, test_event);
+
 	return g_test_run();
 }
Signature · 1 line
1.9.3
author maintainer
#5 lore raw

PATCH v2 8/13 unit/test-mgmt: Add /mgmt/event/2 test

In reply to Luiz Augusto von Dentz

From: Luiz Augusto von Dentz <[email protected]>

This tests if multiple events can be received.

---
 unit/test-mgmt.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
unit/test-mgmt.c +20 -0
index a610d1a..331ee42 100644
@@ -314,6 +314,9 @@ static void event_cb(uint16_t index, uint16_t length, const void *param,
 {
 	struct context *context = user_data;
 
+	if (g_test_verbose())
+		printf("Event received\n");
+
 	context_quit(context);
 }
 
@@ -331,6 +334,22 @@ static void test_event(gconstpointer data)
 	execute_context(context);
 }
 
+static void test_event2(gconstpointer data)
+{
+	const struct command_test_data *test = data;
+	struct context *context = create_context();
+
+	mgmt_register(context->mgmt_client, test->opcode, test->index,
+						event_cb, context, NULL);
+	mgmt_register(context->mgmt_client, test->opcode, test->index,
+						event_cb, context, NULL);
+
+	g_assert_cmpint(write(context->fd, test->cmd_data, test->cmd_size), ==,
+								test->cmd_size);
+
+	execute_context(context);
+}
+
 int main(int argc, char *argv[])
 {
 	g_test_init(&argc, &argv, NULL);
@@ -344,6 +363,7 @@ int main(int argc, char *argv[])
 								test_response);
 
 	g_test_add_data_func("/mgmt/event/1", &event_test_1, test_event);
+	g_test_add_data_func("/mgmt/event/2", &event_test_1, test_event2);
 
 	return g_test_run();
 }
Signature · 1 line
1.9.3
author maintainer
#6 lore raw

PATCH v2 9/13 unit/test-mgmt: Add /mgmt/unregister/1 test

In reply to Luiz Augusto von Dentz

From: Luiz Augusto von Dentz <[email protected]>

This tests if mgmt_unregister_all can be called within event callback.

---
 unit/test-mgmt.c | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)
unit/test-mgmt.c +29 -0
index 331ee42..8dcaa4b 100644
@@ -350,6 +350,32 @@ static void test_event2(gconstpointer data)
 	execute_context(context);
 }
 
+static void unregister_all_cb(uint16_t index, uint16_t length,
+					const void *param, void *user_data)
+{
+	struct context *context = user_data;
+
+	mgmt_unregister_all(context->mgmt_client);
+
+	context_quit(context);
+}
+
+static void test_unregister_all(gconstpointer data)
+{
+	const struct command_test_data *test = data;
+	struct context *context = create_context();
+
+	mgmt_register(context->mgmt_client, test->opcode, test->index,
+					unregister_all_cb, context, NULL);
+	mgmt_register(context->mgmt_client, test->opcode, test->index,
+						event_cb, context, NULL);
+
+	g_assert_cmpint(write(context->fd, test->cmd_data, test->cmd_size), ==,
+								test->cmd_size);
+
+	execute_context(context);
+}
+
 int main(int argc, char *argv[])
 {
 	g_test_init(&argc, &argv, NULL);
@@ -365,5 +391,8 @@ int main(int argc, char *argv[])
 	g_test_add_data_func("/mgmt/event/1", &event_test_1, test_event);
 	g_test_add_data_func("/mgmt/event/2", &event_test_1, test_event2);
 
+	g_test_add_data_func("/mgmt/unregister/1", &event_test_1,
+							test_unregister_all);
+
 	return g_test_run();
 }
Signature · 1 line
1.9.3
author maintainer
#7 lore raw

PATCH v2 10/13 unit/test-mgmt: Add /mgmt/unregister/2 test

In reply to Luiz Augusto von Dentz

From: Luiz Augusto von Dentz <[email protected]>

This tests if mgmt_unregister_index can be called within event callback.

---
 unit/test-mgmt.c | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)
unit/test-mgmt.c +28 -0
index 8dcaa4b..8cd5565 100644
@@ -376,6 +376,32 @@ static void test_unregister_all(gconstpointer data)
 	execute_context(context);
 }
 
+static void unregister_index_cb(uint16_t index, uint16_t length,
+					const void *param, void *user_data)
+{
+	struct context *context = user_data;
+
+	mgmt_unregister_index(context->mgmt_client, index);
+
+	context_quit(context);
+}
+
+static void test_unregister_index(gconstpointer data)
+{
+	const struct command_test_data *test = data;
+	struct context *context = create_context();
+
+	mgmt_register(context->mgmt_client, test->opcode, test->index,
+					unregister_index_cb, context, NULL);
+	mgmt_register(context->mgmt_client, test->opcode, test->index,
+						event_cb, context, NULL);
+
+	g_assert_cmpint(write(context->fd, test->cmd_data, test->cmd_size), ==,
+								test->cmd_size);
+
+	execute_context(context);
+}
+
 int main(int argc, char *argv[])
 {
 	g_test_init(&argc, &argv, NULL);
@@ -393,6 +419,8 @@ int main(int argc, char *argv[])
 
 	g_test_add_data_func("/mgmt/unregister/1", &event_test_1,
 							test_unregister_all);
+	g_test_add_data_func("/mgmt/unregister/2", &event_test_1,
+							test_unregister_index);
 
 	return g_test_run();
 }
Signature · 1 line
1.9.3
author maintainer
#8 lore raw

PATCH v2 11/13 unit/test-mgmt: Add /mgmt/destroy/1 test

In reply to Luiz Augusto von Dentz

From: Luiz Augusto von Dentz <[email protected]>

This tests if mgmt_unref can be called within event callback.

---
 unit/test-mgmt.c | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)
unit/test-mgmt.c +29 -0
index 8cd5565..c67678b 100644
@@ -386,6 +386,17 @@ static void unregister_index_cb(uint16_t index, uint16_t length,
 	context_quit(context);
 }
 
+static void destroy_cb(uint16_t index, uint16_t length, const void *param,
+							void *user_data)
+{
+	struct context *context = user_data;
+
+	mgmt_unref(context->mgmt_client);
+	context->mgmt_client = NULL;
+
+	context_quit(context);
+}
+
 static void test_unregister_index(gconstpointer data)
 {
 	const struct command_test_data *test = data;
@@ -402,6 +413,22 @@ static void test_unregister_index(gconstpointer data)
 	execute_context(context);
 }
 
+static void test_destroy(gconstpointer data)
+{
+	const struct command_test_data *test = data;
+	struct context *context = create_context();
+
+	mgmt_register(context->mgmt_client, test->opcode, test->index,
+					destroy_cb, context, NULL);
+	mgmt_register(context->mgmt_client, test->opcode, test->index,
+						event_cb, context, NULL);
+
+	g_assert_cmpint(write(context->fd, test->cmd_data, test->cmd_size), ==,
+								test->cmd_size);
+
+	execute_context(context);
+}
+
 int main(int argc, char *argv[])
 {
 	g_test_init(&argc, &argv, NULL);
@@ -422,5 +449,7 @@ int main(int argc, char *argv[])
 	g_test_add_data_func("/mgmt/unregister/2", &event_test_1,
 							test_unregister_index);
 
+	g_test_add_data_func("/mgmt/destroy/1", &event_test_1, test_destroy);
+
 	return g_test_run();
 }
Signature · 1 line
1.9.3
author maintainer
#9 lore raw

PATCH v2 12/13 shared/mgmt: Remove unnecessary code

In reply to Luiz Augusto von Dentz

From: Luiz Augusto von Dentz <[email protected]>

This flags were added because calling function like queue_foreach were not safe as the callbacks may remove or destroy the queue before returning but this has been fixed and it is now safe to do that.

---
 src/shared/mgmt.c | 89 +++++++------------------------------------------------
 1 file changed, 11 insertions(+), 78 deletions(-)
src/shared/mgmt.c +11 -78
index 43351e2..1ed635d 100644
@@ -51,9 +51,6 @@ struct mgmt {
 	struct queue *notify_list;
 	unsigned int next_request_id;
 	unsigned int next_notify_id;
-	bool need_notify_cleanup;
-	bool in_notify;
-	bool destroyed;
 	void *buf;
 	uint16_t len;
 	mgmt_debug_func_t debug_callback;
@@ -76,7 +73,6 @@ struct mgmt_notify {
 	unsigned int id;
 	uint16_t event;
 	uint16_t index;
-	bool removed;
 	mgmt_notify_func_t callback;
 	mgmt_destroy_func_t destroy;
 	void *user_data;
@@ -135,22 +131,6 @@ static bool match_notify_index(const void *a, const void *b)
 	return notify->index == index;
 }
 
-static bool match_notify_removed(const void *a, const void *b)
-{
-	const struct mgmt_notify *notify = a;
-
-	return notify->removed;
-}
-
-static void mark_notify_removed(void *data , void *user_data)
-{
-	struct mgmt_notify *notify = data;
-	uint16_t index = PTR_TO_UINT(user_data);
-
-	if (notify->index == index || index == MGMT_INDEX_NONE)
-		notify->removed = true;
-}
-
 static void write_watch_destroy(void *user_data)
 {
 	struct mgmt *mgmt = user_data;
@@ -265,9 +245,6 @@ static void request_complete(struct mgmt *mgmt, uint8_t status,
 		destroy_request(request);
 	}
 
-	if (mgmt->destroyed)
-		return;
-
 	wakeup_writer(mgmt);
 }
 
@@ -283,9 +260,6 @@ static void notify_handler(void *data, void *user_data)
 	struct mgmt_notify *notify = data;
 	struct event_index *match = user_data;
 
-	if (notify->removed)
-		return;
-
 	if (notify->event != match->event)
 		return;
 
@@ -303,28 +277,7 @@ static void process_notify(struct mgmt *mgmt, uint16_t event, uint16_t index,
 	struct event_index match = { .event = event, .index = index,
 					.length = length, .param = param };
 
-	mgmt->in_notify = true;
-
 	queue_foreach(mgmt->notify_list, notify_handler, &match);
-
-	mgmt->in_notify = false;
-
-	if (mgmt->need_notify_cleanup) {
-		queue_remove_all(mgmt->notify_list, match_notify_removed,
-							NULL, destroy_notify);
-		mgmt->need_notify_cleanup = false;
-	}
-}
-
-static void read_watch_destroy(void *user_data)
-{
-	struct mgmt *mgmt = user_data;
-
-	if (mgmt->destroyed) {
-		queue_destroy(mgmt->notify_list, NULL);
-		queue_destroy(mgmt->pending_list, NULL);
-		free(mgmt);
-	}
 }
 
 static bool can_read_data(struct io *io, void *user_data)
@@ -354,6 +307,8 @@ static bool can_read_data(struct io *io, void *user_data)
 	if (bytes_read < length + MGMT_HDR_SIZE)
 		return true;
 
+	mgmt_ref(mgmt);
+
 	switch (event) {
 	case MGMT_EV_CMD_COMPLETE:
 		cc = mgmt->buf + MGMT_HDR_SIZE;
@@ -385,8 +340,7 @@ static bool can_read_data(struct io *io, void *user_data)
 		break;
 	}
 
-	if (mgmt->destroyed)
-		return false;
+	mgmt_unref(mgmt);
 
 	return true;
 }
@@ -457,8 +411,7 @@ struct mgmt *mgmt_new(int fd)
 		return NULL;
 	}
 
-	if (!io_set_read_handler(mgmt->io, can_read_data, mgmt,
-						read_watch_destroy)) {
+	if (!io_set_read_handler(mgmt->io, can_read_data, mgmt, NULL)) {
 		queue_destroy(mgmt->notify_list, NULL);
 		queue_destroy(mgmt->pending_list, NULL);
 		queue_destroy(mgmt->reply_queue, NULL);
@@ -548,14 +501,11 @@ void mgmt_unref(struct mgmt *mgmt)
 	free(mgmt->buf);
 	mgmt->buf = NULL;
 
-	if (!mgmt->in_notify) {
-		queue_destroy(mgmt->notify_list, NULL);
-		queue_destroy(mgmt->pending_list, NULL);
-		free(mgmt);
-		return;
-	}
+	queue_destroy(mgmt->notify_list, NULL);
+	queue_destroy(mgmt->pending_list, NULL);
+	free(mgmt);
 
-	mgmt->destroyed = true;
+	return;
 }
 
 bool mgmt_set_debug(struct mgmt *mgmt, mgmt_debug_func_t callback,
@@ -817,14 +767,7 @@ bool mgmt_unregister(struct mgmt *mgmt, unsigned int id)
 	if (!notify)
 		return false;
 
-	if (!mgmt->in_notify) {
-		destroy_notify(notify);
-		return true;
-	}
-
-	notify->removed = true;
-	mgmt->need_notify_cleanup = true;
-
+	destroy_notify(notify);
 	return true;
 }
 
@@ -833,12 +776,7 @@ bool mgmt_unregister_index(struct mgmt *mgmt, uint16_t index)
 	if (!mgmt)
 		return false;
 
-	if (mgmt->in_notify) {
-		queue_foreach(mgmt->notify_list, mark_notify_removed,
-							UINT_TO_PTR(index));
-		mgmt->need_notify_cleanup = true;
-	} else
-		queue_remove_all(mgmt->notify_list, match_notify_index,
+	queue_remove_all(mgmt->notify_list, match_notify_index,
 					UINT_TO_PTR(index), destroy_notify);
 
 	return true;
@@ -849,12 +787,7 @@ bool mgmt_unregister_all(struct mgmt *mgmt)
 	if (!mgmt)
 		return false;
 
-	if (mgmt->in_notify) {
-		queue_foreach(mgmt->notify_list, mark_notify_removed,
-						UINT_TO_PTR(MGMT_INDEX_NONE));
-		mgmt->need_notify_cleanup = true;
-	} else
-		queue_remove_all(mgmt->notify_list, NULL, NULL, destroy_notify);
+	queue_remove_all(mgmt->notify_list, NULL, NULL, destroy_notify);
 
 	return true;
 }
Signature · 1 line
1.9.3
author maintainer
#10 lore raw

PATCH v2 13/13 doc/test-coverage: Update test count

In reply to Luiz Augusto von Dentz

From: Luiz Augusto von Dentz <[email protected]>

---
 doc/test-coverage.txt | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
doc/test-coverage.txt +2 -2
index 30271b1..533a8c9 100644
@@ -12,11 +12,11 @@ test-eir		  14	EIR and AD parsing
 test-lib		  14	SDP library functions
 test-sdp		 133	SDP qualification test cases
 test-uuid		  30	UUID conversion handling
-test-mgmt		   2	Management interface handling
+test-mgmt		   9	Management interface handling
 test-crypto		   4	Cryptographic toolbox helpers
 test-textfile		   4	Old textfile storage format
 test-ringbuf		   3	Ring buffer functionality
-test-queue		   3	Queue handling functionality
+test-queue		   6	Queue handling functionality
 test-uhid		   6	Userspace HID functionality
 test-hfp		  29	HFP Audio Gateway functionality
 test-avdtp		  60	AVDTP qualification test cases
Signature · 1 line
1.9.3