mirror of
https://github.com/pgpainless/pgpainless.git
synced 2025-09-09 18:29:39 +02:00
Add tests for LongExtension methods
This commit is contained in:
parent
88d9fae2fc
commit
06d0b90ff6
3 changed files with 40 additions and 5 deletions
|
@ -9,7 +9,7 @@ fun Long.openPgpKeyId(): String {
|
||||||
return String.format("%016X", this).uppercase()
|
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 {
|
fun Long.Companion.fromOpenPgpKeyId(hexKeyId: String): Long {
|
||||||
require("^[0-9A-Fa-f]{16}$".toRegex().matches(hexKeyId)) {
|
require("^[0-9A-Fa-f]{16}$".toRegex().matches(hexKeyId)) {
|
||||||
"Provided long key-id does not match expected format. " +
|
"Provided long key-id does not match expected format. " +
|
||||||
|
|
|
@ -17,10 +17,7 @@ class KeyIdUtil {
|
||||||
* @param longKeyId 16-digit hexadecimal string
|
* @param longKeyId 16-digit hexadecimal string
|
||||||
* @return key-id converted to [Long].
|
* @return key-id converted to [Long].
|
||||||
*/
|
*/
|
||||||
@JvmStatic
|
@JvmStatic fun fromLongKeyId(longKeyId: String) = Long.fromOpenPgpKeyId(longKeyId)
|
||||||
@Deprecated(
|
|
||||||
"Superseded by Long extension method.", ReplaceWith("Long.fromHexKeyId(longKeyId)"))
|
|
||||||
fun fromLongKeyId(longKeyId: String) = Long.fromOpenPgpKeyId(longKeyId)
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Format a long key-ID as upper-case hex string.
|
* Format a long key-ID as upper-case hex string.
|
||||||
|
|
38
pgpainless-core/src/test/java/openpgp/LongExtensionTest.java
Normal file
38
pgpainless-core/src/test/java/openpgp/LongExtensionTest.java
Normal 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.");
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue