3 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
5 * This program and the accompanying materials are made available under the
6 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
7 * and is available at http://www.eclipse.org/legal/epl-v10.html
10 package org.opendaylight.controller.usermanager.internal;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertFalse;
14 import static org.junit.Assert.assertTrue;
16 import java.util.Date;
17 import java.util.concurrent.ConcurrentHashMap;
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;
30 * Unit Tests for UserManagerImpl
32 public class UserManagerImplTest {
34 private static UserManagerImpl um;
37 * @throws java.lang.Exception
40 public static void setUpBeforeClass() throws Exception {
42 IUserManager userManager = (IUserManager) ServiceHelper
43 .getGlobalInstance(IUserManager.class, new Object());
44 if (userManager instanceof UserManagerImpl) {
45 um = (UserManagerImpl) userManager;
47 um = new UserManagerImpl();
48 um.setAuthProviders(new ConcurrentHashMap<String, IAAAProvider>());
50 // mock up a remote server list with a dummy server
51 um.setRemoteServerConfigList(new ConcurrentHashMap<String, ServerConfig>() {
52 static final long serialVersionUID = 1L;
54 put("dummyServerConfig", new ServerConfig() { // Server config can't be empty
55 static final long serialVersionUID = 8645L;
57 public String getAddress() {
61 public String getSecret() {
65 public String getProtocol() {
72 // mock up a localUserConfigList with an admin user
73 um.setLocalUserConfigList(new ConcurrentHashMap<String, UserConfig>() {
74 static final long serialVersionUID = 2L;
76 put("admin", new UserConfig("admin", "7029,7455,8165,7029,7881",
77 UserLevel.SYSTEMADMIN.toString()));
80 // instantiate an empty activeUser collection
81 um.setActiveUsers(new ConcurrentHashMap<String, AuthenticatedUser>());
89 * {@link org.opendaylight.controller.usermanager.internal.UserManagerImpl#addAAAProvider(org.opendaylight.controller.usermanager.IAAAProvider)}
93 public void testAddAAAProvider() {
94 // instantiate an anonymous AAAProvider
95 IAAAProvider a3p = new IAAAProvider() {
97 public AuthResponse authService(String userName, String password,
98 String server, String secretKey) {
99 return new AuthResponse();
102 public String getName() {
103 return "dummyAAAProvider";
107 um.addAAAProvider(a3p);
108 assertEquals(a3p, um.getAAAProvider("dummyAAAProvider"));
114 * {@link org.opendaylight.controller.usermanager.internal.UserManagerImpl#removeAAAProvider(org.opendaylight.controller.usermanager.IAAAProvider)}
116 * {@link org.opendaylight.controller.usermanager.internal.UserManagerImpl#getAAAProvider(java.lang.String)}
120 public void testRemoveAAAProvider() {
121 um.removeAAAProvider(um.getAAAProvider("dummyAAAProvider"));
122 assertTrue(um.getAAAProviderNames().isEmpty());
127 * {@link org.opendaylight.controller.usermanager.internal.UserManagerImpl#authenticate(java.lang.String, java.lang.String)}
131 public void testAuthenticateStringString() {
132 UserConfig uc = new UserConfig("administrator", "admin",
133 UserLevel.SYSTEMADMIN.toString());
135 AuthResultEnum authResult = um.authenticate("administrator", "admin");
136 assertEquals(authResult, AuthResultEnum.AUTH_ACCEPT_LOC);
141 * {@link org.opendaylight.controller.usermanager.internal.UserManagerImpl#addRemoveLocalUser(org.opendaylight.controller.usermanager.internal.UserConfig, boolean)}
145 public void testAddRemoveLocalUser() {
146 UserConfig uc = new UserConfig("sysadmin", "7029,7455,8165,7029,7881",
147 UserLevel.SYSTEMADMIN.toString());
149 assertTrue(um.getLocalUserList().contains(uc));
150 um.removeLocalUser(uc);
151 assertFalse(um.getLocalUserList().contains(uc));
156 * {@link org.opendaylight.controller.usermanager.internal.UserManagerImpl#changeLocalUserPassword(java.lang.String, java.lang.String, java.lang.String)}
160 public void testChangeLocalUserPassword() {
161 // fail("Not yet implemented");
166 * {@link org.opendaylight.controller.usermanager.internal.UserManagerImpl#userLogout(java.lang.String)}
170 public void testUserLogout() {
171 // fail("Not yet implemented");
176 * {@link org.opendaylight.controller.usermanager.internal.UserManagerImpl#userTimedOut(java.lang.String)}
180 public void testUserTimedOut() {
181 // fail("Not yet implemented");
186 * {@link org.opendaylight.controller.usermanager.internal.UserManagerImpl#authenticate(org.springframework.security.core.Authentication)}
190 public void testAuthenticateAuthentication() {
191 // fail("Not yet implemented");
196 * {@link org.opendaylight.controller.usermanager.internal.UserManagerImpl#saveLocalUserList()}
200 public void testSaveLocalUserList() {
201 // fail("Not yet implemented");
206 * {@link org.opendaylight.controller.usermanager.internal.UserManagerImpl#saveAAAServerList()}
210 public void testSaveAAAServerList() {
211 // fail("Not yet implemented");
216 * {@link org.opendaylight.controller.usermanager.internal.UserManagerImpl#saveAuthorizationList()}
220 public void testSaveAuthorizationList() {
221 // fail("Not yet implemented");
226 * {@link org.opendaylight.controller.usermanager.internal.UserManagerImpl#readObject(java.io.ObjectInputStream)}
230 public void testReadObject() {
231 // fail("Not yet implemented");
235 public void testGetUserLevel() {
236 um.addLocalUser(new UserConfig("Jack", "password",
237 UserLevel.SYSTEMADMIN.toString()));
238 um.authenticate("Jack", "password");
240 um.addLocalUser(new UserConfig("John", "password",
241 UserLevel.NETWORKOPERATOR.toString()));
242 // Run the check on authenticated user
243 Assert.assertTrue(um.getUserLevel("Jack") == UserLevel.SYSTEMADMIN);
244 // Run the check on configured users
245 Assert.assertTrue(um.getUserLevel("John") == UserLevel.NETWORKOPERATOR);
246 Assert.assertTrue(um.getUserLevel("Andrew") == UserLevel.NOUSER);