1   /*
2    * Copyright 2002-2009 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package net.sf.json.util;
18  
19  import junit.framework.TestCase;
20  import net.sf.json.JSONArray;
21  import net.sf.json.JSONObject;
22  
23  /**
24   * @author Andres Almiray <aalmiray@users.sourceforge.net>
25   */
26  public class TestWebUtils extends TestCase {
27     public static void main( String[] args ) {
28        junit.textui.TestRunner.run( TestWebUtils.class );
29     }
30  
31     public TestWebUtils( String name ) {
32        super( name );
33     }
34  
35     public void testProtect_comments() {
36        JSONObject jsonObject = new JSONObject();
37        jsonObject.element( "with:quotes", "json" );
38        WebUtils.setWebHijackPreventionStrategy( WebHijackPreventionStrategy.COMMENTS );
39        String str = WebUtils.protect( jsonObject );
40        assertEquals( 0, str.compareTo( "/*{\"with:quotes\":\"json\"}*/" ) );
41     }
42  
43     public void testProtect_comments_and_shrink() {
44        JSONObject jsonObject = new JSONObject();
45        jsonObject.element( "noquotes", "json" );
46        WebUtils.setWebHijackPreventionStrategy( WebHijackPreventionStrategy.COMMENTS );
47        String str = WebUtils.protect( jsonObject, true );
48        assertEquals( 0, str.compareTo( "/*{noquotes:\"json\"}*/" ) );
49     }
50  
51     public void testProtect_inifiniteLoop() {
52        JSONObject jsonObject = new JSONObject();
53        jsonObject.element( "with:quotes", "json" );
54        WebUtils.setWebHijackPreventionStrategy( WebHijackPreventionStrategy.INFINITE_LOOP );
55        String str = WebUtils.protect( jsonObject );
56        assertEquals( 0, str.compareTo( "while(1);{\"with:quotes\":\"json\"}" ) );
57     }
58  
59     public void testProtect_inifiniteLoop_and_shrink() {
60        JSONObject jsonObject = new JSONObject();
61        jsonObject.element( "noquotes", "json" );
62        WebUtils.setWebHijackPreventionStrategy( WebHijackPreventionStrategy.INFINITE_LOOP );
63        String str = WebUtils.protect( jsonObject, true );
64        assertEquals( 0, str.compareTo( "while(1);{noquotes:\"json\"}" ) );
65     }
66  
67     public void testToString_array_noquotes() {
68        JSONObject jsonObject = new JSONObject();
69        jsonObject.element( "noquotes", "json" );
70        JSONArray jsonArray = new JSONArray().element( jsonObject );
71        String str = WebUtils.toString( jsonArray );
72        assertEquals( 0, str.compareTo( "[{noquotes:\"json\"}]" ) );
73     }
74  
75     public void testToString_array_withquotes1() {
76        JSONObject jsonObject = new JSONObject();
77        jsonObject.element( "with quotes", "json" );
78        JSONArray jsonArray = new JSONArray().element( jsonObject );
79        String str = WebUtils.toString( jsonArray );
80        assertEquals( 0, str.compareTo( "[{\"with quotes\":\"json\"}]" ) );
81     }
82  
83     public void testToString_array_withquotes2() {
84        JSONObject jsonObject = new JSONObject();
85        jsonObject.element( "with:quotes", "json" );
86        JSONArray jsonArray = new JSONArray().element( jsonObject );
87        String str = WebUtils.toString( jsonArray );
88        assertEquals( 0, str.compareTo( "[{\"with:quotes\":\"json\"}]" ) );
89     }
90  
91     public void testToString_object_noquotes() {
92        JSONObject jsonObject = new JSONObject();
93        jsonObject.element( "noquotes", "json" );
94        String str = WebUtils.toString( jsonObject );
95        assertEquals( 0, str.compareTo( "{noquotes:\"json\"}" ) );
96     }
97  
98     public void testToString_object_withquotes1() {
99        JSONObject jsonObject = new JSONObject();
100       jsonObject.element( "with quotes", "json" );
101       String str = WebUtils.toString( jsonObject );
102       assertEquals( 0, str.compareTo( "{\"with quotes\":\"json\"}" ) );
103    }
104 
105    public void testToString_object_withquotes2() {
106       JSONObject jsonObject = new JSONObject();
107       jsonObject.element( "with:quotes", "json" );
108       String str = WebUtils.toString( jsonObject );
109       assertEquals( 0, str.compareTo( "{\"with:quotes\":\"json\"}" ) );
110    }
111 }