1 /* Test mntent interface with escaped sequences.
2 Copyright (C) 2020-2022 Free Software Foundation, Inc.
3
4 This file is part of the GNU C Library.
5
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
10
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <https://www.gnu.org/licenses/>. */
19
20 #include <mntent.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <support/check.h>
24
25 struct const_mntent
26 {
27 const char *mnt_fsname;
28 const char *mnt_dir;
29 const char *mnt_type;
30 const char *mnt_opts;
31 int mnt_freq;
32 int mnt_passno;
33 const char *expected;
34 };
35
36 struct const_mntent tests[] =
37 {
38 {"/dev/hda1", "/some dir", "ext2", "defaults", 1, 2,
39 "/dev/hda1 /some\\040dir ext2 defaults 1 2\n"},
40 {"device name", "/some dir", "tmpfs", "defaults", 1, 2,
41 "device\\040name /some\\040dir tmpfs defaults 1 2\n"},
42 {" ", "/some dir", "tmpfs", "defaults", 1, 2,
43 "\\040 /some\\040dir tmpfs defaults 1 2\n"},
44 {"\t", "/some dir", "tmpfs", "defaults", 1, 2,
45 "\\011 /some\\040dir tmpfs defaults 1 2\n"},
46 {"\\", "/some dir", "tmpfs", "defaults", 1, 2,
47 "\\134 /some\\040dir tmpfs defaults 1 2\n"},
48 };
49
50 static int
do_test(void)51 do_test (void)
52 {
53 for (int i = 0; i < sizeof (tests) / sizeof (struct const_mntent); i++)
54 {
55 char buf[128];
56 struct mntent *ret, curtest;
57 FILE *fp = fmemopen (buf, sizeof (buf), "w+");
58
59 if (fp == NULL)
60 {
61 printf ("Failed to open file\n");
62 return 1;
63 }
64
65 curtest.mnt_fsname = strdupa (tests[i].mnt_fsname);
66 curtest.mnt_dir = strdupa (tests[i].mnt_dir);
67 curtest.mnt_type = strdupa (tests[i].mnt_type);
68 curtest.mnt_opts = strdupa (tests[i].mnt_opts);
69 curtest.mnt_freq = tests[i].mnt_freq;
70 curtest.mnt_passno = tests[i].mnt_passno;
71
72 if (addmntent (fp, &curtest) != 0)
73 {
74 support_record_failure ();
75 continue;
76 }
77
78 TEST_COMPARE_STRING (buf, tests[i].expected);
79
80 rewind (fp);
81 ret = getmntent (fp);
82 if (ret == NULL)
83 {
84 support_record_failure ();
85 continue;
86 }
87
88 TEST_COMPARE_STRING(tests[i].mnt_fsname, ret->mnt_fsname);
89 TEST_COMPARE_STRING(tests[i].mnt_dir, ret->mnt_dir);
90 TEST_COMPARE_STRING(tests[i].mnt_type, ret->mnt_type);
91 TEST_COMPARE_STRING(tests[i].mnt_opts, ret->mnt_opts);
92 TEST_COMPARE(tests[i].mnt_freq, ret->mnt_freq);
93 TEST_COMPARE(tests[i].mnt_passno, ret->mnt_passno);
94
95 fclose (fp);
96 }
97
98 return 0;
99 }
100
101 #include <support/test-driver.c>
102