diff --git a/src/main/java/fr/xephi/authme/datasource/MySQL.java b/src/main/java/fr/xephi/authme/datasource/MySQL.java index 99ba7247e..e7d5c5653 100644 --- a/src/main/java/fr/xephi/authme/datasource/MySQL.java +++ b/src/main/java/fr/xephi/authme/datasource/MySQL.java @@ -863,23 +863,23 @@ public class MySQL implements DataSource { try (Connection con = getConnection()) { Statement st = con.createStatement(); ResultSet rs = st.executeQuery("SELECT * FROM " + tableName); - PreparedStatement pst = con.prepareStatement("SELECT data FROM xf_user_authenticate WHERE " + col.ID + "=?;"); while (rs.next()) { PlayerAuth pAuth = buildAuthFromResultSet(rs); if (hashAlgorithm == HashAlgorithm.XFBCRYPT) { - int id = rs.getInt(col.ID); - pst.setInt(1, id); - ResultSet rs2 = pst.executeQuery(); - if (rs2.next()) { - Blob blob = rs2.getBlob("data"); - byte[] bytes = blob.getBytes(1, (int) blob.length()); - pAuth.setPassword(new HashedPassword(XFBCRYPT.getHashFromBlob(bytes))); + try (PreparedStatement pst = con.prepareStatement("SELECT data FROM xf_user_authenticate WHERE " + col.ID + "=?;")) { + int id = rs.getInt(col.ID); + pst.setInt(1, id); + ResultSet rs2 = pst.executeQuery(); + if (rs2.next()) { + Blob blob = rs2.getBlob("data"); + byte[] bytes = blob.getBytes(1, (int) blob.length()); + pAuth.setPassword(new HashedPassword(XFBCRYPT.getHashFromBlob(bytes))); + } + rs2.close(); } - rs2.close(); } auths.add(pAuth); } - pst.close(); rs.close(); st.close(); } catch (SQLException ex) { diff --git a/src/main/java/fr/xephi/authme/settings/Settings.java b/src/main/java/fr/xephi/authme/settings/Settings.java index 8e5bf2c9e..53a8560cf 100644 --- a/src/main/java/fr/xephi/authme/settings/Settings.java +++ b/src/main/java/fr/xephi/authme/settings/Settings.java @@ -145,8 +145,6 @@ public final class Settings { denyTabcompleteBeforeLogin = load(RestrictionSettings.DENY_TABCOMPLETE_BEFORE_LOGIN); hideTablistBeforeLogin = load(RestrictionSettings.HIDE_TABLIST_BEFORE_LOGIN); - plugin.checkProtocolLib(); - passwordMaxLength = load(SecuritySettings.MAX_PASSWORD_LENGTH); backupWindowsPath = configFile.getString("BackupSystem.MysqlWindowsPath", "C:\\Program Files\\MySQL\\MySQL Server 5.1\\"); isStopEnabled = configFile.getBoolean("Security.SQLProblem.stopServer", true); diff --git a/src/main/java/fr/xephi/authme/settings/Spawn.java b/src/main/java/fr/xephi/authme/settings/Spawn.java index 275a8d0e0..85f20401d 100644 --- a/src/main/java/fr/xephi/authme/settings/Spawn.java +++ b/src/main/java/fr/xephi/authme/settings/Spawn.java @@ -23,13 +23,13 @@ public class Spawn extends CustomConfiguration { private Spawn() { super(new File(Settings.PLUGIN_FOLDER, "spawn.yml")); - reload(); + load(); + save(); + spawnPriority = Settings.spawnPriority.split(","); } public static void reload() { - getInstance().load(); - getInstance().save(); - spawnPriority = Settings.spawnPriority.split(","); + spawn = new Spawn(); } /** diff --git a/src/test/java/fr/xephi/authme/datasource/AbstractDataSourceIntegrationTest.java b/src/test/java/fr/xephi/authme/datasource/AbstractDataSourceIntegrationTest.java index eedd781fd..dfd6f8255 100644 --- a/src/test/java/fr/xephi/authme/datasource/AbstractDataSourceIntegrationTest.java +++ b/src/test/java/fr/xephi/authme/datasource/AbstractDataSourceIntegrationTest.java @@ -4,10 +4,13 @@ import fr.xephi.authme.cache.auth.PlayerAuth; import fr.xephi.authme.security.crypts.HashedPassword; import org.junit.Test; +import java.util.List; + import static fr.xephi.authme.datasource.AuthMeMatchers.equalToHash; import static fr.xephi.authme.datasource.AuthMeMatchers.hasAuthBasicData; import static fr.xephi.authme.datasource.AuthMeMatchers.hasAuthLocation; import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.Matchers.nullValue; import static org.junit.Assert.assertThat; @@ -98,13 +101,104 @@ public abstract class AbstractDataSourceIntegrationTest { // when int userMailCount = dataSource.countAuthsByEmail("user@example.ORG"); int invalidMailCount = dataSource.countAuthsByEmail("not.in.db@example.com"); - dataSource.saveAuth(PlayerAuth.builder().name("Test").email("user@EXAMPLE.org").build()); + boolean response = dataSource.saveAuth( + PlayerAuth.builder().name("Test").email("user@EXAMPLE.org").build()); int newUserCount = dataSource.countAuthsByEmail("user@Example.org"); // then assertThat(userMailCount, equalTo(1)); assertThat(invalidMailCount, equalTo(0)); + assertThat(response, equalTo(true)); assertThat(newUserCount, equalTo(2)); } + @Test + public void shouldReturnAllAuths() { + // given + DataSource dataSource = getDataSource(); + + // when + List authList = dataSource.getAllAuths(); + boolean response = dataSource.saveAuth( + PlayerAuth.builder().name("Test").email("user@EXAMPLE.org").build()); + List newAuthList = dataSource.getAllAuths(); + + // then + assertThat(response, equalTo(true)); + assertThat(authList, hasSize(2)); + assertThat(newAuthList, hasSize(3)); + boolean hasBobby = false; + for (PlayerAuth auth : authList) { + if (auth.getNickname().equals("bobby")) { + hasBobby = true; + break; + } + } + assertThat(hasBobby, equalTo(true)); + } + + @Test + public void shouldUpdatePassword() { + // given + DataSource dataSource = getDataSource(); + HashedPassword newHash = new HashedPassword("new_hash"); + + // when + boolean response1 = dataSource.updatePassword("user", newHash); + boolean response2 = dataSource.updatePassword("non-existent-name", new HashedPassword("sd")); + + // then + assertThat(response1 && response2, equalTo(true)); + assertThat(dataSource.getPassword("user"), equalToHash(newHash)); + } + + @Test + public void shouldRemovePlayerAuth() { + // given + DataSource dataSource = getDataSource(); + + // when + boolean response1 = dataSource.removeAuth("bobby"); + boolean response2 = dataSource.removeAuth("does-not-exist"); + + // then + assertThat(response1 && response2, equalTo(true)); + assertThat(dataSource.getAuth("bobby"), nullValue()); + assertThat(dataSource.isAuthAvailable("bobby"), equalTo(false)); + } + + @Test + public void shouldUpdateSession() { + // given + DataSource dataSource = getDataSource(); + PlayerAuth bobby = PlayerAuth.builder() + .name("bobby").realName("BOBBY").lastLogin(123L) + .ip("12.12.12.12").build(); + + // when + boolean response = dataSource.updateSession(bobby); + + // then + assertThat(response, equalTo(true)); + PlayerAuth result = dataSource.getAuth("bobby"); + assertThat(result, hasAuthBasicData("bobby", "BOBBY", "your@email.com", "12.12.12.12")); + assertThat(result.getLastLogin(), equalTo(123L)); + } + + @Test + public void shouldUpdateLastLoc() { + // given + DataSource dataSource = getDataSource(); + PlayerAuth user = PlayerAuth.builder() + .name("user").locX(143).locY(-42.12).locZ(29.47) + .locWorld("the_end").build(); + + // when + boolean response = dataSource.updateQuitLoc(user); + + // then + assertThat(response, equalTo(true)); + assertThat(dataSource.getAuth("user"), hasAuthLocation(143, -42.12, 29.47, "the_end")); + } + } diff --git a/src/test/java/fr/xephi/authme/datasource/AuthMeMatchers.java b/src/test/java/fr/xephi/authme/datasource/AuthMeMatchers.java index a1e347388..798d5315b 100644 --- a/src/test/java/fr/xephi/authme/datasource/AuthMeMatchers.java +++ b/src/test/java/fr/xephi/authme/datasource/AuthMeMatchers.java @@ -17,21 +17,26 @@ public final class AuthMeMatchers { } public static Matcher equalToHash(final String hash) { - return equalToHash(hash, null); + return equalToHash(new HashedPassword(hash)); } public static Matcher equalToHash(final String hash, final String salt) { + return equalToHash(new HashedPassword(hash, salt)); + } + + public static Matcher equalToHash(final HashedPassword hash) { return new TypeSafeMatcher() { @Override public boolean matchesSafely(HashedPassword item) { - return Objects.equals(hash, item.getHash()) && Objects.equals(salt, item.getSalt()); + return Objects.equals(hash.getHash(), item.getHash()) + && Objects.equals(hash.getSalt(), item.getSalt()); } @Override public void describeTo(Description description) { - String representation = "'" + hash + "'"; - if (salt != null) { - representation += ", '" + salt + "'"; + String representation = "'" + hash.getHash() + "'"; + if (hash.getSalt() != null) { + representation += ", '" + hash.getSalt() + "'"; } description.appendValue("HashedPassword(" + representation + ")"); } diff --git a/src/test/java/fr/xephi/authme/datasource/MySqlIntegrationTest.java b/src/test/java/fr/xephi/authme/datasource/MySqlIntegrationTest.java index 34a2a6afa..586ca0ad1 100644 --- a/src/test/java/fr/xephi/authme/datasource/MySqlIntegrationTest.java +++ b/src/test/java/fr/xephi/authme/datasource/MySqlIntegrationTest.java @@ -30,8 +30,8 @@ public class MySqlIntegrationTest extends AbstractDataSourceIntegrationTest { /** Mock of a settings instance. */ private static NewSetting settings; - /** Collection of SQL statements to execute for initialization of a test. */ - private static String[] sqlInitialize; + /** SQL statement to execute before running a test. */ + private static String sqlInitialize; /** Connection to the H2 test database. */ private HikariDataSource hikariSource; @@ -56,7 +56,7 @@ public class MySqlIntegrationTest extends AbstractDataSourceIntegrationTest { ConsoleLoggerTestInitializer.setupLogger(); Path sqlInitFile = TestHelper.getJarPath("/datasource-integration/sql-initialize.sql"); - sqlInitialize = new String(Files.readAllBytes(sqlInitFile)).split(";\\n"); + sqlInitialize = new String(Files.readAllBytes(sqlInitFile)); } @Before @@ -73,9 +73,7 @@ public class MySqlIntegrationTest extends AbstractDataSourceIntegrationTest { try (Statement st = connection.createStatement()) { st.execute("DROP TABLE IF EXISTS authme"); - for (String statement : sqlInitialize) { - st.execute(statement); - } + st.execute(sqlInitialize); } hikariSource = ds; } diff --git a/src/test/java/fr/xephi/authme/datasource/SQLiteIntegrationTest.java b/src/test/java/fr/xephi/authme/datasource/SQLiteIntegrationTest.java index 5c08b4790..715d2ce9b 100644 --- a/src/test/java/fr/xephi/authme/datasource/SQLiteIntegrationTest.java +++ b/src/test/java/fr/xephi/authme/datasource/SQLiteIntegrationTest.java @@ -57,7 +57,7 @@ public class SQLiteIntegrationTest extends AbstractDataSourceIntegrationTest { Path sqlInitFile = TestHelper.getJarPath("/datasource-integration/sql-initialize.sql"); // Note ljacqu 20160221: It appears that we can only run one statement per Statement.execute() so we split // the SQL file by ";\n" as to get the individual statements - sqlInitialize = new String(Files.readAllBytes(sqlInitFile)).split(";\\n"); + sqlInitialize = new String(Files.readAllBytes(sqlInitFile)).split(";(\\r?)\\n"); } @Before