|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, 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 8346465 |
| 27 | + * @summary Tests if setData() throws IAE for BuiltIn profiles |
| 28 | + */ |
| 29 | + |
| 30 | +import java.awt.color.ColorSpace; |
| 31 | +import java.awt.color.ICC_Profile; |
| 32 | +import java.io.ByteArrayInputStream; |
| 33 | +import java.io.ByteArrayOutputStream; |
| 34 | +import java.io.FileInputStream; |
| 35 | +import java.io.ObjectInputStream; |
| 36 | +import java.io.ObjectOutputStream; |
| 37 | +import java.util.Map; |
| 38 | + |
| 39 | +public class BuiltInProfileCheck { |
| 40 | + private static final int HEADER_TAG = ICC_Profile.icSigHead; |
| 41 | + private static final int INDEX = ICC_Profile.icHdrDeviceClass; |
| 42 | + private static final String EXCEPTION_MSG = "Built-in profile cannot be modified"; |
| 43 | + /** |
| 44 | + * {@link #prepareTestProfile(String, boolean, int)} |
| 45 | + * stores the profile to test in testProfile. |
| 46 | + */ |
| 47 | + private static ICC_Profile testProfile; |
| 48 | + |
| 49 | + private static final Map<Integer, String> colorSpace = Map.of( |
| 50 | + ColorSpace.CS_sRGB, "CS_sRGB", |
| 51 | + ColorSpace.CS_PYCC, "CS_PYCC", |
| 52 | + ColorSpace.CS_GRAY, "CS_GRAY", |
| 53 | + ColorSpace.CS_CIEXYZ, "CS_CIEXYZ", |
| 54 | + ColorSpace.CS_LINEAR_RGB, "CS_LINEAR_RGB" |
| 55 | + ); |
| 56 | + |
| 57 | + public static void main(String[] args) throws Exception { |
| 58 | + System.out.println("CASE 1: Testing BuiltIn Profile"); |
| 59 | + for (int cs : colorSpace.keySet()) { |
| 60 | + prepareTestProfile("Default", true, cs); |
| 61 | + testProfile(true, cs); |
| 62 | + } |
| 63 | + System.out.println("Passed\n"); |
| 64 | + |
| 65 | + System.out.println("CASE 2: Testing Custom Profile"); |
| 66 | + prepareTestProfile("Default", false, ColorSpace.CS_sRGB); |
| 67 | + testProfile(false, ColorSpace.CS_sRGB); |
| 68 | + System.out.println("Passed\n"); |
| 69 | + |
| 70 | + System.out.println("CASE 3: Testing Built-In Profile" |
| 71 | + + " Serialization & Deserialization"); |
| 72 | + for (int cs : colorSpace.keySet()) { |
| 73 | + prepareTestProfile("Serialize", true, cs); |
| 74 | + testProfile(true, cs); |
| 75 | + } |
| 76 | + System.out.println("Passed\n"); |
| 77 | + |
| 78 | + System.out.println("CASE 4: Testing Custom Profile" |
| 79 | + + " Serialization & Deserialization"); |
| 80 | + prepareTestProfile("Serialize", false, ColorSpace.CS_sRGB); |
| 81 | + testProfile(false, ColorSpace.CS_sRGB); |
| 82 | + System.out.println("Passed\n"); |
| 83 | + |
| 84 | + System.out.println("CASE 5: Test reading Built-In profile from .icc file"); |
| 85 | + prepareTestProfile("ReadFromFile", true, ColorSpace.CS_sRGB); |
| 86 | + testProfile(true, ColorSpace.CS_sRGB); |
| 87 | + System.out.println("Passed\n"); |
| 88 | + |
| 89 | + System.out.println("CASE 6: Test reading Custom profile from .icc file"); |
| 90 | + prepareTestProfile("ReadFromFile", false, ColorSpace.CS_sRGB); |
| 91 | + testProfile(false, ColorSpace.CS_sRGB); |
| 92 | + System.out.println("Passed\n"); |
| 93 | + } |
| 94 | + |
| 95 | + private static void prepareTestProfile(String testCase, |
| 96 | + boolean isBuiltIn, int cs) { |
| 97 | + ICC_Profile builtInProfile = ICC_Profile.getInstance(cs); |
| 98 | + // if isBuiltIn=true use builtInProfile else create a copy |
| 99 | + testProfile = isBuiltIn |
| 100 | + ? builtInProfile |
| 101 | + : ICC_Profile.getInstance(builtInProfile.getData()); |
| 102 | + |
| 103 | + switch (testCase) { |
| 104 | + case "Default" -> { |
| 105 | + // empty case block |
| 106 | + // no further processing of testProfile required for default case |
| 107 | + } |
| 108 | + case "Serialize" -> { |
| 109 | + try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 110 | + ObjectOutputStream oos = new ObjectOutputStream(baos)) { |
| 111 | + oos.writeObject(testProfile); |
| 112 | + |
| 113 | + byte[] array = baos.toByteArray(); |
| 114 | + try (ObjectInputStream ois = |
| 115 | + new ObjectInputStream(new ByteArrayInputStream(array))) { |
| 116 | + testProfile = (ICC_Profile) ois.readObject(); |
| 117 | + } |
| 118 | + } catch (Exception e) { |
| 119 | + throw new RuntimeException("Test Failed ! Serial-Deserialization" |
| 120 | + + " case failed", e); |
| 121 | + } |
| 122 | + } |
| 123 | + case "ReadFromFile" -> { |
| 124 | + // .icc files serialized on older JDK version |
| 125 | + String filename = isBuiltIn ? "builtIn.icc" : "custom.icc"; |
| 126 | + String testDir = System.getProperty("test.src") |
| 127 | + + System.getProperty("file.separator"); |
| 128 | + filename = testDir + filename; |
| 129 | + |
| 130 | + try (FileInputStream fileIn = new FileInputStream(filename); |
| 131 | + ObjectInputStream ois = new ObjectInputStream(fileIn)) { |
| 132 | + testProfile = (ICC_Profile) ois.readObject(); |
| 133 | + } catch (Exception e) { |
| 134 | + throw new RuntimeException("Test Failed ! Unable to fetch" |
| 135 | + + " .icc files", e); |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + private static void testProfile(boolean isBuiltIn, int cs) { |
| 142 | + byte[] headerData = testProfile.getData(HEADER_TAG); |
| 143 | + // Set profile class to valid icSigInputClass = 0x73636E72 |
| 144 | + headerData[INDEX] = 0x73; |
| 145 | + headerData[INDEX + 1] = 0x63; |
| 146 | + headerData[INDEX + 2] = 0x6E; |
| 147 | + headerData[INDEX + 3] = 0x72; |
| 148 | + |
| 149 | + if (isBuiltIn) { |
| 150 | + System.out.println("Testing: " + colorSpace.get(cs)); |
| 151 | + try { |
| 152 | + // Try updating a built-in profile, IAE is expected |
| 153 | + testProfile.setData(HEADER_TAG, headerData); |
| 154 | + throw new RuntimeException("Test Failed! IAE NOT thrown for profile " |
| 155 | + + colorSpace.get(cs)); |
| 156 | + } catch (IllegalArgumentException iae) { |
| 157 | + if (!iae.getMessage().equals(EXCEPTION_MSG)) { |
| 158 | + throw new RuntimeException("Test Failed! IAE with exception msg \"" |
| 159 | + + EXCEPTION_MSG + "\" NOT thrown for profile " |
| 160 | + + colorSpace.get(cs)); |
| 161 | + } |
| 162 | + } |
| 163 | + } else { |
| 164 | + // Modifying custom profile should NOT throw IAE |
| 165 | + testProfile.setData(HEADER_TAG, headerData); |
| 166 | + } |
| 167 | + } |
| 168 | +} |
0 commit comments