ソースを参照

HDFS-1039. Adding test for JspHelper.getUGI

git-svn-id: https://svn.apache.org/repos/asf/hadoop/hdfs/trunk@950726 13f79535-47bb-0310-9956-ffa450edef68
Boris Shkolnik 15 年 前
コミット
d1228de12f

+ 2 - 0
CHANGES.txt

@@ -29,6 +29,8 @@ Trunk (unreleased changes)
 
   BUG FIXES
 
+    HDFS-1039. Adding test for  JspHelper.getUGI(jnp via boryas)
+
     HDFS-1019. Incorrect default values for delegation tokens in 
     hdfs-default.xml (jnp via boryas)
 

+ 94 - 0
src/test/hdfs/org/apache/hadoop/hdfs/server/common/TestJspHelper.java

@@ -0,0 +1,94 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hadoop.hdfs.server.common;
+
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.io.IOException;
+import java.net.InetSocketAddress;
+
+import javax.servlet.http.HttpServletRequest;
+
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.hdfs.DFSConfigKeys;
+import org.apache.hadoop.hdfs.HdfsConfiguration;
+import org.apache.hadoop.hdfs.security.token.delegation.DelegationTokenIdentifier;
+import org.apache.hadoop.hdfs.server.namenode.NameNode;
+import org.apache.hadoop.io.Text;
+import org.apache.hadoop.security.UserGroupInformation;
+import org.apache.hadoop.security.token.Token;
+import org.apache.hadoop.security.token.TokenIdentifier;
+import org.apache.hadoop.security.token.delegation.AbstractDelegationTokenSecretManager;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class TestJspHelper {
+
+  private Configuration conf = new HdfsConfiguration();
+
+  public static class DummySecretManager extends
+      AbstractDelegationTokenSecretManager<DelegationTokenIdentifier> {
+
+    public DummySecretManager(long delegationKeyUpdateInterval,
+        long delegationTokenMaxLifetime, long delegationTokenRenewInterval,
+        long delegationTokenRemoverScanInterval) {
+      super(delegationKeyUpdateInterval, delegationTokenMaxLifetime,
+          delegationTokenRenewInterval, delegationTokenRemoverScanInterval);
+    }
+
+    @Override
+    public DelegationTokenIdentifier createIdentifier() {
+      return null;
+    }
+
+    @Override
+    public byte[] createPassword(DelegationTokenIdentifier dtId) {
+      return new byte[1];
+    }
+  }
+
+  @Test
+  public void testGetUgi() throws IOException {
+    conf.set(DFSConfigKeys.FS_DEFAULT_NAME_KEY, "hdfs://localhost:4321/");
+    HttpServletRequest request = mock(HttpServletRequest.class);
+    String user = "TheDoctor";
+    Text userText = new Text(user);
+    DelegationTokenIdentifier dtId = new DelegationTokenIdentifier(userText,
+        userText, null);
+    Token<DelegationTokenIdentifier> token = new Token<DelegationTokenIdentifier>(
+        dtId, new DummySecretManager(0, 0, 0, 0));
+    String tokenString = token.encodeToUrlString();
+    when(request.getParameter(JspHelper.DELEGATION_PARAMETER_NAME)).thenReturn(
+        tokenString);
+    when(request.getRemoteUser()).thenReturn(user);
+
+    conf.set(DFSConfigKeys.HADOOP_SECURITY_AUTHENTICATION, "kerberos");
+    UserGroupInformation.setConfiguration(conf);
+
+    InetSocketAddress serviceAddr = NameNode.getAddress(conf);
+    Text tokenService = new Text(serviceAddr.getAddress().getHostAddress()
+        + ":" + serviceAddr.getPort());
+
+    UserGroupInformation ugi = JspHelper.getUGI(request, conf);
+    Token<? extends TokenIdentifier> tokenInUgi = ugi.getTokens().iterator()
+        .next();
+    Assert.assertEquals(tokenInUgi.getService(), tokenService);
+  }
+
+}