-
Notifications
You must be signed in to change notification settings - Fork 276
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds tests for CertificateGenerator class
Signed-off-by: Darshit Chanpura <dchanp@amazon.com>
- Loading branch information
1 parent
6163459
commit e6e3b89
Showing
1 changed file
with
84 additions
and
1 deletion.
There are no files selected for viewing
85 changes: 84 additions & 1 deletion
85
src/test/java/org/opensearch/security/tools/democonfig/CertificateGeneratorTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,86 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
* | ||
* Modifications Copyright OpenSearch Contributors. See | ||
* GitHub history for details. | ||
*/ | ||
|
||
package org.opensearch.security.tools.democonfig; | ||
|
||
public class CertificateGeneratorTests {} | ||
import java.io.File; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
|
||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import org.opensearch.security.tools.democonfig.util.NoExitSecurityManager; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.containsString; | ||
import static org.hamcrest.Matchers.equalTo; | ||
import static org.hamcrest.Matchers.is; | ||
import static org.hamcrest.Matchers.not; | ||
import static org.opensearch.security.tools.democonfig.Installer.OPENSEARCH_CONF_DIR; | ||
import static org.opensearch.security.tools.democonfig.util.DemoConfigHelperUtil.createDirectory; | ||
import static org.opensearch.security.tools.democonfig.util.DemoConfigHelperUtil.deleteDirectoryRecursive; | ||
import static org.junit.Assert.fail; | ||
|
||
public class CertificateGeneratorTests { | ||
|
||
@Before | ||
public void setUp() { | ||
OPENSEARCH_CONF_DIR = System.getProperty("user.dir") + File.separator + "test-conf"; | ||
createDirectory(OPENSEARCH_CONF_DIR); | ||
} | ||
|
||
@After | ||
public void tearDown() { | ||
deleteDirectoryRecursive(OPENSEARCH_CONF_DIR); | ||
} | ||
|
||
@Test | ||
public void testCreateDemoCertificates() { | ||
CertificateGenerator certificateGenerator = new CertificateGenerator(); | ||
Certificates[] certificatesArray = Certificates.values(); | ||
|
||
certificateGenerator.createDemoCertificates(); | ||
|
||
for (Certificates cert : certificatesArray) { | ||
String certFilePath = OPENSEARCH_CONF_DIR + File.separator + cert.getFileName(); | ||
File certFile = new File(certFilePath); | ||
assertThat(certFile.exists(), is(equalTo(true))); | ||
assertThat(certFile.canRead(), is(equalTo(true))); | ||
|
||
String fileContents = null; | ||
try { | ||
fileContents = new String(Files.readAllBytes(Path.of(certFilePath))); | ||
} catch (Exception e) { | ||
fail("Expected the test to pass."); | ||
} | ||
|
||
assertThat(fileContents.isEmpty(), not(true)); | ||
assertThat(fileContents, containsString("---BEGIN")); | ||
assertThat(fileContents, containsString("---END")); | ||
} | ||
} | ||
|
||
@Test | ||
public void testCreateDemoCertificates_invalidPath() { | ||
OPENSEARCH_CONF_DIR = "invalidPath"; | ||
CertificateGenerator certificateGenerator = new CertificateGenerator(); | ||
try { | ||
System.setSecurityManager(new NoExitSecurityManager()); | ||
certificateGenerator.createDemoCertificates(); | ||
} catch (SecurityException e) { | ||
assertThat(e.getMessage(), equalTo("System.exit(-1) blocked to allow print statement testing.")); | ||
} finally { | ||
System.setSecurityManager(null); | ||
} | ||
} | ||
} |