KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > core > security > filters > FilterTest


1 /* ====================================================================
2  * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
3  *
4  * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by Jcorporate Ltd.
21  * (http://www.jcorporate.com/)."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. "Jcorporate" and product names such as "Expresso" must
26  * not be used to endorse or promote products derived from this
27  * software without prior written permission. For written permission,
28  * please contact info@jcorporate.com.
29  *
30  * 5. Products derived from this software may not be called "Expresso",
31  * or other Jcorporate product names; nor may "Expresso" or other
32  * Jcorporate product names appear in their name, without prior
33  * written permission of Jcorporate Ltd.
34  *
35  * 6. No product derived from this software may compete in the same
36  * market space, i.e. framework, without prior written permission
37  * of Jcorporate Ltd. For written permission, please contact
38  * partners@jcorporate.com.
39  *
40  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
41  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43  * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
44  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
46  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
47  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This software consists of voluntary contributions made by many
55  * individuals on behalf of the Jcorporate Ltd. Contributions back
56  * to the project(s) are encouraged when you make modifications.
57  * Please send them to support@jcorporate.com. For more information
58  * on Jcorporate Ltd. and its products, please see
59  * <http://www.jcorporate.com/>.
60  *
61  * Portions of this software are based upon other open source
62  * products and are subject to their respective licenses.
63  */

64
65 package com.jcorporate.expresso.core.security.filters;
66
67 import com.jcorporate.expresso.services.test.ExpressoTestCase;
68 import junit.framework.TestSuite;
69
70
71 /**
72  * Unit test for Security Filters
73  *
74  * @author Michael Rimov
75  * @version $Revision: 1.2 $ $Date: 2004/11/17 20:48:23 $
76  */

