langtools/test/tools/javap/ControlCharTest.java
author sdrach
Fri, 29 Jul 2016 12:27:08 -0700
changeset 39919 3d3573afc062
permissions -rw-r--r--
8143366: Control characters in constant pool strings are not escaped properly Reviewed-by: jjg
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
39919
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
     1
/*
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
     4
 *
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
     7
 * published by the Free Software Foundation.
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
     8
 *
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
    13
 * accompanied this code).
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
    14
 *
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
    18
 *
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
    21
 * questions.
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
    22
 */
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
    23
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
    24
/*
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
    25
 * @test
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
    26
 * @bug 8143366
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
    27
 * @summary Check that control chars are displayed correctly
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
    28
 * @modules jdk.jdeps/com.sun.tools.javap
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
    29
 * @run testng ControlCharTest
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
    30
 */
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
    31
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
    32
import org.testng.Assert;
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
    33
import org.testng.annotations.AfterClass;
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
    34
import org.testng.annotations.BeforeClass;
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
    35
import org.testng.annotations.DataProvider;
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
    36
import org.testng.annotations.Test;
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
    37
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
    38
import java.io.IOException;
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
    39
import java.io.PrintWriter;
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
    40
import java.nio.file.Files;
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
    41
import java.nio.file.Path;
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
    42
import java.nio.file.Paths;
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
    43
import java.util.Scanner;
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
    44
import java.util.regex.Pattern;
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
    45
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
    46
public class ControlCharTest {
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
    47
    private String classpath;
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
    48
    private Path output;
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
    49
    private Pattern ptrn;
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
    50
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
    51
    @BeforeClass
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
    52
    public void initialize() throws Exception {
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
    53
        String testClasses = System.getProperty("test.classes", ".");
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
    54
        classpath = "-classpath " + testClasses + " ControlCharTest$Strings";
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
    55
        String userdir = System.getProperty("user.dir", ".");
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
    56
        output = Paths.get(userdir, "output.txt");
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
    57
        String regex = Pattern.quote("\\u0001\\u0002\\u0003")  // \u0001\u0002\u0003
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
    58
                + ".+123.+"                   // 123
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
    59
                + Pattern.quote("\\\\u0000")  // \\u0000
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
    60
                + ".+"
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
    61
                + Pattern.quote("\\u0000")    // \u0000
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
    62
                ;
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
    63
        ptrn = Pattern.compile(regex, Pattern.DOTALL);
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
    64
    }
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
    65
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
    66
    @AfterClass
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
    67
    public void close() throws IOException {
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
    68
        Files.deleteIfExists(output);
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
    69
    }
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
    70
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
    71
    @DataProvider(name = "options")
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
    72
    public Object[][] createData() {
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
    73
        return new Object[][] {
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
    74
                { "-v", ""},
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
    75
                { "-constants", ""}
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
    76
        };
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
    77
    }
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
    78
    @Test(dataProvider = "options")
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
    79
    public void test(String option, String ignore) throws Exception {
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
    80
        String cmdline = option + " " + classpath;
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
    81
        javap(cmdline.split(" +"));
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
    82
        try (Scanner scnr = new Scanner(output)) {
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
    83
            Assert.assertNotNull(scnr.findWithinHorizon(ptrn, 0));
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
    84
        }
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
    85
    }
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
    86
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
    87
    private void javap(String... args) throws Exception {
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
    88
        try (PrintWriter out = new PrintWriter(output.toFile())) {
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
    89
            int rc = com.sun.tools.javap.Main.run(args, out);
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
    90
            if (rc < 0)
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
    91
                throw new Exception("javap exited, rc=" + rc);
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
    92
        }
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
    93
    }
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
    94
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
    95
    // small class to test
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
    96
    static class Strings {
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
    97
        static final String s = "\1\2\3";
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
    98
        static final String s1 = "123";
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
    99
        static final String s2 = "\\u0000";
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
   100
        static final String s3 = "\0";
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
   101
        static String f() { return s + s1 + s2 + s3; }
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
   102
    }
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
   103
}
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
   104
3d3573afc062 8143366: Control characters in constant pool strings are not escaped properly
sdrach
parents:
diff changeset
   105