private NamingEnumeration<SearchResult> mockSearchResults(String password) throws NamingException { @SuppressWarnings("unchecked") NamingEnumeration<SearchResult> searchResults = EasyMock.createNiceMock(NamingEnumeration.class); EasyMock.expect(Boolean.valueOf(searchResults.hasMore())) .andReturn(Boolean.TRUE) .andReturn(Boolean.FALSE) .andReturn(Boolean.TRUE) .andReturn(Boolean.FALSE); EasyMock.expect(searchResults.next()) .andReturn(new SearchResult("ANY RESULT", "", new BasicAttributes(USER_PASSWORD_ATTR, password))) .times(2); EasyMock.replay(searchResults); return searchResults; }
/** * Converts an {@link Entry} to an {@link Attributes}. * * @param entry * the {@link Entry} to convert * @return * the equivalent {@link Attributes} */ public static Attributes toAttributes( Entry entry ) { if ( entry != null ) { Attributes attributes = new BasicAttributes( true ); // Looping on attributes for ( Iterator<Attribute> attributeIterator = entry.iterator(); attributeIterator.hasNext(); ) { Attribute entryAttribute = attributeIterator.next(); attributes.put( toJndiAttribute( entryAttribute ) ); } return attributes; } return null; }
/** * Test a conversion of an attributes from a LDIF file * @throws org.apache.directory.api.ldap.model.ldif.LdapLdifException */ @Test public void testConvertAttributesfromLdif() throws LdapException, LdapLdifException { Attributes attributes = new BasicAttributes( true ); Attribute oc = new BasicAttribute( "objectclass" ); oc.add( "top" ); oc.add( "person" ); oc.add( "inetorgPerson" ); attributes.put( oc ); attributes.put( "cn", "Saarbrucken" ); attributes.put( "sn", "test" ); String ldif = LdifUtils.convertToLdif( attributes, ( Dn ) null, 15 ); Attributes result = LdifUtils.getJndiAttributesFromLdif( ldif ); assertEquals( attributes, result ); }
/** * Create the Trigger execution subentry * * @param apCtx The administration point context * @param subentryCN The CN used by the suentry * @param subtreeSpec The subtree specification * @param prescriptiveTriggerSpec The prescriptive trigger specification * @throws NamingException If the operation failed */ public static void createTriggerExecutionSubentry( LdapContext apCtx, String subentryCN, String subtreeSpec, String prescriptiveTriggerSpec ) throws NamingException { Attributes subentry = new BasicAttributes( SchemaConstants.CN_AT, subentryCN, true ); Attribute objectClass = new BasicAttribute( SchemaConstants.OBJECT_CLASS_AT ); subentry.put( objectClass ); objectClass.add( SchemaConstants.TOP_OC ); objectClass.add( SchemaConstants.SUBENTRY_OC ); objectClass.add( SchemaConstants.TRIGGER_EXECUTION_SUBENTRY_OC ); subentry.put( SchemaConstants.SUBTREE_SPECIFICATION_AT, subtreeSpec ); subentry.put( SchemaConstants.PRESCRIPTIVE_TRIGGER_SPECIFICATION_AT, prescriptiveTriggerSpec ); apCtx.createSubcontext( "cn=" + subentryCN, subentry ); }
boolean compareToSearchResult(String name) { boolean successful = false; switch (status) { case LdapClient.LDAP_COMPARE_TRUE: status = LdapClient.LDAP_SUCCESS; entries = new Vector<>(1,1); Attributes attrs = new BasicAttributes(LdapClient.caseIgnore); LdapEntry entry = new LdapEntry( name, attrs ); entries.addElement(entry); successful = true; break; case LdapClient.LDAP_COMPARE_FALSE: status = LdapClient.LDAP_SUCCESS; entries = new Vector<>(0); successful = true; break; default: successful = false; break; } return successful; }
public static Context getURLContext( String scheme, Hashtable<?,?> environment) throws NamingException { return new DnsContext("", null, new Hashtable<String,String>()) { public Attributes getAttributes(String name, String[] attrIds) throws NamingException { return new BasicAttributes() { public Attribute get(String attrID) { BasicAttribute ba = new BasicAttribute(attrID); ba.add("1 1 99 b.com."); ba.add("0 0 88 a.com."); // 2nd has higher priority return ba; } }; } }; }
public static Context getURLContext( String scheme, Hashtable<?,?> environment) throws NamingException { return new InitialDirContext() { public Attributes getAttributes(String name, String[] attrIds) throws NamingException { return new BasicAttributes() { public Attribute get(String attrID) { BasicAttribute ba = new BasicAttribute(attrID); ba.add("1 1 99 b.com."); ba.add("0 0 88 a.com."); // 2nd has higher priority return ba; } }; } }; }
@Test public void authenticate_shouldReturnAuthenticationResponseWithAuthConfigOnSuccessfulAuthentication() throws Exception { final UserMapper userMapper = mock(UserMapper.class); final AuthConfig validAuthConfig = mock(AuthConfig.class); final LdapConfiguration validLdapConfiguration = mock(LdapConfiguration.class); Attributes attributes = new BasicAttributes(); when(validAuthConfig.getConfiguration()).thenReturn(validLdapConfiguration); when(ldapFactory.ldapForConfiguration(validAuthConfig.getConfiguration())).thenReturn(ldap); when(ldap.authenticate(eq(credentials.getUsername()), eq(credentials.getPassword()), any(AbstractMapper.class))).thenThrow(new RuntimeException()).thenReturn(attributes); when(validLdapConfiguration.getUserMapper(new UsernameResolver(credentials.getUsername()))).thenReturn(userMapper); when(userMapper.mapFromResult(attributes)).thenReturn(mock(User.class)); final AuthenticationResponse authenticationResponse = ldapAuthenticator.authenticate(credentials, Arrays.asList(this.authConfig, validAuthConfig)); assertThat(authenticationResponse.getConfigUsedForAuthentication(), is(validAuthConfig)); }
/** * Collect all the value from the table (Arguments), using this create the * basicAttributes. This will create the Basic Attributes for the User * defined TestCase for Add Test. * * @return the BasicAttributes */ private BasicAttributes getUserAttributes() { BasicAttribute basicattribute = new BasicAttribute("objectclass"); //$NON-NLS-1$ basicattribute.add("top"); //$NON-NLS-1$ basicattribute.add("person"); //$NON-NLS-1$ basicattribute.add("organizationalPerson"); //$NON-NLS-1$ basicattribute.add("inetOrgPerson"); //$NON-NLS-1$ BasicAttributes attrs = new BasicAttributes(true); attrs.put(basicattribute); BasicAttribute attr; for (JMeterProperty jMeterProperty : getArguments()) { Argument item = (Argument) jMeterProperty.getObjectValue(); attr = getBasicAttribute(item.getName(), item.getValue()); attrs.put(attr); } return attrs; }
/** * This will create the Basic Attributes for the In build TestCase for Add * Test. * * @return the BasicAttributes */ private BasicAttributes getBasicAttributes() { BasicAttributes basicattributes = new BasicAttributes(); BasicAttribute basicattribute = new BasicAttribute("objectclass"); //$NON-NLS-1$ basicattribute.add("top"); //$NON-NLS-1$ basicattribute.add("person"); //$NON-NLS-1$ basicattribute.add("organizationalPerson"); //$NON-NLS-1$ basicattribute.add("inetOrgPerson"); //$NON-NLS-1$ basicattributes.put(basicattribute); String s1 = "User"; //$NON-NLS-1$ String s3 = "Test"; //$NON-NLS-1$ String s5 = "user"; //$NON-NLS-1$ String s6 = "test"; //$NON-NLS-1$ counter += 1; basicattributes.put(new BasicAttribute("givenname", s1)); //$NON-NLS-1$ basicattributes.put(new BasicAttribute("sn", s3)); //$NON-NLS-1$ basicattributes.put(new BasicAttribute("cn", "TestUser" + counter)); //$NON-NLS-1$ //$NON-NLS-2$ basicattributes.put(new BasicAttribute("uid", s5)); //$NON-NLS-1$ basicattributes.put(new BasicAttribute("userpassword", s6)); //$NON-NLS-1$ setProperty(new StringProperty(ADD, "cn=TestUser" + counter)); //$NON-NLS-1$ return basicattributes; }
/*************************************************************************** * Collect all the values from the table (Arguments), using this create the * Attributes, this will create the Attributes for the User * defined TestCase for Add Test * * @return The Attributes **************************************************************************/ private Attributes getUserAttributes() { Attributes attrs = new BasicAttributes(true); Attribute attr; for (JMeterProperty jMeterProperty : getArguments()) { Argument item = (Argument) jMeterProperty.getObjectValue(); attr = attrs.get(item.getName()); if (attr == null) { attr = getBasicAttribute(item.getName(), item.getValue()); } else { attr.add(item.getValue()); } attrs.put(attr); } return attrs; }
@Test public void testBind() throws Exception { String dnToBind = "some dn to bind"; initializeTest(dnToBind); Attributes attributes = new BasicAttributes(); attributes.put("some attribute name", "some attribute value"); body.put(SpringLdapProducer.ATTRIBUTES, attributes); producer.sendBody("spring-ldap:" + SpringLdapTestConfiguration.LDAP_MOCK_NAME + "?operation=bind", body); ArgumentCaptor<String> dnCaptor = ArgumentCaptor.forClass(String.class); ArgumentCaptor<Attributes> attributesCaptor = ArgumentCaptor .forClass(Attributes.class); ArgumentCaptor<Object> objectToBindCaptor = ArgumentCaptor .forClass(Object.class); Mockito.verify(ldapTemplate).bind(dnCaptor.capture(), objectToBindCaptor.capture(), attributesCaptor.capture()); assertEquals(dnToBind, dnCaptor.getValue()); assertNull(objectToBindCaptor.getValue()); assertEquals(attributes, attributesCaptor.getValue()); }
@Test public void testBind() throws Exception { String dn = "some dn"; BasicAttributes attributes = new BasicAttributes(); Exchange exchange = new DefaultExchange(context); Message in = new DefaultMessage(); Map<String, Object> body = new HashMap<String, Object>(); body.put(SpringLdapProducer.DN, dn); body.put(SpringLdapProducer.ATTRIBUTES, attributes); when(ldapEndpoint.getOperation()).thenReturn(LdapOperation.BIND); processBody(exchange, in, body); verify(ldapTemplate).bind(eq(dn), isNull(), eq(attributes)); }
@Override public Attributes getAttributes(String name, String[] ids) throws NamingException { if (Validator.isNotNull(name)) { throw new NameNotFoundException(); } Attributes attributes = new BasicAttributes(true); for (int i = 0; i < ids.length; i++) { Attribute attribute = _attributes.get(ids[i]); if (attribute != null) { attributes.put(attribute); } } return attributes; }
private static Attributes toAttributes(String strAttributes,String delimiter, String separator) throws PageException { String[] arrAttr = toStringAttributes(strAttributes,delimiter); BasicAttributes attributes = new BasicAttributes(); for(int i=0; i<arrAttr.length; i++) { String strAttr = arrAttr[i]; // Type int eqIndex=strAttr.indexOf('='); Attribute attr = new BasicAttribute((eqIndex != -1)?strAttr.substring(0, eqIndex).trim():null); // Value String strValue = (eqIndex!=-1)?strAttr.substring( eqIndex+ 1):strAttr; String[] arrValue=ListUtil.toStringArray(ListUtil.listToArrayRemoveEmpty(strValue,separator)); // Fill for(int y=0; y<arrValue.length; y++) { attr.add(arrValue[y]); } attributes.put(attr); } return attributes; }
@Test public void testCreate() throws Exception { String input = "exec native('create;uid=doe,ou=people,o=teiid.org;attributes=one,two,three', 'one', 2, 3.0)"; TranslationUtility util = FakeTranslationFactory.getInstance().getExampleTranslationUtility(); Command command = util.parseCommand(input); ExecutionContext ec = Mockito.mock(ExecutionContext.class); RuntimeMetadata rm = Mockito.mock(RuntimeMetadata.class); LdapContext connection = Mockito.mock(LdapContext.class); LdapContext ctx = Mockito.mock(LdapContext.class); Mockito.stub(connection.lookup("")).toReturn(ctx); LDAPDirectCreateUpdateDeleteQueryExecution execution = (LDAPDirectCreateUpdateDeleteQueryExecution)TRANSLATOR.createExecution(command, ec, rm, connection); execution.execute(); ArgumentCaptor<String> nameArgument = ArgumentCaptor.forClass(String.class); ArgumentCaptor<BasicAttributes> createItemArgument = ArgumentCaptor.forClass(BasicAttributes.class); Mockito.verify(ctx).createSubcontext(nameArgument.capture(), createItemArgument.capture()); assertEquals("uid=doe,ou=people,o=teiid.org", nameArgument.getValue()); assertEquals("one", createItemArgument.getValue().get("one").getID()); assertEquals("one", createItemArgument.getValue().get("one").get()); assertEquals("two", createItemArgument.getValue().get("two").getID()); assertEquals("2", createItemArgument.getValue().get("two").get()); assertEquals("three", createItemArgument.getValue().get("three").getID()); assertEquals("3.0", createItemArgument.getValue().get("three").get()); }
/** * Convenience method for creating principals. * * @param cn the commonName of the person * @param principal the kerberos principal name for the person * @param sn the surName of the person * @param uid the unique identifier for the person * @param userPassword the credentials of the person * @return the attributes of the person principal */ protected Attributes getPrincipalAttributes(String sn, String cn, String uid, String userPassword, String principal) { Attributes attributes = new BasicAttributes(true); Attribute basicAttribute = new BasicAttribute("objectClass"); basicAttribute.add("top"); basicAttribute.add("person"); // sn $ cn basicAttribute.add("inetOrgPerson"); // uid basicAttribute.add("krb5principal"); basicAttribute.add("krb5kdcentry"); attributes.put(basicAttribute); attributes.put("cn", cn); attributes.put("sn", sn); attributes.put("uid", uid); attributes.put(SchemaConstants.USER_PASSWORD_AT, userPassword); attributes.put(KerberosAttribute.KRB5_PRINCIPAL_NAME_AT, principal); attributes.put(KerberosAttribute.KRB5_KEY_VERSION_NUMBER_AT, "0"); return attributes; }
public MyUser(String userId, String surName, String commonName) { myAttrs = new BasicAttributes(true); // Case ignore Attribute oc = new BasicAttribute("objectclass"); oc.add("inetOrgPerson"); oc.add("organizationalPerson"); oc.add("person"); oc.add("top"); Attribute sn = new BasicAttribute("sn"); sn.add(surName); Attribute cn = new BasicAttribute("cn"); cn.add(commonName); Attribute uid = new BasicAttribute("uid"); uid.add(userId); myAttrs.put(sn); myAttrs.put(cn); myAttrs.put(uid); myAttrs.put(oc); }
public Attributes toAttributes() { BasicAttributes bas = new BasicAttributes(true); for (Iterator<Attribute> iter = list.iterator(); iter.hasNext();) { Attribute attr = iter.next(); BasicAttribute ba = new BasicAttribute(attr.getID(), false); try { NamingEnumeration nameEnum = attr.getAll(); while (nameEnum.hasMore()) { ba.add(nameEnum.next()); } } catch (NamingException ne) { } bas.put(ba); } return bas; }
@Test public void testStandardConstructor() throws Exception { Attributes attrs = new BasicAttributes(); attrs.put("objectGUID;binary", AdServerTest.hexStringToByteArray("000102030405060708090a0b0c")); attrs.put("objectSid;binary", // S-1-0-0 AdServerTest.hexStringToByteArray("010100000000000000000000")); attrs.put("uSNChanged", "12345678"); attrs.put("primaryGroupId", "users"); attrs.put("userAccountControl", "512"); // standard, enabled, user SearchResult sr = new SearchResult("SR name", attrs, attrs); sr.setNameInNamespace("cn=user,ou=Users,dc=example,dc=com"); AdEntity adEntity = new AdEntity(sr); assertEquals("user", adEntity.getCommonName()); assertEquals("S-1-0-0", adEntity.getSid()); assertEquals("cn=user,ou=Users,dc=example,dc=com", adEntity.getDn()); assertFalse(adEntity.isWellKnown()); assertEquals(0, adEntity.getMembers().size()); assertEquals("S-1-0-users", adEntity.getPrimaryGroupSid()); assertFalse(adEntity.isDisabled()); }
@Test public void testAppendGroupsOnEmptyGroup() throws Exception { AdEntity adEntity = new AdEntity("parentGroup", "dc=com"); Attributes attrs = new BasicAttributes(); attrs.put("objectGUID;binary", AdServerTest.hexStringToByteArray("000102030405060708090a0b0c")); attrs.put("objectSid;binary", // S-1-0-0 AdServerTest.hexStringToByteArray("010100000000000000000000")); attrs.put("member", null); Attribute memberAttr = attrs.get("member"); memberAttr.clear(); SearchResult sr = new SearchResult("subgroup", attrs, attrs); sr.setNameInNamespace("cn=subgroup,ou=Groups,dc=example,dc=com"); AdEntity ae = new AdEntity(sr); HashSet<String> expectedMembers = new HashSet<String>(); assertEquals(expectedMembers, ae.getMembers()); assertEquals(0, adEntity.appendGroups(sr)); }
@Test public void testAppendGroupsOnRealGroup() throws Exception { AdEntity adEntity = new AdEntity("parentGroup", "dc=com"); Attributes attrs = new BasicAttributes(); attrs.put("objectGUID;binary", AdServerTest.hexStringToByteArray("000102030405060708090a0b0c")); attrs.put("objectSid;binary", // S-1-0-0 AdServerTest.hexStringToByteArray("010100000000000000000000")); List<String> members = Arrays.asList("dn_for_user_1", "dn_for_user_2"); attrs.put("member", null); Attribute memberAttr = attrs.get("member"); memberAttr.clear(); for (String member : members) { memberAttr.add(member); } SearchResult sr = new SearchResult("subgroup", attrs, attrs); sr.setNameInNamespace("cn=subgroup,ou=Groups,dc=example,dc=com"); AdEntity ae = new AdEntity(sr); assertEquals(new HashSet<String>(members), ae.getMembers()); assertEquals(2, adEntity.appendGroups(sr)); }
private void getAll(InitialDirContext ctx, String uid) { try { Attributes matchAttrs = new BasicAttributes(true); matchAttrs.put(new BasicAttribute("member", user)); NamingEnumeration<SearchResult> answer = ctx.search( "ou=permissions", matchAttrs, new String[] { "cn" }); while (answer.hasMore()) { SearchResult searchResult = answer.next(); Attributes attributes = searchResult.getAttributes(); attributes.get("cn"); } } catch (NamingException e) { e.printStackTrace(); } }
@SuppressWarnings("unchecked") public NamingEnumeration<SearchResult> createMock(List<String> groups) throws Exception { NamingEnumeration<SearchResult> searchResultsEnum = mock(NamingEnumeration.class); SearchResult searchResult = mock(SearchResult.class); when(searchResultsEnum.hasMore()).thenReturn(true, false); when(searchResultsEnum.next()).thenReturn(searchResult); Attributes attributes = new BasicAttributes(); if (groups != null) { for (String grp : groups) { Attribute memberOf = attributes.get("memberOf"); if (memberOf == null) { attributes.put("memberOf", grp); } else { memberOf.add(grp); } } } attributes.put("CN", "CN=user,OU=mycompany,OU=IE"); attributes.put("atr1", "val1"); when(searchResult.getAttributes()).thenReturn(attributes); return searchResultsEnum; }
/** * merge two instanceof <code>Attributes</code> to one * * @param first * @param second * @return * @throws NamingException */ private Attributes mergeAttributes(Attributes first, Attributes second) throws NamingException { if (first == null) { return second; } if (second == null) { return first; } BasicAttributes attrs = new BasicAttributes(true); NamingEnumeration<? extends Attribute> enu = first.getAll(); while (enu.hasMore()) { attrs.put(enu.next()); } enu = second.getAll(); while (enu.hasMore()) { Attribute element = enu.next(); element = mergeAttribute(element, attrs.get(element.getID())); attrs.put(element); } return attrs; }