You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by py...@apache.org on 2006/08/11 13:36:24 UTC

svn commit: r430760 - in /incubator/harmony/enhanced/classlib/trunk/modules/luni/src: main/java/java/io/FileInputStream.java test/java/tests/api/java/io/FileInputStreamTest.java

Author: pyang
Date: Fri Aug 11 04:36:24 2006
New Revision: 430760

URL: http://svn.apache.org/viewvc?rev=430760&view=rev
Log:
Patch applied for HARMONY-1141([classlib][luni] Exception throwing problems in the constructors of java.io.FileInputStream)

Modified:
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/FileInputStream.java
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/FileInputStreamTest.java

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/FileInputStream.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/FileInputStream.java?rev=430760&r1=430759&r2=430760&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/FileInputStream.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/FileInputStream.java Fri Aug 11 04:36:24 2006
@@ -60,8 +60,10 @@
 	public FileInputStream(File file) throws FileNotFoundException {
 		super();
 		SecurityManager security = System.getSecurityManager();
-		if (security != null)
-			security.checkRead(file.getPath());
+		if (security != null) {
+            String filePath = (null == file ? null : file.getPath());
+            security.checkRead(filePath);
+        }
 		fd = new FileDescriptor();
 		fd.descriptor = fileSystem.open(file.properPath(true),
                 IFileSystem.O_RDONLY);
@@ -107,7 +109,7 @@
 	 *             If the <code>fileName</code> is not found.
 	 */
 	public FileInputStream(String fileName) throws FileNotFoundException {
-		this(new File(fileName));
+        this(null == fileName ? (File)null : new File(fileName));
 	}
 
 	/**
@@ -272,11 +274,12 @@
 	 *             occurs.
 	 */
 	public int read(byte[] buffer, int offset, int count) throws IOException {
-        if (count < 0 || offset < 0 || offset > buffer.length
-                || count > buffer.length - offset) {
+        if (count > buffer.length - offset || count < 0 || offset < 0) {
             throw new IndexOutOfBoundsException();
         }
-
+        if (0 == count) {
+            return 0;
+        }
         openCheck();
         synchronized (repositioningLock) {
             // stdin requires special handling

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/FileInputStreamTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/FileInputStreamTest.java?rev=430760&r1=430759&r2=430760&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/FileInputStreamTest.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/FileInputStreamTest.java Fri Aug 11 04:36:24 2006
@@ -1,4 +1,4 @@
-/* Copyright 1998, 2005 The Apache Software Foundation or its licensors, as applicable
+/* Copyright 1998, 2006 The Apache Software Foundation or its licensors, as applicable
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -19,7 +19,9 @@
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
+import java.io.FilePermission;
 import java.io.IOException;
+import java.security.Permission;
 
 import tests.support.Support_PlatformFile;
 
@@ -191,6 +193,124 @@
 			fail("Exception during read test : " + e.getMessage());
 		}
 	}
+    
+    /**
+     * @tests java.io.FileInputStream#read(byte[], int, int)
+     */
+    public void test_read_$BII_IOException() throws IOException {
+        byte[] buf = new byte[1000];
+        try {
+            is = new java.io.FileInputStream(fileName);
+            is.read(buf, -1, 0);
+            fail("should throw IndexOutOfBoundsException");
+        } catch (IndexOutOfBoundsException e) {
+            // Expected
+        } finally {
+            is.close();
+        }
+
+        try {
+            is = new java.io.FileInputStream(fileName);
+            is.read(buf, 0, -1);
+            fail("should throw IndexOutOfBoundsException");
+        } catch (IndexOutOfBoundsException e) {
+            // Expected
+        } finally {
+            is.close();
+        }
+
+        try {
+            is = new java.io.FileInputStream(fileName);
+            is.read(buf, -1, -1);
+            fail("should throw IndexOutOfBoundsException");
+        } catch (IndexOutOfBoundsException e) {
+            // Expected
+        } finally {
+            is.close();
+        }
+
+        try {
+            is = new java.io.FileInputStream(fileName);
+            is.read(buf, 0, 1001);
+            fail("should throw IndexOutOfBoundsException");
+        } catch (IndexOutOfBoundsException e) {
+            // Expected
+        } finally {
+            is.close();
+        }
+
+        try {
+            is = new java.io.FileInputStream(fileName);
+            is.read(buf, 1001, 0);
+            fail("should throw IndexOutOfBoundsException");
+        } catch (IndexOutOfBoundsException e) {
+            // Expected
+        } finally {
+            is.close();
+        }
+
+        try {
+            is = new java.io.FileInputStream(fileName);
+            is.read(buf, 500, 501);
+            fail("should throw IndexOutOfBoundsException");
+        } catch (IndexOutOfBoundsException e) {
+            // Expected
+        } finally {
+            is.close();
+        }
+        
+        try {
+            is = new java.io.FileInputStream(fileName);
+            is.close();
+            is.read(buf, 0, 100);
+            fail("should throw IOException");
+        } catch (IOException e) {
+            // Expected
+        } finally {
+            is.close();
+        }
+        
+        try {
+            is = new java.io.FileInputStream(fileName);
+            is.close();
+            is.read(buf, 0, 0);
+        } finally {
+            is.close();
+        }
+    }
+
+    /**
+     * @tests java.io.FileInputStream#read(byte[], int, int)
+     */
+    public void test_read_$BII_NullPointerException() throws IOException {
+        byte[] buf = null;
+        try {
+            is = new java.io.FileInputStream(fileName);
+            is.read(buf, -1, 0);
+            fail("should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // Expected
+        } finally {
+            is.close();
+        }
+    }
+
+    /**
+     * @tests java.io.FileInputStream#read(byte[], int, int)
+     */
+    public void test_read_$BII_IndexOutOfBoundsException() throws IOException {
+        byte[] buf = new byte[1000];
+        try {
+            is = new java.io.FileInputStream(fileName);
+            is.close();
+            is.read(buf, -1, -1);
+            fail("should throw IndexOutOfBoundsException");
+        } catch (IndexOutOfBoundsException e) {
+            // Expected
+        } finally {
+            is.close();
+        }
+    }
 
 	/**
 	 * @tests java.io.FileInputStream#skip(long)
@@ -243,7 +363,24 @@
         } catch (IndexOutOfBoundsException e) {}
         fis.close();
     }
-
+    
+    /**
+     * @tests java.io.FileInputStream#FileInputStream(String)
+     */
+    public void test_Constructor_LString_WithSecurityManager() throws IOException {
+        SecurityManager old = System.getSecurityManager();
+        try {
+            MockSecurityManager msm = new MockSecurityManager();
+            System.setSecurityManager(msm);
+            new FileInputStream((String)null);
+            fail("should throw SecurityException");
+        } catch (SecurityException e) {
+            //expected
+        } finally {
+            System.setSecurityManager(old);
+        }
+    }
+    
     /**
      * Sets up the fixture, for example, open a network connection. This method
      * is called before a test is executed.
@@ -275,4 +412,20 @@
 		new File(fileName).delete();
 
 	}
+}
+
+class MockSecurityManager extends SecurityManager {  
+    public void checkPermission(Permission permission) {
+        if (permission instanceof FilePermission) {
+           if (permission.getActions().indexOf("read") == 0)
+               throw new SecurityException();
+        }
+    }
+    
+    public void checkRead(String file) {
+       if( null == file) {
+            file = "";
+        }
+       checkPermission(new FilePermission(file,"read"));
+    }  
 }