01: package org.bouncycastle.asn1.test;
02:
03: import org.bouncycastle.asn1.DEROctetString;
04: import org.bouncycastle.asn1.DERUTF8String;
05: import org.bouncycastle.util.Arrays;
06: import org.bouncycastle.util.test.SimpleTestResult;
07: import org.bouncycastle.util.test.Test;
08: import org.bouncycastle.util.test.TestResult;
09:
10: public class DERUTF8StringTest implements Test {
11:
12: /**
13: * Unicode code point U+10400 coded as surrogate in two native Java UTF-16
14: * code units
15: */
16: private final static char[] glyph1_utf16 = { 0xd801, 0xdc00 };
17:
18: /**
19: * U+10400 coded in UTF-8
20: */
21: private final static byte[] glyph1_utf8 = { (byte) 0xF0,
22: (byte) 0x90, (byte) 0x90, (byte) 0x80 };
23:
24: /**
25: * Unicode code point U+6771 in native Java UTF-16
26: */
27: private final static char[] glyph2_utf16 = { 0x6771 };
28:
29: /**
30: * U+6771 coded in UTF-8
31: */
32: private final static byte[] glyph2_utf8 = { (byte) 0xE6,
33: (byte) 0x9D, (byte) 0xB1 };
34:
35: /**
36: * Unicode code point U+00DF in native Java UTF-16
37: */
38: private final static char[] glyph3_utf16 = { 0x00DF };
39:
40: /**
41: * U+00DF coded in UTF-8
42: */
43: private final static byte[] glyph3_utf8 = { (byte) 0xC3,
44: (byte) 0x9f };
45:
46: /**
47: * Unicode code point U+0041 in native Java UTF-16
48: */
49: private final static char[] glyph4_utf16 = { 0x0041 };
50:
51: /**
52: * U+0041 coded in UTF-8
53: */
54: private final static byte[] glyph4_utf8 = { 0x41 };
55:
56: private final static byte[][] glyphs_utf8 = { glyph1_utf8,
57: glyph2_utf8, glyph3_utf8, glyph4_utf8 };
58:
59: private final static char[][] glyphs_utf16 = { glyph1_utf16,
60: glyph2_utf16, glyph3_utf16, glyph4_utf16 };
61:
62: public TestResult perform() {
63: try {
64: for (int i = 0; i < glyphs_utf16.length; i++) {
65: String s = new String(glyphs_utf16[i]);
66: byte[] b1 = new DERUTF8String(s).getEncoded();
67: byte temp[] = new byte[b1.length - 2];
68: System.arraycopy(b1, 2, temp, 0, b1.length - 2);
69: byte[] b2 = DERUTF8String.getInstance(
70: new DEROctetString(temp)).getEncoded();
71: if (!Arrays.areEqual(b1, b2)) {
72: return new SimpleTestResult(false, getName()
73: + ": failed UTF-8 encoding and decoding");
74: }
75: if (!Arrays.areEqual(temp, glyphs_utf8[i])) {
76: return new SimpleTestResult(false, getName()
77: + ": failed UTF-8 encoding and decoding");
78: }
79: }
80: } catch (Exception e) {
81: return new SimpleTestResult(false, getName()
82: + ": failed with Exception " + e.getMessage());
83: }
84:
85: return new SimpleTestResult(true, getName() + ": Okay");
86: }
87:
88: public String getName() {
89: return "DERUTF8String";
90: }
91:
92: public static void main(String[] args) {
93: DERUTF8StringTest test = new DERUTF8StringTest();
94: TestResult result = test.perform();
95:
96: System.out.println(result);
97: }
98: }
|