rfcomm-tester: Add test to close socket while connecting
authorLuiz Augusto von Dentz <luiz.von.dentz@intel.com>
Tue, 13 Sep 2022 23:32:38 +0000 (16:32 -0700)
committerAyush Garg <ayush.garg@samsung.com>
Mon, 15 May 2023 09:25:54 +0000 (14:55 +0530)
This adds Basic RFCOMM Socket Client - Close test which attempt to close
socket while connecting.

Signed-off-by: Manika Shrivastava <manika.sh@samsung.com>
Signed-off-by: Ayush Garg <ayush.garg@samsung.com>
tools/rfcomm-tester.c

index d594ebf..a9adf7f 100755 (executable)
@@ -47,6 +47,7 @@ struct test_data {
 struct rfcomm_client_data {
        uint8_t server_channel;
        uint8_t client_channel;
+       bool close;
        int expected_connect_err;
        const uint8_t *send_data;
        const uint8_t *read_data;
@@ -297,6 +298,12 @@ const struct rfcomm_client_data connect_success = {
        .client_channel = 0x0c
 };
 
+const struct rfcomm_client_data connect_close = {
+       .server_channel = 0x0c,
+       .client_channel = 0x0c,
+       .close = true
+};
+
 const uint8_t data[] = {0, 1, 2, 3, 4, 5, 6, 7, 8};
 
 const struct rfcomm_client_data connect_send_success = {
@@ -519,6 +526,8 @@ static gboolean rc_connect_cb(GIOChannel *io, GIOCondition cond,
                return false;
        }
 
+       data->io = NULL;
+
        if (err < 0)
                tester_test_failed();
        else
@@ -527,6 +536,20 @@ static gboolean rc_connect_cb(GIOChannel *io, GIOCondition cond,
        return false;
 }
 
+static gboolean rc_close_cb(GIOChannel *io, GIOCondition cond,
+                                                       gpointer user_data)
+{
+       struct test_data *data = tester_get_data();
+
+       data->io_id = 0;
+
+       tester_print("Closed");
+
+       tester_test_passed();
+
+       return false;
+}
+
 static void client_hook_func(const void *data, uint16_t len,
                                                        void *user_data)
 {
@@ -627,13 +650,20 @@ static void test_connect(const void *test_data)
        }
 
        io = g_io_channel_unix_new(sk);
-       g_io_channel_set_close_on_unref(io, TRUE);
 
-       data->io_id = g_io_add_watch(io, G_IO_OUT, rc_connect_cb, NULL);
+       tester_print("Connect in progress %d", sk);
+
+       if (cli->close) {
+               data->io_id = g_io_add_watch(io, G_IO_NVAL, rc_close_cb, NULL);
+               close(sk);
+               tester_print("Close socket %d", sk);
+       } else {
+               g_io_channel_set_close_on_unref(io, TRUE);
+               data->io_id = g_io_add_watch(io, G_IO_OUT, rc_connect_cb,
+                                               NULL);
+       }
 
        g_io_channel_unref(io);
-
-       tester_print("Connect in progress %d", sk);
 }
 
 static gboolean server_received_data(GIOChannel *io, GIOCondition cond,
@@ -815,6 +845,9 @@ int main(int argc, char *argv[])
                                test_connect);
        test_rfcomm("Basic RFCOMM Socket Client - Conn Refused",
                        &connect_nval, setup_powered_client, test_connect);
+       test_rfcomm("Basic RFCOMM Socket Client - Close",
+                               &connect_close, setup_powered_client,
+                               test_connect);
        test_rfcomm("Basic RFCOMM Socket Server - Success", &listen_success,
                                        setup_powered_server, test_server);
        test_rfcomm("Basic RFCOMM Socket Server - Write Success",