|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.ofbiz.base.util; |
| 20 | + |
| 21 | +import static org.apache.ofbiz.base.util.UtilObject.getObjectException; |
| 22 | +import static org.hamcrest.Matchers.contains; |
| 23 | +import static org.junit.Assert.assertThat; |
| 24 | + |
| 25 | +import java.io.ByteArrayOutputStream; |
| 26 | +import java.io.IOException; |
| 27 | +import java.io.ObjectOutputStream; |
| 28 | +import java.util.Arrays; |
| 29 | +import java.util.List; |
| 30 | + |
| 31 | +import org.junit.Test; |
| 32 | + |
| 33 | +public class UtilObjectUnitTest { |
| 34 | + // Test reading a basic list of string object. |
| 35 | + @Test |
| 36 | + public void testGetObjectExceptionSafe() throws IOException, ClassNotFoundException { |
| 37 | + try (ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
| 38 | + ObjectOutputStream oos = new ObjectOutputStream(bos)) { |
| 39 | + List<String> allowedObject = Arrays.asList("foo", "bar", "baz"); |
| 40 | + oos.writeObject(allowedObject); |
| 41 | + List<String> readObject = UtilGenerics.cast(getObjectException(bos.toByteArray())); |
| 42 | + assertThat(readObject, contains("foo", "bar", "baz")); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + // Test reading a valid customized list of string object. |
| 47 | + @Test |
| 48 | + public void testGetObjectExceptionCustomized() throws IOException, ClassNotFoundException { |
| 49 | + UtilProperties.setPropertyValueInMemory("SafeObjectInputStream", "ListOfSafeObjectsForInputStream", |
| 50 | + "java.util.Arrays.ArrayList,java.lang.String"); |
| 51 | + testGetObjectExceptionSafe(); |
| 52 | + |
| 53 | + // With extra whitespace |
| 54 | + UtilProperties.setPropertyValueInMemory("SafeObjectInputStream", "ListOfSafeObjectsForInputStream", |
| 55 | + "java.util.Arrays.ArrayList, java.lang.String"); |
| 56 | + testGetObjectExceptionSafe(); |
| 57 | + } |
| 58 | + |
| 59 | + // Test reading a basic list of string object after forbidding such kind of objects. |
| 60 | + @Test(expected = ClassCastException.class) |
| 61 | + public void testGetObjectExceptionUnsafe() throws IOException, ClassNotFoundException { |
| 62 | + // Only allow object of type where the package prefix is 'org.apache.ofbiz' |
| 63 | + UtilProperties.setPropertyValueInMemory("SafeObjectInputStream", "ListOfSafeObjectsForInputStream", |
| 64 | + "org.apache.ofbiz..*"); |
| 65 | + try (ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
| 66 | + ObjectOutputStream oos = new ObjectOutputStream(bos)) { |
| 67 | + List<String> forbiddenObject = Arrays.asList("foo", "bar", "baz"); |
| 68 | + oos.writeObject(forbiddenObject); |
| 69 | + getObjectException(bos.toByteArray()); |
| 70 | + } |
| 71 | + } |
| 72 | +} |
0 commit comments