1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
32
33
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 }