1   /*
2    * Copyright 2010 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  package org.apache.hadoop.hbase.security;
21  
22  import static org.junit.Assert.*;
23  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  import org.apache.hadoop.conf.Configuration;
27  import org.apache.hadoop.hbase.HBaseConfiguration;
28  import org.junit.Test;
29  
30  import java.io.IOException;
31  import java.security.PrivilegedAction;
32  import java.security.PrivilegedExceptionAction;
33  
34  public class TestUser {
35    private static Log LOG = LogFactory.getLog(TestUser.class);
36  
37    @Test
38    public void testBasicAttributes() throws Exception {
39      Configuration conf = HBaseConfiguration.create();
40      User user = User.createUserForTesting(conf, "simple", new String[]{"foo"});
41      assertEquals("Username should match", "simple", user.getName());
42      assertEquals("Short username should match", "simple", user.getShortName());
43      // don't test shortening of kerberos names because regular Hadoop doesn't support them
44    }
45  
46    @Test
47    public void testRunAs() throws Exception {
48      Configuration conf = HBaseConfiguration.create();
49      final User user = User.createUserForTesting(conf, "testuser", new String[]{"foo"});
50      final PrivilegedExceptionAction<String> action = new PrivilegedExceptionAction<String>(){
51        public String run() throws IOException {
52            User u = User.getCurrent();
53            return u.getName();
54        }
55      };
56  
57      String username = user.runAs(action);
58      assertEquals("Current user within runAs() should match",
59          "testuser", username);
60  
61      // ensure the next run is correctly set
62      User user2 = User.createUserForTesting(conf, "testuser2", new String[]{"foo"});
63      String username2 = user2.runAs(action);
64      assertEquals("Second username should match second user",
65          "testuser2", username2);
66  
67      // check the exception version
68      username = user.runAs(new PrivilegedExceptionAction<String>(){
69        public String run() throws Exception {
70          return User.getCurrent().getName();
71        }
72      });
73      assertEquals("User name in runAs() should match", "testuser", username);
74  
75      // verify that nested contexts work
76      user2.runAs(new PrivilegedExceptionAction(){
77        public Object run() throws IOException, InterruptedException{
78          String nestedName = user.runAs(action);
79          assertEquals("Nest name should match nested user", "testuser", nestedName);
80          assertEquals("Current name should match current user",
81              "testuser2", User.getCurrent().getName());
82          return null;
83        }
84      });
85    }
86  
87    /**
88     * Make sure that we're returning a result for the current user.
89     * Previously getCurrent() was returning null if not initialized on
90     * non-secure Hadoop variants.
91     */
92    @Test
93    public void testGetCurrent() throws Exception {
94      User user1 = User.getCurrent();
95      assertNotNull(user1.ugi);
96      LOG.debug("User1 is "+user1.getName());
97  
98      for (int i =0 ; i< 100; i++) {
99        User u = User.getCurrent();
100       assertNotNull(u);
101       assertEquals(user1.getName(), u.getName());
102     }
103   }
104 }