1 /*
  2  * Copyright (c) 2015, 2019, 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 /*
 26  * @test
 27  * @summary Tests the format checking of class list format.
 28  *
 29  * @requires vm.cds
 30  * @requires vm.cds.custom.loaders
 31  * @library /test/lib /test/hotspot/jtreg/runtime/cds/appcds
 32  * @compile ../test-classes/Hello.java test-classes/CustomLoadee.java test-classes/CustomLoadee2.java
 33  *          test-classes/CustomInterface2_ia.java test-classes/CustomInterface2_ib.java
 34  * @run driver ClassListFormatA
 35  */
 36 
 37 public class ClassListFormatA extends ClassListFormatBase {
 38     static {
 39         // Uncomment the following line to run only one of the test cases
 40         // ClassListFormatBase.RUN_ONLY_TEST = "TESTCASE A1";
 41     }
 42 
 43     public static void main(String[] args) throws Throwable {
 44         String appJar = JarBuilder.getOrCreateHelloJar();
 45         String customJarPath = JarBuilder.build("ClassListFormatA", "CustomLoadee",
 46                                             "CustomLoadee2", "CustomInterface2_ia", "CustomInterface2_ib");
 47         //----------------------------------------------------------------------
 48         // TESTGROUP A: general bad input
 49         //----------------------------------------------------------------------
 50         dumpShouldFail(
 51             "TESTCASE A1: bad input - interface: instead of interfaces:",
 52             appJar, classlist(
 53                 "Hello",
 54                 "java/lang/Object id: 1",
 55                 "CustomLoadee interface: 1"
 56             ),
 57             "Unknown input:");
 58 
 59         dumpShouldFail(
 60             "TESTCASE A2: bad input - negative IDs not allowed",
 61             appJar, classlist(
 62                 "Hello",
 63                 "java/lang/Object id: -1"
 64             ),
 65             "Error: negative integers not allowed");
 66 
 67         dumpShouldFail(
 68             "TESTCASE A3: bad input - bad ID (not an integer)",
 69             appJar, classlist(
 70                 "Hello",
 71                 "java/lang/Object id: xyz"
 72             ),
 73             "Error: expected integer");
 74 
 75         if (false) {
 76               // FIXME - classFileParser.cpp needs fixing.
 77             dumpShouldFail(
 78                 "TESTCASE A4: bad input - bad ID (integer too big)",
 79                 appJar, classlist(
 80                     "Hello",
 81                     "java/lang/Object id: 2147483648" // <- this is 0x80000000
 82                 ),
 83                 "Error: expected integer");
 84 
 85               // FIXME
 86             dumpShouldFail(
 87                 "TESTCASE A5: bad input - bad ID (integer too big)",
 88                 appJar, classlist(
 89                     "Hello",
 90                     "java/lang/Object id: 21474836489" // bigger than 32-bit!
 91                 ),
 92                 "Error: expected integer");
 93         }
 94 
 95         // Good input:
 96         dumpShouldPass(
 97             "TESTCASE A6: extraneous spaces, tab characters and trailing new line characters",
 98             appJar, classlist(
 99                 "Hello   ",                   // trailing spaces
100                 "java/lang/Object\tid:\t1",   // \t instead of ' '
101                 "CustomLoadee id: 2 super: 1 source: " + customJarPath,
102                 "CustomInterface2_ia id: 3 super: 1 source: " + customJarPath + " ",
103                 "CustomInterface2_ib id: 4 super: 1 source: " + customJarPath + "\t\t\r" ,
104                 "CustomLoadee2 id: 5 super: 1 interfaces: 3 4 source: " + customJarPath      // preceding spaces
105             ));
106         /*
107 
108           FIXME:leyden-premain : these test cases are disabled because ClassListParser can handle much longer lines now
109 
110         int _max_allowed_line = 4096; // Must match ClassListParser::_max_allowed_line in C code.
111         int _line_buf_extra = 10;     // Must match ClassListParser::_line_buf_extra in C code.
112         StringBuffer sbuf = new StringBuffer();
113         for (int i=0; i<_max_allowed_line+1; i++) {
114           sbuf.append("x");
115         }
116 
117         dumpShouldFail(
118             "TESTCASE A7: bad input - line too long",
119             appJar, classlist(
120                 sbuf.toString()
121             ),
122             "input line too long (must be no longer than " + _max_allowed_line + " chars");
123 
124         for (int i=0; i<_line_buf_extra + 1000; i++) {
125           sbuf.append("X");
126         }
127 
128         dumpShouldFail(
129             "TESTCASE A8: bad input - line too long: try to overflow C buffer",
130             appJar, classlist(
131                 sbuf.toString()
132             ),
133             "input line too long (must be no longer than " + _max_allowed_line + " chars");
134 
135         */
136     }
137 }