diff mbox series

[v3,2/3] tests/util-sockets: add abstract unix socket cases

Message ID 20200510061422.24841-3-zxq_yx_007@163.com (mailing list archive)
State New, archived
Headers show
Series None | expand

Commit Message

zhao xiao qiang May 10, 2020, 6:14 a.m. UTC
add cases to test tight and non-tight for abstract address type

Signed-off-by: xiaoqiang zhao <zxq_yx_007@163.com>
---
 tests/test-util-sockets.c | 83 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 83 insertions(+)

Comments

Daniel P. Berrangé May 13, 2020, 3:49 p.m. UTC | #1
On Sun, May 10, 2020 at 02:14:21PM +0800, xiaoqiang zhao wrote:
> add cases to test tight and non-tight for abstract address type
> 
> Signed-off-by: xiaoqiang zhao <zxq_yx_007@163.com>
> ---
>  tests/test-util-sockets.c | 83 +++++++++++++++++++++++++++++++++++++++
>  1 file changed, 83 insertions(+)
> 
> diff --git a/tests/test-util-sockets.c b/tests/test-util-sockets.c
> index 5fd947c7bf..8042fb9276 100644
> --- a/tests/test-util-sockets.c
> +++ b/tests/test-util-sockets.c
> @@ -227,6 +227,84 @@ static void test_socket_fd_pass_num_nocli(void)
>      g_free(addr.u.fd.str);
>  }
>  
> +static gpointer unix_server_thread_func(gpointer user_data)
> +{
> +    SocketAddress addr;
> +    Error *err = NULL;
> +    int fd = -1;
> +    int connfd = -1;
> +    struct sockaddr_un un;
> +    socklen_t len = sizeof(un);
> +    char name[] = "/tmp/unix-test.sock";

Fixed filenames are bad, as even though this is the abstract
namespace and thus safe from on-disk clashes, the abstract
namespace is still OS global. We should append both the PID
and a sequence of random bytes to get something which doesnt
clash if two copies of the unit test run concurrently.

> +
> +    addr.type = SOCKET_ADDRESS_TYPE_UNIX;
> +    addr.u.q_unix.path = name;
> +    addr.u.q_unix.tight = user_data != NULL;
> +    addr.u.q_unix.abstract = true;
> +
> +    fd = socket_listen(&addr, 1, &err);
> +    g_assert_cmpint(fd, >=, 0);
> +    g_assert(fd_is_socket(fd));
> +
> +    connfd = accept(fd, (struct sockaddr *)&un, &len);
> +    g_assert_cmpint(connfd, !=, -1);
> +
> +    close(fd);
> +
> +    return NULL;
> +}
> +
> +static gpointer unix_client_thread_func(gpointer user_data)
> +{
> +    SocketAddress addr;
> +    Error *err = NULL;
> +    int fd = -1;
> +    char name[] = "/tmp/unix-test.sock";
> +
> +    addr.type = SOCKET_ADDRESS_TYPE_UNIX;
> +    addr.u.q_unix.path = name;
> +    addr.u.q_unix.tight = user_data != NULL;
> +    addr.u.q_unix.abstract = true;
> +
> +    fd = socket_connect(&addr, &err);
> +
> +    g_assert_cmpint(fd, >=, 0);
> +
> +    close(fd);
> +
> +    return NULL;
> +}
> +
> +static void test_socket_unix_abstract_good(void)
> +{
> +    /* non tight socklen serv and cli */
> +    GThread *serv = g_thread_new("abstract_unix_server",
> +                                 unix_server_thread_func,
> +                                 NULL);
> +
> +    sleep(1);
> +
> +    GThread *cli = g_thread_new("abstruct_unix_client",
> +                                unix_client_thread_func,
> +                                NULL);
> +
> +    g_thread_join(cli);
> +    g_thread_join(serv);
> +
> +    /* tight socklen serv and cli */
> +    serv = g_thread_new("abstract_unix_server",
> +                        unix_server_thread_func,
> +                        (gpointer)1);
> +
> +    sleep(1);
> +
> +    cli = g_thread_new("abstruct_unix_client",
> +                       unix_client_thread_func,
> +                       (gpointer)1);
> +
> +    g_thread_join(cli);
> +    g_thread_join(serv);
> +}
>  
>  int main(int argc, char **argv)
>  {
> @@ -265,6 +343,11 @@ int main(int argc, char **argv)
>                          test_socket_fd_pass_num_nocli);
>      }
>  
> +#ifdef __linux__
> +        g_test_add_func("/util/socket/unix-abstract/good",
> +                        test_socket_unix_abstract_good);
> +#endif
> +
>  end:
>      return g_test_run();
>  }
> -- 
> 2.17.1
> 
> 
> 

