2022-01-11 13:46:05 +01:00
|
|
|
// SPDX-FileCopyrightText: 2021 Paul Schaub <vanitasvitae@fsfe.org>
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
package sop.cli.picocli.commands;
|
|
|
|
|
|
|
|
import static org.mockito.ArgumentMatchers.any;
|
|
|
|
import static org.mockito.Mockito.mock;
|
|
|
|
import static org.mockito.Mockito.never;
|
|
|
|
import static org.mockito.Mockito.times;
|
|
|
|
import static org.mockito.Mockito.verify;
|
|
|
|
import static org.mockito.Mockito.when;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.OutputStream;
|
|
|
|
|
|
|
|
import com.ginsberg.junit.exit.ExpectSystemExitWithStatus;
|
|
|
|
import org.junit.jupiter.api.BeforeEach;
|
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
import sop.Ready;
|
|
|
|
import sop.SOP;
|
|
|
|
import sop.cli.picocli.SopCLI;
|
2022-02-09 15:12:23 +01:00
|
|
|
import sop.cli.picocli.TestFileUtil;
|
2022-01-11 13:46:05 +01:00
|
|
|
import sop.enums.EncryptAs;
|
|
|
|
import sop.exception.SOPGPException;
|
|
|
|
import sop.operation.Encrypt;
|
|
|
|
|
|
|
|
public class EncryptCmdTest {
|
|
|
|
|
|
|
|
Encrypt encrypt;
|
|
|
|
|
|
|
|
@BeforeEach
|
|
|
|
public void mockComponents() throws IOException {
|
|
|
|
encrypt = mock(Encrypt.class);
|
|
|
|
when(encrypt.plaintext((InputStream) any())).thenReturn(new Ready() {
|
|
|
|
@Override
|
|
|
|
public void writeTo(OutputStream outputStream) {
|
|
|
|
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
SOP sop = mock(SOP.class);
|
|
|
|
when(sop.encrypt()).thenReturn(encrypt);
|
|
|
|
|
|
|
|
SopCLI.setSopInstance(sop);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2022-06-11 11:18:45 +02:00
|
|
|
@ExpectSystemExitWithStatus(SOPGPException.MissingArg.EXIT_CODE)
|
2022-01-11 13:46:05 +01:00
|
|
|
public void missingBothPasswordAndCertFileCauseExit19() {
|
|
|
|
SopCLI.main(new String[] {"encrypt", "--no-armor"});
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2022-06-11 11:18:45 +02:00
|
|
|
@ExpectSystemExitWithStatus(SOPGPException.UnsupportedOption.EXIT_CODE)
|
2022-01-11 13:46:05 +01:00
|
|
|
public void as_unsupportedEncryptAsCausesExit37() throws SOPGPException.UnsupportedOption {
|
|
|
|
when(encrypt.mode(any())).thenThrow(new SOPGPException.UnsupportedOption("Setting encryption mode not supported."));
|
|
|
|
|
|
|
|
SopCLI.main(new String[] {"encrypt", "--as", "Binary"});
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2022-06-11 11:18:45 +02:00
|
|
|
@ExpectSystemExitWithStatus(SOPGPException.UnsupportedOption.EXIT_CODE)
|
2022-01-11 13:46:05 +01:00
|
|
|
public void as_invalidModeOptionCausesExit37() {
|
|
|
|
SopCLI.main(new String[] {"encrypt", "--as", "invalid"});
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2022-02-09 15:12:23 +01:00
|
|
|
public void as_modeIsPassedDown() throws SOPGPException.UnsupportedOption, IOException {
|
|
|
|
File passwordFile = TestFileUtil.writeTempStringFile("0rbit");
|
2022-01-11 13:46:05 +01:00
|
|
|
for (EncryptAs mode : EncryptAs.values()) {
|
2022-02-09 15:12:23 +01:00
|
|
|
SopCLI.main(new String[] {"encrypt", "--as", mode.name(), "--with-password", passwordFile.getAbsolutePath()});
|
2022-01-11 13:46:05 +01:00
|
|
|
verify(encrypt, times(1)).mode(mode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2022-06-11 11:18:45 +02:00
|
|
|
@ExpectSystemExitWithStatus(SOPGPException.PasswordNotHumanReadable.EXIT_CODE)
|
2022-02-09 15:12:23 +01:00
|
|
|
public void withPassword_notHumanReadablePasswordCausesExit31() throws SOPGPException.PasswordNotHumanReadable, SOPGPException.UnsupportedOption, IOException {
|
2022-01-11 13:46:05 +01:00
|
|
|
when(encrypt.withPassword("pretendThisIsNotReadable")).thenThrow(new SOPGPException.PasswordNotHumanReadable());
|
2022-02-09 15:12:23 +01:00
|
|
|
File passwordFile = TestFileUtil.writeTempStringFile("pretendThisIsNotReadable");
|
|
|
|
SopCLI.main(new String[] {"encrypt", "--with-password", passwordFile.getAbsolutePath()});
|
2022-01-11 13:46:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2022-06-11 11:18:45 +02:00
|
|
|
@ExpectSystemExitWithStatus(SOPGPException.UnsupportedOption.EXIT_CODE)
|
2022-02-09 15:12:23 +01:00
|
|
|
public void withPassword_unsupportedWithPasswordCausesExit37() throws SOPGPException.PasswordNotHumanReadable, SOPGPException.UnsupportedOption, IOException {
|
2022-01-11 13:46:05 +01:00
|
|
|
when(encrypt.withPassword(any())).thenThrow(new SOPGPException.UnsupportedOption("Encrypting with password not supported."));
|
2022-02-09 15:12:23 +01:00
|
|
|
File passwordFile = TestFileUtil.writeTempStringFile("orange");
|
|
|
|
SopCLI.main(new String[] {"encrypt", "--with-password", passwordFile.getAbsolutePath()});
|
2022-01-11 13:46:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@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");
|
2022-02-09 15:12:23 +01:00
|
|
|
File passwordFile = TestFileUtil.writeTempStringFile("password");
|
|
|
|
SopCLI.main(new String[] {"encrypt", "--with-password", passwordFile.getAbsolutePath(), "--sign-with", keyFile1.getAbsolutePath(), "--sign-with", keyFile2.getAbsolutePath()});
|
2022-01-11 13:46:05 +01:00
|
|
|
verify(encrypt, times(2)).signWith((InputStream) any());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2022-06-11 11:18:45 +02:00
|
|
|
@ExpectSystemExitWithStatus(SOPGPException.MissingInput.EXIT_CODE)
|
2022-01-11 13:46:05 +01:00
|
|
|
public void signWith_nonExistentKeyFileCausesExit61() {
|
|
|
|
SopCLI.main(new String[] {"encrypt", "--with-password", "admin", "--sign-with", "nonExistent.asc"});
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2022-06-11 11:18:45 +02:00
|
|
|
@ExpectSystemExitWithStatus(SOPGPException.KeyIsProtected.EXIT_CODE)
|
2022-01-11 13:46:05 +01:00
|
|
|
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");
|
2022-02-09 15:12:23 +01:00
|
|
|
File passwordFile = TestFileUtil.writeTempStringFile("starship");
|
|
|
|
SopCLI.main(new String[] {"encrypt", "--sign-with", keyFile.getAbsolutePath(), "--with-password", passwordFile.getAbsolutePath()});
|
2022-01-11 13:46:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2022-06-11 11:18:45 +02:00
|
|
|
@ExpectSystemExitWithStatus(SOPGPException.UnsupportedAsymmetricAlgo.EXIT_CODE)
|
2022-01-11 13:46:05 +01:00
|
|
|
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");
|
2022-02-09 15:12:23 +01:00
|
|
|
File passwordFile = TestFileUtil.writeTempStringFile("123456");
|
|
|
|
SopCLI.main(new String[] {"encrypt", "--with-password", passwordFile.getAbsolutePath(), "--sign-with", keyFile.getAbsolutePath()});
|
2022-01-11 13:46:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2022-06-11 11:18:45 +02:00
|
|
|
@ExpectSystemExitWithStatus(SOPGPException.KeyCannotSign.EXIT_CODE)
|
2022-06-06 20:06:14 +02:00
|
|
|
public void signWith_certCannotSignCausesExit79() throws IOException, SOPGPException.KeyIsProtected, SOPGPException.UnsupportedAsymmetricAlgo, SOPGPException.KeyCannotSign, SOPGPException.BadData {
|
2022-01-11 13:46:05 +01:00
|
|
|
when(encrypt.signWith((InputStream) any())).thenThrow(new SOPGPException.KeyCannotSign());
|
|
|
|
File keyFile = File.createTempFile("sign-with", ".asc");
|
2022-02-09 15:12:23 +01:00
|
|
|
File passwordFile = TestFileUtil.writeTempStringFile("dragon");
|
|
|
|
SopCLI.main(new String[] {"encrypt", "--with-password", passwordFile.getAbsolutePath(), "--sign-with", keyFile.getAbsolutePath()});
|
2022-01-11 13:46:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2022-06-11 11:18:45 +02:00
|
|
|
@ExpectSystemExitWithStatus(SOPGPException.BadData.EXIT_CODE)
|
2022-01-11 13:46:05 +01:00
|
|
|
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");
|
2022-02-09 15:12:23 +01:00
|
|
|
File passwordFile = TestFileUtil.writeTempStringFile("orange");
|
|
|
|
SopCLI.main(new String[] {"encrypt", "--with-password", passwordFile.getAbsolutePath(), "--sign-with", keyFile.getAbsolutePath()});
|
2022-01-11 13:46:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2022-06-11 11:18:45 +02:00
|
|
|
@ExpectSystemExitWithStatus(SOPGPException.MissingInput.EXIT_CODE)
|
2022-01-11 13:46:05 +01:00
|
|
|
public void cert_nonExistentCertFileCausesExit61() {
|
|
|
|
SopCLI.main(new String[] {"encrypt", "invalid.asc"});
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2022-06-11 11:18:45 +02:00
|
|
|
@ExpectSystemExitWithStatus(SOPGPException.UnsupportedAsymmetricAlgo.EXIT_CODE)
|
2022-01-11 13:46:05 +01:00
|
|
|
public void cert_unsupportedAsymmetricAlgorithmCausesExit13() throws IOException, SOPGPException.UnsupportedAsymmetricAlgo, SOPGPException.CertCannotEncrypt, SOPGPException.BadData {
|
|
|
|
when(encrypt.withCert((InputStream) any())).thenThrow(new SOPGPException.UnsupportedAsymmetricAlgo("Unsupported asymmetric algorithm.", new Exception()));
|
|
|
|
File certFile = File.createTempFile("cert", ".asc");
|
|
|
|
SopCLI.main(new String[] {"encrypt", certFile.getAbsolutePath()});
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2022-06-11 11:18:45 +02:00
|
|
|
@ExpectSystemExitWithStatus(SOPGPException.CertCannotEncrypt.EXIT_CODE)
|
2022-01-11 13:46:05 +01:00
|
|
|
public void cert_certCannotEncryptCausesExit17() throws IOException, SOPGPException.UnsupportedAsymmetricAlgo, SOPGPException.CertCannotEncrypt, SOPGPException.BadData {
|
|
|
|
when(encrypt.withCert((InputStream) any())).thenThrow(new SOPGPException.CertCannotEncrypt("Certificate cannot encrypt.", new Exception()));
|
|
|
|
File certFile = File.createTempFile("cert", ".asc");
|
|
|
|
SopCLI.main(new String[] {"encrypt", certFile.getAbsolutePath()});
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2022-06-11 11:18:45 +02:00
|
|
|
@ExpectSystemExitWithStatus(SOPGPException.BadData.EXIT_CODE)
|
2022-01-11 13:46:05 +01:00
|
|
|
public void cert_badDataCausesExit41() throws IOException, SOPGPException.UnsupportedAsymmetricAlgo, SOPGPException.CertCannotEncrypt, SOPGPException.BadData {
|
|
|
|
when(encrypt.withCert((InputStream) any())).thenThrow(new SOPGPException.BadData(new IOException()));
|
|
|
|
File certFile = File.createTempFile("cert", ".asc");
|
|
|
|
SopCLI.main(new String[] {"encrypt", certFile.getAbsolutePath()});
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2022-02-09 15:12:23 +01:00
|
|
|
public void noArmor_notCalledByDefault() throws IOException {
|
|
|
|
File passwordFile = TestFileUtil.writeTempStringFile("clownfish");
|
|
|
|
SopCLI.main(new String[] {"encrypt", "--with-password", passwordFile.getAbsolutePath()});
|
2022-01-11 13:46:05 +01:00
|
|
|
verify(encrypt, never()).noArmor();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2022-02-09 15:12:23 +01:00
|
|
|
public void noArmor_callGetsPassedDown() throws IOException {
|
|
|
|
File passwordFile = TestFileUtil.writeTempStringFile("monkey");
|
|
|
|
SopCLI.main(new String[] {"encrypt", "--with-password", passwordFile.getAbsolutePath(), "--no-armor"});
|
2022-01-11 13:46:05 +01:00
|
|
|
verify(encrypt, times(1)).noArmor();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@ExpectSystemExitWithStatus(1)
|
|
|
|
public void writeTo_ioExceptionCausesExit1() throws IOException {
|
|
|
|
when(encrypt.plaintext((InputStream) any())).thenReturn(new Ready() {
|
|
|
|
@Override
|
|
|
|
public void writeTo(OutputStream outputStream) throws IOException {
|
|
|
|
throw new IOException();
|
|
|
|
}
|
|
|
|
});
|
2022-02-09 15:12:23 +01:00
|
|
|
File passwordFile = TestFileUtil.writeTempStringFile("wildcat");
|
|
|
|
SopCLI.main(new String[] {"encrypt", "--with-password", passwordFile.getAbsolutePath()});
|
2022-01-11 13:46:05 +01:00
|
|
|
}
|
|
|
|
}
|