/
FormMechTestBase.java
199 lines (180 loc) · 10.4 KB
/
FormMechTestBase.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
/*
* JBoss, Home of Professional Open Source.
* Copyright 2018, Red Hat, Inc., and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.wildfly.test.integration.elytron.http;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.Header;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.junit.Test;
import static org.apache.http.HttpStatus.SC_FORBIDDEN;
import static org.apache.http.HttpStatus.SC_MOVED_TEMPORARILY;
import static org.apache.http.HttpStatus.SC_OK;
import static org.jboss.as.test.integration.security.common.servlets.SimpleServlet.RESPONSE_BODY;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
/**
* Test of FORM HTTP mechanism.
*
* @author Jan Kalina
*/
abstract class FormMechTestBase extends AbstractMechTestBase {
protected static final String NAME = FormMechTestCase.class.getSimpleName();
protected static final String LOGIN_PAGE_CONTENT = "LOGINPAGE";
protected static final String ERROR_PAGE_CONTENT = "ERRORPAGE";
@Test
@Override
public void testUnauthorized() throws Exception {
HttpGet request = new HttpGet(new URI(url.toExternalForm() + "role1"));
HttpClientContext context = HttpClientContext.create();
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
try (CloseableHttpResponse response = httpClient.execute(request, context)) {
int statusCode = response.getStatusLine().getStatusCode();
assertEquals("Unexpected status code in HTTP response.", SC_OK, statusCode);
assertEquals("Unexpected content of HTTP response.", LOGIN_PAGE_CONTENT, EntityUtils.toString(response.getEntity()));
}
}
}
@Test
public void testLoginPage() throws Exception {
HttpGet request = new HttpGet(new URI(url.toExternalForm() + "login.html"));
HttpClientContext context = HttpClientContext.create();
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
try (CloseableHttpResponse response = httpClient.execute(request, context)) {
int statusCode = response.getStatusLine().getStatusCode();
assertEquals("Unexpected status code in HTTP response.", SC_OK, statusCode);
assertEquals("Unexpected content of HTTP response.", LOGIN_PAGE_CONTENT, EntityUtils.toString(response.getEntity()));
}
}
}
@Test
public void testCorrectWorkflow() throws Exception {
try (CloseableHttpClient httpClient = HttpClientBuilder.create().disableRedirectHandling().build()) {
// unauthorized - login form should be shown
HttpGet request1 = new HttpGet(new URI(url.toExternalForm() + "role1"));
try (CloseableHttpResponse response = httpClient.execute(request1)) {
int statusCode = response.getStatusLine().getStatusCode();
assertEquals("Unexpected status code in HTTP response.", SC_OK, statusCode);
assertEquals("Unexpected content of HTTP response.", LOGIN_PAGE_CONTENT, EntityUtils.toString(response.getEntity()));
}
// logging-in
HttpPost request2 = createLoginRequest( "user1", "password1");
try (CloseableHttpResponse response = httpClient.execute(request2)) {
int statusCode = response.getStatusLine().getStatusCode();
Header[] locations = response.getHeaders("Location");
assertEquals("Unexpected status code in HTTP response.", SC_MOVED_TEMPORARILY, statusCode);
assertEquals("Missing redirect in HTTP response.", 1, locations.length);
assertEquals("Unexpected redirect in HTTP response.", url.toExternalForm() + "role1", locations[0].getValue());
}
// should be logged now
HttpGet request3 = new HttpGet(new URI(url.toExternalForm() + "role1"));
try (CloseableHttpResponse response = httpClient.execute(request3)) {
int statusCode = response.getStatusLine().getStatusCode();
assertEquals("Unexpected status code in HTTP response.", SC_OK, statusCode);
assertEquals("Unexpected content of HTTP response.", RESPONSE_BODY, EntityUtils.toString(response.getEntity()));
}
// but no role2
HttpGet request4 = new HttpGet(new URI(url.toExternalForm() + "role2"));
try (CloseableHttpResponse response = httpClient.execute(request4)) {
int statusCode = response.getStatusLine().getStatusCode();
assertEquals("Unexpected status code in HTTP response.", SC_FORBIDDEN, statusCode);
assertNotEquals("Unexpected content of HTTP response.", RESPONSE_BODY, EntityUtils.toString(response.getEntity()));
}
// try to log-out
HttpGet request5 = new HttpGet(new URI(url.toExternalForm() + "logout"));
try (CloseableHttpResponse response = httpClient.execute(request5)) {
int statusCode = response.getStatusLine().getStatusCode();
Header[] locations = response.getHeaders("Location");
assertEquals("Unexpected status code in HTTP response.", SC_MOVED_TEMPORARILY, statusCode);
assertEquals("Missing redirect in HTTP response.", 1, locations.length);
assertEquals("Unexpected redirect in HTTP response.", url.toExternalForm() + "index.html", locations[0].getValue());
}
// should be logged-out again
HttpGet request6 = new HttpGet(new URI(url.toExternalForm() + "role1"));
try (CloseableHttpResponse response = httpClient.execute(request6)) {
int statusCode = response.getStatusLine().getStatusCode();
assertEquals("Unexpected status code in HTTP response.", SC_OK, statusCode);
assertEquals("Unexpected content of HTTP response.", LOGIN_PAGE_CONTENT, EntityUtils.toString(response.getEntity()));
}
}
}
@Test
public void testInvalidPrincipal() throws Exception {
try (CloseableHttpClient httpClient = HttpClientBuilder.create().disableRedirectHandling().build()) {
HttpPost request = createLoginRequest("user1wrong", "password1");
try (CloseableHttpResponse response = httpClient.execute(request)) {
int statusCode = response.getStatusLine().getStatusCode();
assertEquals("Unexpected status code in HTTP response.", SC_OK, statusCode);
assertEquals("Unexpected content of HTTP response.", ERROR_PAGE_CONTENT, EntityUtils.toString(response.getEntity()));
}
}
}
@Test
public void testInvalidCredential() throws Exception {
try (CloseableHttpClient httpClient = HttpClientBuilder.create().disableRedirectHandling().build()) {
HttpPost request = createLoginRequest("user1", "password1wrong");
try (CloseableHttpResponse response = httpClient.execute(request)) {
int statusCode = response.getStatusLine().getStatusCode();
assertEquals("Unexpected status code in HTTP response.", SC_OK, statusCode);
assertEquals("Unexpected content of HTTP response.", ERROR_PAGE_CONTENT, EntityUtils.toString(response.getEntity()));
}
}
}
@Test
public void testEmptyUsername() throws Exception {
try (CloseableHttpClient httpClient = HttpClientBuilder.create().disableRedirectHandling().build()) {
HttpPost emptyUsernameRequest = createLoginRequest("", "non-empty-password");
try (CloseableHttpResponse response = httpClient.execute(emptyUsernameRequest)) {
int statusCode = response.getStatusLine().getStatusCode();
assertEquals("Unexpected status code in HTTP response.", SC_OK, statusCode);
assertEquals("Unexpected content of HTTP response.", ERROR_PAGE_CONTENT,
EntityUtils.toString(response.getEntity()));
}
HttpPost emptyUsernameAndPasswordLoginRequest = createLoginRequest("", "");
try (CloseableHttpResponse response = httpClient.execute(emptyUsernameAndPasswordLoginRequest)) {
int statusCode = response.getStatusLine().getStatusCode();
assertEquals("Unexpected status code in HTTP response.", SC_OK, statusCode);
assertEquals("Unexpected content of HTTP response.", ERROR_PAGE_CONTENT,
EntityUtils.toString(response.getEntity()));
}
}
}
protected HttpPost createLoginRequest(String username, String password)
throws Exception {
HttpPost request = new HttpPost(new URI(url.toExternalForm() + "j_security_check"));
List<NameValuePair> params = new ArrayList<>();
params.add(new BasicNameValuePair("j_username", username));
params.add(new BasicNameValuePair("j_password", password));
request.setEntity(new UrlEncodedFormEntity(params));
return request;
}
}