1/* Test the xreadlink function.
2 Copyright (C) 2017-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#include <errno.h>
20#include <fcntl.h>
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24#include <support/check.h>
25#include <support/support.h>
26#include <support/temp_file.h>
27#include <support/xunistd.h>
28
29static int
30do_test (void)
31{
32 char *dir = support_create_temp_directory (base: "tst-xreadlink-");
33 char *symlink_name = xasprintf (format: "%s/symlink", dir);
34 add_temp_file (name: symlink_name);
35
36 /* The limit 10000 is arbitrary and simply there to prevent an
37 attempt to exhaust all available disk space. */
38 for (int size = 1; size < 10000; ++size)
39 {
40 char *contents = xmalloc (n: size + 1);
41 for (int i = 0; i < size; ++i)
42 contents[i] = 'a' + (rand () % 26);
43 contents[size] = '\0';
44 if (symlink (from: contents, to: symlink_name) != 0)
45 {
46 if (errno == ENAMETOOLONG)
47 {
48 printf (format: "info: ENAMETOOLONG failure at %d bytes\n", size);
49 free (ptr: contents);
50 break;
51 }
52 FAIL_EXIT1 ("symlink (%d bytes): %m", size);
53 }
54
55 char *readlink_result = xreadlink (path: symlink_name);
56 TEST_VERIFY (strcmp (readlink_result, contents) == 0);
57 free (ptr: readlink_result);
58 xunlink (path: symlink_name);
59 free (ptr: contents);
60 }
61
62 /* Create an empty file to suppress the temporary file deletion
63 warning. */
64 xclose (xopen (path: symlink_name, O_WRONLY | O_CREAT, 0));
65
66 free (ptr: symlink_name);
67 free (ptr: dir);
68
69 return 0;
70}
71
72#include <support/test-driver.c>
73

source code of glibc/support/tst-xreadlink.c