1 /* Test the __sockaddr_un_set function.
2 Copyright (C) 2022 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
18
19 /* Re-compile the function because the version in libc is not
20 exported. */
21 #include "sockaddr_un_set.c"
22
23 #include <support/check.h>
24
25 static int
do_test(void)26 do_test (void)
27 {
28 struct sockaddr_un sun;
29
30 memset (&sun, 0xcc, sizeof (sun));
31 __sockaddr_un_set (&sun, "");
32 TEST_COMPARE (sun.sun_family, AF_UNIX);
33 TEST_COMPARE (__sockaddr_un_set (&sun, ""), 0);
34
35 memset (&sun, 0xcc, sizeof (sun));
36 TEST_COMPARE (__sockaddr_un_set (&sun, "/example"), 0);
37 TEST_COMPARE_STRING (sun.sun_path, "/example");
38
39 {
40 char pathname[108]; /* Length of sun_path (ABI constant). */
41 memset (pathname, 'x', sizeof (pathname));
42 pathname[sizeof (pathname) - 1] = '\0';
43 memset (&sun, 0xcc, sizeof (sun));
44 TEST_COMPARE (__sockaddr_un_set (&sun, pathname), 0);
45 TEST_COMPARE (sun.sun_family, AF_UNIX);
46 TEST_COMPARE_STRING (sun.sun_path, pathname);
47 }
48
49 {
50 char pathname[109];
51 memset (pathname, 'x', sizeof (pathname));
52 pathname[sizeof (pathname) - 1] = '\0';
53 memset (&sun, 0xcc, sizeof (sun));
54 errno = 0;
55 TEST_COMPARE (__sockaddr_un_set (&sun, pathname), -1);
56 TEST_COMPARE (errno, EINVAL);
57 }
58
59 return 0;
60 }
61
62 #include <support/test-driver.c>
63