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