package org.bouncycastle.openpgp.test;

import org.bouncycastle.bcpg.ArmoredOutputStream;
import org.bouncycastle.util.test.SimpleTest;

import java.io.ByteArrayOutputStream;
import java.nio.charset.Charset;

public class ArmoredOutputStreamUTF8Test extends SimpleTest {

    private static final Charset utf8 = Charset.forName("UTF8");

    public static void main(String[] args) {
        runTest(new ArmoredOutputStreamUTF8Test());
    }

    @Override
    public String getName() {
        return ArmoredOutputStreamUTF8Test.class.getSimpleName();
    }

    @Override
    public void performTest() throws Exception {
        String utf8WithUmlauts = "ÄÖÜß&Umlauts";

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ArmoredOutputStream armorOut = new ArmoredOutputStream(out);
        armorOut.setHeader("Comment", utf8WithUmlauts);

        armorOut.write("Foo\nBar".getBytes(utf8));

        armorOut.close();

        String armoredOutput = out.toString();
        String[] lines = armoredOutput.split("\n");
        String comment = null;
        for (String line : lines) {
            if (line.startsWith("Comment: ")) {
                comment = line.substring("Comment: ".length());
                break;
            }
        }

        isTrue("We did not find the comment line. This MUST never happen.", comment != null);
        isEquals("Comment was not properly encoded. Expected: "  +utf8WithUmlauts + ", Actual: " + comment, comment, utf8WithUmlauts);
    }
}

@@ -29,24 +29,73 @@
 npgp.test;%0A%0A
+import org.bouncycastle.bcpg.ArmoredInputStream;%0A
 import org.b
@@ -124,32 +124,32 @@
 edOutputStream;%0A
-
 import org.bounc
@@ -175,24 +175,61 @@
 impleTest;%0A%0A
+import java.io.ByteArrayInputStream;%0A
 import java.
@@ -1064,24 +1064,243 @@
 toString();%0A
+        ByteArrayInputStream bytesIn = new ByteArrayInputStream(armoredOutput.getBytes(utf8));%0A        ArmoredInputStream armorIn = new ArmoredInputStream(bytesIn);%0A        String%5B%5D header = armorIn.getArmorHeaders();%0A%0A
         Stri
@@ -1660,16 +1660,16 @@
  null);%0A
-
         
@@ -1794,16 +1794,158 @@
 mlauts);
+%0A%0A        // round-tripped comment from ascii armor input stream%0A        isEquals(header%5B1%5D.substring(%22Comment: %22.length()), utf8WithUmlauts);
 %0A    %7D%0A%7D

Add test for comment round-tripping