Path: blob/master/test/jdk/sun/security/tools/jarsigner/JarSigningNonAscii.java
66645 views
/*1* Copyright (c) 2003, 2018, 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 4924188 820281626* @summary sign a JAR file that has entry names with non-ASCII characters.27* @modules jdk.jartool/sun.security.tools.jarsigner28* @run main/othervm JarSigningNonAscii29*/3031import java.io.*;32import java.util.*;33import java.util.jar.*;34import java.security.cert.Certificate;3536public class JarSigningNonAscii {3738private static String jarFile;39private static String keystore;4041public static void main(String[] args) throws Exception {4243String srcDir = System.getProperty("test.src", ".");44String destDir = System.getProperty("test.classes", ".");45String unsignedJar = srcDir + "/JarSigning_RU.jar";46String signedJar = destDir + "/JarSigning_RU.signed.jar";47String keystore = srcDir + "/JarSigning.keystore";4849// remove signed jar if it exists50try {51File removeMe = new File(signedJar);52removeMe.delete();53} catch (Exception e) {54// ignore55e.printStackTrace();56}5758// sign the provided jar file59String[] jsArgs = {60"-keystore", keystore,61"-storepass", "bbbbbb",62"-signedJar", signedJar,63unsignedJar, "b"64};65sun.security.tools.jarsigner.Main.main(jsArgs);6667// verify the signed jar file6869/**70* can not do this because JarSigner calls System.exit71* with an exit code that jtreg does not like72*73String[] vArgs = {74"-verify",75"-keystore", keystore,76"-storepass", "bbbbbb",77"-verbose",78signedJar79};80JarSigner.main(vArgs);81*/8283JarEntry je;84JarFile jf = new JarFile(signedJar, true);8586Vector entriesVec = new Vector();87byte[] buffer = new byte[8192];8889Enumeration entries = jf.entries();90while (entries.hasMoreElements()) {91je = (JarEntry)entries.nextElement();92entriesVec.addElement(je);93InputStream is = jf.getInputStream(je);94int n;95while ((n = is.read(buffer, 0, buffer.length)) != -1) {96// we just read. this will throw a SecurityException97// if a signature/digest check fails.98}99is.close();100}101jf.close();102Manifest man = jf.getManifest();103int isSignedCount = 0;104if (man != null) {105Enumeration e = entriesVec.elements();106while (e.hasMoreElements()) {107je = (JarEntry) e.nextElement();108String name = je.getName();109Certificate[] certs = je.getCertificates();110if ((certs != null) && (certs.length > 0)) {111isSignedCount++;112}113}114}115116if (isSignedCount != 4) {117throw new SecurityException("error signing JAR file");118}119120System.out.println("jar verified");121}122}123124125