This repository has been archived by the owner on Nov 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 481
/
HttpSessionStorageTest.java
201 lines (175 loc) · 7.97 KB
/
HttpSessionStorageTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/* Copyright 2009 Vladimir Schäfer
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.security.saml.storage;
import org.junit.Test;
import org.opensaml.saml2.core.Assertion;
import org.opensaml.saml2.core.Audience;
import org.opensaml.saml2.core.AuthnRequest;
import org.springframework.security.saml.parser.SAMLObject;
import org.springframework.web.util.WebUtils;
import javax.servlet.http.HttpSession;
import java.util.Hashtable;
import static org.easymock.EasyMock.*;
import static org.junit.Assert.*;
/**
* @author Vladimir Schäfer
*/
public class HttpSessionStorageTest {
HttpSession session;
HttpSessionStorage cache;
AuthnRequest request;
static String SPRING_SAML_STORAGE_KEY = "_springSamlStorageKey";
/**
* Verifies that in case the session doesn't yet contain the SAML storage it will be created.
*/
@Test
public void testNonExisting() {
session = createMock(HttpSession.class);
expect(session.getId()).andReturn("session123").anyTimes();
expect(session.getAttribute(WebUtils.SESSION_MUTEX_ATTRIBUTE)).andReturn(session);
expect(session.getAttribute(SPRING_SAML_STORAGE_KEY)).andReturn(null);
expect(session.getAttribute(SPRING_SAML_STORAGE_KEY)).andReturn(null);
session.setAttribute(eq(SPRING_SAML_STORAGE_KEY), notNull());
replay(session);
cache = new HttpSessionStorage(session);
assertNull(cache.retrieveMessage("test"));
assertNotNull(cache.getAllMessages());
assertEquals(0, cache.getAllMessages().size());
verify(session);
}
/**
* Verifies that once the session is set (call to getAttribute returns some value), the newly created
* storage won't try to overwrite the preexisting value.
*/
@Test
public void testRaceInitialization() throws Exception {
session = createMock(HttpSession.class);
expect(session.getId()).andReturn("session123").anyTimes();
expect(session.getAttribute(WebUtils.SESSION_MUTEX_ATTRIBUTE)).andReturn(session).anyTimes();
expect(session.getAttribute(SPRING_SAML_STORAGE_KEY)).andReturn(null).times(3);
session.setAttribute(eq(SPRING_SAML_STORAGE_KEY), notNull());
expect(session.getAttribute(SPRING_SAML_STORAGE_KEY)).andReturn(new Hashtable());
replay(session);
class TestRunner extends Thread {
public SAMLMessageStorage storage;
public void start() {
storage = new HttpSessionStorage(session);
storage.retrieveMessage("abc123");
}
}
TestRunner a = new TestRunner();
TestRunner b = new TestRunner();
// Make both threads verify that the session is empty and wait for the lock
// After one of the thread receives the lock, it will set the session, while
// the second must reverify whether it was already set or not
synchronized (session) {
a.start();
b.start();
synchronized (this) {
wait(50);
}
}
verify(session);
assertNotNull(a.storage);
assertNotNull(b.storage);
}
/**
* Verifies that in case the session already includes the SAML storage, though empty, it behaves
* as expected.
*/
@Test
public void testEmptyExisting() {
Hashtable<String, SAMLObject> storage = new Hashtable<String, SAMLObject>();
session = createMock(HttpSession.class);
expect(session.getId()).andReturn("session123").anyTimes();
expect(session.getAttribute(SPRING_SAML_STORAGE_KEY)).andReturn(storage);
replay(session);
cache = new HttpSessionStorage(session);
assertNull(cache.retrieveMessage("test"));
assertNotNull(cache.getAllMessages());
assertEquals(0, cache.getAllMessages().size());
verify(session);
}
/**
* Verifies that in case the session already includes the SAML storage, though empty, it behaves
* as expected.
*/
@Test
public void testNonEmptyExisting() {
Hashtable<String, SAMLObject> storage = new Hashtable<String, SAMLObject>();
Audience audienceMock = createNiceMock(Audience.class);
SAMLObject<Audience> audience = new SAMLObject<Audience>(audienceMock);
storage.put("testKey", audience);
session = createMock(HttpSession.class);
expect(session.getId()).andReturn("session123").anyTimes();
expect(session.getAttribute(SPRING_SAML_STORAGE_KEY)).andReturn(storage);
session.setAttribute(eq(SPRING_SAML_STORAGE_KEY), anyObject());
replay(session);
cache = new HttpSessionStorage(session);
assertNotNull(cache.getAllMessages());
assertEquals(1, cache.getAllMessages().size());
assertEquals(audienceMock, cache.retrieveMessage("testKey"));
verify(session);
}
/**
* Verifies that in case the session already includes the SAML storage and we store another element,
* it will be accessible until another messages gets retrieved.
*/
@Test
public void testInsert() {
Hashtable<String, SAMLObject> storage = new Hashtable<String, SAMLObject>();
Audience audienceMock = createNiceMock(Audience.class);
Assertion assertionMock = createNiceMock(Assertion.class);
storage.put("testKey", new SAMLObject<Audience>(audienceMock));
session = createMock(HttpSession.class);
expect(session.getId()).andReturn("session123").anyTimes();
expect(session.getAttribute(SPRING_SAML_STORAGE_KEY)).andReturn(storage);
session.setAttribute(eq(SPRING_SAML_STORAGE_KEY), anyObject());
expectLastCall().times(2);
replay(session);
cache = new HttpSessionStorage(session);
cache.storeMessage("testKey2", assertionMock);
assertEquals(2, cache.getAllMessages().size());
assertNotNull(cache.getAllMessages());
assertEquals(assertionMock, cache.retrieveMessage("testKey2"));
assertEquals(0, cache.getAllMessages().size());
assertNotNull(cache.getAllMessages());
assertNull(cache.retrieveMessage("testKey2"));
verify(session);
}
/**
* Verifies that in case the session already includes the SAML storage and we store another element with
* the same key as the one already existing, it will be overwritten.
*/
@Test
public void testOverwrite() {
Hashtable<String, SAMLObject> storage = new Hashtable<String, SAMLObject>();
Audience audienceMock = createNiceMock(Audience.class);
Assertion assertionMock = createNiceMock(Assertion.class);
storage.put("testKey", new SAMLObject<Audience>(audienceMock));
session = createMock(HttpSession.class);
expect(session.getId()).andReturn("session123").anyTimes();
expect(session.getAttribute(SPRING_SAML_STORAGE_KEY)).andReturn(storage);
session.setAttribute(eq(SPRING_SAML_STORAGE_KEY), anyObject());
expectLastCall().times(2);
replay(session);
cache = new HttpSessionStorage(session);
cache.storeMessage("testKey", assertionMock);
assertNotNull(cache.getAllMessages());
assertEquals(1, cache.getAllMessages().size());
assertEquals(assertionMock, cache.retrieveMessage("testKey"));
verify(session);
}
}