UTF8Util.decodeUTF8(): throw CharacterCodingException instead of PasswordNotHumanReadable

This commit is contained in:
Paul Schaub 2023-04-27 13:15:44 +02:00
parent 0308732328
commit 8eba099146
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
3 changed files with 17 additions and 16 deletions

View file

@ -6,6 +6,7 @@ package sop.operation;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.CharacterCodingException;
import sop.Profile;
import sop.Ready;
@ -54,7 +55,11 @@ public interface GenerateKey {
default GenerateKey withKeyPassword(byte[] password)
throws SOPGPException.PasswordNotHumanReadable,
SOPGPException.UnsupportedOption {
return withKeyPassword(UTF8Util.decodeUTF8(password));
try {
return withKeyPassword(UTF8Util.decodeUTF8(password));
} catch (CharacterCodingException e) {
throw new SOPGPException.PasswordNotHumanReadable();
}
}
/**

View file

@ -4,8 +4,6 @@
package sop.util;
import sop.exception.SOPGPException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.CharacterCodingException;
@ -15,7 +13,8 @@ import java.nio.charset.CodingErrorAction;
public class UTF8Util {
private static final CharsetDecoder UTF8Decoder = Charset.forName("UTF8")
public static final Charset UTF8 = Charset.forName("UTF8");
private static final CharsetDecoder UTF8Decoder = UTF8
.newDecoder()
.onUnmappableCharacter(CodingErrorAction.REPORT)
.onMalformedInput(CodingErrorAction.REPORT);
@ -28,13 +27,10 @@ public class UTF8Util {
*
* @return decoded string
*/
public static String decodeUTF8(byte[] data) {
public static String decodeUTF8(byte[] data)
throws CharacterCodingException {
ByteBuffer byteBuffer = ByteBuffer.wrap(data);
try {
CharBuffer charBuffer = UTF8Decoder.decode(byteBuffer);
return charBuffer.toString();
} catch (CharacterCodingException e) {
throw new SOPGPException.PasswordNotHumanReadable();
}
CharBuffer charBuffer = UTF8Decoder.decode(byteBuffer);
return charBuffer.toString();
}
}

View file

@ -5,8 +5,8 @@
package sop.util;
import org.junit.jupiter.api.Test;
import sop.exception.SOPGPException;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.StandardCharsets;
import static org.junit.jupiter.api.Assertions.assertEquals;
@ -15,7 +15,7 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
public class UTF8UtilTest {
@Test
public void testValidUtf8Decoding() {
public void testValidUtf8Decoding() throws CharacterCodingException {
String utf8String = "Hello, World\n";
String decoded = UTF8Util.decodeUTF8(utf8String.getBytes(StandardCharsets.UTF_8));
@ -29,11 +29,11 @@ public class UTF8UtilTest {
*/
@Test
public void testInvalidUtf8StringThrows() {
assertThrows(SOPGPException.PasswordNotHumanReadable.class,
assertThrows(CharacterCodingException.class,
() -> UTF8Util.decodeUTF8(new byte[] {(byte) 0xa0, (byte) 0xa1}));
assertThrows(SOPGPException.PasswordNotHumanReadable.class,
assertThrows(CharacterCodingException.class,
() -> UTF8Util.decodeUTF8(new byte[] {(byte) 0xc0, (byte) 0xaf}));
assertThrows(SOPGPException.PasswordNotHumanReadable.class,
assertThrows(CharacterCodingException.class,
() -> UTF8Util.decodeUTF8(new byte[] {(byte) 0x80, (byte) 0xbf}));
}
}