1 /*******************************************************************************
2 * ============LICENSE_START====================================================
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
11 * * http://www.apache.org/licenses/LICENSE-2.0
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====================================================
21 ******************************************************************************/
22 package org.onap.aaf.auth.validation.test;
24 import static org.junit.Assert.*;
25 import static org.mockito.Mockito.*;
27 import java.lang.reflect.InvocationTargetException;
28 import java.lang.reflect.Method;
29 import java.util.regex.Pattern;
31 import static org.mockito.Matchers.*;
32 import org.mockito.Mock;
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;
42 public class JU_Validator {
48 validator = new Validator();
52 public void testNullOrBlank() {
53 validator.nullOrBlank(null, "str");
54 validator.nullOrBlank("test", "");
55 validator.nullOrBlank("test", null);
59 public void testIsNull() {
60 Object o = new Object();
61 validator.isNull(null, null);
62 validator.isNull(null, o);
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]+");
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());
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());
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());
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());
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());
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());
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());
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());
169 for( char c=0x20;c<0x7F;++c) {
175 continue; // test separately
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());
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());
197 assertTrue(Validator.INST_CHARS.matcher(
198 "/!com.att.*/role/write").matches());
199 assertTrue(Validator.INST_CHARS.matcher(
200 ":!com.att.*:role:write").matches());