test/jdk/sun/security/util/ManifestDigester/DigestInput.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.ByteArrayOutputStream;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    25
import java.io.IOException;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    26
import java.security.MessageDigest;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    27
import java.util.ArrayList;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    28
import java.util.Arrays;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    29
import java.util.List;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    30
import java.util.jar.Attributes.Name;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    31
import java.util.stream.Collectors;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    32
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    33
import sun.security.util.ManifestDigester;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    34
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    35
import org.testng.annotations.DataProvider;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    36
import org.testng.annotations.Factory;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    37
import org.testng.annotations.BeforeMethod;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    38
import org.testng.annotations.AfterTest;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    39
import org.testng.annotations.Test;
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
import static java.nio.charset.StandardCharsets.UTF_8;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    42
import static org.testng.Assert.*;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    43
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    44
/**
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    45
 * @test
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    46
 * @bug 8217375
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    47
 * @modules java.base/sun.security.util
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    48
 * @compile ../../tools/jarsigner/Utils.java
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    49
 * @run testng DigestInput
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    50
 * @summary Checks that the manifest main attributes and entry digests are the
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    51
 * same as before resolution of bug 8217375 which means they treat some white
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    52
 * space different for oldStyle or digestWorkaround except for the blank line
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    53
 * at the end of the manifest file for digestWorkaround.
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    54
 */
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    55
public class DigestInput {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    56
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    57
    /**
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    58
     * Filters some test cases for calibrating expected digests with previous
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    59
     * implementation. TODO: Delete this after calibrating with old sources.
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
    static final boolean FIXED_8217375 = true; // FIXME
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    62
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    63
    /**
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    64
     * {@link ManifestDigester.Entry#digestWorkaround} should not feed the
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    65
     * trailing blank line into the digester. Before resolution of 8217375 it
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    66
     * fed the trailing blank line into the digest if the second line break
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    67
     * was at the end of the file due to <pre>
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    68
     * if (allBlank || (i == len-1)) {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    69
     *     if (i == len-1)
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    70
     *         pos.endOfSection = i;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    71
     *     else
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    72
     *         pos.endOfSection = last;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    73
     * </pre> in {@link ManifestDigester#findSection}. In that case at the end
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    74
     * of the manifest file, {@link ManifestDigester.Entry#digestWorkaround}
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    75
     * would have produced the same digest as
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    76
     * {@link ManifestDigester.Entry#digest} which was wrong and without effect
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    77
     * at best.
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    78
     * <p>
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    79
     * Once this fix is accepted, this flag can be removed along with
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    80
     * {@link #assertDigestEqualsCatchWorkaroundBroken}.
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    81
     */
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    82
    static final boolean FIXED_8217375_EOF_ENDOFSECTION = FIXED_8217375;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    83
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    84
    static final String SECTION_NAME = "some individual section name";
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    85
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    86
    @DataProvider(name = "parameters")
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    87
    public static Object[][] parameters() {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    88
        List<Object[]> tests = new ArrayList<>();
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    89
        for (String lineBreak : new String[] { "\n", "\r", "\r\n" }) {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    90
            if ("\r".equals(lineBreak) && !FIXED_8217375) continue;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    91
            for (int addLB = 0; addLB <= 4; addLB++) {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    92
                for (int numSecs = 0; numSecs <= 4; numSecs++) {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    93
                    for (boolean otherSec : new Boolean[] { false, true }) {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    94
                        for (boolean oldStyle : new Boolean[] { false, true }) {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    95
                            for (boolean workaround :
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    96
                                    new Boolean[] { false, true }) {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    97
                                tests.add(new Object[] {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    98
                                    lineBreak, addLB, numSecs, otherSec,
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    99
                                            oldStyle, workaround
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   100
                                });
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   101
                            }
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
            }
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
        return tests.toArray(new Object[tests.size()][]);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   108
    }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   109
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   110
    @Factory(dataProvider = "parameters")
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   111
    public static Object[] createTests(
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   112
            String lineBreak, int additionalLineBreaks,
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   113
            int numberOfSections, boolean hasOtherSection,
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   114
            boolean oldStyle, boolean digestWorkaround) {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   115
        return new Object[] { new DigestInput(lineBreak,
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   116
                additionalLineBreaks, numberOfSections, hasOtherSection,
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   117
                oldStyle, digestWorkaround)
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   118
        };
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   119
    }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   120
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   121
    final String lineBreak;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   122
    final int additionalLineBreaks; // number of blank lines delimiting section
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   123
    final int numberOfSections;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   124
    final boolean hasOtherSection;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   125
    final boolean oldStyle;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   126
    final boolean digestWorkaround;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   127
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   128
    public DigestInput(
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   129
            String lineBreak, int additionalLineBreaks,
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   130
            int numberOfSections, boolean hasOtherSection,
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   131
            boolean oldStyle, boolean digestWorkaround) {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   132
        this.lineBreak = lineBreak;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   133
        this.additionalLineBreaks = additionalLineBreaks;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   134
        this.numberOfSections = numberOfSections;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   135
        this.hasOtherSection = hasOtherSection;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   136
        this.oldStyle = oldStyle;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   137
        this.digestWorkaround = digestWorkaround;
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
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   140
    @BeforeMethod
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   141
    public void verbose() {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   142
        System.out.println("-".repeat(72));
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   143
        System.out.println("lineBreak = " +
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   144
                Utils.escapeStringWithNumbers(lineBreak));
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   145
        System.out.println("additionalLineBreaks = " + additionalLineBreaks);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   146
        System.out.println("numberOfSections = " + numberOfSections);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   147
        System.out.println("hasOtherSection = " + hasOtherSection);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   148
        System.out.println("oldStyle = " + oldStyle);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   149
        System.out.println("digestWorkaround = " + digestWorkaround);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   150
        System.out.println("-".repeat(72));
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   151
    }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   152
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   153
    byte[] rawManifestBytes() {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   154
        return (
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   155
            Name.MANIFEST_VERSION + ": 1.0" + lineBreak +
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   156
            "OldStyle0: no trailing space" + lineBreak +
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   157
            "OldStyle1: trailing space " + lineBreak +
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   158
            "OldStyle2: two trailing spaces  " + lineBreak +
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   159
                    lineBreak.repeat(additionalLineBreaks) +
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   160
            (
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   161
                "Name: " + SECTION_NAME + lineBreak +
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   162
                "OldStyle0: no trailing space" + lineBreak +
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   163
                "OldStyle1: trailing space " + lineBreak +
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   164
                "OldStyle2: two trailing spaces  " + lineBreak +
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   165
                lineBreak.repeat(additionalLineBreaks)
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   166
            ).repeat(numberOfSections) +
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   167
            (hasOtherSection ?
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   168
                "Name: unrelated trailing section" + lineBreak +
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   169
                "OldStyle0: no trailing space" + lineBreak +
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   170
                "OldStyle1: trailing space " + lineBreak +
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   171
                "OldStyle2: two trailing spaces  " + lineBreak +
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   172
                lineBreak.repeat(additionalLineBreaks)
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   173
            : "")
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   174
        ).getBytes(UTF_8);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   175
    }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   176
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   177
    byte[] expectedMainAttrsDigest(boolean digestWorkaround) {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   178
        return (
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   179
            Name.MANIFEST_VERSION + ": 1.0" + lineBreak +
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   180
            "OldStyle0: no trailing space" + lineBreak +
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   181
            "OldStyle1: trailing space" +
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   182
                (!oldStyle || !lineBreak.startsWith("\r") || digestWorkaround ?
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   183
                    " " : "") + lineBreak +
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   184
            "OldStyle2: two trailing spaces " +
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   185
                (!oldStyle || !lineBreak.startsWith("\r") || digestWorkaround ?
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   186
                        " " : "") + lineBreak +
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   187
            (
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   188
                     (
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   189
                                 !digestWorkaround
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   190
                         || (
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   191
                                 additionalLineBreaks == 1
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   192
                              && numberOfSections == 0
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   193
                              && !hasOtherSection
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   194
                              && (
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   195
                                      digestWorkaround
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   196
                                   && !FIXED_8217375_EOF_ENDOFSECTION
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   197
                                 )
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   198
                         )
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   199
                ) && (
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   200
                            additionalLineBreaks > 0
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   201
                         || numberOfSections > 0
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   202
                         || hasOtherSection
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   203
                )
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   204
            ? lineBreak : "")
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   205
        ).getBytes(UTF_8);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   206
    }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   207
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   208
    byte[] expectedIndividualSectionDigest(boolean digestWorkaround) {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   209
        if (numberOfSections == 0) return null;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   210
        return (
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   211
            (
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   212
                "Name: " + SECTION_NAME + lineBreak +
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   213
                "OldStyle0: no trailing space" + lineBreak +
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   214
                "OldStyle1: trailing space" +
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   215
                    (!oldStyle || !lineBreak.startsWith("\r")
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   216
                            || digestWorkaround ? " " : "") + lineBreak +
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   217
                "OldStyle2: two trailing spaces " +
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   218
                    (!oldStyle || !lineBreak.startsWith("\r")
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   219
                            || digestWorkaround ? " " : "") + lineBreak +
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   220
                (
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   221
                    (
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   222
                           !digestWorkaround
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   223
                    ) && (
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   224
                           additionalLineBreaks > 0
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   225
                    )
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   226
                ? lineBreak : "")
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   227
            ).repeat(numberOfSections) +
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   228
            (
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   229
                   additionalLineBreaks == 1
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   230
                && !hasOtherSection
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   231
                && digestWorkaround
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   232
                && !FIXED_8217375_EOF_ENDOFSECTION
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   233
            ? lineBreak : "")
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   234
        ).getBytes(UTF_8);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   235
    }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   236
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   237
    class EchoMessageDigest extends MessageDigest {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   238
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   239
        ByteArrayOutputStream buf;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   240
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   241
        EchoMessageDigest() {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   242
            super("echo");
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   243
        }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   244
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   245
        @Override
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   246
        protected void engineReset() {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   247
            buf = new ByteArrayOutputStream();
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   248
        }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   249
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   250
        @Override
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   251
        protected void engineUpdate(byte input) {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   252
            buf.write(input);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   253
        }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   254
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   255
        @Override
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   256
        protected void engineUpdate(byte[] i, int o, int l) {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   257
            buf.write(i, o, l);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   258
        }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   259
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   260
        @Override protected byte[] engineDigest() {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   261
            return buf.toByteArray();
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   262
        }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   263
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   264
    }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   265
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   266
    byte[] digestMainAttributes(byte[] mfBytes) throws Exception {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   267
        Utils.echoManifest(mfBytes, "going to digest main attributes of");
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   268
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   269
        ManifestDigester md = new ManifestDigester(mfBytes);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   270
        ManifestDigester.Entry entry =
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   271
                md.get(ManifestDigester.MF_MAIN_ATTRS, oldStyle);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   272
        MessageDigest digester = new EchoMessageDigest();
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   273
        return digestWorkaround ?
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   274
                entry.digestWorkaround(digester) : entry.digest(digester);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   275
    }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   276
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   277
    byte[] digestIndividualSection(byte[] mfBytes) throws Exception {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   278
        Utils.echoManifest(mfBytes,
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   279
                "going to digest section " + SECTION_NAME + " of");
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   280
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   281
        ManifestDigester md = new ManifestDigester(mfBytes);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   282
        ManifestDigester.Entry entry = md.get(SECTION_NAME, oldStyle);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   283
        if (entry == null) {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   284
            return null;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   285
        }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   286
        MessageDigest digester = new EchoMessageDigest();
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   287
        return digestWorkaround ?
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   288
                entry.digestWorkaround(digester) : entry.digest(digester);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   289
    }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   290
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   291
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   292
    /**
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   293
     * Checks that the manifest main attributes digest is the same as before.
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   294
     */
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   295
    @Test
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   296
    public void testMainAttributesDigest() throws Exception {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   297
        byte[] mfRaw = rawManifestBytes();
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   298
        byte[] digest = digestMainAttributes(mfRaw);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   299
        byte[] expectedDigest = expectedMainAttrsDigest(digestWorkaround);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   300
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   301
        // the individual section will be digested along with the main
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   302
        // attributes if not properly delimited with a blank line
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   303
        if (additionalLineBreaks == 0
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   304
                && (numberOfSections > 0 || hasOtherSection)) {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   305
            assertNotEquals(digest, expectedDigest);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   306
            return;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   307
        }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   308
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   309
        byte[] expectedDigestNoWorkaround = expectedMainAttrsDigest(false);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   310
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   311
//        assertDigestEquals(digest, expectedDigest); // FIXME
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   312
        assertDigestEqualsCatchWorkaroundBroken(
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   313
                digest, expectedDigest, expectedDigestNoWorkaround);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   314
    }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   315
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   316
    /**
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   317
     * Checks that an individual section digest is the same as before.
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   318
     */
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   319
    @Test
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   320
    public void testIndividualSectionDigest() throws Exception {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   321
        byte[] mfRaw = rawManifestBytes();
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   322
        byte[] digest = digestIndividualSection(mfRaw);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   323
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   324
        // no digest will be produced for an individual section that is not
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   325
        // properly section delimited with a blank line.
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   326
        byte[] expectedDigest =
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   327
                additionalLineBreaks == 0 ? null :
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   328
                    expectedIndividualSectionDigest(digestWorkaround);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   329
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   330
        byte[] expectedDigestNoWorkaround =
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   331
                additionalLineBreaks == 0 ? null :
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   332
                    expectedIndividualSectionDigest(false);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   333
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   334
//      assertDigestEquals(digest, expectedDigest); // FIXME
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   335
        assertDigestEqualsCatchWorkaroundBroken(
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   336
                digest, expectedDigest, expectedDigestNoWorkaround);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   337
    }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   338
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   339
    static int firstDiffPos = Integer.MAX_VALUE;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   340
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   341
    /**
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   342
     * @see FIXED_8217375_EOF_ENDOFSECTION
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   343
     */
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   344
    void assertDigestEqualsCatchWorkaroundBroken(
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   345
            byte[] actual, byte[] expected, byte[] expectedNoWorkaround)
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   346
                    throws IOException {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   347
        try {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   348
            assertDigestEquals(actual, expected);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   349
        } catch (AssertionError e) {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   350
            if (digestWorkaround && FIXED_8217375_EOF_ENDOFSECTION &&
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   351
                    Arrays.equals(expected, expectedNoWorkaround)) {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   352
                // if digests with and without workaround are the same anyway
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   353
                // the workaround has failed and could not have worked with
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   354
                // the same digest as produced without workaround before
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   355
                // which would not match either because equal.
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   356
                return;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   357
            }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   358
            fail("failed also without digestWorkaound", e);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   359
        }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   360
    }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   361
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   362
    void assertDigestEquals(byte[] actual, byte[] expected) throws IOException {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   363
        if (actual == null && expected == null) return;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   364
        Utils.echoManifest(actual, "actual digest");
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   365
        Utils.echoManifest(expected, "expected digest");
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   366
        for (int i = 0; i < actual.length && i < expected.length; i++) {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   367
            if (actual[i] != expected[i]) {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   368
                firstDiffPos = Math.min(firstDiffPos, i);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   369
                verbose();
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   370
                fail("found first difference in current test"
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   371
                        + " at position " + i);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   372
            }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   373
        }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   374
        if (actual.length != expected.length) {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   375
            int diffPos = Math.min(actual.length, expected.length);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   376
            firstDiffPos = Math.min(firstDiffPos, diffPos);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   377
            verbose();
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   378
            fail("found first difference in current test"
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   379
                    + " at position " + diffPos + " after one digest end");
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   380
        }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   381
        assertEquals(actual, expected);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   382
    }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   383
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   384
    @AfterTest
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   385
    public void reportFirstDiffPos() {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   386
        System.err.println("found first difference in all tests"
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   387
                + " at position " + firstDiffPos);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   388
    }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   389
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   390
}