AT&T 2.0.19 Code drop, stage 3
[aaf/authz.git] / auth / auth-core / src / test / java / org / onap / aaf / auth / validation / test / JU_Validator.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.auth.validation.test;
23
24 import static org.junit.Assert.*;
25 import static org.mockito.Mockito.*;
26
27 import java.lang.reflect.InvocationTargetException;
28 import java.lang.reflect.Method;
29 import java.util.regex.Pattern;
30
31 import static org.mockito.Matchers.*;
32 import org.mockito.Mock;
33
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.junit.Test;
37 import org.onap.aaf.auth.env.AuthzTrans;
38 import org.onap.aaf.auth.env.AuthzTransOnlyFilter;
39 import org.onap.aaf.auth.validation.Validator;
40 import org.onap.aaf.cadi.principal.TaggedPrincipal;
41
42 public class JU_Validator {
43         
44         Validator validator;
45         
46         @Before
47         public void setUp() {
48                 validator = new Validator();
49         }
50         
51         @Test
52         public void testNullOrBlank() {
53                 validator.nullOrBlank(null, "str");
54                 validator.nullOrBlank("test", "");
55                 validator.nullOrBlank("test", null);
56         }
57         
58         @Test
59         public void testIsNull() {
60                 Object o = new Object();
61                 validator.isNull(null, null);
62                 validator.isNull(null, o);
63         }
64         
65         @Test
66         public void testDescription() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
67                 Class c = validator.getClass();
68                 Class[] cArg = new Class[2];
69                 cArg[0] = String.class;
70                 cArg[1] = String.class;         //Steps to test a protected method
71                 Method descriptionMethod = c.getDeclaredMethod("description", cArg);
72                 descriptionMethod.setAccessible(true);
73                 descriptionMethod.invoke(validator,"test", "test1");
74                 descriptionMethod.invoke(validator,null, null);
75                 descriptionMethod.invoke(validator,null, "[\\\\x25\\\\x28\\\\x29\\\\x2C-\\\\x2E\\\\x30-\\\\x39\\\\x3D\\\\x40-\\\\x5A\\\\x5F\\\\x61-\\\\x7A\\\\x20]+");
76
77                 
78         }
79
80         @Test
81         public void test() {
82                 assertTrue(Validator.ACTION_CHARS.matcher("HowdyDoody").matches());
83                 assertFalse(Validator.ACTION_CHARS.matcher("Howd?yDoody").matches());
84                 assertTrue(Validator.ACTION_CHARS.matcher("_HowdyDoody").matches());
85                 assertTrue(Validator.INST_CHARS.matcher("HowdyDoody").matches());
86                 assertFalse(Validator.INST_CHARS.matcher("Howd?yDoody").matches());
87                 assertTrue(Validator.INST_CHARS.matcher("_HowdyDoody").matches());
88
89                 //              
90                 assertTrue(Validator.ACTION_CHARS.matcher("*").matches());
91                 assertTrue(Validator.INST_CHARS.matcher("*").matches());
92                 assertFalse(Validator.ACTION_CHARS.matcher(":*").matches());
93                 assertTrue(Validator.INST_CHARS.matcher(":*").matches());
94                 assertFalse(Validator.ACTION_CHARS.matcher(":*:*").matches());
95                 assertTrue(Validator.INST_CHARS.matcher(":*:*").matches());
96                 
97                 assertFalse(Validator.ACTION_CHARS.matcher(":hello").matches());
98                 assertTrue(Validator.INST_CHARS.matcher(":hello").matches());
99                 assertFalse(Validator.INST_CHARS.matcher("hello:").matches());
100                 assertFalse(Validator.INST_CHARS.matcher("hello:d").matches());
101
102                 assertFalse(Validator.ACTION_CHARS.matcher(":hello:*").matches());
103                 assertTrue(Validator.INST_CHARS.matcher(":hello:*").matches());
104                 assertFalse(Validator.ACTION_CHARS.matcher(":hello:d*:*").matches());
105                 assertFalse(Validator.INST_CHARS.matcher(":hello:d*d:*").matches());
106                 assertTrue(Validator.INST_CHARS.matcher(":hello:d*:*").matches());
107                 assertFalse(Validator.ACTION_CHARS.matcher("HowdyDoody*").matches());
108                 assertFalse(Validator.INST_CHARS.matcher("Howdy*Doody").matches());
109                 assertTrue(Validator.INST_CHARS.matcher("HowdyDoody*").matches());
110                 assertFalse(Validator.ACTION_CHARS.matcher("*HowdyDoody").matches());
111                 assertFalse(Validator.INST_CHARS.matcher("*HowdyDoody").matches());
112                 assertFalse(Validator.ACTION_CHARS.matcher(":h*").matches());
113                 assertFalse(Validator.INST_CHARS.matcher(":h*h*").matches());
114                 assertTrue(Validator.INST_CHARS.matcher(":h*").matches());
115                 assertFalse(Validator.ACTION_CHARS.matcher(":h:h*:*").matches());
116                 assertTrue(Validator.INST_CHARS.matcher(":h:h*:*").matches());
117                 assertFalse(Validator.INST_CHARS.matcher(":h:h*h:*").matches());
118                 assertFalse(Validator.INST_CHARS.matcher(":h:h*h*:*").matches());
119                 assertFalse(Validator.ACTION_CHARS.matcher(":h:*:*h").matches());
120                 assertFalse(Validator.INST_CHARS.matcher(":h:*:*h").matches());
121                 assertTrue(Validator.INST_CHARS.matcher(":com.test.*:ns:*").matches());
122
123                 
124                 assertFalse(Validator.ACTION_CHARS.matcher("1234+235gd").matches());
125                 assertTrue(Validator.ACTION_CHARS.matcher("1234-235gd").matches());
126                 assertTrue(Validator.ACTION_CHARS.matcher("1234-23_5gd").matches());
127                 assertTrue(Validator.ACTION_CHARS.matcher("1234-235g,d").matches());
128                 assertTrue(Validator.ACTION_CHARS.matcher("1234-235gd(Version12)").matches());
129                 assertFalse(Validator.ACTION_CHARS.matcher("123#4-23@5g:d").matches());
130                 assertFalse(Validator.ACTION_CHARS.matcher("123#4-23@5g:d").matches());
131                 assertFalse(Validator.ACTION_CHARS.matcher("1234-23 5gd").matches());
132                 assertFalse(Validator.ACTION_CHARS.matcher("1234-235gd ").matches());
133                 assertFalse(Validator.ACTION_CHARS.matcher(" 1234-235gd").matches());
134                 assertFalse(Validator.ACTION_CHARS.matcher("").matches());
135                 assertFalse(Validator.ACTION_CHARS.matcher(" ").matches());
136
137                 // Allow % and =   (Needed for Escaping & Base64 usages) jg 
138                 assertTrue(Validator.ACTION_CHARS.matcher("1234%235g=d").matches());
139                 assertFalse(Validator.ACTION_CHARS.matcher(":1234%235g=d").matches());
140                 assertTrue(Validator.INST_CHARS.matcher("1234%235g=d").matches());
141                 assertTrue(Validator.INST_CHARS.matcher(":1234%235g=d").matches());
142                 assertTrue(Validator.INST_CHARS.matcher(":1234%235g=d:%20==").matches());
143                 assertTrue(Validator.INST_CHARS.matcher(":1234%235g=d:==%20:=%23").matches());
144                 assertTrue(Validator.INST_CHARS.matcher(":1234%235g=d:*:=%23").matches());
145                 assertTrue(Validator.INST_CHARS.matcher(":1234%235g=d:==%20:*").matches());
146                 assertTrue(Validator.INST_CHARS.matcher(":*:==%20:*").matches());
147
148                 // Allow / instead of :  (more natural instance expression) jg 
149                 assertFalse(Validator.INST_CHARS.matcher("1234/a").matches());
150                 assertTrue(Validator.INST_CHARS.matcher("/1234/a").matches());
151                 assertTrue(Validator.INST_CHARS.matcher("/1234/*/a/").matches());
152                 assertTrue(Validator.INST_CHARS.matcher("/1234//a").matches());
153                 assertFalse(Validator.ACTION_CHARS.matcher("1234/a").matches());
154                 assertFalse(Validator.ACTION_CHARS.matcher("/1234/*/a/").matches());
155                 assertFalse(Validator.ACTION_CHARS.matcher("1234//a").matches());
156
157
158                 assertFalse(Validator.INST_CHARS.matcher("1234+235gd").matches());
159                 assertTrue(Validator.INST_CHARS.matcher("1234-235gd").matches());
160                 assertTrue(Validator.INST_CHARS.matcher("1234-23_5gd").matches());
161                 assertTrue(Validator.INST_CHARS.matcher("1234-235g,d").matches());
162                 assertTrue(Validator.INST_CHARS.matcher("m1234@shb.dd.com").matches());
163                 assertTrue(Validator.INST_CHARS.matcher("1234-235gd(Version12)").matches());
164                 assertFalse(Validator.INST_CHARS.matcher("123#4-23@5g:d").matches());
165                 assertFalse(Validator.INST_CHARS.matcher("123#4-23@5g:d").matches());
166                 assertFalse(Validator.INST_CHARS.matcher("").matches());
167
168                 
169                 for( char c=0x20;c<0x7F;++c) {
170                         boolean b;
171                         switch(c) {
172                                 case '?':
173                                 case '|':
174                                 case '*':
175                                         continue; // test separately
176                                 case '~':
177                                 case ',':
178                                         b = false;
179                                         break;
180                                 default:
181                                         b=true;
182                         }
183                 }
184                 
185                 assertFalse(Validator.ID_CHARS.matcher("abc").matches());
186                 assertFalse(Validator.ID_CHARS.matcher("").matches());
187                 assertTrue(Validator.ID_CHARS.matcher("abc@att.com").matches());
188                 assertTrue(Validator.ID_CHARS.matcher("ab-me@att.com").matches());
189                 assertTrue(Validator.ID_CHARS.matcher("ab-me_.x@att._-com").matches());
190                 
191                 assertFalse(Validator.NAME_CHARS.matcher("ab-me_.x@att._-com").matches());
192                 assertTrue(Validator.NAME_CHARS.matcher("ab-me").matches());
193                 assertTrue(Validator.NAME_CHARS.matcher("ab-me_.xatt._-com").matches());
194
195                 
196                 // 7/22/2016
197                 assertTrue(Validator.INST_CHARS.matcher(
198                                 "/!com.att.*/role/write").matches());
199                 assertTrue(Validator.INST_CHARS.matcher(
200                                 ":!com.att.*:role:write").matches());
201
202         }
203
204 }