]> git.ipfire.org Git - thirdparty/cups.git/commitdiff
Add directory testing to testfile unit test program.
authorMichael R Sweet <msweet@msweet.org>
Sat, 6 Mar 2021 17:39:12 +0000 (12:39 -0500)
committerMichael R Sweet <msweet@msweet.org>
Sat, 6 Mar 2021 17:39:12 +0000 (12:39 -0500)
cups/testfile.c

index 633415d5f80ce7f0528cb485969230251810d3e7..4efb55f27f44b44afd59d6ee0216aac8af7f9173 100644 (file)
@@ -1,6 +1,7 @@
 /*
- * File test program for CUPS.
+ * File/directory test program for CUPS.
  *
+ * Copyright © 2021 by OpenPrinting.
  * Copyright © 2007-2018 by Apple Inc.
  * Copyright © 1997-2007 by Easy Software Products.
  *
@@ -15,6 +16,7 @@
 #include "string-private.h"
 #include "debug-private.h"
 #include "file.h"
+#include "dir.h"
 #include <stdlib.h>
 #include <time.h>
 #ifdef _WIN32
@@ -43,6 +45,7 @@ main(int  argc,                               /* I - Number of command-line arguments */
      char *argv[])                     /* I - Command-line arguments */
 {
   int          status;                 /* Exit status */
+  int          i;                      /* Looping var */
   char         filename[1024];         /* Filename buffer */
   cups_file_t  *fp;                    /* File pointer */
 #ifndef _WIN32
@@ -180,6 +183,101 @@ main(int  argc,                           /* I - Number of command-line arguments */
       status ++;
     }
 
+   /*
+    * Test directory functions...
+    */
+
+    fputs("\nCreating test directory \"test.d\"...\n", stdout);
+    fputs("mkdir(test.d): ", stdout);
+    if (mkdir("test.d", 0777))
+    {
+      printf("FAIL (%s)\n", strerror(errno));
+      status ++;
+    }
+    else
+    {
+      int              num_files;      // Number of files seen
+      cups_dir_t       *dir;           // Directory pointer
+      cups_dentry_t    *dent;          // Directory entry
+
+      puts("PASS");
+
+      fputs("cupsDirOpen(test.d): ", stdout);
+      if ((dir = cupsDirOpen("test.d")) == NULL)
+      {
+        printf("FAIL (%s)\n", strerror(errno));
+        status ++;
+      }
+      else
+      {
+        puts("PASS");
+        fputs("cupsDirRead: ", stdout);
+        if ((dent = cupsDirRead(dir)) != NULL)
+        {
+          printf("FAIL (Got '%s', expected NULL)\n", dent->filename);
+          status ++;
+        }
+        else
+          puts("PASS");
+
+        cupsDirClose(dir);
+      }
+
+      // Create some files...
+      for (i = 0; i < 10; i ++)
+      {
+        snprintf(filename, sizeof(filename), "test.d/testfile%d.txt", i);
+        printf("cupsFileOpen(%s): ", filename);
+        if ((fp = cupsFileOpen(filename, "w")) == NULL)
+        {
+          printf("FAIL (%s)\n", strerror(errno));
+          status ++;
+          break;
+        }
+        else
+        {
+          puts("PASS");
+          cupsFilePuts(fp, "This is a test.\n");
+          cupsFileClose(fp);
+        }
+      }
+
+      if (i >= 10)
+      {
+       fputs("cupsDirOpen(test.d): ", stdout);
+       if ((dir = cupsDirOpen("test.d")) == NULL)
+       {
+         printf("FAIL (%s)\n", strerror(errno));
+         status ++;
+       }
+       else
+       {
+         puts("PASS");
+         fputs("cupsDirRead: ", stdout);
+         for (num_files = 0; (dent = cupsDirRead(dir)) != NULL; num_files ++)
+           printf("%s ", dent->filename);
+         if (num_files != 10)
+         {
+           printf("FAIL (Got %d files, expected 10)\n", num_files);
+           status ++;
+         }
+         else
+           puts("PASS");
+
+         cupsDirClose(dir);
+       }
+      }
+
+      // Cleanup
+      for (i = 0; i < 10; i ++)
+      {
+        snprintf(filename, sizeof(filename), "test.d/testfile%d.txt", i);
+        unlink(filename);
+      }
+
+      rmdir("test.d");
+    }
+
    /*
     * Summarize the results and return...
     */