Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame^] | 1 | /* |
| 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 | |
| 17 | import java.io.*; |
| 18 | import java.net.*; |
| 19 | import com.sun.ejte.ccl.reporter.*; |
| 20 | |
| 21 | /* |
| 22 | * Unit test for 6170450 ("request.getScheme() does not return the correct |
| 23 | * protocol when using passthrough plugin"). |
| 24 | * |
| 25 | * This test includes a 'Proxy-keysize' header in the request and therefore |
| 26 | * expects the scheme of the URL returned as the value of the 'Location' |
| 27 | * response header to be HTTPS (as opposed to regular HTTP). |
| 28 | */ |
| 29 | public class WebTest { |
| 30 | |
| 31 | private static SimpleReporterAdapter stat |
| 32 | = new SimpleReporterAdapter("appserv-tests"); |
| 33 | private static final String TEST_NAME = "web-proxy-keysize"; |
| 34 | |
| 35 | private String host; |
| 36 | private String port; |
| 37 | private String contextRoot; |
| 38 | |
| 39 | public WebTest(String[] args) { |
| 40 | host = args[0]; |
| 41 | port = args[1]; |
| 42 | contextRoot = args[2]; |
| 43 | } |
| 44 | |
| 45 | public static void main(String[] args) { |
| 46 | stat.addDescription("Unit test for 6170450"); |
| 47 | WebTest webTest = new WebTest(args); |
| 48 | webTest.doTest(); |
| 49 | stat.printSummary(TEST_NAME); |
| 50 | } |
| 51 | |
| 52 | public void doTest() { |
| 53 | |
| 54 | try { |
| 55 | invokeJsp(); |
| 56 | } catch (Exception ex) { |
| 57 | stat.addStatus(TEST_NAME, stat.FAIL); |
| 58 | ex.printStackTrace(); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | private void invokeJsp() throws Exception { |
| 63 | |
| 64 | Socket sock = null; |
| 65 | OutputStream os = null; |
| 66 | InputStream is = null; |
| 67 | BufferedReader bis = null; |
| 68 | String line = null; |
| 69 | try { |
| 70 | sock = new Socket(host, new Integer(port).intValue()); |
| 71 | os = sock.getOutputStream(); |
| 72 | String get = "GET " + contextRoot + "/jsp/test.jsp" + " HTTP/1.0\n"; |
| 73 | System.out.println(get); |
| 74 | os.write(get.getBytes()); |
| 75 | os.write("Proxy-keysize: 512\n".getBytes()); |
| 76 | os.write("\n".getBytes()); |
| 77 | |
| 78 | is = sock.getInputStream(); |
| 79 | bis = new BufferedReader(new InputStreamReader(is)); |
| 80 | |
| 81 | while ((line = bis.readLine()) != null) { |
| 82 | if (line.startsWith("Location:")) { |
| 83 | break; |
| 84 | } |
| 85 | } |
| 86 | } finally { |
| 87 | try { |
| 88 | if (sock != null) { |
| 89 | sock.close(); |
| 90 | } |
| 91 | } catch(IOException ioe) { |
| 92 | // ignore |
| 93 | } |
| 94 | try { |
| 95 | if (os != null) { |
| 96 | os.close(); |
| 97 | } |
| 98 | } catch(IOException ioe) { |
| 99 | // ignore |
| 100 | } |
| 101 | try { |
| 102 | if (is != null) { |
| 103 | is.close(); |
| 104 | } |
| 105 | } catch(IOException ioe) { |
| 106 | // ignore |
| 107 | } |
| 108 | try { |
| 109 | if (bis != null) { |
| 110 | bis.close(); |
| 111 | } |
| 112 | } catch(IOException ioe) { |
| 113 | // ignore |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | if (line != null) { |
| 118 | System.out.println("Location header: " + line); |
| 119 | |
| 120 | String location = line.substring("Location:".length()).trim(); |
| 121 | if (location.startsWith("https")) { |
| 122 | stat.addStatus(TEST_NAME, stat.PASS); |
| 123 | } else { |
| 124 | System.err.println("Wrong schema in Location response header, " |
| 125 | + "expected: https"); |
| 126 | stat.addStatus(TEST_NAME, stat.FAIL); |
| 127 | } |
| 128 | } else { |
| 129 | System.err.println("Missing Location response header"); |
| 130 | stat.addStatus(TEST_NAME, stat.FAIL); |
| 131 | } |
| 132 | } |
| 133 | } |