Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4362120

logging APIs: FileHandler constructor allows to exceed file length limit

XMLWordPrintable

    • beta
    • sparc
    • solaris_7
    • Verified



      Name: szR10032 Date: 08/11/2000



      The testbase_nsk/src/nsk/logging/FileHandler/FileHandler_sii/fhnd_sii001
      test shows some contradiction between the behaviour under the using
      public FileHandler(java.lang.String pattern, int limit, int count)
      and its semantics as it is described by the spec.
      The test creates FileHandler with pattern="fhnd_sii001.log.%g", limit=7 and count=7
      then logs 10 INFO messages, using Logger.info() method. The length of each
      message is equal to 10 bytes. According to spec ("When the given limit has been written
      to one file, another file will be opened.") we should form 7 log files
      of length 7. However, we obtain 6 files with full messages (10 bytes),
      and first fhnd_sii001.log.0 file of length 0.

      LOG:
      ====
      novo7% /export/ld24/java/hotspot/Solaris_JDK_1.3/bin/java -version
      java version "1.3.0"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0-RC)
      Java HotSpot(TM) Client VM (build 1.3.0-RC, mixed mode)
      novo7% /export/ld24/java/hotspot/Solaris_JDK_1.3/bin/javac -d . fhnd_sii001.java
      novo7% /export/ld24/java/hotspot/Solaris_JDK_1.3/bin/java -Xbootclasspath/a:/export/home/_FOR_REMOVING/logging/classes logging.FileHandler.FileHandler_sii.fhnd_sii001
      FAILURE: (1) log file with empty info is found: fhnd_sii001.log.0
      novo7% ls -l
      total ...
      -rw-r--r-- 1 zss java 996 Aug 11 12:06 fhnd_sii001.README
      -rw-r--r-- 1 zss java 137 Aug 11 18:10 fhnd_sii001.cfg
      -rw-r--r-- 1 zss java 5282 Aug 11 18:00 fhnd_sii001.java
      -rw-r--r-- 1 zss java 0 Aug 11 18:01 fhnd_sii001.log.0
      -rw-r--r-- 1 zss java 10 Aug 11 18:01 fhnd_sii001.log.1
      -rw-r--r-- 1 zss java 10 Aug 11 18:01 fhnd_sii001.log.2
      -rw-r--r-- 1 zss java 10 Aug 11 18:01 fhnd_sii001.log.3
      -rw-r--r-- 1 zss java 10 Aug 11 18:01 fhnd_sii001.log.4
      -rw-r--r-- 1 zss java 10 Aug 11 18:01 fhnd_sii001.log.5
      -rw-r--r-- 1 zss java 10 Aug 11 18:01 fhnd_sii001.log.6
      drwxr-xr-x 3 zss java 512 Aug 11 12:34 logging
      novo7%

      SOURCE:
      =======
      package logging.FileHandler.FileHandler_sii;

      import java.util.logging.LogManager;
      import java.util.logging.FileHandler;
      import java.util.logging.Logger;
      import java.util.logging.Level;
      import java.util.logging.Formatter;
      import java.util.logging.LogRecord;

      import java.io.PrintStream;
      import java.io.FileInputStream;
      import java.io.InputStream;
      import java.io.DataInputStream;
      import java.io.EOFException;
      import java.io.File;

      public class fhnd_sii001 {
          static final String pattern = "fhnd_sii001.log.%g"; // pattern of log file names
          static final int limit = 7; // limit of bytes in single log file
          static final int count = 7; // count of single log files
          static int res = 0;
          static LogManager manager;
          static FileHandler fh;
          static Logger lggr;
          static PrintStream sout = null;

          public static void main( String argv[] ) {
              System.exit(run(argv, System.out)+95); // JCK-compatible exit status
          }
          
          public static int run(String argv[],PrintStream out) {
              sout = out;
              fhnd_sii001 test = new fhnd_sii001();

              try {
                  test.initit();
                  test.doit();
                  res = test.checkit();
              } catch (SecurityException ex) {
                  // If security manager exists and
                  // 1. access to pattern file is forbidden and/or
                  // 2. lAPIs methods callers do not have LoggingPermission("control")
                  out.println(ex);
                  return 0; //TEST PASSED
              } catch (Exception ex) {
                  out.println(ex);
                  return 2; //TEST FAILED
              }

              if (res == 7) {
                  return 0; //TEST PASSED
              }

              return 2; //TEST FAILED
          }

          private void doit() throws Exception {
              for (int i=0;i<10;i++) {
                  lggr.info(i+"123456789");
                  fh.flush();
              }

              fh.close();
          }

          private int checkit() throws Exception {
              String ln = null;
              int cnt1 = 0;
              int cnt2;

              for (int i=0;i<7;i++) {
                  FileInputStream fis = new FileInputStream("fhnd_sii001.log."+i);
                  DataInputStream dis = new DataInputStream((InputStream) fis);

                  cnt2 = 0;
                  for (;;) {
                      ln = dis.readLine();

                      if (ln == null && cnt2 == 1) {
                          break;
                      } else if (ln == null && cnt2 == 0) {
                          sout.println("FAILURE: (1) log file with empty info is found: fhnd_sii001.log."+i);
                          fis.close();
                          return 0;
                      }

                      cnt2++;
                      if (cnt2 > 1) {
                          sout.println("FAILURE: (2) log file with superfluous info is found: fhnd_sii001.log."+i);
                          fis.close();
                          return 0;
                      }

                      if (ln.indexOf(i+"123456") != -1 && ln.length() == 7) {
                          cnt1++;
                      } else {
                          sout.println("FAILURE: (3) log file with superfluous info is found: fhnd_sii001.log."+i);
                          fis.close();
                          return 0;
                      }

                      fis.close();
                  }
              }


              File fl = new File("fhnd_sii001.log."+"7");
              if (fl.exists()) {
                  sout.println("FAILURE: (4) superfluous log file is found: fhnd_sii001.log.7");
                  return 0;
              }

              return cnt1;
          }

          private void initit() throws Exception {
              for (int i=0;i<10;i++) {
                  File f = new File("fhnd_sii001.log."+i);
                  f.delete();
              }

              manager = LogManager.getLogManager();
              manager.removeAllGlobalHandlers();

              fh = new FileHandler(pattern, limit, count);
              fh.setFormatter(new fhnd_sii001f());
              fh.setLevel(Level.INFO);

              lggr = Logger.getLogger("logging.FileHandler.FileHandler_sii.fhnd_sii001.one");
              lggr.addHandler(fh);
              lggr.setLevel(Level.INFO);
          }
      }


      class fhnd_sii001f extends Formatter {
          public java.lang.String format(LogRecord record) {
              return record.message; // returns only raw log message
          }
      }

      ======================================================================

            ghamiltosunw Graham Hamilton (Inactive)
            zsssunw Zss Zss (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: