add password hash test (#4528)

* clangify tests

* add password hash test

* properly use googletest semantics
This commit is contained in:
ebbit1q 2022-01-16 22:32:11 +01:00 committed by GitHub
parent f6634de18d
commit 1e70989f38
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 125 additions and 81 deletions

View file

@ -1,11 +1,13 @@
enable_testing() enable_testing()
add_test(NAME dummy_test COMMAND dummy_test) add_test(NAME dummy_test COMMAND dummy_test)
add_test(NAME expression_test COMMAND expression_test) add_test(NAME expression_test COMMAND expression_test)
add_test(NAME password_hash_test COMMAND password_hash_test)
# Find GTest # Find GTest
add_executable(dummy_test dummy_test.cpp) add_executable(dummy_test dummy_test.cpp)
add_executable(expression_test expression_test.cpp) add_executable(expression_test expression_test.cpp)
add_executable(password_hash_test password_hash_test.cpp)
find_package(GTest) find_package(GTest)
@ -35,6 +37,7 @@ if(NOT GTEST_FOUND)
SET(GTEST_BOTH_LIBRARIES gtest) SET(GTEST_BOTH_LIBRARIES gtest)
add_dependencies(dummy_test gtest) add_dependencies(dummy_test gtest)
add_dependencies(expression_test gtest) add_dependencies(expression_test gtest)
add_dependencies(password_hash_test gtest)
endif() endif()
find_package(Qt5 COMPONENTS Widgets REQUIRED) find_package(Qt5 COMPONENTS Widgets REQUIRED)
@ -44,6 +47,7 @@ set(TEST_QT_MODULES Qt5::Widgets)
include_directories(${GTEST_INCLUDE_DIRS}) include_directories(${GTEST_INCLUDE_DIRS})
target_link_libraries(dummy_test Threads::Threads ${GTEST_BOTH_LIBRARIES}) target_link_libraries(dummy_test Threads::Threads ${GTEST_BOTH_LIBRARIES})
target_link_libraries(expression_test cockatrice_common Threads::Threads ${GTEST_BOTH_LIBRARIES} ${TEST_QT_MODULES}) target_link_libraries(expression_test cockatrice_common Threads::Threads ${GTEST_BOTH_LIBRARIES} ${TEST_QT_MODULES})
target_link_libraries(password_hash_test cockatrice_common Threads::Threads ${GTEST_BOTH_LIBRARIES} ${TEST_QT_MODULES})
add_subdirectory(carddatabase) add_subdirectory(carddatabase)
add_subdirectory(loading_from_clipboard) add_subdirectory(loading_from_clipboard)

View file

@ -1,7 +1,7 @@
#include "gtest/gtest.h"
#include "mocks.h" #include "mocks.h"
#include "gtest/gtest.h"
namespace namespace
{ {

View file

@ -1,23 +1,25 @@
#include "gtest/gtest.h"
#include "../../cockatrice/src/filter_string.h" #include "../../cockatrice/src/filter_string.h"
#include "mocks.h" #include "mocks.h"
#include "gtest/gtest.h"
#include <cmath> #include <cmath>
CardDatabase *db; CardDatabase *db;
#define Query(name, card, query, match) \ #define QUERY(name, card, query, match) \
TEST_F(CardQuery, name) {\ TEST_F(CardQuery, name) \
{ \
ASSERT_EQ(FilterString(query).check(card), match); \ ASSERT_EQ(FilterString(query).check(card), match); \
} }
namespace namespace
{ {
class CardQuery : public ::testing::Test { class CardQuery : public ::testing::Test
{
protected: protected:
void SetUp() override { void SetUp() override
{
cat = db->getCardBySimpleName("Cat"); cat = db->getCardBySimpleName("Cat");
} }
@ -26,32 +28,32 @@ class CardQuery : public ::testing::Test {
CardData cat; CardData cat;
}; };
Query(Empty, cat, "", true) QUERY(Empty, cat, "", true)
Query(Typing, cat, "t", true) QUERY(Typing, cat, "t", true)
Query(NonMatchingType, cat, "t:kithkin", false) QUERY(NonMatchingType, cat, "t:kithkin", false)
Query(MatchingType, cat, "t:creature", true) QUERY(MatchingType, cat, "t:creature", true)
Query(Not1, cat, "not t:kithkin", true) QUERY(Not1, cat, "not t:kithkin", true)
Query(Not2, cat, "not t:creature", false) QUERY(Not2, cat, "not t:creature", false)
Query(Case, cat, "t:cReAtUrE", true) QUERY(Case, cat, "t:cReAtUrE", true)
Query(And, cat, "t:creature t:creature", true) QUERY(And, cat, "t:creature t:creature", true)
Query(And2, cat, "t:creature t:sorcery", false) QUERY(And2, cat, "t:creature t:sorcery", false)
Query(Or, cat, "t:bat or t:creature", true) QUERY(Or, cat, "t:bat or t:creature", true)
Query(Cmc1, cat, "cmc=2", true) QUERY(Cmc1, cat, "cmc=2", true)
Query(Cmc2, cat, "cmc>3", false) QUERY(Cmc2, cat, "cmc>3", false)
Query(Cmc3, cat, "cmc>1", true) QUERY(Cmc3, cat, "cmc>1", true)
Query(Quotes, cat, "t:\"creature\"", true); QUERY(Quotes, cat, "t:\"creature\"", true)
Query(Field, cat, "pt:\"3/3\"", true) QUERY(Field, cat, "pt:\"3/3\"", true)
Query(Color1, cat, "c:g", true); QUERY(Color1, cat, "c:g", true)
Query(Color2, cat, "c:gw", true); QUERY(Color2, cat, "c:gw", true)
Query(Color3, cat, "c!g", true); QUERY(Color3, cat, "c!g", true)
Query(Color4, cat, "c!gw", false); QUERY(Color4, cat, "c!gw", false)
} // namespace } // namespace

View file

@ -41,14 +41,9 @@ QString SettingsCache::getSafeConfigFilePath(QString /* configEntry */, QString
return defaultPath; return defaultPath;
} }
SettingsCache::SettingsCache() SettingsCache::SettingsCache()
: settings{new QSettings("global.ini", QSettings::IniFormat, this)}, : settings{new QSettings("global.ini", QSettings::IniFormat, this)}, shortcutsSettings{nullptr},
shortcutsSettings{nullptr}, cardDatabaseSettings{new CardDatabaseSettings("", this)}, serversSettings{nullptr}, messageSettings{nullptr},
cardDatabaseSettings{new CardDatabaseSettings("", this)}, gameFiltersSettings{nullptr}, layoutsSettings{nullptr}, downloadSettings{nullptr},
serversSettings{nullptr},
messageSettings{nullptr},
gameFiltersSettings{nullptr},
layoutsSettings{nullptr},
downloadSettings{nullptr},
cardDatabasePath{QString("%1/cards.xml").arg(CARDDB_DATADIR)}, cardDatabasePath{QString("%1/cards.xml").arg(CARDDB_DATADIR)},
customCardDatabasePath{QString("%1/customsets/").arg(CARDDB_DATADIR)}, customCardDatabasePath{QString("%1/customsets/").arg(CARDDB_DATADIR)},
spoilerDatabasePath{QString("%1/spoiler.xml").arg(CARDDB_DATADIR)}, spoilerDatabasePath{QString("%1/spoiler.xml").arg(CARDDB_DATADIR)},

View file

@ -1,9 +1,11 @@
#include "gtest/gtest.h"
#include "../common/expression.h" #include "../common/expression.h"
#include "gtest/gtest.h"
#include <cmath> #include <cmath>
#define TEST_EXPR(name,a,b) TEST(name, Works) { \ #define TEST_EXPR(name, a, b) \
TEST(ExpressionTest, name) \
{ \
Expression exp(8); \ Expression exp(8); \
ASSERT_EQ(exp.parse(a), b) << a; \ ASSERT_EQ(exp.parse(a), b) << a; \
} }

View file

@ -1,4 +1,5 @@
#include "clipboard_testing.h" #include "clipboard_testing.h"
#include <QTextStream> #include <QTextStream>
void Result::operator()(const InnerDecklistNode *innerDecklistNode, const DecklistCardNode *card) void Result::operator()(const InnerDecklistNode *innerDecklistNode, const DecklistCardNode *card)

View file

@ -2,6 +2,7 @@
#define CLIPBOARD_TESTING_H #define CLIPBOARD_TESTING_H
#include "../../common/decklist.h" #include "../../common/decklist.h"
#include "gtest/gtest.h" #include "gtest/gtest.h"
struct Result struct Result

View file

@ -0,0 +1,39 @@
#include "../common/passwordhasher.h"
#include "../common/rng_abstract.h"
#include "../common/rng_sfmt.h"
#include "gtest/gtest.h"
RNG_Abstract *rng;
namespace
{
class PasswordHashTest : public ::testing::Test
{
protected:
void SetUp() override
{
rng = new RNG_SFMT;
}
void TearDown() override
{
delete rng;
}
};
TEST(PasswordHashTest, RegressionTest)
{
QString salt = "saltsaltsaltsalt";
QString password = "password";
QString expected = "vmKoWv975yf+WT2QCXhW48JNzZ2ghGxdgNvuKLBU0h7s6AQHSG72J6QO4ZswuSeqvBbAXbmgJSRBaSJrgc55WA==";
QString hash = PasswordHasher::computeHash(password, salt);
ASSERT_EQ(hash, salt + expected) << "The computed hash value remains the same";
}
} // namespace
int main(int argc, char **argv)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}