1 /*
   2  * Copyright (c) 2018, 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 java.net.ServerSocket;
  44 import java.util.Hashtable;
  45 
  46 public class AddNewEntry {
  47 
  48     public static void main(String[] args) throws Exception {
  49         ServerSocket serverSocket = new ServerSocket(0);
  50 
  51         Hashtable<Object, Object> env;
  52 
  53         // initialize test
  54         env = LDAPTestUtils
  55                 .initEnv(serverSocket, AddNewEntry.class.getName(), args, true);
  56 
  57         /* Build attribute set */
  58         String[] ids = { "objectClass", "sn", "cn", "telephoneNumber", "mail",
  59                 "description", "uid" };
  60         Attribute objectClass = new BasicAttribute(ids[0]);
  61         objectClass.add("top");
  62         objectClass.add("person");
  63         objectClass.add("organizationalPerson");
  64         objectClass.add("inetOrgPerson");
  65 
  66         Attribute sn = new BasicAttribute(ids[1], "Powers");
  67         Attribute cn = new BasicAttribute(ids[2],
  68                 "Austin \\\"Danger\\\" Powers");
  69         Attribute telephoneNumber = new BasicAttribute(ids[3], "+44 582 10101");
  70         Attribute mail = new BasicAttribute(ids[4], "secret_agent_man@imc.org");
  71         Attribute description = new BasicAttribute(ids[5], "Yea Baby!!");
  72         description.add("Behave!");
  73         Attribute uid = new BasicAttribute(ids[6], "secret_agent_man");
  74 
  75         Attributes attrs = new BasicAttributes();
  76         attrs.put(objectClass);
  77         attrs.put(sn);
  78         attrs.put(cn);
  79         attrs.put(telephoneNumber);
  80         attrs.put(mail);
  81         attrs.put(description);
  82         attrs.put(uid);
  83 
  84         DirContext ctx = null;
  85         String[] bases = new String[] { (String) env.get("client"),
  86                 (String) env.get("vendor"), "Add" };
  87         String baseDN = LDAPTestUtils.buildDN(bases, (String) env.get("root"));
  88         String entryDN = "cn=Austin Powers," + baseDN;
  89         String expect = ""; // relative name
  90 
  91         try {
  92             // connect to server
  93             ctx = new InitialDirContext(env);
  94 
  95             // add entry
  96             ctx.createSubcontext(entryDN, attrs);
  97 
  98             // specify base search
  99             SearchControls constraints = new SearchControls();
 100             constraints.setSearchScope(SearchControls.OBJECT_SCOPE);
 101 
 102             NamingEnumeration results = ctx
 103                     .search(entryDN, "(objectclass=*)", constraints);
 104 
 105             int found = LDAPTestUtils.checkResult(results, expect);
 106 
 107             if (found != 1) {
 108                 throw new RuntimeException(
 109                         "Check result failed, expect found 1 but actual is "
 110                                 + found);
 111             }
 112 
 113         } finally {
 114             LDAPTestUtils.cleanupSubcontext(ctx, entryDN);
 115             LDAPTestUtils.cleanup(ctx);
 116         }
 117     }
 118 }