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.io.IOException;
29 import java.security.PrivilegedAction;
30 import java.security.PrivilegedExceptionAction;
31
32 public class TestUser {
33 @Test
34 public void testBasicAttributes() throws Exception {
35 Configuration conf = HBaseConfiguration.create();
36 User user = User.createUserForTesting(conf, "simple", new String[]{"foo"});
37 assertEquals("Username should match", "simple", user.getName());
38 assertEquals("Short username should match", "simple", user.getShortName());
39
40 }
41
42 @Test
43 public void testRunAs() throws Exception {
44 Configuration conf = HBaseConfiguration.create();
45 final User user = User.createUserForTesting(conf, "testuser", new String[]{"foo"});
46 final PrivilegedExceptionAction<String> action = new PrivilegedExceptionAction<String>(){
47 public String run() throws IOException {
48 User u = User.getCurrent();
49 return u.getName();
50 }
51 };
52
53 String username = user.runAs(action);
54 assertEquals("Current user within runAs() should match",
55 "testuser", username);
56
57
58 User user2 = User.createUserForTesting(conf, "testuser2", new String[]{"foo"});
59 String username2 = user2.runAs(action);
60 assertEquals("Second username should match second user",
61 "testuser2", username2);
62
63
64 username = user.runAs(new PrivilegedExceptionAction<String>(){
65 public String run() throws Exception {
66 return User.getCurrent().getName();
67 }
68 });
69 assertEquals("User name in runAs() should match", "testuser", username);
70
71
72 user2.runAs(new PrivilegedExceptionAction(){
73 public Object run() throws IOException, InterruptedException{
74 String nestedName = user.runAs(action);
75 assertEquals("Nest name should match nested user", "testuser", nestedName);
76 assertEquals("Current name should match current user",
77 "testuser2", User.getCurrent().getName());
78 return null;
79 }
80 });
81 }
82 }