You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ec...@apache.org on 2014/05/18 05:25:56 UTC

svn commit: r1595560 - in /commons/proper/vfs/trunk: core/src/main/java/org/apache/commons/vfs2/provider/local/ core/src/test/java/org/apache/commons/vfs2/provider/local/test/ src/changes/

Author: ecki
Date: Sun May 18 03:25:55 2014
New Revision: 1595560

URL: http://svn.apache.org/r1595560
Log:
[VFS-338][Local][Tests] Avoid IndexOutOfBoundsException when validating local file URIs.

Added:
    commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/provider/local/test/WindowsFileNameTests.java   (with props)
Modified:
    commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/provider/local/WindowsFileNameParser.java
    commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/provider/local/test/LocalProviderTestCase.java
    commons/proper/vfs/trunk/src/changes/changes.xml

Modified: commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/provider/local/WindowsFileNameParser.java
URL: http://svn.apache.org/viewvc/commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/provider/local/WindowsFileNameParser.java?rev=1595560&r1=1595559&r2=1595560&view=diff
==============================================================================
--- commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/provider/local/WindowsFileNameParser.java (original)
+++ commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/provider/local/WindowsFileNameParser.java Sun May 18 03:25:55 2014
@@ -59,7 +59,7 @@ public class WindowsFileNameParser exten
         for (; startPos < maxlen && name.charAt(startPos) == '/'; startPos++)
         {
         }
