Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Preventing serialization of incorrect XML characters (MID-1944)
- Loading branch information
Showing
5 changed files
with
140 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
infra/prism/src/test/java/com/evolveum/midpoint/prism/TestXmlSerialization.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* Copyright (c) 2010-2014 Evolveum | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.evolveum.midpoint.prism; | ||
|
||
import com.evolveum.midpoint.prism.PrismContext; | ||
import com.evolveum.midpoint.prism.PrismInternalTestUtil; | ||
import com.evolveum.midpoint.prism.crypto.Protector; | ||
import com.evolveum.midpoint.prism.crypto.TestProtector; | ||
import com.evolveum.midpoint.prism.parser.util.XNodeProcessorUtil; | ||
import com.evolveum.midpoint.prism.util.PrismTestUtil; | ||
import com.evolveum.midpoint.prism.xnode.MapXNode; | ||
import com.evolveum.midpoint.prism.xnode.PrimitiveXNode; | ||
import com.evolveum.midpoint.util.PrettyPrinter; | ||
import com.evolveum.midpoint.util.exception.SchemaException; | ||
import com.evolveum.prism.xml.ns._public.types_3.ProtectedStringType; | ||
import org.apache.xml.security.encryption.XMLCipher; | ||
import org.testng.annotations.BeforeSuite; | ||
import org.testng.annotations.Test; | ||
import org.xml.sax.SAXException; | ||
|
||
import javax.xml.namespace.QName; | ||
import java.io.IOException; | ||
|
||
import static com.evolveum.midpoint.prism.PrismInternalTestUtil.DEFAULT_NAMESPACE_PREFIX; | ||
import static com.evolveum.midpoint.prism.PrismInternalTestUtil.displayTestTitle; | ||
import static org.testng.Assert.assertTrue; | ||
import static org.testng.AssertJUnit.assertEquals; | ||
|
||
/** | ||
* @author mederly | ||
* | ||
*/ | ||
public class TestXmlSerialization { | ||
|
||
@BeforeSuite | ||
public void setupDebug() throws SchemaException, SAXException, IOException { | ||
PrettyPrinter.setDefaultNamespacePrefix(DEFAULT_NAMESPACE_PREFIX); | ||
PrismTestUtil.resetPrismContext(new PrismInternalTestUtil()); | ||
} | ||
|
||
@Test | ||
public void testHandlingInvalidChars() throws Exception { | ||
final String TEST_NAME = "testHandlingInvalidChars"; | ||
displayTestTitle(TEST_NAME); | ||
|
||
// GIVEN | ||
|
||
PrismContext prismContext = PrismTestUtil.getPrismContext(); | ||
|
||
// WHEN | ||
|
||
PrimitiveXNode<String> valOkNode = new PrimitiveXNode<>("abcdef"); | ||
PrimitiveXNode<String> valWrongNode = new PrimitiveXNode<>("abc\1def"); | ||
|
||
// THEN | ||
|
||
String ok = prismContext.getParserDom().serializeToString(valOkNode, new QName("ok")); | ||
System.out.println("correct value serialized to: " + ok); | ||
assertEquals("Wrong serialization", "<ok>abcdef</ok>", ok.trim()); // todo make this less brittle with regards to serialization style | ||
|
||
try { | ||
String wrong = prismContext.getParserDom().serializeToString(valWrongNode, new QName("wrong")); | ||
System.out.println("wrong value serialized to: " + wrong); | ||
assert false : "Wrong value serialization had to fail but it didn't!"; | ||
} catch (RuntimeException e) { | ||
System.out.println("wrong value was not serialized (as expected): " + e); | ||
assertTrue(e.getMessage().contains("Invalid character"), "Didn't get expected error message"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters