Skip to content

Commit f63e8e9

Browse files
committed
New test methods testRfc4648 based on RFC 4648 that show our inconsistent handling of CR LF at the end of data. Failing assertions are //commented out.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/codec/trunk@928129 13f79535-47bb-0310-9956-ffa450edef68
1 parent 00da592 commit f63e8e9

1 file changed

Lines changed: 57 additions & 0 deletions

File tree

src/test/org/apache/commons/codec/binary/Base64Test.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,63 @@ public void testRfc1421Section6Dot8ChunkSizeDefinition() {
517517
assertEquals(64, Base64.PEM_CHUNK_SIZE);
518518
}
519519

520+
/**
521+
* Tests RFC 4648 section 10 test vectors.
522+
* <ul>
523+
* <li>BASE64("") = ""</li>
524+
* <li>BASE64("f") = "Zg=="</li>
525+
* <li>BASE64("fo") = "Zm8="</li>
526+
* <li>BASE64("foo") = "Zm9v"</li>
527+
* <li>BASE64("foob") = "Zm9vYg=="</li>
528+
* <li>BASE64("fooba") = "Zm9vYmE="</li>
529+
* <li>BASE64("foobar") = "Zm9vYmFy"</li>
530+
* </ul>
531+
*
532+
* @see http://tools.ietf.org/html/rfc4648
533+
*/
534+
public void testRfc4648Section10Decode() {
535+
assertEquals("", StringUtils.newStringUsAscii(Base64.decodeBase64("")));
536+
assertEquals("f", StringUtils.newStringUsAscii(Base64.decodeBase64("Zg==")));
537+
assertEquals("fo", StringUtils.newStringUsAscii(Base64.decodeBase64("Zm8=")));
538+
assertEquals("foo", StringUtils.newStringUsAscii(Base64.decodeBase64("Zm9v")));
539+
assertEquals("foob", StringUtils.newStringUsAscii(Base64.decodeBase64("Zm9vYg==")));
540+
assertEquals("fooba", StringUtils.newStringUsAscii(Base64.decodeBase64("Zm9vYmE=")));
541+
assertEquals("foobar", StringUtils.newStringUsAscii(Base64.decodeBase64("Zm9vYmFy")));
542+
}
543+
544+
public void testRfc4648Section10DecodeWithCrLf() {
545+
assertEquals("", StringUtils.newStringUsAscii(Base64.decodeBase64("")));
546+
assertEquals("f", StringUtils.newStringUsAscii(Base64.decodeBase64("Zg==" + Base64.CHUNK_SEPARATOR)));
547+
assertEquals("fo", StringUtils.newStringUsAscii(Base64.decodeBase64("Zm8=" + Base64.CHUNK_SEPARATOR)));
548+
//assertEquals("foo", StringUtils.newStringUsAscii(Base64.decodeBase64("Zm9v" + Base64.CHUNK_SEPARATOR)));
549+
assertEquals("foob", StringUtils.newStringUsAscii(Base64.decodeBase64("Zm9vYg==" + Base64.CHUNK_SEPARATOR)));
550+
assertEquals("fooba", StringUtils.newStringUsAscii(Base64.decodeBase64("Zm9vYmE=" + Base64.CHUNK_SEPARATOR)));
551+
//assertEquals("foobar", StringUtils.newStringUsAscii(Base64.decodeBase64("Zm9vYmFy" + Base64.CHUNK_SEPARATOR)));
552+
}
553+
554+
/**
555+
* Tests RFC 4648 section 10 test vectors.
556+
* <ul>
557+
* <li>BASE64("") = ""</li>
558+
* <li>BASE64("f") = "Zg=="</li>
559+
* <li>BASE64("fo") = "Zm8="</li>
560+
* <li>BASE64("foo") = "Zm9v"</li>
561+
* <li>BASE64("foob") = "Zm9vYg=="</li>
562+
* <li>BASE64("fooba") = "Zm9vYmE="</li>
563+
* <li>BASE64("foobar") = "Zm9vYmFy"</li>
564+
* </ul>
565+
*
566+
* @see http://tools.ietf.org/html/rfc4648
567+
*/
568+
public void testRfc4648Section10Encode() {
569+
//assertEquals("Zg==", Base64.encodeBase64String(StringUtils.getBytesUtf8("f")));
570+
//assertEquals("Zm8=", Base64.encodeBase64String(StringUtils.getBytesUtf8("fo")));
571+
//assertEquals("Zm9v", Base64.encodeBase64String(StringUtils.getBytesUtf8("foo")));
572+
//assertEquals("Zm9vYg==", Base64.encodeBase64String(StringUtils.getBytesUtf8("foob")));
573+
//assertEquals("Zm9vYmE=", Base64.encodeBase64String(StringUtils.getBytesUtf8("fooba")));
574+
//assertEquals("Zm9vYmFy", Base64.encodeBase64String(StringUtils.getBytesUtf8("foobar")));
575+
}
576+
520577
public void testSingletons() {
521578
assertEquals("AA==", new String(Base64.encodeBase64(new byte[]{(byte) 0})));
522579
assertEquals("AQ==", new String(Base64.encodeBase64(new byte[]{(byte) 1})));

0 commit comments

Comments
 (0)