View Javadoc

1   /*
2    * Copyright 2004-2006 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  
18  package org.apache.myfaces.shared.util;
19  
20  import junit.framework.Test;
21  import org.apache.myfaces.test.base.AbstractJsfTestCase;
22  
23  public class SecretKeyConfigurationTest extends AbstractJsfTestCase
24  {
25  
26      public SecretKeyConfigurationTest(String name)
27      {
28          super(name);
29      }
30      
31      // No longer necessary using junit 4 to run tests
32      //public static Test suite() {
33      //    return null; // keep this method or maven won't run it
34      //}
35      
36      public void setUp() throws Exception
37      {
38          super.setUp();
39          servletContext.addInitParameter(StateUtils.INIT_SECRET, "shouldn't matter");
40          servletContext.addInitParameter(StateUtils.INIT_MAC_SECRET, AbstractStateUtilsTest.BASE64_KEY_SIZE_8);
41          
42      }
43  
44      public void testMissingSecretKeyEncrypt(){
45          
46          try{
47              StateUtils.encrypt("serialized objects".getBytes(), externalContext);
48              fail("An exception should be thrown if there" +
49                      " is no SecretKey in application scope and cacheing is enabled ");
50          }catch(NullPointerException e){
51          }
52          
53      }
54      
55      public void testNonSecretKeyEncrypt(){
56          
57          servletContext.setAttribute(StateUtils.INIT_SECRET_KEY_CACHE, new Integer(8));
58          
59          try{
60              
61              StateUtils.encrypt("serialized objects".getBytes(), externalContext);
62              fail("An exception should be thrown if there" +
63                      " is no SecretKey in application scope and cacheing is enabled ");
64          }catch(ClassCastException cce){
65          }
66          
67      }
68      
69      public void testMissingSecretKeyDecrypt(){
70          
71          boolean npeThrown = false;
72          
73          try{
74              StateUtils.decrypt("serialized objects".getBytes(), externalContext);
75          }catch(NullPointerException e){
76              npeThrown = true;
77          }
78          
79          assertTrue("An exception should be thrown if there" +
80                  " is no SecretKey in application scope and cacheing is enabled ", npeThrown);
81      }
82      
83      public void testNonSecretKeyDecrypt(){
84          
85          servletContext.setAttribute(StateUtils.INIT_SECRET_KEY_CACHE, new Integer(8));
86          
87          try{
88              
89              StateUtils.decrypt("serialized objects".getBytes(), externalContext);
90              fail("An exception should be thrown if there" +
91                      " is no SecretKey in application scope and cacheing is enabled ");
92          }catch(ClassCastException cce){
93          }
94          
95      }
96  
97  }