EmptyNameSearch.java revision 3490:78fe1b7a9a1a
1205218Srdivacky/* 2205218Srdivacky * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. 3205218Srdivacky * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4205218Srdivacky * 5205218Srdivacky * This code is free software; you can redistribute it and/or modify it 6205218Srdivacky * under the terms of the GNU General Public License version 2 only, as 7205218Srdivacky * published by the Free Software Foundation. 8205218Srdivacky * 9205218Srdivacky * This code is distributed in the hope that it will be useful, but WITHOUT 10205218Srdivacky * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11205218Srdivacky * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12205218Srdivacky * version 2 for more details (a copy is included in the LICENSE file that 13205218Srdivacky * accompanied this code). 14205218Srdivacky * 15205218Srdivacky * You should have received a copy of the GNU General Public License version 16205218Srdivacky * 2 along with this work; if not, write to the Free Software Foundation, 17205218Srdivacky * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18205218Srdivacky * 19205218Srdivacky * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20205218Srdivacky * or visit www.oracle.com if you need additional information or have any 21205218Srdivacky * questions. 22205218Srdivacky */ 23205218Srdivacky 24205218Srdivacky/** 25205218Srdivacky * @test 26205218Srdivacky * @bug 6997561 27205218Srdivacky * @summary A request for better error handling in JNDI 28205218Srdivacky */ 29205218Srdivacky 30205218Srdivackyimport java.net.Socket; 31205218Srdivackyimport java.net.ServerSocket; 32205218Srdivackyimport java.io.*; 33205218Srdivackyimport javax.naming.*; 34205218Srdivackyimport javax.naming.directory.*; 35205218Srdivackyimport javax.naming.ldap.*; 36205218Srdivackyimport java.util.Collections; 37205218Srdivackyimport java.util.Hashtable; 38205218Srdivacky 39205218Srdivackypublic class EmptyNameSearch { 40205218Srdivacky 41205218Srdivacky public static void main(String[] args) throws Exception { 42205218Srdivacky 43205218Srdivacky // Start the LDAP server 44205218Srdivacky Server s = new Server(); 45205218Srdivacky s.start(); 46205218Srdivacky Thread.sleep(3000); 47205218Srdivacky 48205218Srdivacky // Setup JNDI parameters 49205218Srdivacky Hashtable env = new Hashtable(); 50206083Srdivacky env.put(Context.INITIAL_CONTEXT_FACTORY, 51205218Srdivacky "com.sun.jndi.ldap.LdapCtxFactory"); 52205218Srdivacky env.put(Context.PROVIDER_URL, "ldap://localhost:" + s.getPortNumber()); 53205218Srdivacky 54206083Srdivacky try { 55206083Srdivacky 56205218Srdivacky // Create initial context 57205218Srdivacky System.out.println("Client: connecting..."); 58205218Srdivacky DirContext ctx = new InitialDirContext(env); 59205218Srdivacky 60205218Srdivacky System.out.println("Client: performing search..."); 61205218Srdivacky ctx.search(new LdapName(Collections.EMPTY_LIST), "cn=*", null); 62205218Srdivacky ctx.close(); 63206083Srdivacky 64205218Srdivacky // Exit 65205218Srdivacky throw new RuntimeException(); 66205218Srdivacky 67205218Srdivacky } catch (NamingException e) { 68205218Srdivacky System.err.println("Passed: caught the expected Exception - " + e); 69205218Srdivacky 70206083Srdivacky } catch (Exception e) { 71205218Srdivacky System.err.println("Failed: caught an unexpected Exception - " + e); 72205218Srdivacky throw e; 73205218Srdivacky } 74205218Srdivacky } 75205218Srdivacky 76205218Srdivacky static class Server extends Thread { 77205218Srdivacky 78205218Srdivacky private int serverPort = 0; 79205218Srdivacky private byte[] bindResponse = { 80205218Srdivacky 0x30, 0x0C, 0x02, 0x01, 0x01, 0x61, 0x07, 0x0A, 81205218Srdivacky 0x01, 0x00, 0x04, 0x00, 0x04, 0x00 82205218Srdivacky }; 83205218Srdivacky private byte[] searchResponse = { 84205218Srdivacky 0x30, 0x0C, 0x02, 0x01, 0x02, 0x65, 0x07, 0x0A, 85205218Srdivacky 0x01, 0x02, 0x04, 0x00, 0x04, 0x00 86205218Srdivacky }; 87205218Srdivacky 88205218Srdivacky Server() { 89205218Srdivacky } 90205218Srdivacky 91205218Srdivacky public int getPortNumber() { 92205218Srdivacky return serverPort; 93205218Srdivacky } 94205218Srdivacky 95205218Srdivacky public void run() { 96205218Srdivacky try { 97205218Srdivacky ServerSocket serverSock = new ServerSocket(0); 98205218Srdivacky serverPort = serverSock.getLocalPort(); 99205218Srdivacky System.out.println("Server: listening on port " + serverPort); 100205218Srdivacky 101205218Srdivacky Socket socket = serverSock.accept(); 102206083Srdivacky System.out.println("Server: connection accepted"); 103206083Srdivacky 104206083Srdivacky InputStream in = socket.getInputStream(); 105206083Srdivacky OutputStream out = socket.getOutputStream(); 106206083Srdivacky 107206083Srdivacky // Read the LDAP BindRequest 108205218Srdivacky System.out.println("Server: reading request..."); 109205218Srdivacky while (in.read() != -1) { 110205218Srdivacky in.skip(in.available()); 111205218Srdivacky break; 112205218Srdivacky } 113 114 // Write an LDAP BindResponse 115 System.out.println("Server: writing response..."); 116 out.write(bindResponse); 117 out.flush(); 118 119 // Read the LDAP SearchRequest 120 System.out.println("Server: reading request..."); 121 while (in.read() != -1) { 122 in.skip(in.available()); 123 break; 124 } 125 126 // Write an LDAP SearchResponse 127 System.out.println("Server: writing response..."); 128 out.write(searchResponse); 129 out.flush(); 130 131 in.close(); 132 out.close(); 133 socket.close(); 134 serverSock.close(); 135 136 } catch (IOException e) { 137 // ignore 138 } 139 } 140 } 141} 142