From 0bcc057e741af3fbc108f42b75f9d42f48f6a51e Mon Sep 17 00:00:00 2001 From: Yuuta Liang Date: Sat, 14 Oct 2023 05:12:06 +0800 Subject: Implement the CA Signed-off-by: Yuuta Liang --- src/test/ui/UtilsTest.java | 67 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 src/test/ui/UtilsTest.java (limited to 'src/test/ui/UtilsTest.java') diff --git a/src/test/ui/UtilsTest.java b/src/test/ui/UtilsTest.java new file mode 100644 index 0000000..a7b4a52 --- /dev/null +++ b/src/test/ui/UtilsTest.java @@ -0,0 +1,67 @@ +package ui; + +import model.asn1.exceptions.ParseException; +import org.junit.jupiter.api.Test; + +import java.nio.charset.StandardCharsets; +import java.util.stream.IntStream; + +import static org.junit.jupiter.api.Assertions.*; + +public class UtilsTest { + @Test + void testByteToByte() { + final byte[] primitive = new byte[]{ 1, 1, 4, 5, 1, 4 }; + final Byte[] boxed = new Byte[]{ 1, 1, 4, 5, 1, 4 }; + assertArrayEquals(primitive, Utils.byteToByte(boxed)); + assertArrayEquals(boxed, Utils.byteToByte(primitive)); + } + + @Test + void testBytesToInt() throws ParseException { + assertEquals(1, Utils.bytesToInt(new Byte[]{ 1 })); + assertEquals(257, Utils.bytesToInt(new Byte[]{ 1, 1 })); + assertThrows(ParseException.class, () -> Utils.bytesToInt(new Byte[]{ 1, -1, -1, -1, -1 })); + } + + @Test + void testValToByte() { + assertArrayEquals(new Byte[]{ 0 }, + Utils.valToByte(0)); + assertArrayEquals(new Byte[]{ 1 }, + Utils.valToByte(1)); + assertArrayEquals(new Byte[]{ 1, 1 }, + Utils.valToByte(257)); + assertArrayEquals(new Byte[]{ -1 }, + Utils.valToByte(-1)); + } + + @Test + void testParsePEM() throws ParseException { + assertArrayEquals(IntStream.range(0, 48).mapToObj(i -> (byte) 1).toArray(Byte[]::new), + Utils.parsePEM(Utils.byteToByte(("-----BEGIN BLABLA-----\n" + + "AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEB\n" + + "-----END BLABLA-----").getBytes(StandardCharsets.UTF_8)), + "BLABLA")); + assertThrows(ParseException.class, () -> { + Utils.parsePEM(Utils.byteToByte(("-----BEGIN BLABLA-----\n" + + "AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEB\n" + + "-----END BLABLA-----").getBytes(StandardCharsets.UTF_8)), + "LALA"); + }); + assertThrows(ParseException.class, () -> { + Utils.parsePEM(Utils.byteToByte(("-----BEGIN BLABLA-----" + + "AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEB" + + "-----END BLABLA-----").getBytes(StandardCharsets.UTF_8)), + "BLABLA"); + }); + } + + @Test + void testToPEM() { + assertEquals("-----BEGIN ABC-----\n" + + "AQ==\n" + + "-----END ABC-----", + Utils.toPEM(new Byte[]{ 0x1 }, "ABC")); + } +} -- cgit v1.2.3