View Javadoc
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  
20  package org.apache.wss4j.dom.message.token;
21  
22  import org.apache.wss4j.common.bsp.BSPEnforcer;
23  
24  
25  import org.junit.jupiter.api.Test;
26  
27  import static org.junit.jupiter.api.Assertions.assertEquals;
28  import static org.junit.jupiter.api.Assertions.assertFalse;
29  import static org.junit.jupiter.api.Assertions.assertTrue;
30  
31  /**
32   * Tests for DerivedKeyToken type.
33   */
34  
35  public class DerivedKeyTokenTest {
36  
37      private static final String TEST_TOKEN_TEMPLATE = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
38              + "<wsc:DerivedKeyToken "
39              + "xmlns:wsc=\"http://schemas.xmlsoap.org/ws/2005/02/sc\" xmlns:wsu=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd\" wsu:Id=\"_3\" >"
40              + "<wsse:SecurityTokenReference "
41              + "xmlns:wsse=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\" wsu:Id=\"_5002\">"
42              + "<wsse:Reference ValueType=\"http://schemas.xmlsoap.org/ws/2005/02/sc/sct\" URI=\"PLACEHOLDER1\" />"
43              + "</wsse:SecurityTokenReference>"
44              + "<wsc:Offset>0</wsc:Offset>"
45              + "<wsc:Length>PLACEHOLDER2</wsc:Length>"
46              + "<wsc:Nonce>Kq/1ptgjZpX2g1q6MiJcSfTX</wsc:Nonce>"
47              + "<wsc:Label>WS-SecureConversationWS-SecureConversation</wsc:Label>"
48              + "<wsc:Generation>3</wsc:Generation>"
49              + "<wsc:Properties>"
50              + "<wsc:Name>.../derivedKeySource</wsc:Name>"
51              + "</wsc:Properties>"
52              + "</wsc:DerivedKeyToken>";
53  
54      private DerivedKeyToken token;
55      private DerivedKeyToken tokenEqual;
56      private DerivedKeyToken tokenNotEqual;
57  
58      public DerivedKeyTokenTest() throws Exception {
59          token = new DerivedKeyToken(createReferenceDocument(
60                  TEST_TOKEN_TEMPLATE,
61                  "#uuid-4063ae9b-fe66-4e09-a5fb-8fda903f34d8", "16")
62                  .getDocumentElement(), new BSPEnforcer());
63          tokenEqual = new DerivedKeyToken(createReferenceDocument(
64                  TEST_TOKEN_TEMPLATE,
65                  "#uuid-4063ae9b-fe66-4e09-a5fb-8fda903f34d8", "16")
66                  .getDocumentElement(), new BSPEnforcer());
67          tokenNotEqual = new DerivedKeyToken(createReferenceDocument(
68                  TEST_TOKEN_TEMPLATE,
69                  "#uuid-5603ae9b-fe66-4e09-a5fb-8fda903f34d8", "88")
70                  .getDocumentElement(), new BSPEnforcer());
71      }
72  
73      @Test
74      public void testEquals() throws Exception{
75          assertTrue(token.equals(tokenEqual));
76          assertTrue(tokenEqual.equals(token));
77          assertFalse(token.equals(tokenNotEqual));
78          assertFalse(token.equals(null));
79          assertFalse("string".equals(token));
80      }
81  
82      @Test
83      public void testHashcode() throws Exception{
84          assertEquals(token.hashCode(), tokenEqual.hashCode());
85          assertFalse(token.hashCode() == tokenNotEqual.hashCode());
86      }
87  
88      private static org.w3c.dom.Document createReferenceDocument(
89              final String template, final String placeholder1,
90              final String placeholder2)
91              throws javax.xml.parsers.ParserConfigurationException,
92              org.xml.sax.SAXException, java.io.IOException {
93          final java.io.InputStream in = new java.io.ByteArrayInputStream(
94                  template.replaceFirst("PLACEHOLDER1", placeholder1)
95                          .replaceFirst("PLACEHOLDER2", placeholder2).getBytes());
96          final javax.xml.parsers.DocumentBuilderFactory factory = javax.xml.parsers.DocumentBuilderFactory
97                  .newInstance();
98          factory.setNamespaceAware(true);
99          final javax.xml.parsers.DocumentBuilder builder = factory
100                 .newDocumentBuilder();
101         return builder.parse(in);
102     }
103 }