Linux bluetooth development
 help / color / mirror / Atom feed
From: Luiz Augusto von Dentz <luiz.dentz@gmail.com>
To: linux-bluetooth@vger.kernel.org
Subject: [PATCH BlueZ v4 6/6] l2cap-tester: Add tests for partially accepting Ext-Flowctl requests
Date: Wed, 25 Nov 2026 16:12:00 -0500	[thread overview]
Message-ID: <20261125211200.1955466-7-luiz.dentz@gmail.com> (raw)
In-Reply-To: <20261125211200.1955466-1-luiz.dentz@gmail.com>

From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

Add the following tests:

L2CAP Ext-Flowctl Server - Accept 1 Refuse 1
L2CAP Ext-Flowctl Server - Refuse 1 Accept 1

A listener using deferred setup may accept only some of the channels of
an Enhanced Credit Based Connection Request, e.g. because it limits the
number of channels, and refuse the rest, so the response is expected to
be successful for the ones accepted, with their destination CID, and to
have a destination CID of 0 for the ones refused, with a result telling
that only some of the connections were refused.

The tests send a request with 2 channels and have the listener authorize
one and close the other, in both orders.

Assisted-by: OpenCode:claude-fable-5.2
Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
---
 tools/l2cap-tester.c | 115 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 115 insertions(+)

