1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.directory.server.operations.modify;
21
22
23 import java.util.Arrays;
24
25 import javax.naming.NamingEnumeration;
26 import javax.naming.NamingException;
27 import javax.naming.NoPermissionException;
28 import javax.naming.directory.Attribute;
29 import javax.naming.directory.AttributeInUseException;
30 import javax.naming.directory.AttributeModificationException;
31 import javax.naming.directory.Attributes;
32 import javax.naming.directory.BasicAttribute;
33 import javax.naming.directory.BasicAttributes;
34 import javax.naming.directory.DirContext;
35 import javax.naming.directory.InvalidAttributeValueException;
36 import javax.naming.directory.ModificationItem;
37 import javax.naming.directory.NoSuchAttributeException;
38 import javax.naming.directory.SearchControls;
39 import javax.naming.directory.SearchResult;
40
41 import org.apache.directory.server.core.integ.Level;
42 import org.apache.directory.server.core.integ.annotations.ApplyLdifs;
43 import org.apache.directory.server.core.integ.annotations.CleanupLevel;
44 import org.apache.directory.server.integ.SiRunner;
45 import static org.apache.directory.server.integ.ServerIntegrationUtils.getWiredContext;
46
47 import org.apache.directory.server.ldap.LdapService;
48 import org.junit.Test;
49 import org.junit.runner.RunWith;
50 import static org.junit.Assert.fail;
51 import static org.junit.Assert.assertTrue;
52 import static org.junit.Assert.assertFalse;
53 import static org.junit.Assert.assertEquals;
54 import static org.junit.Assert.assertNotNull;
55
56
57
58
59
60
61
62
63
64
65 @RunWith ( SiRunner.class )
66 @CleanupLevel ( Level.SUITE )
67 @ApplyLdifs( {
68
69 "dn: cn=Tori Amos,ou=system\n" +
70 "objectClass: inetOrgPerson\n" +
71 "objectClass: organizationalPerson\n" +
72 "objectClass: person\n" +
73 "objectClass: top\n" +
74 "description: an American singer-songwriter\n" +
75 "cn: Tori Amos\n" +
76 "sn: Amos\n\n" +
77
78 "dn: cn=Debbie Harry,ou=system\n" +
79 "objectClass: inetOrgPerson\n" +
80 "objectClass: organizationalPerson\n" +
81 "objectClass: person\n" +
82 "objectClass: top\n" +
83 "cn: Debbie Harry\n" +
84 "sn: Harry\n\n"
85 }
86 )
87 public class ModifyAddIT
88 {
89 private static final String BASE = "ou=system";
90 private static final String RDN_TORI_AMOS = "cn=Tori Amos";
91 private static final String PERSON_DESCRIPTION = "an American singer-songwriter";
92 private static final String RDN_DEBBIE_HARRY = "cn=Debbie Harry";
93
94 public static LdapService ldapService;
95
96
97
98
99
100 protected Attributes getPersonAttributes( String sn, String cn )
101 {
102 Attributes attributes = new BasicAttributes( true );
103 Attribute attribute = new BasicAttribute( "objectClass" );
104 attribute.add( "top" );
105 attribute.add( "person" );
106 attribute.add( "organizationalperson" );
107 attribute.add( "inetorgperson" );
108 attributes.put( attribute );
109 attributes.put( "cn", cn );
110 attributes.put( "sn", sn );
111
112 return attributes;
113 }
114
115
116
117
118
119 @Test
120 public void testAddNewAttributeValue() throws Exception
121 {
122 DirContext ctx = ( DirContext ) getWiredContext( ldapService ).lookup( BASE );
123
124
125 String newValue = "1234567890";
126 Attributes attrs = new BasicAttributes( "telephoneNumber", newValue, true );
127 ctx.modifyAttributes( RDN_TORI_AMOS, DirContext.ADD_ATTRIBUTE, attrs );
128
129
130 attrs = ctx.getAttributes( RDN_TORI_AMOS );
131 Attribute attr = attrs.get( "telephoneNumber" );
132 assertNotNull( attr );
133 assertTrue( attr.contains( newValue ) );
134 assertEquals( 1, attr.size() );
135 }
136
137
138
139
140
141 @Test
142 public void testAddNewAttributeValues() throws Exception
143 {
144 DirContext ctx = ( DirContext ) getWiredContext( ldapService ).lookup( BASE );
145
146
147 String[] newValues =
148 { "1234567890", "999999999" };
149 Attribute attr = new BasicAttribute( "telephoneNumber" );
150 attr.add( newValues[0] );
151 attr.add( newValues[1] );
152 Attributes attrs = new BasicAttributes( true );
153 attrs.put( attr );
154 ctx.modifyAttributes( RDN_TORI_AMOS, DirContext.ADD_ATTRIBUTE, attrs );
155
156
157 attrs = ctx.getAttributes( RDN_TORI_AMOS );
158 attr = attrs.get( "telephoneNumber" );
159 assertNotNull( attr );
160 assertTrue( attr.contains( newValues[0] ) );
161 assertTrue( attr.contains( newValues[1] ) );
162 assertEquals( newValues.length, attr.size() );
163 }
164
165
166
167
168
169 @Test
170 public void testAddAdditionalAttributeValue() throws Exception
171 {
172 DirContext ctx = ( DirContext ) getWiredContext( ldapService ).lookup( BASE );
173
174
175 String newValue = "A new description for this person";
176 assertFalse( newValue.equals( PERSON_DESCRIPTION ) );
177 Attributes attrs = new BasicAttributes( "description", newValue, true );
178
179 ctx.modifyAttributes( RDN_TORI_AMOS, DirContext.ADD_ATTRIBUTE, attrs );
180
181
182 attrs = ctx.getAttributes( RDN_TORI_AMOS );
183 Attribute attr = attrs.get( "description" );
184 assertNotNull( attr );
185 assertTrue( attr.contains( newValue ) );
186 assertTrue( attr.contains( PERSON_DESCRIPTION ) );
187 assertEquals( 2, attr.size() );
188 }
189
190
191
192
193
194
195
196
197
198
199 @Test
200 public void testAddExistingAttributeValue() throws Exception
201 {
202 DirContext ctx = ( DirContext ) getWiredContext( ldapService ).lookup( BASE );
203
204
205 Attributes attrs = new BasicAttributes( "description", PERSON_DESCRIPTION, true );
206
207 try
208 {
209 ctx.modifyAttributes( RDN_TORI_AMOS, DirContext.ADD_ATTRIBUTE, attrs );
210 fail( "Adding an already existing atribute value should fail." );
211 }
212 catch ( AttributeInUseException e )
213 {
214
215 }
216
217
218 attrs = ctx.getAttributes( RDN_TORI_AMOS );
219 Attribute attr = attrs.get( "description" );
220 assertNotNull( attr );
221 assertTrue( attr.contains( PERSON_DESCRIPTION ) );
222 assertEquals( 1, attr.size() );
223 }
224
225
226
227
228
229
230
231
232
233
234
235
236 @Test
237 public void testAddExistingNthAttributesDirServer664() throws Exception
238 {
239 DirContext ctx = ( DirContext ) getWiredContext( ldapService ).lookup( BASE );
240
241
242 Attributes attrs = new BasicAttributes( true );
243 attrs.put( new BasicAttribute( "telephoneNumber", "attr 1" ) );
244 attrs.put( new BasicAttribute( "telephoneNumber", "attr 2" ) );
245 attrs.put( new BasicAttribute( "telephoneNumber", "attr 3" ) );
246 attrs.put( new BasicAttribute( "telephoneNumber", "attr 4" ) );
247 attrs.put( new BasicAttribute( "telephoneNumber", "attr 5" ) );
248 attrs.put( new BasicAttribute( "telephoneNumber", "attr 6" ) );
249 attrs.put( new BasicAttribute( "telephoneNumber", "attr 7" ) );
250 attrs.put( new BasicAttribute( "telephoneNumber", "attr 8" ) );
251 attrs.put( new BasicAttribute( "telephoneNumber", "attr 9" ) );
252 attrs.put( new BasicAttribute( "telephoneNumber", "attr 10" ) );
253 attrs.put( new BasicAttribute( "telephoneNumber", "attr 11" ) );
254 attrs.put( new BasicAttribute( "telephoneNumber", "attr 12" ) );
255 attrs.put( new BasicAttribute( "telephoneNumber", "attr 13" ) );
256 attrs.put( new BasicAttribute( "telephoneNumber", "attr 14" ) );
257
258 Attribute attr = new BasicAttribute( "description", PERSON_DESCRIPTION );
259
260 attrs.put( attr );
261
262 try
263 {
264 ctx.modifyAttributes( RDN_TORI_AMOS, DirContext.ADD_ATTRIBUTE, attrs );
265 fail( "Adding an already existing atribute value should fail." );
266 }
267 catch ( AttributeInUseException e )
268 {
269
270 }
271
272
273 attrs = ctx.getAttributes( RDN_TORI_AMOS );
274 attr = attrs.get( "description" );
275 assertNotNull( attr );
276 assertTrue( attr.contains( PERSON_DESCRIPTION ) );
277 assertEquals( 1, attr.size() );
278 }
279
280
281
282
283
284 @Test
285 public void testTwoDescriptionDirServer643() throws Exception
286 {
287 DirContext ctx = ( DirContext ) getWiredContext( ldapService ).lookup( BASE );
288
289
290 Attributes attrs = new BasicAttributes( true );
291 Attribute attr = new BasicAttribute( "description", "a British singer-songwriter with an expressive four-octave voice" );
292 attr.add( "one of the most influential female artists of the twentieth century" );
293 attrs.put( attr );
294
295 ctx.modifyAttributes( RDN_TORI_AMOS, DirContext.ADD_ATTRIBUTE, attrs );
296
297
298 attrs = ctx.getAttributes( RDN_TORI_AMOS );
299 attr = attrs.get( "description" );
300 assertNotNull( attr );
301 assertEquals( 3, attr.size() );
302 assertTrue( attr.contains( "a British singer-songwriter with an expressive four-octave voice" ) );
303 assertTrue( attr.contains( "one of the most influential female artists of the twentieth century" ) );
304 assertTrue( attr.contains( PERSON_DESCRIPTION ) );
305 }
306
307
308
309
310
311
312
313 @Test
314 public void testAddDuplicateValueToExistingAttribute() throws Exception
315 {
316 DirContext ctx = ( DirContext ) getWiredContext( ldapService ).lookup( BASE );
317
318
319 Attribute ocls = new BasicAttribute( "objectClass", "organizationalPerson" );
320 ModificationItem[] modItems = new ModificationItem[2];
321 modItems[0] = new ModificationItem( DirContext.ADD_ATTRIBUTE, ocls );
322 modItems[1] = new ModificationItem( DirContext.ADD_ATTRIBUTE, ocls );
323 try
324 {
325 ctx.modifyAttributes( RDN_TORI_AMOS, modItems );
326 fail( "Adding a duplicate attribute value should cause an error." );
327 }
328 catch ( AttributeInUseException ex )
329 {
330 }
331
332
333 Attributes attrs = ctx.getAttributes( RDN_TORI_AMOS );
334 ocls = attrs.get( "objectClass" );
335 assertEquals( ocls.size(), 4 );
336 assertTrue( ocls.contains( "top" ) );
337 assertTrue( ocls.contains( "person" ) );
338 }
339
340
341
342
343
344
345
346 @Test
347 public void testAddDuplicateValueToNewAttribute() throws Exception
348 {
349 DirContext ctx = ( DirContext ) getWiredContext( ldapService ).lookup( BASE );
350
351
352 Attribute desc = new BasicAttribute( "description", "another description value besides songwriter" );
353 ModificationItem[] modItems = new ModificationItem[2];
354 modItems[0] = new ModificationItem( DirContext.ADD_ATTRIBUTE, desc );
355 modItems[1] = new ModificationItem( DirContext.ADD_ATTRIBUTE, desc );
356 try
357 {
358 ctx.modifyAttributes( RDN_TORI_AMOS, modItems );
359 fail( "Adding a duplicate attribute value should cause an error." );
360 }
361 catch ( AttributeInUseException ex )
362 {
363 }
364
365
366 Attributes attrs = ctx.getAttributes( RDN_TORI_AMOS );
367 assertEquals( 1, attrs.get( "description" ).size() );
368 }
369
370
371
372
373
374 @Test
375 public void testSearchBadAttribute() throws Exception
376 {
377 DirContext ctx = ( DirContext ) getWiredContext( ldapService ).lookup( BASE );
378
379
380 String newValue = "unbelievable";
381 Attributes attrs = new BasicAttributes( "voice", newValue, true );
382
383 try
384 {
385 ctx.modifyAttributes( RDN_TORI_AMOS, DirContext.ADD_ATTRIBUTE, attrs );
386 }
387 catch ( NoSuchAttributeException nsae )
388 {
389
390 assertTrue( true );
391 return;
392 }
393
394 fail( "Cannot reach this point" );
395 }
396
397
398
399
400
401
402 @Test
403 public void testAttributeValueMultiMofificationDIRSERVER_636() throws Exception
404 {
405 DirContext ctx = ( DirContext ) getWiredContext( ldapService ).lookup( BASE );
406
407
408 Attributes attrs = getPersonAttributes("Bush", "Kate Bush");
409 String rdn = "cn=Kate Bush";
410 ctx.createSubcontext(rdn, attrs);
411
412
413 String[] descriptions = {
414 "Kate Bush is a British singer-songwriter.",
415 "She has become one of the most influential female artists of the twentieth century." };
416 Attribute desc1 = new BasicAttribute("description");
417 desc1.add(descriptions[0]);
418 desc1.add(descriptions[1]);
419
420 ModificationItem addModOp = new ModificationItem(
421 DirContext.ADD_ATTRIBUTE, desc1);
422
423 Attribute desc2 = new BasicAttribute("description");
424 desc2.add(descriptions[1]);
425 ModificationItem delModOp = new ModificationItem(
426 DirContext.REMOVE_ATTRIBUTE, desc2);
427
428 ctx.modifyAttributes(rdn, new ModificationItem[] { addModOp,
429 delModOp });
430
431 SearchControls sctls = new SearchControls();
432 sctls.setSearchScope(SearchControls.SUBTREE_SCOPE);
433 String filter = "(cn=*Bush)";
434 String base = "";
435
436
437 NamingEnumeration<SearchResult> enm = ctx.search(base, filter, sctls);
438 assertTrue(enm.hasMore());
439
440 while (enm.hasMore()) {
441 SearchResult sr = enm.next();
442 attrs = sr.getAttributes();
443 Attribute desc = sr.getAttributes().get("description");
444 assertEquals(1, desc.size());
445 assertTrue(desc.contains(descriptions[0]));
446 }
447
448
449 ctx.destroySubcontext(rdn);
450 }
451
452
453
454
455
456 @Test
457 public void testModifyOperationalAttributeAdd() throws Exception
458 {
459 DirContext ctx = ( DirContext ) getWiredContext( ldapService ).lookup( BASE );
460
461 ModificationItem modifyOp = new ModificationItem( DirContext.ADD_ATTRIBUTE, new BasicAttribute(
462 "subschemaSubentry", "cn=anotherSchema" ) );
463
464 try
465 {
466 ctx.modifyAttributes( RDN_DEBBIE_HARRY, new ModificationItem[]
467 { modifyOp } );
468
469 fail( "modification of entry should fail" );
470 }
471 catch ( InvalidAttributeValueException e )
472 {
473
474 }
475 catch ( NoPermissionException e )
476 {
477
478 }
479 }
480
481
482
483
484
485
486
487
488 @Test
489 public void testDNAttributeMemberMofificationDIRSERVER_687() throws Exception
490 {
491 DirContext ctx = ( DirContext ) getWiredContext( ldapService ).lookup( BASE );
492
493
494 Attributes attrs = getPersonAttributes("Bush", "Kate Bush");
495 String rdn = "cn=Kate Bush";
496 ctx.createSubcontext(rdn, attrs);
497
498
499 Attribute desc1 = new BasicAttribute( "cn", "Georges Bush" );
500
501 ModificationItem addModOp = new ModificationItem(
502 DirContext.REPLACE_ATTRIBUTE, desc1);
503
504 try
505 {
506 ctx.modifyAttributes( rdn, new ModificationItem[] { addModOp } );
507 fail();
508 }
509 catch ( AttributeModificationException ame )
510 {
511 assertTrue( true );
512
513 ctx.destroySubcontext(rdn);
514 }
515 catch ( NamingException ne )
516 {
517 assertTrue( true );
518
519 ctx.destroySubcontext(rdn);
520 }
521 }
522
523
524
525
526
527
528
529 @Test
530 public void testModifyAddWithInvalidNumberOfAttributeValues() throws Exception
531 {
532 DirContext ctx = ( DirContext ) getWiredContext( ldapService ).lookup( BASE );
533
534 Attributes attrs = new BasicAttributes( true );
535 Attribute ocls = new BasicAttribute( "objectClass" );
536 ocls.add( "top" );
537 ocls.add( "inetOrgPerson" );
538 attrs.put( ocls );
539 attrs.put( "cn", "Fiona Apple" );
540 attrs.put( "sn", "Apple" );
541 ctx.createSubcontext( "cn=Fiona Apple", attrs );
542
543
544 attrs = new BasicAttributes( true );
545 Attribute displayName = new BasicAttribute( "displayName" );
546 displayName.add( "Fiona" );
547 displayName.add( "Fiona A." );
548 attrs.put( displayName );
549
550 try
551 {
552 ctx.modifyAttributes( "cn=Fiona Apple", DirContext.ADD_ATTRIBUTE, attrs );
553 fail( "modification of entry should fail" );
554 }
555 catch ( InvalidAttributeValueException e )
556 {
557
558 }
559 }
560
561
562
563
564
565 @Test
566 public void testAddNewBinaryAttributeValue() throws Exception
567 {
568 DirContext ctx = ( DirContext ) getWiredContext( ldapService ).lookup( BASE );
569
570
571 byte[] newValue = new byte[]{0x00, 0x01, 0x02, 0x03};
572 Attributes attrs = new BasicAttributes( "userCertificate;binary", newValue, true );
573 ctx.modifyAttributes( RDN_TORI_AMOS, DirContext.ADD_ATTRIBUTE, attrs );
574
575
576 attrs = ctx.getAttributes( RDN_TORI_AMOS );
577 Attribute attr = attrs.get( "userCertificate" );
578 assertNotNull( attr );
579 assertTrue( attr.contains( newValue ) );
580 byte[] certificate = (byte[])attr.get();
581 assertTrue( Arrays.equals( newValue, certificate ) );
582 assertEquals( 1, attr.size() );
583 }
584 }