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