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