summary refs log tree commit diff
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2020-04-05 18:34:12 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2020-04-05 18:34:12 +0700
commitb825f8f5fb199bd163a4ffe2cd4e999e4a0ba045 (patch)
tree864b9f257a6174b461b4952d6aefccd6ba802296
parentImprove SigAlgName for certs/CRLs (diff)
downloadBouncyCastle.NET-ed25519-b825f8f5fb199bd163a4ffe2cd4e999e4a0ba045.tar.xz
Support headers with multiple values
- tidy up test names
-rw-r--r--crypto/src/bcpg/ArmoredOutputStream.cs76
-rw-r--r--crypto/test/src/openpgp/test/PGPArmoredTest.cs32
-rw-r--r--crypto/test/src/openpgp/test/PGPDSATest.cs2
-rw-r--r--crypto/test/src/openpgp/test/PGPNoPrivateKeyTest.cs2
-rw-r--r--crypto/test/src/openpgp/test/PGPPBETest.cs2
-rw-r--r--crypto/test/src/openpgp/test/PGPPacketTest.cs2
-rw-r--r--crypto/test/src/openpgp/test/PGPRSATest.cs2
-rw-r--r--crypto/test/src/openpgp/test/PGPSignatureTest.cs2
8 files changed, 95 insertions, 25 deletions
diff --git a/crypto/src/bcpg/ArmoredOutputStream.cs b/crypto/src/bcpg/ArmoredOutputStream.cs
index 82e7cc1f0..0df5d1141 100644
--- a/crypto/src/bcpg/ArmoredOutputStream.cs
+++ b/crypto/src/bcpg/ArmoredOutputStream.cs
@@ -111,35 +111,68 @@ namespace Org.BouncyCastle.Bcpg
         {
             this.outStream = outStream;
             this.headers = Platform.CreateHashtable(1);
-            this.headers.Add(HeaderVersion, Version);
+            SetHeader(HeaderVersion, Version);
         }
 
         public ArmoredOutputStream(Stream outStream, IDictionary headers)
+            : this(outStream)
         {
-            this.outStream = outStream;
-            this.headers = Platform.CreateHashtable(headers);
-            if (!this.headers.Contains(HeaderVersion))
+            foreach (string header in headers.Keys)
             {
-                this.headers.Add(HeaderVersion, Version);
+                IList headerList = Platform.CreateArrayList(1);
+                headerList.Add(headers[header]);
+
+                this.headers[header] = headerList;
             }
         }
 
         /**
-         * Set an additional header entry. A null value will clear the entry for name.
-         *
+         * Set an additional header entry. Any current value(s) under the same name will be
+         * replaced by the new one. A null value will clear the entry for name.         *
          * @param name the name of the header entry.
          * @param v the value of the header entry.
          */
-        public void SetHeader(string name, string v)
+        public void SetHeader(string name, string val)
         {
-            if (v == null)
+            if (val == null)
             {
-                headers.Remove(name);
+                this.headers.Remove(name);
             }
             else
             {
-                headers[name] = v;
+                IList valueList = (IList)headers[name];
+                if (valueList == null)
+                {
+                    valueList = Platform.CreateArrayList(1);
+                    this.headers[name] = valueList;
+                }
+                else
+                {
+                    valueList.Clear();
+                }
+                valueList.Add(val);
+            }
+        }
+
+        /**
+         * Set an additional header entry. The current value(s) will continue to exist together
+         * with the new one. Adding a null value has no effect.
+         *
+         * @param name the name of the header entry.
+         * @param value the value of the header entry.
+         */
+        public void AddHeader(string name, string val)
+        {
+            if (val == null || name == null)
+                return;
+
+            IList valueList = (IList)headers[name];
+            if (valueList == null)
+            {
+                valueList = Platform.CreateArrayList(1);
+                this.headers[name] = valueList;
             }
+            valueList.Add(val);
         }
 
         /**
@@ -147,13 +180,13 @@ namespace Org.BouncyCastle.Bcpg
          */
         public void ResetHeaders()
         {
-            string existingVersion = (string)headers[HeaderVersion];
+            IList versions = (IList)headers[HeaderVersion];
 
             headers.Clear();
 
-            if (existingVersion != null)
+            if (versions != null)
             {
-                headers.Add(HeaderVersion, existingVersion);
+                headers[HeaderVersion] = versions;
             }
         }
 
@@ -264,9 +297,13 @@ namespace Org.BouncyCastle.Bcpg
                 }
 
                 DoWrite(headerStart + type + headerTail + nl);
-                if (headers.Contains(HeaderVersion))
+
                 {
-                    WriteHeaderEntry(HeaderVersion, (string)headers[HeaderVersion]);
+                    IList versionHeaders = (IList)headers[HeaderVersion];
+                    if (versionHeaders != null)
+                    {
+                        WriteHeaderEntry(HeaderVersion, versionHeaders[0].ToString());
+                    }
                 }
 
                 foreach (DictionaryEntry de in headers)
@@ -274,8 +311,11 @@ namespace Org.BouncyCastle.Bcpg
                     string k = (string)de.Key;
                     if (k != HeaderVersion)
                     {
-                        string v = (string)de.Value;
-                        WriteHeaderEntry(k, v);
+                        IList values = (IList)de.Value;
+                        foreach (string v in values)
+                        {
+                            WriteHeaderEntry(k, v);
+                        }
                     }
                 }
 