77 public class FilterTest
78         extends ExpressoTestCase {
79     private static final String JavaDoc[] TestStrings = {
80         "abcdefg<>%^lmnop",
81         "\"This is a test of the emergency broadcast system!!!\"",
82         "cat /etc/password", "\\What about This???\\", "\' Or this \'",
83         "& This? ", "And lets just talk about some other stuff\n \t!!",
84         "<a HREF=\"www.nazdome.com\">",
85         "javascript:openWindow(\"Bad Girls dot Com\");"
86     };
87     FilterManager fm = null;
88
89     public FilterTest(String JavaDoc testName)
90             throws Exception JavaDoc {
91         super(testName);
92     }
93
94     public void setUp() {
95
96         //Load FilterManager
97
fm = FilterManager.getInstance();
98
99         if (fm == null) {
100             fail("Filter Manager was NULL!");
101
102             return;
103         }
104     }
105
106     public static void main(String JavaDoc[] args)
107             throws Exception JavaDoc {
108         junit.textui.TestRunner.run(suite());
109     }
110
111     public static junit.framework.Test suite()
112             throws Exception JavaDoc {
113         return new TestSuite(FilterTest.class);
114     }
115
116     public void testStandardFilter()
117             throws Exception JavaDoc {
118         final String JavaDoc[] StandardFilterResult = {
119             "abcdefg&lt;&gt;%^lmnop",
120             "&quot;This is a test of the emergency broadcast " +
121                 "system!!!&quot;",
122             "cat /etc/password", "\\What about This???\\", "' Or this '",
123             "&amp; This? ",
124             "And lets just talk about some other stuff<br /> &nbsp;&nbsp;!!",
125             "&lt;a HREF=&quot;www.nazdome.com&quot;&gt;",
126             "javascript:openWindow(&quot;Bad Girls " + "dot Com&quot;);"
127         };
128         String JavaDoc result1;
129         String JavaDoc result2;
130
131         //Standard Filter
132
for (int i = 0; i < TestStrings.length; i++) {
133             result1 = fm.filterString(TestStrings[i], ISO_8859_1.class,
134                     "standardFilter");
135             assertTrue("Standard Filter Round 1: i=" + Integer.toString(i),
136                     result1.equals(StandardFilterResult[i]));
137
138             //result2 should equal reslt1. Further filtering shouldn't cause
139
//further multilation of the filtering string.
140
result2 = fm.filterString(result1, ISO_8859_1.class,
141                     "standardFilter");
142             assertTrue("Standard Filter Round 2: i=" + Integer.toString(i),
143                     result2.equals(result1));
144         }
145     }
146
147     public void testStripFilter()
148             throws Exception JavaDoc {
149 // result with stripped chars replaced with " " - stripFilter has now changed
150
// final String[] StripFilterResult = {
151
// "abcdefg %^lmnop",
152
// " This is a test of the emergency broadcast system!!! ",
153
// "cat /etc/password", "\\What about This???\\", "' Or this '",
154
// " This? ", "And lets just talk about some other stuff !!",
155
// " a HREF= www.nazdome.com ",
156
// "javascript:openWindow( Bad Girls dot Com );"
157
// };
158
final String JavaDoc[] StripFilterResult = {
159             "abcdefg%^lmnop",
160             "This is a test of the emergency broadcast system!!!",
161             "cat /etc/password", "\\What about This???\\", "' Or this '",
162             " This? ", "And lets just talk about some other stuff !!",
163             "a HREF=www.nazdome.com",
164             "javascript:openWindow(Bad Girls dot Com);"
165         };
166
167         String JavaDoc result1;
168         String JavaDoc result2;
169
170         for (int i = 0; i < TestStrings.length; i++) {
171             result1 = fm.filterString(TestStrings[i], ISO_8859_1.class,
172                     "stripFilter");
173             assertTrue("Strip Filter Round 1: i=" + Integer.toString(i),
174                     result1.equals(StripFilterResult[i]));
175
176             //result2 should equal reslt1. Further filtering shouldn't cause
177
//further multilation of the filtering string.
178
result2 = fm.filterString(result1, ISO_8859_1.class,
179                     "stripFilter");
180             assertTrue("Strip Filter Round 2: i=" + Integer.toString(i),
181                     result2.equals(result1));
182         }
183     }
184
185     public void testRawFilter()
186             throws Exception JavaDoc {
187         final String JavaDoc[] RawFilterResult = {
188             "abcdefg<>%^lmnop",
189             "\"This is a test of the emergency broadcast system!!!\"",
190             "cat /etc/password", "\\What about This???\\", "\' Or this \'",
191             "& This? ", "And lets just talk about some other stuff\n \t!!",
192             "<a HREF=\"www.nazdome.com\">",
193             "javascript:openWindow(\"Bad Girls dot Com\");"
194         };
195         String JavaDoc result1;
196         String JavaDoc result2;
197
198         for (int i = 0; i < TestStrings.length; i++) {
199             result1 = fm.filterString(TestStrings[i], ISO_8859_1.class,
200                     "rawFilter");
201             assertTrue("Raw Filter Rount 1: i=" + Integer.toString(i),
202                     result1.equals(RawFilterResult[i]));
203
204             //result2 should equal reslt1. Further filtering shouldn't cause
205
//further multilation of the filtering string.
206
result2 = fm.filterString(result1, ISO_8859_1.class,
207                     "rawFilter");
208             assertTrue("Raw Filter Rount 2: i=" + Integer.toString(i),
209                     result2.equals(result1));
210         }
211     }
212     /* This was removed because I suspect of an APi change that required it.
213
214 @todo research this.
215
216     public void testBadCharset() throws Exception {
217
218         //
219
220         //Finally, check for bad inputs
221
222         //
223
224         String temp;
225
226         try {
227
228             temp = fm.filterString(TestStrings[0], "ISO-8859", "rawFilter");
229
230             fail("Failed Test: Bad Characterset parameter");
231
232             return;
233
234         } catch(IllegalArgumentException e) { }
235
236     }
237
238 */

239     /**
240      * The filter manager should throw an IllegalArgumentException if a bad
241      * filtername is passed to it.
242      */

243     public void testBadFiltername()
244             throws Exception JavaDoc {
245         String JavaDoc temp;
246
247         try {
248             temp = fm.filterString(TestStrings[0], ISO_8859_1.class,
249                     "badFilterName");
250             fail("Failed Test: Bad FilterName parameter");
251
252             return;
253         } catch (IllegalArgumentException JavaDoc e) {
254         }
255     }
256
257 }
Popular Tags