001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase.http.lib; 019 020import static org.junit.Assert.*; 021import static org.mockito.ArgumentMatchers.any; 022 023import java.util.Map; 024import org.apache.hadoop.conf.Configuration; 025import org.apache.hadoop.hbase.HBaseClassTestRule; 026import org.apache.hadoop.hbase.http.FilterContainer; 027import org.apache.hadoop.hbase.http.HttpServer; 028import org.apache.hadoop.hbase.testclassification.MiscTests; 029import org.apache.hadoop.hbase.testclassification.SmallTests; 030import org.apache.hadoop.security.authentication.server.AuthenticationFilter; 031import org.junit.ClassRule; 032import org.junit.Test; 033import org.junit.experimental.categories.Category; 034import org.mockito.Mockito; 035import org.mockito.invocation.InvocationOnMock; 036import org.mockito.stubbing.Answer; 037 038@Category({ MiscTests.class, SmallTests.class }) 039public class TestAuthenticationFilterInitializer { 040 @ClassRule 041 public static final HBaseClassTestRule CLASS_RULE = 042 HBaseClassTestRule.forClass(TestAuthenticationFilterInitializer.class); 043 044 @Test 045 public void testConfiguration() throws Exception { 046 Configuration conf = new Configuration(); 047 conf.set("hadoop.http.authentication.foo", "bar"); 048 049 conf.set(HttpServer.BIND_ADDRESS, "barhost"); 050 051 FilterContainer container = Mockito.mock(FilterContainer.class); 052 Mockito.doAnswer(new Answer() { 053 @Override 054 public Object answer(InvocationOnMock invocationOnMock) throws Throwable { 055 Object[] args = invocationOnMock.getArguments(); 056 057 assertEquals("authentication", args[0]); 058 059 assertEquals(AuthenticationFilter.class.getName(), args[1]); 060 061 Map<String, String> conf = (Map<String, String>) args[2]; 062 assertEquals("/", conf.get("cookie.path")); 063 064 assertEquals("simple", conf.get("type")); 065 assertEquals("36000", conf.get("token.validity")); 066 assertNull(conf.get("cookie.domain")); 067 assertEquals("true", conf.get("simple.anonymous.allowed")); 068 assertEquals("HTTP/barhost@LOCALHOST", conf.get("kerberos.principal")); 069 assertEquals(System.getProperty("user.home") + "/hadoop.keytab", 070 conf.get("kerberos.keytab")); 071 assertEquals("bar", conf.get("foo")); 072 073 return null; 074 } 075 }).when(container).addFilter(any(), any(), any()); 076 077 new AuthenticationFilterInitializer().initFilter(container, conf); 078 } 079 080}