-
Notifications
You must be signed in to change notification settings - Fork 736
/
ConfigurationTest.java
236 lines (207 loc) · 9.4 KB
/
ConfigurationTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* See LICENSE.txt included in this distribution for the specific
* language governing permissions and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at LICENSE.txt.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 2017, 2021, Oracle and/or its affiliates. All rights reserved.
* Portions Copyright (c) 2020, Chris Fraire <cfraire@me.com>.
*/
package org.opengrok.indexer.configuration;
import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.LinkedList;
import java.util.stream.Collectors;
import javax.xml.XMLConstants;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.opengrok.indexer.util.ClassUtil;
import org.xml.sax.Attributes;
import org.xml.sax.ext.DefaultHandler2;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
/**
*
* @author Krystof Tulinger
*/
public class ConfigurationTest {
private static class Handler extends DefaultHandler2 {
Handler() {
}
@Override
public void startElement(String uri, String localName, String qname, Attributes attr) {
if ("void".equals(qname)) {
String prop = null;
if ((prop = attr.getValue("property")) != null) {
for (Field f : Group.class.getDeclaredFields()) {
if (Modifier.isTransient(f.getModifiers())) {
assertNotEquals(prop, f.getName(), "'" + f.getName() +
"' field is transient and yet is present in XML " +
"encoding of Group object");
}
}
}
}
}
}
/**
* Verify that encoding of Group class does not contain transient members.
* @throws Exception exception
*/
@Test
public void testTransientKeywordGroups() throws Exception {
Group foo = new Group("foo", "foo.*");
Group bar = new Group("bar", "bar.*");
Configuration cfg = new Configuration();
cfg.addGroup(foo);
foo.addGroup(bar);
cfg.addGroup(bar);
ByteArrayOutputStream out = new ByteArrayOutputStream();
try (XMLEncoder enc = new XMLEncoder(out)) {
enc.writeObject(cfg);
}
// In this test we are no so much interested in exceptions during the
// XML decoding as that is covered by the {@code serializationOrderTest}
// test.
try (ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray())) {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
saxParser.setProperty(XMLConstants.ACCESS_EXTERNAL_DTD, ""); // Compliant
saxParser.setProperty(XMLConstants.ACCESS_EXTERNAL_SCHEMA, ""); // compliant
Handler handler = new Handler();
saxParser.parse(new BufferedInputStream(in), handler);
}
}
/**
* Test for a serialization bug in configuration. The problem is that with
* this scenario the output is written in a way that when deserializing the
* input later on, we get {@link NullPointerException} trying to use
* {@link Group#compareTo(Group)}. This exception is caused by wrong order
* of serialization of
* {@link Group#getDescendants()}, {@link Group#getParent()} and
* {@link Project#getGroups()} where the backpointers in a {@link Project}
* to several {@link Group}s shall be stored in a set while this
* {@link Group} does not have a name yet (= {@code null}).
*
* @throws IOException I/O exception
* @see ClassUtil#remarkTransientFields(java.lang.Class)
* ClassUtil#remarkTransientFields() for suggested solution
*/
@Test
public void serializationOrderTest() throws IOException {
Project project = new Project("project");
Group apache = new Group("Apache", "test.*");
Group bsd = new Group("BSD", "test.*");
Group opensource = new Group("OpenSource", "test.*");
opensource.addGroup(apache);
opensource.addGroup(bsd);
bsd.addProject(project);
opensource.addProject(project);
project.getGroups().add(opensource);
project.getGroups().add(bsd);
Configuration cfg = new Configuration();
Configuration oldCfg = cfg;
cfg.addGroup(apache);
cfg.addGroup(bsd);
cfg.addGroup(opensource);
ByteArrayOutputStream out = new ByteArrayOutputStream();
try (XMLEncoder enc = new XMLEncoder(out)) {
enc.writeObject(cfg);
}
// Create an exception listener to detect errors while encoding and
// decoding
final LinkedList<Exception> exceptions = new LinkedList<>();
try (ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
XMLDecoder dec = new XMLDecoder(in, null, exceptions::addLast)) {
cfg = (Configuration) dec.readObject();
assertNotNull(cfg);
// verify that the read didn't fail
if (!exceptions.isEmpty()) {
// Can only chain one of the exceptions. Take the first one.
throw new AssertionError("Got " + exceptions.size() + " exception(s)", exceptions.getFirst());
}
assertEquals(oldCfg.getGroups(), cfg.getGroups());
}
}
@ParameterizedTest
@ValueSource(strings = {
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<java version=\"11.0.8\" class=\"java.beans.XMLDecoder\">\n" +
" <object class=\"java.lang.Runtime\" method=\"getRuntime\">\n" +
" <void method=\"exec\">\n" +
" <array class=\"java.lang.String\" length=\"2\">\n" +
" <void index=\"0\">\n" +
" <string>/usr/bin/nc</string>\n" +
" </void>\n" +
" <void index=\"1\">\n" +
" <string>-l</string>\n" +
" </void>\n" +
" </array>\n" +
" </void>\n" +
" </object>\n" +
"</java>",
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<java version=\"11.0.8\" class=\"java.beans.XMLDecoder\">\n" +
" <object class=\"java.lang.ProcessBuilder\">\n" +
" <array class=\"java.lang.String\" length=\"1\" >\n" +
" <void index=\"0\"> \n" +
" <string>/usr/bin/curl https://oracle.com</string>\n" +
" </void>\n" +
" </array>\n" +
" <void method=\"start\"/>\n" +
" </object>\n" +
"</java>",
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<java version=\"11.0.8\" class=\"java.beans.XMLDecoder\">\n" +
" <object class = \"java.io.FileOutputStream\"> \n" +
" <string>opengrok_test.txt</string>\n" +
" <method name = \"write\">\n" +
" <array class=\"byte\" length=\"3\">\n" +
" <void index=\"0\"><byte>96</byte></void>\n" +
" <void index=\"1\"><byte>96</byte></void>\n" +
" <void index=\"2\"><byte>96</byte></void>\n" +
" </array>\n" +
" </method>\n" +
" <method name=\"close\"/>\n" +
" </object>\n" +
"</java>"
})
void testDeserializationOfNotWhiteListedClassThrowsError(final String exploit) {
assertThrows(IllegalAccessError.class, () -> Configuration.makeXMLStringAsConfiguration(exploit));
}
@Test
void testLoadingValidConfiguration() throws IOException {
try (var br = new BufferedReader(new InputStreamReader(ConfigurationTest.class.getClassLoader()
.getResourceAsStream("configuration/valid_configuration.xml")))) {
String xml = br.lines().collect(Collectors.joining(System.lineSeparator()));
var config = Configuration.makeXMLStringAsConfiguration(xml);
assertEquals("/opt/opengrok_data", config.getDataRoot());
}
}
}