Path: blob/master/test/jdk/com/sun/jndi/ldap/LdapName/EmptyNameSearch.java
41155 views
/*1* Copyright (c) 2011, 2020, 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* @library ../lib/ /test/lib28*/2930import java.net.Socket;31import java.io.*;32import javax.naming.*;33import javax.naming.directory.*;34import javax.naming.ldap.*;35import java.util.Collections;36import java.util.Hashtable;37import jdk.test.lib.net.URIBuilder;3839public class EmptyNameSearch {4041private static final byte[] bindResponse = {420x30, 0x0C, 0x02, 0x01, 0x01, 0x61, 0x07, 0x0A,430x01, 0x00, 0x04, 0x00, 0x04, 0x0044};45private static final byte[] searchResponse = {460x30, 0x0C, 0x02, 0x01, 0x02, 0x65, 0x07, 0x0A,470x01, 0x02, 0x04, 0x00, 0x04, 0x0048};4950public static void main(String[] args) throws Exception {5152// Start the LDAP server53var ldapServer = new BaseLdapServer() {54@Override55protected void handleRequest(Socket socket, LdapMessage request,56OutputStream out) throws IOException {57switch (request.getOperation()) {58case BIND_REQUEST:59out.write(bindResponse);60break;61case SEARCH_REQUEST:62out.write(searchResponse);63break;64default:65break;66}67}68}.start();69Thread.sleep(3000);7071// Setup JNDI parameters72Hashtable<Object, Object> env = new Hashtable<>();73env.put(Context.INITIAL_CONTEXT_FACTORY,74"com.sun.jndi.ldap.LdapCtxFactory");75env.put(Context.PROVIDER_URL, URIBuilder.newBuilder()76.scheme("ldap")77.loopback()78.port(ldapServer.getPort())79.build().toString());8081try (ldapServer) {8283// Create initial context84System.out.println("Client: connecting...");85DirContext ctx = new InitialDirContext(env);8687System.out.println("Client: performing search...");88ctx.search(new LdapName(Collections.emptyList()), "cn=*", null);89ctx.close();9091// Exit92throw new RuntimeException();9394} catch (NamingException e) {95System.err.println("Passed: caught the expected Exception - " + e);9697} catch (Exception e) {98System.err.println("Failed: caught an unexpected Exception - " + e);99throw e;100}101}102}103104105