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.*;
25 import static org.hamcrest.CoreMatchers.*;
26 import static org.mockito.Mockito.*;
30 import java.io.ByteArrayOutputStream;
31 import java.io.IOException;
32 import java.io.PrintStream;
33 import java.lang.reflect.Field;
34 import java.security.Principal;
35 import java.util.ArrayList;
36 import java.util.List;
38 import org.onap.aaf.cadi.AbsUserCache;
39 import org.onap.aaf.cadi.AbsUserCache.*;
40 import org.onap.aaf.cadi.Access;
41 import org.onap.aaf.cadi.CachedPrincipal.Resp;
42 import org.onap.aaf.cadi.CachingLur;
43 import org.onap.aaf.cadi.GetCred;
44 import org.onap.aaf.cadi.Hash;
45 import org.onap.aaf.cadi.Permission;
46 import org.onap.aaf.cadi.PropAccess;
47 import org.onap.aaf.cadi.User;
48 import org.onap.aaf.cadi.lur.LocalPermission;
49 import org.onap.aaf.cadi.principal.CachedBasicPrincipal;
51 public class JU_AbsUserCache {
53 @Mock private CachingLur<Permission> cl;
55 @Mock private Principal principal;
57 @Mock private CachedBasicPrincipal cbp;
59 @Mock private LocalPermission permission1;
60 @Mock private LocalPermission permission2;
63 private Access access;
65 private ByteArrayOutputStream outStream;
67 private String name1 = "name1";
68 private String name2 = "name2";
69 private byte[] password = "password".getBytes();
71 private static Field timerField;
74 public static void setupOnce() throws Exception {
75 timerField = AbsUserCache.class.getDeclaredField("timer");
76 timerField.setAccessible(true);
80 public void setup() throws Exception {
81 MockitoAnnotations.initMocks(this);
83 outStream = new ByteArrayOutputStream();
84 System.setOut(new PrintStream(outStream));
86 // This must happen after changing System.out
87 access = new PropAccess();
89 when(permission1.getKey()).thenReturn("NewKey1");
90 when(permission2.getKey()).thenReturn("NewKey2");
92 timerField.set(null, null);
96 public void tearDown() throws Exception {
97 System.setOut(System.out);
98 timerField.set(null, null);
101 @SuppressWarnings("unused")
103 public void constructorTest() {
104 int cleanInterval = 65000;
105 int maxInterval = 70000;
107 AbsUserCacheStub<Permission> aucs1 = new AbsUserCacheStub<Permission>(access, cleanInterval, maxInterval, Integer.MAX_VALUE);
108 String output = outStream.toString().split(" ", 2)[1];
109 StringBuilder expected = new StringBuilder();
110 expected.append("INIT [cadi] Cleaning Thread initialized with interval of ");
111 expected.append(String.valueOf(cleanInterval));
112 expected.append(" ms and max objects of ");
113 expected.append(String.valueOf(maxInterval));
114 expected.append("\n");
115 assertThat(output, is(expected.toString()));
118 AbsUserCacheStub<Permission> aucs2 = new AbsUserCacheStub<Permission>(access, cleanInterval, maxInterval, Integer.MAX_VALUE);
119 output = outStream.toString().split(" ", 2)[1];
120 expected = new StringBuilder();
121 expected.append("INIT [cadi] Cleaning Thread initialized with interval of ");
122 expected.append(String.valueOf(cleanInterval));
123 expected.append(" ms and max objects of ");
124 expected.append(String.valueOf(maxInterval));
125 expected.append("\n");
126 assertThat(output, is(expected.toString()));
128 AbsUserCacheStub<Permission> aucs3 = new AbsUserCacheStub<Permission>(access, 0, 0, Integer.MAX_VALUE);
129 AbsUserCacheStub<Permission> aucs4 = new AbsUserCacheStub<Permission>(aucs1);
132 AbsUserCacheCLStub<Permission> auccls1 = new AbsUserCacheCLStub<Permission>(aucs1);
134 auccls1 = new AbsUserCacheCLStub<Permission>(aucs1);
135 AbsUserCacheCLStub<Permission> auccls2 = new AbsUserCacheCLStub<Permission>(aucs3);
139 public void setLurTest() {
140 AbsUserCacheStub<Permission> aucs1 = new AbsUserCacheStub<Permission>(access, 65000, 70000, Integer.MAX_VALUE);
141 AbsUserCacheStub<Permission> aucs2 = new AbsUserCacheStub<Permission>(access, 0, 0, Integer.MAX_VALUE);
147 public void addUserGetUserTest() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
148 AbsUserCacheStub<Permission> aucs = new AbsUserCacheStub<Permission>(access, 0, 0, Integer.MAX_VALUE);
149 User<Permission> user;
151 // Test adding a user with a principal (non-GetCred). user does not have a cred
152 // Then test getting that user
153 when(principal.getName()).thenReturn(name1);
154 user = new User<Permission>(principal, 0);
156 assertThat(aucs.getUser(principal), is(user));
158 // Test adding a user with a principal (GetCred). user does not have a cred
159 // Then test getting that user
160 GetCredStub gc = new GetCredStub();
161 user = new User<Permission>(gc, 0);
163 assertThat(aucs.getUser(gc), is(user));
165 // Test adding a user with no principal
166 // Then test getting that user via his name and cred
167 user = new User<Permission>(name2, password);
169 assertThat(aucs.getUser(name2, password), is(user));
171 // Test getting a user by a CachedBasicPrincipal
172 when(cbp.getName()).thenReturn(name2);
173 when(cbp.getCred()).thenReturn(password);
174 assertThat(aucs.getUser(cbp), is(user));
176 // Force the user to expire, then test that he is no longer in the cache
177 Field permExpiresField = User.class.getDeclaredField("permExpires");
178 permExpiresField.setAccessible(true);
179 permExpiresField.set(user, 0);
180 assertThat(aucs.getUser(name2, password), is(nullValue()));
182 // Test adding a user with a custom key
183 // Then test gettin that user
184 user = new User<Permission>(principal, 0);
185 String key = principal.getName() + "NoCred";
186 aucs.addUser(key, user);
187 assertThat(aucs.getUser(principal), is(user));
189 // Test that getUser returns null for principals that don't match any users
190 when(principal.getName()).thenReturn("not in the cache");
191 assertThat(aucs.getUser(principal), is(nullValue()));
193 // That that getUser returns null for name/creds that are not in the cache
194 assertThat(aucs.getUser("not a real user", "not in the cache".getBytes()), is(nullValue()));
198 public void removeTest() {
199 AbsUserCacheStub<Permission> aucs = new AbsUserCacheStub<Permission>(access, 0, 0, Integer.MAX_VALUE);
200 User<Permission> user;
202 when(principal.getName()).thenReturn(name1);
203 user = new User<Permission>(principal);
204 // Add a user with a principal
206 // Check that the user is in the cache
207 assertThat(aucs.getUser(principal), is(user));
209 when(principal.getName()).thenReturn(name1 + "NoCred");
211 // Check that the user is no longer in the cache
212 when(principal.getName()).thenReturn(name1);
213 assertThat(aucs.getUser(principal), is(nullValue()));
215 // Add the user again
217 // Check that the user is in the cache
218 assertThat(aucs.getUser(principal), is(user));
219 // Remove the user by name
220 aucs.remove(name1 + "NoCred");
221 // Check that the user is no longer in the cache
222 assertThat(aucs.getUser(principal), is(nullValue()));
224 // Coverage test - attempt to remove a user that is not in the cache
225 aucs.remove(name1 + "NoCred");
226 assertThat(aucs.getUser(principal), is(nullValue()));
230 public void clearAllTest() {
231 AbsUserCacheStub<Permission> aucs = new AbsUserCacheStub<Permission>(access, 0, 0, Integer.MAX_VALUE);
232 User<Permission> user1;
233 User<Permission> user2;
235 // Add some users to the cache
236 when(principal.getName()).thenReturn(name1);
237 user1 = new User<Permission>(principal);
238 when(principal.getName()).thenReturn(name2);
239 user2 = new User<Permission>(principal);
243 // Check that the users are in the cache
244 when(principal.getName()).thenReturn(name1);
245 assertThat(aucs.getUser(principal), is(user1));
246 when(principal.getName()).thenReturn(name2);
247 assertThat(aucs.getUser(principal), is(user2));
252 // Check that the users are no longer in the cache
253 when(principal.getName()).thenReturn(name1);
254 assertThat(aucs.getUser(principal), is(nullValue()));
255 when(principal.getName()).thenReturn(name2);
256 assertThat(aucs.getUser(principal), is(nullValue()));
260 public void dumpInfoTest() {
261 AbsUserCacheStub<Permission> aucs = new AbsUserCacheStub<Permission>(access, 0, 0, Integer.MAX_VALUE);
262 User<Permission> user1;
263 User<Permission> user2;
265 Principal principal1 = mock(Principal.class);
266 Principal principal2 = mock(Principal.class);
267 when(principal1.getName()).thenReturn(name1);
268 when(principal2.getName()).thenReturn(name2);
270 // Add some users with permissions to the cache
271 user1 = new User<Permission>(principal1);
272 user1.add(permission1);
273 user1.add(permission2);
274 user2 = new User<Permission>(principal2);
275 user2.add(permission1);
276 user2.add(permission2);
281 List<AbsUserCache<Permission>.DumpInfo> dumpInfo = aucs.dumpInfo();
282 assertThat(dumpInfo.size(), is(2));
285 List<String> names = new ArrayList<String>();
288 List<String> permissions = new ArrayList<String>();
289 permissions.add("NewKey1");
290 permissions.add("NewKey2");
292 // We need to use "contains" because the dumpInfo was created from a list, so we don't know it's order
293 for (AbsUserCache<Permission>.DumpInfo di : dumpInfo) {
294 assertTrue(names.contains(di.user));
295 for (String perm : di.perms) {
296 assertTrue(permissions.contains(perm));
302 public void handlesExclusivelyTest() {
303 AbsUserCacheStub<Permission> aucs = new AbsUserCacheStub<Permission>(access, 0, 0, Integer.MAX_VALUE);
304 assertFalse(aucs.handlesExclusively(permission1));
305 assertFalse(aucs.handlesExclusively(permission2));
309 public void destroyTest() {
310 AbsUserCacheStub<Permission> aucs = new AbsUserCacheStub<Permission>(access, 0, 0, Integer.MAX_VALUE);
312 aucs = new AbsUserCacheStub<Permission>(access, 1, 1, Integer.MAX_VALUE);
317 public void missTest() throws IOException {
318 AbsUserCacheStub<Permission> aucs = new AbsUserCacheStub<Permission>(access, 0, 0, Integer.MAX_VALUE);
319 // Add the Miss to the missmap
320 assertTrue(aucs.addMiss("key", password)); // This one actually adds it
321 assertTrue(aucs.addMiss("key", password)); // this one doesn't really do anything
322 assertTrue(aucs.addMiss("key", password)); // neither does this one
323 assertFalse(aucs.addMiss("key", password)); // By this time, the missMap is tired of this nonsense, and retaliates
324 assertFalse(aucs.addMiss("key", password)); // Oh yea. He's angry
326 // Can't really test this due to visibility
327 aucs.missed("key", password);
330 AbsUserCacheStub<Permission> aucs1 = new AbsUserCacheStub<Permission>(access, 1, 1, Integer.MAX_VALUE);
331 aucs1.addMiss("key", password);
334 class AbsUserCacheStub<PERM extends Permission> extends AbsUserCache<PERM> {
335 public AbsUserCacheStub(Access access, long cleanInterval, int highCount, int usageCount) { super(access, cleanInterval, highCount, usageCount); }
336 public AbsUserCacheStub(AbsUserCache<PERM> cache) { super(cache); }
337 @Override public void setLur(CachingLur<PERM> lur) { super.setLur(lur); }
338 @Override public void addUser(User<PERM> user) { super.addUser(user); }
339 @Override public void addUser(String key, User<PERM> user) { super.addUser(key, user); }
340 @Override public User<PERM> getUser(Principal p) { return super.getUser(p); }
341 @Override public User<PERM> getUser(CachedBasicPrincipal p) { return super.getUser(p); }
342 @Override public User<PERM> getUser(String user, byte[] cred) { return super.getUser(user, cred); }
343 @Override public void remove(User<PERM> user) { super.remove(user); }
344 @Override public boolean addMiss(String key, byte[] bs) { return super.addMiss(key, bs); }
345 @Override public Miss missed(String key, byte[] bs) throws IOException { return super.missed(key, bs); }
348 class AbsUserCacheCLStub<PERM extends Permission> extends AbsUserCache<PERM> implements CachingLur<PERM> {
349 public AbsUserCacheCLStub(AbsUserCache<PERM> cache) { super(cache); }
350 @Override public Permission createPerm(String p) { return null; }
351 @Override public boolean fish(Principal bait, Permission pond) { return false; }
352 @Override public void fishAll(Principal bait, List<Permission> permissions) { }
353 @Override public boolean handles(Principal principal) { return false; }
354 @Override public Resp reload(User<PERM> user) { return null; }
355 @Override public void setDebug(String commaDelimIDsOrNull) { }
358 class GetCredStub implements Principal, GetCred {
359 @Override public byte[] getCred() { return password; }
360 @Override public String getName() { return name1; }