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.jupiter.api.Assertions.assertFalse; 021import static org.junit.jupiter.api.Assertions.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.client.AsyncConnection; 030import org.apache.hadoop.hbase.client.Connection; 031import org.apache.hadoop.hbase.testclassification.SmallTests; 032import org.junit.jupiter.api.AfterEach; 033import org.junit.jupiter.api.BeforeEach; 034import org.junit.jupiter.api.Tag; 035import org.junit.jupiter.api.Test; 036 037import org.apache.hbase.thirdparty.com.google.common.io.Closeables; 038import org.apache.hbase.thirdparty.com.google.protobuf.ServiceException; 039 040import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil; 041 042@Tag(SmallTests.TAG) 043public class TestClientTokenUtil { 044 045 private URLClassLoader cl; 046 047 @BeforeEach 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 @AfterEach 055 public void tearDown() throws IOException { 056 Closeables.close(cl, true); 057 } 058 059 private void assertException(Throwable injected, Throwable t) { 060 while ((t = t.getCause()) != null) { 061 if (t == injected) { // reference equality 062 return; 063 } 064 } 065 fail("can not find injected exception " + injected + ", actual exception is " + t); 066 } 067 068 @Test 069 public void testObtainToken() throws Exception { 070 Exception injected = new Exception("injected"); 071 072 Class<?> clientTokenUtil = cl.loadClass(ClientTokenUtil.class.getCanonicalName()); 073 Field shouldInjectFault = clientTokenUtil.getDeclaredField("injectedException"); 074 shouldInjectFault.setAccessible(true); 075 shouldInjectFault.set(null, new ServiceException(injected)); 076 077 try { 078 ClientTokenUtil.obtainToken((Connection) null); 079 fail("Should have injected exception."); 080 } catch (IOException e) { 081 assertException(injected, e); 082 } 083 084 CompletableFuture<?> future = ClientTokenUtil.obtainToken((AsyncConnection) null); 085 try { 086 future.get(); 087 fail("Should have injected exception."); 088 } catch (ExecutionException e) { 089 assertException(injected, e); 090 } 091 Boolean loaded = (Boolean) cl.loadClass(ProtobufUtil.class.getCanonicalName()) 092 .getDeclaredMethod("isClassLoaderLoaded").invoke(null); 093 assertFalse(loaded, "Should not have loaded DynamicClassLoader"); 094 } 095}