Skip to content

Commit 092c227

Browse files
author
Roger Riggs
committed
8252523: Add ASN.1 Formatter to work with test utility HexPrinter
Reviewed-by: weijun
1 parent 06d8cf6 commit 092c227

File tree

5 files changed

+797
-10
lines changed

5 files changed

+797
-10
lines changed
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/*
2+
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
package jdk.test.lib.hexdump;
25+
26+
import org.testng.Assert;
27+
import org.testng.annotations.Test;
28+
29+
import java.io.DataInputStream;
30+
import java.io.EOFException;
31+
import java.io.IOException;
32+
import java.io.InputStream;
33+
import java.nio.file.Files;
34+
import java.nio.file.Path;
35+
import java.util.Base64;
36+
37+
import static org.testng.Assert.*;
38+
39+
/*
40+
* @test
41+
* @summary ASN.1 formatting
42+
* @library /test/lib
43+
* @compile ASN1FormatterTest.java
44+
* @run testng jdk.test.lib.hexdump.ASN1FormatterTest
45+
*/
46+
@Test
47+
public class ASN1FormatterTest {
48+
private static final String DIR = System.getProperty("test.src", ".");
49+
50+
@Test
51+
static void testPEM() throws IOException {
52+
String certFile = "openssl.p12.pem";
53+
Path certPath = Path.of(DIR, certFile);
54+
System.out.println("certPath: " + certPath);
55+
56+
try (InputStream certStream = Files.newInputStream(certPath)) {
57+
while (certStream.read() != '\n') {
58+
// Skip first line "-----BEGIN CERTIFICATE-----"
59+
}
60+
// Mime decoder for Certificate
61+
InputStream wis = Base64.getMimeDecoder().wrap(certStream);
62+
DataInputStream is = new DataInputStream(wis);
63+
String result = ASN1Formatter.formatter().annotate(is);
64+
System.out.println(result);
65+
66+
Assert.assertEquals(result.lines().count(), 76, "Lines");
67+
Assert.assertEquals(result.lines().filter(s -> s.contains("SEQUENCE")).count(),24, "Sequences");
68+
Assert.assertEquals(result.lines().filter(s -> s.contains("OBJECT ID")).count(), 17, "ObjectIDs");
69+
Assert.assertEquals(result.lines().filter(s -> s.contains("UTCTIME")).count(), 2, "UTCTIME");
70+
Assert.assertEquals(result.lines().filter(s -> s.contains("BIT STRING")).count(), 3, "BitStrings");
71+
} catch (EOFException eof) {
72+
// done
73+
}
74+
}
75+
76+
@Test
77+
static void dumpPEM() throws IOException {
78+
String file = "openssl.p12.pem";
79+
Path path = Path.of(DIR, file);
80+
System.out.println("path: " + path);
81+
82+
try (InputStream certStream = Files.newInputStream(path)) {
83+
while (certStream.read() != '\n') {
84+
// Skip first line "-----BEGIN CERTIFICATE-----"
85+
}
86+
// Mime decoder for Certificate
87+
InputStream wis = Base64.getMimeDecoder().wrap(certStream);
88+
89+
HexPrinter p = HexPrinter.simple()
90+
.formatter(ASN1Formatter.formatter(), "; ", 100);
91+
String result = p.toString(wis);
92+
System.out.println(result);
93+
94+
Assert.assertEquals(result.lines().count(), 126, "Lines");
95+
Assert.assertEquals(result.lines().filter(s -> s.contains("SEQUENCE")).count(), 24, "Sequences");
96+
Assert.assertEquals(result.lines().filter(s -> s.contains("OBJECT ID")).count(), 17, "ObjectIDs");
97+
Assert.assertEquals(result.lines().filter(s -> s.contains("UTCTIME")).count(), 2, "UTCTIME");
98+
Assert.assertEquals(result.lines().filter(s -> s.contains("BIT STRING")).count(), 3, "BitStrings");
99+
} catch (EOFException eof) {
100+
// done
101+
}
102+
}
103+
104+
@Test
105+
static void testIndefinate() {
106+
byte[] bytes = {0x24, (byte) 0x80, 4, 2, 'a', 'b', 4, 2, 'c', 'd', 0, 0};
107+
HexPrinter p = HexPrinter.simple()
108+
.formatter(ASN1Formatter.formatter(), "; ", 100);
109+
String result = p.toString(bytes);
110+
System.out.println(result);
111+
112+
Assert.assertEquals(result.lines().filter(s -> s.contains("OCTET STRING [INDEFINITE]")).count(),
113+
1, "Indefinite length");
114+
Assert.assertEquals(result.lines().filter(s -> s.contains("; OCTET STRING [2]")).count(),
115+
2, "Octet Sequences");
116+
Assert.assertEquals(result.lines().filter(s -> s.contains("; END-OF-CONTENT")).count(),
117+
1, "end of content");
118+
}
119+
120+
@Test
121+
static void testMain() {
122+
String file = "openssl.p12.pem";
123+
Path path = Path.of(DIR, file);
124+
String[] args = { path.toString() };
125+
System.out.println("path: " + path);
126+
ASN1Formatter.main(args);
127+
}
128+
129+
}

test/lib-test/jdk/test/lib/hexdump/HexPrinterTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ Object[][] builtinParams() {
7878
return new Object[][]{
7979
{"minimal", "", "%02x", 16, "", 64, HexPrinter.Formatters.NONE, ""},
8080
{"canonical", "%08x ", "%02x ", 16, "|", 31, HexPrinter.Formatters.PRINTABLE, "|" + System.lineSeparator()},
81-
{"simple", "%5d: ", "%02x ", 16, " // ", 64, HexPrinter.Formatters.ASCII, System.lineSeparator()},
81+
{"simple", "%04x: ", "%02x ", 16, " // ", 64, HexPrinter.Formatters.ASCII, System.lineSeparator()},
8282
{"source", " ", "(byte)%3d, ", 8, " // ", 64, HexPrinter.Formatters.PRINTABLE, System.lineSeparator()},
8383
};
8484
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
-----BEGIN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