Check Perm Instance ending in colon
[aaf/authz.git] / cadi / core / src / test / java / org / onap / aaf / cadi / lur / test / JU_LocalLur.java
1 /*******************************************************************************
2  * ============LICENSE_START====================================================
3  * * org.onap.aaf
4  * * ===========================================================================
5  * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * * ===========================================================================
7  * * Licensed under the Apache License, Version 2.0 (the "License");
8  * * you may not use this file except in compliance with the License.
9  * * You may obtain a copy of the License at
10  * * 
11  *  *      http://www.apache.org/licenses/LICENSE-2.0
12  * * 
13  *  * Unless required by applicable law or agreed to in writing, software
14  * * distributed under the License is distributed on an "AS IS" BASIS,
15  * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * * See the License for the specific language governing permissions and
17  * * limitations under the License.
18  * * ============LICENSE_END====================================================
19  * *
20  * *
21  ******************************************************************************/
22 package org.onap.aaf.cadi.lur.test;
23
24 import static junit.framework.Assert.assertEquals;
25 import static junit.framework.Assert.assertFalse;
26 import static junit.framework.Assert.assertTrue;
27
28 import java.io.ByteArrayOutputStream;
29 import java.io.IOException;
30 import java.security.Principal;
31 import java.util.ArrayList;
32 import java.util.List;
33 import java.util.Set;
34 import java.util.TreeSet;
35
36 import org.junit.Test;
37 import org.onap.aaf.cadi.Lur;
38 import org.onap.aaf.cadi.Permission;
39 import org.onap.aaf.cadi.PropAccess;
40 import org.onap.aaf.cadi.Symm;
41 import org.onap.aaf.cadi.CredVal.Type;
42 import org.onap.aaf.cadi.config.UsersDump;
43 import org.onap.aaf.cadi.lur.LocalLur;
44 import org.onap.aaf.cadi.lur.LocalPermission;
45
46 public class JU_LocalLur {
47
48         @Test
49         public void test() throws IOException {
50                 final Symm symmetric = Symm.baseCrypt().obtain();
51                 LocalLur up;
52                 ByteArrayOutputStream baos = new ByteArrayOutputStream();
53                 baos.write(Symm.ENC.getBytes());
54                 symmetric.enpass("<pass>", baos);
55                 PropAccess ta = new PropAccess() {
56                         @Override
57                         public String decrypt(String encrypted, boolean anytext) throws IOException {
58                                 return symmetric.depass(encrypted);
59                         }
60
61                         @Override
62                         public String encrypt(String unencrypted) throws IOException {
63                                 return symmetric.enpass(unencrypted);
64                         }
65                         
66                 };
67                 
68                 Lur ml = up = new LocalLur(ta,"myname:groupA,groupB","admin:myname,yourname;suser:hisname,hername,m1234%"+baos.toString());
69
70                 
71 //              Permission admin = new LocalPermission("admin");
72 //              Permission suser = new LocalPermission("suser");
73 //              
74 //              // Check User fish
75 //              assertTrue(ml.fish(new JUPrincipal("myname"),admin));
76 //              assertTrue(ml.fish(new JUPrincipal("hisname"),admin));
77 //              assertFalse(ml.fish(new JUPrincipal("noname"),admin));
78 //              assertTrue(ml.fish(new JUPrincipal("itsname"),suser));
79 //              assertTrue(ml.fish(new JUPrincipal("hername"),suser));
80 //              assertFalse(ml.fish(new JUPrincipal("myname"),suser));
81 //              
82 //              // Check validate password
83 //              assertTrue(up.validate("m1234",Type.PASSWORD, "<pass>".getBytes()));
84 //              assertFalse(up.validate("m1234",Type.PASSWORD, "badPass".getBytes()));
85 //              
86                 // Check fishAll
87                 Set<String> set = new TreeSet<String>();
88                 List<Permission> perms = new ArrayList<Permission>();
89                 ml.fishAll(new JUPrincipal("myname"), perms);
90                 for(Permission p : perms) {
91                         set.add(p.getKey());
92                 }
93 //              assertEquals("[admin, groupA, groupB]",set.toString());
94                 UsersDump.write(System.out, up);
95                 System.out.flush();
96                 
97         }
98         
99         // Simplistic Principal for testing purposes
100         private static class JUPrincipal implements Principal {
101                 private String name;
102                 public JUPrincipal(String name) {
103                         this.name = name;
104                 }
105 //              @Override
106                 public String getName() {
107                         return name;
108                 }
109         }
110
111
112
113         
114         
115 }