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 org.apache.hadoop.hbase.HBaseClassTestRule;
028import org.apache.hadoop.hbase.client.Connection;
029import org.apache.hadoop.hbase.testclassification.SmallTests;
030import org.junit.After;
031import org.junit.Before;
032import org.junit.ClassRule;
033import org.junit.Test;
034import org.junit.experimental.categories.Category;
035
036import org.apache.hbase.thirdparty.com.google.common.io.Closeables;
037
038import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil;
039
040@Category(SmallTests.class)
041public class TestClientTokenUtil {
042
043  @ClassRule
044  public static final HBaseClassTestRule CLASS_RULE =
045    HBaseClassTestRule.forClass(TestClientTokenUtil.class);
046
047  private URLClassLoader cl;
048
049  @Before
050  public void setUp() {
051    URL urlPU = ProtobufUtil.class.getProtectionDomain().getCodeSource().getLocation();
052    URL urlCTU = ClientTokenUtil.class.getProtectionDomain().getCodeSource().getLocation();
053    cl = new URLClassLoader(new URL[] { urlPU, urlCTU }, getClass().getClassLoader());
054  }
055
056  @After
057  public void tearDown() throws IOException {
058    Closeables.close(cl, true);
059  }
060
061  @Test
062  public void testObtainToken() throws Exception {
063    Throwable injected = new com.google.protobuf.ServiceException("injected");
064
065    Class<?> clientTokenUtil = cl.loadClass(ClientTokenUtil.class.getCanonicalName());
066    Field shouldInjectFault = clientTokenUtil.getDeclaredField("injectedException");
067    shouldInjectFault.setAccessible(true);
068    shouldInjectFault.set(null, injected);
069
070    try {
071      ClientTokenUtil.obtainToken((Connection) null);
072      fail("Should have injected exception.");
073    } catch (IOException e) {
074      Throwable t = e;
075      boolean serviceExceptionFound = false;
076      while ((t = t.getCause()) != null) {
077        if (t == injected) { // reference equality
078          serviceExceptionFound = true;
079          break;
080        }
081      }
082      if (!serviceExceptionFound) {
083        throw e; // wrong exception, fail the test
084      }
085    }
086
087    Boolean loaded = (Boolean) cl.loadClass(ProtobufUtil.class.getCanonicalName())
088      .getDeclaredMethod("isClassLoaderLoaded").invoke(null);
089    assertFalse("Should not have loaded DynamicClassLoader", loaded);
090  }
091}