UserManager as role coordinator
[controller.git] / opendaylight / usermanager / src / test / java / org / opendaylight / controller / usermanager / internal / UserManagerImplTest.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.controller.usermanager.internal;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertFalse;
13 import static org.junit.Assert.assertTrue;
14
15 import java.util.ArrayList;
16 import java.util.List;
17 import java.util.concurrent.ConcurrentHashMap;
18
19 import org.junit.Assert;
20 import org.junit.BeforeClass;
21 import org.junit.Test;
22 import org.opendaylight.controller.sal.authorization.AuthResultEnum;
23 import org.opendaylight.controller.sal.authorization.UserLevel;
24 import org.opendaylight.controller.sal.utils.ServiceHelper;
25 import org.opendaylight.controller.usermanager.AuthResponse;
26 import org.opendaylight.controller.usermanager.IAAAProvider;
27 import org.opendaylight.controller.usermanager.IUserManager;
28
29 /**
30  * Unit Tests for UserManagerImpl
31  */
32 public class UserManagerImplTest {
33
34     private static UserManagerImpl um;
35
36     /**
37      * @throws java.lang.Exception
38      */
39     @BeforeClass
40     public static void setUpBeforeClass() throws Exception {
41
42         IUserManager userManager = (IUserManager) ServiceHelper
43                 .getGlobalInstance(IUserManager.class, new Object());
44         if (userManager instanceof UserManagerImpl) {
45             um = (UserManagerImpl) userManager;
46         } else {
47             um = new UserManagerImpl();
48             um.setAuthProviders(new ConcurrentHashMap<String, IAAAProvider>());
49
50             // mock up a remote server list with a dummy server
51             um.setRemoteServerConfigList(new ConcurrentHashMap<String, ServerConfig>() {
52                 static final long serialVersionUID = 1L;
53                 {
54                     put("dummyServerConfig", new ServerConfig() {
55                         // Server config can't be empty
56                         static final long serialVersionUID = 8645L;
57
58                         @Override
59                         public String getAddress() {
60                             return "1.1.1.1";
61                         }
62
63                         @Override
64                         public String getSecret() {
65                             return "secret";
66                         }
67
68                         @Override
69                         public String getProtocol() {
70                             return "IPv4";
71                         }
72                     });
73                 }
74             });
75
76             // mock up a localUserConfigList with an admin user
77             um.setLocalUserConfigList(new ConcurrentHashMap<String, UserConfig>() {
78                 static final long serialVersionUID = 2L;
79                 {
80                     List<String> roles = new ArrayList<String>(1);
81                     roles.add(UserLevel.SYSTEMADMIN.toString());
82                     put("admin", new UserConfig("admin",
83                             "7029,7455,8165,7029,7881", roles));
84                 }
85             });
86
87             um.setAuthorizationConfList(new ConcurrentHashMap<String, AuthorizationConfig>() {
88                 static final long serialVersionUID = 2L;
89                 {
90                     List<String> roles = new ArrayList<String>(3);
91                     roles.add(UserLevel.NETWORKOPERATOR.toString());
92                     roles.add("Container1-Admin");
93                     roles.add("Application2-User");
94
95                     put("Andrew", new AuthorizationConfig("Andrew", roles));
96                 }
97             });
98             // instantiate an empty activeUser collection
99             um.setActiveUsers(new ConcurrentHashMap<String, AuthenticatedUser>());
100         }
101     }
102
103     /**
104      * Test method for
105      * {@link org.opendaylight.controller.usermanager.internal.UserManagerImpl#addAAAProvider(org.opendaylight.controller.usermanager.IAAAProvider)}
106      * .
107      */
108     @Test
109     public void testAddAAAProvider() {
110         // instantiate an anonymous AAAProvider
111         IAAAProvider a3p = new IAAAProvider() {
112
113             @Override
114             public AuthResponse authService(String userName, String password,
115                     String server, String secretKey) {
116                 return new AuthResponse();
117             };
118
119             @Override
120             public String getName() {
121                 return "dummyAAAProvider";
122             }
123         };
124
125         um.addAAAProvider(a3p);
126         assertEquals(a3p, um.getAAAProvider("dummyAAAProvider"));
127
128     }
129
130     /**
131      * Test method for
132      * {@link org.opendaylight.controller.usermanager.internal.UserManagerImpl#removeAAAProvider(org.opendaylight.controller.usermanager.IAAAProvider)}
133      * and for for
134      * {@link org.opendaylight.controller.usermanager.internal.UserManagerImpl#getAAAProvider(java.lang.String)}
135      * .
136      */
137     @Test
138     public void testRemoveAAAProvider() {
139         um.removeAAAProvider(um.getAAAProvider("dummyAAAProvider"));
140         assertTrue(um.getAAAProviderNames().isEmpty());
141     }
142
143     /**
144      * Test method for
145      * {@link org.opendaylight.controller.usermanager.internal.UserManagerImpl#authenticate(java.lang.String, java.lang.String)}
146      * .
147      */
148     @Test
149     public void testAuthenticateStringString() {
150         List<String> roles = new ArrayList<String>(1);
151         roles.add(UserLevel.SYSTEMADMIN.toString());
152         UserConfig uc = new UserConfig("administrator", "admin", roles);
153         um.addLocalUser(uc);
154         AuthResultEnum authResult = um.authenticate("administrator", "admin");
155         assertEquals(authResult, AuthResultEnum.AUTH_ACCEPT_LOC);
156     }
157
158     /**
159      * Test method for
160      * {@link org.opendaylight.controller.usermanager.internal.UserManagerImpl#addRemoveLocalUser(org.opendaylight.controller.usermanager.internal.UserConfig, boolean)}
161      * .
162      */
163     @Test
164     public void testAddRemoveLocalUser() {
165         List<String> roles = new ArrayList<String>(1);
166         roles.add(UserLevel.SYSTEMADMIN.toString());
167         UserConfig uc = new UserConfig("sysadmin", "7029,7455,8165,7029,7881",
168                 roles);
169         um.addLocalUser(uc);
170         assertTrue(um.getLocalUserList().contains(uc));
171         um.removeLocalUser(uc);
172         assertFalse(um.getLocalUserList().contains(uc));
173     }
174
175     /**
176      * Test method for
177      * {@link org.opendaylight.controller.usermanager.internal.UserManagerImpl#changeLocalUserPassword(java.lang.String, java.lang.String, java.lang.String)}
178      * .
179      */
180     @Test
181     public void testChangeLocalUserPassword() {
182         // fail("Not yet implemented");
183     }
184
185     /**
186      * Test method for
187      * {@link org.opendaylight.controller.usermanager.internal.UserManagerImpl#userLogout(java.lang.String)}
188      * .
189      */
190     @Test
191     public void testUserLogout() {
192         // fail("Not yet implemented");
193     }
194
195     /**
196      * Test method for
197      * {@link org.opendaylight.controller.usermanager.internal.UserManagerImpl#userTimedOut(java.lang.String)}
198      * .
199      */
200     @Test
201     public void testUserTimedOut() {
202         // fail("Not yet implemented");
203     }
204
205     /**
206      * Test method for
207      * {@link org.opendaylight.controller.usermanager.internal.UserManagerImpl#authenticate(org.springframework.security.core.Authentication)}
208      * .
209      */
210     @Test
211     public void testAuthenticateAuthentication() {
212         // fail("Not yet implemented");
213     }
214
215     /**
216      * Test method for
217      * {@link org.opendaylight.controller.usermanager.internal.UserManagerImpl#saveLocalUserList()}
218      * .
219      */
220     @Test
221     public void testSaveLocalUserList() {
222         // fail("Not yet implemented");
223     }
224
225     /**
226      * Test method for
227      * {@link org.opendaylight.controller.usermanager.internal.UserManagerImpl#saveAAAServerList()}
228      * .
229      */
230     @Test
231     public void testSaveAAAServerList() {
232         // fail("Not yet implemented");
233     }
234
235     /**
236      * Test method for
237      * {@link org.opendaylight.controller.usermanager.internal.UserManagerImpl#saveAuthorizationList()}
238      * .
239      */
240     @Test
241     public void testSaveAuthorizationList() {
242         // fail("Not yet implemented");
243     }
244
245     /**
246      * Test method for
247      * {@link org.opendaylight.controller.usermanager.internal.UserManagerImpl#readObject(java.io.ObjectInputStream)}
248      * .
249      */
250     @Test
251     public void testReadObject() {
252         // fail("Not yet implemented");
253     }
254
255     @Test
256     public void testGetUserLevel() {
257         List<String> roles = new ArrayList<String>(2);
258         roles.add(UserLevel.SYSTEMADMIN.toString());
259         roles.add("App1_supervisor");
260         um.addLocalUser(new UserConfig("Jack", "password", roles));
261         um.authenticate("Jack", "password");
262
263         roles.clear();
264         roles.add("App2Admin");
265         roles.add(UserLevel.NETWORKOPERATOR.toString());
266         um.addLocalUser(new UserConfig("John", "password", roles));
267
268         // Run the check on authenticated user
269         Assert.assertTrue(um.getUserLevel("Jack") == UserLevel.SYSTEMADMIN);
270         // Run the check on configured users
271         Assert.assertTrue(um.getUserLevel("John") == UserLevel.NETWORKOPERATOR);
272         // Run the check on local authorized users
273         Assert.assertTrue(um.getUserLevel("Andrew") == UserLevel.NETWORKOPERATOR);
274         // Non locally known user
275         Assert.assertTrue(um.getUserLevel("Tom") == UserLevel.NOUSER);
276     }
277 }