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
24
25
26
27 public class SecretKeyConfigurationTest extends AbstractJsfTestCase
28 {
29
30 public SecretKeyConfigurationTest(String name)
31 {
32 super(name);
33 }
34
35
36
37
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 }