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.security.provider;
019
020import static org.junit.Assert.assertNotNull;
021
022import java.util.Arrays;
023import java.util.Collections;
024import java.util.HashSet;
025import java.util.Set;
026
027import org.apache.hadoop.conf.Configuration;
028import org.apache.hadoop.hbase.HBaseClassTestRule;
029import org.apache.hadoop.hbase.testclassification.SmallTests;
030import org.junit.Before;
031import org.junit.ClassRule;
032import org.junit.Test;
033import org.junit.experimental.categories.Category;
034
035@Category({SmallTests.class})
036public class TestDefaultProviderSelector {
037
038  @ClassRule
039  public static final HBaseClassTestRule CLASS_RULE =
040      HBaseClassTestRule.forClass(TestDefaultProviderSelector.class);
041
042  BuiltInProviderSelector selector;
043  @Before
044  public void setup() {
045    selector = new BuiltInProviderSelector();
046  }
047
048  @Test(expected = IllegalStateException.class)
049  public void testExceptionOnMissingProviders() {
050    selector.configure(new Configuration(false), Collections.emptySet());
051  }
052
053  @Test(expected = NullPointerException.class)
054  public void testNullConfiguration() {
055    selector.configure(null, Collections.emptySet());
056  }
057
058  @Test(expected = NullPointerException.class)
059  public void testNullProviderMap() {
060    selector.configure(new Configuration(false), null);
061  }
062
063  @Test(expected = IllegalStateException.class)
064  public void testDuplicateProviders() {
065    Set<SaslClientAuthenticationProvider> providers = new HashSet<>();
066    providers.add(new SimpleSaslClientAuthenticationProvider());
067    providers.add(new SimpleSaslClientAuthenticationProvider());
068    selector.configure(new Configuration(false), providers);
069  }
070
071  @Test
072  public void testExpectedProviders() {
073    HashSet<SaslClientAuthenticationProvider> providers = new HashSet<>(Arrays.asList(
074        new SimpleSaslClientAuthenticationProvider(), new GssSaslClientAuthenticationProvider(),
075        new DigestSaslClientAuthenticationProvider()));
076
077    selector.configure(new Configuration(false), providers);
078
079    assertNotNull("Simple provider was null", selector.simpleAuth);
080    assertNotNull("Kerberos provider was null", selector.krbAuth);
081    assertNotNull("Digest provider was null", selector.digestAuth);
082  }
083}