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