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