From 6416d49ef9b7ce4815d9ca2faf330c985654b8b8 Mon Sep 17 00:00:00 2001
From: Endi Sukma Dewata <edewata@redhat.com>
Date: Wed, 4 Jan 2012 20:30:41 -0600
Subject: [PATCH] Added unit tests for converting null characters.

New unit tests have been added to verify bug bug 359010.

Ticket #3
---
 .../com/netscape/security/util/BMPStringTest.java  |   34 +++++++++++++++++++
 .../com/netscape/security/util/IA5StringTest.java  |   34 +++++++++++++++++++
 .../security/util/PrintableStringTest.java         |   35 ++++++++++++++++++++
 .../com/netscape/security/util/StringTestUtil.java |    2 +
 .../netscape/security/util/TeletexStringTest.java  |   34 +++++++++++++++++++
 .../com/netscape/security/util/UTF8StringTest.java |   34 +++++++++++++++++++
 .../security/util/UniversalStringTest.java         |   34 +++++++++++++++++++
 .../security/x509/DirStrConverterTest.java         |   15 ++++++++
 .../security/x509/GenericValueConverterTest.java   |   15 ++++++++
 .../security/x509/IA5StringConverterTest.java      |   15 ++++++++
 .../security/x509/PrintableConverterTest.java      |   20 +++++++++++
 11 files changed, 272 insertions(+), 0 deletions(-)

diff --git a/pki/base/util/test/com/netscape/security/util/BMPStringTest.java b/pki/base/util/test/com/netscape/security/util/BMPStringTest.java
index e1dcb659163c15e9b6ebb14e116774051211cf22..e82ea50ce25db1825d5fb2b6f176a4597ce1293a 100644
--- a/pki/base/util/test/com/netscape/security/util/BMPStringTest.java
+++ b/pki/base/util/test/com/netscape/security/util/BMPStringTest.java
@@ -41,6 +41,40 @@ public class BMPStringTest {
     }
 
     @Test
