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")); } }