001: /*
002: * Copyright 2001 Sun Microsystems, Inc. All rights reserved.
003: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
004: */
005: package com.sun.portal.rproxy.rewriter.engines.js.test;
006:
007: import com.sun.portal.rewriter.Rewriter;
008: import com.sun.portal.log.common.PortalLogger;
009: import com.sun.portal.rewriter.RewriterPool;
010: import com.sun.portal.rewriter.Translator;
011: import com.sun.portal.rewriter.engines.LanguageConstants;
012: import com.sun.portal.rewriter.rom.RuleSet;
013: import com.sun.portal.rewriter.test.util.BasicTestCase;
014: import com.sun.portal.rewriter.test.util.CreateRuleSet;
015: import com.sun.portal.rewriter.test.util.JScriptInterpreter;
016: import com.sun.portal.rproxy.rewriter.SRAPRewriterModule;
017: import com.sun.portal.rproxy.rewriter.test.util.SRAPJSWrapperHelper;
018: import com.sun.portal.rproxy.rewriter.test.util.SRAPTestSystemFactory;
019: import junit.framework.TestSuite;
020:
021: public class TestSRAPJSConvertSystemFunction extends BasicTestCase {
022: public TestSRAPJSConvertSystemFunction(String aName) {
023: super (aName);
024: }//constuctor
025:
026: public void testConvertSystemVarFunctionBoundary() throws Exception {
027: String[][] lData = { { null }, { "" }, { "\t\t\t" }, { "" },
028: //{"\t\t\t\n"},
029: //{"\n"},
030: { "\t\b" }, };
031:
032: Translator lSRAPTranslator = SRAPTestSystemFactory
033: .createSRAPTranslator();
034:
035: for (int i = 0; i < lData.length; i++) {
036: String functionDef = createSystemFunctionInTestEnv(
037: lData[i][0], lSRAPTranslator);
038: String result = JScriptInterpreter.exec(functionDef,
039: lSRAPTranslator.getJSFunctionSpec()
040: .getSystemFunctionName(), new String[] {
041: "null", lData[i][0], lData[i][0] });
042: assertEquals("Failed at i=" + i + ":", lData[i][0], result);
043: }
044: //tobedone
045: }//testConvertSystemVarFunctionBoundary()
046:
047: public void testConvertSystemBoundary() throws Exception {
048: String[][] lData = {
049: { "getWindow().location", "" },
050: { "getTop().location", "" },
051: { "window.location", "" },
052: { "document.location", "" },
053: { "document.URL", "" },
054: { "location", "" },
055: { "window.location", "/abc.html" },
056: { "document.location", "https://raja.sun.com/?abc=10" },
057: { "document.URL", "ftp://raju.com#23" },
058: { "location", "http://syu.com" },
059:
060: { "abclocation", "" }, { "locationabc", "" }, };
061:
062: Translator lSRAPTranslator = SRAPTestSystemFactory
063: .createSRAPTranslator();
064:
065: for (int i = 0; i < lData.length; i++) {
066: String result = JScriptInterpreter.exec(
067: createSystemFunctionInTestEnv(lData[i][1],
068: lSRAPTranslator), lSRAPTranslator
069: .getJSFunctionSpec()
070: .getSystemFunctionName(), new String[] {
071: "null", lData[i][0], lData[i][1] });
072: assertEquals("Failed at i=" + i, lData[i][1], result);
073: }
074: }//testConvertSystemBoundary()
075:
076: public void testConvertSystemBoundary1() throws Exception {
077: Object[][] lData = { { "window.location", null },
078: { "document.location", null },
079: { "document.URL", null }, { "location", null },
080: { "location", Boolean.FALSE },
081:
082: { "abclocation", null },
083: { "locationabc", Boolean.FALSE }, };
084:
085: Translator lSRAPTranslator = SRAPTestSystemFactory
086: .createSRAPTranslator();
087:
088: for (int i = 0; i < lData.length; i++) {
089:
090: Object result = JScriptInterpreter.execObject(
091: createSystemFunctionInTestEnv((String) lData[i][0],
092: lSRAPTranslator), lSRAPTranslator
093: .getJSFunctionSpec()
094: .getSystemFunctionName(), new Object[] {
095: "null", lData[i][0], lData[i][1] });
096:
097: assertEquals("Failed at i=" + i, lData[i][1], result);
098: }
099: }//testConvertSystemBoundary1()
100:
101: public void testBugNo4770669() throws Exception {
102: String lInput = "<HTML>\n"
103: + "<HEAD>\n"
104: + "<TITLE>JavaScript Test</TITLE>\n"
105: + "<SCRIPT LANGUAGE=\"JAVASCRIPT\">\n"
106: + "var protocol = window.location.protocol;\n"
107: + "document.writeln(\"protocol: \" + protocol + \"<BR>\");\n"
108: + "var host = window.location.host;\n"
109: + "document.writeln(\"host: \" + host + \"<BR>\");\n"
110: + "var port = window.location.port;\n"
111: + "document.writeln(\"port: \" + port + \"<BR>\");\n"
112: + "var pathname = window.location.pathname;\n"
113: + "document.writeln(\"pathname: \" + pathname + \"<BR>\");\n"
114: + "\n" + "surf_form_url = bogus;\n"
115: + "//myURL = protocol + \"//\" + host + pathname;\n"
116: + "//document.writeln(\"myURL: \" + myURL);\n" + "\n"
117: + "\n" + "</SCRIPT>\n" + "</HEAD>\n"
118: + "<BODY BGCOLOR=\"#FFFFFF\" TEXT=\"#000000\">\n"
119: + "</BODY>\n" + "</HTML>\n";
120:
121: String lExpectation = "<HTML>\n" + "<HEAD>\n"
122: + "<TITLE>JavaScript Test</TITLE>\n"
123: + SRAPJSWrapperHelper.SRAP_CONVERT_SYSTEM_FUNCTION_HTML
124: + "<SCRIPT LANGUAGE=\"JAVASCRIPT\">\n"
125: + "var protocol = "
126: + SRAPJSWrapperHelper.CONVERT_SYSTEM_FUNCTION_WRAP_START
127: + "window.location, 'window.location.protocol', window.location.protocol);\n"
128: + "document.writeln(\"protocol: \" + protocol + \"<BR>\");\n"
129: + "var host = "
130: + SRAPJSWrapperHelper.CONVERT_SYSTEM_FUNCTION_WRAP_START
131: + "window.location, 'window.location.host', window.location.host);\n"
132: + "document.writeln(\"host: \" + host + \"<BR>\");\n"
133: + "var port = "
134: + SRAPJSWrapperHelper.CONVERT_SYSTEM_FUNCTION_WRAP_START
135: + "window.location, 'window.location.port', window.location.port);\n"
136: + "document.writeln(\"port: \" + port + \"<BR>\");\n"
137: + "var pathname = "
138: + SRAPJSWrapperHelper.CONVERT_SYSTEM_FUNCTION_WRAP_START
139: + "window.location, 'window.location.pathname', window.location.pathname);\n"
140: + "document.writeln(\"pathname: \" + pathname + \"<BR>\");\n"
141: + "\n"
142: + "surf_form_url = bogus;\n"
143: + "//myURL = protocol + \"//\" + host + pathname;\n"
144: + "//document.writeln(\"myURL: \" + myURL);\n"
145: + "\n"
146: + "\n"
147: + "</SCRIPT>\n"
148: + "</HEAD>\n"
149: + "<BODY BGCOLOR=\"#FFFFFF\" TEXT=\"#000000\">\n"
150: + "</BODY>\n" + "</HTML>\n";
151:
152: String lRules = "<Variable name=\" location \" type=\"SYSTEM\"/>\n"
153: + "<Variable name=\" *.location \" type=\"SYSTEM\"/>\n"
154: + "<Variable name=\" *.href \" type=\"SYSTEM\"/>\n"
155: + "<Variable name=\" *.protocol \" type=\"SYSTEM\"/>\n"
156: + "<Variable name=\" *.hostname \" type=\"SYSTEM\"/>\n"
157: + "<Variable name=\" *.host \" type=\"SYSTEM\"/>\n"
158: + "<Variable name=\" *.port \" type=\"SYSTEM\"/>\n"
159: + "<Variable name=\" *.pathname \" type=\"SYSTEM\"/>\n"
160: + "<Variable name=\" *.hash \" type=\"SYSTEM\"/>\n"
161: + "<Variable name=\" *.search \" type=\"SYSTEM\"/>\n"
162: + "<Variable name=\" document.URL \" type=\"SYSTEM\"/>\n"
163: + "<Variable name=\"*location.href\" type=\"EXPRESSION\"/>"
164: + "<Variable name=\"tempDjs\" type=\"DJS\"/>";
165:
166: RuleSet lRuleSet = CreateRuleSet.withJSVariableRules(lRules);
167: Rewriter lRewriter = RewriterPool.create(lRuleSet,
168: LanguageConstants.HTML_MIME);
169: String lResult = lRewriter.rewrite(lInput,
170: SRAPTestSystemFactory.createSRAPTranslator());
171: assertEquals(lExpectation, lResult);
172: }//testBugNo4770669()
173:
174: public void testGatewaySchemeAndCaseStipping() throws Exception {
175: String[][] lData = {
176: {
177: "location.href",
178: "https://zeus.naWab.inda.COM:443/hp://raju.com#23",
179: "hp://raju.com#23" },
180: {
181: "location..hostname",
182: "https://zeus.naWab.inda.COM:443/hp://raju.com#23",
183: "raju.com" },
184: {
185: "location.host",
186: "https://zeus.naWab.inda.COM:443/hp://raju.com:56#23",
187: "raju.com:56" },
188: {
189: "location.hash",
190: "https://zeus.naWab.inda.COM:443/hp://raju.com?abc=10#23",
191: "#23" },
192: {
193: "location.protocol",
194: "https://zeus.naWab.inda.COM:443/hp://raju.com?abc=10#23",
195: "hp:" },
196: {
197: "location.pathname",
198: "https://zeus.naWab.inda.COM:443/hp://raju.com/raja/base.html?abc=10#23",
199: "/raja/base.html?abc=10" },
200: {
201: "location.pathname",
202: "https://zeus.naWab.inda.COM:443/hp://raju.com/raja/base.html#23",
203: "/raja/base.html" },
204: {
205: "location.pathname",
206: "https://zeus.naWab.inda.COM:443/hp://raju.com/raja/base.html?23",
207: "/raja/base.html" },
208: {
209: "location.pathname",
210: "https://zeus.naWab.inda.COM:443/hp://raju.com/raja/base.html#23?abc=10",
211: "/raja/base.html" },
212: {
213: "location.hash",
214: "https://zeus.naWab.inda.COM:443/hp://raju.com/raja/base.html#23?abc=10",
215: "#23" },
216: {
217: "location.search",
218: "https://zeus.naWab.inda.COM:443/hp://raju.com/raja/base.html#23?abc=10",
219: "?abc=10" },
220: {
221: "location.pathname",
222: "https://zeus.naWab.inda.COM:443/hp://raju.com/raja/",
223: "/raja/" },
224: {
225: "location.pathname",
226: "https://zeus.naWab.inda.COM:443/hp://raju.com/",
227: "/" },
228: {
229: "location.pathname",
230: "https://zeus.naWab.inda.COM:443/hp://raju.com",
231: "" },
232: {
233: "location.port",
234: "https://zeus.naWab.inda.COM:443/hp://raju.com:80",
235: "80" },
236: {
237: "location.port",
238: "https://zeus.naWab.inda.COM:443/hp://raju.com:8080/abc.html",
239: "8080" },
240: {
241: "location.port",
242: "https://zeus.naWab.inda.COM:443/hp://raju.com:88/a/b/c/c",
243: "88" },
244: {
245: "location.port",
246: "https://zeus.naWab.inda.COM:443/hp://raju.com/a/b/c/c",
247: "" },
248: {
249: "location.search",
250: "https://zeus.naWab.inda.COM:443/hp://raju.com?abc=10#23",
251: "" },
252: {
253: "location.search",
254: "https://zeus.naWab.inda.COM:443/hp://raju.com?abc=10",
255: "?abc=10" },
256: { "document.URL", "ftp://raju.com#23",
257: "ftp://raju.com#23" },
258: {
259: "document.URL",
260: "https://zeus.nawab.inda.com/ftp://raju.com#23",
261: "ftp://raju.com#23" },
262: {
263: "document.URL",
264: "https://zeus.nawab.inda.COM/ftp://raju.com#23",
265: "ftp://raju.com#23" },
266:
267: { "document.URL", "http://raju.com#23",
268: "http://raju.com#23" },
269: {
270: "document.URL",
271: "https://zeus.nawab.inda.com/https://raju.com#23",
272: "https://raju.com#23" },
273: {
274: "document.URL",
275: "https://zeus.nawab.inda.COM/htp://raju.com#23",
276: "htp://raju.com#23" },
277: {
278: "document.URL",
279: " https://zeus.nawab.inda.COM/htp://raju.com#23 ",
280: "htp://raju.com#23 \t\t " },
281:
282: {
283: "document.URL",
284: "https://zeus.nawab.inda.com/redirect/https://raju.com#23",
285: "https://raju.com#23" },
286: {
287: "document.URL",
288: "https://zeus.nawab.inda.COM/RedIREct/htp://raju.com#23",
289: "htp://raju.com#23" },
290: { "location", "ftp://raju.com#23", "ftp://raju.com#23" },
291: {
292: "location",
293: "https://zeus.nawab.inda.com/ftp://raju.com#23",
294: "ftp://raju.com#23" },
295: {
296: "location",
297: "https://zeus.nawab.inda.COM/ftp://raju.com#23",
298: "ftp://raju.com#23" },
299: { "location", "http://raju.com#23",
300: "http://raju.com#23" },
301: {
302: "location",
303: "https://zeus.nawab.inda.com/https://raju.com#23",
304: "https://raju.com#23" },
305: {
306: "location",
307: "https://zeus.nawab.inda.COM/htp://raju.com#23",
308: "htp://raju.com#23" },
309: {
310: "location",
311: "https://zeus.nawab.iNDA.com/redirect/https://raju.com#23",
312: "https://raju.com#23" },
313: {
314: "location",
315: "https://zeus.nawab.inda.COM/RedIREct/htp://raju.com#23",
316: "htp://raju.com#23" },
317: { "location", "http://syu.com", "http://syu.com" },
318: {
319: "location",
320: "httpS://zeus.nawab.inda.COM:443/RedIREct/htp://raju.com#23",
321: "htp://raju.com#23" },
322: {
323: "document.URL",
324: "https://zeus.nawab.inda.COM:443/RedIREct/htp://raju.com#23",
325: "htp://raju.com#23" },
326: {
327: "location",
328: "https://zeus.naWab.inda.COM:443/hp://raju.com#23",
329: "hp://raju.com#23" }, };
330:
331: Translator lSRAPTranslator = SRAPTestSystemFactory
332: .createSRAPTranslator();
333: String functionDef = "\n\nfunction testWrapper(a,b)\n{"
334: + "\nreturn "
335: + lSRAPTranslator.getJSFunctionSpec()
336: .getSystemFunctionName() + "(location,a,b);\n"
337: + "}\n";
338:
339: for (int i = 0; i < lData.length; i++) {
340: String tempDef = createSystemFunctionInTestEnv(lData[i][1],
341: lSRAPTranslator)
342: + functionDef;
343: String result = JScriptInterpreter.exec(tempDef,
344: "testWrapper", new String[] { lData[i][0],
345: lData[i][1] });
346: assertEquals("Failed at i=" + i, lData[i][2], result);
347: }
348: }//testGatewaySchemeAndCaseStipping()
349:
350: private String createSystemFunctionInTestEnv(String aURI,
351: Translator aSRAPTranslator) {
352: return "var gURIOfMyOwn = '"
353: + aURI
354: + "';\n"
355: + "var location = new CustomLocation( gURIOfMyOwn );\n"
356: + aSRAPTranslator.getJSFunctionSpec()
357: .getSystemFunctionDefination(
358: aSRAPTranslator.getPageSpec());
359: }//createSystemFunctionInTestEnv()
360:
361: public static void main(String[] args) {
362: SRAPRewriterModule.initFile();
363: BasicTestCase.run(TestSRAPJSConvertSystemFunction.class);
364: TestSuite testSuite = new TestSuite();
365: //testSuite.addTest( new TestSRAPJSConvertSystemFunction( "testConvertSystemVarFunctionBoundary" ) );
366: BasicTestCase.run(testSuite);
367: }//main()
368:
369: }//class TestSRAPJSConvertSystemFunction
|