use of com.unboundid.util.PassphraseEncryptedOutputStream in project ldapsdk by pingidentity.
the class ToolUtilsTestCase method testGetInputStreamForLDIFFilesOneFileEncryptedNotCompressed2.
/**
* Tests the {@code getInputStreamForLDIFFiles} method with a single file that
* is encrypted but not compressed. The wrong passphrase will be
* provided as an argument rather than obtained via prompt.
*
* @throws Exception If an unexpected problem occurs.
*/
@Test(expectedExceptions = { IOException.class })
public void testGetInputStreamForLDIFFilesOneFileEncryptedNotCompressed2() throws Exception {
final Entry testEntry = new Entry("dn: dc=example,dc=com", "objectClass: top", "objectClass: domain", "dc: example");
final File testFile = createTempFile(testEntry.toLDIF());
assertTrue(testFile.delete());
final LDIFWriter ldifWriter = new LDIFWriter(new PassphraseEncryptedOutputStream("ThisIsThePassphrase", new FileOutputStream(testFile)));
ldifWriter.writeEntry(testEntry);
ldifWriter.close();
final ObjectPair<InputStream, String> p = ToolUtils.getInputStreamForLDIFFiles(Collections.singletonList(testFile), "wrong", SUPPRESS_OUTPUT, SUPPRESS_OUTPUT);
}
use of com.unboundid.util.PassphraseEncryptedOutputStream in project ldapsdk by pingidentity.
the class ToolUtilsTestCase method testGetInputStreamForLDIFFilesMultipleFiles1.
/**
* Tests the {@code getInputStreamForLDIFFiles} method with multiple files
* that have a mix of encryption and compression characteristics. All
* encrypted files will use the same passphrase, and that passphrase will be
* provided rather than obtained via prompt.
*
* @throws Exception If an unexpected problem occurs.
*/
@Test()
public void testGetInputStreamForLDIFFilesMultipleFiles1() throws Exception {
final String passphrase = "ThisIsThePassphrase";
final Entry testEntry1 = new Entry("dn: dc=example,dc=com", "objectClass: top", "objectClass: domain", "dc: example");
final File testFile1 = createTempFile(testEntry1.toLDIF());
final Entry testEntry2 = new Entry("dn: ou=foo,dc=example,dc=com", "objectClass: top", "objectClass: organizationalUnit", "ou: foo");
final File testFile2 = createTempFile();
assertTrue(testFile2.delete());
LDIFWriter ldifWriter = new LDIFWriter(new GZIPOutputStream(new FileOutputStream(testFile2)));
ldifWriter.writeEntry(testEntry2);
ldifWriter.close();
final Entry testEntry3 = new Entry("dn: ou=bar,dc=example,dc=com", "objectClass: top", "objectClass: organizationalUnit", "ou: bar");
final File testFile3 = createTempFile();
assertTrue(testFile3.delete());
ldifWriter = new LDIFWriter(new PassphraseEncryptedOutputStream(passphrase, new FileOutputStream(testFile3)));
ldifWriter.writeEntry(testEntry3);
ldifWriter.close();
final Entry testEntry4 = new Entry("dn: ou=baz,dc=example,dc=com", "objectClass: top", "objectClass: organizationalUnit", "ou: baz");
final File testFile4 = createTempFile();
assertTrue(testFile4.delete());
ldifWriter = new LDIFWriter(new GZIPOutputStream(new PassphraseEncryptedOutputStream(passphrase, new FileOutputStream(testFile4))));
ldifWriter.writeEntry(testEntry4);
ldifWriter.close();
final ObjectPair<InputStream, String> p = ToolUtils.getInputStreamForLDIFFiles(Arrays.asList(testFile1, testFile2, testFile3, testFile4), passphrase, SUPPRESS_OUTPUT, SUPPRESS_OUTPUT);
final InputStream inputStream = p.getFirst();
assertNotNull(inputStream);
assertNotNull(p.getSecond());
assertEquals(p.getSecond(), passphrase);
final LDIFReader ldifReader = new LDIFReader(inputStream);
assertEquals(ldifReader.readEntry(), testEntry1);
assertEquals(ldifReader.readEntry(), testEntry2);
assertEquals(ldifReader.readEntry(), testEntry3);
assertEquals(ldifReader.readEntry(), testEntry4);
assertNull(ldifReader.readEntry());
ldifReader.close();
}
use of com.unboundid.util.PassphraseEncryptedOutputStream in project ldapsdk by pingidentity.
the class ToolUtilsTestCase method testGetInputStreamForLDIFFilesOneFileEncryptedNotCompressed3.
/**
* Tests the {@code getInputStreamForLDIFFiles} method with a single file that
* is encrypted but not compressed. The correct passphrase will be provided
* interactively, and the correct passphrase will be provided on the first
* try.
*
* @throws Exception If an unexpected problem occurs.
*/
@Test()
public void testGetInputStreamForLDIFFilesOneFileEncryptedNotCompressed3() throws Exception {
try {
final String passphrase = "ThisIsThePassphrase";
PasswordReader.setTestReaderLines(passphrase);
final Entry testEntry = new Entry("dn: dc=example,dc=com", "objectClass: top", "objectClass: domain", "dc: example");
final File testFile = createTempFile(testEntry.toLDIF());
assertTrue(testFile.delete());
final LDIFWriter ldifWriter = new LDIFWriter(new PassphraseEncryptedOutputStream(passphrase, new FileOutputStream(testFile)));
ldifWriter.writeEntry(testEntry);
ldifWriter.close();
final ObjectPair<InputStream, String> p = ToolUtils.getInputStreamForLDIFFiles(Collections.singletonList(testFile), null, SUPPRESS_OUTPUT, SUPPRESS_OUTPUT);
final InputStream inputStream = p.getFirst();
assertNotNull(inputStream);
assertNotNull(p.getSecond());
assertEquals(p.getSecond(), passphrase);
final LDIFReader ldifReader = new LDIFReader(inputStream);
assertEquals(ldifReader.readEntry(), testEntry);
assertNull(ldifReader.readEntry());
ldifReader.close();
} finally {
PasswordReader.setTestReader(null);
}
}
use of com.unboundid.util.PassphraseEncryptedOutputStream in project ldapsdk by pingidentity.
the class ToolUtilsTestCase method testGetInputStreamForLDIFFilesOneFileEncryptedNotCompressed4.
/**
* Tests the {@code getInputStreamForLDIFFiles} method with a single file that
* is encrypted but not compressed. The correct passphrase will be provided
* interactively, and the correct passphrase will be provided on the second
* try.
*
* @throws Exception If an unexpected problem occurs.
*/
@Test()
public void testGetInputStreamForLDIFFilesOneFileEncryptedNotCompressed4() throws Exception {
try {
final String passphrase = "ThisIsThePassphrase";
PasswordReader.setTestReaderLines("wrong", passphrase);
final Entry testEntry = new Entry("dn: dc=example,dc=com", "objectClass: top", "objectClass: domain", "dc: example");
final File testFile = createTempFile(testEntry.toLDIF());
assertTrue(testFile.delete());
final LDIFWriter ldifWriter = new LDIFWriter(new PassphraseEncryptedOutputStream(passphrase, new FileOutputStream(testFile)));
ldifWriter.writeEntry(testEntry);
ldifWriter.close();
final ObjectPair<InputStream, String> p = ToolUtils.getInputStreamForLDIFFiles(Collections.singletonList(testFile), null, SUPPRESS_OUTPUT, SUPPRESS_OUTPUT);
final InputStream inputStream = p.getFirst();
assertNotNull(inputStream);
assertNotNull(p.getSecond());
assertEquals(p.getSecond(), passphrase);
final LDIFReader ldifReader = new LDIFReader(inputStream);
assertEquals(ldifReader.readEntry(), testEntry);
assertNull(ldifReader.readEntry());
ldifReader.close();
} finally {
PasswordReader.setTestReader(null);
}
}
use of com.unboundid.util.PassphraseEncryptedOutputStream in project ldapsdk by pingidentity.
the class LDIFModifyTestCase method createTempFile.
/**
* Writes the provided lines to an optionally compressed and/or encrypted
* output file.
*
* @param compress Indicates whether to compress the file.
* @param encPWFile A file containing the passphrase to use to encrypt the
* contents of the file. It may be {@code null} if the
* file should not be encrypted.
* @param lines The lines to be written.
*
* @return The file to which the lines were written.
*
* @throws Exception If an unexpected problem occurs.
*/
private static File createTempFile(final boolean compress, final File encPWFile, final String... lines) throws Exception {
File f = File.createTempFile("ldapsdk-", ".tmp");
f.deleteOnExit();
OutputStream outputStream = new FileOutputStream(f);
try {
if (encPWFile != null) {
final char[] pwChars = new PasswordFileReader().readPassword(encPWFile);
outputStream = new PassphraseEncryptedOutputStream(pwChars, outputStream);
}
if (compress) {
outputStream = new GZIPOutputStream(outputStream);
}
try (PrintWriter printStream = new PrintWriter(outputStream)) {
for (final String line : lines) {
printStream.println(line);
}
}
} finally {
outputStream.close();
}
return f;
}
Aggregations