broker/DBAuthenticatorTest: code formatter used
This commit is contained in:
@@ -1,15 +1,14 @@
|
|||||||
|
|
||||||
package io.moquette.spi.impl.security;
|
package io.moquette.spi.impl.security;
|
||||||
|
|
||||||
import org.apache.commons.codec.binary.Hex;
|
import org.apache.commons.codec.binary.Hex;
|
||||||
import org.junit.*;
|
import org.junit.*;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.security.MessageDigest;
|
import java.security.MessageDigest;
|
||||||
import java.security.NoSuchAlgorithmException;
|
import java.security.NoSuchAlgorithmException;
|
||||||
import java.sql.*;
|
import java.sql.*;
|
||||||
|
|
||||||
import static org.junit.Assert.assertFalse;
|
import static org.junit.Assert.assertFalse;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
@@ -25,7 +24,6 @@ public class DBAuthenticatorTest {
|
|||||||
public static final String SHA_256 = "SHA-256";
|
public static final String SHA_256 = "SHA-256";
|
||||||
private Connection connection;
|
private Connection connection;
|
||||||
|
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setup() throws ClassNotFoundException, SQLException, NoSuchAlgorithmException {
|
public void setup() throws ClassNotFoundException, SQLException, NoSuchAlgorithmException {
|
||||||
Class.forName(ORG_H2_DRIVER);
|
Class.forName(ORG_H2_DRIVER);
|
||||||
@@ -33,52 +31,62 @@ public class DBAuthenticatorTest {
|
|||||||
Statement statement = this.connection.createStatement();
|
Statement statement = this.connection.createStatement();
|
||||||
try {
|
try {
|
||||||
statement.execute("DROP TABLE ACCOUNT");
|
statement.execute("DROP TABLE ACCOUNT");
|
||||||
} catch(SQLException sqle) {
|
} catch (SQLException sqle) {
|
||||||
LOG.info("Table not found, not dropping",sqle);
|
LOG.info("Table not found, not dropping", sqle);
|
||||||
}
|
}
|
||||||
MessageDigest digest = MessageDigest.getInstance(SHA_256);
|
MessageDigest digest = MessageDigest.getInstance(SHA_256);
|
||||||
String hash = new String(Hex.encodeHex(digest.digest("password".getBytes(StandardCharsets.UTF_8))));
|
String hash = new String(Hex.encodeHex(digest.digest("password".getBytes(StandardCharsets.UTF_8))));
|
||||||
try {
|
try {
|
||||||
if (statement.execute("CREATE TABLE ACCOUNT ( LOGIN VARCHAR(64), PASSWORD VARCHAR(256))")){
|
if (statement.execute("CREATE TABLE ACCOUNT ( LOGIN VARCHAR(64), PASSWORD VARCHAR(256))")) {
|
||||||
throw new SQLException("can't create USER table");
|
throw new SQLException("can't create USER table");
|
||||||
}
|
}
|
||||||
if (statement.execute("INSERT INTO ACCOUNT ( LOGIN , PASSWORD ) VALUES ('dbuser', '"+hash+"')")){
|
if (statement.execute("INSERT INTO ACCOUNT ( LOGIN , PASSWORD ) VALUES ('dbuser', '" + hash + "')")) {
|
||||||
throw new SQLException("can't insert in USER table");
|
throw new SQLException("can't insert in USER table");
|
||||||
}
|
}
|
||||||
} catch(SQLException sqle) {
|
} catch (SQLException sqle) {
|
||||||
LOG.error("Table not created, not inserted",sqle);
|
LOG.error("Table not created, not inserted", sqle);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
LOG.info("Table User created");
|
LOG.info("Table User created");
|
||||||
statement.close();
|
statement.close();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void Db_verifyValid() {
|
public void Db_verifyValid() {
|
||||||
final DBAuthenticator dbAuthenticator = new DBAuthenticator(ORG_H2_DRIVER, JDBC_H2_MEM_TEST, "SELECT PASSWORD FROM ACCOUNT WHERE LOGIN=?", SHA_256);
|
final DBAuthenticator dbAuthenticator = new DBAuthenticator(
|
||||||
assertTrue(dbAuthenticator.checkValid(null, "dbuser","password".getBytes()));
|
ORG_H2_DRIVER,
|
||||||
|
JDBC_H2_MEM_TEST,
|
||||||
|
"SELECT PASSWORD FROM ACCOUNT WHERE LOGIN=?",
|
||||||
|
SHA_256);
|
||||||
|
assertTrue(dbAuthenticator.checkValid(null, "dbuser", "password".getBytes()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void Db_verifyInvalidLogin(){
|
public void Db_verifyInvalidLogin() {
|
||||||
final DBAuthenticator dbAuthenticator = new DBAuthenticator(ORG_H2_DRIVER, JDBC_H2_MEM_TEST, "SELECT PASSWORD FROM ACCOUNT WHERE LOGIN=?", SHA_256);
|
final DBAuthenticator dbAuthenticator = new DBAuthenticator(
|
||||||
assertFalse(dbAuthenticator.checkValid(null, "dbuser2","password".getBytes()));
|
ORG_H2_DRIVER,
|
||||||
|
JDBC_H2_MEM_TEST,
|
||||||
|
"SELECT PASSWORD FROM ACCOUNT WHERE LOGIN=?",
|
||||||
|
SHA_256);
|
||||||
|
assertFalse(dbAuthenticator.checkValid(null, "dbuser2", "password".getBytes()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void Db_verifyInvalidPassword(){
|
public void Db_verifyInvalidPassword() {
|
||||||
final DBAuthenticator dbAuthenticator = new DBAuthenticator(ORG_H2_DRIVER, JDBC_H2_MEM_TEST, "SELECT PASSWORD FROM ACCOUNT WHERE LOGIN=?", SHA_256);
|
final DBAuthenticator dbAuthenticator = new DBAuthenticator(
|
||||||
assertFalse(dbAuthenticator.checkValid(null, "dbuser","wrongPassword".getBytes()));
|
ORG_H2_DRIVER,
|
||||||
|
JDBC_H2_MEM_TEST,
|
||||||
|
"SELECT PASSWORD FROM ACCOUNT WHERE LOGIN=?",
|
||||||
|
SHA_256);
|
||||||
|
assertFalse(dbAuthenticator.checkValid(null, "dbuser", "wrongPassword".getBytes()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@After
|
@After
|
||||||
public void teardown(){
|
public void teardown() {
|
||||||
try {
|
try {
|
||||||
this.connection.close();
|
this.connection.close();
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
LOG.error("can't close connection",e);
|
LOG.error("can't close connection", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user