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  /**
24   * @author Dennis C. Byrne
25   */
26  
27  public class SecretKeyConfigurationTest extends AbstractJsfTestCase
28  {
29  
30      public SecretKeyConfigurationTest(String name)
31      {
32          super(name);
33      }
34      
35      // No longer necessary using junit 4 to run tests
36      //public static Test suite() {
37      //    return null; // keep this method or maven won't run it
38      //}
39      
40      public void setUp() throws Exception
41      {
42          super.setUp();
43          servletContext.addInitParameter(StateUtils.INIT_SECRET, "shouldn't matter");
44          servletContext.addInitParameter(StateUtils.INIT_MAC_SECRET, AbstractStateUtilsTest.BASE64_KEY_SIZE_8);
45          
46      }
47  
48      public void testMissingSecretKeyEncrypt(){
49          
50          try{
51              StateUtils.encrypt("serialized objects".getBytes(), externalContext);
52              fail("An exception should be thrown if there" +
53                      " is no SecretKey in application scope and cacheing is enabled ");
54          }catch(NullPointerException e){
55          }
56          
57      }
58      
59      public void testNonSecretKeyEncrypt(){
60          
61          servletContext.setAttribute(StateUtils.INIT_SECRET_KEY_CACHE, new Integer(8));
62          
63          try{
64              
65              StateUtils.encrypt("serialized objects".getBytes(), externalContext);
66              fail("An exception should be thrown if there" +
67                      " is no SecretKey in application scope and cacheing is enabled ");
68          }catch(ClassCastException cce){
69          }
70          
71      }
72      
73      public void testMissingSecretKeyDecrypt(){
74          
75          boolean npeThrown = false;
76          
77          try{
78              StateUtils.decrypt("serialized objects".getBytes(), externalContext);
79          }catch(NullPointerException e){
80              npeThrown = true;
81          }
82          
83          assertTrue("An exception should be thrown if there" +
84                  " is no SecretKey in application scope and cacheing is enabled ", npeThrown);
85      }
86      
87      public void testNonSecretKeyDecrypt(){
88          
89          servletContext.setAttribute(StateUtils.INIT_SECRET_KEY_CACHE, new Integer(8));
90          
91          try{
92              
93              StateUtils.decrypt("serialized objects".getBytes(), externalContext);
94              fail("An exception should be thrown if there" +
95                      " is no SecretKey in application scope and cacheing is enabled ");
96          }catch(ClassCastException cce){
97          }
98          
99      }
100 
101 }