6502392: Invalid relative names for Filer.createResource and Filer.getResource

Wed, 29 Sep 2010 14:01:37 -0700

author
jjg
date
Wed, 29 Sep 2010 14:01:37 -0700
changeset 698
f94af0667151
parent 697
28b021bb889f
child 699
d2aaaec153e8

6502392: Invalid relative names for Filer.createResource and Filer.getResource
Reviewed-by: darcy

src/share/classes/com/sun/tools/javac/file/JavacFileManager.java file | annotate | diff | comparison | revisions
test/tools/javac/processing/filer/TestInvalidRelativeNames.java file | annotate | diff | comparison | revisions
     1.1 --- a/src/share/classes/com/sun/tools/javac/file/JavacFileManager.java	Tue Sep 28 22:46:36 2010 +0530
     1.2 +++ b/src/share/classes/com/sun/tools/javac/file/JavacFileManager.java	Wed Sep 29 14:01:37 2010 -0700
     1.3 @@ -1,5 +1,5 @@
     1.4  /*
     1.5 - * Copyright (c) 2005, 2009, Oracle and/or its affiliates. All rights reserved.
     1.6 + * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
     1.7   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.8   *
     1.9   * This code is free software; you can redistribute it and/or modify it
    1.10 @@ -605,7 +605,7 @@
    1.11          nullCheck(className);
    1.12          nullCheck(kind);
    1.13          if (!sourceOrClass.contains(kind))
    1.14 -            throw new IllegalArgumentException("Invalid kind " + kind);
    1.15 +            throw new IllegalArgumentException("Invalid kind: " + kind);
    1.16          return getFileForInput(location, RelativeFile.forClass(className, kind));
    1.17      }
    1.18  
    1.19 @@ -658,7 +658,7 @@
    1.20          nullCheck(className);
    1.21          nullCheck(kind);
    1.22          if (!sourceOrClass.contains(kind))
    1.23 -            throw new IllegalArgumentException("Invalid kind " + kind);
    1.24 +            throw new IllegalArgumentException("Invalid kind: " + kind);
    1.25          return getFileForOutput(location, RelativeFile.forClass(className, kind), sibling);
    1.26      }
    1.27  
    1.28 @@ -672,7 +672,7 @@
    1.29          // validatePackageName(packageName);
    1.30          nullCheck(packageName);
    1.31          if (!isRelativeUri(relativeName))
    1.32 -            throw new IllegalArgumentException("relativeName is invalid");
    1.33 +            throw new IllegalArgumentException("Invalid relative name: " + relativeName);
    1.34          RelativeFile name = packageName.length() == 0
    1.35              ? new RelativeFile(relativeName)
    1.36              : new RelativeFile(RelativeDirectory.forPackage(packageName), relativeName);
    1.37 @@ -806,6 +806,8 @@
    1.38          String path = uri.normalize().getPath();
    1.39          if (path.length() == 0 /* isEmpty() is mustang API */)
    1.40              return false;
    1.41 +        if (!path.equals(uri.getPath())) // implicitly checks for embedded . and ..
    1.42 +            return false;
    1.43          char first = path.charAt(0);
    1.44          return first != '.' && first != '/';
    1.45      }
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/test/tools/javac/processing/filer/TestInvalidRelativeNames.java	Wed Sep 29 14:01:37 2010 -0700
     2.3 @@ -0,0 +1,115 @@
     2.4 +/*
     2.5 + * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
     2.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     2.7 + *
     2.8 + * This code is free software; you can redistribute it and/or modify it
     2.9 + * under the terms of the GNU General Public License version 2 only, as
    2.10 + * published by the Free Software Foundation.
    2.11 + *
    2.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    2.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    2.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    2.15 + * version 2 for more details (a copy is included in the LICENSE file that
    2.16 + * accompanied this code).
    2.17 + *
    2.18 + * You should have received a copy of the GNU General Public License version
    2.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    2.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    2.21 + *
    2.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    2.23 + * or visit www.oracle.com if you need additional information or have any
    2.24 + * questions.
    2.25 + */
    2.26 +
    2.27 +/*
    2.28 + * @test
    2.29 + * @bug 6502392
    2.30 + * @summary Invalid relative names for Filer.createResource and Filer.getResource
    2.31 + * @compile TestInvalidRelativeNames.java
    2.32 + * @compile/process -processor TestInvalidRelativeNames java.lang.Object
    2.33 + */
    2.34 +
    2.35 +import java.io.*;
    2.36 +import java.util.*;
    2.37 +import javax.annotation.processing.*;
    2.38 +import javax.lang.model.*;
    2.39 +import javax.lang.model.element.*;
    2.40 +import javax.tools.Diagnostic;
    2.41 +import javax.tools.StandardLocation;
    2.42 +
    2.43 +
    2.44 +@SupportedAnnotationTypes("*")
    2.45 +public class TestInvalidRelativeNames extends AbstractProcessor {
    2.46 +    enum Kind { CREATE_WRITER, GET_READER, CREATE_OUTPUT_STREAM, GET_INPUT_STREAM };
    2.47 +
    2.48 +    static final String[] invalidRelativeNames = {
    2.49 +            "/boo", "goo/../hoo", "./ioo", ""
    2.50 +    };
    2.51 +
    2.52 +    @Override
    2.53 +    public SourceVersion getSupportedSourceVersion() {
    2.54 +        return SourceVersion.latest();
    2.55 +    }
    2.56 +
    2.57 +    Filer filer;
    2.58 +    Messager messager;
    2.59 +
    2.60 +    @Override
    2.61 +    public void init(ProcessingEnvironment pEnv) {
    2.62 +        super.init(pEnv);
    2.63 +        filer = processingEnv.getFiler();
    2.64 +        messager = processingEnv.getMessager();
    2.65 +    }
    2.66 +
    2.67 +    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    2.68 +        if (roundEnv.processingOver()) {
    2.69 +            for (String relative: invalidRelativeNames) {
    2.70 +                for (Kind kind: Kind.values()) {
    2.71 +                    test(relative, kind);
    2.72 +                }
    2.73 +            }
    2.74 +        }
    2.75 +        return true;
    2.76 +    }
    2.77 +
    2.78 +    void test(String relative, Kind kind) {
    2.79 +        System.out.println("test relative path: " + relative + ", kind: " + kind);
    2.80 +        try {
    2.81 +            switch (kind) {
    2.82 +                case CREATE_WRITER:
    2.83 +                    Writer writer = filer.createResource(
    2.84 +                            StandardLocation.SOURCE_OUTPUT, "", relative).openWriter();
    2.85 +                    writer.close();
    2.86 +                    break;
    2.87 +
    2.88 +                case GET_READER:
    2.89 +                    Reader reader = filer.getResource(
    2.90 +                            StandardLocation.SOURCE_OUTPUT, "", relative).openReader(true);
    2.91 +                    reader.close();
    2.92 +                    break;
    2.93 +
    2.94 +                case CREATE_OUTPUT_STREAM:
    2.95 +                    OutputStream out = filer.createResource(
    2.96 +                            StandardLocation.SOURCE_OUTPUT, "", relative).openOutputStream();
    2.97 +                    out.close();
    2.98 +                    break;
    2.99 +
   2.100 +                case GET_INPUT_STREAM:
   2.101 +                    InputStream in = filer.createResource(
   2.102 +                            StandardLocation.SOURCE_OUTPUT, "", relative).openInputStream();
   2.103 +                    in.close();
   2.104 +                    break;
   2.105 +            }
   2.106 +        } catch (IllegalArgumentException expected) {
   2.107 +            System.out.println("expected exception thrown: " + expected);
   2.108 +            return;
   2.109 +        } catch (Exception e) {
   2.110 +            messager.printMessage(Diagnostic.Kind.ERROR,
   2.111 +                    "relative path: " + relative + ", kind: " + kind + ", unexpected exception: " + e);
   2.112 +            return;
   2.113 +        }
   2.114 +        messager.printMessage(Diagnostic.Kind.ERROR,
   2.115 +                "relative path: " + relative + ", kind: " + kind + ", no exception thrown");
   2.116 +    }
   2.117 +}
   2.118 +

mercurial