tests: Implement test_fcntl_lock correctly
[socket_wrapper.git] / 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);
 }