bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

read-file test


From: Bruno Haible
Subject: read-file test
Date: Sat, 24 Mar 2007 17:05:06 +0100
User-agent: KMail/1.5.4

Hi Simon,

A "gnulib-tool --create-testdir --with-tests" directory is giving me this
output during "make check" on NetBSD 3.0:

  Could not read file: No such file or directory
  BAD: out[len] not zero: No such file or directory
  Read -1077940928 from /etc/resolv.conf...
  Read 0 from /dev/null...
  PASS: test-read-file

It gives the impression of a test that encountered several error conditions
and produced wrong results, yet it *passes*!?

The guidelines about tests that I mentioned a week ago permit to improve
this test:

 1) Put into the test itself the knowledge whether something is a failure or
    not. Don't expect the human user/tester to know this.

    Here this means:
      - Is a "Could not read file" expected or not? If the file is absent,
        it is expected, otherwise it is unexpected and should make the
        test fail. This means, we need to test the file's presence with
        stat().
      - Is a "BAD: out[len] not zero" expected or not? It is expected
        if the read_file() function returned NULL, otherwise not. So let's
        use an 'if' in the test.
      - Is a "Read -1077940928 from /etc/resolv.conf..." expected or not?
        It's expected if and only if -1077940928 is the size of the file,
        as determined by stat() (assuming the file is a regular file).
      - Is the "Read 0 from /dev/null" expected or not? With the number 0,
        yes; with any other value, no.

 2) Produce some output only if the encountered results are suspicious.

      - "Read 0 from /dev/null" is not suspicious, it's normal. So let's
        drop this output.

The result of these considerations is this. OK to commit?

Bruno


2007-03-24  Bruno Haible  <address@hidden>

        * tests/test-read-file.c (main): Don't produce spurious output for
        expected situations. Make the test fail if it encountered unexpected
        results.

*** tests/test-read-file.c      17 Mar 2007 19:26:29 -0000      1.3
--- tests/test-read-file.c      24 Mar 2007 15:24:41 -0000
***************
*** 25,30 ****
--- 25,31 ----
  
  #include <stdio.h>
  #include <stdlib.h>
+ #include <sys/stat.h>
  
  #define FILE1 "/etc/resolv.conf"
  #define FILE2 "/dev/null"
***************
*** 32,66 ****
  int
  main (void)
  {
!   {
!     size_t len;
!     char *out = read_file (FILE1, &len);
  
!     if (!out)
!       perror ("Could not read file");
  
!     if (out[len] != '\0')
!       perror ("BAD: out[len] not zero");
! 
!     printf ("Read %ld from %s...\n", (unsigned long) len, FILE1);
! 
!     free (out);
!   }
! 
!   {
!     size_t len;
!     char *out = read_file (FILE2, &len);
! 
!     if (!out)
!       perror ("Could not read file");
! 
!     if (out[len] != '\0')
!       perror ("BAD: out[len] not zero");
! 
!     printf ("Read %ld from %s...\n", (unsigned long) len, FILE2);
! 
!     free (out);
!   }
! 
!   return 0;
  }
--- 33,101 ----
  int
  main (void)
  {
!   struct stat statbuf;
!   int err = 0;
  
!   /* We can perform the test only if the file exists and is readable.
!      Test whether it exists, then assume it is world-readable.  */
!   if (stat (FILE1, &statbuf) >= 0)
!     {
!       size_t len;
!       char *out = read_file (FILE1, &len);
! 
!       if (!out)
!         {
!         perror ("Could not read file");
!         err = 1;
!       }
!       else
!       {
!         if (out[len] != '\0')
!           {
!             perror ("BAD: out[len] not zero");
!             err = 1;
!           }
! 
!         /* Assume FILE1 is a regular file or a symlink to a regular file.  */
!         if (len != statbuf.st_size)
!           {
!             fprintf (stderr, "Read %ld from %s...\n", (unsigned long) len, 
FILE1);
!             err = 1;
!           }
!         free (out);
!       }
!     }
! 
!   /* We can perform the test only if the file exists and is readable.
!      Test whether it exists, then assume it is world-readable.  */
!   if (stat (FILE2, &statbuf) >= 0)
!     {
!       size_t len;
!       char *out = read_file (FILE2, &len);
! 
!       if (!out)
!         {
!         perror ("Could not read file");
!         err = 1;
!       }
!       else
!       {
!         if (out[len] != '\0')
!           {
!             perror ("BAD: out[len] not zero");
!             err = 1;
!           }
! 
!         /* /dev/null should always be empty.  Ignore statbuf.st_size, since it
!            is not a regular file.  */
!         if (len != 0)
!           {
!             fprintf (stderr, "Read %ld from %s...\n", (unsigned long) len, 
FILE2);
!             err = 1;
!           }
!         free (out);
!       }
!     }
  
!   return err;
  }





reply via email to

[Prev in Thread] Current Thread [Next in Thread]