Add several JUnits for test coverage
[aaf/authz.git] / cadi / core / src / test / java / org / onap / aaf / cadi / test / JU_AbsUserCache.java
1 /*******************************************************************************
2  * * org.onap.aaf
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
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  * * ============LICENSE_END====================================================
18  * *
19  * *
20  ******************************************************************************/
21
22 package org.onap.aaf.cadi.test;
23
24 import static org.junit.Assert.*;
25 import static org.hamcrest.CoreMatchers.*;
26 import static org.mockito.Mockito.*;
27 import org.junit.*;
28 import org.mockito.*;
29
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;
37
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;
50
51 public class JU_AbsUserCache {
52
53         @Mock private CachingLur<Permission> cl;
54
55         @Mock private Principal principal;
56
57         @Mock private CachedBasicPrincipal cbp;
58
59         @Mock private LocalPermission permission1;
60         @Mock private LocalPermission permission2;
61         
62
63         private Access access;
64
65         private ByteArrayOutputStream outStream;
66
67         private String name1 = "name1";
68         private String name2 = "name2";
69         private byte[] password = "password".getBytes();
70         
71         private static Field timerField;
72
73         @BeforeClass
74         public static void setupOnce() throws Exception {
75                 timerField = AbsUserCache.class.getDeclaredField("timer");
76                 timerField.setAccessible(true);
77         }
78
79         @Before
80         public void setup() throws Exception {
81                 MockitoAnnotations.initMocks(this);
82
83                 outStream = new ByteArrayOutputStream();
84                 System.setOut(new PrintStream(outStream));
85
86                 // This must happen after changing System.out
87                 access = new PropAccess();
88
89                 when(permission1.getKey()).thenReturn("NewKey1");
90                 when(permission2.getKey()).thenReturn("NewKey2");
91
92                 timerField.set(null, null);
93         }
94
95         @After
96         public void tearDown() throws Exception {
97                 System.setOut(System.out);
98                 timerField.set(null, null);
99         }
100
101         @SuppressWarnings("unused")
102         @Test
103         public void constructorTest() {
104                 int cleanInterval = 65000;
105                 int maxInterval = 70000;
106
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()));
116
117                 outStream.reset();
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()));
127
128                 AbsUserCacheStub<Permission> aucs3 = new AbsUserCacheStub<Permission>(access, 0, 0, Integer.MAX_VALUE);
129                 AbsUserCacheStub<Permission> aucs4 = new AbsUserCacheStub<Permission>(aucs1);
130
131                 // For coverage
132                 AbsUserCacheCLStub<Permission> auccls1 = new AbsUserCacheCLStub<Permission>(aucs1);
133                 aucs1.setLur(cl);
134                 auccls1 = new AbsUserCacheCLStub<Permission>(aucs1);
135                 AbsUserCacheCLStub<Permission> auccls2 = new AbsUserCacheCLStub<Permission>(aucs3);
136         }
137
138         @Test
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);
142                 aucs1.setLur(cl);
143                 aucs2.setLur(cl);
144         }
145
146         @Test
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;
150
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);
155                 aucs.addUser(user);
156                 assertThat(aucs.getUser(principal), is(user));
157
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);
162                 aucs.addUser(user);
163                 assertThat(aucs.getUser(gc), is(user));
164
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);
168                 aucs.addUser(user);
169                 assertThat(aucs.getUser(name2, password), is(user));
170
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));
175
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()));
181
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));
188
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()));
192
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()));
195         }
196
197         @Test
198         public void removeTest() {
199                 AbsUserCacheStub<Permission> aucs = new AbsUserCacheStub<Permission>(access, 0, 0, Integer.MAX_VALUE);
200                 User<Permission> user;
201
202                 when(principal.getName()).thenReturn(name1);
203                 user = new User<Permission>(principal);
204                 // Add a user with a principal
205                 aucs.addUser(user);
206                 // Check that the user is in the cache
207                 assertThat(aucs.getUser(principal), is(user));
208                 // Remove the user
209                 when(principal.getName()).thenReturn(name1 + "NoCred");
210                 aucs.remove(user);
211                 // Check that the user is no longer in the cache
212                 when(principal.getName()).thenReturn(name1);
213                 assertThat(aucs.getUser(principal), is(nullValue()));
214
215                 // Add the user again
216                 aucs.addUser(user);
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()));
223
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()));
227         }
228
229         @Test
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;
234
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);
240                 aucs.addUser(user1);
241                 aucs.addUser(user2);
242
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));
248
249                 // Clear the cache
250                 aucs.clearAll();
251
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()));
257         }
258
259         @Test
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;
264
265                 Principal principal1 = mock(Principal.class);
266                 Principal principal2 = mock(Principal.class);
267                 when(principal1.getName()).thenReturn(name1);
268                 when(principal2.getName()).thenReturn(name2);
269
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);
277                 aucs.addUser(user1);
278                 aucs.addUser(user2);
279
280                 // Dump the info
281                 List<AbsUserCache<Permission>.DumpInfo> dumpInfo = aucs.dumpInfo();
282                 assertThat(dumpInfo.size(), is(2));
283
284                 // Utility lists
285                 List<String> names = new ArrayList<String>();
286                 names.add(name1);
287                 names.add(name2);
288                 List<String> permissions = new ArrayList<String>();
289                 permissions.add("NewKey1");
290                 permissions.add("NewKey2");
291
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));
297                         }
298                 }
299         }
300
301         @Test
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));
306         }
307
308         @Test
309         public void destroyTest() {
310                 AbsUserCacheStub<Permission> aucs = new AbsUserCacheStub<Permission>(access, 0, 0, Integer.MAX_VALUE); 
311                 aucs.destroy();
312                 aucs = new AbsUserCacheStub<Permission>(access, 1, 1, Integer.MAX_VALUE); 
313                 aucs.destroy();
314         }
315
316         @Test
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
325
326                 // Can't really test this due to visibility
327                 aucs.missed("key", password);
328
329                 // Coverage
330                 AbsUserCacheStub<Permission> aucs1 = new AbsUserCacheStub<Permission>(access, 1, 1, Integer.MAX_VALUE);
331                 aucs1.addMiss("key", password);
332         }
333
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); }
346         }
347
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) { }
356         }
357
358         class GetCredStub implements Principal, GetCred {
359                 @Override public byte[] getCred() { return password; }
360                 @Override public String getName() { return name1; }
361         }
362
363 }