3490N/A/*
3490N/A * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
3490N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3490N/A *
3490N/A * This code is free software; you can redistribute it and/or modify it
3490N/A * under the terms of the GNU General Public License version 2 only, as
3490N/A * published by the Free Software Foundation.
3490N/A *
3490N/A * This code is distributed in the hope that it will be useful, but WITHOUT
3490N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3490N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
3490N/A * version 2 for more details (a copy is included in the LICENSE file that
3490N/A * accompanied this code).
3490N/A *
3490N/A * You should have received a copy of the GNU General Public License version
3490N/A * 2 along with this work; if not, write to the Free Software Foundation,
3490N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3490N/A *
3490N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
3490N/A * or visit www.oracle.com if you need additional information or have any
3490N/A * questions.
3490N/A */
3490N/A
3485N/A/**
3485N/A * @test
3485N/A * @bug 6997561
3485N/A * @summary A request for better error handling in JNDI
3485N/A */
3485N/A
3485N/Aimport java.net.Socket;
3485N/Aimport java.net.ServerSocket;
3485N/Aimport java.io.*;
3485N/Aimport javax.naming.*;
3485N/Aimport javax.naming.directory.*;
3485N/Aimport javax.naming.ldap.*;
3485N/Aimport java.util.Collections;
3485N/Aimport java.util.Hashtable;
3485N/A
3485N/Apublic class EmptyNameSearch {
3485N/A
3485N/A public static void main(String[] args) throws Exception {
3485N/A
3485N/A // Start the LDAP server
3485N/A Server s = new Server();
3485N/A s.start();
3485N/A Thread.sleep(3000);
3485N/A
3485N/A // Setup JNDI parameters
3485N/A Hashtable env = new Hashtable();
3485N/A env.put(Context.INITIAL_CONTEXT_FACTORY,
3485N/A "com.sun.jndi.ldap.LdapCtxFactory");
3485N/A env.put(Context.PROVIDER_URL, "ldap://localhost:" + s.getPortNumber());
3485N/A
3485N/A try {
3485N/A
3485N/A // Create initial context
3485N/A System.out.println("Client: connecting...");
3485N/A DirContext ctx = new InitialDirContext(env);
3485N/A
3485N/A System.out.println("Client: performing search...");
3485N/A ctx.search(new LdapName(Collections.EMPTY_LIST), "cn=*", null);
3485N/A ctx.close();
3485N/A
3485N/A // Exit
3485N/A throw new RuntimeException();
3485N/A
3485N/A } catch (NamingException e) {
3485N/A System.err.println("Passed: caught the expected Exception - " + e);
3485N/A
3485N/A } catch (Exception e) {
3485N/A System.err.println("Failed: caught an unexpected Exception - " + e);
3485N/A throw e;
3485N/A }
3485N/A }
3485N/A
3485N/A static class Server extends Thread {
3485N/A
3485N/A private int serverPort = 0;
3485N/A private byte[] bindResponse = {
3485N/A 0x30, 0x0C, 0x02, 0x01, 0x01, 0x61, 0x07, 0x0A,
3485N/A 0x01, 0x00, 0x04, 0x00, 0x04, 0x00
3485N/A };
3485N/A private byte[] searchResponse = {
3485N/A 0x30, 0x0C, 0x02, 0x01, 0x02, 0x65, 0x07, 0x0A,
3485N/A 0x01, 0x02, 0x04, 0x00, 0x04, 0x00
3485N/A };
3485N/A
3485N/A Server() {
3485N/A }
3485N/A
3485N/A public int getPortNumber() {
3485N/A return serverPort;
3485N/A }
3485N/A
3485N/A public void run() {
3485N/A try {
3485N/A ServerSocket serverSock = new ServerSocket(0);
3485N/A serverPort = serverSock.getLocalPort();
3485N/A System.out.println("Server: listening on port " + serverPort);
3485N/A
3485N/A Socket socket = serverSock.accept();
3485N/A System.out.println("Server: connection accepted");
3485N/A
3485N/A InputStream in = socket.getInputStream();
3485N/A OutputStream out = socket.getOutputStream();
3485N/A
3485N/A // Read the LDAP BindRequest
3485N/A System.out.println("Server: reading request...");
3485N/A while (in.read() != -1) {
3485N/A in.skip(in.available());
3485N/A break;
3485N/A }
3485N/A
3485N/A // Write an LDAP BindResponse
3485N/A System.out.println("Server: writing response...");
3485N/A out.write(bindResponse);
3485N/A out.flush();
3485N/A
3485N/A // Read the LDAP SearchRequest
3485N/A System.out.println("Server: reading request...");
3485N/A while (in.read() != -1) {
3485N/A in.skip(in.available());
3485N/A break;
3485N/A }
3485N/A
3485N/A // Write an LDAP SearchResponse
3485N/A System.out.println("Server: writing response...");
3485N/A out.write(searchResponse);
3485N/A out.flush();
3485N/A
3485N/A in.close();
3485N/A out.close();
3485N/A socket.close();
3485N/A serverSock.close();
3485N/A
3485N/A } catch (IOException e) {
3485N/A // ignore
3485N/A }
3485N/A }
3485N/A }
3485N/A}