Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/sun/jndi/ldap/LdapName/EmptyNameSearch.java
38867 views
/*1* Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 699756126* @summary A request for better error handling in JNDI27*/2829import java.net.Socket;30import java.net.ServerSocket;31import java.io.*;32import javax.naming.*;33import javax.naming.directory.*;34import javax.naming.ldap.*;35import java.util.Collections;36import java.util.Hashtable;3738public class EmptyNameSearch {3940public static void main(String[] args) throws Exception {4142// Start the LDAP server43Server s = new Server();44s.start();45Thread.sleep(3000);4647// Setup JNDI parameters48Hashtable env = new Hashtable();49env.put(Context.INITIAL_CONTEXT_FACTORY,50"com.sun.jndi.ldap.LdapCtxFactory");51env.put(Context.PROVIDER_URL, "ldap://localhost:" + s.getPortNumber());5253try {5455// Create initial context56System.out.println("Client: connecting...");57DirContext ctx = new InitialDirContext(env);5859System.out.println("Client: performing search...");60ctx.search(new LdapName(Collections.EMPTY_LIST), "cn=*", null);61ctx.close();6263// Exit64throw new RuntimeException();6566} catch (NamingException e) {67System.err.println("Passed: caught the expected Exception - " + e);6869} catch (Exception e) {70System.err.println("Failed: caught an unexpected Exception - " + e);71throw e;72}73}7475static class Server extends Thread {7677private int serverPort = 0;78private byte[] bindResponse = {790x30, 0x0C, 0x02, 0x01, 0x01, 0x61, 0x07, 0x0A,800x01, 0x00, 0x04, 0x00, 0x04, 0x0081};82private byte[] searchResponse = {830x30, 0x0C, 0x02, 0x01, 0x02, 0x65, 0x07, 0x0A,840x01, 0x02, 0x04, 0x00, 0x04, 0x0085};8687Server() {88}8990public int getPortNumber() {91return serverPort;92}9394public void run() {95try {96ServerSocket serverSock = new ServerSocket(0);97serverPort = serverSock.getLocalPort();98System.out.println("Server: listening on port " + serverPort);99100Socket socket = serverSock.accept();101System.out.println("Server: connection accepted");102103InputStream in = socket.getInputStream();104OutputStream out = socket.getOutputStream();105106// Read the LDAP BindRequest107System.out.println("Server: reading request...");108while (in.read() != -1) {109in.skip(in.available());110break;111}112113// Write an LDAP BindResponse114System.out.println("Server: writing response...");115out.write(bindResponse);116out.flush();117118// Read the LDAP SearchRequest119System.out.println("Server: reading request...");120while (in.read() != -1) {121in.skip(in.available());122break;123}124125// Write an LDAP SearchResponse126System.out.println("Server: writing response...");127out.write(searchResponse);128out.flush();129130in.close();131out.close();132socket.close();133serverSock.close();134135} catch (IOException e) {136// ignore137}138}139}140}141142143