blob: a143d2e1a1da100192b701481e0ad0f6e2fd72c2 [file] [log] [blame]
Vinay Vishal57171472018-09-18 20:22:00 +05301/*
2 * Copyright (c) 1997, 2018 Oracle and/or its affiliates. All rights reserved.
3 *
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v. 2.0, which is available at
6 * http://www.eclipse.org/legal/epl-2.0.
7 *
8 * This Source Code may also be made available under the following Secondary
9 * Licenses when the conditions for such availability set forth in the
10 * Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
11 * version 2 with the GNU Classpath Exception, which is available at
12 * https://www.gnu.org/software/classpath/license.html.
13 *
14 * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15 */
16
17import java.io.*;
18import java.net.*;
19import java.util.regex.Matcher;
20import java.util.regex.Pattern;
21import com.sun.ejte.ccl.reporter.*;
22
23/**
24 * Unit test for:
25 *
26 * ("XSS for HttpServletResponse.sendError()")
27 *
28 */
29public class WebTest {
30
31 private static SimpleReporterAdapter stat
32 = new SimpleReporterAdapter("appserv-tests");
33
34 private static final String TEST_NAME
35 = "http-response-error-message";
36
37 private static final Pattern PATTERN = Pattern.compile("http/\\d\\.\\d 403 .*Hi, there.*", Pattern.CASE_INSENSITIVE);
38
39 private String host;
40 private String port;
41 private String contextRoot;
42 private Socket sock = null;
43
44 public WebTest(String[] args) {
45 host = args[0];
46 port = args[1];
47 contextRoot = args[2];
48 }
David Matějčekf4dc06a2021-05-17 12:10:57 +020049
Vinay Vishal57171472018-09-18 20:22:00 +053050 public static void main(String[] args) {
51 stat.addDescription("Unit test for XSS HttpServletResponse.sendError");
52 WebTest webTest = new WebTest(args);
53 webTest.doTest();
54 stat.printSummary(TEST_NAME);
55 }
56
David Matějčekf4dc06a2021-05-17 12:10:57 +020057 public void doTest() {
58 try {
Vinay Vishal57171472018-09-18 20:22:00 +053059 invoke();
60 } catch (Exception ex) {
61 stat.addStatus(TEST_NAME, stat.FAIL);
62 ex.printStackTrace();
63 } finally {
64 try {
65 if (sock != null) {
66 sock.close();
67 }
68 } catch (IOException ioe) {
69 // ignore
70 }
71 }
72 }
73
74 private void invoke() throws Exception {
75
David Matějčekf4dc06a2021-05-17 12:10:57 +020076 System.out.println("Host=" + host + ", port=" + port);
Vinay Vishal57171472018-09-18 20:22:00 +053077 sock = new Socket(host, new Integer(port).intValue());
78 OutputStream os = sock.getOutputStream();
79 String get = "GET " + contextRoot + "/index.jsp HTTP/1.1\n";
80 System.out.println(get);
81 os.write(get.getBytes());
82 os.write("Host: localhost\n".getBytes());
83 os.write("Connection: close\n".getBytes());
84 os.write("\n".getBytes());
85
86 InputStream is = null;
87 BufferedReader bis = null;
88 boolean isExpected = false;
89
90 try {
91 is = sock.getInputStream();
92 bis = new BufferedReader(new InputStreamReader(is));
93 String line = null;
94 while ((line = bis.readLine()) != null) {
95 System.out.println(line);
96 Matcher m = PATTERN.matcher(line);
97 if (m.matches()) {
98 isExpected = true;
99
100 break;
101 }
102 }
103 } finally {
104 try {
105 if (is != null) {
106 is.close();
107 }
108 } catch (IOException ioe) {
109 // ignore
110 }
111 try {
112 if (bis != null) {
113 bis.close();
114 }
115 } catch (IOException ioe) {
116 // ignore
117 }
118 }
119
120 if (isExpected) {
121 stat.addStatus(TEST_NAME, stat.PASS);
122 } else {
123 stat.addStatus(TEST_NAME, stat.FAIL);
124 System.err.println("Missing expected response: " + PATTERN.toString());
125 }
126 }
127}