diff --git a/tools/l2cap-tester.c b/tools/l2cap-tester.c
index 58fd77a3ec79..ca868e9d72ec 100644
--- a/tools/l2cap-tester.c
+++ b/tools/l2cap-tester.c
@@ -144,6 +144,8 @@ struct l2cap_data {
 	bool pending_rsp;
 	/* Link key type the controller reports instead of the actual one */
 	uint8_t link_key_type;
+	/* Bitmask of the incoming connections the listener refuses */
+	uint8_t refuse_mask;
 
 	bool shut_sock_wr;
 
@@ -1657,6 +1659,54 @@ static const uint8_t ecred_connect_req_2[] = {
 						0x42, 0x00, /* SCID #2 */
 };
 
+/* Only some of the channels of the request are refused: the response is
+ * successful for the others, with their destination CID, and has none for
+ * the refused ones.
+ */
+static const uint8_t ecred_connect_rsp_refuse_2[] = {
+						0xa0, 0x02, /* MTU */
+						0xbc, 0x00, /* MPS */
+						0x04, 0x00, /* Credits */
+						0x04, 0x00, /* Result */
+						0x40, 0x00, /* DCID #1 */
+						0x00, 0x00, /* DCID #2 */
+};
+
+static const uint8_t ecred_connect_rsp_refuse_1[] = {
+						0xa0, 0x02, /* MTU */
+						0xbc, 0x00, /* MPS */
+						0x04, 0x00, /* Credits */
+						0x04, 0x00, /* Result */
+						0x00, 0x00, /* DCID #1 */
+						0x41, 0x00, /* DCID #2 */
+};
+
+static const struct l2cap_data ext_flowctl_server_accept_1_refuse_2_test = {
+	.server_psm = 0x0080,
+	.defer = true,
+	.refuse_mask = 0x02,
+	.mode = BT_MODE_EXT_FLOWCTL,
+	.send_cmd_code = BT_L2CAP_PDU_ECRED_CONN_REQ,
+	.send_cmd = ecred_connect_req_2,
+	.send_cmd_len = ARRAY_SIZE(ecred_connect_req_2),
+	.expect_cmd_code = BT_L2CAP_PDU_ECRED_CONN_RSP,
+	.expect_cmd = ecred_connect_rsp_refuse_2,
+	.expect_cmd_len = sizeof(ecred_connect_rsp_refuse_2),
+};
+
+static const struct l2cap_data ext_flowctl_server_refuse_1_accept_2_test = {
+	.server_psm = 0x0080,
+	.defer = true,
+	.refuse_mask = 0x01,
+	.mode = BT_MODE_EXT_FLOWCTL,
+	.send_cmd_code = BT_L2CAP_PDU_ECRED_CONN_REQ,
+	.send_cmd = ecred_connect_req_2,
+	.send_cmd_len = ARRAY_SIZE(ecred_connect_req_2),
+	.expect_cmd_code = BT_L2CAP_PDU_ECRED_CONN_RSP,
+	.expect_cmd = ecred_connect_rsp_refuse_1,
+	.expect_cmd_len = sizeof(ecred_connect_rsp_refuse_1),
+};
+
 static const struct l2cap_data ext_flowctl_server_accept_2_close_1_test = {
 	.server_psm = 0x0080,
 	.defer = true,
@@ -4561,6 +4611,63 @@ fail:
 	return false;
 }
 
+/* Accept the connections of the request in order, authorizing the ones the
+ * listener doesn't refuse, which are kept open, and closing without
+ * authorizing the ones it does.
+ */
+static bool ext_flowctl_accept_refuse_listen_cb(struct tester_io *io,
+					uint32_t cond, void *user_data)
+{
+	struct test_data *data = tester_get_data();
+	const struct l2cap_data *l2data = data->test_data;
+	struct tester_io *new_io;
+	int sk, new_sk;
+	int idx = data->step++;
+
+	sk = tester_io_get_fd(io);
+
+	new_sk = accept(sk, NULL, NULL);
+	if (new_sk < 0) {
+		tester_warn("accept failed: %s (%u)", strerror(errno), errno);
+		tester_test_failed();
+		return false;
+	}
+
+	if (l2data->refuse_mask & (1 << idx)) {
+		tester_print("Refuse sk = %d", new_sk);
+		close(new_sk);
+	} else {
+		tester_print("Authorize sk = %d", new_sk);
+
+		new_io = tester_io_new(new_sk);
+		tester_io_set_close_on_unref(new_io, true);
+
+		if (!data->io_channels)
+			data->io_channels = queue_new();
+		queue_push_tail(data->io_channels, new_io);
+
+		if (!defer_accept(data, tester_io_ref(new_io), NULL)) {
+			tester_io_unref(new_io);
+			tester_warn("Unable to accept deferred setup");
+			tester_test_failed();
+			return false;
+		}
+	}
+
+	/* The request has two channels */
+	if (data->step < 2)
+		return true;
+
+	data->io_id = 0;
+	return false;
+}
+
+static void test_ext_flowctl_server_accept_refuse(const void *test_data)
+{
+	start_test_server(test_data, send_req_new_conn,
+					ext_flowctl_accept_refuse_listen_cb);
+}
+
 static void test_ext_flowctl_server_accept_2_close_1(const void *test_data)
 {
 	struct test_data *data = tester_get_data();
@@ -5173,6 +5280,14 @@ int main(int argc, char *argv[])
 				&ext_flowctl_server_accept_2_close_1_test,
 				setup_powered_server,
 				test_ext_flowctl_server_accept_2_close_1);
+	test_l2cap_le("L2CAP Ext-Flowctl Server - Accept 1 Refuse 1",
+				&ext_flowctl_server_accept_1_refuse_2_test,
+				setup_powered_server,
+				test_ext_flowctl_server_accept_refuse);
+	test_l2cap_le("L2CAP Ext-Flowctl Server - Refuse 1 Accept 1",
+				&ext_flowctl_server_refuse_1_accept_2_test,
+				setup_powered_server,
+				test_ext_flowctl_server_accept_refuse);
 	test_l2cap_le("L2CAP Ext-Flowctl Server - PHY",
 				&ext_flowctl_server_phy_test,
 				setup_powered_server, test_server);
-- 
2.56.0


      parent reply	other threads:[~2026-11-25 21:12 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-25 21:11 [PATCH BlueZ v4 0/6] Add tests for security of deferred setup Luiz Augusto von Dentz
2026-11-25 21:11 ` [PATCH BlueZ v4 1/6] emulator/bthost: Add helpers to test " Luiz Augusto von Dentz
2026-11-25 23:46   ` Add tests for " bluez.test.bot
2026-11-25 21:11 ` [PATCH BlueZ v4 2/6] emulator/btdev: Add btdev_set_link_key_type Luiz Augusto von Dentz
2026-11-25 21:11 ` [PATCH BlueZ v4 3/6] l2cap-tester: Add tests for security of deferred setup Luiz Augusto von Dentz
2026-11-25 21:11 ` [PATCH BlueZ v4 4/6] rfcomm-tester: Add tests for " Luiz Augusto von Dentz
2026-11-25 21:11 ` [PATCH BlueZ v4 5/6] l2cap-tester: Set IO capability in Client SSP - Success 2 Luiz Augusto von Dentz
2026-11-25 21:12 ` Luiz Augusto von Dentz [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261125211200.1955466-7-luiz.dentz@gmail.com \
    --to=luiz.dentz@gmail.com \
    --cc=linux-bluetooth@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox