1 /*******************************************************************************
3 * * ===========================================================================
4 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
5 * * ===========================================================================
6 * * Licensed under the Apache License, Version 2.0 (the "License");
7 * * you may not use this file except in compliance with the License.
8 * * You may obtain a copy of the License at
10 * * http://www.apache.org/licenses/LICENSE-2.0
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 * * ============LICENSE_END====================================================
20 ******************************************************************************/
22 package org.onap.aaf.cadi.test;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertThat;
26 import static org.junit.Assert.assertTrue;
27 import static org.hamcrest.CoreMatchers.is;
28 import static org.hamcrest.CoreMatchers.nullValue;
29 import static org.mockito.Mockito.mock;
30 import static org.mockito.Mockito.when;
32 import java.io.ByteArrayOutputStream;
33 import java.io.IOException;
34 import java.io.PrintStream;
35 import java.lang.reflect.Field;
36 import java.security.Principal;
37 import java.util.ArrayList;
38 import java.util.List;
40 import org.junit.After;
41 import org.junit.Before;
42 import org.junit.BeforeClass;
43 import org.junit.Test;
44 import org.mockito.Mock;
45 import org.mockito.MockitoAnnotations;
46 import org.onap.aaf.cadi.AbsUserCache;
47 import org.onap.aaf.cadi.Access;
48 import org.onap.aaf.cadi.CachedPrincipal.Resp;
49 import org.onap.aaf.cadi.CachingLur;
50 import org.onap.aaf.cadi.GetCred;
51 import org.onap.aaf.cadi.Permission;
52 import org.onap.aaf.cadi.PropAccess;
53 import org.onap.aaf.cadi.User;
54 import org.onap.aaf.cadi.lur.LocalPermission;
55 import org.onap.aaf.cadi.principal.CachedBasicPrincipal;
57 public class JU_AbsUserCache {
59 @Mock private CachingLur<Permission> cl;
60 @Mock private Principal principal;
61 @Mock private CachedBasicPrincipal cbp;
62 @Mock private LocalPermission permission1;
63 @Mock private LocalPermission permission2;
65 private Access access;
67 private ByteArrayOutputStream outStream;
69 private String name1 = "name1";
70 private String name2 = "name2";
71 private byte[] password = "password".getBytes();
73 private static Field timerField;
76 public static void setupOnce() throws Exception {
77 timerField = AbsUserCache.class.getDeclaredField("timer");
78 timerField.setAccessible(true);
82 public void setup() throws Exception {
83 MockitoAnnotations.initMocks(this);
85 outStream = new ByteArrayOutputStream();
86 System.setOut(new PrintStream(outStream));
88 // This must happen after changing System.out
89 access = new PropAccess();
91 when(permission1.getKey()).thenReturn("NewKey1");
92 when(permission2.getKey()).thenReturn("NewKey2");
94 timerField.set(null, null);
98 public void tearDown() throws Exception {
99 System.setOut(System.out);
100 timerField.set(null, null);
103 @SuppressWarnings("unused")
105 public void constructorTest() {
106 int cleanInterval = 65000;
107 int maxInterval = 70000;
109 AbsUserCacheStub<Permission> aucs1 = new AbsUserCacheStub<Permission>(access, cleanInterval, maxInterval, Integer.MAX_VALUE);
110 String output = outStream.toString().split(" ", 2)[1];
113 AbsUserCacheStub<Permission> aucs2 = new AbsUserCacheStub<Permission>(access, cleanInterval, maxInterval, Integer.MAX_VALUE);
114 output = outStream.toString().split(" ", 2)[1];
116 AbsUserCacheStub<Permission> aucs3 = new AbsUserCacheStub<Permission>(access, 0, 0, Integer.MAX_VALUE);
117 AbsUserCacheStub<Permission> aucs4 = new AbsUserCacheStub<Permission>(aucs1);
120 AbsUserCacheCLStub<Permission> auccls1 = new AbsUserCacheCLStub<Permission>(aucs1);
122 auccls1 = new AbsUserCacheCLStub<Permission>(aucs1);
123 AbsUserCacheCLStub<Permission> auccls2 = new AbsUserCacheCLStub<Permission>(aucs3);
127 public void setLurTest() {
128 AbsUserCacheStub<Permission> aucs1 = new AbsUserCacheStub<Permission>(access, 65000, 70000, Integer.MAX_VALUE);
129 AbsUserCacheStub<Permission> aucs2 = new AbsUserCacheStub<Permission>(access, 0, 0, Integer.MAX_VALUE);
135 public void addUserGetUserTest() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
136 AbsUserCacheStub<Permission> aucs = new AbsUserCacheStub<Permission>(access, 0, 0, Integer.MAX_VALUE);
137 User<Permission> user;
139 // Test adding a user with a principal (non-GetCred). user does not have a cred
140 // Then test getting that user
141 when(principal.getName()).thenReturn(name1);
142 user = new User<Permission>(principal, 0);
144 assertThat(aucs.getUser(principal), is(user));
146 // Test adding a user with a principal (GetCred). user does not have a cred
147 // Then test getting that user
148 GetCredStub gc = new GetCredStub();
149 user = new User<Permission>(gc, 0);
151 assertThat(aucs.getUser(gc), is(user));
153 // Test adding a user with no principal
154 // Then test getting that user via his name and cred
155 user = new User<Permission>(name2, password);
157 assertThat(aucs.getUser(name2, password), is(user));
159 // Test getting a user by a CachedBasicPrincipal
160 when(cbp.getName()).thenReturn(name2);
161 when(cbp.getCred()).thenReturn(password);
162 assertThat(aucs.getUser(cbp), is(user));
164 // Force the user to expire, then test that he is no longer in the cache
165 Field permExpiresField = User.class.getDeclaredField("permExpires");
166 permExpiresField.setAccessible(true);
167 permExpiresField.set(user, 0);
168 assertThat(aucs.getUser(name2, password), is(nullValue()));
170 // Test adding a user with a custom key
171 // Then test gettin that user
172 user = new User<Permission>(principal, 0);
173 String key = principal.getName() + "NoCred";
174 aucs.addUser(key, user);
175 assertThat(aucs.getUser(principal), is(user));
177 // Test that getUser returns null for principals that don't match any users
178 when(principal.getName()).thenReturn("not in the cache");
179 assertThat(aucs.getUser(principal), is(nullValue()));
181 // That that getUser returns null for name/creds that are not in the cache
182 assertThat(aucs.getUser("not a real user", "not in the cache".getBytes()), is(nullValue()));
186 public void removeTest() {
187 AbsUserCacheStub<Permission> aucs = new AbsUserCacheStub<Permission>(access, 0, 0, Integer.MAX_VALUE);
188 User<Permission> user;
190 when(principal.getName()).thenReturn(name1);
191 user = new User<Permission>(principal);
192 // Add a user with a principal
194 // Check that the user is in the cache
195 assertThat(aucs.getUser(principal), is(user));
197 when(principal.getName()).thenReturn(name1 + "NoCred");
199 // Check that the user is no longer in the cache
200 when(principal.getName()).thenReturn(name1);
201 assertThat(aucs.getUser(principal), is(nullValue()));
203 // Add the user again
205 // Check that the user is in the cache
206 assertThat(aucs.getUser(principal), is(user));
207 // Remove the user by name
208 aucs.remove(name1 + "NoCred");
209 // Check that the user is no longer in the cache
210 assertThat(aucs.getUser(principal), is(nullValue()));
212 // Coverage test - attempt to remove a user that is not in the cache
213 aucs.remove(name1 + "NoCred");
214 assertThat(aucs.getUser(principal), is(nullValue()));
218 public void clearAllTest() {
219 AbsUserCacheStub<Permission> aucs = new AbsUserCacheStub<Permission>(access, 0, 0, Integer.MAX_VALUE);
220 User<Permission> user1;
221 User<Permission> user2;
223 // Add some users to the cache
224 when(principal.getName()).thenReturn(name1);
225 user1 = new User<Permission>(principal);
226 when(principal.getName()).thenReturn(name2);
227 user2 = new User<Permission>(principal);
231 // Check that the users are in the cache
232 when(principal.getName()).thenReturn(name1);
233 assertThat(aucs.getUser(principal), is(user1));
234 when(principal.getName()).thenReturn(name2);
235 assertThat(aucs.getUser(principal), is(user2));
240 // Check that the users are no longer in the cache
241 when(principal.getName()).thenReturn(name1);
242 assertThat(aucs.getUser(principal), is(nullValue()));
243 when(principal.getName()).thenReturn(name2);
244 assertThat(aucs.getUser(principal), is(nullValue()));
248 public void dumpInfoTest() {
249 AbsUserCacheStub<Permission> aucs = new AbsUserCacheStub<Permission>(access, 0, 0, Integer.MAX_VALUE);
250 User<Permission> user1;
251 User<Permission> user2;
253 Principal principal1 = mock(Principal.class);
254 Principal principal2 = mock(Principal.class);
255 when(principal1.getName()).thenReturn(name1);
256 when(principal2.getName()).thenReturn(name2);
258 // Add some users with permissions to the cache
259 user1 = new User<Permission>(principal1);
260 user1.add(permission1);
261 user1.add(permission2);
262 user2 = new User<Permission>(principal2);
263 user2.add(permission1);
264 user2.add(permission2);
269 List<AbsUserCache<Permission>.DumpInfo> dumpInfo = aucs.dumpInfo();
270 assertThat(dumpInfo.size(), is(2));
273 List<String> names = new ArrayList<>();
276 List<String> permissions = new ArrayList<>();
277 permissions.add("NewKey1");
278 permissions.add("NewKey2");
280 // We need to use "contains" because the dumpInfo was created from a list, so we don't know it's order
281 for (AbsUserCache<Permission>.DumpInfo di : dumpInfo) {
282 assertTrue(names.contains(di.user));
283 for (String perm : di.perms) {
284 assertTrue(permissions.contains(perm));
290 public void handlesExclusivelyTest() {
291 AbsUserCacheStub<Permission> aucs = new AbsUserCacheStub<Permission>(access, 0, 0, Integer.MAX_VALUE);
292 assertFalse(aucs.handlesExclusively(permission1));
293 assertFalse(aucs.handlesExclusively(permission2));
297 public void destroyTest() {
298 AbsUserCacheStub<Permission> aucs = new AbsUserCacheStub<Permission>(access, 0, 0, Integer.MAX_VALUE);
300 aucs = new AbsUserCacheStub<Permission>(access, 1, 1, Integer.MAX_VALUE);
305 public void missTest() throws IOException {
306 AbsUserCacheStub<Permission> aucs = new AbsUserCacheStub<Permission>(access, 0, 0, Integer.MAX_VALUE);
307 // Add the Miss to the missmap
308 assertTrue(aucs.addMiss("key", password)); // This one actually adds it
309 assertTrue(aucs.addMiss("key", password)); // this one doesn't really do anything
310 assertTrue(aucs.addMiss("key", password)); // neither does this one
311 assertFalse(aucs.addMiss("key", password)); // By this time, the missMap is tired of this nonsense, and retaliates
312 assertFalse(aucs.addMiss("key", password)); // Oh yea. He's angry
314 // Can't really test this due to visibility
315 aucs.missed("key", password);
318 AbsUserCacheStub<Permission> aucs1 = new AbsUserCacheStub<Permission>(access, 1, 1, Integer.MAX_VALUE);
319 aucs1.addMiss("key", password);
322 class AbsUserCacheStub<PERM extends Permission> extends AbsUserCache<PERM> {
323 public AbsUserCacheStub(Access access, long cleanInterval, int highCount, int usageCount) { super(access, cleanInterval, highCount, usageCount); }
324 public AbsUserCacheStub(AbsUserCache<PERM> cache) { super(cache); }
325 @Override public void setLur(CachingLur<PERM> lur) { super.setLur(lur); }
326 @Override public void addUser(User<PERM> user) { super.addUser(user); }
327 @Override public void addUser(String key, User<PERM> user) { super.addUser(key, user); }
328 @Override public User<PERM> getUser(Principal p) { return super.getUser(p); }
329 @Override public User<PERM> getUser(CachedBasicPrincipal p) { return super.getUser(p); }
330 @Override public User<PERM> getUser(String user, byte[] cred) { return super.getUser(user, cred); }
331 @Override public void remove(User<PERM> user) { super.remove(user); }
332 @Override public boolean addMiss(String key, byte[] bs) { return super.addMiss(key, bs); }
333 @Override public Miss missed(String key, byte[] bs) throws IOException { return super.missed(key, bs); }
336 class AbsUserCacheCLStub<PERM extends Permission> extends AbsUserCache<PERM> implements CachingLur<PERM> {
337 public AbsUserCacheCLStub(AbsUserCache<PERM> cache) { super(cache); }
338 @Override public Permission createPerm(String p) { return null; }
339 @Override public boolean fish(Principal bait, Permission ... pond) { return false; }
340 @Override public void fishAll(Principal bait, List<Permission> permissions) { }
341 @Override public boolean handles(Principal principal) { return false; }
342 @Override public Resp reload(User<PERM> user) { return null; }
343 @Override public void setDebug(String commaDelimIDsOrNull) { }
346 class GetCredStub implements Principal, GetCred {
347 @Override public byte[] getCred() { return password; }
348 @Override public String getName() { return name1; }