+    public void testEncodingNullCharacters() throws Exception {
+
+        String string = StringTestUtil.NULL_CHARS;
+        System.out.println("Encoding: ["+StringTestUtil.toString(string.getBytes())+"]");
+
+        byte[] expected = JSSUtil.encode(tag, string);
+        System.out.println(" - expected: "+StringTestUtil.toString(expected));
+
+        byte[] actual = StringTestUtil.encode(tag, string);
+        System.out.println(" - actual  : "+StringTestUtil.toString(actual));
+
+        actual = StringTestUtil.normalizeUnicode(actual);
+        System.out.println(" - norm.   : "+StringTestUtil.toString(actual));
+
+        Assert.assertArrayEquals(expected, actual);
+    }
+
+    @Test
+    public void testDecodingNullCharacters() throws Exception {
+
+        String input = StringTestUtil.NULL_CHARS;
+        byte[] data = JSSUtil.encode(tag, input);
+
+        System.out.println("Decoding: ["+StringTestUtil.toString(data)+"]");
+
+        System.out.println(" - expected: ["+StringTestUtil.toString(input.getBytes())+"]");
+
+        String output = StringTestUtil.decode(tag, data);
+        System.out.println(" - actual  : ["+StringTestUtil.toString(output.getBytes())+"]");
+
+        Assert.assertEquals(input, output);
+    }
+
+    @Test
     public void testEncodingPrintableCharacters() throws Exception {
 
         String string = StringTestUtil.PRINTABLE_CHARS;
diff --git a/pki/base/util/test/com/netscape/security/util/IA5StringTest.java b/pki/base/util/test/com/netscape/security/util/IA5StringTest.java
index f101e549741e3a3f40df2d5dce1ca1225cad48ba..a8962459560094232753d882887cf99d9e6cb4a8 100644
--- a/pki/base/util/test/com/netscape/security/util/IA5StringTest.java
+++ b/pki/base/util/test/com/netscape/security/util/IA5StringTest.java
@@ -43,6 +43,40 @@ public class IA5StringTest {
     }
 
     @Test
+    public void testEncodingNullCharacters() throws Exception {
+
+        String string = StringTestUtil.NULL_CHARS;
+        System.out.println("Encoding: ["+StringTestUtil.toString(string.getBytes())+"]");
+
+        byte[] expected = JSSUtil.encode(tag, string);
+        System.out.println(" - expected: "+StringTestUtil.toString(expected));
+
+        byte[] actual = StringTestUtil.encode(tag, string);
+        System.out.println(" - actual  : "+StringTestUtil.toString(actual));
+
+        actual = StringTestUtil.normalizeUnicode(actual);
+        System.out.println(" - norm.   : "+StringTestUtil.toString(actual));
+
+        Assert.assertArrayEquals(expected, actual);
+    }
+
+    @Test
+    public void testDecodingNullCharacters() throws Exception {
+
+        String input = StringTestUtil.NULL_CHARS;
+        byte[] data = JSSUtil.encode(tag, input);
+
+        System.out.println("Decoding: ["+StringTestUtil.toString(data)+"]");
+
+        System.out.println(" - expected: ["+StringTestUtil.toString(input.getBytes())+"]");
+
+        String output = StringTestUtil.decode(tag, data);
+        System.out.println(" - actual  : ["+StringTestUtil.toString(output.getBytes())+"]");
+
+        Assert.assertEquals(input, output);
+    }
+
+    @Test
     public void testEncodingPrintableCharacters() throws Exception {
 
         String string = StringTestUtil.PRINTABLE_CHARS;
diff --git a/pki/base/util/test/com/netscape/security/util/PrintableStringTest.java b/pki/base/util/test/com/netscape/security/util/PrintableStringTest.java
index 4d0bc2cbf334110d54300fda4b4fad87a94e5020..07525648b1bbd41499227cf02f6772c58752fb6c 100644
--- a/pki/base/util/test/com/netscape/security/util/PrintableStringTest.java
+++ b/pki/base/util/test/com/netscape/security/util/PrintableStringTest.java
@@ -43,6 +43,41 @@ public class PrintableStringTest {
     }
 
     @Test
+    public void testEncodingNullCharacters() throws Exception {
+
+        String string = StringTestUtil.NULL_CHARS;
+        System.out.println("Encoding: ["+StringTestUtil.toString(string.getBytes())+"]");
+
+        System.out.println(" - expected: IOException");
+
+        try {
+            byte[] actual = StringTestUtil.encode(tag, string);
+            System.out.println(" - actual  : "+StringTestUtil.toString(actual));
+
+            Assert.fail();
+
+        } catch (Exception e) {
+            System.out.println(" - actual  : "+e.getClass().getSimpleName());
+            Assert.assertTrue(e instanceof IOException);
+        }
+    }
+
+    @Test
+    public void testDecodingNullCharacters() throws Exception {
+
+        byte[] data = { 0x13, 0x01, 0x00 };
+        System.out.println("Decoding: ["+StringTestUtil.toString(data)+"]");
+
+        String expected = ""; // skip null chars (bug 359010)
+        System.out.println(" - expected: ["+expected+"]");
+
+        String output = StringTestUtil.decode(tag, data);
+        System.out.println(" - actual  : ["+output+"]");
+
+        Assert.assertEquals(expected, output);
+    }
+
+    @Test
     public void testEncodingPrintableCharacters() throws Exception {
 
         String string = StringTestUtil.PRINTABLE_CHARS;
diff --git a/pki/base/util/test/com/netscape/security/util/StringTestUtil.java b/pki/base/util/test/com/netscape/security/util/StringTestUtil.java
index 211ba518316c49fa7ab952944dee967ed03704a1..cb536e7b3826d81a6fe3dead39724f3fe18033fe 100644
--- a/pki/base/util/test/com/netscape/security/util/StringTestUtil.java
+++ b/pki/base/util/test/com/netscape/security/util/StringTestUtil.java
@@ -6,6 +6,8 @@ import netscape.security.util.DerValue;
 
 public class StringTestUtil {
 
+    public final static String NULL_CHARS = "\u0000";
+
     public final static String PRINTABLE_CHARS =
         "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 \'()+,-./:=?";
 
diff --git a/pki/base/util/test/com/netscape/security/util/TeletexStringTest.java b/pki/base/util/test/com/netscape/security/util/TeletexStringTest.java
index 7217cae40c64691ef5437ede22af97bb69e8cf1a..92c61b9f9376dc41ac9fb7540c2640ea89ace984 100644
--- a/pki/base/util/test/com/netscape/security/util/TeletexStringTest.java
+++ b/pki/base/util/test/com/netscape/security/util/TeletexStringTest.java
@@ -43,6 +43,40 @@ public class TeletexStringTest {
     }
 
     @Test
+    public void testEncodingNullCharacters() throws Exception {
+
+        String string = StringTestUtil.NULL_CHARS;
+        System.out.println("Encoding: ["+StringTestUtil.toString(string.getBytes())+"]");
+
+        byte[] expected = JSSUtil.encode(tag, string);
+        System.out.println(" - expected: "+StringTestUtil.toString(expected));
+
+        byte[] actual = StringTestUtil.encode(tag, string);
+        System.out.println(" - actual  : "+StringTestUtil.toString(actual));
+
+        actual = StringTestUtil.normalizeUnicode(actual);
+        System.out.println(" - norm.   : "+StringTestUtil.toString(actual));
+
+        Assert.assertArrayEquals(expected, actual);
+    }
+
+    @Test
+    public void testDecodingNullCharacters() throws Exception {
+
+        String input = StringTestUtil.NULL_CHARS;
+        byte[] data = JSSUtil.encode(tag, input);
+
+        System.out.println("Decoding: ["+StringTestUtil.toString(data)+"]");
+
+        System.out.println(" - expected: ["+StringTestUtil.toString(input.getBytes())+"]");
+
+        String output = StringTestUtil.decode(tag, data);
+        System.out.println(" - actual  : ["+StringTestUtil.toString(output.getBytes())+"]");
+
+        Assert.assertEquals(input, output);
+    }
+
+    @Test
     public void testEncodingPrintableCharacters() throws Exception {
 
         String string = StringTestUtil.PRINTABLE_CHARS;
diff --git a/pki/base/util/test/com/netscape/security/util/UTF8StringTest.java b/pki/base/util/test/com/netscape/security/util/UTF8StringTest.java
index 3256a8e33b8805271e7093af90e6e66bd9add77d..48c20c49dbf363cbf8ec1edb50be48c63f50360a 100644
--- a/pki/base/util/test/com/netscape/security/util/UTF8StringTest.java
+++ b/pki/base/util/test/com/netscape/security/util/UTF8StringTest.java
@@ -41,6 +41,40 @@ public class UTF8StringTest {
     }
 
     @Test
+    public void testEncodingNullCharacters() throws Exception {
+
+        String string = StringTestUtil.NULL_CHARS;
+        System.out.println("Encoding: ["+StringTestUtil.toString(string.getBytes())+"]");
+
+        byte[] expected = JSSUtil.encode(tag, string);
+        System.out.println(" - expected: "+StringTestUtil.toString(expected));
+
+        byte[] actual = StringTestUtil.encode(tag, string);
+        System.out.println(" - actual  : "+StringTestUtil.toString(actual));
+
+        actual = StringTestUtil.normalizeUnicode(actual);
+        System.out.println(" - norm.   : "+StringTestUtil.toString(actual));
+
+        Assert.assertArrayEquals(expected, actual);
+    }
+
+    @Test
+    public void testDecodingNullCharacters() throws Exception {
+
+        String input = StringTestUtil.NULL_CHARS;
+        byte[] data = JSSUtil.encode(tag, input);
+
+        System.out.println("Decoding: ["+StringTestUtil.toString(data)+"]");
+
+        System.out.println(" - expected: ["+StringTestUtil.toString(input.getBytes())+"]");
+
+        String output = StringTestUtil.decode(tag, data);
+        System.out.println(" - actual  : ["+StringTestUtil.toString(output.getBytes())+"]");
+
+        Assert.assertEquals(input, output);
+    }
+
+    @Test
     public void testEncodingPrintableCharacters() throws Exception {
 
         String string = StringTestUtil.PRINTABLE_CHARS;
diff --git a/pki/base/util/test/com/netscape/security/util/UniversalStringTest.java b/pki/base/util/test/com/netscape/security/util/UniversalStringTest.java
index aceae03b3067254a3132abfff25c07f10e58ac73..1e2c7efe0f2d121fa2c6b08d99443f258f105e07 100644
--- a/pki/base/util/test/com/netscape/security/util/UniversalStringTest.java
+++ b/pki/base/util/test/com/netscape/security/util/UniversalStringTest.java
@@ -41,6 +41,40 @@ public class UniversalStringTest {
     }
 
     @Test
+    public void testEncodingNullCharacters() throws Exception {
+
+        String string = StringTestUtil.NULL_CHARS;
+        System.out.println("Encoding: ["+StringTestUtil.toString(string.getBytes())+"]");
+
+        byte[] expected = JSSUtil.encode(tag, string);
+        System.out.println(" - expected: "+StringTestUtil.toString(expected));
+
+        byte[] actual = StringTestUtil.encode(tag, string);
+        System.out.println(" - actual  : "+StringTestUtil.toString(actual));
+
+        actual = StringTestUtil.normalizeUnicode(actual);
+        System.out.println(" - norm.   : "+StringTestUtil.toString(actual));
+
+        Assert.assertArrayEquals(expected, actual);
+    }
+
+    @Test
+    public void testDecodingNullCharacters() throws Exception {
+
+        String input = StringTestUtil.NULL_CHARS;
+        byte[] data = JSSUtil.encode(tag, input);
+
+        System.out.println("Decoding: ["+StringTestUtil.toString(data)+"]");
+
+        System.out.println(" - expected: ["+StringTestUtil.toString(input.getBytes())+"]");
+
+        String output = StringTestUtil.decode(tag, data);
+        System.out.println(" - actual  : ["+StringTestUtil.toString(output.getBytes())+"]");
+
+        Assert.assertEquals(input, output);
+    }
+
+    @Test
     public void testEncodingPrintableCharacters() throws Exception {
 
         String string = StringTestUtil.PRINTABLE_CHARS;
diff --git a/pki/base/util/test/com/netscape/security/x509/DirStrConverterTest.java b/pki/base/util/test/com/netscape/security/x509/DirStrConverterTest.java
index ec8ddc51b15171269135518fb3e4a7dfce54fdb5..2abf057143550a8854fdf6085a28dd6973252499 100644
--- a/pki/base/util/test/com/netscape/security/x509/DirStrConverterTest.java
+++ b/pki/base/util/test/com/netscape/security/x509/DirStrConverterTest.java
@@ -27,6 +27,21 @@ public class DirStrConverterTest {
     }
 
     @Test
+    public void testNullCharacters() throws Exception {
+
+        String string = StringTestUtil.NULL_CHARS;
+        System.out.println("Converting: ["+StringTestUtil.toString(string.getBytes())+"]");
+
+        byte[] expected = JSSUtil.encode(DerValue.tag_T61String, string);
+        System.out.println(" - expected: "+StringTestUtil.toString(expected));
+
+        byte[] actual = ConverterTestUtil.convert(new DirStrConverter(), string);
+        System.out.println(" - actual  : "+StringTestUtil.toString(actual));
+
+        Assert.assertArrayEquals(expected, actual);
+    }
+
+    @Test
     public void testPrintableCharacters() throws Exception {
 
         String string = StringTestUtil.PRINTABLE_CHARS;
diff --git a/pki/base/util/test/com/netscape/security/x509/GenericValueConverterTest.java b/pki/base/util/test/com/netscape/security/x509/GenericValueConverterTest.java
index bee2b241fff84d619df5bb0bf10dd622a0479a69..1962d1bfa99dd3db6a1a03a0eafa5cc41d799437 100644
--- a/pki/base/util/test/com/netscape/security/x509/GenericValueConverterTest.java
+++ b/pki/base/util/test/com/netscape/security/x509/GenericValueConverterTest.java
@@ -27,6 +27,21 @@ public class GenericValueConverterTest {
     }
 
     @Test
+    public void testNullCharacters() throws Exception {
+
+        String string = StringTestUtil.NULL_CHARS;
+        System.out.println("Converting: ["+StringTestUtil.toString(string.getBytes())+"]");
+
+        byte[] expected = JSSUtil.encode(DerValue.tag_IA5String, string);
+        System.out.println(" - expected: "+StringTestUtil.toString(expected));
+
+        byte[] actual = ConverterTestUtil.convert(new GenericValueConverter(), string);
+        System.out.println(" - actual  : "+StringTestUtil.toString(actual));
+
+        Assert.assertArrayEquals(expected, actual);
+    }
+
+    @Test
     public void testPrintableCharacters() throws Exception {
 
         String string = StringTestUtil.PRINTABLE_CHARS;
diff --git a/pki/base/util/test/com/netscape/security/x509/IA5StringConverterTest.java b/pki/base/util/test/com/netscape/security/x509/IA5StringConverterTest.java
index 682e7384fba0b01ca6bd57f56782ae6a9055f029..a7361ccbfa6a43ea208e1426542df1a01ed78b5f 100644
--- a/pki/base/util/test/com/netscape/security/x509/IA5StringConverterTest.java
+++ b/pki/base/util/test/com/netscape/security/x509/IA5StringConverterTest.java
@@ -27,6 +27,21 @@ public class IA5StringConverterTest {
     }
 
     @Test
+    public void testNullCharacters() throws Exception {
+
+        String string = StringTestUtil.NULL_CHARS;
+        System.out.println("Converting: ["+StringTestUtil.toString(string.getBytes())+"]");
+
+        byte[] expected = JSSUtil.encode(DerValue.tag_IA5String, string);
+        System.out.println(" - expected: "+StringTestUtil.toString(expected));
+
+        byte[] actual = ConverterTestUtil.convert(new IA5StringConverter(), string);
+        System.out.println(" - actual  : "+StringTestUtil.toString(actual));
+
+        Assert.assertArrayEquals(expected, actual);
+    }
+
+    @Test
     public void testPrintableCharacters() throws Exception {
 
         String string = StringTestUtil.PRINTABLE_CHARS;
diff --git a/pki/base/util/test/com/netscape/security/x509/PrintableConverterTest.java b/pki/base/util/test/com/netscape/security/x509/PrintableConverterTest.java
index acaa5c1b8981dd7cdfaaaed11c4551a4f985cef6..e85d11824a1aa0e5019e082af2ea8873f209f568 100644
--- a/pki/base/util/test/com/netscape/security/x509/PrintableConverterTest.java
+++ b/pki/base/util/test/com/netscape/security/x509/PrintableConverterTest.java
@@ -27,6 +27,26 @@ public class PrintableConverterTest {
     }
 
     @Test
+    public void testNullCharacters() throws Exception {
+
+        String string = StringTestUtil.NULL_CHARS;
+        System.out.println("Converting: ["+StringTestUtil.toString(string.getBytes())+"]");
+
+        System.out.println(" - expected: IllegalArgumentException");
+
+        try {
+            byte[] actual = ConverterTestUtil.convert(new PrintableConverter(), string);
+            System.out.println(" - actual  : "+StringTestUtil.toString(actual));
+
+            Assert.fail();
+
+        } catch (Exception e) {
+            System.out.println(" - actual  : "+e.getClass().getSimpleName());
+            Assert.assertTrue(e instanceof IllegalArgumentException);
+        }
+    }
+
+    @Test
     public void testPrintableCharacters() throws Exception {
 
         String string = StringTestUtil.PRINTABLE_CHARS;
-- 
1.7.7.4

