package org.bouncycastle.openpgp.test;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

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

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

    public String getName()
    {
        return "ArmoredOutputStreamUTF8Test";
    }

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

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

        armorOut.write(Strings.toUTF8ByteArray("Foo\nBar"));

        armorOut.close();

        String armoredOutput = out.toString();
        ByteArrayInputStream bytesIn = new ByteArrayInputStream(Strings.toUTF8ByteArray(armoredOutput));
        ArmoredInputStream armorIn = new ArmoredInputStream(bytesIn);
        String[] header = armorIn.getArmorHeaders();

        String[] lines = armoredOutput.split("\n");
        String comment = null;
        for (int i = 0; i != lines.length; i++)
        {
            String line = lines[i];
            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);

        // round-tripped comment from ascii armor input stream
        isEquals(header[1].substring("Comment: ".length()), utf8WithUmlauts);
    }
}

@@ -33,16 +33,47 @@
 .test;%0A%0A
+import java.io.BufferedReader;%0A
 import j
@@ -138,16 +138,78 @@
 tStream;
+%0Aimport java.io.IOException;%0Aimport java.io.InputStreamReader;
 %0A%0Aimport
@@ -704,24 +704,82 @@
 ption%0A    %7B%0A
+        // Hex.decode(%22c384c396c39cc39f26556d6c61757473%22)%0A
         Stri
@@ -998,33 +998,32 @@
 f8WithUmlauts);%0A
-%0A
         armorOut
@@ -1067,17 +1067,16 @@
 Bar%22));%0A
-%0A
         
@@ -1102,22 +1102,22 @@
         
-String
+byte%5B%5D
  armored
@@ -1122,16 +1122,20 @@
 edOutput
+UTF8
  = out.t
@@ -1139,26 +1139,30 @@
 t.to
-String
+ByteArray
 ();%0A
+%0A
         
 Byte
@@ -1161,87 +1161,96 @@
     
-ByteArrayInputStream bytesIn = new ByteArrayInputStream(Strings.toUTF8ByteArray
+String comment = findComment(armoredOutputUTF8);%0A        String%5B%5D headers = parseHeaders
 (arm
@@ -1259,28 +1259,32 @@
 edOutput
-)
+UTF8
 );%0A
+%0A
         
 ArmoredI
@@ -1279,78 +1279,238 @@
     
-ArmoredInputStream armorIn = new ArmoredInputStream(bytesIn
+isTrue(%22We did not find the comment line. This MUST never happen.%22, comment != null);%0A        isEquals(%22Comment was not properly encoded. Expected: %22 + utf8WithUmlauts + %22, Actual: %22 + comment, comment, utf8WithUmlauts
 );%0A
+%0A
         
 Stri
@@ -1509,230 +1509,472 @@
     
-String%5B%5D header = armorIn.getArmorHeaders(
+// round-tripped comment from ascii armor input stream%0A        isEquals(headers%5B1%5D.substring(%22Comment: %22.length()), utf8WithUmlauts
 );%0A
-%0A
     
+%7D%0A%0A
     
-String%5B%5D lines = armoredOutput.split(%22%5Cn%22);%0A        String comment = null;%0A        for (int i = 0; i != lines.length; i++)%0A        %7B%0A            String line = lines%5Bi%5D;
+private String findComment(byte%5B%5D armoredOutputUTF8)%0A        throws IOException%0A    %7B%0A        BufferedReader br = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(armoredOutputUTF8), %22UTF-8%22));%0A        String comment = null;%0A        String line;%0A        while ((line = br.readLine()) != null)%0A        %7B
 %0A   
@@ -2141,25 +2141,24 @@
       %7D%0A
-%0A
         
 isTrue(%22
@@ -2153,238 +2153,228 @@
     
-isTrue(%22We did not find the comment line. This MUST never happen.%22, comment != null)
+br.close();%0A        return comment
 ;%0A    
+%7D%0A%0A
     
-isEquals(%22Comment was not properly encoded. Expected: %22 + utf8WithUmlauts + %22, Actual: %22 + comment, comment, utf8WithUmla
+private String%5B%5D parseHeaders(byte%5B%5D armoredOutput)%0A        throws IOException%0A    %7B%0A        ByteArrayInputStream bytesIn = new ByteArrayInputStream(armoredOutp
 ut
-s
 );%0A
-%0A
         
 // r
@@ -2373,139 +2373,169 @@
     
-// round-tripped comment from ascii a
+ArmoredInputStream armorIn = new A
 rmor
- i
+edI
 nput
- s
+S
 tream
-%0A        isEquals(header%5B1%5D.substring(%22Comment: %22.length()), utf8WithUmlauts)
+(bytesIn);%0A        String%5B%5D header = armorIn.getArmorHeaders();%0A        armorIn.close();%0A        return header
 ;%0A  

Fix platform-dependent test code