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.ipc; 019 020import static org.junit.jupiter.api.Assertions.assertEquals; 021 022import java.io.IOException; 023import java.util.Collections; 024import java.util.Map; 025import org.apache.hadoop.conf.Configuration; 026import org.apache.hadoop.hbase.HBaseConfiguration; 027import org.apache.hadoop.hbase.net.Address; 028import org.apache.hadoop.hbase.security.User; 029import org.apache.hadoop.hbase.security.provider.SaslClientAuthenticationProviders; 030import org.apache.hadoop.hbase.testclassification.SmallTests; 031import org.apache.hadoop.hbase.util.Bytes; 032import org.junit.jupiter.api.Tag; 033import org.junit.jupiter.api.Test; 034 035import org.apache.hbase.thirdparty.com.google.common.collect.ImmutableMap; 036 037import org.apache.hadoop.hbase.shaded.protobuf.generated.HBaseProtos; 038import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.MasterService; 039import org.apache.hadoop.hbase.shaded.protobuf.generated.RPCProtos; 040 041@Tag(SmallTests.TAG) 042public class TestRpcConnectionHeader { 043 private final ConnectionId connectionId; 044 045 public TestRpcConnectionHeader() throws IOException { 046 User user = User.createUserForTesting(HBaseConfiguration.create(), "test", new String[] {}); 047 String serviceName = MasterService.getDescriptor().getName(); 048 049 connectionId = new ConnectionId(user, serviceName, Address.fromParts("localhost", 12345)); 050 } 051 052 private class MyRpcConnection extends RpcConnection { 053 054 protected MyRpcConnection(Configuration conf, Map<String, byte[]> connectionAttributes) 055 throws IOException { 056 super(conf, null, connectionId, "cluster-id", false, 057 new SaslClientAuthenticationProviders(conf), null, null, null, null, connectionAttributes); 058 } 059 060 @Override 061 protected void callTimeout(Call call) { 062 } 063 064 @Override 065 public boolean isActive() { 066 return false; 067 } 068 069 @Override 070 public void shutdown() { 071 } 072 073 @Override 074 public void sendRequest(Call call, HBaseRpcController hrc) throws IOException { 075 } 076 077 @Override 078 public void cleanupConnection() { 079 } 080 } 081 082 @Test 083 public void testEmptyHeaders() throws IOException { 084 Configuration configuration = HBaseConfiguration.create(); 085 086 MyRpcConnection connection = new MyRpcConnection(configuration, Collections.emptyMap()); 087 RPCProtos.ConnectionHeader connectionHeader = connection.getConnectionHeader(); 088 089 assertEquals(0, connectionHeader.getAttributeCount()); 090 } 091 092 @Test 093 public void testConfigHeaders() throws IOException { 094 Configuration configuration = HBaseConfiguration.create(); 095 configuration.set("hbase.client.header.test", "true"); 096 097 MyRpcConnection connection = new MyRpcConnection(configuration, Collections.emptyMap()); 098 RPCProtos.ConnectionHeader connectionHeader = connection.getConnectionHeader(); 099 100 assertEquals(1, connectionHeader.getAttributeCount()); 101 102 HBaseProtos.NameBytesPair attribute = connectionHeader.getAttribute(0); 103 assertEquals("hbase.client.header.test", attribute.getName()); 104 assertEquals("true", attribute.getValue().toStringUtf8()); 105 } 106 107 @Test 108 public void testConfigHeadersNoOverride() throws IOException { 109 Configuration configuration = HBaseConfiguration.create(); 110 configuration.set("hbase.client.header.test", "true"); 111 configuration.set("hbase.client.header.test2", "true"); 112 113 Map<String, byte[]> attributes = 114 ImmutableMap.of("hbase.client.header.test", Bytes.toBytes("false")); 115 116 MyRpcConnection connection = new MyRpcConnection(configuration, attributes); 117 RPCProtos.ConnectionHeader connectionHeader = connection.getConnectionHeader(); 118 119 assertEquals(2, connectionHeader.getAttributeCount()); 120 121 HBaseProtos.NameBytesPair attribute0 = connectionHeader.getAttribute(0); 122 assertEquals("hbase.client.header.test", attribute0.getName()); 123 assertEquals("false", attribute0.getValue().toStringUtf8()); 124 125 HBaseProtos.NameBytesPair attribute1 = connectionHeader.getAttribute(1); 126 assertEquals("hbase.client.header.test2", attribute1.getName()); 127 assertEquals("true", attribute1.getValue().toStringUtf8()); 128 } 129}