1 /*
   2  * Copyright (c) 2010, 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  * @compile TestInvalidRelativeNames.java
  29  * @compile/process -processor TestInvalidRelativeNames java.lang.Object
  30  */
  31 
  32 import java.io.*;
  33 import java.util.*;
  34 import javax.annotation.processing.*;
  35 import javax.lang.model.*;
  36 import javax.lang.model.element.*;
  37 import javax.tools.Diagnostic;
  38 import javax.tools.StandardLocation;
  39 
  40 
  41 @SupportedAnnotationTypes("*")
  42 public class TestInvalidRelativeNames extends AbstractProcessor {
  43     enum Kind { CREATE_WRITER, GET_READER, CREATE_OUTPUT_STREAM, GET_INPUT_STREAM };
  44 
  45     static final String[] invalidRelativeNames = {
  46             "/boo", "goo/../hoo", "./ioo", ""
  47     };
  48 
  49     @Override
  50     public SourceVersion getSupportedSourceVersion() {
  51         return SourceVersion.latest();
  52     }
  53 
  54     Filer filer;
  55     Messager messager;
  56 
  57     @Override
  58     public void init(ProcessingEnvironment pEnv) {
  59         super.init(pEnv);
  60         filer = processingEnv.getFiler();
  61         messager = processingEnv.getMessager();
  62     }
  63 
  64     public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
  65         if (roundEnv.processingOver()) {
  66             for (String relative: invalidRelativeNames) {
  67                 for (Kind kind: Kind.values()) {
  68                     test(relative, kind);
  69                 }
  70             }
  71         }
  72         return true;
  73     }
  74 
  75     void test(String relative, Kind kind) {
  76         System.out.println("test relative path: " + relative + ", kind: " + kind);
  77         try {
  78             switch (kind) {
  79                 case CREATE_WRITER:
  80                     Writer writer = filer.createResource(
  81                             StandardLocation.SOURCE_OUTPUT, "", relative).openWriter();
  82                     writer.close();
  83                     break;
  84 
  85                 case GET_READER:
  86                     Reader reader = filer.getResource(
  87                             StandardLocation.SOURCE_OUTPUT, "", relative).openReader(true);
  88                     reader.close();
  89                     break;
  90 
  91                 case CREATE_OUTPUT_STREAM:
  92                     OutputStream out = filer.createResource(
  93                             StandardLocation.SOURCE_OUTPUT, "", relative).openOutputStream();
  94                     out.close();
  95                     break;
  96 
  97                 case GET_INPUT_STREAM:
  98                     InputStream in = filer.createResource(
  99                             StandardLocation.SOURCE_OUTPUT, "", relative).openInputStream();
 100                     in.close();
 101                     break;
 102             }
 103         } catch (IllegalArgumentException expected) {
 104             System.out.println("expected exception thrown: " + expected);
 105             return;
 106         } catch (Exception e) {
 107             messager.printMessage(Diagnostic.Kind.ERROR,
 108                     "relative path: " + relative + ", kind: " + kind + ", unexpected exception: " + e);
 109             return;
 110         }
 111         messager.printMessage(Diagnostic.Kind.ERROR,
 112                 "relative path: " + relative + ", kind: " + kind + ", no exception thrown");
 113     }
 114 }
 115