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