lib: Add a little closefrom() test
authorVolker Lendecke <vl@samba.org>
Fri, 10 Jun 2016 09:40:33 +0000 (11:40 +0200)
committerAndreas Schneider <asn@cryptomilk.org>
Mon, 13 Jun 2016 12:11:11 +0000 (14:11 +0200)
Signed-off-by: Volker Lendecke <vl@samba.org>
Reviewed-by: Andreas Schneider <asn@samba.org>
Autobuild-User(master): Andreas Schneider <asn@cryptomilk.org>
Autobuild-Date(master): Mon Jun 13 14:11:11 CEST 2016 on sn-devel-144

lib/replace/test/testsuite.c

index 961b77d9eaf81f9e3184464b1ddc3254873f987e..dba545e27a24343b563e026f481e5e52392c2bc0 100644 (file)
@@ -1063,6 +1063,38 @@ static int test_memmem(void)
        return true;
 }
 
+static bool test_closefrom(void)
+{
+       int i, fd;
+
+       for (i=0; i<100; i++) {
+               fd = dup(0);
+               if (fd == -1) {
+                       perror("dup failed");
+                       return false;
+               }
+
+               /* 1000 is just an arbitrarily chosen upper bound */
+
+               if (fd >= 1000) {
+                       printf("fd=%d\n", fd);
+                       return false;
+               }
+       }
+
+       closefrom(3);
+
+       for (i=3; i<=fd; i++) {
+               off_t off;
+               off = lseek(i, 0, SEEK_CUR);
+               if ((off != (off_t)-1) || (errno != EBADF)) {
+                       printf("fd %d not closed\n", i);
+                       return false;
+               }
+       }
+
+       return true;
+}
 
 bool torture_local_replace(struct torture_context *ctx)
 {
@@ -1113,6 +1145,7 @@ bool torture_local_replace(struct torture_context *ctx)
        ret &= test_utime();
        ret &= test_utimes();
        ret &= test_memmem();
+       ret &= test_closefrom();
 
        return ret;
 }