1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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 }