test/jdk/sun/security/util/ManifestDigester/LineBreaks.java
author weijun
Thu, 18 Jul 2019 08:53:06 +0800
changeset 57488 94691d8e746f
permissions -rw-r--r--
8217375: jarsigner breaks old signature with long lines in manifest Reviewed-by: jjiang, weijun Contributed-by: Philipp Kunz <philipp.kunz@paratix.ch>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
57488
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
     1
/*
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
     2
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
     4
 *
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
     7
 * published by the Free Software Foundation.
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
     8
 *
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    13
 * accompanied this code).
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    14
 *
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    18
 *
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    21
 * questions.
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    22
 */
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    23
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    24
import java.io.ByteArrayInputStream;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    25
import java.io.ByteArrayOutputStream;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    26
import java.io.IOException;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    27
import java.util.List;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    28
import java.util.ArrayList;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    29
import java.util.function.Function;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    30
import java.util.jar.Attributes;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    31
import java.util.jar.Manifest;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    32
import sun.security.util.ManifestDigester;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    33
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    34
import org.testng.annotations.DataProvider;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    35
import org.testng.annotations.Test;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    36
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    37
import static java.nio.charset.StandardCharsets.UTF_8;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    38
import static org.testng.Assert.*;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    39
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    40
/**
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    41
 * @test
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    42
 * @bug 8217375
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    43
 * @modules java.base/sun.security.util
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    44
 * @compile ../../tools/jarsigner/Utils.java
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    45
 * @run testng LineBreaks
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    46
 * @summary Verify {@code ManifestDigester} reads different line breaks well.
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    47
 * The specifications state:
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    48
 * <q><pre>newline: CR LF | LF | CR (not followed by LF)</pre></q>.
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    49
 * This test does not verify that the digests are correct.
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    50
 */
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    51
public class LineBreaks {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    52
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    53
    static final String KEY = "Key";
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    54
    static final String VALUE = "Value";
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    55
    static final String SECTION = "Section";
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    56
    static final String FOO = "Foo";
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    57
    static final String BAR = "Bar";
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    58
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    59
    static final String EXCEED_LINE_WIDTH_LIMIT = "x".repeat(71);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    60
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    61
    String breakAndContinue(String str, String lineBreak) {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    62
        // assert no multi-byte UTF-8 encoded characters in this test
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    63
        assertEquals(str.getBytes(UTF_8).length, str.length());
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    64
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    65
        int p = 1;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    66
        while (p + 71 < str.length()) {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    67
            p += 71;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    68
            str = str.substring(0, p) + lineBreak + " " + str.substring(p);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    69
            p += lineBreak.length() + 1;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    70
        }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    71
        return str;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    72
    }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    73
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    74
    byte[] createTestManifest(String lineBreak, boolean onlyMainAttrs,
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    75
            String excess) throws IOException {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    76
        System.out.println("lineBreak = "
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    77
                + Utils.escapeStringWithNumbers(lineBreak));
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    78
        System.out.println("onlyMainAttrs = " + onlyMainAttrs);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    79
        String mf = "";
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    80
        mf += breakAndContinue(
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    81
                KEY + ": " + VALUE + excess, lineBreak) + lineBreak;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    82
        mf += lineBreak;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    83
        if (!onlyMainAttrs) {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    84
            mf += breakAndContinue(
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    85
                    "Name: " + SECTION + excess, lineBreak) + lineBreak;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    86
            mf += breakAndContinue(
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    87
                    FOO + ": " + BAR + excess, lineBreak) + lineBreak;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    88
            mf += lineBreak;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    89
        }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    90
        byte[] mfBytes = mf.getBytes(UTF_8);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    91
        Utils.echoManifest(mfBytes, "binary manifest");
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    92
        return mfBytes;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    93
    }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    94
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    95
    @DataProvider(name = "parameters")
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    96
    public static Object[][] parameters() {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    97
        List<Object[]> tests = new ArrayList<>();
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    98
        for (String lineBreak : new String[] { "\n", "\r", "\r\n" }) {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    99
            for (boolean onlyMainAttrs : new boolean[] { false, true }) {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   100
                for (int numLbs = 0; numLbs < 3; numLbs++) {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   101
                    tests.add(new Object[]{ lineBreak, onlyMainAttrs, numLbs });
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   102
                }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   103
            }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   104
        }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   105
        return tests.toArray(new Object[tests.size()][]);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   106
    }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   107
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   108
    @Test(dataProvider = "parameters")
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   109
    public void test(String lineBreak, boolean onlyMainAttrs, int numLbs)
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   110
            throws IOException {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   111
        String excess = EXCEED_LINE_WIDTH_LIMIT.repeat(numLbs);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   112
        byte[] mfBytes = createTestManifest(lineBreak, onlyMainAttrs, excess);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   113
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   114
        // self-test: make sure the manifest is valid and represents the
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   115
        // values as expected before attempting to digest it
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   116
        Manifest mf = new Manifest(new ByteArrayInputStream(mfBytes));
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   117
        assertEquals(mf.getMainAttributes().getValue(KEY), VALUE + excess);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   118
        Attributes section = mf.getAttributes(SECTION + excess);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   119
        if (onlyMainAttrs) {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   120
            assertNull(section);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   121
        } else {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   122
            assertEquals(section.getValue(FOO), BAR + excess);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   123
        }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   124
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   125
        // verify that ManifestDigester has actually found the individual
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   126
        // section if and only if it was present thereby also implying based
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   127
        // on ManifestDigester implementation that the main attributes were
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   128
        // found before
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   129
        ManifestDigester md = new ManifestDigester(mfBytes);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   130
        assertTrue((md.get(SECTION + excess, false) != null) != onlyMainAttrs);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   131
    }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   132
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   133
    static List<Integer> stringToIntList(String string) {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   134
        byte[] bytes = string.getBytes(UTF_8);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   135
        List<Integer> list = new ArrayList<>();
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   136
        for (int i = 0; i < bytes.length; i++) {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   137
            list.add((int) bytes[i]);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   138
        }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   139
        return list;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   140
    }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   141
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   142
}