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.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
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
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
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
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
89
90
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 }