Changeset 3778


Ignore:
Timestamp:
Sep 25, 2007, 12:11:51 PM (15 years ago)
Author:
Nicklas Nordborg
Message:

Fixes #786: Path constructor doesn't validate the filename part of the path

Location:
branches/2.4-stable/src
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/2.4-stable/src/core/net/sf/basedb/core/InvalidPathException.java

    r3679 r3778  
    4343  public InvalidPathException(String path)
    4444  {
    45     super("Invalid path: " + path);
     45    super(path);
    4646  }
    4747
  • branches/2.4-stable/src/core/net/sf/basedb/core/Path.java

    r3679 r3778  
    143143      }
    144144      filename = parts[parts.length-1];
     145      if (!Path.isValidName(filename))
     146      {
     147        throw new InvalidPathException("Path contains invalid characters ("+Path.INVALID_CHARACTERS+"): "+path);
     148      }
    145149      parts[parts.length-1] = "";
    146150    }
  • branches/2.4-stable/src/test/TestPath.java

    r3679 r3778  
    4242  {
    4343    write("++Testing path");
    44     test_create("/filename", Path.Type.FILE);
    45     test_create("/directory/filename", Path.Type.FILE);
    46     test_create("/directory/subdirectory/filename", Path.Type.FILE);
    47     test_create("~userlogin/filename", Path.Type.FILE);
    48     test_create("~userlogin/directory/filename", Path.Type.FILE);
    49     test_create("~userlogin/directory/subdirectory/filename", Path.Type.FILE);
    50     test_create("~/filename", Path.Type.FILE);
    51     test_create("~/directory/filename", Path.Type.FILE);
    52     test_create("~/directory/subdirectory/filename", Path.Type.FILE);
     44    test_create("/filename", Path.Type.FILE, false);
     45    test_create("/directory/filename", Path.Type.FILE, false);
     46    test_create("/directory/subdirectory/filename", Path.Type.FILE, false);
     47    test_create("~userlogin/filename", Path.Type.FILE, false);
     48    test_create("~userlogin/directory/filename", Path.Type.FILE, false);
     49    test_create("~userlogin/directory/subdirectory/filename", Path.Type.FILE, false);
     50    test_create("~/filename", Path.Type.FILE, false);
     51    test_create("~/directory/filename", Path.Type.FILE, false);
     52    test_create("~/directory/subdirectory/filename", Path.Type.FILE, false);
    5353
    54     test_create("/", Path.Type.DIRECTORY);
    55     test_create("/directory", Path.Type.DIRECTORY);
    56     test_create("/directory/subdirectory", Path.Type.DIRECTORY);
    57     test_create("/directory/subdirectory/subsub", Path.Type.DIRECTORY);
    58     test_create("~userlogin", Path.Type.DIRECTORY);
    59     test_create("~userlogin/directory", Path.Type.DIRECTORY);
    60     test_create("~userlogin/directory/subdirectory", Path.Type.DIRECTORY);
    61     test_create("~userlogin/directory/subdirectory/subsub", Path.Type.DIRECTORY);
    62     test_create("~", Path.Type.DIRECTORY);
    63     test_create("~/directory", Path.Type.DIRECTORY);
    64     test_create("~/directory/subdirectory", Path.Type.DIRECTORY);
    65     test_create("~/directory/subdirectory/subsub", Path.Type.DIRECTORY);
     54    test_create("/", Path.Type.DIRECTORY, false);
     55    test_create("/directory", Path.Type.DIRECTORY, false);
     56    test_create("/directory/subdirectory", Path.Type.DIRECTORY, false);
     57    test_create("/directory/subdirectory/subsub", Path.Type.DIRECTORY, false);
     58    test_create("~userlogin", Path.Type.DIRECTORY, false);
     59    test_create("~userlogin/directory", Path.Type.DIRECTORY, false);
     60    test_create("~userlogin/directory/subdirectory", Path.Type.DIRECTORY, false);
     61    test_create("~userlogin/directory/subdirectory/subsub", Path.Type.DIRECTORY, false);
     62    test_create("~", Path.Type.DIRECTORY, false);
     63    test_create("~/directory", Path.Type.DIRECTORY, false);
     64    test_create("~/directory/subdirectory", Path.Type.DIRECTORY, false);
     65    test_create("~/directory/subdirectory/subsub", Path.Type.DIRECTORY, false);
    6666
    6767    test_valid_name("abc", true);
     
    7777    test_valid_name("|abc", false);
    7878   
     79    test_create("/file<name>", Path.Type.FILE, true);
     80    test_create("/dire<ctory>/filename", Path.Type.FILE, true);
     81    test_create("/directory/file<name>", Path.Type.FILE, true);
     82    test_create("/sub<dir>", Path.Type.DIRECTORY, true);
     83    test_create("/directory/sub<dir>", Path.Type.DIRECTORY, true);
     84   
    7985    test_replace_invalid("abc", ".", "abc");
    8086    test_replace_invalid("~abc", ".", ".abc");
     
    8591  }
    8692 
    87   static void test_create(String path, Path.Type type)
     93  static void test_create(String path, Path.Type type, boolean shouldFail)
    8894  {
    8995    try
     
    95101        throw new Exception("Original path ("+path+") is different from path: "+pp);
    96102      }
    97       write("--Path " + p.getType() + ": '"+p+"' OK");
     103      if (shouldFail)
     104      {
     105        write("--Path '"+path+"' FAILED");
     106        new Throwable("Expected test to fail but it didn't.").printStackTrace();
     107        ok = false;
     108      }
     109      else
     110      {
     111        write("--Path " + p.getType() + ": '"+p+"' OK");
     112      }
    98113    }
    99114    catch (Throwable ex)
    100115    {
    101       write("--Path '"+path+"' FAILED");
    102       ex.printStackTrace();
    103       ok = false;
     116      if (!shouldFail)
     117      {
     118        write("--Path '"+path+"' FAILED");
     119        ex.printStackTrace();
     120        ok = false;
     121      }
     122      else
     123      {
     124        write("--Path " + type + ": '"+path+"' OK");
     125      }
    104126    }
    105127  }
Note: See TracChangeset for help on using the changeset viewer.