1 /******************************************************************************
2 * sync_file_range01.c
3 * Copyright (c) International Business Machines Corp., 2008
4 * Email: bnpoorni@in.ibm.com
5 *****************************************************************************/
6
7 /******************************************************************************/
8 /* */
9 /* This program is free software; you can redistribute it and/or modify */
10 /* it under the terms of the GNU General Public License as published by */
11 /* the Free Software Foundation; either version 2 of the License, or */
12 /* (at your option) any later version. */
13 /* */
14 /* This program is distributed in the hope that it will be useful, */
15 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
16 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See */
17 /* the GNU General Public License for more details. */
18 /* */
19 /* You should have received a copy of the GNU General Public License */
20 /* along with this program; if not, write to the Free Software */
21 /* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */
22 /* */
23 /******************************************************************************/
24
25 /*****************************************************************************
26 * TEST IDENTIFIER : sync_file_range01 $
27 * $
28 * EXECUTED BY : anyone $
29 *
30 * TEST TITLE : Checks for Errors from sync_file_range()
31 *
32 * TEST CASE TOTAL : 5
33 *
34 * CPU ARCHITECTURES : All
35 *
36 * AUTHOR : B N Poornima
37 *
38 * DATE STARTED : 21/07/2008
39 *
40 * TEST CASES
41 * (Tests sync_file_range() for different test cases as reported in the man
42 * page)
43 *
44 * INPUT SPECIFICATIONS
45 * No input needs to be specified
46 * sync_file_data() in-puts are specified through test_data
47 *
48 * OUTPUT SPECIFICATIONS
49 * sync_file_data() error message matches with the expected error
50 * message.
51 *
52 * ENVIRONMENTAL NEEDS
53 * Kernel version 2.6.17 and above
54 * Kernel version 2.6.22 and above in case of PPC and PPC64
55 *
56 * SPECIAL PROCEDURAL REQUIREMENTS
57 * None
58 *
59 * DETAILED DESCRIPTION
60 * This is a test case for sync_file_range() system call.
61 * This test suite tests various error messages from the system call
62 * If the error message received matches with the expected
63 * test is considered passed else test fails
64 *
65 * Total 5 Test Cases :-
66 * Various error messages from the man page
67 *
68 * Setup:
69 * Setup files on which sync_file_range is to be called
70 *
71 * Test:
72 * Loop if the proper options are given.
73 * Execute system call
74 * Check return code.
75 * If error obtained matches with the expected error
76 * PASS the test, otherwise TEST FAILS
77 *
78 * Cleanup:
79 * Cleanup the temporary folder
80 *
81 ******************************************************************************/
82 #define _GNU_SOURCE
83
84 #include <sys/types.h>
85 #include <sys/stat.h>
86 #include <sys/utsname.h>
87 #include <endian.h>
88 #include <errno.h>
89 #include <fcntl.h>
90 #include <stdio.h>
91 #include <stdlib.h>
92 #include <unistd.h>
93
94 #include "test.h"
95 #include "lapi/sync_file_range.h"
96 #include "check_sync_file_range.h"
97
98 #ifndef SYNC_FILE_RANGE_WAIT_BEFORE
99 #define SYNC_FILE_RANGE_WAIT_BEFORE 1
100 #define SYNC_FILE_RANGE_WRITE 2 //DUMMY VALUES
101 #define SYNC_FILE_RANGE_WAIT_AFTER 4
102 #endif
103
104 #define SYNC_FILE_RANGE_INVALID 8
105
106 char *TCID = "sync_file_range01";
107 char filename[255]; /* file used for testing */
108 char spl_file[] = "/dev/null";
109 int filed, sfd; /* normal and special fds */
110 int bfd = -1; /* Bad file descriptor */
111
112 struct test_data_t {
113 int *fd;
114 off64_t offset;
115 off64_t nbytes;
116 unsigned int flags;
117 int error;
118 } test_data[] = {
119 {
120 &bfd, 0, 1, SYNC_FILE_RANGE_WRITE, EBADF}, {
121 &sfd, 0, 1, SYNC_FILE_RANGE_WAIT_AFTER, ESPIPE}, {
122 &filed, -1, 1, SYNC_FILE_RANGE_WAIT_BEFORE, EINVAL}, {
123 &filed, 0, -1, SYNC_FILE_RANGE_WRITE, EINVAL}, {
124 &filed, 0, 1, SYNC_FILE_RANGE_INVALID, EINVAL}
125 };
126
127 int TST_TOTAL = sizeof(test_data) / sizeof(test_data[0]);
128
129 /* Extern Global Functions */
130 /******************************************************************************/
131 /* */
132 /* Function: cleanup */
133 /* */
134 /* Description: Performs all one time clean up for this test on successful */
135 /* completion, premature exit or failure. Closes all temporary */
136 /* files, removes all temporary directories exits the test with */
137 /* appropriate return code by calling tst_exit() function. */
138 /* */
139 /* Input: None. */
140 /* */
141 /* Output: None. */
142 /* */
143 /* Return: On failure - Exits calling tst_exit(). Non '0' return code. */
144 /* On success - Exits calling tst_exit(). With '0' return code. */
145 /* */
146 /******************************************************************************/
cleanup(void)147 void cleanup(void)
148 {
149
150 /* close the file we have open */
151 if (close(filed) == -1) {
152 tst_resm(TWARN | TERRNO, "close(%s) failed", filename);
153 }
154
155 tst_rmdir();
156 }
157
158 /* Local Functions */
159 /******************************************************************************/
160 /* */
161 /* Function: setup */
162 /* */
163 /* Description: Performs all one time setup for this test. This function is */
164 /* typically used to capture signals, create temporary dirs */
165 /* and temporary files that may be used in the course of this */
166 /* test. */
167 /* */
168 /* Input: None. */
169 /* */
170 /* Output: None. */
171 /* */
172 /* Return: On failure - Exits by calling cleanup(). */
173 /* On success - returns 0. */
174 /* */
175 /******************************************************************************/
setup(void)176 void setup(void)
177 {
178
179 tst_sig(NOFORK, DEF_HANDLER, cleanup);
180
181 TEST_PAUSE;
182
183 tst_tmpdir();
184
185 sprintf(filename, "tmpfile_%d", getpid());
186 if ((filed = open(filename, O_RDWR | O_CREAT, 0700)) == -1) {
187 tst_brkm(TBROK | TERRNO, cleanup,
188 "open(%s, O_RDWR|O_CREAT,0700) failed", filename);
189 }
190
191 sfd = open(spl_file, O_RDWR | O_CREAT, 0700);
192 }
193
194 /******************************************************************************/
195 /* */
196 /* Function: main */
197 /* */
198 /* Description: Entry point to this test-case. It parses all the command line */
199 /* inputs, calls the global setup and executes the test. It logs */
200 /* the test status and results appropriately using the LTP API's */
201 /* On successful completion or premature failure, cleanup() func */
202 /* is called and test exits with an appropriate return code. */
203 /* */
204 /* Input: Describe input arguments to this test-case */
205 /* -l - Number of iteration */
206 /* -v - Prints verbose output */
207 /* -V - Prints the version number */
208 /* */
209 /* Exit: On failure - Exits by calling cleanup(). */
210 /* On success - exits with 0 exit value. */
211 /* */
212 /******************************************************************************/
main(int ac,char ** av)213 int main(int ac, char **av)
214 {
215
216 int test_index = 0;
217
218 tst_parse_opts(ac, av, NULL, NULL);
219
220 if (!check_sync_file_range())
221 tst_brkm(TCONF, NULL, "sync_file_range() not supported");
222
223 setup();
224
225 for (test_index = 0; test_index < TST_TOTAL; test_index++) {
226 TEST(sync_file_range
227 (*(test_data[test_index].fd),
228 test_data[test_index].offset,
229 test_data[test_index].nbytes,
230 test_data[test_index].flags));
231
232 if (TEST_RETURN != -1) {
233 tst_resm(TFAIL,
234 "call succeeded unexpectedly (%ld != -1)",
235 TEST_RETURN);
236 continue;
237 }
238
239 if (TEST_ERRNO == test_data[test_index].error) {
240 tst_resm(TPASS | TTERRNO, "got expected error");
241 } else {
242 tst_resm(TFAIL | TTERRNO, "got unexpected error; "
243 "expected %d", test_data[test_index].error);
244 }
245
246 }
247
248 cleanup();
249 tst_exit();
250 }
251