Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/javax/imageio/plugins/png/WriteInvalidKeywordTest.java
41155 views
1
/*
2
* Copyright (c) 2020, 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
/*
25
* @test
26
* @bug 8242557
27
* @summary Test verifies that PNGImageWriter does not write
28
* longer than 79 length null terminated strings.
29
* @run main WriteInvalidKeywordTest
30
*/
31
32
import java.awt.Graphics2D;
33
import java.awt.image.BufferedImage;
34
import java.awt.Color;
35
import java.io.ByteArrayOutputStream;
36
import java.io.IOException;
37
import java.util.Iterator;
38
import javax.imageio.*;
39
import javax.imageio.metadata.IIOInvalidTreeException;
40
import javax.imageio.metadata.IIOMetadata;
41
import javax.imageio.metadata.IIOMetadataNode;
42
import javax.imageio.stream.ImageOutputStream;
43
44
public class WriteInvalidKeywordTest {
45
46
private static BufferedImage img;
47
private static ImageWriter writer;
48
private static ImageWriteParam param;
49
private static IIOMetadata metadata;
50
51
private static void initialize(int type) {
52
int width = 1;
53
int height = 1;
54
img = new BufferedImage(width, height, type);
55
Graphics2D g2D = img.createGraphics();
56
g2D.setColor(new Color(255, 255, 255));
57
g2D.fillRect(0, 0, width, width);
58
g2D.dispose();
59
60
Iterator<ImageWriter> iterWriter =
61
ImageIO.getImageWritersBySuffix("png");
62
writer = iterWriter.next();
63
64
param = writer.getDefaultWriteParam();
65
ImageTypeSpecifier specifier =
66
ImageTypeSpecifier.
67
createFromBufferedImageType(type);
68
metadata = writer.getDefaultImageMetadata(specifier, param);
69
}
70
71
private static void createTEXTNode()
72
throws IIOInvalidTreeException {
73
IIOMetadataNode tEXt_Entry = new IIOMetadataNode("tEXtEntry");
74
// Keyword length greater than 79
75
tEXt_Entry.setAttribute("keyword", "Authored" +
76
"AuthoredAuthoredAuthoredAuthoredAuthoredAuthored" +
77
"AuthoredAuthoredAuthoredAuthored");
78
tEXt_Entry.setAttribute("value", "");
79
80
IIOMetadataNode tEXt = new IIOMetadataNode("tEXt");
81
tEXt.appendChild(tEXt_Entry);
82
IIOMetadataNode root = new IIOMetadataNode("javax_imageio_png_1.0");
83
root.appendChild(tEXt);
84
metadata.mergeTree("javax_imageio_png_1.0", root);
85
}
86
87
private static void writeImage() throws IOException {
88
ByteArrayOutputStream baos = new ByteArrayOutputStream();
89
ImageOutputStream ios = ImageIO.createImageOutputStream(baos);
90
writer.setOutput(ios);
91
writer.write(metadata, new IIOImage(img, null, metadata), param);
92
writer.dispose();
93
baos.close();
94
ios.close();
95
}
96
97
private static void writePNGTEXTChunk() throws IOException {
98
initialize(BufferedImage.TYPE_BYTE_GRAY);
99
createTEXTNode();
100
writeImage();
101
}
102
103
public static void main(String[] args) throws IOException {
104
// write PNG image with tEXT chunk having keyword length
105
// greater than 79.
106
boolean failed = true;
107
try {
108
writePNGTEXTChunk();
109
} catch (IIOException e) {
110
// we expect it to throw IIOException
111
if (e.getCause().getMessage() ==
112
"tEXt keyword is longer than 79") {
113
failed = false;
114
}
115
}
116
if (failed) {
117
throw new RuntimeException("Test failed, did not throw " +
118
"expected exception");
119
}
120
}
121
}
122
123
124