Skip to content

Commit

Permalink
#83: added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
firaja committed Oct 18, 2022
1 parent 9303e33 commit 508e126
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 7 deletions.
7 changes: 3 additions & 4 deletions src/main/java/com/password4j/Password.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,9 @@
public class Password
{

private static final Logger LOGGER = LoggerFactory.getLogger(Password.class);

static {
Utils.printBanner();
static
{
Utils.printBanner(System.out);
}

private Password()
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/com/password4j/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package com.password4j;


import java.io.PrintStream;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
Expand Down Expand Up @@ -554,7 +555,7 @@ static String randomPrintable(int count)
return builder.toString();
}

static void printBanner()
static void printBanner(PrintStream printStream)
{
if (PropertyReader.readBoolean("global.banner", true))
{
Expand Down Expand Up @@ -583,7 +584,7 @@ static void printBanner()
banner += " ⭐ If you enjoy Password4j, please star the project at https://github.com/Password4j/password4j\n";
banner += " \uD83D\uDC1B Report any issue at https://github.com/Password4j/password4j/issues\n";

System.out.println(banner);
printStream.println(banner);

}
}
Expand Down
55 changes: 55 additions & 0 deletions src/test/com/password4j/PasswordTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,16 @@
import org.junit.Assert;
import org.junit.Test;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.net.URL;
import java.net.URLClassLoader;
import java.security.Provider;
import java.security.SecureRandom;
import java.security.Security;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import static org.junit.Assert.assertTrue;
Expand Down Expand Up @@ -791,4 +800,50 @@ public void real()
Assert.assertTrue(verified);
}

@Test
public void testBanner()
{
ByteArrayOutputStream outputStreamCaptor = new ByteArrayOutputStream();

PropertyReader.properties.setProperty("global.banner", "false");

Utils.printBanner(new PrintStream(outputStreamCaptor));
Assert.assertEquals(0, outputStreamCaptor.toString().length());

PropertyReader.properties.setProperty("global.banner", "true");

}

@Test
public void testBanner2()
{
ByteArrayOutputStream outputStreamCaptor = new ByteArrayOutputStream();

Provider[] oldProviders = Security.getProviders();
for (Provider provider : oldProviders)
{
for (Provider.Service service : provider.getServices())
{
if ("SecretKeyFactory".equals(service.getType()) && service.getAlgorithm().startsWith("PBKDF2"))
{
Security.removeProvider(provider.getName());
}
}
}

Utils.printBanner(new PrintStream(outputStreamCaptor));
Assert.assertTrue(outputStreamCaptor.toString().indexOf("❌") > 0);
Assert.assertTrue(outputStreamCaptor.toString().indexOf(System.getProperty("java.vm.name")) > 0);

for (Provider provider : oldProviders)
{
if (Security.getProvider(provider.getName()) == null)
{
Security.addProvider(provider);
}
}

}


}
2 changes: 1 addition & 1 deletion src/test/resources/psw4j.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
global.random.strong=false

global.banner=true
global.pepper=AlicePepper

hash.md.algorithm=SHA-512
Expand Down

0 comments on commit 508e126

Please sign in to comment.