|
| 1 | +/* |
| 2 | + * Copyright Amazon.com Inc. 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.File; |
| 25 | +import java.io.FileInputStream; |
| 26 | +import java.io.InvalidObjectException; |
| 27 | +import java.io.ObjectInputStream; |
| 28 | +import java.io.OptionalDataException; |
| 29 | + |
| 30 | +/** |
| 31 | + * @test |
| 32 | + * @bug 8367384 |
| 33 | + * @summary Verify ICC_Profile serialization per spec, all name/data cases |
| 34 | + */ |
| 35 | +public final class SerializationSpecTest { |
| 36 | + |
| 37 | + public static void main(String[] args) throws Exception { |
| 38 | + // Serialization form for ICC_Profile includes version, profile name, |
| 39 | + // and profile data. If the name is invalid or does not match a standard |
| 40 | + // profile, the data is used. An exception is thrown only if both the |
| 41 | + // name and the data are invalid, or if one of them is missing or is of |
| 42 | + // the wrong type. |
| 43 | + |
| 44 | + // Naming conventions used in test file names: |
| 45 | + // null : null reference |
| 46 | + // valid : valid standard profile name or valid profile data (byte[]) |
| 47 | + // invalid : unrecognized name or data with incorrect ICC header |
| 48 | + // wrongType: incorrect type, e.g., int[] instead of String or byte[] |
| 49 | + |
| 50 | + // No name or data |
| 51 | + test("empty", OptionalDataException.class); |
| 52 | + |
| 53 | + // Cases where only the profile name is present (no profile data) |
| 54 | + test("null", OptionalDataException.class); |
| 55 | + test("valid", OptionalDataException.class); |
| 56 | + test("invalid", OptionalDataException.class); |
| 57 | + test("wrongType", InvalidObjectException.class); |
| 58 | + |
| 59 | + // The test files are named as <name>_<data>.ser |
| 60 | + test("null_null", InvalidObjectException.class); |
| 61 | + test("null_valid", null); // valid data is enough if name is null |
| 62 | + test("null_invalid", InvalidObjectException.class); |
| 63 | + test("null_wrongType", InvalidObjectException.class); |
| 64 | + |
| 65 | + test("invalid_null", InvalidObjectException.class); |
| 66 | + test("invalid_valid", null); // valid data is enough if name is invalid |
| 67 | + test("invalid_invalid", InvalidObjectException.class); |
| 68 | + test("invalid_wrongType", InvalidObjectException.class); |
| 69 | + |
| 70 | + test("wrongType_null", InvalidObjectException.class); |
| 71 | + test("wrongType_valid", InvalidObjectException.class); |
| 72 | + test("wrongType_invalid", InvalidObjectException.class); |
| 73 | + test("wrongType_wrongType", InvalidObjectException.class); |
| 74 | + |
| 75 | + test("valid_null", null); // the valid name is enough |
| 76 | + test("valid_valid", null); // the valid name is enough |
| 77 | + test("valid_invalid", null); // the valid name is enough |
| 78 | + test("valid_wrongType", InvalidObjectException.class); |
| 79 | + } |
| 80 | + |
| 81 | + private static void test(String test, Class<?> expected) { |
| 82 | + String fileName = test + ".ser"; |
| 83 | + File file = new File(System.getProperty("test.src", "."), fileName); |
| 84 | + Class<?> actual = null; |
| 85 | + try (var fis = new FileInputStream(file); |
| 86 | + var ois = new ObjectInputStream(fis)) |
| 87 | + { |
| 88 | + ois.readObject(); |
| 89 | + } catch (Exception e) { |
| 90 | + actual = e.getClass(); |
| 91 | + } |
| 92 | + if (actual != expected) { |
| 93 | + System.err.println("Test: " + test); |
| 94 | + System.err.println("Expected: " + expected); |
| 95 | + System.err.println("Actual: " + actual); |
| 96 | + throw new RuntimeException("Test failed"); |
| 97 | + } |
| 98 | + } |
| 99 | +} |
0 commit comments