Debian Codegraph @

test/nfstest_posix

raw
1 #!/usr/bin/env python3
2 #===============================================================================
3 # Copyright 2012 NetApp, Inc. All Rights Reserved,
4 # contribution by Jorge Mora <[email protected]>
5 #
6 # This program is free software; you can redistribute it and/or modify it under
7 # the terms of the GNU General Public License as published by the Free Software
8 # Foundation; either version 2 of the License, or (at your option) any later
9 # version.
10 #
11 # This program is distributed in the hope that it will be useful, but WITHOUT
12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 #===============================================================================
15 import os
16 import mmap
17 import stat
18 import fcntl
19 import errno
20 import posix
21 import ctypes
22 import random
23 import itertools
24 import traceback
25 from time import sleep
26 import nfstest_config as c
27 from nfstest.test_util import TestUtil
28 import packet.nfs.nfs3_const as nfs3_const
29 import packet.nfs.nfs4_const as nfs4_const
30
31 # Module constants
32 __author__ = "Jorge Mora (%s)" % c.NFSTEST_AUTHOR_EMAIL
33 __copyright__ = "Copyright (C) 2012 NetApp, Inc."
34 __license__ = "GPL v2"
35 __version__ = "1.2"
36
37 USAGE = """%prog --server <server> [options]
38
39 POSIX file system level access tests
40 ====================================
41 Verify POSIX file system level access over the specified path using
42 positive and negative testing.
43
44 Valid for any version of NFS.
45
46 Examples:
47 The only required option is --server
48 $ %prog --server 192.168.0.11
49
50 Notes:
51 The user id in the local host must have access to run commands as root
52 using the 'sudo' command without the need for a password."""
53
54 # Test script ID
55 SCRIPT_ID = "POSIX"
56
57 TESTNAMES = [
58 'access',
59 'chdir',
60 'close',
61 'closedir',
62 'creat',
63 'fcntl',
64 'fdatasync',
65 'fstat',
66 'fstatvfs',
67 'fsync',
68 'link',
69 'lseek',
70 'lstat',
71 'mkdir',
72 'mmap',
73 'munmap',
74 'opendir',
75 'read',
76 'readdir',
77 'readlink',
78 'rename',
79 'rewinddir',
80 'rmdir',
81 'seekdir',
82 'stat',
83 'statvfs',
84 'symlink',
85 'sync',
86 'telldir',
87 'unlink',
88 'write',
89 'open',
90 'chmod',
91 ]
92
93 stat_map = {
94 1: 'stat',
95 2: 'lstat',
96 3: 'fstat',
97 }
98
99 access_names = {
100 posix.F_OK: 'F_OK',
101 posix.R_OK: 'R_OK',
102 posix.W_OK: 'W_OK',
103 posix.X_OK: 'X_OK',
104 }
105
106 def access_str(mode):
107 """Convert the access mode bitmap to its string representation."""
108 access_list = []
109 for perm in access_names:
110 if perm & mode != 0:
111 access_list.append(access_names[perm])
112 if len(access_list) == 0:
113 access_list.append(access_names[0])
114 return '|'.join(access_list)
115
116 class DirEnt(ctypes.Structure):
117 """
118 struct dirent {
119 ino_t d_ino; /* inode number */
120 off_t d_off; /* offset to the next dirent */
121 unsigned short d_reclen; /* length of this record */
122 unsigned char d_type; /* type of file; not supported
123 by all file system types */
124 char d_name[256]; /* filename */
125 };
126 """
127 _fields_ = [
128 ("d_ino", ctypes.c_ulong),
129 ("d_off", ctypes.c_ulong),
130 ("d_reclen", ctypes.c_ushort),
131 ("d_type", ctypes.c_char),
132 ("d_name", ctypes.c_char*256),
133 ]
134
135 class Flock(ctypes.Structure):
136 """
137 struct flock {
138 short l_type; /* Type of lock: F_RDLCK,
139 F_WRLCK, F_UNLCK */
140 short l_whence; /* How to interpret l_start:
141 SEEK_SET, SEEK_CUR, SEEK_END */
142 off_t l_start; /* Starting offset for lock */
143 off_t l_len; /* Number of bytes to lock */
144 pid_t l_pid; /* PID of process blocking our lock
145 (F_GETLK only) */
146
147 };
148 """
149 _fields_ = [
150 ("l_type", ctypes.c_short),
151 ("l_whence", ctypes.c_short),
152 ("l_start", ctypes.c_ulong),
153 ("l_len", ctypes.c_ulong),
154 ("l_pid", ctypes.c_int),
155 ]
156
157 # OPEN flags
158 access_flag_list = [
159 posix.O_RDONLY,
160 posix.O_WRONLY,
161 posix.O_RDWR,
162 ]
163 open_flag_list = [
164 posix.O_RDONLY,
165 posix.O_WRONLY,
166 posix.O_RDWR,
167 posix.O_CREAT,
168 posix.O_EXCL,
169 posix.O_NOCTTY,
170 posix.O_TRUNC,
171 posix.O_APPEND,
172 posix.O_ASYNC,
173 # Linux-specific flags
174 posix.O_DIRECTORY,
175 posix.O_NOATIME,
176 posix.O_NOFOLLOW,
177 ]
178 open_flag_map = {
179 posix.O_RDONLY: 'O_RDONLY',
180 posix.O_WRONLY: 'O_WRONLY',
181 posix.O_RDWR: 'O_RDWR',
182 posix.O_CREAT: 'O_CREAT',
183 posix.O_EXCL: 'O_EXCL',
184 posix.O_NOCTTY: 'O_NOCTTY',
185 posix.O_TRUNC: 'O_TRUNC',
186 posix.O_APPEND: 'O_APPEND',
187 posix.O_ASYNC: 'O_ASYNC',
188 # Linux-specific flags
189 posix.O_DIRECTORY: 'O_DIRECTORY',
190 posix.O_NOATIME: 'O_NOATIME',
191 posix.O_NOFOLLOW: 'O_NOFOLLOW',
192 }
193
194 perm_map = {
195 0o0001: 'XOTH',
196 0o0002: 'WOTH',
197 0o0004: 'ROTH',
198 0o0010: 'XGRP',
199 0o0020: 'WGRP',
200 0o0040: 'RGRP',
201 0o0100: 'XUSR',
202 0o0200: 'WUSR',
203 0o0400: 'RUSR',
204 0o1000: 'SVTX',
205 0o2000: 'SGID',
206 0o4000: 'SUID',
207 }
208
209 def oflag_str(flags):
210 """Convert the open flags bitmap to its string representation."""
211 flist = []
212 flag_list = list(flags)
213 if 0 in access_flag_list:
214 # Flag with no bits set is in the access list
215 found = False
216 for flag in access_flag_list:
217 if flag in flags:
218 # At least one access flag is in flags
219 found = True
220 break
221 if not found:
222 flag_list = [0] + flag_list
223 for flag in flag_list:
224 flist.append(open_flag_map[flag])
225 return '|'.join(flist)
226
227 class PosixTest(TestUtil):
228 """PosixTest object
229
230 PosixTest() -> New test object
231
232 Usage:
233 x = PosixTest(testnames=['access', 'chdir', 'creat', ...])
234
235 # Run all the tests
236 x.run_tests()
237 x.exit()
238 """
239 def __init__(self, **kwargs):
240 """Constructor
241
242 Initialize object's private data.
243 """
244 TestUtil.__init__(self, **kwargs)
245 self.opts.version = "%prog " + __version__
246 self.scan_options()
247
248 # Function prototypes for system calls in libc
249 self.libc.opendir.restype = ctypes.c_void_p
250 self.libc.closedir.argtypes= ctypes.c_void_p,
251 self.libc.rewinddir.argtypes = ctypes.c_void_p,
252 self.libc.readdir.argtypes = ctypes.c_void_p,
253 self.libc.readdir.restype = ctypes.POINTER(DirEnt)
254 self.libc.telldir.argtypes = ctypes.c_void_p,
255 self.libc.telldir.restype = ctypes.c_long
256 self.libc.seekdir.argtypes = ctypes.c_void_p, ctypes.c_long
257 self.libc.fcntl.restype = ctypes.c_int
258 self.libc.mmap.argtypes = ctypes.c_void_p, ctypes.c_ulong, ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_long
259 self.libc.mmap.restype = ctypes.c_long # Use c_long to check for errors
260 self.libc.munmap.argtypes = ctypes.c_void_p, ctypes.c_ulong
261 self.libc.munmap.restype = ctypes.c_int
262
263 # Make sure actimeo is set
264 if 'actimeo' not in self.mtopts:
265 self.mtopts += ",actimeo=0"
266
267 # Clear umask
268 os.umask(0)
269
270 def setup(self, **kwargs):
271 """Setup test environment"""
272 self.umount()
273 x.trace_start()
274 self.mount()
275 super(PosixTest, self).setup(**kwargs)
276
277 self.setup_readlink = {}
278 self.setup_readlink_types = ("file", "directory", "non-existent file")
279 if "readlink" in self.testlist:
280 # Setup specific to "readlink" test
281 for stype in self.setup_readlink_types:
282 if stype == "file":
283 self.create_file()
284 srcpath = self.absfile
285 elif stype == "directory":
286 self.create_dir()
287 srcpath = self.absdir
288 else:
289 self.get_filename()
290 srcpath = self.absfile
291 self.get_filename()
292 self.dprint('DBG3', "Creating symbolic link to %s [%s -> %s]" % (stype, self.absfile, srcpath))
293 os.symlink(srcpath, self.absfile)
294 self.setup_readlink[stype] = (srcpath, self.absfile)
295 x.umount()
296 x.mount()
297 x.trace_stop()
298
299 def access(self, path, mode, test, msg=""):
300 """Test file access.
301
302 path:
303 File system object to get access from
304 mode:
305 Mode access to check
306 test:
307 Expected output from access()
308 msg:
309 Message to be appended to test message
310 """
311 not_str = "" if test else "not "
312 out = posix.access(path, mode)
313 self.dprint('DBG4', "access(%s) returns %s" % (access_str(mode), out))
314 self.test(out == test, "access - file access %sallowed with mode %s%s" % (not_str, access_str(mode), msg))
315
316 def access_test(self):
317 """Verify POSIX API access() on files with different modes."""
318 self.test_group("Verify POSIX API access() on %s" % self.nfsstr())
319 perms = [0o777, 0o666, 0o555, 0o333, 0o444, 0o222, 0o111, 0o000]
320 cperms = [0o7, 0o6, 0o5, 0o3]
321
322 try:
323 self.create_file()
324
325 isroot = False
326 if os.getuid() == 0:
327 fstat = os.stat(self.absfile)
328 if fstat.st_uid == 0:
329 isroot = True
330
331 self.access(self.absfile, posix.F_OK, True)
332 self.access(self.absfile+'bogus', posix.F_OK, False, " for a non-existent file")
333 for perm in perms:
334 msg = " for file with permissions %s" % oct(perm)
335 os.chmod(self.absfile, perm)
336 # When running as root and the file is created as root,
337 # access is granted for either R or W even when the file
338 # does not have R or W permission.
339 self.access(self.absfile, posix.R_OK, isroot or (perm&4)!=0, msg)
340 self.access(self.absfile, posix.W_OK, isroot or (perm&2)!=0, msg)
341 self.access(self.absfile, posix.X_OK, (perm&1)!=0, msg)
342 for mode in cperms:
343 if isroot:
344 # Asking for multiple access bits when running as root
345 # and the file is created as root, expect access unless
346 # asking for X and file does not have X permissions.
347 expr = (not mode&posix.X_OK or perm&posix.X_OK)
348 else:
349 # Running as a regular user
350 expr = (mode&perm == mode)
351 self.access(self.absfile, mode, expr, msg)
352 except Exception:
353 self.test(False, traceback.format_exc())
354
355 def chdir_test(self):
356 """Verify POSIX API chdir() by changing to a newly created directory
357 and then by changing back to the original directory.
358 """
359 self.test_group("Verify POSIX API chdir() on %s" % self.nfsstr())
360
361 try:
362 cwd_orig = os.getcwd()
363 self.create_dir()
364 self.dprint('DBG3', "Change to directory %s using POSIX API chdir()" % self.absdir)
365 amsg = "chdir - chdir() should succeed"
366 self.run_func(posix.chdir, self.absdir, msg=amsg)
367 cwd = os.getcwd()
368 self.test(cwd == self.absdir, "chdir - current working directory should be changed")
369 self.run_func(posix.chdir, cwd_orig, msg=amsg)
370 cwd = os.getcwd()
371 self.test(cwd == cwd_orig, "chdir - current working directory should be changed back to the original directory")
372 amsg = "chdir - changing to a non-existent directory should return an error"
373 self.run_func(posix.chdir, self.absdir+"_bogus", msg=amsg, err=errno.ENOENT)
374 self.test(cwd == os.getcwd(), "chdir - current working directory should not be changed")
375 except Exception:
376 self.test(False, traceback.format_exc())
377
378 def chmod_test(self):
379 """Verify POSIX API chmod() on a file and directory by trying all valid
380 combinations of modes. Verify that the st_ctime files is updated
381 for both the file and directory.
382 """
383 self.test_group("Verify POSIX API chmod() on %s" % self.nfsstr())
384
385 try:
386 for objtype in ['file', 'directory']:
387 if objtype == 'file':
388 self.create_file()
389 else:
390 self.create_dir()
391 self.absfile = self.absdir
392 chmod_count = 0
393 max_perm = 0o7777+1
394 bad_dict = {}
395 bit_list = []
396 for perm in range(max_perm):
397 bit_list.append(perm)
398 posix.chmod(self.absfile, perm)
399 fstat = os.stat(self.absfile)
400 mode = fstat.st_mode & 0o7777
401 if mode == perm:
402 chmod_count += 1
403 if self.tverbose != 1:
404 self.test(mode == perm, "chmod - changing %s permission mode to %05o should succeed" % (objtype, perm), failmsg=", got %05o from stat()" % mode)
405 if mode != perm:
406 item = perm^mode
407 if bad_dict.get(item) is None:
408 bad_dict[item] = 0
409 bad_dict[item] += 1
410 for item in bad_dict:
411 out = self.bitmap_str(item, bad_dict[item], perm_map, bit_list)
412 if out is None:
413 out = "%05o" % item
414 self.test(False, "chmod - %d %s permission mode changes failed when setting (%s)" % (bad_dict[item], objtype, out))
415
416 if chmod_count == max_perm:
417 msg = "chmod - %d %s permission mode changes succeeded" % (chmod_count, objtype)
418 if self.tverbose == 1:
419 self.test(True, msg)
420 else:
421 self.test_info(">>>>: " + msg)
422 fstat_b = os.stat(self.absfile)
423 sleep(1)
424 posix.chmod(self.absfile, 0o777)
425 fstat = os.stat(self.absfile)
426 self.test(fstat.st_ctime > fstat_b.st_ctime, "chmod - %s st_ctime should be updated" % objtype)
427 except Exception:
428 self.test(False, traceback.format_exc())
429
430 def close_test(self):
431 """Verify POSIX API close() works and that writing to a closed file
432 descriptor returns an error.
433 """
434 self.test_group("Verify POSIX API close() on %s" % self.nfsstr())
435
436 try:
437 fd = None
438 self.get_filename()
439 self.dprint('DBG3', "Open file %s for writing" % self.absfile)
440 fd = posix.open(self.absfile, posix.O_WRONLY|posix.O_CREAT)
441 amsg = "close - close() on write file should succeed"
442 self.run_func(posix.close, fd, msg=amsg)
443 amsg = "close - write after close should return an error"
444 self.run_func(posix.write, fd, self.data_pattern(0, 32), msg=amsg, err=errno.EBADF)
445
446 self.dprint('DBG3', "Open file %s for reading" % self.absfile)
447 fd = posix.open(self.absfile, posix.O_RDONLY)
448 amsg = "close - close() on read file should succeed"
449 self.run_func(posix.close, fd, msg=amsg)
450 amsg = "close - read after close should return an error"
451 self.run_func(posix.read, fd, 32, msg=amsg, err=errno.EBADF)
452 except Exception:
453 self.test(False, traceback.format_exc())
454 finally:
455 self.close_files(fd)
456
457 def closedir_test(self):
458 """Verify POSIX API closedir() works
459 """
460 self.test_group("Verify POSIX API closedir() on %s" % self.nfsstr())
461
462 try:
463 fd = None
464 self.create_dir()
465
466 fd = self.libc.opendir(self.absdir.encode())
467 out = self.libc.closedir(fd)
468 fd = None
469 self.test(out == 0, "closedir - closedir() should succeed")
470 except Exception:
471 self.test(False, traceback.format_exc())
472 finally:
473 if fd:
474 self.libc.closedir(fd)
475
476 def creat_test(self):
477 """Verify POSIX API creat(path, mode) is equivalent to
478 open(path, O_WRONLY|O_CREAT|O_TRUNC, mode). First test with a path
479 that does not exist to verify the file was created and then test
480 with a path that does exist to verify that the file is truncated.
481 """
482 self.test_group("Verify POSIX API creat() on %s" % self.nfsstr())
483
484 try:
485 fd = None
486 mode = 0o754
487 self.get_filename()
488 self.dprint('DBG3', "Create file %s using POSIX API creat()" % self.absfile)
489 fd = self.libc.creat(self.absfile.encode(), mode)
490 self.test(fd > 0, "creat - creat() should succeed")
491 count = posix.write(fd, self.data_pattern(0, self.filesize))
492 posix.close(fd)
493 self.test(os.path.exists(self.absfile), "creat - file should be created")
494 fstat = os.stat(self.absfile)
495 self.test(fstat.st_mode & 0o777 == mode, "creat - mode permissions of created file should be correct")
496 self.test(fstat.st_size == count, "creat - file size should be correct")
497
498 self.dprint('DBG3', "Open existing file %s using POSIX API creat()" % self.absfile)
499 fd = self.libc.creat(self.absfile.encode(), 0o744)
500 self.test(fd > 0, "creat - existent file open should succeed")
501 posix.close(fd)
502 fstat = os.stat(self.absfile)
503 self.test(os.path.exists(self.absfile), "creat - file should still exist")
504 self.test(fstat.st_mode & 0o777 == mode, "creat - mode permissions of opened file should not be changed")
505 self.test(fstat.st_size == 0, "creat - existent file should be truncated")
506 except Exception:
507 self.test(False, traceback.format_exc())
508 finally:
509 self.close_files(fd)
510
511 def _do_io(self, fd, dmsg, pattern=None):
512 """Wrapper to do I/O."""
513 self.dprint('DBG3', dmsg)
514 if self.posix_read:
515 return posix.read(fd, self.posix_iosize)
516 else:
517 count = posix.write(fd, self.data_pattern(self.posix_offset, self.posix_iosize, pattern=pattern))
518 self.posix_offset += count
519 return count
520
521 def _fcntl_test(self, objtype):
522 """Verify the POSIX API fcntl() commands F_DUPFD, F_GETFD, F_SETFD,
523 F_GETFL, F_SETFL, and FD_CLOEXEC. The F_DUPFD command is tested
524 by performing operations on the original and dupped file descriptor
525 to ensure they behave correctly. The F_GETFD and F_SETFD commands
526 are tested by setting the FD_CLOEXEC flag and making sure it gets set.
527 The F_GETFL and F_SETFL commands are tested by setting the O_APPEND
528 flag and making sure it gets set.
529 """
530 try:
531 fd = None
532 if objtype == 'read':
533 str_ing = 'reading'
534 str_ed = 'read'
535 str_lck = 'F_RDLCK'
536 mode = posix.O_RDONLY
537 lock_type = fcntl.F_RDLCK
538 self.posix_read = True
539 self.absfile = self.abspath(self.files[0])
540 else:
541 str_ing = 'writing'
542 str_ed = 'written'
543 str_lck = 'F_WRLCK'
544 mode = posix.O_WRONLY|posix.O_CREAT
545 lock_type = fcntl.F_WRLCK
546 self.posix_read = False
547 self.get_filename()
548
549 self.test_info("fcntl on %s file descriptor" % objtype)
550 self.dprint('DBG3', "Open file %s for %s" % (self.absfile, str_ing))
551 fd = posix.open(self.absfile, mode)
552 self.posix_offset = 0
553 self.posix_iosize = 64
554 test_pos = 32
555 nfd = 1000
556
557 for i in range(10):
558 self._do_io(fd, "%s file" % objtype.capitalize())
559
560 self.dprint('DBG3', "DUP file descriptor using POSIX API fcntl(fd, F_DUPFD, n)")
561 self.libc.fcntl.argtypes = [ctypes.c_int, ctypes.c_int, ctypes.c_int]
562 fd2 = self.libc.fcntl(fd, fcntl.F_DUPFD, nfd)
563 self.test(fd2 > 0, "fcntl - F_DUPFD should succeed")
564 self.test(fd2 >= nfd, "fcntl - DUP'ed file descriptor should be greater than or equal to the value given")
565
566 self.dprint('DBG3', "Reposition file pointer using DUP'ed file descriptor")
567 posix.lseek(fd2, test_pos, os.SEEK_SET)
568 pos = posix.lseek(fd, 0, os.SEEK_CUR)
569 self.test(pos == test_pos, "fcntl - repositioning file pointer using DUP'ed file descriptor should reposition it in the original file descriptor as well")
570 self.posix_offset = pos
571 out = self._do_io(fd, "%s file using original file descriptor" % objtype.capitalize(), pattern=b"-")
572 if self.posix_read:
573 data = self.data_pattern(test_pos, self.posix_iosize)
574 else:
575 data = self.data_pattern(test_pos, self.posix_iosize, pattern=b"-")
576 with open(self.absfile, "rb") as fdr:
577 fdr.seek(test_pos)
578 out = fdr.read(self.posix_iosize)
579 self.test(out == data, "fcntl - %s data is correct when repositioning file pointer using DUP'ed file descriptor" % str_ed)
580
581 fdflags = self.libc.fcntl(fd, fcntl.F_GETFD, 0)
582 self.test(fdflags >= 0, "fcntl - F_GETFD should succeed")
583 out = self.libc.fcntl(fd, fcntl.F_SETFD, fcntl.FD_CLOEXEC)
584 self.test(out != -1, "fcntl - F_SETFD should succeed")
585 fdflags = self.libc.fcntl(fd, fcntl.F_GETFD, 0)
586 self.test(fdflags & fcntl.FD_CLOEXEC == fcntl.FD_CLOEXEC, "fcntl - F_SETFD should set the given file descriptor flag correctly")
587 fdflags = self.libc.fcntl(fd2, fcntl.F_GETFD, 0)
588 self.test(fdflags & fcntl.FD_CLOEXEC != fcntl.FD_CLOEXEC, "fcnlt - F_SETFD should change the flag for the given file descriptor only")
589
590 flflags = self.libc.fcntl(fd, fcntl.F_GETFL, 0)
591 self.test(flflags >= 0, "fcntl - F_GETFL should succeed")
592 out = self.libc.fcntl(fd, fcntl.F_SETFL, os.O_APPEND)
593 self.test(out != -1, "fcntl - F_SETFL should succeed")
594 flflags = self.libc.fcntl(fd, fcntl.F_GETFL, 0)
595 self.test(flflags & os.O_APPEND == os.O_APPEND, "fcntl - F_SETFL should set the given file status flag correctly")
596 flflags = self.libc.fcntl(fd2, fcntl.F_GETFL, 0)
597 self.test(flflags & os.O_APPEND == os.O_APPEND, "fcnlt - F_SETFL should change the flag for all file descriptors on the same file")
598
599 if not self.posix_read:
600 self.dprint('DBG3', "Reposition file pointer using DUP'ed file descriptor")
601 posix.lseek(fd2, test_pos, os.SEEK_SET)
602 pos = posix.lseek(fd, 0, os.SEEK_CUR)
603 self.test(pos == test_pos, "fcntl - repositioning file pointer using DUP'ed file descriptor should reposition it in the original file descriptor as well")
604 self.posix_offset = pos
605 out = self._do_io(fd2, "%s file using DUP'ed file descriptor" % objtype.capitalize(), pattern=b"+")
606
607 data = self.data_pattern(test_pos, self.posix_iosize, pattern=b"+")
608 with open(self.absfile, "rb") as fdr:
609 fdr.seek(-self.posix_iosize, os.SEEK_END)
610 out = fdr.read(self.posix_iosize)
611 self.test(out == data, "fcntl - data is %s correctly at the end of the file when O_APPEND is set regardless where the file pointer is set" % str_ed)
612
613 posix.close(fd)
614
615 try:
616 werrno = 0
617 self._do_io(fd, "%s file using original file descriptor" % objtype.capitalize())
618 except OSError as e:
619 werrno = e.errno
620 self.test(werrno == errno.EBADF, "fcntl - %s after closing original file descriptor should return an error" % objtype)
621
622 flock = Flock(lock_type, 0, 0, int(self.filesize/2), 0)
623 self.libc.fcntl.argtypes = [ctypes.c_int, ctypes.c_int, ctypes.POINTER(Flock)]
624 out = self.libc.fcntl(fd2, fcntl.F_SETLKW, ctypes.byref(flock))
625 self.test(out != -1, "fcntl - F_SETLKW (%s) should succeed" % str_lck)
626
627 pos = posix.lseek(fd2, 0, os.SEEK_SET)
628 self.posix_offset = pos
629 out = self._do_io(fd2, "%s file using DUP'ed file descriptor" % objtype.capitalize())
630 if self.posix_read:
631 test_expr = out == self.data_pattern(0, self.posix_iosize)
632 else:
633 test_expr = out == self.posix_iosize
634 self.test(test_expr, "fcntl - %s on DUP'ed descriptor after closing original file descriptor should succeed" % objtype)
635
636 flock = Flock(fcntl.F_UNLCK, 0, 0, int(self.filesize/4), 0)
637 out = self.libc.fcntl(fd2, fcntl.F_SETLKW, ctypes.byref(flock))
638 self.test(out != -1, "fcntl - F_SETLKW (F_UNLCK) should succeed")
639
640 flock = Flock(lock_type, 0, int(self.filesize/2), self.filesize, 0)
641 out = self.libc.fcntl(fd2, fcntl.F_SETLK, ctypes.byref(flock))
642 self.test(out != -1, "fcntl - F_SETLK (%s) should succeed" % str_lck)
643
644 self._do_io(fd2, "%s file using DUP'ed file descriptor" % objtype.capitalize())
645
646 flock = Flock(fcntl.F_UNLCK, 0, int(self.filesize/2), int(self.filesize/4), 0)
647 out = self.libc.fcntl(fd2, fcntl.F_SETLK, ctypes.byref(flock))
648 self.test(out != -1, "fcntl - F_SETLK (F_UNLCK) should succeed")
649
650 posix.close(fd2)
651 except Exception:
652 self.test(False, traceback.format_exc())
653 finally:
654 self.close_files(fd, fd2)
655
656 def fcntl_test(self):
657 """Verify the POSIX API fcntl() commands F_DUPFD, F_GETFD, F_SETFD,
658 F_GETFL, F_SETFL, and FD_CLOEXEC. The F_DUPFD command is tested
659 by performing operations on the original and dupped file descriptor
660 to ensure they behave correctly. The F_GETFD and F_SETFD commands
661 are tested by setting the FD_CLOEXEC flag and making sure it gets set.
662 The F_GETFL and F_SETFL commands are tested by setting the O_APPEND
663 flag and making sure it gets set.
664 Run the test for both 'read' and 'write'.
665 """
666 self.test_group("Verify POSIX API fcntl() on %s" % self.nfsstr())
667
668 self._fcntl_test('read')
669 self._fcntl_test('write')
670
671 def fdatasync_test(self):
672 """Verify POSIX API fdatasync()."""
673 self._sync('fdatasync')
674
675 def fstat_test(self):
676 """Verify POSIX API fstat() by checking the mode on a file and that
677 it returns the expected structure members. Create a symlink and
678 verify that fstat returns information about the link.
679 """
680 self._stat_test(stat_mode=3)
681
682 def _statvfs_test(self, path=None, fd=None, objtype='file'):
683 """Verify POSIX API statvfs() or fstatvfs() by making sure
684 all the members of the structure are returned.
685 Options path and fd are mutually exclusive.
686
687 path:
688 Verify statvfs()
689 fd:
690 Verify fstatvfs()
691 objtype:
692 Object type 'file' or 'directory' [default: 'file']
693 """
694 if path != None:
695 op_str = 'statvfs'
696 self.dprint('DBG3', "statvfs %s [%s]" % (objtype, path))
697 amsg = "statvfs - statvfs() should succeed"
698 stv = self.run_func(posix.statvfs, path, msg=amsg)
699 elif fd != None:
700 op_str = 'fstatvfs'
701 self.dprint('DBG3', "fstatvfs %s (%d)" % (objtype, fd))
702 amsg = "fstatvfs - fstatvfs() should succeed"
703 stv = self.run_func(posix.fstatvfs, fd, msg=amsg)
704
705 self.test(stv.f_bsize != None, "%s - f_bsize should be returned for %s" % (op_str, objtype))
706 self.test(stv.f_frsize != None, "%s - f_frsize should be returned for %s" % (op_str, objtype))
707 self.test(stv.f_blocks != None, "%s - f_blocks should be returned for %s" % (op_str, objtype))
708 self.test(stv.f_bfree != None, "%s - f_bfree should be returned for %s" % (op_str, objtype))
709 self.test(stv.f_bavail != None, "%s - f_bavail should be returned for %s" % (op_str, objtype))
710 self.test(stv.f_files != None, "%s - f_files should be returned for %s" % (op_str, objtype))
711 self.test(stv.f_ffree != None, "%s - f_ffree should be returned for %s" % (op_str, objtype))
712 self.test(stv.f_favail != None, "%s - f_favail should be returned for %s" % (op_str, objtype))
713 self.test(stv.f_flag != None, "%s - f_flag should be returned for %s" % (op_str, objtype))
714 self.test(stv.f_namemax != None, "%s - f_namemax should be returned for %s" % (op_str, objtype))
715
716 def fstatvfs_test(self):
717 """Verify POSIX API fstatvfs() by making sure all the members of the
718 structure are returned.
719 """
720 self.test_group("Verify POSIX API fstatvfs() on %s" % self.nfsstr())
721
722 try:
723 fd = None
724 self.create_file()
725 self.dprint('DBG3', "Open file %s for reading" % self.absfile)
726 fd = posix.open(self.absfile, posix.O_RDONLY)
727 self._statvfs_test(fd=fd)
728 posix.close(fd)
729
730 self.create_dir()
731 self.dprint('DBG3', "Open directory %s" % self.absdir)
732 fd = posix.open(self.absdir, posix.O_RDONLY|posix.O_NONBLOCK|posix.O_DIRECTORY)
733 self._statvfs_test(fd=fd, objtype='directory')
734 posix.close(fd)
735 except Exception:
736 self.test(False, traceback.format_exc())
737 finally:
738 self.close_files(fd)
739
740 def fsync_test(self):
741 """Verify POSIX API fsync()."""
742 self._sync('fsync')
743
744 def link_test(self):
745 """Verify POSIX API link(src, dst) creates a link and updates
746 st_ctime field for the file. Verify that link updates the
747 st_ctime and st_mtime for the directory. Verify st_link count
748 incremented by 1 for the file.
749 """
750 self.test_group("Verify POSIX API link() on %s" % self.nfsstr())
751
752 try:
753 # Get info of source file
754 srcfile = self.abspath(self.files[0])
755 fstat_b = os.stat(srcfile)
756 dstat_b = os.stat(self.mtdir)
757 sleep(1.1)
758
759 self.get_filename()
760 self.dprint('DBG3', "Create link %s -> %s using POSIX API link()" % (self.absfile, srcfile))
761 amsg = "link - link() should succeed"
762 self.run_func(posix.link, srcfile, self.absfile, msg=amsg)
763 lstat = os.stat(self.absfile)
764 fstat = os.stat(srcfile)
765 dstat = os.stat(self.mtdir)
766 self.test(lstat != None, "link - link should be created")
767 self.test(fstat.st_nlink == fstat_b.st_nlink+1, "link - file st_nlink should be incremented by 1")
768 self.test(fstat.st_ctime > fstat_b.st_ctime, "link - file st_ctime should be updated")
769 self.test(dstat.st_ctime > dstat_b.st_ctime, "link - parent directory st_ctime should be updated")
770 self.test(dstat.st_mtime > dstat_b.st_mtime, "link - parent directory st_mtime should be updated")
771 except Exception:
772 self.test(False, traceback.format_exc())
773
774 def lseek_test(self):
775 """Verify POSIX API lseek() with different offsets and whence
776 values including seeking past the end of the file.
777 """
778 self.test_group("Verify POSIX API lseek() on %s" % self.nfsstr())
779
780 try:
781 fd = None
782 data = b"AAAAAAAAAAAAAAAAAAAA"
783 dataB = b'BBBB'
784 dataC = b'CCCCCC'
785 dataD = b'DDDDDDDD'
786 self.get_filename()
787 self.dprint('DBG3', "Open file %s for writing" % self.absfile)
788 fd = posix.open(self.absfile, posix.O_WRONLY|posix.O_CREAT)
789 self.dprint('DBG3', "Write file [%s]" % data)
790 count = posix.write(fd, data)
791 self.test(count == len(data), "lseek - write should succeed at offset=0")
792 self.dprint('DBG3', "Set file pointer lseek(4, SEEK_SET)")
793 offset = self.run_func(posix.lseek, fd, 4, os.SEEK_SET)
794 self.test(offset == 4, "lseek - offset: 4, whence: SEEK_SET succeeds")
795 self.dprint('DBG3', "Write file [%s]" % dataB)
796 count = posix.write(fd, dataB)
797 self.dprint('DBG3', "Set file pointer lseek(8, SEEK_CUR)")
798 offset = self.run_func(posix.lseek, fd, 8, os.SEEK_CUR)
799 self.test(offset == 16, "lseek - offset: 8, whence: SEEK_CUR succeeds")
800 self.dprint('DBG3', "Write file [%s]" % dataC)
801 count = posix.write(fd, dataC)
802 self.dprint('DBG3', "Set file pointer lseek(1000, SEEK_END)")
803 offset = self.run_func(posix.lseek, fd, 1000, os.SEEK_END)
804 self.test(offset == 1022, "lseek - offset: 1000, whence: SEEK_END succeeds")
805 self.dprint('DBG3', "Write file [%s]" % dataD)
806 count = posix.write(fd, dataD)
807 if self.nfs_version > 2:
808 N = 2*1024*1024*1024
809 self.dprint('DBG3', "Set file pointer lseek(2GB, SEEK_SET)")
810 offset = self.run_func(posix.lseek, fd, N, os.SEEK_SET)
811 self.test(offset == N, "lseek - offset: 2GB, whence: SEEK_SET succeeds")
812 self.dprint('DBG3', "Set file pointer lseek(2GB-1, SEEK_CUR)")
813 offset = self.run_func(posix.lseek, fd, N-1, os.SEEK_CUR)
814 self.test(offset == 2*N-1, "lseek - offset: 4GB-1, whence: SEEK_CUR succeeds")
815 self.dprint('DBG3', "Set file pointer lseek(2GB, SEEK_END)")
816 offset = self.run_func(posix.lseek, fd, N, os.SEEK_END)
817 self.test(offset == N+1030, "lseek - offset: 2GB+22, whence: SEEK_END succeeds")
818 posix.close(fd)
819
820 self.dprint('DBG3', "Open file %s for reading" % self.absfile)
821 fd = posix.open(self.absfile, posix.O_RDONLY)
822 self.dprint('DBG3', "Set file pointer lseek(4, SEEK_SET)")
823 offset = self.run_func(posix.lseek, fd, 4, os.SEEK_SET)
824 self.dprint('DBG3', "Read 4 bytes")
825 data = posix.read(fd, 4)
826 self.test(data == dataB, "lseek - read data at offset = 4 should be correct")
827 self.dprint('DBG3', "Set file pointer lseek(16, SEEK_SET)")
828 offset = self.run_func(posix.lseek, fd, 16, os.SEEK_SET)
829 self.dprint('DBG3', "Read 6 bytes")
830 data = posix.read(fd, 6)
831 self.test(data == dataC, "lseek - read data at offset = 16 should be correct")
832 self.dprint('DBG3', "Set file pointer lseek(1022, SEEK_SET)")
833 offset = self.run_func(posix.lseek, fd, 1022, os.SEEK_SET)
834 self.dprint('DBG3', "Read 8 bytes")
835 data = posix.read(fd, 8)
836 self.test(data == dataD, "lseek - read data at offset = 1022 should be correct")
837 posix.close(fd)
838 except Exception:
839 self.test(False, traceback.format_exc())
840 finally:
841 self.close_files(fd)
842
843 def _stat_obj(self, mode, stat_mode=1, type='file', srcfile=None, srctype='file'):
844 """Verify POSIX API stat()/lstat()/fstat() by checking the mode on a file and
845 that it returns the expected structure members. Create a symlink and
846 verify that stat()/lstat()/fstat() returns information about the file/link.
847
848 mode:
849 Expected permission mode
850 stat_mode:
851 Stat function to use (1: 'stat', 2: 'lstat', 3: 'fstat')
852 type:
853 Type of file system object under test [default: 'file']
854 srcfile:
855 Source file of symbolic link [default: None]
856 srctype:
857 Type of file system object for source of symbolic link [default: 'file']
858 """
859 op_str = stat_map[stat_mode]
860 reg = 'regular ' if type == 'file' else ''
861 size = self.filesize
862 nlink = 1
863 if type == 'file' or (srcfile != None and srctype == 'file'):
864 is_func = stat.S_ISREG
865 elif type == 'directory' or (srcfile != None and srctype == 'directory'):
866 is_func = stat.S_ISDIR
867 nlink = 2
868 stv = os.statvfs(self.absfile)
869 size = stv.f_bsize
870
871 if stat_mode == 1:
872 self.dprint('DBG3', "stat %s [%s]" % (type, self.absfile))
873 fstat = self.run_func(posix.stat, self.absfile)
874 elif stat_mode == 2:
875 self.dprint('DBG3', "lstat %s [%s]" % (type, self.absfile))
876 fstat = self.run_func(posix.lstat, self.absfile)
877 if srcfile != None:
878 mode = 0o777
879 size = len(srcfile)
880 is_func = stat.S_ISLNK
881 nlink = 1
882 else:
883 try:
884 fd = None
885 self.dprint('DBG3', "fstat %s [%s]" % (type, self.absfile))
886 if type == 'directory':
887 self.dprint('DBG3', "Open directory %s" % self.absfile)
888 fd = posix.open(self.absfile, posix.O_RDONLY|posix.O_NONBLOCK|posix.O_DIRECTORY)
889 else:
890 self.dprint('DBG3', "Open file %s for reading" % self.absfile)
891 fd = posix.open(self.absfile, posix.O_RDONLY)
892 fstat = self.run_func(posix.fstat, fd)
893 posix.close(fd)
894 finally:
895 self.close_files(fd)
896
897 if type != "symbolic link":
898 self.test(fstat.st_mode & 0o777 == mode, "%s - %s permissions should be correct" % (op_str, type), failmsg=", expecting %05o and got %05o" % (mode, fstat.st_mode & 0o777))
899 self.test(is_func(fstat.st_mode), "%s - object type should be a %s%s" % (op_str, reg, type))
900 self.test(fstat.st_nlink == nlink, "%s - %s st_nlink should be equal to %d" % (op_str, type, nlink), failmsg=", expecting %d and got %d" % (nlink, fstat.st_nlink))
901 self.test(fstat.st_uid == os.getuid(), "%s - %s st_uid should be correct" % (op_str, type), failmsg=", expecting %d and got %d" % (os.getuid(), fstat.st_uid))
902 self.test(fstat.st_gid == os.getgid(), "%s - %s st_gid should be correct" % (op_str, type), failmsg=", expecting %d and got %d" % (os.getgid(), fstat.st_gid))
903 expr = (fstat.st_size == size) if type == 'file' else True
904 self.test(expr, "%s - %s st_size should be correct" % (op_str, type), failmsg=", expecting %d and got %d" % (size, fstat.st_size))
905 self.test(fstat.st_ino != None, "%s - %s st_ino should be returned" % (op_str, type))
906 self.test(fstat.st_dev != None, "%s - %s st_dev should be returned" % (op_str, type))
907 self.test(fstat.st_atime != None, "%s - %s st_atime should be returned" % (op_str, type))
908 self.test(fstat.st_mtime != None, "%s - %s st_mtime should be returned" % (op_str, type))
909 self.test(fstat.st_ctime != None, "%s - %s st_ctime should be returned" % (op_str, type))
910
911 def _stat_test(self, stat_mode=1):
912 """Verify POSIX API stat()/lstat()/fstat() by checking the mode on a file and
913 that it returns the expected structure members. Create a symlink and
914 verify that stat()/lstat()/fstat() returns information about the file/link.
915
916 Test a regular file, directory, symbolic link to a file,
917 and symbolic link to a directory.
918 """
919 op_str = stat_map[stat_mode]
920 self.test_group("Verify POSIX API %s() on %s" % (op_str, self.nfsstr()))
921
922 try:
923 self.test_info("Regular file")
924 self.create_file(mode=0o754)
925 self._stat_obj(0o754, stat_mode=stat_mode, type='file')
926 testfile = self.absfile
927
928 self.test_info("Directory")
929 self.create_dir(mode=0o755)
930 self.absfile = self.absdir
931 self._stat_obj(0o755, stat_mode=stat_mode, type='directory')
932 testdir = self.absdir
933
934 self.test_info("Symbolic link to a file")
935 srcfile = testfile
936 self.get_filename()
937 self.dprint('DBG3', "Creating symbolic link to file [%s -> %s]" % (self.absfile, srcfile))
938 os.symlink(srcfile, self.absfile)
939 self._stat_obj(0o754, stat_mode=stat_mode, type='symbolic link', srcfile=srcfile)
940
941 self.test_info("Symbolic link to a directory")
942 srcfile = testdir
943 self.get_filename()
944 self.dprint('DBG3', "Creating symbolic link to directory [%s -> %s]" % (self.absfile, srcfile))
945 os.symlink(srcfile, self.absfile)
946 self._stat_obj(0o755, stat_mode=stat_mode, type='symbolic link', srcfile=srcfile, srctype='directory')
947 except Exception:
948 self.test(False, traceback.format_exc())
949
950 def lstat_test(self):
951 """Verify POSIX API lstat() by checking the mode on a file and that
952 it returns the expected structure members. Create a symlink and
953 verify that lstat returns information about the link.
954 """
955 self._stat_test(stat_mode=2)
956
957 def mkdir_test(self):
958 """Verify POSIX API mkdir(). Verify that mkdir with a path of a symbolic
959 link fails. Verify that the st_ctime and st_mtime fields of the
960 parent directory are updated.
961 """
962 self.test_group("Verify POSIX API mkdir() on %s" % self.nfsstr())
963
964 try:
965 topdir = self.create_dir()
966 abstopdir = self.absdir
967 dstat_b = os.stat(abstopdir)
968 sleep(1)
969
970 mode = 0o754
971 self.get_dirname(dir=topdir)
972 self.dprint('DBG3', "Creating directory [%s]" % self.absdir)
973 amsg = "mkdir - mkdir() should succeed"
974 self.run_func(posix.mkdir, self.absdir, mode, msg=amsg)
975 dstat = os.stat(self.absdir)
976 self.test(os.path.exists(self.absdir), "mkdir - directory should be created")
977 self.test(dstat.st_mode & 0o777 == mode, "mkdir - mode permissions of created directory should be correct")
978
979 dstat = os.stat(abstopdir)
980 self.test(dstat.st_ctime > dstat_b.st_ctime, "mkdir - parent directory st_ctime should be updated")
981 self.test(dstat.st_mtime > dstat_b.st_mtime, "mkdir - parent directory st_mtime should be updated")
982
983 amsg = "mkdir - create directory should return an error if name already exists as a directory"
984 self.run_func(posix.mkdir, self.absdir, mode, msg=amsg, err=errno.EEXIST)
985
986 self.create_file()
987 amsg = "mkdir - create directory should return an error if name already exists as a file"
988 self.run_func(posix.mkdir, self.absfile, mode, msg=amsg, err=errno.EEXIST)
989
990 self.get_filename()
991 self.dprint('DBG3', "Creating symbolic link to directory [%s -> %s]" % (self.absfile, abstopdir))
992 os.symlink(abstopdir, self.absfile)
993 amsg = "mkdir - create directory should return an error if name already exists as a symbolic link"
994 self.run_func(posix.mkdir, self.absfile, mode, msg=amsg, err=errno.EEXIST)
995 except Exception:
996 self.test(False, traceback.format_exc())
997
998 def _mmap_test(self, objtype):
999 """Verify POSIX API mmap() by mapping a file and verifying I/O
1000 operations. Verify mmap followed by memory read of existing
1001 file works. Verify mmap followed by memory write to file works.
1002
1003 Verify POSIX API munmap() by mapping a file and then unmapping
1004 the file.
1005 """
1006 self.test_group("Verify POSIX API %s() on %s" % (objtype, self.nfsstr()))
1007 N = self.filesize
1008
1009 try:
1010 fd = None
1011 addr = 0
1012 map_len = N
1013 absfile = self.abspath(self.files[0])
1014 self.dprint('DBG3', "Open file %s for reading" % absfile)
1015 fd = posix.open(absfile, posix.O_RDONLY)
1016
1017 self.dprint('DBG3', "mmap file for reading")
1018 addr = self.libc.mmap(0, N, mmap.PROT_READ, mmap.MAP_SHARED, fd, 0)
1019 self.test(addr > 0, "%s - mmap for reading should succeed" % objtype)
1020 ptr = ctypes.cast(addr, ctypes.c_char_p)
1021 self.test(ptr.value[:N] == self.data_pattern(0,N), "%s - read data should be correct" % objtype)
1022 self.dprint('DBG3', "munmap file")
1023 out = self.libc.munmap(addr, N)
1024 addr = 0
1025 self.test(out == 0, "%s - munmap should succeed" % objtype)
1026
1027 self.dprint('DBG3', "mmap file for reading using aligned offset")
1028 map_len = N-self.PAGESIZE
1029 addr = self.libc.mmap(0, map_len, mmap.PROT_READ, mmap.MAP_SHARED, fd, self.PAGESIZE)
1030 self.test(addr > 0, "%s - mmap using aligned offset should succeed" % objtype)
1031 ptr = ctypes.cast(addr, ctypes.c_char_p)
1032 self.test(ptr.value[:map_len] == self.data_pattern(self.PAGESIZE,map_len), "%s - read data should be correct" % objtype)
1033 self.dprint('DBG3', "munmap file")
1034 out = self.libc.munmap(addr, map_len)
1035 addr = 0
1036 self.test(out == 0, "%s - munmap should succeed" % objtype)
1037
1038 self.dprint('DBG3', "mmap file for reading")
1039 map_len = N
1040 addr = self.libc.mmap(0, N, mmap.PROT_READ, mmap.MAP_SHARED, fd, 0)
1041 self.test(addr > 0, "%s - mmap should succeed" % objtype)
1042 ptr = ctypes.cast(addr, ctypes.c_char_p)
1043 # Closing the file descriptor does not unmap the region
1044 self.dprint('DBG3', "Close file")
1045 posix.close(fd)
1046 self.test(ptr.value[:N] == self.data_pattern(0,N), "%s - read data after file descriptor has been closed should be correct" % objtype)
1047 self.dprint('DBG3', "munmap file")
1048 out = self.libc.munmap(addr, N)
1049 addr = 0
1050 self.test(out == 0, "%s - munmap after the file has been closed should succeed" % objtype)
1051
1052 self.get_filename()
1053 self.dprint('DBG3', "Open file %s for reading and writing" % self.absfile)
1054 fd = posix.open(self.absfile, posix.O_RDWR|posix.O_CREAT)
1055 # Write a dummy byte at end of file to reserve space on the file
1056 posix.lseek(fd, N-1, os.SEEK_SET)
1057 posix.write(fd, b'\000')
1058
1059 if objtype == 'mmap':
1060 self.dprint('DBG3', "mmap file using length of 0")
1061 addr = self.libc.mmap(0, 0, mmap.PROT_READ|mmap.PROT_WRITE, mmap.MAP_SHARED, fd, 0)
1062 self.test(addr == -1, "%s - mmap with length of 0 should return an error" % objtype)
1063 self.dprint('DBG3', "mmap file using a non-aligned offset")
1064 addr = self.libc.mmap(0, N, mmap.PROT_READ|mmap.PROT_WRITE, mmap.MAP_SHARED, fd, 1)
1065 self.test(addr == -1, "%s - mmap with non-aligned offset should return an error" % objtype)
1066
1067 self.dprint('DBG3', "mmap file for writing")
1068 addr = self.libc.mmap(0, N, mmap.PROT_WRITE, mmap.MAP_SHARED, fd, 0)
1069 self.test(addr > 0, "%s - mmap for writing should succeed" % objtype)
1070 ptr = ctypes.cast(addr, ctypes.c_char_p)
1071 self.libc.memcpy(ptr, self.data_pattern(0,N), N)
1072 self.dprint('DBG3', "munmap file")
1073 if objtype == 'munmap':
1074 self.dprint('DBG3', "munmap using length of 0")
1075 out = self.libc.munmap(addr, 0)
1076 self.test(out == -1, "%s - munmap with length of 0 should return an error" % objtype)
1077 out = self.libc.munmap(addr, N)
1078 addr = 0
1079 self.test(out == 0, "%s - munmap with correct length should succeed" % objtype)
1080 posix.close(fd)
1081
1082 self.dprint('DBG3', "Read data from file to verify data written using mmap region")
1083 with open(self.absfile, "rb") as fdr:
1084 data = fdr.read()
1085 self.test(data == self.data_pattern(0,N), "%s - written data should be correct" % objtype)
1086
1087 absfile = self.abspath(self.files[0])
1088 fd = posix.open(absfile, posix.O_RDONLY)
1089 posix.close(fd)
1090
1091 if objtype == 'mmap':
1092 self.dprint('DBG3', "mmap on closed file")
1093 addr = self.libc.mmap(0, N, mmap.PROT_READ, mmap.MAP_SHARED, fd, 0)
1094 self.test(addr == -1, "%s - mmap on closed file should return an error" % objtype)
1095 except Exception:
1096 self.test(False, traceback.format_exc())
1097 finally:
1098 self.close_files(fd)
1099 if addr > 0:
1100 # Make sure to also unmap the file region
1101 self.libc.munmap(addr, map_len)
1102
1103 def mmap_test(self):
1104 """Verify POSIX API mmap() by mapping a file and verifying I/O
1105 operations. Verify mmap followed by memory read of existing
1106 file works. Verify mmap followed by memory write to file works.
1107 """
1108 self._mmap_test('mmap')
1109
1110 def munmap_test(self):
1111 """Verify POSIX API munmap() by mapping a file and then unmapping
1112 the file.
1113 """
1114 self._mmap_test('munmap')
1115
1116 def _oserror(self, oserrno, err):
1117 """Internal method to return fail message when expecting an error"""
1118 fmsg = ""
1119 # Test expression to return
1120 expr = oserrno == err
1121 if oserrno == 0:
1122 fmsg = ": no error was returned"
1123 elif oserrno != err:
1124 # Got the wrong error
1125 expected = errno.errorcode[err]
1126 error = errno.errorcode[oserrno]
1127 fmsg = ": expecting %s, got %s" % (expected, error)
1128 return (expr, fmsg)
1129
1130 def open_test(self):
1131 """Verify POSIX API open() on a file. Verify file creation using
1132 the O_CREAT flag and verifying the file mode is set to the
1133 specified value. Verify the st_ctime and st_mtime are updated
1134 on the parent directory after the file was created. Verify open
1135 on existing file fails with the O_EXCL flag set. Verify write
1136 succeeds and read fails if file was open with O_WRONLY. Verify
1137 read succeeds and write fails if file was open with O_RDONLY.
1138 Verify that all writes with O_APPEND set are to the end of the file.
1139 Use O_DSYNC, O_RSYNC, and O_SYNC flags in open calls.
1140 Verify file open with O_CREAT and O_TRUNC set will truncate an
1141 existing file. Verify that it updates the file st_ctime and st_mtime.
1142 """
1143 self.test_group("Verify POSIX API open() on %s" % self.nfsstr())
1144
1145 self.set_nfserr_list(
1146 nfs3list=[nfs3_const.NFS3ERR_NOENT, nfs3_const.NFS3ERR_INVAL],
1147 nfs4list=[nfs4_const.NFS4ERR_NOENT, nfs4_const.NFS4ERR_INVAL,
1148 nfs4_const.NFS4ERR_OPENMODE, nfs4_const.NFS4ERR_BADXDR],
1149 )
1150
1151 flist = []
1152 flag_list = list(open_flag_list)
1153 try:
1154 # Remove flag with no bits set
1155 flag_list.remove(0)
1156 flist += [(0,)]
1157 except:
1158 pass
1159
1160 # Create a list of all possible combination of flags
1161 for i in range(1, len(flag_list)+1):
1162 flist += list(itertools.combinations(flag_list, i))
1163
1164 iosize = 1024
1165 EXISTENT = 1
1166 NONEXISTENT = 2
1167 SYMLINK = 3
1168 fmap = {
1169 EXISTENT : 'existent file',
1170 NONEXISTENT : 'non-existent file',
1171 SYMLINK : 'symbolic link',
1172 }
1173
1174 for ftype in [NONEXISTENT, EXISTENT, SYMLINK]:
1175 srcfile = None
1176 if ftype == NONEXISTENT:
1177 # Get a new name for non-existent file
1178 self.get_filename()
1179 else:
1180 # Create file to use as existent file or as the source file
1181 # for the symbolic link
1182 self.create_file()
1183 os.chmod(self.absfile, 0o777)
1184 if ftype == SYMLINK:
1185 # Create symbolic link
1186 srcfile = self.absfile
1187 self.get_filename()
1188 self.dprint('DBG3', "Creating symbolic link file [%s -> %s]" % (self.absfile, srcfile))
1189 os.symlink(srcfile, self.absfile)
1190
1191 # Get the name for file under test
1192 filename = os.path.basename(self.absfile)
1193
1194 for flags in flist:
1195 try:
1196 tryopen = False
1197 opened = False
1198 flag_str = ", flags %s" % oflag_str(flags)
1199 wflag_str = " with flags %s" % oflag_str(flags)
1200 mode = 0
1201 for flag in flags:
1202 mode |= flag
1203
1204 perms = 0o700 | random.randint(0,0o77)
1205 if perms == 0o777:
1206 # Change open permissions to make it different than current
1207 # file permissions
1208 perms = 0o755
1209
1210 # Booleans for checking file if able to read or/and write
1211 is_write_allowed = posix.O_WRONLY in flags or posix.O_RDWR in flags
1212 is_read_allowed = posix.O_WRONLY not in flags or posix.O_RDWR in flags
1213
1214 if ftype in [EXISTENT, SYMLINK]:
1215 # Get file stats before open
1216 ofstat = os.stat(self.absfile)
1217
1218 try:
1219 openerr = 0
1220 tryopen = True
1221 self.dprint('DBG7', "Open %s %s%s" % (fmap[ftype], filename, wflag_str))
1222 fd = posix.open(self.absfile, mode, perms)
1223 opened = True
1224 except OSError as e:
1225 openerr = e.errno
1226 self.dprint('DBG7', "Open error: %s" % e.strerror)
1227 strerror = ": %s" % os.strerror(openerr) if openerr != 0 else ""
1228
1229 fstat = None
1230 if opened:
1231 # Get file stats after open
1232 fstat = posix.fstat(fd)
1233
1234 if ftype in [EXISTENT, SYMLINK]:
1235 if posix.O_EXCL in flags and posix.O_CREAT in flags:
1236 # O_EXCL and O_CREAT are set
1237 (expr, fmsg) = self._oserror(openerr, errno.EEXIST)
1238 msg = "open - opening %s should return an error when O_EXCL|O_CREAT is used" % fmap[ftype]
1239 self.test(expr, msg, subtest=flag_str, failmsg=fmsg)
1240 elif openerr != 0 and posix.O_EXCL in flags and posix.O_CREAT not in flags:
1241 msg = "open - opening %s should be unspecified when O_EXCL is used and O_CREAT is not specified" % fmap[ftype]
1242 self.test(True, msg, subtest=flag_str)
1243 elif posix.O_DIRECTORY in flags and posix.O_CREAT in flags:
1244 msg = "open - opening %s should be unspecified when O_DIRECTORY|O_CREAT is used" % fmap[ftype]
1245 self.test(True, msg, subtest=flag_str)
1246 elif posix.O_DIRECTORY in flags:
1247 # O_DIRECTORY is set
1248 (expr, fmsg) = self._oserror(openerr, errno.ENOTDIR)
1249 if not expr and ftype == SYMLINK:
1250 (expr, fmsg) = self._oserror(openerr, errno.ELOOP)
1251 msg = "open - opening %s should return an error when O_DIRECTORY is used" % fmap[ftype]
1252 self.test(expr, msg, subtest=flag_str, failmsg=fmsg)
1253 elif posix.O_WRONLY not in flags and posix.O_RDWR not in flags and posix.O_TRUNC in flags:
1254 # O_RDONLY and O_TRUNC are set
1255 msg = "open - opening %s should be unspecified when O_RDONLY|O_TRUNC is used" % fmap[ftype]
1256 self.test(True, msg, subtest=flag_str)
1257 elif posix.O_NOFOLLOW in flags and ftype == SYMLINK:
1258 (expr, fmsg) = self._oserror(openerr, errno.ELOOP)
1259 msg = "open - opening %s should return an error when O_NOFOLLOW is used" % fmap[ftype]
1260 self.test(expr, msg, subtest=flag_str, failmsg=fmsg)
1261 elif posix.O_NOATIME in flags and openerr != 0:
1262 msg = "open - opening %s should be unspecified when O_NOATIME is used" % fmap[ftype]
1263 self.test(True, msg, subtest=wflag_str)
1264 else:
1265 msg = "open - opening %s should succeed" % fmap[ftype]
1266 self.test(openerr == 0, msg, subtest=wflag_str, failmsg=strerror)
1267 if openerr == 0:
1268 expr = fstat.st_mode & 0o777 == ofstat.st_mode & 0o777
1269 msg = "open - permission mode should not be changed when opening %s" % fmap[ftype]
1270 fmsg = ": changed from %04o to %04o" % (ofstat.st_mode & 0o777, fstat.st_mode & 0o777)
1271 self.test(expr, msg, subtest=wflag_str, failmsg=fmsg)
1272 else:
1273 if openerr != 0 and posix.O_CREAT in flags and posix.O_DIRECTORY in flags:
1274 msg = "open - opening %s should be unspecified when O_CREAT|O_DIRECTORY is used" % fmap[ftype]
1275 self.test(True, msg, subtest=flag_str)
1276 elif posix.O_CREAT in flags:
1277 # O_CREAT is set
1278 msg = "open - file should be created when O_CREAT is used"
1279 file_exists = os.path.exists(self.absfile)
1280 self.test(file_exists, msg, subtest=flag_str)
1281 if file_exists:
1282 if fstat is None:
1283 fstat = os.stat(self.absfile)
1284 expr = fstat.st_mode & 0o777 == perms
1285 msg = "open - file should be created with correct permission mode"
1286 fmsg = ": expecting %04o, got %04o" % (perms, fstat.st_mode & 0o777)
1287 self.test(expr, msg, subtest=flag_str, failmsg=fmsg)
1288 else:
1289 # O_CREAT is not set
1290 (expr, fmsg) = self._oserror(openerr, errno.ENOENT)
1291 msg = "open - opening %s should return an error when O_CREAT is not used" % fmap[ftype]
1292 self.test(expr, msg, subtest=flag_str, failmsg=fmsg)
1293
1294 if openerr != 0:
1295 if not is_write_allowed and posix.O_EXCL in flags:
1296 msg = "open - opening %s should be unspecified when O_RDONLY|O_EXCL" % fmap[ftype]
1297 self.test(True, msg, subtest=flag_str)
1298 continue
1299
1300 if posix.O_TRUNC in flags:
1301 expr = fstat.st_size == 0
1302 msg = "open - file size should be 0 after open when O_TRUNC is used"
1303 fmsg = ": file size = %d" % fstat.st_size
1304 self.test(expr, msg, subtest=flag_str, failmsg=fmsg)
1305
1306 wdata = self.data_pattern(0, iosize)
1307 try:
1308 ioerr = 0
1309 posix.write(fd, wdata)
1310 except OSError as e:
1311 ioerr = e.errno
1312 wstrerror = ": %s" % os.strerror(ioerr) if ioerr != 0 else ""
1313
1314 if ioerr == 0:
1315 # Get file stats after write
1316 nfstat = posix.fstat(fd)
1317
1318 if is_write_allowed:
1319 if ioerr != 0 and posix.O_WRONLY in flags and posix.O_RDWR in flags:
1320 msg = "open - writing should be unspecified when opening with O_WRONLY|O_RDWR"
1321 self.test(True, msg, subtest=flag_str)
1322 else:
1323 msg = "open - writing should succeed when opening with O_WRONLY or O_RDWR"
1324 self.test(ioerr == 0, msg, subtest=flag_str, failmsg=wstrerror)
1325 if posix.O_TRUNC in flags:
1326 expr = nfstat.st_size == iosize
1327 msg = "open - data should be written at the beginning of file when O_TRUNC is used"
1328 fmsg = ": expecting file size = %d, got %d" % (iosize, nfstat.st_size)
1329 self.test(expr, msg, subtest=flag_str, failmsg=fmsg)
1330 elif posix.O_APPEND in flags:
1331 expr = nfstat.st_size == fstat.st_size + iosize
1332 msg = "open - data should be written at the end of file when O_APPEND is used"
1333 fmsg = ": expecting file size = %d, got %d" % (fstat.st_size + iosize, nfstat.st_size)
1334 self.test(expr, msg, subtest=flag_str, failmsg=fmsg)
1335 else:
1336 (expr, fmsg) = self._oserror(ioerr, errno.EBADF)
1337 msg = "open - writing should return an error when opening with O_RDONLY"
1338 self.test(expr, msg, subtest=flag_str, failmsg=fmsg)
1339
1340 try:
1341 ioerr = 0
1342 posix.lseek(fd, 0, os.SEEK_SET)
1343 rdata = posix.read(fd, iosize)
1344 except OSError as e:
1345 ioerr = e.errno
1346 rstrerror = ": %s" % os.strerror(ioerr) if ioerr != 0 else ""
1347
1348 if is_read_allowed:
1349 if ioerr != 0 and posix.O_WRONLY in flags and posix.O_RDWR in flags:
1350 msg = "open - reading should be unspecified when opening with O_WRONLY|O_RDWR"
1351 self.test(True, msg, subtest=flag_str)
1352 else:
1353 msg = "open - reading should succeed when opening with O_RDONLY or O_RDWR"
1354 self.test(ioerr == 0, msg, subtest=flag_str, failmsg=rstrerror)
1355 else:
1356 (expr, fmsg) = self._oserror(ioerr, errno.EBADF)
1357 msg = "open - reading should return an error when opening with O_WRONLY"
1358 self.test(expr, msg, subtest=flag_str, failmsg=fmsg)
1359
1360 except Exception:
1361 self.test(False, traceback.format_exc())
1362 finally:
1363 if opened:
1364 self.dprint('DBG7', "Close file %s" % filename)
1365 posix.close(fd)
1366 if tryopen and ftype == NONEXISTENT and (opened or os.path.exists(self.absfile)):
1367 # Remove file so the file does not exist
1368 # for next iteration
1369 self.dprint('DBG5', "Removing file [%s]" % self.absfile)
1370 os.unlink(self.absfile)
1371
1372 def opendir_test(self):
1373 """Verify POSIX API opendir() on a directory."""
1374 self.test_group("Verify POSIX API opendir() on %s" % self.nfsstr())
1375
1376 try:
1377 fd = None
1378 self.create_dir()
1379 fd = self.libc.opendir(self.absdir.encode())
1380 self.test(fd != None, "opendir - opendir() on an existent directory should succeed")
1381 self.libc.closedir(fd)
1382 fd = None
1383 fd = self.libc.opendir(self.absdir.encode() + b'bogus')
1384 self.test(fd == None, "opendir - opendir() on a non-existent directory should fail")
1385 except Exception:
1386 self.test(False, traceback.format_exc())
1387 finally:
1388 if fd:
1389 self.libc.closedir(fd)
1390
1391 def read_test(self):
1392 """Verify POSIX API read() by reading data from a file. Verify that
1393 the st_atime of the file is updated after the read. Verify a read
1394 of 0 bytes returns 0.
1395 """
1396 self.test_group("Verify POSIX API read() on %s" % self.nfsstr())
1397
1398 try:
1399 fd = None
1400 absfile = self.abspath(self.files[1])
1401 self.dprint('DBG3', "Open file %s for reading" % absfile)
1402 fd = posix.open(absfile, posix.O_RDONLY)
1403 fstat_b = os.stat(absfile)
1404 sleep(1)
1405
1406 self.dprint('DBG3', "Read data from file %s using POSIX API read()" % absfile)
1407 amsg = "read - read() should succeed"
1408 data = self.run_func(posix.read, fd, 0, msg=amsg)
1409 self.test(len(data) == 0, "read - reading 0 bytes should return 0")
1410 data = self.run_func(posix.read, fd, self.filesize, msg=amsg)
1411 self.test(data == self.data_pattern(0, len(data)), "read - data returned should be correct")
1412 posix.close(fd)
1413 fstat = os.stat(absfile)
1414 self.test(fstat.st_atime > fstat_b.st_atime, "read - file st_atime should be updated")
1415 except Exception:
1416 self.test(False, traceback.format_exc())
1417 finally:
1418 self.close_files(fd)
1419
1420 def readdir_test(self):
1421 """Verify POSIX API readdir() on a directory."""
1422 self.test_group("Verify POSIX API readdir() on %s" % self.nfsstr())
1423
1424 try:
1425 fd = None
1426 self.create_dir()
1427 self.create_file(dir=self.dirname)
1428 filename = os.path.basename(self.filename).encode()
1429
1430 fd = self.libc.opendir(self.absdir.encode())
1431 dirlist = []
1432 while True:
1433 dirent = self.libc.readdir(fd)
1434 if not bool(dirent):
1435 break
1436 dirlist.append(dirent[0].d_name)
1437 self.libc.closedir(fd)
1438 fd = None
1439
1440 self.test(len(dirlist) > 0, "readdir - readdir() on an open directory should succeed")
1441 self.test(filename in dirlist, "readdir - file in directory should be returned by readdir()")
1442 self.test(True, "readdir - readdir() should return 0 at the end of the list")
1443 except Exception:
1444 self.test(False, traceback.format_exc())
1445 finally:
1446 if fd:
1447 self.libc.closedir(fd)
1448
1449 def readlink_test(self):
1450 """Verify Test POSIX API readlink() by reading a symbolic link."""
1451 self.test_group("Verify POSIX API readlink() on %s" % self.nfsstr())
1452
1453 try:
1454 for stype in self.setup_readlink_types:
1455 srcpath, symlink = self.setup_readlink.get(stype)
1456 self.dprint('DBG3', "Reading symbolic link to %s [%s]" % (stype, symlink))
1457 amsg = "readlink - reading symbolic link to a %s should succeed" % stype
1458 data = self.run_func(posix.readlink, symlink, msg=amsg)
1459 if self.oserror is None:
1460 src_str = "source " if stype in ("file", "directory") else ""
1461 self.test(data == srcpath, "readlink - data of symbolic link should be the name of the %s%s" % (src_str, stype))
1462
1463 # The named file does not exist.
1464 symlink += "_bogus"
1465 self.dprint('DBG3', "Reading non-existent symbolic link [%s]" % symlink)
1466 amsg = "readlink - reading non-existent symbolic link should fail"
1467 self.run_func(posix.readlink, symlink, msg=amsg, err=errno.ENOENT)
1468
1469 # The named file is not a symbolic link.
1470 for stype in ("file", "directory"):
1471 srcpath, symlink = self.setup_readlink.get(stype)
1472 self.dprint('DBG3', "Reading a %s as a symbolic link [%s]" % (stype, srcpath))
1473 amsg = "readlink - reading a %s as a symbolic link should fail" % stype
1474 self.run_func(posix.readlink, srcpath, msg=amsg, err=errno.EINVAL)
1475 except Exception:
1476 self.test(False, traceback.format_exc())
1477
1478 def rename_test(self):
1479 """Verify POSIX API rename() by renaming a file, directory, and a
1480 symbolic link. Verify that a rename from a file to a symbolic
1481 link will cause the symbolic link to be removed.
1482 """
1483 self.test_group("Verify POSIX API rename() on %s" % self.nfsstr())
1484
1485 try:
1486 self.test_info("Rename file")
1487 self.create_file()
1488 oldname = self.absfile
1489 self.get_filename()
1490 self.dprint('DBG3', "Rename file %s to %s using POSIX API rename()" % (oldname, self.absfile))
1491 amsg = "rename - rename() should succeed"
1492 self.run_func(posix.rename, oldname, self.absfile, msg=amsg)
1493 self.test(os.path.exists(self.absfile), "rename - new file name should exist")
1494 self.test(not os.path.exists(oldname), "rename - old file name should not exist")
1495
1496 self.test_info("Rename directory")
1497 self.create_dir()
1498 oldname = self.absdir
1499 self.get_dirname()
1500 self.dprint('DBG3', "Rename directory %s to %s using POSIX API rename()" % (oldname, self.absdir))
1501 self.run_func(posix.rename, oldname, self.absdir, msg=amsg)
1502 self.test(os.path.exists(self.absdir), "rename - new directory name should exist")
1503 self.test(not os.path.exists(oldname), "rename - old directory name should not exist")
1504
1505 self.test_info("Rename symbolic link")
1506 srcfile = self.absfile
1507 self.get_filename()
1508 self.dprint('DBG3', "Creating symbolic link [%s -> %s]" % (self.absfile, srcfile))
1509 os.symlink(srcfile, self.absfile)
1510 oldname = self.absfile
1511 self.get_filename()
1512 self.dprint('DBG3', "Rename symbolic link %s to %s using POSIX API rename()" % (oldname, self.absfile))
1513 self.run_func(posix.rename, oldname, self.absfile, msg=amsg)
1514 self.test(os.path.exists(self.absfile), "rename - new symbolic link name should exist")
1515 self.test(not os.path.exists(oldname), "rename - old symbolic link name should not exist")
1516 self.test(os.path.islink(self.absfile), "rename - new name should be a symbolic link")
1517
1518 self.test_info("Rename file to an existing symbolic link")
1519 newname = self.absfile
1520 self.create_file()
1521 oldname = self.absfile
1522 self.dprint('DBG3', "Rename file %s to an existing symbolic link %s using POSIX API rename()" % (oldname, newname))
1523 try:
1524 self.run_func(posix.rename, oldname, newname, msg=amsg)
1525 self.test(os.path.exists(newname), "rename - new file name should exist")
1526 self.test(not os.path.exists(oldname), "rename - old file name should not exist")
1527 self.test(os.path.isfile(newname) and not os.path.islink(newname), "rename - new name should be a regular file")
1528 except:
1529 self.test(False, "rename - renaming file to an existing symbolic link should have succeeded")
1530 except Exception:
1531 self.test(False, traceback.format_exc())
1532
1533 def rewinddir_test(self):
1534 """Verify POSIX API rewinddir() on a directory."""
1535 self._tell_seek_dir_test('rewinddir')
1536
1537 def rmdir_test(self):
1538 """Verify POSIX API rmdir() by removing a directory. Verify that the
1539 parent's st_ctime and st_mtime are updated.
1540 """
1541 self.set_nfserr_list(
1542 nfs3list=[nfs3_const.NFS3ERR_NOENT, nfs3_const.NFS3ERR_NOTEMPTY],
1543 nfs4list=[nfs4_const.NFS4ERR_NOENT, nfs4_const.NFS4ERR_NOTEMPTY],
1544 )
1545 self.test_group("Verify POSIX API rmdir() on %s" % self.nfsstr())
1546
1547 try:
1548 dir = self.create_dir()
1549 topdir = self.absdir
1550 self.create_dir(dir=dir)
1551 dstat_b = os.stat(topdir)
1552 sleep(1)
1553
1554 self.dprint('DBG3', "Remove directory %s using POSIX API rmdir()" % self.absdir)
1555 amsg = "rmdir - rmdir() should succeed"
1556 self.run_func(posix.rmdir, self.absdir, msg=amsg)
1557 dstat = os.stat(topdir)
1558 self.test(not os.path.exists(self.absdir), "rmdir - directory should be removed")
1559 self.test(dstat.st_ctime > dstat_b.st_ctime, "rmdir - parent directory st_ctime should be updated")
1560 self.test(dstat.st_mtime > dstat_b.st_mtime, "rmdir - parent directory st_mtime should be updated")
1561
1562 amsg = "rmdir - removing non-existent directory should return an error"
1563 self.run_func(posix.rmdir, self.absdir+'_bogus', msg=amsg, err=errno.ENOENT)
1564
1565 self.create_dir(dir=dir)
1566 self.create_file(dir=dir)
1567 amsg = "rmdir - removing non-empty directory should return an error"
1568 self.run_func(posix.rmdir, topdir, msg=amsg, err=errno.ENOTEMPTY)
1569 except Exception:
1570 self.test(False, traceback.format_exc())
1571
1572 def seekdir_test(self):
1573 """Verify POSIX API seekdir() on a directory."""
1574 self._tell_seek_dir_test('seekdir')
1575
1576 def stat_test(self):
1577 """Verify POSIX API stat() by checking the mode on a file and that
1578 it returns the expected structure members. Create a symlink and
1579 verify that stat returns information about the file.
1580 """
1581 self._stat_test(stat_mode=1)
1582
1583 def statvfs_test(self):
1584 """Verify POSIX API statvfs() by making sure all the members of the
1585 structure are returned.
1586 """
1587 self.test_group("Verify POSIX API statvfs() on %s" % self.nfsstr())
1588
1589 try:
1590 self.create_file()
1591 self._statvfs_test(path=self.absfile)
1592
1593 self.create_dir()
1594 self._statvfs_test(path=self.absdir, objtype='directory')
1595 except Exception:
1596 self.test(False, traceback.format_exc())
1597
1598 def symlink_test(self):
1599 """Verify POSIX API symlink() by creating a symbolic link and verify
1600 that the file type is slnk.
1601 """
1602 self.test_group("Verify POSIX API symlink() on %s" % self.nfsstr())
1603
1604 try:
1605 srcfile = self.abspath(self.files[0])
1606 self.get_filename()
1607 self.dprint('DBG3', "Create symbolic link %s -> %s using POSIX API symlink()" % (self.absfile, srcfile))
1608 amsg = "symlink - symlink() should succeed"
1609 self.run_func(posix.symlink, srcfile, self.absfile, msg=amsg)
1610 lstat = os.lstat(self.absfile)
1611 rlink = os.readlink(self.absfile)
1612 self.test(lstat != None, "symlink - symbolic link should be created")
1613 self.test(stat.S_ISLNK(lstat.st_mode), "symlink - object type should be a symbolic link")
1614 self.test(rlink == srcfile, "symlink - symbolic link data should be the name of the source file")
1615 except Exception:
1616 self.test(False, traceback.format_exc())
1617
1618 def _sync(self, objtype):
1619 """Verify POSIX API sync(), fdatasync() or fsync()."""
1620 self.test_group("Verify POSIX API %s() on %s" % (objtype, self.nfsstr()))
1621
1622 try:
1623 fd = None
1624 self.get_filename()
1625 self.dprint('DBG3', "Open file %s for writing" % self.absfile)
1626 fd = posix.open(self.absfile, posix.O_WRONLY|posix.O_CREAT)
1627
1628 offset = 0
1629 self.dprint('DBG3', "Write data to file")
1630 count = posix.write(fd, self.data_pattern(0, self.filesize))
1631 offset += count
1632 sleep(1)
1633 self.dprint('DBG3', "Write data to file")
1634 count = posix.write(fd, self.data_pattern(offset, self.filesize))
1635 offset += count
1636 sleep(1)
1637 self.dprint('DBG3', "Write data to file")
1638 count = posix.write(fd, self.data_pattern(offset, self.filesize))
1639 offset += count
1640
1641 self.dprint('DBG3', "Sync data to file")
1642 amsg = "%s - sync should succeed" % objtype
1643 if objtype == 'sync':
1644 out = self.libc.sync()
1645 self.test(out == 0, amsg)
1646 elif objtype == 'fsync':
1647 self.run_func(posix.fsync, fd, msg=amsg)
1648 else:
1649 self.run_func(posix.fdatasync, fd, msg=amsg)
1650 posix.close(fd)
1651
1652 amsg = "%s - sync after close should return an error" % objtype
1653 if objtype == 'sync':
1654 out = self.libc.sync()
1655 self.test(out == 0, "%s - sync after close should succeed" % objtype)
1656 elif objtype == 'fsync':
1657 self.run_func(posix.fsync, fd, msg=amsg, err=errno.EBADF)
1658 elif objtype == 'fdatasync':
1659 self.run_func(posix.fdatasync, fd, msg=amsg, err=errno.EBADF)
1660 except Exception:
1661 self.test(False, traceback.format_exc())
1662 finally:
1663 self.close_files(fd)
1664
1665 def sync_test(self):
1666 """Verify POSIX API sync()."""
1667 self._sync('sync')
1668
1669 def _readdir(self, fd, offset):
1670 """Wrapper for readdir()."""
1671 self.libc.seekdir(fd, offset)
1672 c_dirent = self.libc.readdir(fd)
1673 dirent = ctypes.cast(c_dirent, ctypes.POINTER(DirEnt))
1674 return dirent[0].d_name
1675
1676 def _tell_seek_dir_test(self, objtype):
1677 """Verify POSIX API telldir(), seekdir() or rewinddir() on a directory."""
1678 self.test_group("Verify POSIX API %s() on %s" % (objtype, self.nfsstr()))
1679
1680 try:
1681 fd = None
1682 self.create_dir()
1683 N = 5
1684 for i in range(N):
1685 self.create_file(dir=self.dirname)
1686
1687 fd = self.libc.opendir(self.absdir.encode())
1688
1689 dirlist = []
1690 offsets = []
1691 while True:
1692 offset = self.libc.telldir(fd)
1693 offsets.append(offset)
1694 dirent = self.libc.readdir(fd)
1695 if not bool(dirent):
1696 break
1697 dirlist.append(dirent[0].d_name)
1698
1699 if objtype == 'rewinddir':
1700 self.libc.rewinddir(fd)
1701 self.test(True, "%s - directory rewind should succeed after reaching end of list" % objtype)
1702 dirent = self.libc.readdir(fd)
1703 self.test(dirent[0].d_name == dirlist[0], "%s - directory entry is correct after rewind" % objtype)
1704 index = int(N/2)
1705 name = self._readdir(fd, index)
1706 self.libc.rewinddir(fd)
1707 self.test(True, "%s - directory rewind from the middle of list should succeed" % objtype)
1708 c_dirent = self.libc.readdir(fd)
1709 dirent = ctypes.cast(c_dirent, ctypes.POINTER(DirEnt))
1710 self.test(dirent[0].d_name == dirlist[0], "%s - directory entry is correct after rewind" % objtype)
1711 else:
1712 random_list = list(range(N))
1713 random.shuffle(random_list)
1714 for index in random_list:
1715 name = self._readdir(fd, offsets[index])
1716 self.test(name == dirlist[index], "%s - directory entry is correct at offset = %d" % (objtype, offsets[index]))
1717
1718 self.libc.closedir(fd)
1719 fd = None
1720 except Exception:
1721 self.test(False, traceback.format_exc())
1722 finally:
1723 if fd:
1724 self.libc.closedir(fd)
1725
1726 def telldir_test(self):
1727 """Verify POSIX API telldir() on a directory."""
1728 self._tell_seek_dir_test('telldir')
1729
1730 def unlink_test(self):
1731 """Verify POSIX API unlink() by unlinking a file and verify that it
1732 was removed. Verify that the st_ctime and st_mtime fields of the
1733 parent directory were updated. Then unlink a symbolic link and
1734 verify that the symbolic link was removed but not the referenced
1735 file. Then remove an opened file and verify that I/O still occurs
1736 to the file after the unlink and that the file gets removed when
1737 the file is closed. Create a file and then hard link to it so the
1738 link count is greater than 1. Unlink the hard file and verify that
1739 st_ctime field is updated.
1740 """
1741 self.test_group("Verify POSIX API unlink() on %s" % self.nfsstr())
1742
1743 try:
1744 fd = None
1745 self.create_file()
1746 dstat_b = os.stat(self.mtdir)
1747 sleep(1)
1748
1749 self.test_info("Unlink file")
1750 self.dprint('DBG3', "Remove file %s using POSIX API unlink()" % self.absfile)
1751 amsg = "unlink - unlink() should succeed"
1752 self.run_func(posix.unlink, self.absfile, msg=amsg)
1753 dstat = os.stat(self.mtdir)
1754 self.test(not os.path.exists(self.absfile), "unlink - file should be removed")
1755 self.test(dstat.st_ctime > dstat_b.st_ctime, "unlink - parent directory st_ctime should be updated")
1756 self.test(dstat.st_mtime > dstat_b.st_mtime, "unlink - parent directory st_mtime should be updated")
1757
1758 self.test_info("Unlink symbolic link")
1759 self.create_file()
1760 srcfile = self.absfile
1761 self.get_filename()
1762 os.symlink(srcfile, self.absfile)
1763 self.dprint('DBG3', "Remove symbolic link %s using POSIX API unlink()" % self.absfile)
1764 self.run_func(posix.unlink, self.absfile, msg=amsg)
1765 self.test(not os.path.exists(self.absfile), "unlink - symbolic link should be removed")
1766 self.test(os.path.exists(srcfile), "unlink - symbolic link source file should not be removed")
1767
1768 self.test_info("Unlink opened file")
1769 self.get_filename()
1770 self.dprint('DBG3', "Open file %s for writing" % self.absfile)
1771 fd = posix.open(self.absfile, posix.O_WRONLY|posix.O_CREAT)
1772 self.dprint('DBG3', "Remove file %s using POSIX API unlink()" % self.absfile)
1773 self.run_func(posix.unlink, self.absfile, msg=amsg)
1774 self.test(not os.path.exists(self.absfile), "unlink - opened file should be removed")
1775 self.dprint('DBG3', "Write file %s %s@0" % (self.absfile, self.filesize))
1776 count = posix.write(fd, self.data_pattern(0, self.filesize))
1777 self.test(count > 0, "unlink - write should succeed after unlink()")
1778 posix.close(fd)
1779 self.test(not os.path.exists(self.absfile), "unlink - opened file should be removed after close()")
1780
1781 self.test_info("Unlink file after hard link is created")
1782 self.create_file()
1783 srcfile = self.absfile
1784 self.get_filename()
1785 self.dprint('DBG3', "Create hard link %s to file %s" % (self.absfile, srcfile))
1786 os.link(srcfile, self.absfile)
1787 fstat_b = os.stat(self.absfile)
1788 sleep(1)
1789
1790 self.dprint('DBG3', "Remove file %s using POSIX API unlink()" % srcfile)
1791 self.run_func(posix.unlink, srcfile, msg=amsg)
1792 fstat = os.stat(self.absfile)
1793 self.test(not os.path.exists(srcfile), "unlink - original file should be removed")
1794 self.test(os.path.exists(self.absfile), "unlink - hard link file should not be removed")
1795 self.test(fstat.st_nlink == fstat_b.st_nlink-1, "unlink - file st_nlink should be decremented by 1")
1796 self.test(fstat.st_ctime > fstat_b.st_ctime, "unlink - file st_ctime should be updated")
1797 except Exception:
1798 self.test(False, traceback.format_exc())
1799 finally:
1800 self.close_files(fd)
1801
1802 def write_test(self):
1803 """Verify POSIX API write() by writing 0 bytes and verifying 0
1804 is returned. Write a pattern the file, seek +N, write another
1805 pattern, and close the file. Open the file and read in both
1806 written patterns and verify that it is the correct pattern.
1807 Read in the data from the hole in the file and verify that it is 0.
1808 """
1809 self.test_group("Verify POSIX API write() on %s" % self.nfsstr())
1810
1811 try:
1812 fd = None
1813 self.get_filename()
1814 self.dprint('DBG3', "Open file %s for writing" % self.absfile)
1815 fd = posix.open(self.absfile, posix.O_WRONLY|posix.O_CREAT)
1816 fstat_b = os.stat(self.absfile)
1817 sleep(1)
1818
1819 self.dprint('DBG3', "Write 0 bytes to file %s using POSIX API write()" % self.absfile)
1820 amsg = "write - write() should succeed"
1821 count = self.run_func(posix.write, fd, b'', msg=amsg)
1822 self.test(count == 0, "write - writing 0 bytes should return 0")
1823
1824 self.dprint('DBG3', "Write data to file %s at offset = 0 using POSIX API write()" % self.absfile)
1825 count = self.run_func(posix.write, fd, self.data_pattern(0, self.filesize), msg=amsg)
1826 self.test(count == self.filesize, "write - writing N bytes should return N")
1827
1828 offset = posix.lseek(fd, self.filesize, os.SEEK_CUR)
1829 self.dprint('DBG3', "Write data to file %s at different offset = N using POSIX API write()" % self.absfile)
1830 count = self.run_func(posix.write, fd, self.data_pattern(offset, self.filesize), msg=amsg)
1831 posix.close(fd)
1832
1833 fstat = os.stat(self.absfile)
1834 self.test(fstat.st_ctime > fstat_b.st_ctime, "write - file st_ctime should be updated")
1835 self.test(fstat.st_mtime > fstat_b.st_mtime, "write - file st_mtime should be updated")
1836
1837 self.dprint('DBG3', "Open file %s for reading" % self.absfile)
1838 fd = posix.open(self.absfile, posix.O_RDONLY)
1839
1840 self.dprint('DBG3', "Read data from file %s at offset = 0" % self.absfile)
1841 data = posix.read(fd, 1000)
1842 self.test(data == self.data_pattern(0, len(data)), "write - written data should be correct at offset = 0")
1843
1844 off = posix.lseek(fd, offset, os.SEEK_SET)
1845 data = posix.read(fd, 1000)
1846 self.test(data == self.data_pattern(off, len(data)), "write - written data should be correct at offset = N")
1847
1848 # Read hole
1849 hsize = int(self.filesize/2)
1850 off = posix.lseek(fd, offset-hsize, os.SEEK_SET)
1851 data = posix.read(fd, hsize)
1852 self.test(data == bytes(len(data)), "write - hole in middle of file should be read correctly")
1853
1854 posix.close(fd)
1855 except Exception:
1856 self.test(False, traceback.format_exc())
1857 finally:
1858 self.close_files(fd)
1859
1860 ################################################################################
1861 # Entry point
1862 x = PosixTest(usage=USAGE, testnames=TESTNAMES, sid=SCRIPT_ID)
1863
1864 try:
1865 x.setup(nfiles=2)
1866
1867 # Run all the tests
1868 x.run_tests()
1869 except Exception:
1870 x.test(False, traceback.format_exc())
1871 finally:
1872 x.cleanup()
1873 x.exit()