Path: blob/master/test/jdk/sun/security/util/ManifestDigester/LineBreaks.java
41152 views
/*1* Copyright (c) 2019, 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*/2223import java.io.ByteArrayInputStream;24import java.io.ByteArrayOutputStream;25import java.io.IOException;26import java.util.List;27import java.util.ArrayList;28import java.util.function.Function;29import java.util.jar.Attributes;30import java.util.jar.Manifest;31import sun.security.util.ManifestDigester;3233import org.testng.annotations.DataProvider;34import org.testng.annotations.Test;3536import static java.nio.charset.StandardCharsets.UTF_8;37import static org.testng.Assert.*;3839/**40* @test41* @bug 821737542* @modules java.base/sun.security.util43* @compile ../../tools/jarsigner/Utils.java44* @run testng LineBreaks45* @summary Verify {@code ManifestDigester} reads different line breaks well.46* The specifications state:47* <q><pre>newline: CR LF | LF | CR (not followed by LF)</pre></q>.48* This test does not verify that the digests are correct.49*/50public class LineBreaks {5152static final String KEY = "Key";53static final String VALUE = "Value";54static final String SECTION = "Section";55static final String FOO = "Foo";56static final String BAR = "Bar";5758static final String EXCEED_LINE_WIDTH_LIMIT = "x".repeat(71);5960String breakAndContinue(String str, String lineBreak) {61// assert no multi-byte UTF-8 encoded characters in this test62assertEquals(str.getBytes(UTF_8).length, str.length());6364int p = 1;65while (p + 71 < str.length()) {66p += 71;67str = str.substring(0, p) + lineBreak + " " + str.substring(p);68p += lineBreak.length() + 1;69}70return str;71}7273byte[] createTestManifest(String lineBreak, boolean onlyMainAttrs,74String excess) throws IOException {75System.out.println("lineBreak = "76+ Utils.escapeStringWithNumbers(lineBreak));77System.out.println("onlyMainAttrs = " + onlyMainAttrs);78String mf = "";79mf += breakAndContinue(80KEY + ": " + VALUE + excess, lineBreak) + lineBreak;81mf += lineBreak;82if (!onlyMainAttrs) {83mf += breakAndContinue(84"Name: " + SECTION + excess, lineBreak) + lineBreak;85mf += breakAndContinue(86FOO + ": " + BAR + excess, lineBreak) + lineBreak;87mf += lineBreak;88}89byte[] mfBytes = mf.getBytes(UTF_8);90Utils.echoManifest(mfBytes, "binary manifest");91return mfBytes;92}9394@DataProvider(name = "parameters")95public static Object[][] parameters() {96List<Object[]> tests = new ArrayList<>();97for (String lineBreak : new String[] { "\n", "\r", "\r\n" }) {98for (boolean onlyMainAttrs : new boolean[] { false, true }) {99for (int numLbs = 0; numLbs < 3; numLbs++) {100tests.add(new Object[]{ lineBreak, onlyMainAttrs, numLbs });101}102}103}104return tests.toArray(new Object[tests.size()][]);105}106107@Test(dataProvider = "parameters")108public void test(String lineBreak, boolean onlyMainAttrs, int numLbs)109throws IOException {110String excess = EXCEED_LINE_WIDTH_LIMIT.repeat(numLbs);111byte[] mfBytes = createTestManifest(lineBreak, onlyMainAttrs, excess);112113// self-test: make sure the manifest is valid and represents the114// values as expected before attempting to digest it115Manifest mf = new Manifest(new ByteArrayInputStream(mfBytes));116assertEquals(mf.getMainAttributes().getValue(KEY), VALUE + excess);117Attributes section = mf.getAttributes(SECTION + excess);118if (onlyMainAttrs) {119assertNull(section);120} else {121assertEquals(section.getValue(FOO), BAR + excess);122}123124// verify that ManifestDigester has actually found the individual125// section if and only if it was present thereby also implying based126// on ManifestDigester implementation that the main attributes were127// found before128ManifestDigester md = new ManifestDigester(mfBytes);129assertTrue((md.get(SECTION + excess, false) != null) != onlyMainAttrs);130}131132static List<Integer> stringToIntList(String string) {133byte[] bytes = string.getBytes(UTF_8);134List<Integer> list = new ArrayList<>();135for (int i = 0; i < bytes.length; i++) {136list.add((int) bytes[i]);137}138return list;139}140141}142143144