1 /*
   2  * Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 8196770
  27  * @summary Verify capability to add a new entry to the directory using the
  28  *          ADD operation.
  29  * @modules java.naming/com.sun.jndi.ldap
  30  * @library ../../lib/ /javax/naming/module/src/test/test/
  31  * @build LDAPServer LDAPTestUtils
  32  * @run main/othervm AddNewEntry
  33  */
  34 
  35 import javax.naming.NamingEnumeration;
  36 import javax.naming.directory.Attribute;
  37 import javax.naming.directory.Attributes;
  38 import javax.naming.directory.BasicAttribute;
  39 import javax.naming.directory.BasicAttributes;
  40 import javax.naming.directory.DirContext;
  41 import javax.naming.directory.InitialDirContext;
  42 import javax.naming.directory.SearchControls;
  43 import javax.naming.directory.SearchResult;
  44 import java.net.ServerSocket;
  45 import java.util.Hashtable;
  46 
  47 public class AddNewEntry {
  48 
  49     public static void main(String[] args) throws Exception {
  50         ServerSocket serverSocket = new ServerSocket(0);
  51 
  52         Hashtable<Object, Object> env;
  53 
  54         // initialize test
  55         env = LDAPTestUtils
  56                 .initEnv(serverSocket, AddNewEntry.class.getName(), args, true);
  57 
  58         /* Build attribute set */
  59         String[] ids = { "objectClass", "sn", "cn", "telephoneNumber", "mail",
  60                 "description", "uid" };
  61         Attribute objectClass = new BasicAttribute(ids[0]);
  62         objectClass.add("top");
  63         objectClass.add("person");
  64         objectClass.add("organizationalPerson");
  65         objectClass.add("inetOrgPerson");
  66 
  67         Attribute sn = new BasicAttribute(ids[1], "Powers");
  68         Attribute cn = new BasicAttribute(ids[2],
  69                 "Austin \\\"Danger\\\" Powers");
  70         Attribute telephoneNumber = new BasicAttribute(ids[3], "+44 582 10101");
  71         Attribute mail = new BasicAttribute(ids[4], "secret_agent_man@imc.org");
  72         Attribute description = new BasicAttribute(ids[5], "Yea Baby!!");
  73         description.add("Behave!");
  74         Attribute uid = new BasicAttribute(ids[6], "secret_agent_man");
  75 
  76         Attributes attrs = new BasicAttributes();
  77         attrs.put(objectClass);
  78         attrs.put(sn);
  79         attrs.put(cn);
  80         attrs.put(telephoneNumber);
  81         attrs.put(mail);
  82         attrs.put(description);
  83         attrs.put(uid);
  84 
  85         DirContext ctx = null;
  86         String[] bases = new String[] { (String) env.get("client"),
  87                 (String) env.get("vendor"), "Add" };
  88         String baseDN = LDAPTestUtils.buildDN(bases, (String) env.get("root"));
  89         String entryDN = "cn=Austin Powers," + baseDN;
  90         String expect = ""; // relative name
  91 
  92         try {
  93             // connect to server
  94             ctx = new InitialDirContext(env);
  95 
  96             // add entry
  97             ctx.createSubcontext(entryDN, attrs);
  98 
  99             // specify base search
 100             SearchControls constraints = new SearchControls();
 101             constraints.setSearchScope(SearchControls.OBJECT_SCOPE);
 102 
 103             NamingEnumeration<SearchResult> results = ctx
 104                     .search(entryDN, "(objectclass=*)", constraints);
 105 
 106             int found = LDAPTestUtils.checkResult(results, expect);
 107 
 108             if (found != 1) {
 109                 throw new RuntimeException(
 110                         "Check result failed, expect found 1 but actual is "
 111                                 + found);
 112             }
 113 
 114         } finally {
 115             LDAPTestUtils.cleanupSubcontext(ctx, entryDN);
 116             LDAPTestUtils.cleanup(ctx);
 117         }
 118     }
 119 }