/*
* Copyright ( c ) 2014 , 2020 , Oracle and / or its affiliates . All rights reserved .
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER .
*
* This code is free software ; you can redistribute it and / or modify it
* under the terms of the GNU General Public License version 2 only , as
* published by the Free Software Foundation .
*
* This code is distributed in the hope that it will be useful , but WITHOUT
* ANY WARRANTY ; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE . See the GNU General Public License
* version 2 for more details ( a copy is included in the LICENSE file that
* accompanied this code ) .
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work ; if not , write to the Free Software Foundation ,
* Inc . , 51 Franklin St , Fifth Floor , Boston , MA 02110 - 1301 USA .
*
* Please contact Oracle , 500 Oracle Parkway , Redwood Shores , CA 94065 USA
* or visit www . oracle . com if you need additional information or have any
* questions .
*/
/**
* @ test
* @ bug 8035807
* @ summary Confirm that old and new Base64 encodings are compatible .
*/
import java.io.*;
import java.util.*;
import javax.naming.*;
import javax.naming.directory.*;
/*
* RFC 2713 specifies an encoding for Java objects stored in an LDAP directory .
* Section 3 . 6 specifies how a binary - valued JNDI RefAddr object is encoded
* in the value of a javaReferenceAttribute LDAP attribute : first the RefAddr
* object is serialized and then it is encoded using Base64 .
*
* Since JDK 9 , the JNDI / LDAP provider uses the public Base64 encoder which
* adheres strictly to the MIME encoding rules . The encoder inserts ' \ r \ n '
* as the line separator at intervals of 76 characters . Previously the
* JNDI / LDAP provider used a private Base64 encoder which inserted ' \ n '
* as the line separator . It is a compatible change .
*
* This test demonstrates that there is no compatability problem when
* decoding using the new Base64 coder :
*
* encoded bytes captured from s . m . BASE64Encoder , decode with j . u . Base64 . Decoder = > OK
* encoded bytes captured from j . u . Base64 . Encoder , decode with j . u . Base64 . Decoder = > OK
*
*
* NOTE : The two Base64 encodings used in this test were captured from
* LDAP protocol exchanges during attempts by the JNDI / LDAP provider
* to store a JNDI Reference test object .
*/
public class Base64Test {
/*
* The old Base64 encoding uses ' \ n ' as the line separator at 76 character
* intervals :
*
* 0000 : 72 4 F 30 41 42 58 4 E 79 41 42 70 71 59 58 5 A 68 rO0ABXNyABpqYXZh
* 0010 : 65 43 35 75 59 57 31 70 62 6 D 63 75 51 6 D 6 C 75 eC5uYW1pbmcuQmlu
* 0020 : 59 58 4 A 35 55 6 D 56 6 D 51 57 52 6 B 63 74 43 61 YXJ5UmVmQWRkctCa
* 0030 : 6 B 37 4 C 65 73 34 68 48 41 67 41 42 57 77 41 44 k7Les4hHAgABWwAD
* 0040 : 59 6 E 56 6 D 64 41 41 43 57 30 4 A 34 0 A 63 67 41 YnVmdAACW0J4 . cgA <
* 0050 : 55 61 6 D 46 32 59 58 67 75 62 6 D 46 74 61 57 35 UamF2YXgubmFtaW5
* 0060 : 6 E 4 C 6 C 4 A 6 C 5 A 6 B 46 6 B 5 A 48 4 C 72 6 F 41 65 nLlJlZkFkZHLroAe
* 0070 : 61 41 6 A 69 76 53 67 49 41 41 55 77 41 43 47 46 aAjivSgIAAUwACGF
* 0080 : 6 B 5 A 48 4 A 55 65 58 42 6 C 64 41 41 53 54 47 70 kZHJUeXBldAASTGp
* 0090 : 68 64 6 D 45 76 62 47 46 75 0 A 5 A 79 39 54 64 48 hdmEvbGFu . Zy9TdH <
* 00 A0 : 4 A 70 62 6 D 63 37 65 48 42 30 41 41 52 30 5 A 58 Jpbmc7eHB0AAR0ZX
* 00 B0 : 4 E 30 64 58 49 41 41 6 C 74 43 72 50 4 D 58 2 B 41 N0dXIAAltCrPMX + A
* 00 C0 : 59 49 56 4 F 41 43 41 41 42 34 63 41 41 41 41 49 YIVOACAAB4cAAAAI
* 00 D0 : 41 41 41 51 49 44 42 41 55 47 42 77 67 4 A 43 67 AAAQIDBAUGBwgJCg
* 00 E0 : 73 4 D 44 51 34 50 0 A 45 42 45 53 45 78 51 56 46 sMDQ4P . EBESExQVF <
* 00 F0 : 68 63 59 47 52 6 F 62 48 42 30 65 48 79 41 68 49 hcYGRobHB0eHyAhI
* 0100 : 69 4 D 6 B 4 A 53 59 6 E 4 B 43 6 B 71 4 B 79 77 74 4 C iMkJSYnKCkqKywtL
* 0110 : 69 38 77 4 D 54 49 7 A 4 E 44 55 32 4 E 7 A 67 35 4 F i8wMTIzNDU2Nzg5O
* 0120 : 6 A 73 38 50 54 34 2 F 51 45 46 43 51 30 52 46 52 js8PT4 / QEFCQ0RFR
* 0130 : 6 B 64 49 0 A 53 55 70 4 C 54 45 31 4 F 54 31 42 52 kdI . SUpLTE1OT1BR <
* 0140 : 55 6 C 4 E 55 56 56 5 A 58 57 46 6 C 61 57 31 78 64 UlNUVVZXWFlaW1xd
* 0150 : 58 6 C 39 67 59 57 4 A 6 A 5 A 47 56 6 D 5 A 32 68 70 Xl9gYWJjZGVmZ2hp
* 0160 : 61 6 D 74 73 62 57 35 76 63 48 46 79 63 33 52 31 amtsbW5vcHFyc3R1
* 0170 : 64 6 E 64 34 65 58 70 37 66 48 31 2 B 66 77 3 D 3 D dnd4eXp7fH1 + fw = =
* 0180 : 0 A <
*/
private static final String OLD_ENCODING = "rO0ABXNyABpqYXZheC5uYW1pbmcuQmluYXJ5UmVmQWRkctCak7Les4hHAgABWwADYnVmdAACW0J4\ncgAUamF2YXgubmFtaW5nLlJlZkFkZHLroAeaAjivSgIAAUwACGFkZHJUeXBldAASTGphdmEvbGFu\nZy9TdHJpbmc7eHB0AAR0ZXN0dXIAAltCrPMX+AYIVOACAAB4cAAAAIAAAQIDBAUGBwgJCgsMDQ4P\nEBESExQVFhcYGRobHB0eHyAhIiMkJSYnKCkqKywtLi8wMTIzNDU2Nzg5Ojs8PT4/QEFCQ0RFRkdI\nSUpLTE1OT1BRUlNUVVZXWFlaW1xdXl9gYWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXp7fH1+fw==\n" ;
/*
* The new Base64 encoding uses ' \ r \ n ' as the line separator at 76 character
* intervals :
*
* 0000 : 72 4 F 30 41 42 58 4 E 79 41 42 70 71 59 58 5 A 68 rO0ABXNyABpqYXZh
* 0010 : 65 43 35 75 59 57 31 70 62 6 D 63 75 51 6 D 6 C 75 eC5uYW1pbmcuQmlu
* 0020 : 59 58 4 A 35 55 6 D 56 6 D 51 57 52 6 B 63 74 43 61 YXJ5UmVmQWRkctCa
* 0030 : 6 B 37 4 C 65 73 34 68 48 41 67 41 42 57 77 41 44 k7Les4hHAgABWwAD
* 0040 : 59 6 E 56 6 D 64 41 41 43 57 30 4 A 34 0 D 0 A 63 67 YnVmdAACW0J4 . . cg <
* 0050 : 41 55 61 6 D 46 32 59 58 67 75 62 6 D 46 74 61 57 AUamF2YXgubmFtaW
* 0060 : 35 6 E 4 C 6 C 4 A 6 C 5 A 6 B 46 6 B 5 A 48 4 C 72 6 F 41 5 nLlJlZkFkZHLroA
* 0070 : 65 61 41 6 A 69 76 53 67 49 41 41 55 77 41 43 47 eaAjivSgIAAUwACG
* 0080 : 46 6 B 5 A 48 4 A 55 65 58 42 6 C 64 41 41 53 54 47 FkZHJUeXBldAASTG
* 0090 : 70 68 64 6 D 45 76 62 47 46 75 0 D 0 A 5 A 79 39 54 phdmEvbGFu . . Zy9T <
* 00 A0 : 64 48 4 A 70 62 6 D 63 37 65 48 42 30 41 41 52 30 dHJpbmc7eHB0AAR0
* 00 B0 : 5 A 58 4 E 30 64 58 49 41 41 6 C 74 43 72 50 4 D 58 ZXN0dXIAAltCrPMX
* 00 C0 : 2 B 41 59 49 56 4 F 41 43 41 41 42 34 63 41 41 41 + AYIVOACAAB4cAAA
* 00 D0 : 41 49 41 41 41 51 49 44 42 41 55 47 42 77 67 4 A AIAAAQIDBAUGBwgJ
* 00 E0 : 43 67 73 4 D 44 51 34 50 0 D 0 A 45 42 45 53 45 78 CgsMDQ4P . . EBESEx <
* 00 F0 : 51 56 46 68 63 59 47 52 6 F 62 48 42 30 65 48 79 QVFhcYGRobHB0eHy
* 0100 : 41 68 49 69 4 D 6 B 4 A 53 59 6 E 4 B 43 6 B 71 4 B 79 AhIiMkJSYnKCkqKy
* 0110 : 77 74 4 C 69 38 77 4 D 54 49 7 A 4 E 44 55 32 4 E 7 A wtLi8wMTIzNDU2Nz
* 0120 : 67 35 4 F 6 A 73 38 50 54 34 2 F 51 45 46 43 51 30 g5Ojs8PT4 / QEFCQ0
* 0130 : 52 46 52 6 B 64 49 0 D 0 A 53 55 70 4 C 54 45 31 4 F RFRkdI . . SUpLTE1O <
* 0140 : 54 31 42 52 55 6 C 4 E 55 56 56 5 A 58 57 46 6 C 61 T1BRUlNUVVZXWFla
* 0150 : 57 31 78 64 58 6 C 39 67 59 57 4 A 6 A 5 A 47 56 6 D W1xdXl9gYWJjZGVm
* 0160 : 5 A 32 68 70 61 6 D 74 73 62 57 35 76 63 48 46 79 Z2hpamtsbW5vcHFy
* 0170 : 63 33 52 31 64 6 E 64 34 65 58 70 37 66 48 31 2 B c3R1dnd4eXp7fH1 +
* 0180 : 66 77 3 D 3 D
*/
private static final String NEW_ENCODING = "rO0ABXNyABpqYXZheC5uYW1pbmcuQmluYXJ5UmVmQWRkctCak7Les4hHAgABWwADYnVmdAACW0J4\r\ncgAUamF2YXgubmFtaW5nLlJlZkFkZHLroAeaAjivSgIAAUwACGFkZHJUeXBldAASTGphdmEvbGFu\r\nZy9TdHJpbmc7eHB0AAR0ZXN0dXIAAltCrPMX+AYIVOACAAB4cAAAAIAAAQIDBAUGBwgJCgsMDQ4P\r\nEBESExQVFhcYGRobHB0eHyAhIiMkJSYnKCkqKywtLi8wMTIzNDU2Nzg5Ojs8PT4/QEFCQ0RFRkdI\r\nSUpLTE1OT1BRUlNUVVZXWFlaW1xdXl9gYWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXp7fH1+fw==" ;
/*
* Binary - valued JNDI RefAddr test object
*/
private static final RefAddr BINARY_REF_ADDR =
new BinaryRefAddr("test" , new byte [] {
0 x00, 0 x01, 0 x02, 0 x03, 0 x04, 0 x05, 0 x06, 0 x07, 0 x08, 0 x09, 0 x0A, 0 x0B,
0 x0C, 0 x0D, 0 x0E, 0 x0F, 0 x10, 0 x11, 0 x12, 0 x13, 0 x14, 0 x15, 0 x16, 0 x17,
0 x18, 0 x19, 0 x1A, 0 x1B, 0 x1C, 0 x1D, 0 x1E, 0 x1F, 0 x20, 0 x21, 0 x22, 0 x23,
0 x24, 0 x25, 0 x26, 0 x27, 0 x28, 0 x29, 0 x2A, 0 x2B, 0 x2C, 0 x2D, 0 x2E, 0 x2F,
0 x30, 0 x31, 0 x32, 0 x33, 0 x34, 0 x35, 0 x36, 0 x37, 0 x38, 0 x39, 0 x3A, 0 x3B,
0 x3C, 0 x3D, 0 x3E, 0 x3F, 0 x40, 0 x41, 0 x42, 0 x43, 0 x44, 0 x45, 0 x46, 0 x47,
0 x48, 0 x49, 0 x4A, 0 x4B, 0 x4C, 0 x4D, 0 x4E, 0 x4F, 0 x50, 0 x51, 0 x52, 0 x53,
0 x54, 0 x55, 0 x56, 0 x57, 0 x58, 0 x59, 0 x5A, 0 x5B, 0 x5C, 0 x5D, 0 x5E, 0 x5F,
0 x60, 0 x61, 0 x62, 0 x63, 0 x64, 0 x65, 0 x66, 0 x67, 0 x68, 0 x69, 0 x6A, 0 x6B,
0 x6C, 0 x6D, 0 x6E, 0 x6F, 0 x70, 0 x71, 0 x72, 0 x73, 0 x74, 0 x75, 0 x76, 0 x77,
0 x78, 0 x79, 0 x7A, 0 x7B, 0 x7C, 0 x7D, 0 x7E, 0 x7F
});
public static void main(String[] args) throws Exception {
System.out.println("\nOriginal RefAddr object:\n" + BINARY_REF_ADDR);
System.out.println("Old Base64 encoded serialized RefAddr object:\n" +
OLD_ENCODING + "\n" );
System.out.println("Decode using new Base64 decoder..." );
deserialize(Base64.getMimeDecoder().decode(OLD_ENCODING));
System.out.println("----" );
System.out.println("\nOriginal RefAddr object:\n" + BINARY_REF_ADDR);
System.out.println("New Base64 encoded serialized RefAddr object:\n" +
NEW_ENCODING + "\n" );
System.out.println("Decode using new Base64 decoder..." );
deserialize(Base64.getMimeDecoder().decode(NEW_ENCODING));
System.out.println("----" );
}
/*
* Deserialize the decoded Base64 bytes to recover the BinaryRefAddr object .
*/
private static void deserialize(byte [] bytes) throws Exception {
ObjectInputStream objectStream =
new ObjectInputStream(new ByteArrayInputStream(bytes));
Object object = objectStream.readObject();
if (!BINARY_REF_ADDR.equals(object)) {
throw new Exception("Recovered object does not match the original" );
}
System.out.println("Recovered RefAddr object:\n" + object);
}
/*
* Dumps the encoding of a JNDI Reference object during an attempt to store
* in an LDAP directory .
*/
private static void storeObjectInLDAP() {
Hashtable<Object, Object> env = new Hashtable<>();
env.put(Context.REFERRAL, "follow" ); // omit an LDAP control
env.put("java.naming.ldap.version" , "3" ); // omit LDAP bind operation
env.put("com.sun.jndi.ldap.trace.ber" , System.err); // dump protocol
try {
DirContext ctx = new InitialDirContext(env);
Reference reference = new Reference("test" , BINARY_REF_ADDR);
ctx.bind("ldap://ldap.example.com/cn=test", reference);
} catch (NamingException ignore) {
}
}
}
Messung V0.5 in Prozent C=96 H=89 G=92
¤ Dauer der Verarbeitung: 0.13 Sekunden
(vorverarbeitet am 2026-10-11)
¤
*© Formatika GbR, Deutschland