1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2025-12-08 13:21:09 +01:00

Add tests for LongExtension methods

This commit is contained in:
Paul Schaub 2025-05-12 13:06:21 +02:00
parent 88d9fae2fc
commit 06d0b90ff6
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
3 changed files with 40 additions and 5 deletions

View file

@ -0,0 +1,38 @@
// SPDX-FileCopyrightText: 2025 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
package openpgp;
import org.junit.jupiter.api.Test;
import org.pgpainless.key.util.KeyIdUtil;
import java.util.Random;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
public class LongExtensionTest {
private final Random random = new Random();
@Test
public void testFromOpenPGPKeyId() {
long id = random.nextLong();
String hexId = LongExtensionsKt.openPgpKeyId(id);
assertEquals(16, hexId.length());
// Calling companion object extension methods from java is tricky.
// KeyIdUtil delegates to Long.Companion extension method though.
long parsed = KeyIdUtil.fromLongKeyId(hexId);
assertEquals(id, parsed, "Long MUST still match after converting to hex and back.");
}
@Test
public void testParsingMalformedHexIdFails() {
assertThrows(IllegalArgumentException.class, () ->
KeyIdUtil.fromLongKeyId("00"),
"Hex encoding is too short, expect 16 chars.");assertThrows(IllegalArgumentException.class, () ->
KeyIdUtil.fromLongKeyId("00010203040506XX"),
"Hex encoding contains non-hex chars.");
}
}