mirror of
https://codeberg.org/PGPainless/sop-java.git
synced 2025-09-10 10:49:48 +02:00
Treat password and sessionkkey arguments as indirect data types
This commit is contained in:
parent
a2f2069380
commit
117117b735
8 changed files with 134 additions and 42 deletions
|
@ -0,0 +1,29 @@
|
|||
// SPDX-FileCopyrightText: 2022 Paul Schaub <vanitasvitae@fsfe.org>
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package sop.cli.picocli;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
|
||||
public class TestFileUtil {
|
||||
|
||||
public static File writeTempStringFile(String string) throws IOException {
|
||||
File tempDir = Files.createTempDirectory("tmpDir").toFile();
|
||||
tempDir.deleteOnExit();
|
||||
tempDir.mkdirs();
|
||||
|
||||
File passwordFile = new File(tempDir, "file");
|
||||
passwordFile.createNewFile();
|
||||
|
||||
FileOutputStream fileOut = new FileOutputStream(passwordFile);
|
||||
fileOut.write(string.getBytes(StandardCharsets.UTF_8));
|
||||
fileOut.close();
|
||||
|
||||
return passwordFile;
|
||||
}
|
||||
}
|
|
@ -38,6 +38,7 @@ import sop.SessionKey;
|
|||
import sop.Verification;
|
||||
import sop.cli.picocli.DateParser;
|
||||
import sop.cli.picocli.SopCLI;
|
||||
import sop.cli.picocli.TestFileUtil;
|
||||
import sop.exception.SOPGPException;
|
||||
import sop.operation.Decrypt;
|
||||
import sop.util.HexUtil;
|
||||
|
@ -90,22 +91,25 @@ public class DecryptCmdTest {
|
|||
@Test
|
||||
@ExpectSystemExitWithStatus(31)
|
||||
public void assertNotHumanReadablePasswordCausesExit31() throws SOPGPException.PasswordNotHumanReadable,
|
||||
SOPGPException.UnsupportedOption {
|
||||
SOPGPException.UnsupportedOption, IOException {
|
||||
File passwordFile = TestFileUtil.writeTempStringFile("pretendThisIsNotReadable");
|
||||
when(decrypt.withPassword(any())).thenThrow(new SOPGPException.PasswordNotHumanReadable());
|
||||
SopCLI.main(new String[] {"decrypt", "--with-password", "pretendThisIsNotReadable"});
|
||||
SopCLI.main(new String[] {"decrypt", "--with-password", passwordFile.getAbsolutePath()});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void assertWithPasswordPassesPasswordDown() throws SOPGPException.PasswordNotHumanReadable, SOPGPException.UnsupportedOption {
|
||||
SopCLI.main(new String[] {"decrypt", "--with-password", "orange"});
|
||||
public void assertWithPasswordPassesPasswordDown() throws SOPGPException.PasswordNotHumanReadable, SOPGPException.UnsupportedOption, IOException {
|
||||
File passwordFile = TestFileUtil.writeTempStringFile("orange");
|
||||
SopCLI.main(new String[] {"decrypt", "--with-password", passwordFile.getAbsolutePath()});
|
||||
verify(decrypt, times(1)).withPassword("orange");
|
||||
}
|
||||
|
||||
@Test
|
||||
@ExpectSystemExitWithStatus(37)
|
||||
public void assertUnsupportedWithPasswordCausesExit37() throws SOPGPException.PasswordNotHumanReadable, SOPGPException.UnsupportedOption {
|
||||
public void assertUnsupportedWithPasswordCausesExit37() throws SOPGPException.PasswordNotHumanReadable, SOPGPException.UnsupportedOption, IOException {
|
||||
File passwordFile = TestFileUtil.writeTempStringFile("swordfish");
|
||||
when(decrypt.withPassword(any())).thenThrow(new SOPGPException.UnsupportedOption("Decrypting with password not supported."));
|
||||
SopCLI.main(new String[] {"decrypt", "--with-password", "swordfish"});
|
||||
SopCLI.main(new String[] {"decrypt", "--with-password", passwordFile.getAbsolutePath()});
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -289,21 +293,26 @@ public class DecryptCmdTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void assertWithSessionKeyIsPassedDown() throws SOPGPException.UnsupportedOption {
|
||||
public void assertWithSessionKeyIsPassedDown() throws SOPGPException.UnsupportedOption, IOException {
|
||||
SessionKey key1 = new SessionKey((byte) 9, HexUtil.hexToBytes("C7CBDAF42537776F12509B5168793C26B93294E5ABDFA73224FB0177123E9137"));
|
||||
SessionKey key2 = new SessionKey((byte) 9, HexUtil.hexToBytes("FCA4BEAF687F48059CACC14FB019125CD57392BAB7037C707835925CBF9F7BCD"));
|
||||
|
||||
File sessionKeyFile1 = TestFileUtil.writeTempStringFile(key1.toString());
|
||||
File sessionKeyFile2 = TestFileUtil.writeTempStringFile(key2.toString());
|
||||
|
||||
SopCLI.main(new String[] {"decrypt",
|
||||
"--with-session-key", "9:C7CBDAF42537776F12509B5168793C26B93294E5ABDFA73224FB0177123E9137",
|
||||
"--with-session-key", "9:FCA4BEAF687F48059CACC14FB019125CD57392BAB7037C707835925CBF9F7BCD"});
|
||||
"--with-session-key", sessionKeyFile1.getAbsolutePath(),
|
||||
"--with-session-key", sessionKeyFile2.getAbsolutePath()});
|
||||
verify(decrypt).withSessionKey(key1);
|
||||
verify(decrypt).withSessionKey(key2);
|
||||
}
|
||||
|
||||
@Test
|
||||
@ExpectSystemExitWithStatus(1)
|
||||
public void assertMalformedSessionKeysResultInExit1() {
|
||||
public void assertMalformedSessionKeysResultInExit1() throws IOException {
|
||||
File sessionKeyFile = TestFileUtil.writeTempStringFile("C7CBDAF42537776F12509B5168793C26B93294E5ABDFA73224FB0177123E9137");
|
||||
SopCLI.main(new String[] {"decrypt",
|
||||
"--with-session-key", "C7CBDAF42537776F12509B5168793C26B93294E5ABDFA73224FB0177123E9137"});
|
||||
"--with-session-key", sessionKeyFile.getAbsolutePath()});
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
@ -22,6 +22,7 @@ import org.junit.jupiter.api.Test;
|
|||
import sop.Ready;
|
||||
import sop.SOP;
|
||||
import sop.cli.picocli.SopCLI;
|
||||
import sop.cli.picocli.TestFileUtil;
|
||||
import sop.enums.EncryptAs;
|
||||
import sop.exception.SOPGPException;
|
||||
import sop.operation.Encrypt;
|
||||
|
@ -67,35 +68,36 @@ public class EncryptCmdTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void as_modeIsPassedDown() throws SOPGPException.UnsupportedOption {
|
||||
public void as_modeIsPassedDown() throws SOPGPException.UnsupportedOption, IOException {
|
||||
File passwordFile = TestFileUtil.writeTempStringFile("0rbit");
|
||||
for (EncryptAs mode : EncryptAs.values()) {
|
||||
SopCLI.main(new String[] {"encrypt", "--as", mode.name(), "--with-password", "0rbit"});
|
||||
SopCLI.main(new String[] {"encrypt", "--as", mode.name(), "--with-password", passwordFile.getAbsolutePath()});
|
||||
verify(encrypt, times(1)).mode(mode);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@ExpectSystemExitWithStatus(31)
|
||||
public void withPassword_notHumanReadablePasswordCausesExit31() throws SOPGPException.PasswordNotHumanReadable, SOPGPException.UnsupportedOption {
|
||||
public void withPassword_notHumanReadablePasswordCausesExit31() throws SOPGPException.PasswordNotHumanReadable, SOPGPException.UnsupportedOption, IOException {
|
||||
when(encrypt.withPassword("pretendThisIsNotReadable")).thenThrow(new SOPGPException.PasswordNotHumanReadable());
|
||||
|
||||
SopCLI.main(new String[] {"encrypt", "--with-password", "pretendThisIsNotReadable"});
|
||||
File passwordFile = TestFileUtil.writeTempStringFile("pretendThisIsNotReadable");
|
||||
SopCLI.main(new String[] {"encrypt", "--with-password", passwordFile.getAbsolutePath()});
|
||||
}
|
||||
|
||||
@Test
|
||||
@ExpectSystemExitWithStatus(37)
|
||||
public void withPassword_unsupportedWithPasswordCausesExit37() throws SOPGPException.PasswordNotHumanReadable, SOPGPException.UnsupportedOption {
|
||||
public void withPassword_unsupportedWithPasswordCausesExit37() throws SOPGPException.PasswordNotHumanReadable, SOPGPException.UnsupportedOption, IOException {
|
||||
when(encrypt.withPassword(any())).thenThrow(new SOPGPException.UnsupportedOption("Encrypting with password not supported."));
|
||||
|
||||
SopCLI.main(new String[] {"encrypt", "--with-password", "orange"});
|
||||
File passwordFile = TestFileUtil.writeTempStringFile("orange");
|
||||
SopCLI.main(new String[] {"encrypt", "--with-password", passwordFile.getAbsolutePath()});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void signWith_multipleTimesGetPassedDown() throws IOException, SOPGPException.KeyIsProtected, SOPGPException.UnsupportedAsymmetricAlgo, SOPGPException.KeyCannotSign, SOPGPException.BadData {
|
||||
File keyFile1 = File.createTempFile("sign-with-1-", ".asc");
|
||||
File keyFile2 = File.createTempFile("sign-with-2-", ".asc");
|
||||
|
||||
SopCLI.main(new String[] {"encrypt", "--with-password", "password", "--sign-with", keyFile1.getAbsolutePath(), "--sign-with", keyFile2.getAbsolutePath()});
|
||||
File passwordFile = TestFileUtil.writeTempStringFile("password");
|
||||
SopCLI.main(new String[] {"encrypt", "--with-password", passwordFile.getAbsolutePath(), "--sign-with", keyFile1.getAbsolutePath(), "--sign-with", keyFile2.getAbsolutePath()});
|
||||
verify(encrypt, times(2)).signWith((InputStream) any());
|
||||
}
|
||||
|
||||
|
@ -110,7 +112,8 @@ public class EncryptCmdTest {
|
|||
public void signWith_keyIsProtectedCausesExit67() throws SOPGPException.KeyIsProtected, SOPGPException.UnsupportedAsymmetricAlgo, SOPGPException.KeyCannotSign, SOPGPException.BadData, IOException {
|
||||
when(encrypt.signWith((InputStream) any())).thenThrow(new SOPGPException.KeyIsProtected());
|
||||
File keyFile = File.createTempFile("sign-with", ".asc");
|
||||
SopCLI.main(new String[] {"encrypt", "--sign-with", keyFile.getAbsolutePath(), "--with-password", "starship"});
|
||||
File passwordFile = TestFileUtil.writeTempStringFile("starship");
|
||||
SopCLI.main(new String[] {"encrypt", "--sign-with", keyFile.getAbsolutePath(), "--with-password", passwordFile.getAbsolutePath()});
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -118,7 +121,8 @@ public class EncryptCmdTest {
|
|||
public void signWith_unsupportedAsymmetricAlgoCausesExit13() throws SOPGPException.KeyIsProtected, SOPGPException.UnsupportedAsymmetricAlgo, SOPGPException.KeyCannotSign, SOPGPException.BadData, IOException {
|
||||
when(encrypt.signWith((InputStream) any())).thenThrow(new SOPGPException.UnsupportedAsymmetricAlgo("Unsupported asymmetric algorithm.", new Exception()));
|
||||
File keyFile = File.createTempFile("sign-with", ".asc");
|
||||
SopCLI.main(new String[] {"encrypt", "--with-password", "123456", "--sign-with", keyFile.getAbsolutePath()});
|
||||
File passwordFile = TestFileUtil.writeTempStringFile("123456");
|
||||
SopCLI.main(new String[] {"encrypt", "--with-password", passwordFile.getAbsolutePath(), "--sign-with", keyFile.getAbsolutePath()});
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -126,7 +130,8 @@ public class EncryptCmdTest {
|
|||
public void signWith_certCannotSignCausesExit1() throws IOException, SOPGPException.KeyIsProtected, SOPGPException.UnsupportedAsymmetricAlgo, SOPGPException.KeyCannotSign, SOPGPException.BadData {
|
||||
when(encrypt.signWith((InputStream) any())).thenThrow(new SOPGPException.KeyCannotSign());
|
||||
File keyFile = File.createTempFile("sign-with", ".asc");
|
||||
SopCLI.main(new String[] {"encrypt", "--with-password", "dragon", "--sign-with", keyFile.getAbsolutePath()});
|
||||
File passwordFile = TestFileUtil.writeTempStringFile("dragon");
|
||||
SopCLI.main(new String[] {"encrypt", "--with-password", passwordFile.getAbsolutePath(), "--sign-with", keyFile.getAbsolutePath()});
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -134,7 +139,8 @@ public class EncryptCmdTest {
|
|||
public void signWith_badDataCausesExit41() throws SOPGPException.KeyIsProtected, SOPGPException.UnsupportedAsymmetricAlgo, SOPGPException.KeyCannotSign, SOPGPException.BadData, IOException {
|
||||
when(encrypt.signWith((InputStream) any())).thenThrow(new SOPGPException.BadData(new IOException()));
|
||||
File keyFile = File.createTempFile("sign-with", ".asc");
|
||||
SopCLI.main(new String[] {"encrypt", "--with-password", "orange", "--sign-with", keyFile.getAbsolutePath()});
|
||||
File passwordFile = TestFileUtil.writeTempStringFile("orange");
|
||||
SopCLI.main(new String[] {"encrypt", "--with-password", passwordFile.getAbsolutePath(), "--sign-with", keyFile.getAbsolutePath()});
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -168,14 +174,16 @@ public class EncryptCmdTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void noArmor_notCalledByDefault() {
|
||||
SopCLI.main(new String[] {"encrypt", "--with-password", "clownfish"});
|
||||
public void noArmor_notCalledByDefault() throws IOException {
|
||||
File passwordFile = TestFileUtil.writeTempStringFile("clownfish");
|
||||
SopCLI.main(new String[] {"encrypt", "--with-password", passwordFile.getAbsolutePath()});
|
||||
verify(encrypt, never()).noArmor();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void noArmor_callGetsPassedDown() {
|
||||
SopCLI.main(new String[] {"encrypt", "--with-password", "monkey", "--no-armor"});
|
||||
public void noArmor_callGetsPassedDown() throws IOException {
|
||||
File passwordFile = TestFileUtil.writeTempStringFile("monkey");
|
||||
SopCLI.main(new String[] {"encrypt", "--with-password", passwordFile.getAbsolutePath(), "--no-armor"});
|
||||
verify(encrypt, times(1)).noArmor();
|
||||
}
|
||||
|
||||
|
@ -188,7 +196,7 @@ public class EncryptCmdTest {
|
|||
throw new IOException();
|
||||
}
|
||||
});
|
||||
|
||||
SopCLI.main(new String[] {"encrypt", "--with-password", "wildcat"});
|
||||
File passwordFile = TestFileUtil.writeTempStringFile("wildcat");
|
||||
SopCLI.main(new String[] {"encrypt", "--with-password", passwordFile.getAbsolutePath()});
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue