1 | /** |
---|
2 | * Licensed to the Apache Software Foundation (ASF) under one |
---|
3 | * or more contributor license agreements. See the NOTICE file |
---|
4 | * distributed with this work for additional information |
---|
5 | * regarding copyright ownership. The ASF licenses this file |
---|
6 | * to you under the Apache License, Version 2.0 (the |
---|
7 | * "License"); you may not use this file except in compliance |
---|
8 | * with the License. You may obtain a copy of the License at |
---|
9 | * |
---|
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
11 | * |
---|
12 | * Unless required by applicable law or agreed to in writing, software |
---|
13 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
15 | * See the License for the specific language governing permissions and |
---|
16 | * limitations under the License. |
---|
17 | */ |
---|
18 | |
---|
19 | package org.apache.hadoop.security; |
---|
20 | |
---|
21 | import org.apache.hadoop.conf.Configuration; |
---|
22 | import org.apache.hadoop.io.TestWritable; |
---|
23 | |
---|
24 | import junit.framework.TestCase; |
---|
25 | |
---|
26 | /** Unit tests for UnixUserGroupInformation */ |
---|
27 | public class TestUnixUserGroupInformation extends TestCase { |
---|
28 | final private static String USER_NAME = "user1"; |
---|
29 | final private static String GROUP1_NAME = "group1"; |
---|
30 | final private static String GROUP2_NAME = "group2"; |
---|
31 | final private static String GROUP3_NAME = "group3"; |
---|
32 | final private static String[] GROUP_NAMES = |
---|
33 | new String[]{GROUP1_NAME, GROUP2_NAME, GROUP3_NAME}; |
---|
34 | |
---|
35 | /** Test login method */ |
---|
36 | public void testLogin() throws Exception { |
---|
37 | Configuration conf = new Configuration(); |
---|
38 | |
---|
39 | // loin from unix |
---|
40 | String userName = UnixUserGroupInformation.getUnixUserName(); |
---|
41 | UnixUserGroupInformation curUserGroupInfo = |
---|
42 | UnixUserGroupInformation.login(conf); |
---|
43 | assertEquals(curUserGroupInfo.getUserName(), userName); |
---|
44 | assertTrue(curUserGroupInfo == UnixUserGroupInformation.login(conf)); |
---|
45 | |
---|
46 | // login from the configuration |
---|
47 | UnixUserGroupInformation userGroupInfo = new UnixUserGroupInformation( |
---|
48 | USER_NAME, GROUP_NAMES ); |
---|
49 | UnixUserGroupInformation.saveToConf(conf, |
---|
50 | UnixUserGroupInformation.UGI_PROPERTY_NAME, userGroupInfo); |
---|
51 | curUserGroupInfo = UnixUserGroupInformation.login(conf); |
---|
52 | assertEquals(curUserGroupInfo, userGroupInfo); |
---|
53 | assertTrue(curUserGroupInfo == UnixUserGroupInformation.login(conf)); |
---|
54 | } |
---|
55 | |
---|
56 | /** test constructor */ |
---|
57 | public void testConstructor() throws Exception { |
---|
58 | UnixUserGroupInformation uugi = |
---|
59 | new UnixUserGroupInformation(USER_NAME, GROUP_NAMES); |
---|
60 | assertEquals(uugi, new UnixUserGroupInformation( new String[]{ |
---|
61 | USER_NAME, GROUP1_NAME, GROUP2_NAME, GROUP3_NAME} )); |
---|
62 | // failure test |
---|
63 | testConstructorFailures(null, GROUP_NAMES); |
---|
64 | testConstructorFailures("", GROUP_NAMES); |
---|
65 | testConstructorFailures(USER_NAME, null); |
---|
66 | testConstructorFailures(USER_NAME, new String[0]); |
---|
67 | testConstructorFailures(USER_NAME, new String[]{null}); |
---|
68 | testConstructorFailures(USER_NAME, new String[]{""}); |
---|
69 | testConstructorFailures(USER_NAME, new String[]{GROUP1_NAME, null}); |
---|
70 | testConstructorFailures(USER_NAME, |
---|
71 | new String[]{GROUP1_NAME, null, GROUP2_NAME}); |
---|
72 | } |
---|
73 | |
---|
74 | private void testConstructorFailures(String userName, String[] groupNames) { |
---|
75 | boolean gotException = false; |
---|
76 | try { |
---|
77 | new UnixUserGroupInformation(userName, groupNames); |
---|
78 | } catch (Exception e) { |
---|
79 | gotException = true; |
---|
80 | } |
---|
81 | assertTrue(gotException); |
---|
82 | } |
---|
83 | |
---|
84 | public void testEquals() throws Exception { |
---|
85 | UnixUserGroupInformation uugi = |
---|
86 | new UnixUserGroupInformation(USER_NAME, GROUP_NAMES); |
---|
87 | |
---|
88 | assertEquals(uugi, uugi); |
---|
89 | assertEquals(uugi, new UnixUserGroupInformation(USER_NAME, GROUP_NAMES)); |
---|
90 | assertEquals(uugi, new UnixUserGroupInformation(USER_NAME, |
---|
91 | new String[]{GROUP1_NAME, GROUP3_NAME, GROUP2_NAME})); |
---|
92 | assertFalse(uugi.equals(new UnixUserGroupInformation())); |
---|
93 | assertFalse(uugi.equals(new UnixUserGroupInformation(USER_NAME, |
---|
94 | new String[]{GROUP2_NAME, GROUP3_NAME, GROUP1_NAME}))); |
---|
95 | } |
---|
96 | |
---|
97 | /** test Writable */ |
---|
98 | public void testWritable() throws Exception { |
---|
99 | UnixUserGroupInformation ugi = new UnixUserGroupInformation( |
---|
100 | USER_NAME, GROUP_NAMES); |
---|
101 | TestWritable.testWritable(ugi, new Configuration()); |
---|
102 | } |
---|
103 | } |
---|