tests: Implement test_fcntl_lock correctly
authorAndreas Schneider <asn@samba.org>
Fri, 23 Jun 2023 11:50:03 +0000 (13:50 +0200)
committerAndreas Schneider <asn@samba.org>
Thu, 29 Jun 2023 09:22:19 +0000 (11:22 +0200)
This does:

openat(AT_FDCWD, "/tmp/test_socket_wrapper_Win6aA/file", O_RDWR|O_CREAT, 0600) = 3
fcntl(3, F_OFD_SETLK, {l_type=F_WRLCK, l_whence=SEEK_SET, l_start=0, l_len=1}) = 0
write(3, "fd=3\n", 5)                   = 5
fcntl(3, F_OFD_SETLK, {l_type=F_UNLCK, l_whence=SEEK_SET, l_start=0, l_len=1}) = 0
unlink("/tmp/test_socket_wrapper_Win6aA/file") = 0                                                                                                                                   close(3)
close(3)                                = 0

Signed-off-by: Andreas Schneider <asn@samba.org>
Reviewed-by: Stefan Metzmacher <metze@samba.org>
tests/test_fcntl_lock.c

index 6bf2cd160ab7e84a6bdc548f0f754e3fe42cc815..0c172d696c4e99736278b81b2aa202ca9e69ae66 100644 (file)
@@ -49,34 +49,56 @@ static int teardown(void **state)
 static void test_fcntl_lock(void **state)
 {
        char file[PATH_MAX];
-       int fd, rc;
-       struct flock lock;
+       char buf[16];
+       int fd, rc, len;
        char *s = (char *)*state;
+       struct flock lock = {
+               .l_type = F_WRLCK,
+               .l_whence = SEEK_SET,
+               .l_start = 0,
+               .l_len = 1,
+       };
        int cmd = F_SETLK;
-#ifdef F_SETLK64
-       cmd = F_SETLK64;
-#endif
+
+/* Prefer OFD locks on Linux with _GNU_SOURCE set */
 #ifdef F_OFD_SETLK
-       cmd = F_OFD_SETLK;
+       if (sizeof(lock) >= 24) {
+               cmd = F_OFD_SETLK;
+       }
+#endif
+
+       printf("sizeof(lock)=%zu\n", sizeof(lock));
+#ifdef __USE_LARGEFILE64
+       printf("__USE_LARGEFILE64\n");
+#endif
+#ifdef __USE_FILE_OFFSET64
+       printf("__USE_FILE_OFFSET64\n");
 #endif
 
        rc = snprintf(file, sizeof(file), "%s/file", s);
        assert_in_range(rc, 0, PATH_MAX);
 
-       fd = open(file, O_CREAT, 0600);
+       fd = open(file, O_RDWR|O_CREAT, 0600);
        assert_return_code(fd, errno);
 
-       lock.l_type = F_RDLCK;
-       lock.l_whence = SEEK_SET;
-       lock.l_start = 0;
-       lock.l_len = 4;
-       lock.l_pid = 0;
+       rc = fcntl(fd, cmd, &lock);
+       assert_return_code(rc, errno);
 
+       len = snprintf(buf, sizeof(buf), "fd=%d\n", fd);
+       assert_in_range(len, 0, sizeof(buf));
+
+       rc = write(fd, buf, len);
+       assert_return_code(rc, errno);
+
+       lock.l_type = F_UNLCK;
        rc = fcntl(fd, cmd, &lock);
        assert_return_code(rc, errno);
 
        rc = unlink(file);
        assert_return_code(rc, errno);
+
+       rc = close(fd);
+       assert_return_code(rc, errno);
 }