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.lang.reflect.Field;
024import java.lang.reflect.InvocationTargetException;
025import java.net.URL;
026import java.net.URLClassLoader;
027
028import org.apache.hadoop.hbase.HBaseClassTestRule;
029import org.apache.hadoop.hbase.client.Connection;
030import org.apache.hadoop.hbase.testclassification.SmallTests;
031import org.junit.ClassRule;
032import org.junit.Test;
033import org.junit.experimental.categories.Category;
034
035import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil;
036
037@Category(SmallTests.class)
038public class TestTokenUtil {
039  @ClassRule
040  public static final HBaseClassTestRule CLASS_RULE =
041      HBaseClassTestRule.forClass(TestTokenUtil.class);
042
043  @Test
044  public void testObtainToken() throws Exception {
045    URL urlPU = ProtobufUtil.class.getProtectionDomain().getCodeSource().getLocation();
046    URL urlTU = TokenUtil.class.getProtectionDomain().getCodeSource().getLocation();
047
048    ClassLoader cl = new URLClassLoader(new URL[] { urlPU, urlTU }, getClass().getClassLoader());
049
050    Throwable injected = new com.google.protobuf.ServiceException("injected");
051
052    Class<?> tokenUtil = cl.loadClass(TokenUtil.class.getCanonicalName());
053    Field shouldInjectFault = tokenUtil.getDeclaredField("injectedException");
054    shouldInjectFault.setAccessible(true);
055    shouldInjectFault.set(null, injected);
056
057    try {
058      tokenUtil.getMethod("obtainToken", Connection.class)
059          .invoke(null, new Object[] { null });
060      fail("Should have injected exception.");
061    } catch (InvocationTargetException e) {
062      Throwable t = e;
063      boolean serviceExceptionFound = false;
064      while ((t = t.getCause()) != null) {
065        if (t == injected) { // reference equality
066          serviceExceptionFound = true;
067          break;
068        }
069      }
070      if (!serviceExceptionFound) {
071        throw e; // wrong exception, fail the test
072      }
073    }
074
075    Boolean loaded = (Boolean) cl.loadClass(ProtobufUtil.class.getCanonicalName())
076        .getDeclaredMethod("isClassLoaderLoaded")
077        .invoke(null);
078    assertFalse("Should not have loaded DynamicClassLoader", loaded);
079  }
080}