| | |
| | | import org.opends.server.protocols.asn1.ASN1Reader; |
| | | import org.opends.server.protocols.asn1.ASN1Writer; |
| | | import org.opends.server.protocols.asn1.ASN1Exception; |
| | | import org.opends.server.protocols.asn1.ASN1Element; |
| | | import org.opends.server.protocols.ldap.*; |
| | | import org.opends.server.types.*; |
| | | import org.opends.server.TestCaseUtils; |
| | | import org.opends.server.util.StaticUtils; |
| | | import org.opends.server.controls.MatchedValuesFilter; |
| | | import org.opends.server.controls.MatchedValuesControl; |
| | | import org.opends.server.plugins.InvocationCounterPlugin; |
| | |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.LinkedHashSet; |
| | | import java.util.List; |
| | | import java.net.Socket; |
| | | import java.io.IOException; |
| | | |
| | | public class SearchOperationTestCase extends OperationTestCase |
| | | { |
| | | private Entry entry; |
| | | private static final String SUFFIX = "dc=example,dc=com"; |
| | | private static final String BASE = "o=Test Core Search,dc=example,dc=com"; |
| | | |
| | | private Entry testEntry; |
| | | private int ldapAttrCount; |
| | | |
| | | @BeforeClass |
| | | public void setUp() throws Exception |
| | | { |
| | | TestCaseUtils.startServer(); |
| | | TestCaseUtils.initializeTestBackend(true); |
| | | TestCaseUtils.clearJEBackend(true); |
| | | |
| | | InternalClientConnection connection = |
| | | InternalClientConnection.getRootConnection(); |
| | | |
| | | // Add the suffix entry. |
| | | DN suffixDN = DN.decode(SUFFIX); |
| | | if (DirectoryServer.getEntry(suffixDN) == null) |
| | | { |
| | | Entry suffixEntry = StaticUtils.createEntry(suffixDN); |
| | | AddOperation addOperation = |
| | | connection.processAdd(suffixEntry.getDN(), |
| | | suffixEntry.getObjectClasses(), |
| | | suffixEntry.getUserAttributes(), |
| | | suffixEntry.getOperationalAttributes()); |
| | | assertEquals(addOperation.getResultCode(), ResultCode.SUCCESS); |
| | | assertNotNull(DirectoryServer.getEntry(suffixEntry.getDN())); |
| | | } |
| | | |
| | | // Add a search base entry. |
| | | DN baseDN = DN.decode(BASE); |
| | | if (DirectoryServer.getEntry(baseDN) == null) |
| | | { |
| | | Entry baseEntry = StaticUtils.createEntry(baseDN); |
| | | AddOperation addOperation = |
| | | connection.processAdd(baseEntry.getDN(), |
| | | baseEntry.getObjectClasses(), |
| | | baseEntry.getUserAttributes(), |
| | | baseEntry.getOperationalAttributes()); |
| | | assertEquals(addOperation.getResultCode(), ResultCode.SUCCESS); |
| | | assertNotNull(DirectoryServer.getEntry(baseEntry.getDN())); |
| | | } |
| | | |
| | | // Add a test entry. |
| | | entry = TestCaseUtils.makeEntry( |
| | | "dn: uid=rogasawara,o=test", |
| | | testEntry = TestCaseUtils.makeEntry( |
| | | "dn: uid=rogasawara," + BASE, |
| | | "userpassword: password", |
| | | "objectclass: top", |
| | | "objectclass: person", |
| | |
| | | |
| | | // Calculate the total number of LDAP attributes in this entry. |
| | | ldapAttrCount = 1; // For the objectclass attribute. |
| | | for (Attribute a : entry.getAttributes()) |
| | | for (Attribute a : testEntry.getAttributes()) |
| | | { |
| | | ldapAttrCount += a.getValues().size(); |
| | | } |
| | | |
| | | // The add operation changes the attributes, so let's duplicate the entry. |
| | | Entry duplicateEntry = entry.duplicate(); |
| | | Entry duplicateEntry = testEntry.duplicate(); |
| | | |
| | | AddOperation addOperation = |
| | | connection.processAdd(duplicateEntry.getDN(), |
| | |
| | | duplicateEntry.getUserAttributes(), |
| | | duplicateEntry.getOperationalAttributes()); |
| | | assertEquals(addOperation.getResultCode(), ResultCode.SUCCESS); |
| | | assertNotNull(DirectoryServer.getEntry(entry.getDN())); |
| | | assertNotNull(DirectoryServer.getEntry(testEntry.getDN())); |
| | | |
| | | // Add a test ldapsubentry. |
| | | Entry ldapSubentry = TestCaseUtils.makeEntry( |
| | | "dn: cn=subentry,o=test", |
| | | "dn: cn=subentry," + BASE, |
| | | "objectclass: ldapsubentry"); |
| | | addOperation = |
| | | connection.processAdd(ldapSubentry.getDN(), |
| | |
| | | ldapSubentry.getOperationalAttributes()); |
| | | assertEquals(addOperation.getResultCode(), ResultCode.SUCCESS); |
| | | assertNotNull(DirectoryServer.getEntry(ldapSubentry.getDN())); |
| | | |
| | | // Add a test referral entry. |
| | | Entry referralEntry = TestCaseUtils.makeEntry( |
| | | "dn: ou=People," + BASE, |
| | | "objectclass: extensibleobject", |
| | | "objectclass: referral", |
| | | "ref: ldap://hostb/OU=People,O=MNN,C=US", |
| | | "ref: ldap://hostc/OU=People,O=MNN,C=US"); |
| | | addOperation = |
| | | connection.processAdd(referralEntry.getDN(), |
| | | referralEntry.getObjectClasses(), |
| | | referralEntry.getUserAttributes(), |
| | | referralEntry.getOperationalAttributes()); |
| | | assertEquals(addOperation.getResultCode(), ResultCode.SUCCESS); |
| | | assertNotNull(DirectoryServer.getEntry(referralEntry.getDN())); |
| | | } |
| | | |
| | | |
| | |
| | | InternalClientConnection.nextOperationID(), |
| | | InternalClientConnection.nextMessageID(), |
| | | new ArrayList<Control>(), |
| | | new ASN1OctetString("o=test"), |
| | | new ASN1OctetString(BASE), |
| | | SearchScope.WHOLE_SUBTREE, |
| | | DereferencePolicy.NEVER_DEREF_ALIASES, |
| | | -1, |
| | |
| | | message = new LDAPMessage(2, searchRequest, controls); |
| | | w.writeElement(message.encode()); |
| | | |
| | | message = LDAPMessage.decode(r.readElement().decodeAsSequence()); |
| | | SearchResultEntryProtocolOp searchResultEntry = |
| | | message.getSearchResultEntryProtocolOp(); |
| | | SearchResultEntryProtocolOp searchResultEntry = null; |
| | | SearchResultDoneProtocolOp searchResultDone = null; |
| | | ASN1Element element; |
| | | while (searchResultDone == null && (element = r.readElement()) != null) |
| | | { |
| | | message = LDAPMessage.decode(element.decodeAsSequence()); |
| | | switch (message.getProtocolOpType()) |
| | | { |
| | | case LDAPConstants.OP_TYPE_SEARCH_RESULT_ENTRY: |
| | | searchResultEntry = message.getSearchResultEntryProtocolOp(); |
| | | break; |
| | | |
| | | message = LDAPMessage.decode(r.readElement().decodeAsSequence()); |
| | | SearchResultDoneProtocolOp searchResultDone = |
| | | message.getSearchResultDoneProtocolOp(); |
| | | case LDAPConstants.OP_TYPE_SEARCH_RESULT_REFERENCE: |
| | | break; |
| | | |
| | | assertEquals(InvocationCounterPlugin.waitForPostResponse(), 1); |
| | | assertEquals(searchResultDone.getResultCode(), LDAPResultCode.SUCCESS); |
| | | case LDAPConstants.OP_TYPE_SEARCH_RESULT_DONE: |
| | | searchResultDone = message.getSearchResultDoneProtocolOp(); |
| | | assertEquals(searchResultDone.getResultCode(), |
| | | LDAPResultCode.SUCCESS); |
| | | assertEquals(InvocationCounterPlugin.waitForPostResponse(), 1); |
| | | break; |
| | | } |
| | | } |
| | | |
| | | return searchResultEntry; |
| | | } |
| | |
| | | InternalClientConnection.nextOperationID(), |
| | | InternalClientConnection.nextMessageID(), |
| | | new ArrayList<Control>(), |
| | | new ASN1OctetString("o=test"), |
| | | new ASN1OctetString(BASE), |
| | | SearchScope.WHOLE_SUBTREE, |
| | | DereferencePolicy.NEVER_DEREF_ALIASES, |
| | | Integer.MAX_VALUE, |
| | |
| | | searchOperation.run(); |
| | | assertEquals(searchOperation.getResultCode(), ResultCode.SUCCESS); |
| | | assertEquals(searchOperation.getEntriesSent(), 2); |
| | | assertEquals(searchOperation.getReferencesSent(), 1); |
| | | assertEquals(searchOperation.getErrorMessage().length(), 0); |
| | | |
| | | examineCompletedOperation(searchOperation); |
| | |
| | | InternalClientConnection.nextOperationID(), |
| | | InternalClientConnection.nextMessageID(), |
| | | new ArrayList<Control>(), |
| | | new ASN1OctetString("o=test"), |
| | | new ASN1OctetString(BASE), |
| | | SearchScope.WHOLE_SUBTREE, |
| | | DereferencePolicy.NEVER_DEREF_ALIASES, |
| | | Integer.MAX_VALUE, |
| | |
| | | |
| | | Entry resultEntry = searchInternalForSingleEntry(searchOperation); |
| | | |
| | | assertEquals(resultEntry.getObjectClasses(), entry.getObjectClasses()); |
| | | assertEquals(resultEntry.getObjectClasses(), testEntry.getObjectClasses()); |
| | | assertEquals(resultEntry.getUserAttributes().size(), |
| | | entry.getUserAttributes().size()); |
| | | testEntry.getUserAttributes().size()); |
| | | assertEquals(resultEntry.getOperationalAttributes().size(), 0); |
| | | } |
| | | |
| | |
| | | InternalClientConnection.nextOperationID(), |
| | | InternalClientConnection.nextMessageID(), |
| | | new ArrayList<Control>(), |
| | | new ASN1OctetString("o=test"), |
| | | new ASN1OctetString(BASE), |
| | | SearchScope.WHOLE_SUBTREE, |
| | | DereferencePolicy.NEVER_DEREF_ALIASES, |
| | | Integer.MAX_VALUE, |
| | |
| | | |
| | | assertEquals(resultEntry.getObjectClasses().size(), 0); |
| | | assertEquals(resultEntry.getUserAttributes().size(), |
| | | entry.getUserAttributes().size() + 1); |
| | | testEntry.getUserAttributes().size() + 1); |
| | | assertEquals(resultEntry.getOperationalAttributes().size(), 0); |
| | | } |
| | | |
| | |
| | | InternalClientConnection.nextOperationID(), |
| | | InternalClientConnection.nextMessageID(), |
| | | new ArrayList<Control>(), |
| | | new ASN1OctetString("o=test"), |
| | | new ASN1OctetString(BASE), |
| | | SearchScope.WHOLE_SUBTREE, |
| | | DereferencePolicy.NEVER_DEREF_ALIASES, |
| | | Integer.MAX_VALUE, |
| | |
| | | InternalClientConnection.nextOperationID(), |
| | | InternalClientConnection.nextMessageID(), |
| | | new ArrayList<Control>(), |
| | | new ASN1OctetString("o=test"), |
| | | new ASN1OctetString(BASE), |
| | | SearchScope.WHOLE_SUBTREE, |
| | | DereferencePolicy.NEVER_DEREF_ALIASES, |
| | | Integer.MAX_VALUE, |
| | |
| | | |
| | | Entry resultEntry = searchInternalForSingleEntry(searchOperation); |
| | | |
| | | assertEquals(resultEntry.getObjectClasses(), entry.getObjectClasses()); |
| | | assertEquals(resultEntry.getObjectClasses(), testEntry.getObjectClasses()); |
| | | assertTrue(resultEntry.getOperationalAttributes().size() > 0); |
| | | assertEquals(resultEntry.getUserAttributes().size(), |
| | | entry.getUserAttributes().size()); |
| | | testEntry.getUserAttributes().size()); |
| | | } |
| | | |
| | | @Test |
| | |
| | | InternalClientConnection.nextOperationID(), |
| | | InternalClientConnection.nextMessageID(), |
| | | new ArrayList<Control>(), |
| | | new ASN1OctetString("o=test"), |
| | | new ASN1OctetString(BASE), |
| | | SearchScope.WHOLE_SUBTREE, |
| | | DereferencePolicy.NEVER_DEREF_ALIASES, |
| | | Integer.MAX_VALUE, |
| | |
| | | |
| | | Entry resultEntry = searchInternalForSingleEntry(searchOperation); |
| | | |
| | | assertEquals(resultEntry.getObjectClasses(), entry.getObjectClasses()); |
| | | assertEquals(resultEntry.getObjectClasses(), testEntry.getObjectClasses()); |
| | | assertEquals(resultEntry.getUserAttributes().size(), |
| | | entry.getUserAttributes().size()); |
| | | testEntry.getUserAttributes().size()); |
| | | assertEquals(resultEntry.getOperationalAttributes().size(), 1); |
| | | } |
| | | |
| | |
| | | InternalClientConnection.nextOperationID(), |
| | | InternalClientConnection.nextMessageID(), |
| | | new ArrayList<Control>(), |
| | | new ASN1OctetString("o=test"), |
| | | new ASN1OctetString(BASE), |
| | | SearchScope.WHOLE_SUBTREE, |
| | | DereferencePolicy.NEVER_DEREF_ALIASES, |
| | | Integer.MAX_VALUE, |
| | |
| | | { |
| | | SearchRequestProtocolOp searchRequest = |
| | | new SearchRequestProtocolOp( |
| | | new ASN1OctetString("o=test"), |
| | | new ASN1OctetString(BASE), |
| | | SearchScope.WHOLE_SUBTREE, |
| | | DereferencePolicy.NEVER_DEREF_ALIASES, |
| | | Integer.MAX_VALUE, |
| | |
| | | attributes.add("*"); |
| | | SearchRequestProtocolOp searchRequest = |
| | | new SearchRequestProtocolOp( |
| | | new ASN1OctetString("o=test"), |
| | | new ASN1OctetString(BASE), |
| | | SearchScope.WHOLE_SUBTREE, |
| | | DereferencePolicy.NEVER_DEREF_ALIASES, |
| | | Integer.MAX_VALUE, |
| | |
| | | { |
| | | SearchRequestProtocolOp searchRequest = |
| | | new SearchRequestProtocolOp( |
| | | new ASN1OctetString("o=test"), |
| | | new ASN1OctetString(BASE), |
| | | SearchScope.WHOLE_SUBTREE, |
| | | DereferencePolicy.NEVER_DEREF_ALIASES, |
| | | Integer.MAX_VALUE, |
| | |
| | | attributes.add("*"); |
| | | SearchRequestProtocolOp searchRequest = |
| | | new SearchRequestProtocolOp( |
| | | new ASN1OctetString("o=test"), |
| | | new ASN1OctetString(BASE), |
| | | SearchScope.WHOLE_SUBTREE, |
| | | DereferencePolicy.NEVER_DEREF_ALIASES, |
| | | Integer.MAX_VALUE, |
| | |
| | | attributes.add("objectclass"); |
| | | SearchRequestProtocolOp searchRequest = |
| | | new SearchRequestProtocolOp( |
| | | new ASN1OctetString("o=test"), |
| | | new ASN1OctetString(BASE), |
| | | SearchScope.WHOLE_SUBTREE, |
| | | DereferencePolicy.NEVER_DEREF_ALIASES, |
| | | Integer.MAX_VALUE, |
| | |
| | | attributes.add("objectclass"); |
| | | SearchRequestProtocolOp searchRequest = |
| | | new SearchRequestProtocolOp( |
| | | new ASN1OctetString("o=test"), |
| | | new ASN1OctetString(BASE), |
| | | SearchScope.WHOLE_SUBTREE, |
| | | DereferencePolicy.NEVER_DEREF_ALIASES, |
| | | Integer.MAX_VALUE, |
| | |
| | | attributes.add("createtimestamp"); |
| | | SearchRequestProtocolOp searchRequest = |
| | | new SearchRequestProtocolOp( |
| | | new ASN1OctetString("o=test"), |
| | | new ASN1OctetString(BASE), |
| | | SearchScope.WHOLE_SUBTREE, |
| | | DereferencePolicy.NEVER_DEREF_ALIASES, |
| | | Integer.MAX_VALUE, |
| | |
| | | attributes.add("title"); |
| | | SearchRequestProtocolOp searchRequest = |
| | | new SearchRequestProtocolOp( |
| | | new ASN1OctetString("o=test"), |
| | | new ASN1OctetString(BASE), |
| | | SearchScope.WHOLE_SUBTREE, |
| | | DereferencePolicy.NEVER_DEREF_ALIASES, |
| | | Integer.MAX_VALUE, |
| | |
| | | attributes.add("title"); |
| | | SearchRequestProtocolOp searchRequest = |
| | | new SearchRequestProtocolOp( |
| | | new ASN1OctetString("o=test"), |
| | | new ASN1OctetString(BASE), |
| | | SearchScope.WHOLE_SUBTREE, |
| | | DereferencePolicy.NEVER_DEREF_ALIASES, |
| | | Integer.MAX_VALUE, |
| | |
| | | attributes.add("title;lang-ja;phonetic"); |
| | | SearchRequestProtocolOp searchRequest = |
| | | new SearchRequestProtocolOp( |
| | | new ASN1OctetString("o=test"), |
| | | new ASN1OctetString(BASE), |
| | | SearchScope.WHOLE_SUBTREE, |
| | | DereferencePolicy.NEVER_DEREF_ALIASES, |
| | | Integer.MAX_VALUE, |
| | |
| | | |
| | | SearchRequestProtocolOp searchRequest = |
| | | new SearchRequestProtocolOp( |
| | | new ASN1OctetString("o=test"), |
| | | new ASN1OctetString(BASE), |
| | | SearchScope.WHOLE_SUBTREE, |
| | | DereferencePolicy.NEVER_DEREF_ALIASES, |
| | | Integer.MAX_VALUE, |
| | |
| | | assertEquals(valueCount, 1); |
| | | } |
| | | |
| | | @Test |
| | | public void testSearchInternalReferences() throws Exception |
| | | { |
| | | InvocationCounterPlugin.resetAllCounters(); |
| | | |
| | | InternalClientConnection conn = |
| | | InternalClientConnection.getRootConnection(); |
| | | |
| | | InternalSearchOperation searchOperation = |
| | | new InternalSearchOperation( |
| | | conn, |
| | | InternalClientConnection.nextOperationID(), |
| | | InternalClientConnection.nextMessageID(), |
| | | new ArrayList<Control>(), |
| | | new ASN1OctetString(BASE), |
| | | SearchScope.WHOLE_SUBTREE, |
| | | DereferencePolicy.NEVER_DEREF_ALIASES, |
| | | Integer.MAX_VALUE, |
| | | Integer.MAX_VALUE, |
| | | false, |
| | | LDAPFilter.decode("(objectclass=inetorgperson)"), |
| | | null, null); |
| | | |
| | | searchOperation.run(); |
| | | |
| | | assertEquals(searchOperation.getResultCode(), ResultCode.SUCCESS); |
| | | assertEquals(searchOperation.getReferencesSent(), 1); |
| | | assertEquals(searchOperation.getErrorMessage().length(), 0); |
| | | examineCompletedOperation(searchOperation); |
| | | |
| | | List<SearchResultReference> references = |
| | | searchOperation.getSearchReferences(); |
| | | assertEquals(references.size(), 1); |
| | | |
| | | List<String> referrals = references.get(0).getReferralURLs(); |
| | | assertEquals(referrals.size(), 2); |
| | | } |
| | | } |