1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.commons.io.serialization;
20
21 import static org.junit.jupiter.api.Assertions.assertEquals;
22
23 import java.io.ByteArrayInputStream;
24 import java.io.ByteArrayOutputStream;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.io.ObjectInputStream;
28 import java.io.ObjectOutputStream;
29 import java.util.ArrayList;
30 import java.util.Random;
31
32 import org.junit.jupiter.api.BeforeEach;
33 import org.junit.jupiter.api.Test;
34
35
36
37
38
39
40 class MoreComplexObjectTest extends AbstractCloseableListTest {
41
42 private InputStream inputStream;
43 private MoreComplexObject original;
44
45 private void assertSerialization(final ObjectInputStream ois) throws ClassNotFoundException, IOException {
46 final MoreComplexObject copy = (MoreComplexObject) ois.readObject();
47 assertEquals(original.toString(), copy.toString(), "Expecting same data after deserializing");
48 }
49
50 @BeforeEach
51 public void setupMoreComplexObject() throws IOException {
52 original = new MoreComplexObject();
53 final ByteArrayOutputStream bos = addCloseable(new ByteArrayOutputStream());
54 final ObjectOutputStream oos = addCloseable(new ObjectOutputStream(bos));
55 oos.writeObject(original);
56 inputStream = addCloseable(new ByteArrayInputStream(bos.toByteArray()));
57 }
58
59
60
61
62 @Test
63 void testTrustJavaIncludingArrays() throws IOException, ClassNotFoundException {
64
65 assertSerialization(addCloseable(
66 ValidatingObjectInputStream.builder()
67 .setInputStream(inputStream)
68 .accept(MoreComplexObject.class)
69 .accept("java.*", "[Ljava.*")
70 .get()
71 ));
72
73 }
74
75
76
77
78
79 @Test
80 void testTrustJavaLang() throws IOException, ClassNotFoundException {
81
82 assertSerialization(addCloseable(
83 ValidatingObjectInputStream.builder()
84 .setInputStream(inputStream)
85 .accept(MoreComplexObject.class, ArrayList.class, Random.class)
86 .accept("java.lang.*", "[Ljava.lang.*")
87 .get()
88 ));
89
90 }
91
92
93
94
95
96
97 @Test
98 void testUseBlacklist() throws IOException, ClassNotFoundException {
99 final String [] blacklist = {
100 "org.apache.commons.collections.functors.InvokerTransformer",
101 "org.codehaus.groovy.runtime.ConvertedClosure",
102 "org.codehaus.groovy.runtime.MethodClosure",
103 "org.springframework.beans.factory.ObjectFactory"
104 };
105
106 assertSerialization(addCloseable(
107 ValidatingObjectInputStream.builder()
108 .setInputStream(inputStream)
109 .accept("*")
110 .reject(blacklist)
111 .get()
112 ));
113
114 }
115 }