HBASE-26921 Rewrite the counting cells part in TestMultiVersions (#4316)
[hbase.git] / hbase-http / src / test / java / org / apache / hadoop / hbase / http / TestHttpCookieFlag.java
blobd373d60f7467bb7db94cbf4a2a695138272cbe3a
1 /**
2 * Licensed under the Apache License, Version 2.0 (the "License");
3 * you may not use this file except in compliance with the License.
4 * You may obtain a copy of the License at
6 * http://www.apache.org/licenses/LICENSE-2.0
8 * Unless required by applicable law or agreed to in writing, software
9 * distributed under the License is distributed on an "AS IS" BASIS,
10 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 * See the License for the specific language governing permissions and
12 * limitations under the License. See accompanying LICENSE file.
14 package org.apache.hadoop.hbase.http;
16 import java.util.List;
17 import java.io.File;
18 import java.io.IOException;
19 import java.net.HttpURLConnection;
20 import java.net.HttpCookie;
21 import java.net.URI;
22 import java.net.URL;
23 import javax.net.ssl.HttpsURLConnection;
24 import javax.servlet.Filter;
25 import javax.servlet.FilterConfig;
26 import javax.servlet.FilterChain;
27 import javax.servlet.ServletRequest;
28 import javax.servlet.ServletResponse;
29 import javax.servlet.ServletException;
30 import javax.servlet.http.HttpServletResponse;
31 import java.security.GeneralSecurityException;
32 import org.apache.hadoop.hbase.HBaseClassTestRule;
33 import org.apache.hadoop.hbase.testclassification.MiscTests;
34 import org.apache.hadoop.hbase.testclassification.SmallTests;
35 import org.apache.hadoop.conf.Configuration;
36 import org.apache.hadoop.fs.FileUtil;
37 import org.apache.hadoop.net.NetUtils;
38 import org.apache.hadoop.security.authentication.server.AuthenticationFilter;
39 import org.apache.hadoop.security.ssl.KeyStoreTestUtil;
40 import org.apache.hadoop.security.ssl.SSLFactory;
42 import org.junit.Assert;
43 import org.junit.AfterClass;
44 import org.junit.BeforeClass;
45 import org.junit.ClassRule;
46 import org.junit.Test;
47 import org.junit.experimental.categories.Category;
49 @Category({ MiscTests.class, SmallTests.class})
50 public class TestHttpCookieFlag {
51 @ClassRule
52 public static final HBaseClassTestRule CLASS_RULE =
53 HBaseClassTestRule.forClass(TestHttpCookieFlag.class);
55 private static final String BASEDIR = System.getProperty("test.build.dir",
56 "target/test-dir") + "/" +
57 org.apache.hadoop.hbase.http.TestHttpCookieFlag.class.getSimpleName();
58 private static String keystoresDir;
59 private static String sslConfDir;
60 private static SSLFactory clientSslFactory;
61 private static HttpServer server;
63 public static class DummyAuthenticationFilter implements Filter {
65 @Override
66 public void init(FilterConfig filterConfig) throws ServletException {
69 @Override
70 public void doFilter(ServletRequest request, ServletResponse response,
71 FilterChain chain) throws IOException,
72 ServletException {
73 HttpServletResponse resp = (HttpServletResponse) response;
74 boolean isHttps = "https".equals(request.getScheme());
75 AuthenticationFilter.createAuthCookie(resp, "token", null, null, -1,
76 true, isHttps);
77 chain.doFilter(request, resp);
80 @Override
81 public void destroy() {
84 public static class DummyFilterInitializer extends FilterInitializer {
85 @Override
86 public void initFilter(FilterContainer container, Configuration conf) {
87 container.addFilter("DummyAuth", DummyAuthenticationFilter.class
88 .getName(), null);
92 @BeforeClass
93 public static void setUp() throws Exception {
94 Configuration conf = new Configuration();
95 conf.set(HttpServer.FILTER_INITIALIZERS_PROPERTY,
96 DummyFilterInitializer.class.getName());
97 conf.setInt("hbase.http.max.threads", 19); /* acceptors=2 + selectors=16 + request=1 */
98 System.setProperty("hadoop.log.dir", BASEDIR); /* needed for /logs */
100 File base = new File(BASEDIR);
101 FileUtil.fullyDelete(base);
102 base.mkdirs();
103 keystoresDir = new File(BASEDIR).getAbsolutePath();
104 sslConfDir = KeyStoreTestUtil.getClasspathDir(TestSSLHttpServer.class);
106 KeyStoreTestUtil.setupSSLConfig(keystoresDir, sslConfDir, conf, false);
107 Configuration sslConf = KeyStoreTestUtil.getSslConfig();
109 clientSslFactory = new SSLFactory(SSLFactory.Mode.CLIENT, sslConf);
110 clientSslFactory.init();
112 server = new HttpServer.Builder()
113 .setName("test")
114 .addEndpoint(new URI("http://localhost"))
115 .addEndpoint(new URI("https://localhost"))
116 .setConf(conf)
117 .keyPassword(sslConf.get("ssl.server.keystore.keypassword"))
118 .keyStore(sslConf.get("ssl.server.keystore.location"),
119 sslConf.get("ssl.server.keystore.password"),
120 sslConf.get("ssl.server.keystore.type", "jks"))
121 .trustStore(sslConf.get("ssl.server.truststore.location"),
122 sslConf.get("ssl.server.truststore.password"),
123 sslConf.get("ssl.server.truststore.type", "jks"))
124 .build();
125 server.addPrivilegedServlet("echo", "/echo", TestHttpServer.EchoServlet.class);
126 server.start();
129 @Test
130 public void testHttpCookie() throws IOException {
131 URL base = new URL("http://" + NetUtils.getHostPortString(server
132 .getConnectorAddress(0)));
133 HttpURLConnection conn = (HttpURLConnection) new URL(base,
134 "/echo").openConnection();
136 String header = conn.getHeaderField("Set-Cookie");
137 Assert.assertTrue(header != null);
138 List<HttpCookie> cookies = HttpCookie.parse(header);
139 Assert.assertTrue(!cookies.isEmpty());
140 Assert.assertTrue(header.contains("; HttpOnly"));
141 Assert.assertTrue("token".equals(cookies.get(0).getValue()));
144 @Test
145 public void testHttpsCookie() throws IOException, GeneralSecurityException {
146 URL base = new URL("https://" + NetUtils.getHostPortString(server
147 .getConnectorAddress(1)));
148 HttpsURLConnection conn = (HttpsURLConnection) new URL(base,
149 "/echo").openConnection();
150 conn.setSSLSocketFactory(clientSslFactory.createSSLSocketFactory());
152 String header = conn.getHeaderField("Set-Cookie");
153 Assert.assertTrue(header != null);
155 List<HttpCookie> cookies = HttpCookie.parse(header);
156 Assert.assertTrue(!cookies.isEmpty());
157 Assert.assertTrue(header.contains("; HttpOnly"));
158 Assert.assertTrue(cookies.get(0).getSecure());
159 Assert.assertTrue("token".equals(cookies.get(0).getValue()));
162 @Test
163 public void testHttpsCookieDefaultServlets() throws Exception {
164 HttpsURLConnection conn = null;
166 URL base = new URL("https://" + NetUtils.getHostPortString(server
167 .getConnectorAddress(1)) + "/");
169 for (String servlet : new String[] { "static", "stacks", "logLevel", "jmx", "logs" }) {
170 conn = (HttpsURLConnection) new URL(base,
171 "/" + servlet).openConnection();
172 conn.setSSLSocketFactory(clientSslFactory.createSSLSocketFactory());
174 String header = conn.getHeaderField("Set-Cookie");
175 Assert.assertTrue(header != null);
176 List<HttpCookie> cookies = HttpCookie.parse(header);
177 Assert.assertTrue(!cookies.isEmpty());
178 Assert.assertTrue(header.contains("; HttpOnly"));
179 Assert.assertTrue(cookies.get(0).getSecure());
180 Assert.assertTrue("token".equals(cookies.get(0).getValue()));
184 @AfterClass
185 public static void cleanup() throws Exception {
186 server.stop();
187 FileUtil.fullyDelete(new File(BASEDIR));
188 KeyStoreTestUtil.cleanupSSLConfig(keystoresDir, sslConfDir);
189 clientSslFactory.destroy();