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