package simple; import java.util.Hashtable; import java.util.Random; import javax.naming.Context; import javax.naming.NamingException; import javax.naming.directory.Attribute; import javax.naming.directory.Attributes; import javax.naming.directory.BasicAttribute; import javax.naming.directory.BasicAttributes; import javax.naming.directory.DirContext; import javax.naming.directory.InitialDirContext; import junit.framework.TestCase; /** * Testcase to demonstrate JIRA DIREVE-163 * "Putting a jpeg picture in the inetOrgPerson jpegPhoto attribute alter the bytes datas of the picture" * with a JNDI client. * * @author szoerner */ public class BinaryTest extends TestCase { public static String TORI_DN = "cn=Tori Amos"; DirContext ctx; protected void setUp() throws Exception { Hashtable env = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, "ldap://localhost:10389/ou=system"); env.put(Context.SECURITY_AUTHENTICATION, "simple"); env.put(Context.SECURITY_PRINCIPAL, "uid=admin,ou=system"); env.put(Context.SECURITY_CREDENTIALS, "secret"); ctx = new InitialDirContext(env); } public void testToriWithFunData() throws NamingException { // Create attributes for test entry Attribute ocls = new BasicAttribute("objectclass"); ocls.add("top"); ocls.add("person"); ocls.add("organizationalPerson"); ocls.add("inetOrgPerson"); Attributes attrs = new BasicAttributes(); attrs.put(ocls); attrs.put("sn", "Amos"); attrs.put("cn", "Tori Amos"); // Random data for jpegPhoto, 64k byte[] sourceData = new byte[64 * 1024]; Random r = new Random(0); for (int i = 0; i < sourceData.length; i++) { sourceData[i] = (byte) r.nextInt(); } attrs.put("jpegPhoto", sourceData); // Create entry ctx.bind(TORI_DN, null, attrs); // Read same entry and compare data bytewise Attributes readAttrs = ctx.getAttributes(TORI_DN); Attribute image = readAttrs.get("jpegPhoto"); byte[] imageBytes = (byte[]) image.get(); for (int i = 0; i < imageBytes.length; i++) { assertEquals("Byte number " + i, sourceData[i], imageBytes[i]); } } protected void tearDown() throws Exception { // Try to delete Entry ctx.destroySubcontext(TORI_DN); } }