diff --git a/backend/CMakeLists.txt b/backend/CMakeLists.txt index 7618728..4f152d4 100644 --- a/backend/CMakeLists.txt +++ b/backend/CMakeLists.txt @@ -1,4 +1,6 @@ cmake_minimum_required(VERSION 3.27) + +## application project(backend) find_package(Crow REQUIRED) @@ -7,6 +9,7 @@ find_package(SQLite3 REQUIRED) set(SOURCES src/main.cpp src/database.cpp + src/passwords.cpp ) set(CMAKE_EXPORT_COMPILE_COMMANDS ON) @@ -16,3 +19,20 @@ add_executable(${PROJECT_NAME} ${SOURCES}) target_link_libraries(${PROJECT_NAME} Crow::Crow SQLite::SQLite3) target_include_directories(${PROJECT_NAME} PRIVATE src) + +## tests +project(tests) + +find_package(Catch2 REQUIRED) + +set (TEST_SOURCE + tests/test.cpp +) + +add_executable(${PROJECT_NAME} ${TEST_SOURCE}) + +target_link_libraries(${PROJECT_NAME} PRIVATE Catch2::Catch2WithMain) + +target_include_directories(${PROJECT_NAME} PRIVATE tests) + +include(Catch) \ No newline at end of file diff --git a/backend/README.md b/backend/README.md index 2bdad04..6295eb0 100644 --- a/backend/README.md +++ b/backend/README.md @@ -30,3 +30,9 @@ To fix VS Code import errors with Crow, try adding the following line to your `s ``` [Source](https://stackoverflow.com/questions/58077908/linking-conan-include-to-vs-code) + +## Testing +After building, you can run tests from `/backend`: +```bash +cd build && ./tests +``` diff --git a/backend/conanfile.py b/backend/conanfile.py index e9af6e1..451d8fd 100644 --- a/backend/conanfile.py +++ b/backend/conanfile.py @@ -8,6 +8,7 @@ class BackendRecipe(ConanFile): def requirements(self): self.requires("crowcpp-crow/1.0+5") self.requires("sqlite3/3.42.0") + self.requires("catch2/3.4.0") def build_requirements(self): self.tool_requires("cmake/3.22.6") diff --git a/backend/src/main.cpp b/backend/src/main.cpp index df1c70e..7b61467 100644 --- a/backend/src/main.cpp +++ b/backend/src/main.cpp @@ -5,9 +5,10 @@ #include "crow.h" #include #include "database.hpp" +#include "passwords.hpp" int main() -{ +{ database::Database db = database::Database("passwords.db"); db.execute("CREATE TABLE passwords (password TEXT);"); db.execute("INSERT INTO passwords (password) VALUES ('chocolate1');"); diff --git a/backend/tests/test.cpp b/backend/tests/test.cpp index cad41b7..38b01c0 100644 --- a/backend/tests/test.cpp +++ b/backend/tests/test.cpp @@ -1,7 +1,29 @@ -#include -#include +#define CATCH_CONFIG_MAIN +#include +#include +#include "../src/passwords.cpp" -int main() +std::unordered_set passwordSet = passwords::generatePasswords(5, 24); + +bool hasLettersAndDigit(std::string str) +{ + const std::regex pattern("^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d).+$"); + return regex_match(str, pattern); +} + +bool hasSymbol(std::string str) +{ + return str.find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890_") != std::string::npos; +} + +TEST_CASE("Test basic password requirements") { - return 0; + // function generated 5 passwords + REQUIRE(passwordSet.size() == 5); + // a password has more than 10 characters + auto password = *(passwordSet.begin()); + REQUIRE(password.length() >= 10); + // a password meets all char requirements + REQUIRE(hasLettersAndDigit(password)); + REQUIRE(hasSymbol(password)); } \ No newline at end of file