Skip to content

Added a class Main #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.idea
target
*.iml
29 changes: 28 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,34 @@

<groupId>org.mindrot</groupId>
<artifactId>jbcrypt</artifactId>
<version>0.3</version>
<version>0.3.1</version>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<archive>
<manifest>
<mainClass>Main</mainClass>
<packageName></packageName>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
<packaging>jar</packaging>

<name>jbcrypt</name>
Expand Down
33 changes: 33 additions & 0 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import org.mindrot.*;

public class Main {

static MainUtil MAIN_UTIL = new MainUtil();

public static void main(String[] args) {
String password;
String salt;

if (args.length==0) {
System.out.println("Please specify a secret");
MAIN_UTIL.exit(1);
} else {
password = args[0];
salt = MAIN_UTIL.genSalt();
System.out.println(BCrypt.hashpw(password, salt));
MAIN_UTIL.exit(0);
}
}

}


class MainUtil {
public void exit(int exitCode) {
System.exit(exitCode);
}

public String genSalt() {
return BCrypt.gensalt();
}
}
63 changes: 63 additions & 0 deletions src/test/java/TestMain.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import java.io.*;

import junit.framework.TestCase;
import org.mindrot.*;


public class TestMain extends TestCase {
ByteArrayOutputStream stdout = new ByteArrayOutputStream();
String fixedSalt = "$2a$10$MkkJUCZctqHDtrcWu0gfOe";
StubbedMainUtil stubbedMainUtil = new StubbedMainUtil(fixedSalt);

@Override
public void setUp() throws Exception {
Main.MAIN_UTIL = stubbedMainUtil;
PrintStream printStream = new PrintStream(stdout);
System.setOut(printStream);
}

public void testIfArgEmptyShowInstruction() throws Exception {
Main.main(new String[0]);
assertEquals("Please specify a secret\n", stdout.toString());
}

public void testIfArgEmptyExitsWithStatusCode1() throws Exception {
Main.main(new String[0]);
assertEquals(1, stubbedMainUtil.lastExitCode);
}

public void testIfArgsProvidedReturnHashedSecret() throws Exception {
String secret = "pa$$w0rd";
String expectedHash = BCrypt.hashpw(secret, fixedSalt) + "\n";

Main.main(new String[]{secret});
String calculatedHash = stdout.toString();

assertEquals(expectedHash, calculatedHash);
}

public void testIfArgsProvidedExitsWithStatusCode0() throws Exception {
Main.main(new String[]{"dummy"});
assertEquals(0, stubbedMainUtil.lastExitCode);
}

private class StubbedMainUtil extends MainUtil {
final String fixedSalt;
int lastExitCode = Integer.MAX_VALUE;

public StubbedMainUtil(String fixedSalt) {
super();
this.fixedSalt = fixedSalt;
}

@Override
public void exit(int exitCode) {
lastExitCode = exitCode;
}

@Override
public String genSalt() {
return fixedSalt;
}
}
}