|
1 /* |
|
2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. |
|
8 * |
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
|
13 * accompanied this code). |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License version |
|
16 * 2 along with this work; if not, write to the Free Software Foundation, |
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 * |
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 * or visit www.oracle.com if you need additional information or have any |
|
21 * questions. |
|
22 */ |
|
23 |
|
24 import java.io.*; |
|
25 import java.net.*; |
|
26 import java.util.*; |
|
27 |
|
28 /** |
|
29 * @test |
|
30 * @bug 8034170 |
|
31 * @summary Digest authentication interop issue |
|
32 * @run main/othervm B8034170 unquoted |
|
33 * @run main/othervm -Dhttp.auth.digest.quoteParameters=true B8034170 quoted |
|
34 */ |
|
35 |
|
36 public class B8034170 { |
|
37 |
|
38 static boolean expectQuotes; |
|
39 |
|
40 static class BasicServer extends Thread { |
|
41 |
|
42 ServerSocket server; |
|
43 |
|
44 Socket s; |
|
45 InputStream is; |
|
46 OutputStream os; |
|
47 |
|
48 static final String realm = "wallyworld"; |
|
49 |
|
50 String reply1 = "HTTP/1.1 401 Unauthorized\r\n"+ |
|
51 "WWW-Authenticate: Digest realm=\""+realm+"\", qop=\"auth\"" + |
|
52 ", nonce=\"8989de95ea2402b64d73cecdb15da255\"" + |
|
53 ", opaque=\"bbfb4c9ee92ddccc73521c3e6e841ba2\"\r\n\r\n"; |
|
54 |
|
55 String OKreply = "HTTP/1.1 200 OK\r\n"+ |
|
56 "Date: Mon, 15 Jan 2001 12:18:21 GMT\r\n" + |
|
57 "Server: Apache/1.3.14 (Unix)\r\n" + |
|
58 "Connection: close\r\n" + |
|
59 "Content-Type: text/plain; charset=iso-8859-1\r\n" + |
|
60 "Content-Length: 10\r\n\r\n"; |
|
61 |
|
62 String ERRreply = "HTTP/1.1 500 Internal server error\r\n"+ |
|
63 "Date: Mon, 15 Jan 2001 12:18:21 GMT\r\n" + |
|
64 "Server: Apache/1.3.14 (Unix)\r\n" + |
|
65 "Connection: close\r\n" + |
|
66 "Content-Length: 0\r\n\r\n"; |
|
67 |
|
68 BasicServer (ServerSocket s) { |
|
69 server = s; |
|
70 } |
|
71 |
|
72 int readAll (Socket s, byte[] buf) throws IOException { |
|
73 int pos = 0; |
|
74 InputStream is = s.getInputStream (); |
|
75 // wait two seconds for request, as client doesn't close |
|
76 // the connection |
|
77 s.setSoTimeout(2000); |
|
78 try { |
|
79 int n; |
|
80 while ((n=is.read(buf, pos, buf.length-pos)) > 0) |
|
81 pos +=n; |
|
82 } catch (SocketTimeoutException x) { } |
|
83 return pos; |
|
84 } |
|
85 |
|
86 public void run () { |
|
87 byte[] buf = new byte[5000]; |
|
88 try { |
|
89 System.out.println ("Server 1: accept"); |
|
90 s = server.accept (); |
|
91 System.out.println ("accepted"); |
|
92 os = s.getOutputStream(); |
|
93 os.write (reply1.getBytes()); |
|
94 readAll (s, buf); |
|
95 s.close (); |
|
96 |
|
97 System.out.println ("Server 2: accept"); |
|
98 s = server.accept (); |
|
99 System.out.println ("accepted"); |
|
100 os = s.getOutputStream(); |
|
101 int count = readAll (s, buf); |
|
102 String reply = new String(buf, 0, count); |
|
103 |
|
104 boolean error; |
|
105 |
|
106 if (expectQuotes) { |
|
107 error = false; |
|
108 if (!reply.contains("qop=\"auth\"")) { |
|
109 System.out.println ("Expecting quoted qop. Not found"); |
|
110 error = true; |
|
111 } |
|
112 if (!reply.contains("algorithm=\"MD5\"")) { |
|
113 System.out.println ("Expecting quoted algorithm. Not found"); |
|
114 error = true; |
|
115 } |
|
116 } else { |
|
117 error = false; |
|
118 if (!reply.contains("qop=auth")) { |
|
119 System.out.println ("Expecting unquoted qop. Not found"); |
|
120 error = true; |
|
121 } |
|
122 if (!reply.contains("algorithm=MD5")) { |
|
123 System.out.println ("Expecting unquoted algorithm. Not found"); |
|
124 error = true; |
|
125 } |
|
126 } |
|
127 if (error) { |
|
128 os.write(ERRreply.getBytes()); |
|
129 os.flush(); |
|
130 s.close(); |
|
131 } else { |
|
132 os.write((OKreply+"HelloWorld").getBytes()); |
|
133 os.flush(); |
|
134 s.close(); |
|
135 } |
|
136 } |
|
137 catch (Exception e) { |
|
138 System.out.println (e); |
|
139 } |
|
140 finished (); |
|
141 } |
|
142 |
|
143 public synchronized void finished () { |
|
144 notifyAll(); |
|
145 } |
|
146 |
|
147 } |
|
148 |
|
149 static class MyAuthenticator3 extends Authenticator { |
|
150 PasswordAuthentication pw; |
|
151 MyAuthenticator3 () { |
|
152 super (); |
|
153 pw = new PasswordAuthentication ("user", "passwordNotCheckedAnyway".toCharArray()); |
|
154 } |
|
155 |
|
156 public PasswordAuthentication getPasswordAuthentication () |
|
157 { |
|
158 System.out.println ("Auth called"); |
|
159 return pw; |
|
160 } |
|
161 } |
|
162 |
|
163 |
|
164 static void read (InputStream is) throws IOException { |
|
165 int c; |
|
166 System.out.println ("reading"); |
|
167 while ((c=is.read()) != -1) { |
|
168 System.out.write (c); |
|
169 } |
|
170 System.out.println (""); |
|
171 System.out.println ("finished reading"); |
|
172 } |
|
173 |
|
174 public static void main (String args[]) throws Exception { |
|
175 expectQuotes = args[0].equals("quoted"); |
|
176 |
|
177 MyAuthenticator3 auth = new MyAuthenticator3 (); |
|
178 Authenticator.setDefault (auth); |
|
179 ServerSocket ss = new ServerSocket (0); |
|
180 int port = ss.getLocalPort (); |
|
181 BasicServer server = new BasicServer (ss); |
|
182 synchronized (server) { |
|
183 server.start(); |
|
184 System.out.println ("client 1"); |
|
185 URL url = new URL ("http://localhost:"+port+"/d1/d2/d3/foo.html"); |
|
186 URLConnection urlc = url.openConnection (); |
|
187 InputStream is = urlc.getInputStream (); |
|
188 read (is); |
|
189 is.close (); |
|
190 } |
|
191 } |
|
192 } |