1 /* Licensed to the Apache Software Foundation (ASF) under one or more 2 * contributor license agreements. See the NOTICE file distributed with 3 * this work for additional information regarding copyright ownership. 4 * The ASF licenses this file to You under the Apache License, Version 2.0 5 * (the "License"); you may not use this file except in compliance with 6 * the License. You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package org.apache.harmony.luni.tests.java.net; 18 19 import java.io.IOException; 20 import java.io.InputStream; 21 import java.io.OutputStream; 22 import java.net.Authenticator; 23 import java.net.InetAddress; 24 import java.net.MalformedURLException; 25 import java.net.PasswordAuthentication; 26 import java.net.ServerSocket; 27 import java.net.Socket; 28 import java.net.URL; 29 import java.net.UnknownHostException; 30 import java.net.Authenticator.RequestorType; 31 32 import junit.framework.TestCase; 33 import tests.support.Support_PortManager; 34 35 public class AuthenticatorTest extends TestCase { 36 37 /** 38 * @tests java.net.Authenticator.RequestorType#valueOf(String) 39 */ 40 public void test_RequestorType_valueOfLjava_lang_String() throws Exception { 41 assertEquals(RequestorType.PROXY, Authenticator.RequestorType 42 .valueOf("PROXY")); 43 assertEquals(RequestorType.SERVER, Authenticator.RequestorType 44 .valueOf("SERVER")); 45 try { 46 RequestorType rt = Authenticator.RequestorType.valueOf("BADNAME"); 47 fail("Must throw IllegalArgumentException"); 48 } catch (IllegalArgumentException e) { 49 // correct 50 } 51 // Some old RIs throw IllegalArgumentException 52 // Latest RIs throw NullPointerException. 53 try { 54 Authenticator.RequestorType.valueOf(null); 55 fail("Must throw an exception"); 56 } catch (NullPointerException e) { 57 // May be caused by some compilers' code 58 } catch (IllegalArgumentException e) { 59 // other compilers will throw this 60 } 61 } 62 63 /** 64 * @tests java.net.Authenticator.RequestorType#values() 65 */ 66 public void test_RequestorType_values() throws Exception { 67 RequestorType[] rt = RequestorType.values(); 68 assertEquals(RequestorType.PROXY, rt[0]); 69 assertEquals(RequestorType.SERVER, rt[1]); 70 } 71 72 /** 73 * @tests java.net.Authenticator#requestPasswordAuthentication(java.net.InetAddress, int, String, String, String) 74 */ 75 public void test_requestPasswordAuthentication_InetAddress_int_String_String_String() throws Exception { 76 // Regression test for Harmony-2413 77 MockAuthenticator mock = new MockAuthenticator(); 78 InetAddress addr = InetAddress.getLocalHost(); 79 Authenticator.setDefault(mock); 80 Authenticator.requestPasswordAuthentication(addr, -1, "http", "promt", "HTTP"); 81 assertEquals(mock.getRequestorType(), RequestorType.SERVER); 82 } 83 84 /** 85 * @tests java.net.Authenticator#requestPasswordAuthentication(String, java.net.InetAddress, int, String, String, String) 86 */ 87 public void test_requestPasswordAuthentication_String_InetAddress_int_String_String_String() throws Exception { 88 // Regression test for Harmony-2413 89 MockAuthenticator mock = new MockAuthenticator(); 90 InetAddress addr = InetAddress.getLocalHost(); 91 Authenticator.setDefault(mock); 92 Authenticator.requestPasswordAuthentication("test_host", addr, -1, "http", "promt", "HTTP"); 93 assertEquals(mock.getRequestorType(), RequestorType.SERVER); 94 } 95 96 /** 97 * 98 * @tests java.net.Authenticator# 99 * requestPasswordAuthentication_String_InetAddress_int_String_String_String_URL_Authenticator_RequestorType() 100 */ 101 public void test_requestPasswordAuthentication_String_InetAddress_int_String_String_String_URL_Authenticator_RequestorType() 102 throws UnknownHostException, MalformedURLException { 103 MockAuthenticator mock = new MockAuthenticator(); 104 URL url = new URL("http://127.0.0.1"); 105 Authenticator.requestPasswordAuthentication("localhost", InetAddress 106 .getByName("127.0.0.1"), 80, "HTTP", "", "", url, 107 RequestorType.PROXY); 108 assertNull(mock.getRequestingURL()); 109 assertNull(mock.getRequestorType()); 110 } 111 112 /** 113 * 114 * @tests java.net.Authenticator#getRequestingURL() 115 */ 116 public void test_getRequestingURL() throws Exception { 117 MockAuthenticator mock = new MockAuthenticator(); 118 assertNull(mock.getRequestingURL()); 119 } 120 121 /** 122 * 123 * @tests java.net.Authenticator#getRequestorType() 124 */ 125 public void test_getRequestorType() throws Exception { 126 MockAuthenticator mock = new MockAuthenticator(); 127 assertNull(mock.getRequestorType()); 128 } 129 130 /** 131 * @tests java.net.Authenticator#setDefault(java.net.Authenticator) 132 */ 133 public void test_setDefault() { 134 final int port = Support_PortManager.getNextPort(); 135 final Object lock = new Object(); 136 final int[] result = new int[1]; 137 Thread t = new Thread(new Runnable() { 138 public void run() { 139 try { 140 ServerSocket ss = null; 141 synchronized (lock) { 142 ss = new ServerSocket(port); 143 lock.notifyAll(); 144 } 145 Socket s = ss.accept(); 146 InputStream in = s.getInputStream(); 147 in.read(new byte[1024]); 148 OutputStream out = s.getOutputStream(); 149 out 150 .write("HTTP/1.1 401 Unauthorized\r\nWWW-Authenticate:Basic realm=\"something\"\r\n\r\n" 151 .getBytes("ISO8859_1")); 152 Thread.sleep(500); 153 out.close(); 154 } catch (Exception e) { 155 e.printStackTrace(); 156 } 157 } 158 }); 159 synchronized (lock) { 160 t.start(); 161 try { 162 lock.wait(); 163 } catch (InterruptedException e) { 164 // ignored 165 } 166 } 167 Authenticator.setDefault(new Authenticator() { 168 protected PasswordAuthentication getPasswordAuthentication() { 169 synchronized (lock) { 170 result[0] = 1; 171 } 172 return null; 173 } 174 }); 175 Authenticator.setDefault(new Authenticator() { 176 protected PasswordAuthentication getPasswordAuthentication() { 177 synchronized (lock) { 178 result[0] = 2; 179 } 180 return null; 181 } 182 }); 183 try { 184 new URL("http://localhost:" + port).openStream(); 185 } catch (IOException e) { 186 // ignored 187 } 188 synchronized (lock) { 189 assertEquals("wrong authenticator: " + result[0], 2, result[0]); 190 } 191 } 192 193 /* 194 * Mock Authernticator for test 195 */ 196 class MockAuthenticator extends java.net.Authenticator { 197 public MockAuthenticator() { 198 super(); 199 } 200 201 public URL getRequestingURL() { 202 return super.getRequestingURL(); 203 } 204 205 public Authenticator.RequestorType getRequestorType() { 206 return super.getRequestorType(); 207 } 208 } 209 } 210