Search in sources :

Example 1 with PassphraseEncryptedOutputStream

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);
}
Also used : Entry(com.unboundid.ldap.sdk.Entry) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) LDIFWriter(com.unboundid.ldif.LDIFWriter) File(java.io.File) PassphraseEncryptedOutputStream(com.unboundid.util.PassphraseEncryptedOutputStream) Test(org.testng.annotations.Test)

Example 2 with PassphraseEncryptedOutputStream

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();
}
Also used : Entry(com.unboundid.ldap.sdk.Entry) GZIPOutputStream(java.util.zip.GZIPOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) LDIFReader(com.unboundid.ldif.LDIFReader) LDIFWriter(com.unboundid.ldif.LDIFWriter) File(java.io.File) PassphraseEncryptedOutputStream(com.unboundid.util.PassphraseEncryptedOutputStream) Test(org.testng.annotations.Test)

Example 3 with PassphraseEncryptedOutputStream

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);
    }
}
Also used : Entry(com.unboundid.ldap.sdk.Entry) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) LDIFReader(com.unboundid.ldif.LDIFReader) LDIFWriter(com.unboundid.ldif.LDIFWriter) File(java.io.File) PassphraseEncryptedOutputStream(com.unboundid.util.PassphraseEncryptedOutputStream) Test(org.testng.annotations.Test)

Example 4 with PassphraseEncryptedOutputStream

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);
    }
}
Also used : Entry(com.unboundid.ldap.sdk.Entry) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) LDIFReader(com.unboundid.ldif.LDIFReader) LDIFWriter(com.unboundid.ldif.LDIFWriter) File(java.io.File) PassphraseEncryptedOutputStream(com.unboundid.util.PassphraseEncryptedOutputStream) Test(org.testng.annotations.Test)

Example 5 with PassphraseEncryptedOutputStream

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;
}
Also used : PasswordFileReader(com.unboundid.util.PasswordFileReader) GZIPOutputStream(java.util.zip.GZIPOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PassphraseEncryptedOutputStream(com.unboundid.util.PassphraseEncryptedOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) PassphraseEncryptedOutputStream(com.unboundid.util.PassphraseEncryptedOutputStream) PrintWriter(java.io.PrintWriter)

Aggregations

PassphraseEncryptedOutputStream (com.unboundid.util.PassphraseEncryptedOutputStream)29 FileOutputStream (java.io.FileOutputStream)29 GZIPOutputStream (java.util.zip.GZIPOutputStream)25 File (java.io.File)23 OutputStream (java.io.OutputStream)14 Test (org.testng.annotations.Test)14 Entry (com.unboundid.ldap.sdk.Entry)11 FileInputStream (java.io.FileInputStream)11 LDIFReader (com.unboundid.ldif.LDIFReader)10 LDIFWriter (com.unboundid.ldif.LDIFWriter)10 InputStream (java.io.InputStream)10 ByteArrayInputStream (java.io.ByteArrayInputStream)9 LDAPException (com.unboundid.ldap.sdk.LDAPException)8 NotNull (com.unboundid.util.NotNull)8 ArgumentException (com.unboundid.util.args.ArgumentException)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)8 PrintStream (java.io.PrintStream)8 PrintWriter (java.io.PrintWriter)7 IOException (java.io.IOException)5 InMemoryDirectoryServer (com.unboundid.ldap.listener.InMemoryDirectoryServer)4