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.hadoop.conf.Configuration;
25  import org.apache.hadoop.hbase.HBaseConfiguration;
26  import org.junit.Test;
27  
28  import java.security.PrivilegedAction;
29  import java.security.PrivilegedExceptionAction;
30  
31  public class TestUser {
32    @Test
33    public void testBasicAttributes() throws Exception {
34      Configuration conf = HBaseConfiguration.create();
35      User user = User.createUserForTesting(conf, "simple", new String[]{"foo"});
36      assertEquals("Username should match", "simple", user.getName());
37      assertEquals("Short username should match", "simple", user.getShortName());
38      // don't test shortening of kerberos names because regular Hadoop doesn't support them
39    }
40  
41    @Test
42    public void testRunAs() throws Exception {
43      Configuration conf = HBaseConfiguration.create();
44      final User user = User.createUserForTesting(conf, "testuser", new String[]{"foo"});
45      final PrivilegedAction<String> action = new PrivilegedAction<String>(){
46        public String run() {
47          User u = User.getCurrent();
48          return u.getName();
49        }
50      };
51  
52      String username = user.runAs(action);
53      assertEquals("Current user within runAs() should match",
54          "testuser", username);
55  
56      // ensure the next run is correctly set
57      User user2 = User.createUserForTesting(conf, "testuser2", new String[]{"foo"});
58      String username2 = user2.runAs(action);
59      assertEquals("Second username should match second user",
60          "testuser2", username2);
61  
62      // check the exception version
63      username = user.runAs(new PrivilegedExceptionAction<String>(){
64        public String run() throws Exception {
65          return User.getCurrent().getName();
66        }
67      });
68      assertEquals("User name in runAs() should match", "testuser", username);
69  
70      // verify that nested contexts work
71      user2.runAs(new PrivilegedAction(){
72        public Object run() {
73          String nestedName = user.runAs(action);
74          assertEquals("Nest name should match nested user", "testuser", nestedName);
75          assertEquals("Current name should match current user",
76              "testuser2", User.getCurrent().getName());
77          return null;
78        }
79      });
80    }
81  }