1 /*
2  * Copyright (c) 2010, 2015, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 /*
25  * @test
26  * @bug 6502392
27  * @summary Invalid relative names for Filer.createResource and Filer.getResource
28  * @library /tools/javac/lib
29  * @modules java.compiler
30  *          jdk.compiler
31  * @build   JavacTestingAbstractProcessor
32  * @compile TestInvalidRelativeNames.java
33  * @compile/process -processor TestInvalidRelativeNames java.lang.Object
34  */
35 
36 import java.io.*;
37 import java.util.*;
38 import javax.annotation.processing.*;
39 import javax.lang.model.*;
40 import javax.lang.model.element.*;
41 import javax.tools.Diagnostic;
42 import javax.tools.StandardLocation;
43 
44 public class TestInvalidRelativeNames extends JavacTestingAbstractProcessor {
45     enum Kind { CREATE_WRITER, GET_READER, CREATE_OUTPUT_STREAM, GET_INPUT_STREAM };
46 
47     static final String[] invalidRelativeNames = {
48             "/boo", "goo/../hoo", "./ioo", ""
49     };
50 
process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv)51     public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
52         if (roundEnv.processingOver()) {
53             for (String relative: invalidRelativeNames) {
54                 for (Kind kind: Kind.values()) {
55                     test(relative, kind);
56                 }
57             }
58         }
59         return true;
60     }
61 
test(String relative, Kind kind)62     void test(String relative, Kind kind) {
63         System.out.println("test relative path: " + relative + ", kind: " + kind);
64         try {
65             switch (kind) {
66                 case CREATE_WRITER:
67                     Writer writer = filer.createResource(
68                             StandardLocation.SOURCE_OUTPUT, "", relative).openWriter();
69                     writer.close();
70                     break;
71 
72                 case GET_READER:
73                     Reader reader = filer.getResource(
74                             StandardLocation.SOURCE_OUTPUT, "", relative).openReader(true);
75                     reader.close();
76                     break;
77 
78                 case CREATE_OUTPUT_STREAM:
79                     OutputStream out = filer.createResource(
80                             StandardLocation.SOURCE_OUTPUT, "", relative).openOutputStream();
81                     out.close();
82                     break;
83 
84                 case GET_INPUT_STREAM:
85                     InputStream in = filer.createResource(
86                             StandardLocation.SOURCE_OUTPUT, "", relative).openInputStream();
87                     in.close();
88                     break;
89             }
90         } catch (IllegalArgumentException expected) {
91             System.out.println("expected exception thrown: " + expected);
92             return;
93         } catch (Exception e) {
94             messager.printMessage(Diagnostic.Kind.ERROR,
95                     "relative path: " + relative + ", kind: " + kind + ", unexpected exception: " + e);
96             return;
97         }
98         messager.printMessage(Diagnostic.Kind.ERROR,
99                 "relative path: " + relative + ", kind: " + kind + ", no exception thrown");
100     }
101 }
102 
103