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 import javax.crypto.SecretKey;
24
25 public class SecretKeyCacheTest extends AbstractJsfTestCase
26 {
27
28 public SecretKeyCacheTest(String name)
29 {
30 super(name);
31 }
32
33
34
35
36
37
38 public void setUp() throws Exception
39 {
40 super.setUp();
41
42 servletContext.addInitParameter(StateUtils.INIT_SECRET,
43 AbstractStateUtilsTest.BASE64_KEY_SIZE_8);
44 servletContext.addInitParameter(StateUtils.INIT_MAC_SECRET, AbstractStateUtilsTest.BASE64_KEY_SIZE_8);
45 }
46
47 public void testDefaultAlgorithmUse(){
48
49 StateUtils.initSecret(servletContext);
50
51 SecretKey secretKey = (SecretKey) servletContext.getAttribute(StateUtils.INIT_SECRET_KEY_CACHE);
52
53 assertTrue("Making sure MyFaces uses the " +
54 "default algorithm when one is not specified",
55 StateUtils.DEFAULT_ALGORITHM.equals(secretKey.getAlgorithm()));
56
57 }
58
59 public void testInitFacesWithoutCache(){
60
61 servletContext.addInitParameter(StateUtils.INIT_SECRET_KEY_CACHE, "false");
62
63 StateUtils.initSecret(servletContext);
64
65 Object object = servletContext.getAttribute(StateUtils.INIT_SECRET_KEY_CACHE);
66
67 assertNull("Making sure StateUtils.initSecret does not create a SecretKey", object);
68
69 }
70
71 public void testInitFacesWithCache(){
72
73 StateUtils.initSecret(servletContext);
74
75 Object object = servletContext.getAttribute(StateUtils.INIT_SECRET_KEY_CACHE);
76
77 assertFalse("Making sure StateUtils.initSecret() puts an object in application scope",
78 object == null);
79
80 assertTrue("Making sure StateUtils.initSecret() is creating a SecretKey",
81 object instanceof SecretKey);
82
83 }
84
85 }