Regards,
Daniel
zhao xiao qiang May 14, 2020, 12:26 a.m. UTC | #2
在 2020/5/13 下午11:49, Daniel P. Berrangé 写道:
> On Sun, May 10, 2020 at 02:14:21PM +0800, xiaoqiang zhao wrote:
>> add cases to test tight and non-tight for abstract address type
>>
>> Signed-off-by: xiaoqiang zhao<zxq_yx_007@163.com>
>> ---
>>   tests/test-util-sockets.c | 83 +++++++++++++++++++++++++++++++++++++++
>>   1 file changed, 83 insertions(+)
>>
>> diff --git a/tests/test-util-sockets.c b/tests/test-util-sockets.c
>> index 5fd947c7bf..8042fb9276 100644
>> --- a/tests/test-util-sockets.c
>> +++ b/tests/test-util-sockets.c
>> @@ -227,6 +227,84 @@ static void test_socket_fd_pass_num_nocli(void)
>>       g_free(addr.u.fd.str);
>>   }
>>   
>> +static gpointer unix_server_thread_func(gpointer user_data)
>> +{
>> +    SocketAddress addr;
>> +    Error *err = NULL;
>> +    int fd = -1;
>> +    int connfd = -1;
>> +    struct sockaddr_un un;
>> +    socklen_t len = sizeof(un);
>> +    char name[] = "/tmp/unix-test.sock";
> Fixed filenames are bad, as even though this is the abstract
> namespace and thus safe from on-disk clashes, the abstract
> namespace is still OS global. We should append both the PID
> and a sequence of random bytes to get something which doesnt
> clash if two copies of the unit test run concurrently.
>
agree.
diff mbox series

Patch

diff --git a/tests/test-util-sockets.c b/tests/test-util-sockets.c
index 5fd947c7bf..8042fb9276 100644
--- a/tests/test-util-sockets.c
+++ b/tests/test-util-sockets.c
@@ -227,6 +227,84 @@  static void test_socket_fd_pass_num_nocli(void)
     g_free(addr.u.fd.str);
 }
 
+static gpointer unix_server_thread_func(gpointer user_data)
+{
+    SocketAddress addr;
+    Error *err = NULL;
+    int fd = -1;
+    int connfd = -1;
+    struct sockaddr_un un;
+    socklen_t len = sizeof(un);
+    char name[] = "/tmp/unix-test.sock";
+
+    addr.type = SOCKET_ADDRESS_TYPE_UNIX;
+    addr.u.q_unix.path = name;
+    addr.u.q_unix.tight = user_data != NULL;
+    addr.u.q_unix.abstract = true;
+
+    fd = socket_listen(&addr, 1, &err);
+    g_assert_cmpint(fd, >=, 0);
+    g_assert(fd_is_socket(fd));
+
+    connfd = accept(fd, (struct sockaddr *)&un, &len);
+    g_assert_cmpint(connfd, !=, -1);
+
+    close(fd);
+
+    return NULL;
+}
+
+static gpointer unix_client_thread_func(gpointer user_data)
+{
+    SocketAddress addr;
+    Error *err = NULL;
+    int fd = -1;
+    char name[] = "/tmp/unix-test.sock";
+
+    addr.type = SOCKET_ADDRESS_TYPE_UNIX;
+    addr.u.q_unix.path = name;
+    addr.u.q_unix.tight = user_data != NULL;
+    addr.u.q_unix.abstract = true;
+
+    fd = socket_connect(&addr, &err);
+
+    g_assert_cmpint(fd, >=, 0);
+
+    close(fd);
+
+    return NULL;
+}
+
+static void test_socket_unix_abstract_good(void)
+{
+    /* non tight socklen serv and cli */
+    GThread *serv = g_thread_new("abstract_unix_server",
+                                 unix_server_thread_func,
+                                 NULL);
+
+    sleep(1);
+
+    GThread *cli = g_thread_new("abstruct_unix_client",
+                                unix_client_thread_func,
+                                NULL);
+
+    g_thread_join(cli);
+    g_thread_join(serv);
+
+    /* tight socklen serv and cli */
+    serv = g_thread_new("abstract_unix_server",
+                        unix_server_thread_func,
+                        (gpointer)1);
+
+    sleep(1);
+
+    cli = g_thread_new("abstruct_unix_client",
+                       unix_client_thread_func,
+                       (gpointer)1);
+
+    g_thread_join(cli);
+    g_thread_join(serv);
+}
 
 int main(int argc, char **argv)
 {
@@ -265,6 +343,11 @@  int main(int argc, char **argv)
                         test_socket_fd_pass_num_nocli);
     }
 
+#ifdef __linux__
+        g_test_add_func("/util/socket/unix-abstract/good",
+                        test_socket_unix_abstract_good);
+#endif
+
 end:
     return g_test_run();
 }