|
@@ -19,8 +19,7 @@ package org.apache.hadoop.security;
|
|
|
import static org.junit.Assert.*;
|
|
|
import org.junit.*;
|
|
|
|
|
|
-import org.mockito.Mockito;
|
|
|
-import static org.mockito.Mockito.mock;
|
|
|
+import static org.mockito.Mockito.*;
|
|
|
|
|
|
import java.io.BufferedReader;
|
|
|
import java.io.IOException;
|
|
@@ -35,6 +34,7 @@ import javax.security.auth.login.AppConfigurationEntry;
|
|
|
import javax.security.auth.login.LoginContext;
|
|
|
|
|
|
import org.apache.hadoop.conf.Configuration;
|
|
|
+import org.apache.hadoop.io.Text;
|
|
|
import org.apache.hadoop.metrics2.MetricsRecordBuilder;
|
|
|
import org.apache.hadoop.security.UserGroupInformation.AuthenticationMethod;
|
|
|
import org.apache.hadoop.security.token.Token;
|
|
@@ -194,8 +194,6 @@ public class TestUserGroupInformation {
|
|
|
public void testEqualsWithRealUser() throws Exception {
|
|
|
UserGroupInformation realUgi1 = UserGroupInformation.createUserForTesting(
|
|
|
"RealUser", GROUP_NAMES);
|
|
|
- UserGroupInformation realUgi2 = UserGroupInformation.createUserForTesting(
|
|
|
- "RealUser", GROUP_NAMES);
|
|
|
UserGroupInformation proxyUgi1 = UserGroupInformation.createProxyUser(
|
|
|
USER_NAME, realUgi1);
|
|
|
UserGroupInformation proxyUgi2 =
|
|
@@ -213,7 +211,82 @@ public class TestUserGroupInformation {
|
|
|
assertArrayEquals(new String[]{GROUP1_NAME, GROUP2_NAME, GROUP3_NAME},
|
|
|
uugi.getGroupNames());
|
|
|
}
|
|
|
+
|
|
|
+ @SuppressWarnings("unchecked") // from Mockito mocks
|
|
|
+ @Test
|
|
|
+ public <T extends TokenIdentifier> void testAddToken() throws Exception {
|
|
|
+ UserGroupInformation ugi =
|
|
|
+ UserGroupInformation.createRemoteUser("someone");
|
|
|
+
|
|
|
+ Token<T> t1 = mock(Token.class);
|
|
|
+ Token<T> t2 = mock(Token.class);
|
|
|
+ Token<T> t3 = mock(Token.class);
|
|
|
+
|
|
|
+ // add token to ugi
|
|
|
+ ugi.addToken(t1);
|
|
|
+ checkTokens(ugi, t1);
|
|
|
+
|
|
|
+ // replace token t1 with t2 - with same key (null)
|
|
|
+ ugi.addToken(t2);
|
|
|
+ checkTokens(ugi, t2);
|
|
|
+
|
|
|
+ // change t1 service and add token
|
|
|
+ when(t1.getService()).thenReturn(new Text("t1"));
|
|
|
+ ugi.addToken(t1);
|
|
|
+ checkTokens(ugi, t1, t2);
|
|
|
+
|
|
|
+ // overwrite t1 token with t3 - same key (!null)
|
|
|
+ when(t3.getService()).thenReturn(new Text("t1"));
|
|
|
+ ugi.addToken(t3);
|
|
|
+ checkTokens(ugi, t2, t3);
|
|
|
+
|
|
|
+ // just try to re-add with new name
|
|
|
+ when(t1.getService()).thenReturn(new Text("t1.1"));
|
|
|
+ ugi.addToken(t1);
|
|
|
+ checkTokens(ugi, t1, t2, t3);
|
|
|
+
|
|
|
+ // just try to re-add with new name again
|
|
|
+ ugi.addToken(t1);
|
|
|
+ checkTokens(ugi, t1, t2, t3);
|
|
|
+ }
|
|
|
|
|
|
+ private void checkTokens(UserGroupInformation ugi, Token<?> ... tokens) {
|
|
|
+ // check the ugi's token collection
|
|
|
+ Collection<Token<?>> ugiTokens = ugi.getTokens();
|
|
|
+ for (Token<?> t : tokens) {
|
|
|
+ assertTrue(ugiTokens.contains(t));
|
|
|
+ }
|
|
|
+ assertEquals(tokens.length, ugiTokens.size());
|
|
|
+
|
|
|
+ // check the ugi's credentials
|
|
|
+ Credentials ugiCreds = ugi.getCredentials();
|
|
|
+ for (Token<?> t : tokens) {
|
|
|
+ assertSame(t, ugiCreds.getToken(t.getService()));
|
|
|
+ }
|
|
|
+ assertEquals(tokens.length, ugiCreds.numberOfTokens());
|
|
|
+ }
|
|
|
+
|
|
|
+ @SuppressWarnings("unchecked") // from Mockito mocks
|
|
|
+ @Test
|
|
|
+ public <T extends TokenIdentifier> void testAddNamedToken() throws Exception {
|
|
|
+ UserGroupInformation ugi =
|
|
|
+ UserGroupInformation.createRemoteUser("someone");
|
|
|
+
|
|
|
+ Token<T> t1 = mock(Token.class);
|
|
|
+ Text service1 = new Text("t1");
|
|
|
+ Text service2 = new Text("t2");
|
|
|
+ when(t1.getService()).thenReturn(service1);
|
|
|
+
|
|
|
+ // add token
|
|
|
+ ugi.addToken(service1, t1);
|
|
|
+ assertSame(t1, ugi.getCredentials().getToken(service1));
|
|
|
+
|
|
|
+ // add token with another name
|
|
|
+ ugi.addToken(service2, t1);
|
|
|
+ assertSame(t1, ugi.getCredentials().getToken(service1));
|
|
|
+ assertSame(t1, ugi.getCredentials().getToken(service2));
|
|
|
+ }
|
|
|
+
|
|
|
@SuppressWarnings("unchecked") // from Mockito mocks
|
|
|
@Test
|
|
|
public <T extends TokenIdentifier> void testUGITokens() throws Exception {
|
|
@@ -221,7 +294,9 @@ public class TestUserGroupInformation {
|
|
|
UserGroupInformation.createUserForTesting("TheDoctor",
|
|
|
new String [] { "TheTARDIS"});
|
|
|
Token<T> t1 = mock(Token.class);
|
|
|
+ when(t1.getService()).thenReturn(new Text("t1"));
|
|
|
Token<T> t2 = mock(Token.class);
|
|
|
+ when(t2.getService()).thenReturn(new Text("t2"));
|
|
|
|
|
|
ugi.addToken(t1);
|
|
|
ugi.addToken(t2);
|