1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2025-09-09 02:09:38 +02:00

Add tests for LongExtension methods

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

View file

@ -9,7 +9,7 @@ fun Long.openPgpKeyId(): String {
return String.format("%016X", this).uppercase()
}
/** Parse a Long form a 16 digit hex encoded OpenPgp key-ID. */
/** Parse a Long from a 16 digit hex encoded OpenPgp key-ID. */
fun Long.Companion.fromOpenPgpKeyId(hexKeyId: String): Long {
require("^[0-9A-Fa-f]{16}$".toRegex().matches(hexKeyId)) {
"Provided long key-id does not match expected format. " +

View file

@ -17,10 +17,7 @@ class KeyIdUtil {
* @param longKeyId 16-digit hexadecimal string
* @return key-id converted to [Long].
*/
@JvmStatic
@Deprecated(
"Superseded by Long extension method.", ReplaceWith("Long.fromHexKeyId(longKeyId)"))
fun fromLongKeyId(longKeyId: String) = Long.fromOpenPgpKeyId(longKeyId)
@JvmStatic fun fromLongKeyId(longKeyId: String) = Long.fromOpenPgpKeyId(longKeyId)
/**
* Format a long key-ID as upper-case hex string.

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