1 | /* Test autofs "ignore" filtering for getment_r. |
2 | Copyright (C) 2019-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 <array_length.h> |
20 | #include <errno.h> |
21 | #include <mntent.h> |
22 | #include <stdio.h> |
23 | #include <stdlib.h> |
24 | #include <string.h> |
25 | #include <support/check.h> |
26 | #include <support/temp_file.h> |
27 | #include <support/xstdio.h> |
28 | #include <support/xunistd.h> |
29 | |
30 | struct test_case |
31 | { |
32 | const char *line; |
33 | struct |
34 | { |
35 | /* Like struct mntent, but with const pointers. */ |
36 | const char *mnt_fsname; |
37 | const char *mnt_dir; |
38 | const char *mnt_type; |
39 | const char *mnt_opts; |
40 | int mnt_freq; |
41 | int mnt_passno; |
42 | } expected; |
43 | }; |
44 | |
45 | static struct test_case test_cases[] = |
46 | { |
47 | { "/etc/auto.direct /mnt/auto/1 autofs defaults 0 0" , |
48 | { "/etc/auto.direct" , "/mnt/auto/1" , "autofs" , "defaults" , 0, 0 } }, |
49 | |
50 | /* These entries are filtered out. */ |
51 | { "/etc/auto.2 /mnt/auto/2 autofs ignore 0 0" , { NULL, } }, |
52 | { "/etc/auto.3 /mnt/auto/3 autofs ignore,other 1 2" , { NULL, } }, |
53 | { "/etc/auto.4 /mnt/auto/4 autofs other,ignore 3 4" , { NULL, } }, |
54 | { "/etc/auto.5 /mnt/auto/5 autofs opt1,ignore,opt2 5 6" , { NULL, } }, |
55 | |
56 | /* Dummy entry to make the desynchronization more obvious. */ |
57 | { "/dev/sda1 / xfs defaults 0 0" , |
58 | { "/dev/sda1" , "/" , "xfs" , "defaults" , 0, 0 } }, |
59 | |
60 | /* These are not filtered because the file system is not autofs. */ |
61 | { "/etc/auto.direct /mnt/auto/6 autofs1 ignore 0 0" , |
62 | { "/etc/auto.direct" , "/mnt/auto/6" , "autofs1" , "ignore" , 0, 0 } }, |
63 | { "/etc/auto.direct /mnt/auto/7 autofs1 ignore,other 0 0" , |
64 | { "/etc/auto.direct" , "/mnt/auto/7" , "autofs1" , "ignore,other" , 0, 0 } }, |
65 | { "/etc/auto.direct /mnt/auto/8 autofs1 other,ignore 0 0" , |
66 | { "/etc/auto.direct" , "/mnt/auto/8" , "autofs1" , "other,ignore" , 0, 0 } }, |
67 | { "/etc/auto.direct /mnt/auto/9 autofs1 opt1,ignore,opt2 0 0" , |
68 | { "/etc/auto.direct" , "/mnt/auto/9" , "autofs1" , "opt1,ignore,opt2" , } }, |
69 | |
70 | /* These are not filtered because the string "ignore" is not an |
71 | option name. */ |
72 | { "/etc/auto.direct /mnt/auto/10 autofs noignore 1 2" , |
73 | { "/etc/auto.direct" , "/mnt/auto/10" , "autofs" , "noignore" , 1, 2 } }, |
74 | { "/etc/auto.direct /mnt/auto/11 autofs noignore,other 0 0" , |
75 | { "/etc/auto.direct" , "/mnt/auto/11" , "autofs" , "noignore,other" , } }, |
76 | { "/etc/auto.direct /mnt/auto/12 autofs other,noignore 0 0" , |
77 | { "/etc/auto.direct" , "/mnt/auto/12" , "autofs" , "other,noignore" , } }, |
78 | { "/etc/auto.direct /mnt/auto/13 autofs errors=ignore 0 0" , |
79 | { "/etc/auto.direct" , "/mnt/auto/13" , "autofs" , "errors=ignore" , } }, |
80 | { "/etc/auto.direct /mnt/auto/14 autofs errors=ignore,other 0 0" , |
81 | { "/etc/auto.direct" , "/mnt/auto/14" , "autofs" , |
82 | "errors=ignore,other" , } }, |
83 | { "/etc/auto.direct /mnt/auto/15 autofs other,errors=ignore 0 0" , |
84 | { "/etc/auto.direct" , "/mnt/auto/15" , "autofs" , |
85 | "other,errors=ignore" , } }, |
86 | |
87 | /* These are not filtered because the string is escaped. '\151' |
88 | is 'i', but it is not actually decoded by the parser. */ |
89 | { "/etc/auto.\\151gnore /mnt/auto/16 autofs \\151gnore 0 0" , |
90 | { "/etc/auto.\\151gnore" , "/mnt/auto/16" , "autofs" , |
91 | "\\151gnore" , } }, |
92 | }; |
93 | |
94 | static int |
95 | do_test (void) |
96 | { |
97 | char *path; |
98 | xclose (create_temp_file (base: "tst-mntent-autofs-" , filename: &path)); |
99 | |
100 | /* Write the test file. */ |
101 | FILE *fp = xfopen (path, mode: "w" ); |
102 | for (size_t i = 0; i < array_length (test_cases); ++i) |
103 | fprintf (fp, "%s\n" , test_cases[i].line); |
104 | xfclose (fp); |
105 | |
106 | /* Open the test file again, this time for parsing. */ |
107 | fp = setmntent (file: path, mode: "r" ); |
108 | TEST_VERIFY_EXIT (fp != NULL); |
109 | char buffer[512]; |
110 | struct mntent me; |
111 | |
112 | for (size_t i = 0; i < array_length (test_cases); ++i) |
113 | { |
114 | if (test_cases[i].expected.mnt_type == NULL) |
115 | continue; |
116 | |
117 | memset (buffer, 0xcc, sizeof (buffer)); |
118 | memset (&me, 0xcc, sizeof (me)); |
119 | struct mntent *pme = getmntent_r (stream: fp, result: &me, buffer: buffer, bufsize: sizeof (buffer)); |
120 | TEST_VERIFY_EXIT (pme != NULL); |
121 | TEST_VERIFY (pme == &me); |
122 | TEST_COMPARE_STRING (test_cases[i].expected.mnt_fsname, me.mnt_fsname); |
123 | TEST_COMPARE_STRING (test_cases[i].expected.mnt_dir, me.mnt_dir); |
124 | TEST_COMPARE_STRING (test_cases[i].expected.mnt_type, me.mnt_type); |
125 | TEST_COMPARE_STRING (test_cases[i].expected.mnt_opts, me.mnt_opts); |
126 | TEST_COMPARE (test_cases[i].expected.mnt_freq, me.mnt_freq); |
127 | TEST_COMPARE (test_cases[i].expected.mnt_passno, me.mnt_passno); |
128 | } |
129 | |
130 | TEST_VERIFY (getmntent_r (fp, &me, buffer, sizeof (buffer)) == NULL); |
131 | |
132 | TEST_COMPARE (feof (fp), 1); |
133 | TEST_COMPARE (ferror (fp), 0); |
134 | errno = 0; |
135 | TEST_COMPARE (endmntent (fp), 1); |
136 | TEST_COMPARE (errno, 0); |
137 | free (ptr: path); |
138 | return 0; |
139 | } |
140 | |
141 | #include <support/test-driver.c> |
142 | |