Skip to content

Commit bbe1346

Browse files
committed
Roundtrip tests (some commented out that fail)
Applied https://issues.apache.org/jira/secure/attachment/12439949/codec-99-test-fixes.patch git-svn-id: https://svn.apache.org/repos/asf/commons/proper/codec/trunk@928158 13f79535-47bb-0310-9956-ffa450edef68
1 parent ed37475 commit bbe1346

1 file changed

Lines changed: 68 additions & 7 deletions

File tree

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

Lines changed: 68 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -556,13 +556,14 @@ public void testRfc4648Section10Decode() {
556556
* @see <a href="http://tools.ietf.org/html/rfc4648">http://tools.ietf.org/html/rfc4648</a>
557557
*/
558558
public void testRfc4648Section10DecodeWithCrLf() {
559-
assertEquals("", StringUtils.newStringUsAscii(Base64.decodeBase64("")));
560-
assertEquals("f", StringUtils.newStringUsAscii(Base64.decodeBase64("Zg==" + Base64.CHUNK_SEPARATOR)));
561-
assertEquals("fo", StringUtils.newStringUsAscii(Base64.decodeBase64("Zm8=" + Base64.CHUNK_SEPARATOR)));
562-
//assertEquals("foo", StringUtils.newStringUsAscii(Base64.decodeBase64("Zm9v" + Base64.CHUNK_SEPARATOR)));
563-
assertEquals("foob", StringUtils.newStringUsAscii(Base64.decodeBase64("Zm9vYg==" + Base64.CHUNK_SEPARATOR)));
564-
assertEquals("fooba", StringUtils.newStringUsAscii(Base64.decodeBase64("Zm9vYmE=" + Base64.CHUNK_SEPARATOR)));
565-
//assertEquals("foobar", StringUtils.newStringUsAscii(Base64.decodeBase64("Zm9vYmFy" + Base64.CHUNK_SEPARATOR)));
559+
String CRLF = StringUtils.newStringUsAscii(Base64.CHUNK_SEPARATOR);
560+
assertEquals("", StringUtils.newStringUsAscii(Base64.decodeBase64("" + CRLF)));
561+
assertEquals("f", StringUtils.newStringUsAscii(Base64.decodeBase64("Zg==" + CRLF)));
562+
assertEquals("fo", StringUtils.newStringUsAscii(Base64.decodeBase64("Zm8=" + CRLF)));
563+
assertEquals("foo", StringUtils.newStringUsAscii(Base64.decodeBase64("Zm9v" + CRLF)));
564+
assertEquals("foob", StringUtils.newStringUsAscii(Base64.decodeBase64("Zm9vYg==" + CRLF)));
565+
assertEquals("fooba", StringUtils.newStringUsAscii(Base64.decodeBase64("Zm9vYmE=" + CRLF)));
566+
assertEquals("foobar", StringUtils.newStringUsAscii(Base64.decodeBase64("Zm9vYmFy" + CRLF)));
566567
}
567568

568569
/**
@@ -589,6 +590,66 @@ public void testRfc4648Section10Encode() {
589590
//assertEquals("Zm9vYmFy", Base64.encodeBase64String(StringUtils.getBytesUtf8("foobar")));
590591
}
591592

593+
/**
594+
* Tests RFC 4648 section 10 test vectors.
595+
* <ul>
596+
* <li>BASE64("") = ""</li>
597+
* <li>BASE64("f") = "Zg=="</li>
598+
* <li>BASE64("fo") = "Zm8="</li>
599+
* <li>BASE64("foo") = "Zm9v"</li>
600+
* <li>BASE64("foob") = "Zm9vYg=="</li>
601+
* <li>BASE64("fooba") = "Zm9vYmE="</li>
602+
* <li>BASE64("foobar") = "Zm9vYmFy"</li>
603+
* </ul>
604+
*
605+
* @see <a href="http://tools.ietf.org/html/rfc4648">http://tools.ietf.org/html/rfc4648</a>
606+
*/
607+
public void testRfc4648Section10DecodeEncode() {
608+
testDecodeEncode("");
609+
//testDecodeEncode("Zg==");
610+
//testDecodeEncode("Zm8=");
611+
//testDecodeEncode("Zm9v");
612+
//testDecodeEncode("Zm9vYg==");
613+
//testDecodeEncode("Zm9vYmE=");
614+
//testDecodeEncode("Zm9vYmFy");
615+
}
616+
617+
private void testDecodeEncode(String encodedText) {
618+
String decodedText = StringUtils.newStringUsAscii(Base64.decodeBase64(encodedText));
619+
String encodedText2 = Base64.encodeBase64String(StringUtils.getBytesUtf8(decodedText));
620+
assertEquals(encodedText, encodedText2);
621+
}
622+
623+
/**
624+
* Tests RFC 4648 section 10 test vectors.
625+
* <ul>
626+
* <li>BASE64("") = ""</li>
627+
* <li>BASE64("f") = "Zg=="</li>
628+
* <li>BASE64("fo") = "Zm8="</li>
629+
* <li>BASE64("foo") = "Zm9v"</li>
630+
* <li>BASE64("foob") = "Zm9vYg=="</li>
631+
* <li>BASE64("fooba") = "Zm9vYmE="</li>
632+
* <li>BASE64("foobar") = "Zm9vYmFy"</li>
633+
* </ul>
634+
*
635+
* @see <a href="http://tools.ietf.org/html/rfc4648">http://tools.ietf.org/html/rfc4648</a>
636+
*/
637+
public void testRfc4648Section10EncodeDecode() {
638+
testEncodeDecode("");
639+
testEncodeDecode("f");
640+
testEncodeDecode("fo");
641+
testEncodeDecode("foo");
642+
testEncodeDecode("foob");
643+
testEncodeDecode("fooba");
644+
testEncodeDecode("foobar");
645+
}
646+
647+
private void testEncodeDecode(String plainText) {
648+
String encodedText = Base64.encodeBase64String(StringUtils.getBytesUtf8(plainText));
649+
String decodedText = StringUtils.newStringUsAscii(Base64.decodeBase64(encodedText));
650+
assertEquals(plainText, decodedText);
651+
}
652+
592653
public void testSingletons() {
593654
assertEquals("AA==", new String(Base64.encodeBase64(new byte[]{(byte) 0})));
594655
assertEquals("AQ==", new String(Base64.encodeBase64(new byte[]{(byte) 1})));

0 commit comments

Comments
 (0)