/*
- * "$Id: testfile.c 6962 2007-09-17 20:35:47Z mike $"
+ * "$Id: testfile.c 7720 2008-07-11 22:46:21Z mike $"
*
- * File test program for the Common UNIX Printing System (CUPS).
+ * File test program for CUPS.
*
- * Copyright 2007 by Apple Inc.
+ * Copyright 2007-2011 by Apple Inc.
* Copyright 1997-2007 by Easy Software Products.
*
* These coded instructions, statements, and computer programs are the
* Contents:
*
* 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.
*/
* Include necessary headers...
*/
-#include <stdio.h>
+#include "string-private.h"
+#include "debug-private.h"
+#include "file.h"
#include <stdlib.h>
-#include <errno.h>
#include <time.h>
-#include "string.h"
-#include "file.h"
-#include "debug.h"
#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 psglyphs, rewind, then count again.
+ */
+
+ fputs("\ncupsFileOpen(\"../data/media.defs\", \"r\"): ", stdout);
+
+ if ((fp = cupsFileOpen("../data/media.defs", "r")) == NULL)
+ {
+ puts("FAIL");
+ status ++;
+ }
+ else
+ {
+ puts("PASS");
+ fputs("cupsFileGets: ", stdout);
+
+ if ((count = count_lines(fp)) != 208)
+ {
+ printf("FAIL (got %d lines, expected 208)\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)) != 208)
+ {
+ printf("FAIL (got %d lines, expected 208)\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)))
* 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...
*/
-#ifdef WIN32
- srand((unsigned)time(NULL));
-#else
- srand(time(NULL));
-#endif /* WIN32 */
+ CUPS_SRAND((unsigned)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)
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)
for (i = 0; i < 1000; i ++)
if (!cupsFileGetConf(fp, line, sizeof(line), &value, &linenum))
break;
- else if (strcasecmp(line, "TestLine") || !value || atoi(value) != i ||
+ else if (_cups_strcasecmp(line, "TestLine") || !value || atoi(value) != i ||
linenum != (i + 2))
break;
fputs("cupsFileGetChar(): ", stdout);
-#ifdef DEBUG
- puts("\ni byte\n----- -----");
-
- for (i = 0; i < 256; i ++)
- {
- byte = cupsFileGetChar(fp);
-
- printf("%-5d %-5d\n", i, byte);
-
- if (byte != i)
- break;
- }
-#else
for (i = 0; i < 256; i ++)
if ((byte = cupsFileGetChar(fp)) != i)
break;
-#endif /* DEBUG */
if (i >= 256)
puts("PASS");
fputs("cupsFileGetChar(partial line): ", stdout);
- for (i = 0; i < strlen(partial_line); i ++)
+ for (i = 0; i < (int)strlen(partial_line); i ++)
if ((byte = cupsFileGetChar(fp)) < 0)
break;
else if (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 6962 2007-09-17 20:35:47Z mike $".
+ * End of "$Id: testfile.c 7720 2008-07-11 22:46:21Z mike $".
*/