1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.security;
20
21 import static org.junit.Assert.*;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.apache.hadoop.conf.Configuration;
26 import org.apache.hadoop.hbase.HBaseConfiguration;
27 import org.apache.hadoop.hbase.SmallTests;
28 import org.junit.Test;
29 import org.junit.experimental.categories.Category;
30
31 import java.io.IOException;
32 import java.security.PrivilegedAction;
33 import java.security.PrivilegedExceptionAction;
34
35 @Category(SmallTests.class)
36 public class TestUser {
37 private static Log LOG = LogFactory.getLog(TestUser.class);
38
39 @Test
40 public void testBasicAttributes() throws Exception {
41 Configuration conf = HBaseConfiguration.create();
42 User user = User.createUserForTesting(conf, "simple", new String[]{"foo"});
43 assertEquals("Username should match", "simple", user.getName());
44 assertEquals("Short username should match", "simple", user.getShortName());
45
46 }
47
48 @Test
49 public void testRunAs() throws Exception {
50 Configuration conf = HBaseConfiguration.create();
51 final User user = User.createUserForTesting(conf, "testuser", new String[]{"foo"});
52 final PrivilegedExceptionAction<String> action = new PrivilegedExceptionAction<String>(){
53 public String run() throws IOException {
54 User u = User.getCurrent();
55 return u.getName();
56 }
57 };
58
59 String username = user.runAs(action);
60 assertEquals("Current user within runAs() should match",
61 "testuser", username);
62
63
64 User user2 = User.createUserForTesting(conf, "testuser2", new String[]{"foo"});
65 String username2 = user2.runAs(action);
66 assertEquals("Second username should match second user",
67 "testuser2", username2);
68
69
70 username = user.runAs(new PrivilegedExceptionAction<String>(){
71 public String run() throws Exception {
72 return User.getCurrent().getName();
73 }
74 });
75 assertEquals("User name in runAs() should match", "testuser", username);
76
77
78 user2.runAs(new PrivilegedExceptionAction(){
79 public Object run() throws IOException, InterruptedException{
80 String nestedName = user.runAs(action);
81 assertEquals("Nest name should match nested user", "testuser", nestedName);
82 assertEquals("Current name should match current user",
83 "testuser2", User.getCurrent().getName());
84 return null;
85 }
86 });
87 }
88
89
90
91
92
93
94 @Test
95 public void testGetCurrent() throws Exception {
96 User user1 = User.getCurrent();
97 assertNotNull(user1.ugi);
98 LOG.debug("User1 is "+user1.getName());
99
100 for (int i =0 ; i< 100; i++) {
101 User u = User.getCurrent();
102 assertNotNull(u);
103 assertEquals(user1.getName(), u.getName());
104 assertEquals(user1, u);
105 assertEquals(user1.hashCode(), u.hashCode());
106 }
107 }
108
109 }
110