1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.wss4j.dom.message.token;
21
22 import org.apache.wss4j.common.ext.WSSecurityException;
23 import org.apache.wss4j.common.token.Reference;
24
25 import org.apache.wss4j.dom.engine.WSSConfig;
26
27 import org.junit.jupiter.api.Test;
28
29 import static org.junit.jupiter.api.Assertions.assertEquals;
30 import static org.junit.jupiter.api.Assertions.assertFalse;
31 import static org.junit.jupiter.api.Assertions.assertTrue;
32 import static org.junit.jupiter.api.Assertions.fail;
33
34
35
36
37 public class ReferenceTest {
38
39 private static final String
40 TEST_REFERENCE_TEMPLATE =
41 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
42 + "<wss:Reference "
43 + "xmlns:wss=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\" "
44 + "ValueType=\"TheValueType\" "
45 + "URI=\"TheURI\" "
46 + "/>"
47 ;
48
49 private static final String
50 BOGUS_REFERENCE_TEMPLATE =
51 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
52 + "<wss:Reference "
53 + "xmlns:wss=\"http://something-completely-different\" "
54 + "ValueType=\"TheValueType\" "
55 + "URI=\"TheURI\" "
56 + "/>"
57 ;
58
59 private Reference ref;
60 private Reference refEqual;
61 private Reference refNotEqual;
62
63 public ReferenceTest() throws Exception{
64 ref = new Reference (createReferenceDocument(TEST_REFERENCE_TEMPLATE, "test", "equalscheck").getDocumentElement());
65 refEqual = new Reference (createReferenceDocument(TEST_REFERENCE_TEMPLATE, "test", "equalscheck").getDocumentElement());
66 refNotEqual = new Reference (createReferenceDocument(TEST_REFERENCE_TEMPLATE, "test", "unequalscheck").getDocumentElement());
67 WSSConfig.init();
68 }
69
70
71 @Test
72 public void testConstructor() throws Exception {
73
74
75
76 try {
77 new Reference((org.w3c.dom.Element) null);
78 fail("Expected failure on null Element passed to ctor");
79 } catch (final WSSecurityException ex) {
80 assertTrue(ex.getErrorCode() == WSSecurityException.ErrorCode.INVALID_SECURITY);
81 }
82
83
84
85 try {
86 new Reference(
87 createReferenceDocument(
88 BOGUS_REFERENCE_TEMPLATE,
89 "foo", "bar"
90 ).getDocumentElement()
91 );
92 fail("Expected failure on bogus template");
93 } catch (final WSSecurityException ex) {
94 assertTrue(ex.getErrorCode() == WSSecurityException.ErrorCode.FAILURE);
95 }
96
97
98
99 new Reference(
100 createReferenceDocument(
101 TEST_REFERENCE_TEMPLATE,
102 "foo", "bar"
103 )
104 );
105 new Reference(
106 createReferenceDocument(
107 TEST_REFERENCE_TEMPLATE,
108 "foo", "bar"
109 ).getDocumentElement()
110 );
111 }
112
113 @Test
114 public void
115 testAccessors() throws Exception {
116 final Reference ref = new Reference(
117 createReferenceDocument(
118 TEST_REFERENCE_TEMPLATE,
119 "foo", "bar"
120 ).getDocumentElement()
121 );
122 assertEquals(ref.getValueType(), "foo");
123 assertEquals(ref.getURI(), "bar");
124 }
125
126 @Test
127 public void testEquals() throws Exception{
128 assertTrue(ref.equals(refEqual));
129 assertTrue(refEqual.equals(ref));
130 assertFalse(ref.equals(refNotEqual));
131 assertFalse(ref.equals(null));
132 assertFalse("string".equals(ref));
133 }
134
135 @Test
136 public void testHashcode() throws Exception{
137 assertEquals(ref.hashCode(), refEqual.hashCode());
138 assertFalse(ref.hashCode() == refNotEqual.hashCode());
139 }
140
141 private static org.w3c.dom.Document
142 createReferenceDocument(
143 final String template,
144 final String valueType,
145 final String uri
146 ) throws javax.xml.parsers.ParserConfigurationException,
147 org.xml.sax.SAXException,
148 java.io.IOException {
149 final java.io.InputStream in =
150 new java.io.ByteArrayInputStream(
151 template.replaceFirst(
152 "TheValueType", valueType
153 ).replaceFirst(
154 "TheURI", uri
155 ).getBytes()
156 );
157 final javax.xml.parsers.DocumentBuilderFactory factory =
158 javax.xml.parsers.DocumentBuilderFactory.newInstance();
159 factory.setNamespaceAware(true);
160 final javax.xml.parsers.DocumentBuilder builder = factory.newDocumentBuilder();
161 return builder.parse(in);
162 }
163 }