Path: blob/master/test/jdk/com/sun/jndi/ldap/blits/AddTests/AddNewEntry.java
41161 views
/*1* Copyright (c) 2018, 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 819677026* @summary Verify capability to add a new entry to the directory using the27* ADD operation.28* @modules java.naming/com.sun.jndi.ldap29* @library /test/lib ../../lib/ /javax/naming/module/src/test/test/30* @build LDAPServer LDAPTestUtils31* @run main/othervm AddNewEntry32*/3334import javax.naming.NamingEnumeration;35import javax.naming.directory.Attribute;36import javax.naming.directory.Attributes;37import javax.naming.directory.BasicAttribute;38import javax.naming.directory.BasicAttributes;39import javax.naming.directory.DirContext;40import javax.naming.directory.InitialDirContext;41import javax.naming.directory.SearchControls;42import javax.naming.directory.SearchResult;43import java.net.InetAddress;44import java.net.InetSocketAddress;45import java.net.ServerSocket;46import java.net.SocketAddress;47import java.util.Hashtable;48import jdk.test.lib.net.URIBuilder;4950public class AddNewEntry {5152public static void main(String[] args) throws Exception {53// Create unbound server socket54ServerSocket serverSocket = new ServerSocket();5556// Bind it to the loopback address57SocketAddress sockAddr = new InetSocketAddress(58InetAddress.getLoopbackAddress(), 0);59serverSocket.bind(sockAddr);6061// Construct the provider URL for LDAPTestUtils62String providerURL = URIBuilder.newBuilder()63.scheme("ldap")64.loopback()65.port(serverSocket.getLocalPort())66.buildUnchecked().toString();6768Hashtable<Object, Object> env;6970// initialize test71env = LDAPTestUtils.initEnv(serverSocket, providerURL,72AddNewEntry.class.getName(), args, true);7374/* Build attribute set */75String[] ids = { "objectClass", "sn", "cn", "telephoneNumber", "mail",76"description", "uid" };77Attribute objectClass = new BasicAttribute(ids[0]);78objectClass.add("top");79objectClass.add("person");80objectClass.add("organizationalPerson");81objectClass.add("inetOrgPerson");8283Attribute sn = new BasicAttribute(ids[1], "Powers");84Attribute cn = new BasicAttribute(ids[2],85"Austin \\\"Danger\\\" Powers");86Attribute telephoneNumber = new BasicAttribute(ids[3], "+44 582 10101");87Attribute mail = new BasicAttribute(ids[4], "[email protected]");88Attribute description = new BasicAttribute(ids[5], "Yea Baby!!");89description.add("Behave!");90Attribute uid = new BasicAttribute(ids[6], "secret_agent_man");9192Attributes attrs = new BasicAttributes();93attrs.put(objectClass);94attrs.put(sn);95attrs.put(cn);96attrs.put(telephoneNumber);97attrs.put(mail);98attrs.put(description);99attrs.put(uid);100101DirContext ctx = null;102String[] bases = new String[] { (String) env.get("client"),103(String) env.get("vendor"), "Add" };104String baseDN = LDAPTestUtils.buildDN(bases, (String) env.get("root"));105String entryDN = "cn=Austin Powers," + baseDN;106String expect = ""; // relative name107108try {109// connect to server110ctx = new InitialDirContext(env);111112// add entry113ctx.createSubcontext(entryDN, attrs);114115// specify base search116SearchControls constraints = new SearchControls();117constraints.setSearchScope(SearchControls.OBJECT_SCOPE);118119NamingEnumeration<SearchResult> results = ctx120.search(entryDN, "(objectclass=*)", constraints);121122int found = LDAPTestUtils.checkResult(results, expect);123124if (found != 1) {125throw new RuntimeException(126"Check result failed, expect found 1 but actual is "127+ found);128}129130} finally {131LDAPTestUtils.cleanupSubcontext(ctx, entryDN);132LDAPTestUtils.cleanup(ctx);133}134}135}136137138