diff --git a/crypto/test/src/openpgp/test/PGPArmoredTest.cs b/crypto/test/src/openpgp/test/PGPArmoredTest.cs
index e48827a35..f78165d5a 100644
--- a/crypto/test/src/openpgp/test/PGPArmoredTest.cs
+++ b/crypto/test/src/openpgp/test/PGPArmoredTest.cs
@@ -124,6 +124,35 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp.Tests
 			}
 		}
 
+        private void repeatHeaderTest()
+        {
+            MemoryStream bOut = new MemoryStream();
+            ArmoredOutputStream aOut = new ArmoredOutputStream(bOut);
+
+            aOut.SetHeader("Comment", "Line 1");
+            aOut.AddHeader("Comment", "Line 2");
+
+            aOut.Write(sample, 0, sample.Length);
+
+            aOut.Close();
+
+            MemoryStream bIn = new MemoryStream(bOut.ToArray(), false);
+            ArmoredInputStream aIn = new ArmoredInputStream(bIn, true);
+
+            string[] hdrs = aIn.GetArmorHeaders();
+            int count = 0;
+
+            for (int i = 0; i != hdrs.Length; i++)
+            {
+                if (hdrs[i].IndexOf("Comment: ") == 0)
+                {
+                    count++;
+                }
+            }
+
+            IsEquals(2, count);
+        }
+
 		public override void PerformTest()
 		{
 			//
@@ -258,7 +287,8 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp.Tests
 
 			blankLineTest();
             pgpUtilTest();
-		}
+            repeatHeaderTest();
+        }
 
 		public override string Name
 		{
diff --git a/crypto/test/src/openpgp/test/PGPDSATest.cs b/crypto/test/src/openpgp/test/PGPDSATest.cs
index 7808ed6cd..e1dc384db 100644
--- a/crypto/test/src/openpgp/test/PGPDSATest.cs
+++ b/crypto/test/src/openpgp/test/PGPDSATest.cs
@@ -577,7 +577,7 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp.Tests
 
 		public override string Name
         {
-			get { return "PGPDSATest"; }
+			get { return "PgpDsaTest"; }
         }
 
 		public static void Main(
diff --git a/crypto/test/src/openpgp/test/PGPNoPrivateKeyTest.cs b/crypto/test/src/openpgp/test/PGPNoPrivateKeyTest.cs
index 8f702b67b..222b50a4b 100644
--- a/crypto/test/src/openpgp/test/PGPNoPrivateKeyTest.cs
+++ b/crypto/test/src/openpgp/test/PGPNoPrivateKeyTest.cs
@@ -149,7 +149,7 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp.Tests
 
         public override string Name
         {
-            get { return "PGPNoPrivateKeyTest"; }
+            get { return "PgpNoPrivateKeyTest"; }
         }
 
         public static void Main(
diff --git a/crypto/test/src/openpgp/test/PGPPBETest.cs b/crypto/test/src/openpgp/test/PGPPBETest.cs
index 29b786a83..eee3aaa63 100644
--- a/crypto/test/src/openpgp/test/PGPPBETest.cs
+++ b/crypto/test/src/openpgp/test/PGPPBETest.cs
@@ -364,7 +364,7 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp.Tests
 
 		public override string Name
         {
-			get { return "PGPPBETest"; }
+			get { return "PgpPbeTest"; }
         }
 
 		public static void Main(
diff --git a/crypto/test/src/openpgp/test/PGPPacketTest.cs b/crypto/test/src/openpgp/test/PGPPacketTest.cs
index b82f8526f..b3dbbc2ed 100644
--- a/crypto/test/src/openpgp/test/PGPPacketTest.cs
+++ b/crypto/test/src/openpgp/test/PGPPacketTest.cs
@@ -60,7 +60,7 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp.Tests
 
 		public override string Name
         {
-			get { return "PGPPacketTest"; }
+			get { return "PgpPacketTest"; }
         }
 
 		public static void Main(
diff --git a/crypto/test/src/openpgp/test/PGPRSATest.cs b/crypto/test/src/openpgp/test/PGPRSATest.cs
index 82b569bbb..be111c958 100644
--- a/crypto/test/src/openpgp/test/PGPRSATest.cs
+++ b/crypto/test/src/openpgp/test/PGPRSATest.cs
@@ -1213,7 +1213,7 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp.Tests
 
         public override string Name
         {
-            get { return "PGPRSATest"; }
+            get { return "PgpRsaTest"; }
         }
 
         public static void Main(
diff --git a/crypto/test/src/openpgp/test/PGPSignatureTest.cs b/crypto/test/src/openpgp/test/PGPSignatureTest.cs
index af490193a..d2f4a8a31 100644
--- a/crypto/test/src/openpgp/test/PGPSignatureTest.cs
+++ b/crypto/test/src/openpgp/test/PGPSignatureTest.cs
@@ -1066,7 +1066,7 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp.Tests
 
         public override string Name
         {
-            get { return "PGPSignatureTest"; }
+            get { return "PgpSignatureTest"; }
         }
 
         public static void Main(