Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/tools/jarsigner/JarSigningNonAscii.java
38853 views
/*1* Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 492418826* @summary sign a JAR file that has entry names with non-ASCII characters.27* @run main/othervm -Djava.security.properties=${test.src}/reenable.jar.alg.props JarSigningNonAscii28*/2930import sun.security.tools.*;31import java.io.*;32import java.security.Security;33import java.util.*;34import java.util.jar.*;35import java.security.cert.Certificate;3637public class JarSigningNonAscii {3839private static String jarFile;40private static String keystore;4142public static void main(String[] args) throws Exception {4344String srcDir = System.getProperty("test.src", ".");45String destDir = System.getProperty("test.classes", ".");46String unsignedJar = srcDir + "/JarSigning_RU.jar";47String signedJar = destDir + "/JarSigning_RU.signed.jar";48String keystore = srcDir + "/JarSigning.keystore";4950// remove signed jar if it exists51try {52File removeMe = new File(signedJar);53removeMe.delete();54} catch (Exception e) {55// ignore56e.printStackTrace();57}5859// sign the provided jar file60String[] jsArgs = {61"-keystore", keystore,62"-storepass", "bbbbbb",63"-signedJar", signedJar,64unsignedJar, "b"65};66sun.security.tools.jarsigner.Main.main(jsArgs);6768// verify the signed jar file6970/**71* can not do this because JarSigner calls System.exit72* with an exit code that jtreg does not like73*74String[] vArgs = {75"-verify",76"-keystore", keystore,77"-storepass", "bbbbbb",78"-verbose",79signedJar80};81JarSigner.main(vArgs);82*/8384JarEntry je;85JarFile jf = new JarFile(signedJar, true);8687Vector entriesVec = new Vector();88byte[] buffer = new byte[8192];8990Enumeration entries = jf.entries();91while (entries.hasMoreElements()) {92je = (JarEntry)entries.nextElement();93entriesVec.addElement(je);94InputStream is = jf.getInputStream(je);95int n;96while ((n = is.read(buffer, 0, buffer.length)) != -1) {97// we just read. this will throw a SecurityException98// if a signature/digest check fails.99}100is.close();101}102jf.close();103Manifest man = jf.getManifest();104int isSignedCount = 0;105if (man != null) {106Enumeration e = entriesVec.elements();107while (e.hasMoreElements()) {108je = (JarEntry) e.nextElement();109String name = je.getName();110Certificate[] certs = je.getCertificates();111if ((certs != null) && (certs.length > 0)) {112isSignedCount++;113}114}115}116117if (isSignedCount != 4) {118throw new SecurityException("error signing JAR file");119}120121System.out.println("jar verified");122}123}124125126