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.token;
019
020import static org.junit.Assert.assertFalse;
021import static org.junit.Assert.fail;
022
023import java.io.IOException;
024import java.lang.reflect.Field;
025import java.net.URL;
026import java.net.URLClassLoader;
027import java.util.concurrent.CompletableFuture;
028import java.util.concurrent.ExecutionException;
029import org.apache.hadoop.hbase.HBaseClassTestRule;
030import org.apache.hadoop.hbase.client.AsyncConnection;
031import org.apache.hadoop.hbase.client.Connection;
032import org.apache.hadoop.hbase.testclassification.SmallTests;
033import org.junit.After;
034import org.junit.Before;
035import org.junit.ClassRule;
036import org.junit.Test;
037import org.junit.experimental.categories.Category;
038
039import org.apache.hbase.thirdparty.com.google.common.io.Closeables;
040import org.apache.hbase.thirdparty.com.google.protobuf.ServiceException;
041
042import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil;
043
044@Category(SmallTests.class)
045public class TestClientTokenUtil {
046
047  @ClassRule
048  public static final HBaseClassTestRule CLASS_RULE =
049    HBaseClassTestRule.forClass(TestClientTokenUtil.class);
050
051  private URLClassLoader cl;
052
053  @Before
054  public void setUp() {
055    URL urlPU = ProtobufUtil.class.getProtectionDomain().getCodeSource().getLocation();
056    URL urlCTU = ClientTokenUtil.class.getProtectionDomain().getCodeSource().getLocation();
057    cl = new URLClassLoader(new URL[] { urlPU, urlCTU }, getClass().getClassLoader());
058  }
059
060  @After
061  public void tearDown() throws IOException {
062    Closeables.close(cl, true);
063  }
064
065  private void assertException(Throwable injected, Throwable t) {
066    while ((t = t.getCause()) != null) {
067      if (t == injected) { // reference equality
068        return;
069      }
070    }
071    fail("can not find injected exception " + injected + ", actual exception is " + t);
072  }
073
074  @Test
075  public void testObtainToken() throws Exception {
076    Exception injected = new Exception("injected");
077
078    Class<?> clientTokenUtil = cl.loadClass(ClientTokenUtil.class.getCanonicalName());
079    Field shouldInjectFault = clientTokenUtil.getDeclaredField("injectedException");
080    shouldInjectFault.setAccessible(true);
081    shouldInjectFault.set(null, new ServiceException(injected));
082
083    try {
084      ClientTokenUtil.obtainToken((Connection) null);
085      fail("Should have injected exception.");
086    } catch (IOException e) {
087      assertException(injected, e);
088    }
089
090    CompletableFuture<?> future = ClientTokenUtil.obtainToken((AsyncConnection) null);
091    try {
092      future.get();
093      fail("Should have injected exception.");
094    } catch (ExecutionException e) {
095      assertException(injected, e);
096    }
097    Boolean loaded = (Boolean) cl.loadClass(ProtobufUtil.class.getCanonicalName())
098      .getDeclaredMethod("isClassLoaderLoaded").invoke(null);
099    assertFalse("Should not have loaded DynamicClassLoader", loaded);
100  }
101}