-        if (startPos == maxlen && name.length() > startPos && name.charAt(startPos + 1) == '/')
+        if (startPos == maxlen && name.length() > (startPos + 1) && name.charAt(startPos + 1) == '/')
         {
             // Too many '/'
             throw new FileSystemException("vfs.provider.local/not-absolute-file-name.error", uri);

Modified: commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/provider/local/test/LocalProviderTestCase.java
URL: http://svn.apache.org/viewvc/commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/provider/local/test/LocalProviderTestCase.java?rev=1595560&r1=1595559&r2=1595560&view=diff
==============================================================================
--- commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/provider/local/test/LocalProviderTestCase.java (original)
+++ commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/provider/local/test/LocalProviderTestCase.java Sun May 18 03:25:55 2014
@@ -27,6 +27,7 @@ import org.apache.commons.vfs2.test.Abst
 import org.apache.commons.vfs2.test.PermissionsTests;
 import org.apache.commons.vfs2.test.ProviderTestConfig;
 import org.apache.commons.vfs2.test.ProviderTestSuite;
+import org.apache.commons.vfs2.util.Os;
 
 /**
  * Tests for the local file system.
@@ -47,6 +48,11 @@ public class LocalProviderTestCase
         testSuite.addTests(UrlTests.class);
         testSuite.addTests(PermissionsTests.class);
 
+        if (Os.isFamily(Os.OS_FAMILY_WINDOWS))
+        {
+            testSuite.addTests(WindowsFileNameTests.class);
+        }
+
         return testSuite;
     }
 

Added: commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/provider/local/test/WindowsFileNameTests.java
URL: http://svn.apache.org/viewvc/commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/provider/local/test/WindowsFileNameTests.java?rev=1595560&view=auto
==============================================================================
--- commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/provider/local/test/WindowsFileNameTests.java (added)
+++ commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/provider/local/test/WindowsFileNameTests.java Sun May 18 03:25:55 2014
@@ -0,0 +1,110 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.vfs2.provider.local.test;
+
+import org.apache.commons.vfs2.FileName;
+import org.apache.commons.vfs2.FileObject;
+import org.apache.commons.vfs2.FileSystemException;
+import org.apache.commons.vfs2.provider.local.WindowsFileName;
+import org.apache.commons.vfs2.test.AbstractProviderTestCase;
+
+/**
+ * Additional naming tests for local file system.
+ * <p>
+ * Only executed on Windows O/S.
+ */
+public class WindowsFileNameTests
+    extends AbstractProviderTestCase
+{
+    public void testWindowsRoots() throws Exception
+    {
+        // valid URI forms of the filesystem root
+        String[] tests = new String[] { "file:///C:/", "file://C:/", "file:/C:/", "file:C:/" };
+
+        for(String name : tests)
+        {
+            FileName fn = getManager().resolveFile(name).getName();
+
+            // the following tests work for Windows file names only
+            assertSame(WindowsFileName.class, fn.getClass());
+
+            // all should result in the same FileName
+            assertEquals("file:///C:/", fn.toString());
+            assertEquals("/", fn.getPath());
+            assertEquals("/", fn.getPathDecoded());
+            assertEquals("file:///C:/", fn.getRootURI());
+            assertEquals("file:///C:/", fn.getFriendlyURI());
+
+            assertEquals("file:///C:/", fn.getRoot().toString());
+
+            assertEquals("", fn.getExtension());
+            assertEquals("", fn.getBaseName());
+        }
+    }
+
+    public void testWindowsWrongRoots() throws Exception
+    {
+        String[] tests = new String[] { "file:///C:", "file://C:", "file:/C:", "file:C:" };
+
+        for(String name : tests)
+        {
+            try
+            {
+                FileName fn = getManager().resolveFile(name).getName();
+                fail("should not accept root " + name);
+            }
+            catch (FileSystemException ex)
+            {
+                assertEquals("vfs.provider/invalid-absolute-uri.error", ex.getCode());
+                assertTrue(ex.toString().indexOf(name) >= 0);
+            }
+        }
+    }
+
+    public void testWindowsFilenameUNCStartError() throws Exception
+    {
+        try
+        {
+            final String FILE = "file://///";
+            FileObject fo = getManager().resolveFile(FILE);
+            fail("Windows File Parser should not allow " + FILE + " " + fo);
+        }
+        catch(FileSystemException ex)
+        {
+            assertEquals("Exception code", "vfs.provider/invalid-absolute-uri.error", ex.getCode());
+            ex = (FileSystemException)ex.getCause();
+            assertEquals("Exception code", "vfs.provider.local/missing-share-name.error", ex.getCode());
+        }
+    }
+
+    public void testWindowsFilenameParserError() throws Exception
+    {
+        // check VFS-338 with 2+4 slashes we want a dedicated error
+        try
+        {
+            final String FILE = "file://////";
+            FileObject fo = getManager().resolveFile(FILE);
+            fail("Windows File Parser should not allow " + FILE + " " + fo);
+        }
+        catch(FileSystemException ex)
+        {
+            assertEquals("Exception code", "vfs.provider/invalid-absolute-uri.error", ex.getCode());
+            ex = (FileSystemException)ex.getCause();
+            assertEquals("Exception code", "vfs.provider.local/not-absolute-file-name.error", ex.getCode());
+        }
+    }
+}

Propchange: commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/provider/local/test/WindowsFileNameTests.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: commons/proper/vfs/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/vfs/trunk/src/changes/changes.xml?rev=1595560&r1=1595559&r2=1595560&view=diff
==============================================================================
--- commons/proper/vfs/trunk/src/changes/changes.xml (original)
+++ commons/proper/vfs/trunk/src/changes/changes.xml Sun May 18 03:25:55 2014
@@ -26,6 +26,9 @@
 <!--       <action issue="VFS-443" dev="ggregory" type="update" due-to="nickallen"> -->
 <!--     	[Local] Need an easy way to convert from a FileObject to a File. -->
 <!--       </action> -->
+      <action issue="VFS-338" dev="ecki" type="fix" due-to="Daniel R.">
+       [Local][Tests] Avoid IndexOutOfBoundsException when validating local file URIs.
+      </action>
       <action issue="VFS-526" dev="ecki" type="update">
        [HDFS][Tests] Enable HDFS testing on Windows (remove Maven profile "hdfs")
       </action>