/*
- * "$Id: testfile.c 177 2006-06-21 00:20:03Z jlovell $"
+ * "$Id: testfile.c 7720 2008-07-11 22:46:21Z mike $"
*
* File test program for the Common UNIX Printing System (CUPS).
*
- * Copyright 1997-2005 by Easy Software Products.
+ * Copyright 2007-2008 by Apple Inc.
+ * Copyright 1997-2007 by Easy Software Products.
*
* These coded instructions, statements, and computer programs are the
- * property of Easy Software Products and are protected by Federal
- * copyright law. Distribution and use rights are outlined in the file
- * "LICENSE.txt" which should have been included with this file. If this
- * file is missing or damaged please contact Easy Software Products
- * at:
- *
- * Attn: CUPS Licensing Information
- * Easy Software Products
- * 44141 Airport View Drive, Suite 204
- * Hollywood, Maryland 20636 USA
- *
- * Voice: (301) 373-9600
- * EMail: cups-info@cups.org
- * WWW: http://www.cups.org
+ * property of Apple Inc. and are protected by Federal copyright
+ * law. Distribution and use rights are outlined in the file "LICENSE.txt"
+ * which should have been included with this file. If this file is
+ * file is missing or damaged, see the license at "http://www.cups.org/".
*
* This file is subject to the Apple OS-Developed Software exception.
*
* Contents:
*
- * main() - Main entry.
+ * main() - Main entry.
+ * count_lines() - Count the number of lines in a file.
+ * random_tests() - Do random access tests.
+ * read_write_tests() - Perform read/write tests.
*/
/*
#ifdef HAVE_LIBZ
# include <zlib.h>
#endif /* HAVE_LIBZ */
+#ifdef WIN32
+# include <io.h>
+#else
+# include <unistd.h>
+#endif /* WIN32 */
+#include <fcntl.h>
/*
* Local functions...
*/
+static int count_lines(cups_file_t *fp);
+static int random_tests(void);
static int read_write_tests(int compression);
main(int argc, /* I - Number of command-line arguments */
char *argv[]) /* I - Command-line arguments */
{
- int status; /* Exit status */
- char filename[1024]; /* Filename buffer */
+ int status; /* Exit status */
+ char filename[1024]; /* Filename buffer */
+ cups_file_t *fp; /* File pointer */
+#ifndef WIN32
+ int fds[2]; /* Open file descriptors */
+ cups_file_t *fdfile; /* File opened with cupsFileOpenFd() */
+#endif /* !WIN32 */
+ int count; /* Number of lines in file */
if (argc == 1)
status += read_write_tests(1);
#endif /* HAVE_LIBZ */
+ /*
+ * Do uncompressed random I/O tests...
+ */
+
+ status += random_tests();
+
+#ifndef WIN32
+ /*
+ * Test fdopen and close without reading...
+ */
+
+ pipe(fds);
+ close(fds[1]);
+
+ fputs("\ncupsFileOpenFd(fd, \"r\"): ", stdout);
+ fflush(stdout);
+
+ if ((fdfile = cupsFileOpenFd(fds[0], "r")) == NULL)
+ {
+ puts("FAIL");
+ status ++;
+ }
+ else
+ {
+ /*
+ * Able to open file, now close without reading. If we don't return
+ * before the alarm fires, that is a failure and we will crash on the
+ * alarm signal...
+ */
+
+ puts("PASS");
+ fputs("cupsFileClose(no read): ", stdout);
+ fflush(stdout);
+
+ alarm(5);
+ cupsFileClose(fdfile);
+ alarm(0);
+
+ puts("PASS");
+ }
+#endif /* !WIN32 */
+
+ /*
+ * Count lines in euc-jp.txt, rewind, then count again.
+ */
+
+ fputs("\ncupsFileOpen(\"../data/euc-jp.txt\", \"r\"): ", stdout);
+
+ if ((fp = cupsFileOpen("../data/euc-jp.txt", "r")) == NULL)
+ {
+ puts("FAIL");
+ status ++;
+ }
+ else
+ {
+ puts("PASS");
+ fputs("cupsFileGets: ", stdout);
+
+ if ((count = count_lines(fp)) != 15184)
+ {
+ printf("FAIL (got %d lines, expected 15184)\n", count);
+ status ++;
+ }
+ else
+ {
+ puts("PASS");
+ fputs("cupsFileRewind: ", stdout);
+
+ if (cupsFileRewind(fp) != 0)
+ {
+ puts("FAIL");
+ status ++;
+ }
+ else
+ {
+ puts("PASS");
+ fputs("cupsFileGets: ", stdout);
+
+ if ((count = count_lines(fp)) != 15184)
+ {
+ printf("FAIL (got %d lines, expected 15184)\n", count);
+ status ++;
+ }
+ else
+ puts("PASS");
+ }
+ }
+
+ cupsFileClose(fp);
+ }
+
/*
* Test path functions...
*/
- fputs("cupsFileFind: ", stdout);
+ fputs("\ncupsFileFind: ", stdout);
+#ifdef WIN32
+ if (cupsFileFind("notepad.exe", "C:/WINDOWS", 1, filename, sizeof(filename)) &&
+ cupsFileFind("notepad.exe", "C:/WINDOWS;C:/WINDOWS/SYSTEM32", 1, filename, sizeof(filename)))
+#else
if (cupsFileFind("cat", "/bin", 1, filename, sizeof(filename)) &&
cupsFileFind("cat", "/bin:/usr/bin", 1, filename, sizeof(filename)))
+#endif /* WIN32 */
printf("PASS (%s)\n", filename);
else
{
* Cat the filename on the command-line...
*/
- cups_file_t *fp; /* File pointer */
char line[1024]; /* Line from file */
-
if ((fp = cupsFileOpen(argv[1], "r")) == NULL)
{
perror(argv[1]);
}
+/*
+ * 'count_lines()' - Count the number of lines in a file.
+ */
+
+static int /* O - Number of lines */
+count_lines(cups_file_t *fp) /* I - File to read from */
+{
+ int count; /* Number of lines */
+ char line[1024]; /* Line buffer */
+
+
+ for (count = 0; cupsFileGets(fp, line, sizeof(line)); count ++);
+
+ return (count);
+}
+
+
+/*
+ * 'random_tests()' - Do random access tests.
+ */
+
+static int /* O - Status */
+random_tests(void)
+{
+ int status, /* Status of tests */
+ pass, /* Current pass */
+ count, /* Number of records read */
+ record, /* Current record */
+ num_records; /* Number of records */
+ ssize_t pos, /* Position in file */
+ expected; /* Expected position in file */
+ cups_file_t *fp; /* File */
+ char buffer[512]; /* Data buffer */
+
+
+ /*
+ * Run 4 passes, each time appending to a data file and then reopening the
+ * file for reading to validate random records in the file.
+ */
+
+ for (status = 0, pass = 0; pass < 4; pass ++)
+ {
+ /*
+ * cupsFileOpen(append)
+ */
+
+ printf("\ncupsFileOpen(append %d): ", pass);
+
+ if ((fp = cupsFileOpen("testfile.dat", "a")) == NULL)
+ {
+ printf("FAIL (%s)\n", strerror(errno));
+ status ++;
+ break;
+ }
+ else
+ puts("PASS");
+
+ /*
+ * cupsFileTell()
+ */
+
+ expected = 256 * sizeof(buffer) * pass;
+
+ fputs("cupsFileTell(): ", stdout);
+ if ((pos = cupsFileTell(fp)) != expected)
+ {
+ printf("FAIL (" CUPS_LLFMT " instead of " CUPS_LLFMT ")\n",
+ CUPS_LLCAST pos, CUPS_LLCAST expected);
+ status ++;
+ break;
+ }
+ else
+ puts("PASS");
+
+ /*
+ * cupsFileWrite()
+ */
+
+ fputs("cupsFileWrite(256 512-byte records): ", stdout);
+ for (record = 0; record < 256; record ++)
+ {
+ memset(buffer, record, sizeof(buffer));
+ if (cupsFileWrite(fp, buffer, sizeof(buffer)) < sizeof(buffer))
+ break;
+ }
+
+ if (record < 256)
+ {
+ printf("FAIL (%d: %s)\n", record, strerror(errno));
+ status ++;
+ break;
+ }
+ else
+ puts("PASS");
+
+ /*
+ * cupsFileTell()
+ */
+
+ expected += 256 * sizeof(buffer);
+
+ fputs("cupsFileTell(): ", stdout);
+ if ((pos = cupsFileTell(fp)) != expected)
+ {
+ printf("FAIL (" CUPS_LLFMT " instead of " CUPS_LLFMT ")\n",
+ CUPS_LLCAST pos, CUPS_LLCAST expected);
+ status ++;
+ break;
+ }
+ else
+ puts("PASS");
+
+ cupsFileClose(fp);
+
+ /*
+ * cupsFileOpen(read)
+ */
+
+ printf("\ncupsFileOpen(read %d): ", pass);
+
+ if ((fp = cupsFileOpen("testfile.dat", "r")) == NULL)
+ {
+ printf("FAIL (%s)\n", strerror(errno));
+ status ++;
+ break;
+ }
+ else
+ puts("PASS");
+
+ /*
+ * cupsFileSeek, cupsFileRead
+ */
+
+ fputs("cupsFileSeek(), cupsFileRead(): ", stdout);
+
+ for (num_records = (pass + 1) * 256, count = (pass + 1) * 256,
+ record = CUPS_RAND() % num_records;
+ count > 0;
+ count --, record = (record + (CUPS_RAND() & 31) - 16 + num_records) %
+ num_records)
+ {
+ /*
+ * The last record is always the first...
+ */
+
+ if (count == 1)
+ record = 0;
+
+ /*
+ * Try reading the data for the specified record, and validate the
+ * contents...
+ */
+
+ expected = sizeof(buffer) * record;
+
+ if ((pos = cupsFileSeek(fp, expected)) != expected)
+ {
+ printf("FAIL (" CUPS_LLFMT " instead of " CUPS_LLFMT ")\n",
+ CUPS_LLCAST pos, CUPS_LLCAST expected);
+ status ++;
+ break;
+ }
+ else
+ {
+ if (cupsFileRead(fp, buffer, sizeof(buffer)) != sizeof(buffer))
+ {
+ printf("FAIL (%s)\n", strerror(errno));
+ status ++;
+ break;
+ }
+ else if ((buffer[0] & 255) != (record & 255) ||
+ memcmp(buffer, buffer + 1, sizeof(buffer) - 1))
+ {
+ printf("FAIL (Bad Data - %d instead of %d)\n", buffer[0] & 255,
+ record & 255);
+ status ++;
+ break;
+ }
+ }
+ }
+
+ if (count == 0)
+ puts("PASS");
+
+ cupsFileClose(fp);
+ }
+
+ /*
+ * Remove the test file...
+ */
+
+ unlink("testfile.dat");
+
+ /*
+ * Return the test status...
+ */
+
+ return (status);
+}
+
+
/*
* 'read_write_tests()' - Perform read/write tests.
*/
read_write_tests(int compression) /* I - Use compression? */
{
int i; /* Looping var */
- cups_file_t *fp; /* First file */
+ cups_file_t *fp; /* File */
int status; /* Exit status */
char line[1024], /* Line from file */
*value; /* Directive value from line */
unsigned char readbuf[8192], /* Read buffer */
writebuf[8192]; /* Write buffer */
int byte; /* Byte from file */
+ off_t length; /* Length of file */
+ static const char *partial_line = "partial line";
+ /* Partial line */
/*
* Initialize the write buffer with random data...
*/
- srand(time(NULL));
+ CUPS_SRAND(time(NULL));
+
for (i = 0; i < (int)sizeof(writebuf); i ++)
- writebuf[i] = rand();
+ writebuf[i] = CUPS_RAND();
/*
* cupsFileOpen(write)
fputs("cupsFilePrintf(): ", stdout);
for (i = 0; i < 1000; i ++)
- if (cupsFilePrintf(fp, "TestLine %d\n", i) < 0)
+ if (cupsFilePrintf(fp, "TestLine %03d\n", i) < 0)
break;
if (i >= 1000)
fputs("cupsFileWrite(): ", stdout);
- for (i = 0; i < 100; i ++)
+ for (i = 0; i < 10000; i ++)
if (cupsFileWrite(fp, (char *)writebuf, sizeof(writebuf)) < 0)
break;
- if (i >= 100)
+ if (i >= 10000)
+ puts("PASS");
+ else
+ {
+ printf("FAIL (%s)\n", strerror(errno));
+ status ++;
+ }
+
+ /*
+ * cupsFilePuts() with partial line...
+ */
+
+ fputs("cupsFilePuts(\"partial line\"): ", stdout);
+
+ if (cupsFilePuts(fp, partial_line) > 0)
puts("PASS");
else
{
status ++;
}
+ /*
+ * cupsFileTell()
+ */
+
+ fputs("cupsFileTell(): ", stdout);
+
+ if ((length = cupsFileTell(fp)) == 81933283)
+ puts("PASS");
+ else
+ {
+ printf("FAIL (" CUPS_LLFMT " instead of 81933283)\n", CUPS_LLCAST length);
+ status ++;
+ }
+
/*
* cupsFileClose()
*/
* cupsFileOpen(read)
*/
- fputs("cupsFileOpen(read): ", stdout);
+ fputs("\ncupsFileOpen(read): ", stdout);
fp = cupsFileOpen(compression ? "testfile.dat.gz" : "testfile.dat", "r");
if (fp)
}
/*
- * cupsGetChar()
+ * cupsFileGetChar()
*/
- fputs("cupsGetChar(): ", stdout);
+ fputs("cupsFileGetChar(): ", stdout);
for (i = 0; i < 256; i ++)
if ((byte = cupsFileGetChar(fp)) != i)
fputs("cupsFileRead(): ", stdout);
- for (i = 0; i < 100; i ++)
+ for (i = 0; i < 10000; i ++)
if ((byte = cupsFileRead(fp, (char *)readbuf, sizeof(readbuf))) < 0)
break;
else if (memcmp(readbuf, writebuf, sizeof(readbuf)))
break;
- if (i >= 100)
+ if (i >= 10000)
puts("PASS");
else if (byte > 0)
{
status ++;
}
+ /*
+ * cupsFileGetChar() with partial line...
+ */
+
+ fputs("cupsFileGetChar(partial line): ", stdout);
+
+ for (i = 0; i < (int)strlen(partial_line); i ++)
+ if ((byte = cupsFileGetChar(fp)) < 0)
+ break;
+ else if (byte != partial_line[i])
+ break;
+
+ if (!partial_line[i])
+ puts("PASS");
+ else
+ {
+ printf("FAIL (got '%c', expected '%c')\n", byte, partial_line[i]);
+ status ++;
+ }
+
+ /*
+ * cupsFileTell()
+ */
+
+ fputs("cupsFileTell(): ", stdout);
+
+ if ((length = cupsFileTell(fp)) == 81933283)
+ puts("PASS");
+ else
+ {
+ printf("FAIL (" CUPS_LLFMT " instead of 81933283)\n", CUPS_LLCAST length);
+ status ++;
+ }
+
/*
* cupsFileClose()
*/
status ++;
}
+ /*
+ * Remove the test file...
+ */
+
+ unlink(compression ? "testfile.dat.gz" : "testfile.dat");
+
/*
* Return the test status...
*/
/*
- * End of "$Id: testfile.c 177 2006-06-21 00:20:03Z jlovell $".
+ * End of "$Id: testfile.c 7720 2008-07-11 22:46:21Z mike $".
*/