diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 80cc101..a6d82e6 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -7,58 +7,271 @@ on: branches: [master, staging] jobs: - doctests: + # ────────────────────────────────────────────── + # Core test matrix: OS × SQLite + # ────────────────────────────────────────────── + tests: runs-on: ${{ matrix.os }} timeout-minutes: 120 strategy: fail-fast: false matrix: - os: - - ubuntu-22.04 - - macos-15 - - windows-2022 - - include: + os: [ubuntu-22.04, ubuntu-24.04, macos-15, windows-2022] + sqlite: [OFF, ON] + exclude: + # Windows has no system sqlite3.lib - os: windows-2022 - cmake-generator: -G "Visual Studio 17 2022" -A x64 - cmake-install: "" #Already installed on hosted runner - dependencies: "" #Already installed on hosted runner - make: msbuild countly-tests.vcxproj -t:rebuild -verbosity:diag -property:Configuration=Release && .\Release\countly-tests.exe - - os: macos-15 - cmake-install: "" #Already installed on hosted runner - dependencies: "" #Already installed on hosted runner - make: make ./countly-tests && ./countly-tests - - os: ubuntu-22.04 - cmake-install: "" #Already installed on hosted runner - dependencies: | - sudo apt-get update && sudo apt-get install -y \ - libcurl4-openssl-dev \ - libssl-dev - make: make ./countly-tests && ./countly-tests + sqlite: ON steps: - name: Checkout code - uses: actions/checkout@v2 + uses: actions/checkout@v4 with: submodules: "recursive" - - name: Update submodules - run: git submodule update --init --recursive + - name: Install dependencies (Ubuntu) + if: startsWith(matrix.os, 'ubuntu') + run: | + sudo apt-get update && sudo apt-get install -y \ + libcurl4-openssl-dev \ + libssl-dev \ + libsqlite3-dev - - name: Install CMake - run: ${{ matrix.cmake-install }} + - name: Set up MSVC + if: matrix.os == 'windows-2022' + uses: microsoft/setup-msbuild@v2 - - name: Install dependencies - run: ${{ matrix.dependencies }} + - name: Configure (Unix) + if: matrix.os != 'windows-2022' + run: cmake -DCOUNTLY_BUILD_TESTS=1 -DCOUNTLY_USE_SQLITE=${{ matrix.sqlite }} -B build . + env: + CMAKE_POLICY_VERSION_MINIMUM: "3.31" - - name: Set up MSVC + - name: Configure (Windows) if: matrix.os == 'windows-2022' - uses: microsoft/setup-msbuild@v1 + run: cmake -DCOUNTLY_BUILD_TESTS=1 -DCOUNTLY_USE_SQLITE=${{ matrix.sqlite }} -G "Visual Studio 17 2022" -A x64 -B build . + env: + CMAKE_POLICY_VERSION_MINIMUM: "3.31" + + - name: Build (Unix) + if: matrix.os != 'windows-2022' + run: cd build && make ./countly-tests + + - name: Build (Windows) + if: matrix.os == 'windows-2022' + run: cd build && msbuild countly-tests.vcxproj -t:rebuild -verbosity:minimal -property:Configuration=Release + + - name: Run tests (Unix) + if: matrix.os != 'windows-2022' + run: cd build && ./countly-tests + + - name: Run tests (Windows) + if: matrix.os == 'windows-2022' + run: cd build && .\Release\countly-tests.exe + + # ────────────────────────────────────────────── + # Sanitizers (Linux only, with SQLite) + # ────────────────────────────────────────────── + sanitizer-asan: + runs-on: ubuntu-22.04 + timeout-minutes: 120 + steps: + - name: Checkout code + uses: actions/checkout@v4 + with: + submodules: "recursive" + + - name: Install dependencies + run: | + sudo apt-get update && sudo apt-get install -y \ + libcurl4-openssl-dev \ + libssl-dev \ + libsqlite3-dev + + - name: Configure with ASAN + run: | + cmake -DCOUNTLY_BUILD_TESTS=1 -DCOUNTLY_USE_SQLITE=ON \ + -DCMAKE_CXX_FLAGS="-fsanitize=address -fno-omit-frame-pointer" \ + -DCMAKE_EXE_LINKER_FLAGS="-fsanitize=address" \ + -DCMAKE_SHARED_LINKER_FLAGS="-fsanitize=address" \ + -B build . + env: + CMAKE_POLICY_VERSION_MINIMUM: "3.31" + + - name: Build + run: cd build && make ./countly-tests + + - name: Run tests + run: cd build && ./countly-tests + env: + ASAN_OPTIONS: "detect_leaks=1" + + sanitizer-tsan: + runs-on: ubuntu-22.04 + timeout-minutes: 120 + # TSAN detects known threading issues tracked for future refactor. + # Runs for visibility but does not block the pipeline. + continue-on-error: true + steps: + - name: Checkout code + uses: actions/checkout@v4 + with: + submodules: "recursive" + + - name: Install dependencies + run: | + sudo apt-get update && sudo apt-get install -y \ + libcurl4-openssl-dev \ + libssl-dev \ + libsqlite3-dev + + - name: Configure with TSAN + run: | + cmake -DCOUNTLY_BUILD_TESTS=1 -DCOUNTLY_USE_SQLITE=ON \ + -DCMAKE_CXX_FLAGS="-fsanitize=thread" \ + -DCMAKE_EXE_LINKER_FLAGS="-fsanitize=thread" \ + -DCMAKE_SHARED_LINKER_FLAGS="-fsanitize=thread" \ + -B build . + env: + CMAKE_POLICY_VERSION_MINIMUM: "3.31" + + - name: Build + run: cd build && make ./countly-tests + + - name: Run tests + run: cd build && ./countly-tests + env: + TSAN_OPTIONS: "second_deadlock_stack=1" + + sanitizer-ubsan: + runs-on: ubuntu-22.04 + timeout-minutes: 120 + steps: + - name: Checkout code + uses: actions/checkout@v4 + with: + submodules: "recursive" + + - name: Install dependencies + run: | + sudo apt-get update && sudo apt-get install -y \ + libcurl4-openssl-dev \ + libssl-dev \ + libsqlite3-dev - - name: Build and run tests + - name: Configure with UBSAN run: | - cmake -DCOUNTLY_BUILD_TESTS=1 -B build . ${{ matrix.cmake-generator }} - cd build - ${{ matrix.make }} + cmake -DCOUNTLY_BUILD_TESTS=1 -DCOUNTLY_USE_SQLITE=ON \ + -DCMAKE_CXX_FLAGS="-fsanitize=undefined -fno-sanitize-recover=all" \ + -DCMAKE_EXE_LINKER_FLAGS="-fsanitize=undefined" \ + -DCMAKE_SHARED_LINKER_FLAGS="-fsanitize=undefined" \ + -B build . + env: + CMAKE_POLICY_VERSION_MINIMUM: "3.31" + + - name: Build + run: cd build && make ./countly-tests + + - name: Run tests + run: cd build && ./countly-tests env: - CMAKE_POLICY_VERSION_MINIMUM: 3.31 + UBSAN_OPTIONS: "print_stacktrace=1" + + # ────────────────────────────────────────────── + # Static library build (custom HTTP, no curl) + # ────────────────────────────────────────────── + static-build: + runs-on: ubuntu-22.04 + timeout-minutes: 120 + steps: + - name: Checkout code + uses: actions/checkout@v4 + with: + submodules: "recursive" + + - name: Install dependencies + run: | + sudo apt-get update && sudo apt-get install -y \ + libssl-dev \ + libsqlite3-dev + + - name: Configure static build + run: | + cmake -DCOUNTLY_BUILD_TESTS=1 -DCOUNTLY_USE_SQLITE=ON \ + -DBUILD_SHARED_LIBS=OFF -DCOUNTLY_USE_CUSTOM_HTTP=ON \ + -B build . + env: + CMAKE_POLICY_VERSION_MINIMUM: "3.31" + + - name: Build + run: cd build && make ./countly-tests + + - name: Run tests + run: cd build && ./countly-tests + + # ────────────────────────────────────────────── + # C++17 compatibility check + # ────────────────────────────────────────────── + cpp17: + runs-on: ubuntu-24.04 + timeout-minutes: 120 + steps: + - name: Checkout code + uses: actions/checkout@v4 + with: + submodules: "recursive" + + - name: Install dependencies + run: | + sudo apt-get update && sudo apt-get install -y \ + libcurl4-openssl-dev \ + libssl-dev \ + libsqlite3-dev + + - name: Configure with C++17 + run: | + cmake -DCOUNTLY_BUILD_TESTS=1 -DCOUNTLY_USE_SQLITE=ON \ + -DCMAKE_CXX_STANDARD=17 \ + -B build . + env: + CMAKE_POLICY_VERSION_MINIMUM: "3.31" + + - name: Build + run: cd build && make ./countly-tests + + - name: Run tests + run: cd build && ./countly-tests + + # ────────────────────────────────────────────── + # Clang on Linux + # ────────────────────────────────────────────── + clang-linux: + runs-on: ubuntu-24.04 + timeout-minutes: 120 + steps: + - name: Checkout code + uses: actions/checkout@v4 + with: + submodules: "recursive" + + - name: Install dependencies + run: | + sudo apt-get update && sudo apt-get install -y \ + clang \ + libcurl4-openssl-dev \ + libssl-dev \ + libsqlite3-dev + + - name: Configure with Clang + run: | + cmake -DCOUNTLY_BUILD_TESTS=1 -DCOUNTLY_USE_SQLITE=ON \ + -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ \ + -B build . + env: + CMAKE_POLICY_VERSION_MINIMUM: "3.31" + + - name: Build + run: cd build && make ./countly-tests + + - name: Run tests + run: cd build && ./countly-tests diff --git a/.gitignore b/.gitignore index 93c1c62..51298a8 100644 --- a/.gitignore +++ b/.gitignore @@ -20,3 +20,7 @@ compile_commands.json CTestTestfile.cmake _deps cmake-build-debug/ + +.DS_Store +test_results_*.log +*.db diff --git a/CHANGELOG.md b/CHANGELOG.md index 23a645c..c32cefe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,17 @@ +## 26.1.0 +- ! Minor breaking change ! SDK Behavior Settings is now enabled by default. Changes made on SDK Manager > SDK Behavior Settings on your server will affect SDK behavior directly. + +- Added `enableImmediateRequestOnStop` configuration option. When enabled, the update loop uses a condition variable instead of polling, allowing `stop()` and `setUpdateInterval()` to take effect immediately rather than waiting for the current sleep interval to expire. +- Added init config method "disableSDKBehaviorSettingsUpdates" to disable periodic SBS updates from the server. +- Added init config method "setSDKBehaviorSettings" to provide server configuration in JSON format during initialization. + +- Fixed OpenSSL discovery in CMakeLists.txt to dynamically resolve the Homebrew prefix, supporting both Apple Silicon and Intel Macs. +- Added `dow` (day of week) and `hour` fields to every event. +- Added `dow`, `hour`, and `tz` (timezone offset in minutes) fields to every request. + +## 23.2.4 +- Mitigated an issue where cached events were not queued when a user property was recorded. + ## 23.2.3 - Mitigated an issue where the new device ID was used when ending a session if device ID was changed without merging. diff --git a/CMakeLists.txt b/CMakeLists.txt index 1d72104..ae71747 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -48,7 +48,8 @@ add_library(countly ${CMAKE_CURRENT_SOURCE_DIR}/src/request_builder.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/storage_module_db.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/storage_module_memory.cpp - ${CMAKE_CURRENT_SOURCE_DIR}/src/event.cpp) + ${CMAKE_CURRENT_SOURCE_DIR}/src/event.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/src/configuration_module.cpp) target_include_directories(countly PUBLIC $ @@ -75,7 +76,10 @@ if (COUNTLY_USE_CUSTOM_SHA256) else() if (APPLE) message("Setting openssl root for Mac") - set(OPENSSL_ROOT_DIR "/usr/local/opt/openssl") + execute_process(COMMAND brew --prefix openssl OUTPUT_VARIABLE OPENSSL_ROOT_DIR OUTPUT_STRIP_TRAILING_WHITESPACE) + if(NOT OPENSSL_ROOT_DIR) + set(OPENSSL_ROOT_DIR "/usr/local/opt/openssl") + endif() endif() find_package(OpenSSL REQUIRED) target_include_directories(countly PRIVATE ${OPENSSL_INCLUDE_DIR}) @@ -113,7 +117,9 @@ if(COUNTLY_BUILD_TESTS) ${CMAKE_CURRENT_SOURCE_DIR}/tests/event.cpp ${CMAKE_CURRENT_SOURCE_DIR}/tests/crash.cpp ${CMAKE_CURRENT_SOURCE_DIR}/tests/request.cpp - ${CMAKE_CURRENT_SOURCE_DIR}/tests/config.cpp) + ${CMAKE_CURRENT_SOURCE_DIR}/tests/config.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/tests/immediate_stop.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/tests/sbs.cpp) target_compile_options(countly-tests PRIVATE -g) target_compile_definitions(countly-tests PRIVATE COUNTLY_BUILD_TESTS) @@ -154,4 +160,4 @@ endif() CXX_EXTENSIONS NO) endif() -install(TARGETS countly ARCHIVE DESTINATION lib PUBLIC_HEADER DESTINATION include/countly) \ No newline at end of file +install(TARGETS countly ARCHIVE DESTINATION lib PUBLIC_HEADER DESTINATION include/countly) diff --git a/examples/example_integration.cpp b/examples/example_integration.cpp index 5e7410d..2189ae5 100644 --- a/examples/example_integration.cpp +++ b/examples/example_integration.cpp @@ -163,7 +163,7 @@ int main() { string _serverUrl = "https://your.server.ly"; if(_appKey.compare("YOUR_APP_KEY") == 0 || _serverUrl.compare("https://your.server.ly") == 0) { - cerr << "Please do not use default set of app key and server url" << endl; + printLog(LogLevel::WARNING, "[ExampleIntegration] Please do not use default set of app key and server url"); } ct.start(_appKey, _serverUrl, 443, true); @@ -276,7 +276,7 @@ int main() { flag = false; break; default: - cout << "Option not found!" << endl; + printLog(LogLevel::DEBUG, "[ExampleIntegration] Please do not use default set of app key and server url"); break; } } diff --git a/include/countly.hpp b/include/countly.hpp index fa985e3..64de2b7 100644 --- a/include/countly.hpp +++ b/include/countly.hpp @@ -4,7 +4,9 @@ #include "countly/constants.hpp" #include "countly/countly_configuration.hpp" +#include #include +#include #include #include #include @@ -24,6 +26,8 @@ #include "countly/logger_module.hpp" #include "countly/storage_module_base.hpp" #include "countly/views_module.hpp" +#include +#include #include #include #include @@ -58,6 +62,10 @@ class Countly : public cly::CountlyDelegates { void enableManualSessionControl(); + void disableAutoEventsOnUserProperties(); + + void enableImmediateRequestOnStop(); + void setHTTPClient(HTTPClientFunction fun); void setMetrics(const std::string &os, const std::string &os_version, const std::string &device, const std::string &resolution, const std::string &carrier, const std::string &app_version); @@ -255,16 +263,47 @@ class Countly : public cly::CountlyDelegates { addEvent(event); } + void RecordLocation(const std::string &countryCode, const std::string &city, const std::string &gpsCoordinates, const std::string &ipAddress) override { setLocation(countryCode, city, gpsCoordinates, ipAddress); }; + /* Provide 'updateInterval' in seconds. */ inline void setAutomaticSessionUpdateInterval(unsigned short updateInterval) { if (is_sdk_initialized) { - log(LogLevel::WARNING, "[Countly][setAutomaticSessionUpdateInterval] You can not set the session duration after SDK initialization."); + log(LogLevel::WARNING, "[Countly]setAutomaticSessionUpdateInterval, You can not set the session duration after SDK initialization."); return; } configuration->sessionDuration = updateInterval; } + /** + * Disable SDK behavior settings updates that SDK performs periodically from the server. + */ + void disableSDKBehaviorSettingsUpdates() { + if (is_sdk_initialized) { + log(LogLevel::WARNING, "[Countly] disableSDKBehaviorSettingsUpdates, You can not disable SDK behavior settings updates after SDK initialization."); + return; + } + + configuration->sdkBehaviorSettingsUpdatesDisabled = true; + } + + /** + * Provide SDK behavior settings in JSON format string. + */ + void setSDKBehaviorSettings(std::string &settings_json) { + if (is_sdk_initialized) { + log(LogLevel::WARNING, "[Countly] setSDKBehaviorSettings, You can not provide SDK behavior settings after SDK initialization."); + return; + } + + if(settings_json.empty()) { + log(LogLevel::WARNING, "[Countly] setSDKBehaviorSettings, Provided SDK behavior settings is empty."); + return; + } + + configuration->sdkBehaviorSettings = settings_json; + } + #ifdef COUNTLY_BUILD_TESTS /** * Convert event queue into list. @@ -328,8 +367,8 @@ class Countly : public cly::CountlyDelegates { void updateLoop(); void packEvents(); bool began_session = false; - bool is_being_disposed = false; - bool is_sdk_initialized = false; + std::atomic is_being_disposed{false}; + std::atomic is_sdk_initialized{false}; std::chrono::system_clock::time_point last_sent_session_request; nlohmann::json session_params; @@ -343,12 +382,14 @@ class Countly : public cly::CountlyDelegates { std::shared_ptr requestBuilder; std::shared_ptr requestModule; std::shared_ptr storageModule; + std::shared_ptr configurationModule; std::shared_ptr mutex = std::make_shared(); bool is_queue_being_processed = false; - bool enable_automatic_session = false; - bool stop_thread = false; - bool running = false; + std::atomic enable_automatic_session{false}; + std::atomic stop_thread{false}; + std::atomic running{false}; + std::condition_variable stop_cv; // Wakes updateLoop immediately on stop size_t wait_milliseconds = COUNTLY_KEEPALIVE_INTERVAL; size_t max_events = COUNTLY_MAX_EVENTS_DEFAULT; diff --git a/include/countly/configuration_module.hpp b/include/countly/configuration_module.hpp new file mode 100644 index 0000000..396ea93 --- /dev/null +++ b/include/countly/configuration_module.hpp @@ -0,0 +1,57 @@ +#ifndef CONFIGURATION_MODULE_HPP_ +#define CONFIGURATION_MODULE_HPP_ + +#include "countly/configuration_provider.hpp" +#include "countly/constants.hpp" +#include "countly/countly_configuration.hpp" +#include "countly/logger_module.hpp" +#include "countly/request_builder.hpp" +#include "countly/request_module.hpp" +#include "countly/storage_module_base.hpp" + +#include +#include + +namespace cly { + +template +struct FilterList { + T filterList; + bool isWhitelist = false; +}; + +class ConfigurationModule : public ConfigurationProvider { + +public: + ~ConfigurationModule(); + ConfigurationModule(cly::CountlyDelegates *cly, std::shared_ptr config, std::shared_ptr logger, std::shared_ptr requestBuilder, std::shared_ptr storageModule, std::shared_ptr requestModule, + std::shared_ptr mutex); + + void fetchConfigFromServer(nlohmann::json session_params); + void fetchConfigFromStorage(); + void startServerConfigUpdateTimer(nlohmann::json session_params); + void stopTimer(); + bool isTrackingEnabled() const override; + bool isNetworkingEnabled() const override; + + bool isLocationTrackingEnabled(); + bool isViewTrackingEnabled() const override; + bool isSessionTrackingEnabled(); + bool isCustomEventTrackingEnabled(); + bool isCrashReportingEnabled() const override; + + unsigned int getRequestQueueSizeLimit() const override; + unsigned int getEventQueueSizeLimit(); + unsigned int getSessionUpdateInterval(); + + FilterList> getEventFilterList() const; + FilterList> getUserPropertyFilterList() const; + FilterList> getSegmentationFilterList() const; + FilterList>> getEventSegmentationFilterList() const; + +private: + class ConfigurationModuleImpl; + std::unique_ptr impl; +}; +} // namespace cly +#endif diff --git a/include/countly/configuration_provider.hpp b/include/countly/configuration_provider.hpp new file mode 100644 index 0000000..e573de0 --- /dev/null +++ b/include/countly/configuration_provider.hpp @@ -0,0 +1,16 @@ +#ifndef CONFIGURATION_PROVIDER_HPP_ +#define CONFIGURATION_PROVIDER_HPP_ +namespace cly { + +class ConfigurationProvider { +public: + virtual ~ConfigurationProvider() = default; + + virtual bool isNetworkingEnabled() const = 0; + virtual bool isTrackingEnabled() const = 0; + virtual bool isCrashReportingEnabled() const = 0; + virtual bool isViewTrackingEnabled() const = 0; + virtual unsigned int getRequestQueueSizeLimit() const = 0; +}; +} // namespace cly +#endif \ No newline at end of file diff --git a/include/countly/constants.hpp b/include/countly/constants.hpp index 5ea5367..9626997 100644 --- a/include/countly/constants.hpp +++ b/include/countly/constants.hpp @@ -13,7 +13,7 @@ #include #define COUNTLY_SDK_NAME "cpp-native-unknown" -#define COUNTLY_SDK_VERSION "23.2.3" +#define COUNTLY_SDK_VERSION "26.1.0" #define COUNTLY_POST_THRESHOLD 2000 #define COUNTLY_KEEPALIVE_INTERVAL 3000 #define COUNTLY_MAX_EVENTS_DEFAULT 200 @@ -93,6 +93,8 @@ class CountlyDelegates { virtual void RecordEvent(const std::string &key, const std::map &segmentation, int count, double sum) = 0; virtual void RecordEvent(const std::string &key, const std::map &segmentation, int count, double sum, double duration) = 0; + + virtual void RecordLocation(const std::string &countryCode, const std::string &city, const std::string &gpsCoordinates, const std::string &ipAddress) = 0; }; } // namespace cly diff --git a/include/countly/countly_configuration.hpp b/include/countly/countly_configuration.hpp index c98d500..cee2aef 100644 --- a/include/countly/countly_configuration.hpp +++ b/include/countly/countly_configuration.hpp @@ -70,10 +70,23 @@ struct CountlyConfiguration { bool manualSessionControl = false; + bool autoEventsOnUserProperties = true; + + /** + * Enable immediate stop notification using a condition variable. + * When enabled, the update loop wakes immediately on stop instead of + * waiting for the current sleep interval to expire. + */ + bool immediateRequestOnStop = false; + HTTPClientFunction http_client_function = nullptr; nlohmann::json metrics; + bool sdkBehaviorSettingsUpdatesDisabled = false; + + std::string sdkBehaviorSettings; + CountlyConfiguration(const std::string appKey, std::string serverUrl) { this->appKey = appKey; this->serverUrl = serverUrl; diff --git a/include/countly/crash_module.hpp b/include/countly/crash_module.hpp index 1cabe7e..e2bbadc 100644 --- a/include/countly/crash_module.hpp +++ b/include/countly/crash_module.hpp @@ -35,8 +35,10 @@ class CrashModule { void recordException(const std::string &title, const std::string &stackTrace, const bool fatal, const std::map &crashMetrics, const std::map &segmentation = {}); private: + friend class Countly; class CrashModuleImpl; std::unique_ptr impl; + void setConfigurationProvider(std::weak_ptr provider); // try injecting }; } // namespace cly #endif diff --git a/include/countly/event.hpp b/include/countly/event.hpp index 3f57064..74d700a 100644 --- a/include/countly/event.hpp +++ b/include/countly/event.hpp @@ -27,6 +27,14 @@ class Event { std::string serialize() const; + std::string getKey() const; + + bool hasSegmentation() const; + + void removeSegmentation(const std::string &key); + + void clearSegmentation(); + private: nlohmann::json object; bool timer_running; diff --git a/include/countly/request_builder.hpp b/include/countly/request_builder.hpp index ef022be..8d765b5 100644 --- a/include/countly/request_builder.hpp +++ b/include/countly/request_builder.hpp @@ -3,6 +3,7 @@ #include "countly/countly_configuration.hpp" #include "countly/logger_module.hpp" #include "nlohmann/json.hpp" +#include #include #include diff --git a/include/countly/request_module.hpp b/include/countly/request_module.hpp index 7d494a1..3c5087c 100644 --- a/include/countly/request_module.hpp +++ b/include/countly/request_module.hpp @@ -9,6 +9,7 @@ #include "countly/logger_module.hpp" #include "countly/request_builder.hpp" #include "countly/storage_module_base.hpp" +#include "countly/configuration_provider.hpp" namespace cly { class RequestModule { @@ -36,10 +37,12 @@ class RequestModule { void clearRequestQueue(); long long RQSize(); + void setConfigurationProvider(std::weak_ptr provider); // try injecting private: class RequestModuleImpl; std::unique_ptr impl; + std::weak_ptr _configProvider; }; } // namespace cly #endif diff --git a/include/countly/storage_module_base.hpp b/include/countly/storage_module_base.hpp index 1cda567..507a35e 100644 --- a/include/countly/storage_module_base.hpp +++ b/include/countly/storage_module_base.hpp @@ -94,6 +94,10 @@ class StorageModuleBase { * @param request: content of the request */ virtual void RQInsertAtEnd(const std::string &request) = 0; + + virtual void storeSDKBehaviorSettings(const std::string &sdk_behavior_settings) = 0; + + virtual std::string getSDKBehaviorSettings() = 0; }; } // namespace cly diff --git a/include/countly/storage_module_db.hpp b/include/countly/storage_module_db.hpp index 47bf852..a57022c 100644 --- a/include/countly/storage_module_db.hpp +++ b/include/countly/storage_module_db.hpp @@ -24,6 +24,8 @@ class StorageModuleDB : public StorageModuleBase { std::vector> RQPeekAll() override; void RQRemoveFront(std::shared_ptr request) override; void RQInsertAtEnd(const std::string &request) override; + void storeSDKBehaviorSettings(const std::string &sdk_behavior_settings) override; + std::string getSDKBehaviorSettings() override; }; } // namespace cly #endif \ No newline at end of file diff --git a/include/countly/storage_module_memory.hpp b/include/countly/storage_module_memory.hpp index f1e3468..89fc3e7 100644 --- a/include/countly/storage_module_memory.hpp +++ b/include/countly/storage_module_memory.hpp @@ -26,6 +26,8 @@ class StorageModuleMemory : public StorageModuleBase { std::vector> RQPeekAll() override; void RQRemoveFront(std::shared_ptr request) override; void RQInsertAtEnd(const std::string &request) override; + void storeSDKBehaviorSettings(const std::string &sdk_behavior_settings) override; + std::string getSDKBehaviorSettings() override; }; } // namespace cly #endif \ No newline at end of file diff --git a/include/countly/views_module.hpp b/include/countly/views_module.hpp index a7a3d09..1f48006 100644 --- a/include/countly/views_module.hpp +++ b/include/countly/views_module.hpp @@ -4,6 +4,7 @@ #include #include +#include "countly/configuration_provider.hpp" #include "countly/constants.hpp" #include "countly/logger_module.hpp" @@ -36,10 +37,13 @@ class ViewsModule { */ std::string openView(const std::string &name, const std::map &segmentation = {}); + private: + friend class Countly; void _recordView(std::string eventID); class ViewModuleImpl; std::unique_ptr impl; + void setConfigurationProvider(std::weak_ptr provider); // try injecting }; } // namespace cly #endif diff --git a/src/configuration_module.cpp b/src/configuration_module.cpp new file mode 100644 index 0000000..883106d --- /dev/null +++ b/src/configuration_module.cpp @@ -0,0 +1,508 @@ +#include "countly/configuration_module.hpp" +#include +#include +#include + +namespace cly { +// some keys do not have feature yet, but reserved for future use. +static constexpr const char *KEY_TIMESTAMP = "t"; // not used yet +static constexpr const char *KEY_CONFIG = "c"; // used +static constexpr const char *KEY_VERSION = "v"; // not used yet + +static constexpr const char *KEY_TRACKING = "tracking"; +static constexpr const char *KEY_NETWORKING = "networking"; + +static constexpr const char *KEY_REQ_QUEUE_SIZE = "rqs"; +static constexpr const char *KEY_EVENT_QUEUE_SIZE = "eqs"; +static constexpr const char *KEY_SESSION_UPDATE_INTERVAL = "sui"; +static constexpr const char *KEY_SESSION_TRACKING = "st"; +static constexpr const char *KEY_VIEW_TRACKING = "vt"; +static constexpr const char *KEY_LOCATION_TRACKING = "lt"; +static constexpr const char *KEY_CUSTOM_EVENT_TRACKING = "cet"; +static constexpr const char *KEY_CRASH_REPORTING = "crt"; +static constexpr const char *KEY_SERVER_CONFIG_UPDATE_INTERVAL = "scui"; +static constexpr const char *KEY_LOGGING = "log"; // not used and implemented yet + +// whitelist / blacklist +static constexpr const char *KEY_EVENT_BLACKLIST = "eb"; +static constexpr const char *KEY_USER_PROPERTY_BLACKLIST = "upb"; +static constexpr const char *KEY_SEGMENTATION_BLACKLIST = "sb"; +static constexpr const char *KEY_EVENT_SEGMENTATION_BLACKLIST = "esb"; +static constexpr const char *KEY_EVENT_WHITELIST = "ew"; +static constexpr const char *KEY_USER_PROPERTY_WHITELIST = "upw"; +static constexpr const char *KEY_SEGMENTATION_WHITELIST = "sw"; +static constexpr const char *KEY_EVENT_SEGMENTATION_WHITELIST = "esw"; + +// sdk configuration - not implemented yet +static constexpr const char *KEY_CONSENT_REQUIRED = "cr"; +static constexpr const char *KEY_DROP_OLD_REQUEST_TIME = "dort"; + +// sdk internal limits - not implemented yet +static constexpr const char *KEY_LIMIT_KEY_LENGTH = "lkl"; +static constexpr const char *KEY_LIMIT_VALUE_SIZE = "lvs"; +static constexpr const char *KEY_LIMIT_SEG_VALUES = "lsv"; +static constexpr const char *KEY_LIMIT_BREADCRUMB = "lbc"; +static constexpr const char *KEY_LIMIT_TRACE_LINE = "ltlpt"; +static constexpr const char *KEY_LIMIT_TRACE_LENGTH = "ltl"; +// -- This limit is introduced lately and experimental +static constexpr const char *KEY_USER_PROPERTY_CACHE_LIMIT = "upcl"; + +// backoff mechanism - not implemented yet +static constexpr const char *KEY_BACKOFF_MECHANISM = "bom"; +static constexpr const char *KEY_BOM_ACCEPTED_TIMEOUT = "bom_at"; +static constexpr const char *KEY_BOM_RQ_PERCENTAGE = "bom_rqp"; +static constexpr const char *KEY_BOM_REQUEST_AGE = "bom_ra"; +static constexpr const char *KEY_BOM_DURATION = "bom_d"; + +class ConfigurationModule::ConfigurationModuleImpl { +private: + std::shared_ptr _requestBuilder; + std::shared_ptr _storageModule; + std::shared_ptr _requestModule; + cly::CountlyDelegates *_cly; + nlohmann::json sdk_behavior_settings; + +public: + std::shared_ptr _logger; + std::shared_ptr _mutex; + std::shared_ptr _configuration; + + std::atomic stopConfigThread{false}; + std::thread configUpdateThread; + std::mutex configUpdateMutex; + std::condition_variable configUpdateCv; + + // current settings cached for quick access + std::atomic networkingEnabled{true}; + std::atomic trackingEnabled{true}; + std::atomic sessionTrackingEnabled{true}; + std::atomic viewTrackingEnabled{true}; + std::atomic locationTrackingEnabled{true}; + std::atomic customEventTrackingEnabled{true}; + std::atomic crashReportingEnabled{true}; + std::atomic eventQueueThreshold{0}; + std::atomic requestQueueSizeLimit{0}; + std::atomic sessionUpdateInterval{0}; + std::atomic serverConfigUpdateInterval{4}; + + mutable std::mutex sbsMutex; + std::thread configFetchThread; + + mutable std::mutex filterMutex; + FilterList> eventFilter; + FilterList> userPropertyFilter; + FilterList> segmentationFilter; + FilterList>> eventSegmentationFilter; + + ConfigurationModuleImpl(cly::CountlyDelegates *cly, std::shared_ptr config, std::shared_ptr logger, std::shared_ptr requestBuilder, std::shared_ptr storageModule, std::shared_ptr requestModule, + std::shared_ptr mutex) + : _configuration(config), _logger(logger), _requestBuilder(requestBuilder), _storageModule(storageModule), _requestModule(requestModule), _mutex(mutex), _cly(cly) {} + + std::set parseStringArray(const nlohmann::json &arr) const { + std::set result; + if (arr.is_array()) { + for (const auto &item : arr) { + if (item.is_string()) { + result.insert(item.get()); + } + } + } + return result; + } + + std::map> parseEventSegmentationMap(const nlohmann::json &obj) const { + std::map> result; + if (obj.is_object()) { + for (auto it = obj.begin(); it != obj.end(); ++it) { + if (it.value().is_array()) { + result[it.key()] = parseStringArray(it.value()); + } + } + } + return result; + } + + // Helper to populate a list filter from blacklist/whitelist keys, reducing duplication + template + void populateListFilter(FilterList &filter, const char *blacklistKey, const char *whitelistKey, ParseFunc parseFunc) { + if (sdk_behavior_settings.contains(blacklistKey)) { + filter.isWhitelist = false; + filter.filterList = parseFunc(sdk_behavior_settings[blacklistKey]); + } else if (sdk_behavior_settings.contains(whitelistKey)) { + filter.isWhitelist = true; + filter.filterList = parseFunc(sdk_behavior_settings[whitelistKey]); + } else { + filter.filterList.clear(); + filter.isWhitelist = false; + } + } + + void _fetchConfigFromServerHTTP(const std::map &data, const nlohmann::json &session_params) { + try { + HTTPResponse response = _requestModule->sendHTTP("/o/sdk", _requestBuilder->serializeData(data)); + if (response.success && response.data.is_object() && response.data.contains(KEY_CONFIG)) { + nlohmann::json changedSettings; + { + std::lock_guard lock(sbsMutex); + sanitizeConfig(response.data[KEY_CONFIG]); + sdk_behavior_settings = response.data[KEY_CONFIG]; + _storageModule->storeSDKBehaviorSettings(sdk_behavior_settings.dump()); + _logger->log(LogLevel::INFO, "[Countly] [ConfigurationModule] _fetchConfigFromServerHTTP, SDK config:\n" + sdk_behavior_settings.dump(2)); + changedSettings = _populateConfigValues(); + } + _onSBSChanged(changedSettings, session_params); + } else { + _logger->log(LogLevel::WARNING, + "[Countly] [ConfigurationModule] _fetchConfigFromServerHTTP, failed to fetch." + " success=" + + std::string(response.success ? "true" : "false") + ", is_object=" + std::string(response.data.is_object() ? "true" : "false") + + ", has_config=" + std::string((response.data.is_object() && response.data.contains(KEY_CONFIG)) ? "true" : "false") + ", response=" + response.data.dump()); + } + } catch (const std::exception &e) { + _logger->log(LogLevel::ERROR, "[Countly] [ConfigurationModule] _fetchConfigFromServerHTTP, exception: [" + std::string(e.what()) + "]"); + } + } + + // Returns changed settings JSON for the caller to pass to _onSBSChanged outside of _mutex + nlohmann::json _initializeSBSFromStorage() { + _initializeConfigParameters(); + std::string sbs_string = _storageModule->getSDKBehaviorSettings(); + if (!sbs_string.empty()) { + nlohmann::json changed = _processSDKBehaviorSettings(sbs_string); + _logger->log(LogLevel::INFO, "[Countly] [ConfigurationModule] _initializeSBSFromStorage, initialized SDK behavior settings from storage."); + return changed; + } else if (!_configuration->sdkBehaviorSettings.empty()) { + nlohmann::json changed = _processSDKBehaviorSettings(_configuration->sdkBehaviorSettings); + // Persist the provided SBS so it's available on future re-inits + _storageModule->storeSDKBehaviorSettings(sdk_behavior_settings.dump()); + _logger->log(LogLevel::INFO, "[Countly] [ConfigurationModule] _initializeSBSFromStorage, initialized SDK behavior settings from configuration."); + return changed; + } + return nlohmann::json{}; + } + + void _initializeConfigParameters() { + requestQueueSizeLimit.store(_configuration->requestQueueThreshold, std::memory_order_release); + sessionUpdateInterval.store(_configuration->sessionDuration, std::memory_order_release); + } + + nlohmann::json _processSDKBehaviorSettings(const std::string &settings) { + try { + std::lock_guard lock(sbsMutex); + nlohmann::json sbs_json = nlohmann::json::parse(settings); + sanitizeConfig(sbs_json); + sdk_behavior_settings = sbs_json; + _logger->log(LogLevel::INFO, "[Countly] [ConfigurationModule] _processSDKBehaviorSettings, SDK config:\n" + sdk_behavior_settings.dump(2)); + return _populateConfigValues(); + } catch (const nlohmann::json::parse_error &e) { + _logger->log(LogLevel::ERROR, "[Countly] [ConfigurationModule] _processSDKBehaviorSettings, Failed to parse SDK behavior settings: [" + std::string(e.what()) + "]"); + return nlohmann::json{}; + } + } + + void _onSBSChanged(const nlohmann::json &changedSettings, const nlohmann::json &session_params = nullptr) { + if (_configuration->sdkBehaviorSettingsUpdatesDisabled != true && changedSettings.contains(KEY_SERVER_CONFIG_UPDATE_INTERVAL)) { + // Wake the timer thread so it picks up the new interval on next iteration. + // The timer loop re-reads serverConfigUpdateInterval (atomic) each cycle, + // so we just need to interrupt the current wait. + configUpdateCv.notify_all(); + } + + if (changedSettings.contains(KEY_LOCATION_TRACKING) && changedSettings[KEY_LOCATION_TRACKING] == false) { + // disable location - safe because _onSBSChanged is called outside of _mutex + _cly->RecordLocation("", "", "", ""); + } + } + + nlohmann::json _populateConfigValues() { + bool trackingEnabledVal = trackingEnabled.load(std::memory_order_acquire); + bool networkingEnabledVal = networkingEnabled.load(std::memory_order_acquire); + bool sessionTrackingEnabledVal = sessionTrackingEnabled.load(std::memory_order_acquire); + bool viewTrackingEnabledVal = viewTrackingEnabled.load(std::memory_order_acquire); + bool locationTrackingEnabledVal = locationTrackingEnabled.load(std::memory_order_acquire); + bool customEventTrackingEnabledVal = customEventTrackingEnabled.load(std::memory_order_acquire); + bool crashReportingEnabledVal = crashReportingEnabled.load(std::memory_order_acquire); + unsigned int serverConfigUpdateIntervalVal = serverConfigUpdateInterval.load(std::memory_order_acquire); + + bool locationTrackingCurrent = getBool(KEY_LOCATION_TRACKING, locationTrackingEnabledVal); + unsigned int serverConfigUpdateIntervalCurrent = getUInt(KEY_SERVER_CONFIG_UPDATE_INTERVAL, serverConfigUpdateIntervalVal); + + trackingEnabled.store(getBool(KEY_TRACKING, trackingEnabledVal), std::memory_order_release); + networkingEnabled.store(getBool(KEY_NETWORKING, networkingEnabledVal), std::memory_order_release); + sessionTrackingEnabled.store(getBool(KEY_SESSION_TRACKING, sessionTrackingEnabledVal), std::memory_order_release); + viewTrackingEnabled.store(getBool(KEY_VIEW_TRACKING, viewTrackingEnabledVal), std::memory_order_release); + locationTrackingEnabled.store(locationTrackingCurrent, std::memory_order_release); + customEventTrackingEnabled.store(getBool(KEY_CUSTOM_EVENT_TRACKING, customEventTrackingEnabledVal), std::memory_order_release); + crashReportingEnabled.store(getBool(KEY_CRASH_REPORTING, crashReportingEnabledVal), std::memory_order_release); + eventQueueThreshold.store(getUInt(KEY_EVENT_QUEUE_SIZE, 0), std::memory_order_release); + + unsigned int rqs = getUInt(KEY_REQ_QUEUE_SIZE, _configuration->requestQueueThreshold); + if (rqs < 1) { + rqs = _configuration->requestQueueThreshold; + } + requestQueueSizeLimit.store(rqs, std::memory_order_release); + + unsigned int sui = getUInt(KEY_SESSION_UPDATE_INTERVAL, _configuration->sessionDuration); + if (sui < 1) { + sui = _configuration->sessionDuration; + } + sessionUpdateInterval.store(sui, std::memory_order_release); + + unsigned int scui = getUInt(KEY_SERVER_CONFIG_UPDATE_INTERVAL, 4); + if (scui < 1) { + scui = 4; + } else if (scui > 720) { + scui = 720; // cap at 30 days + } + serverConfigUpdateInterval.store(scui, std::memory_order_release); + + // Parse listing filters — blacklist takes precedence over whitelist for each type + { + std::lock_guard lock(filterMutex); + auto parseArray = [this](const nlohmann::json &j) { return parseStringArray(j); }; + auto parseMap = [this](const nlohmann::json &j) { return parseEventSegmentationMap(j); }; + populateListFilter(eventFilter, KEY_EVENT_BLACKLIST, KEY_EVENT_WHITELIST, parseArray); + populateListFilter(userPropertyFilter, KEY_USER_PROPERTY_BLACKLIST, KEY_USER_PROPERTY_WHITELIST, parseArray); + populateListFilter(segmentationFilter, KEY_SEGMENTATION_BLACKLIST, KEY_SEGMENTATION_WHITELIST, parseArray); + populateListFilter(eventSegmentationFilter, KEY_EVENT_SEGMENTATION_BLACKLIST, KEY_EVENT_SEGMENTATION_WHITELIST, parseMap); + } + + nlohmann::json changedSettings; + if (locationTrackingCurrent != locationTrackingEnabledVal) { + changedSettings[KEY_LOCATION_TRACKING] = locationTrackingCurrent; + } + if (serverConfigUpdateIntervalCurrent != serverConfigUpdateIntervalVal) { + changedSettings[KEY_SERVER_CONFIG_UPDATE_INTERVAL] = serverConfigUpdateIntervalCurrent; + } + return changedSettings; + } + + void sanitizeConfig(nlohmann::json &c) { + if (!c.is_object()) { + c.clear(); + return; + } + + for (auto it = c.begin(); it != c.end();) { + std::string key = it.key(); + auto value = it.value(); + if (key == KEY_REQ_QUEUE_SIZE || key == KEY_EVENT_QUEUE_SIZE || key == KEY_SESSION_UPDATE_INTERVAL || key == KEY_LIMIT_KEY_LENGTH || key == KEY_LIMIT_VALUE_SIZE || key == KEY_LIMIT_SEG_VALUES || key == KEY_LIMIT_BREADCRUMB || key == KEY_LIMIT_TRACE_LINE || key == KEY_LIMIT_TRACE_LENGTH || + key == KEY_USER_PROPERTY_CACHE_LIMIT || key == KEY_DROP_OLD_REQUEST_TIME || key == KEY_SERVER_CONFIG_UPDATE_INTERVAL) { + if (!value.is_number_unsigned()) { + it = c.erase(it); + continue; + } + } else if (key == KEY_TRACKING || key == KEY_NETWORKING || key == KEY_LOGGING || key == KEY_SESSION_TRACKING || key == KEY_VIEW_TRACKING || key == KEY_LOCATION_TRACKING || key == KEY_CUSTOM_EVENT_TRACKING || key == KEY_CONSENT_REQUIRED || key == KEY_CRASH_REPORTING) { + if (!value.is_boolean()) { + it = c.erase(it); + continue; + } + } else if (key == KEY_EVENT_BLACKLIST || key == KEY_USER_PROPERTY_BLACKLIST || key == KEY_SEGMENTATION_BLACKLIST || key == KEY_EVENT_WHITELIST || key == KEY_USER_PROPERTY_WHITELIST || key == KEY_SEGMENTATION_WHITELIST) { + if (!value.is_array()) { + it = c.erase(it); + continue; + } + } else if (key == KEY_EVENT_SEGMENTATION_BLACKLIST || key == KEY_EVENT_SEGMENTATION_WHITELIST) { + if (!value.is_object()) { + it = c.erase(it); + continue; + } + } else { + _logger->log(LogLevel::DEBUG, "[Countly] [ConfigurationModule] sanitizeConfig, removing unknown key: [" + key + "]"); + it = c.erase(it); + continue; + } + ++it; + } + } + + // Lock ordering: _mutex -> sbsMutex -> filterMutex (must never be reversed) + // configUpdateMutex is only used by the timer thread and _stopTimer; never held while acquiring _mutex. + void _updateConfigPeriodically(const nlohmann::json &session_params) { + std::unique_lock lock(configUpdateMutex); + + while (!stopConfigThread.load(std::memory_order_acquire)) { + try { + unsigned int interval = serverConfigUpdateInterval.load(std::memory_order_acquire); + + if (interval < 1) { + interval = 4; + } + + auto deadline = std::chrono::steady_clock::now() + std::chrono::hours(interval); + bool stopped = configUpdateCv.wait_until(lock, deadline, [&] { return stopConfigThread.load(std::memory_order_acquire); }); + if (stopped) { + return; + } + + if (std::chrono::steady_clock::now() < deadline) { + continue; + } + + lock.unlock(); + std::map data; + { + std::lock_guard mutexLock(*_mutex); + data = {{"method", "sc"}, {"app_key", session_params["app_key"].get()}, {"device_id", session_params["device_id"].get()}}; + } + _fetchConfigFromServerHTTP(data, session_params); + lock.lock(); + } catch (const std::exception &e) { + _logger->log(LogLevel::ERROR, "[Countly] [ConfigurationModule] _updateConfigPeriodically, exception: [" + std::string(e.what()) + "]"); + if (!lock.owns_lock()) { + lock.lock(); + } + } + } + } + + void _stopTimer() { + _logger->log(LogLevel::WARNING, "[Countly] [ConfigurationModule] stopTimer, stopping server config update timer thread."); + stopConfigThread.store(true, std::memory_order_release); + configUpdateCv.notify_all(); + + if (configUpdateThread.joinable()) { + configUpdateThread.join(); + } + } + + void _startTimer(nlohmann::json session_params) { + if (_configuration->sdkBehaviorSettingsUpdatesDisabled) { + _logger->log(LogLevel::INFO, "[Countly] [ConfigurationModule] _startTimer, SDK behavior settings updates are disabled."); + return; + } + + if (configUpdateThread.joinable()) { + return; + } + + stopConfigThread.store(false, std::memory_order_release); + configUpdateThread = std::thread(&ConfigurationModule::ConfigurationModuleImpl::_updateConfigPeriodically, this, session_params); + } + + ~ConfigurationModuleImpl() { + // Join the one-shot fetch thread. The HTTP client MUST have a timeout + // configured, otherwise this will block destruction indefinitely. + if (configFetchThread.joinable()) { + configFetchThread.join(); + } + _stopTimer(); + _logger.reset(); + } + + bool getBool(const char *key, bool defaultValue) const { + if (!sdk_behavior_settings.is_object()) { + return defaultValue; + } + + auto it = sdk_behavior_settings.find(key); + if (it == sdk_behavior_settings.end() || !it->is_boolean()) { + return defaultValue; + } + + bool value = it->get(); + return value; + } + + unsigned int getUInt(const char *key, unsigned int defaultValue) const { + if (!sdk_behavior_settings.is_object()) { + return defaultValue; + } + + auto it = sdk_behavior_settings.find(key); + if (it == sdk_behavior_settings.end() || !it->is_number_unsigned()) { + return defaultValue; + } + + unsigned int value = it->get(); + return value; + } +}; + +ConfigurationModule::ConfigurationModule(cly::CountlyDelegates *cly, std::shared_ptr config, std::shared_ptr logger, std::shared_ptr requestBuilder, std::shared_ptr storageModule, std::shared_ptr requestModule, + std::shared_ptr mutex) { + impl.reset(new ConfigurationModuleImpl(cly, config, logger, requestBuilder, storageModule, requestModule, mutex)); + impl->_logger->log(LogLevel::DEBUG, "[Countly] [ConfigurationModule] Initialized"); +} + +ConfigurationModule::~ConfigurationModule() { impl.reset(); } + +void ConfigurationModule::fetchConfigFromServer(nlohmann::json session_params) { + // Join any previous fetch thread before starting a new one + if (impl->configFetchThread.joinable()) { + impl->configFetchThread.join(); + } + + std::map data; + { + std::lock_guard lock(*impl->_mutex); + data = {{"method", "sc"}, {"app_key", session_params["app_key"].get()}, {"device_id", session_params["device_id"].get()}}; + } + + impl->configFetchThread = std::thread(&ConfigurationModule::ConfigurationModuleImpl::_fetchConfigFromServerHTTP, impl.get(), data, session_params); +} + +void ConfigurationModule::fetchConfigFromStorage() { + nlohmann::json changedSettings; + { + std::lock_guard lock(*impl->_mutex); + changedSettings = impl->_initializeSBSFromStorage(); + } + + // Call _onSBSChanged outside of _mutex to avoid deadlock with RecordLocation + if (!changedSettings.empty()) { + impl->_onSBSChanged(changedSettings); + } +} + +void ConfigurationModule::startServerConfigUpdateTimer(nlohmann::json session_params) { impl->_startTimer(session_params); } + +void ConfigurationModule::stopTimer() { impl->_stopTimer(); } + +bool ConfigurationModule::isTrackingEnabled() const { return impl->trackingEnabled.load(std::memory_order_acquire); } + +bool ConfigurationModule::isNetworkingEnabled() const { return impl->networkingEnabled.load(std::memory_order_acquire); } + +bool ConfigurationModule::isLocationTrackingEnabled() { return impl->locationTrackingEnabled.load(std::memory_order_acquire); } + +bool ConfigurationModule::isViewTrackingEnabled() const { return impl->viewTrackingEnabled.load(std::memory_order_acquire); } + +bool ConfigurationModule::isSessionTrackingEnabled() { return impl->sessionTrackingEnabled.load(std::memory_order_acquire); } + +bool ConfigurationModule::isCustomEventTrackingEnabled() { return impl->customEventTrackingEnabled.load(std::memory_order_acquire); } + +bool ConfigurationModule::isCrashReportingEnabled() const { return impl->crashReportingEnabled.load(std::memory_order_acquire); } + +unsigned int ConfigurationModule::getRequestQueueSizeLimit() const { return impl->requestQueueSizeLimit.load(std::memory_order_acquire); } + +unsigned int ConfigurationModule::getEventQueueSizeLimit() { + // this is because we permit EQ size to change after initialization + unsigned int value = impl->eventQueueThreshold.load(std::memory_order_acquire); + return value == 0 ? impl->_configuration->eventQueueThreshold : value; + +} + +unsigned int ConfigurationModule::getSessionUpdateInterval() { return impl->sessionUpdateInterval.load(std::memory_order_acquire); } + +FilterList> ConfigurationModule::getEventFilterList() const { + std::lock_guard lock(impl->filterMutex); + return impl->eventFilter; +} + +FilterList> ConfigurationModule::getUserPropertyFilterList() const { + std::lock_guard lock(impl->filterMutex); + return impl->userPropertyFilter; +} + +FilterList> ConfigurationModule::getSegmentationFilterList() const { + std::lock_guard lock(impl->filterMutex); + return impl->segmentationFilter; +} + +FilterList>> ConfigurationModule::getEventSegmentationFilterList() const { + std::lock_guard lock(impl->filterMutex); + return impl->eventSegmentationFilter; +} + +// namespace cly +} // namespace cly \ No newline at end of file diff --git a/src/countly.cpp b/src/countly.cpp index 42552d6..c7cb4ef 100644 --- a/src/countly.cpp +++ b/src/countly.cpp @@ -31,6 +31,7 @@ Countly::~Countly() { stop(); crash_module.reset(); views_module.reset(); + configurationModule.reset(); logger.reset(); } @@ -44,7 +45,11 @@ Countly &Countly::getInstance() { } #ifdef COUNTLY_BUILD_TESTS -void Countly::halt() { _sharedInstance.reset(new Countly()); } +void Countly::halt() { + if (_sharedInstance) { + _sharedInstance->stop(); // joins threads, releases mutex normally + } + _sharedInstance.reset(new Countly()); } #endif /** @@ -53,7 +58,7 @@ void Countly::halt() { _sharedInstance.reset(new Countly()); } */ void Countly::setMaxRequestQueueSize(unsigned int requestQueueSize) { if (is_sdk_initialized) { - log(LogLevel::WARNING, "[Countly][setMaxRequestQueueSize] You can not set the request queue size after SDK initialization."); + log(LogLevel::WARNING, "[Countly] setMaxRequestQueueSize, This method can't be called after SDK initialization. Returning."); return; } @@ -75,7 +80,7 @@ void Countly::setMaxRQProcessingBatchSize(unsigned int batchSize) { void Countly::alwaysUsePost(bool value) { if (is_sdk_initialized) { - log(LogLevel::WARNING, "[Countly][alwaysUsePost] You can not set the http method after SDK initialization."); + log(LogLevel::WARNING, "[Countly] alwaysUsePost, This method can't be called after SDK initialization. Returning."); return; } @@ -86,7 +91,7 @@ void Countly::alwaysUsePost(bool value) { void Countly::setSalt(const std::string &value) { if (is_sdk_initialized) { - log(LogLevel::WARNING, "[Countly][setSalt] You can not set the salt after SDK initialization."); + log(LogLevel::WARNING, "[Countly] setSalt, This method can't be called after SDK initialization. Returning."); return; } @@ -97,7 +102,7 @@ void Countly::setSalt(const std::string &value) { void Countly::setLogger(void (*fun)(LogLevel level, const std::string &message)) { if (is_sdk_initialized) { - log(LogLevel::WARNING, "[Countly][setLogger] You can not set the logger after SDK initialization."); + log(LogLevel::WARNING, "[Countly] setLogger, This method can't be called after SDK initialization. Returning."); return; } @@ -108,7 +113,7 @@ void Countly::setLogger(void (*fun)(LogLevel level, const std::string &message)) void Countly::setHTTPClient(HTTPClientFunction fun) { if (is_sdk_initialized) { - log(LogLevel::WARNING, "[Countly][setHTTPClient] You can not set the http client after SDK initialization."); + log(LogLevel::WARNING, "[Countly] setHTTPClient, This method can't be called after SDK initialization. Returning."); return; } @@ -119,7 +124,7 @@ void Countly::setHTTPClient(HTTPClientFunction fun) { void Countly::setSha256(SHA256Function fun) { if (is_sdk_initialized) { - log(LogLevel::WARNING, "[Countly][setHTTPClient] You can not set the 'SHA256' function after SDK initialization."); + log(LogLevel::WARNING, "[Countly] setSha256, This method can't be called after SDK initialization. Returning."); return; } @@ -133,7 +138,7 @@ void Countly::setSha256(SHA256Function fun) { */ void Countly::enableManualSessionControl() { if (is_sdk_initialized) { - log(LogLevel::WARNING, "[Countly][enableManualSessionControl] You can not enable manual session control after SDK initialization."); + log(LogLevel::WARNING, "[Countly] enableManualSessionControl, You can not enable manual session control after SDK initialization."); return; } @@ -142,9 +147,33 @@ void Countly::enableManualSessionControl() { mutex->unlock(); } +/** + * Disable automatic events on user properties changes. + */ +void Countly::disableAutoEventsOnUserProperties() { + if (is_sdk_initialized) { + log(LogLevel::WARNING, "[Countly] disableAutoEventsOnUserProperties, You can not disable automatic events on user properties after SDK initialization."); + return; + } + + mutex->lock(); + configuration->autoEventsOnUserProperties = false; + mutex->unlock(); +} + +void Countly::enableImmediateRequestOnStop() { + if (is_sdk_initialized) { + log(LogLevel::WARNING, "[Countly][enableImmediateRequestOnStop] You can not enable immediate request on stop after SDK initialization."); + return; + } + + std::lock_guard lk(*mutex); + configuration->immediateRequestOnStop = true; +} + void Countly::setMetrics(const std::string &os, const std::string &os_version, const std::string &device, const std::string &resolution, const std::string &carrier, const std::string &app_version) { if (is_sdk_initialized) { - log(LogLevel::WARNING, "[Countly][setMetrics] You can not set metrics after SDK initialization."); + log(LogLevel::WARNING, "[Countly] setMetrics, This method can't be called after SDK initialization. Returning."); return; } @@ -177,11 +206,17 @@ void Countly::setUserDetails(const std::map &value) { session_params["user_details"] = value; if (!is_sdk_initialized) { - log(LogLevel::ERROR, "[Countly][setUserDetails] Can not send user detail if the SDK has not been initialized."); + log(LogLevel::ERROR, "[Countly] setUserDetails, This method can't be called before SDK initialization. Returning."); mutex->unlock(); return; } + if (configuration->autoEventsOnUserProperties == true) { + mutex->unlock(); + flushEvents(); + mutex->lock(); + } + std::map data = {{"app_key", session_params["app_key"].get()}, {"device_id", session_params["device_id"].get()}, {"user_details", session_params["user_details"].dump()}}; requestModule->addRequestToQueue(data); @@ -190,14 +225,49 @@ void Countly::setUserDetails(const std::map &value) { void Countly::setCustomUserDetails(const std::map &value) { mutex->lock(); - session_params["user_details"]["custom"] = value; + + // Apply user property filter + if (configurationModule) { + auto upFilter = configurationModule->getUserPropertyFilterList(); + if (!upFilter.filterList.empty()) { + std::map filteredValue; + for (const auto &kv : value) { + bool allowed; + if (upFilter.isWhitelist) { + allowed = (upFilter.filterList.find(kv.first) != upFilter.filterList.end()); + } else { + allowed = (upFilter.filterList.find(kv.first) == upFilter.filterList.end()); + } + if (allowed) { + filteredValue[kv.first] = kv.second; + } + } + + if (filteredValue.empty()) { + log(LogLevel::DEBUG, "[Countly] setCustomUserDetails, All user properties were filtered out by SBS user property filter."); + mutex->unlock(); + return; + } + session_params["user_details"]["custom"] = filteredValue; + } else { + session_params["user_details"]["custom"] = value; + } + } else { + session_params["user_details"]["custom"] = value; + } if (!is_sdk_initialized) { - log(LogLevel::ERROR, "[Countly][setCustomUserDetails] Can not send user detail if the SDK has not been initialized."); + log(LogLevel::ERROR, "[Countly] setCustomUserDetails, This method can't be called before SDK initialization. Returning."); mutex->unlock(); return; } + if (configuration->autoEventsOnUserProperties == true) { + mutex->unlock(); + flushEvents(); + mutex->lock(); + } + std::map data = {{"app_key", session_params["app_key"].get()}, {"device_id", session_params["device_id"].get()}, {"user_details", session_params["user_details"].dump()}}; requestModule->addRequestToQueue(data); @@ -207,17 +277,17 @@ void Countly::setCustomUserDetails(const std::map &val #pragma region User location void Countly::setCountry(const std::string &country_code) { - log(LogLevel::WARNING, "[Countly][setCountry] 'setCountry' is deprecated, please use 'setLocation(countryCode, city, gpsCoordinates, ipAddress)' method instead."); + log(LogLevel::WARNING, "[Countly] setCountry, 'setCountry' is deprecated, please use 'setLocation(countryCode, city, gpsCoordinates, ipAddress)' method instead."); setLocation(country_code, "", "", ""); } void Countly::setCity(const std::string &city_name) { - log(LogLevel::WARNING, "[Countly][setCity] 'setCity' is deprecated, please use 'setLocation(countryCode, city, gpsCoordinates, ipAddress)' method instead."); + log(LogLevel::WARNING, "[Countly] setCity, 'setCity' is deprecated, please use 'setLocation(countryCode, city, gpsCoordinates, ipAddress)' method instead."); setLocation("", city_name, "", ""); } void Countly::setLocation(double lattitude, double longitude) { - log(LogLevel::WARNING, "[Countly][setLocation] 'setLocation(latitude, longitude)' is deprecated, please use 'setLocation(countryCode, city, gpsCoordinates, ipAddress)' method instead."); + log(LogLevel::WARNING, "[Countly] setLocation, 'setLocation(latitude, longitude)' is deprecated, please use 'setLocation(countryCode, city, gpsCoordinates, ipAddress)' method instead."); std::ostringstream location_stream; location_stream << lattitude << ',' << longitude; @@ -225,11 +295,21 @@ void Countly::setLocation(double lattitude, double longitude) { } void Countly::setLocation(const std::string &countryCode, const std::string &city, const std::string &gpsCoordinates, const std::string &ipAddress) { + if (!is_sdk_initialized) { + log(LogLevel::WARNING, "[Countly] setLocation, SDK is not initialized."); + return; + } + bool isClearingLocation = countryCode.empty() && city.empty() && gpsCoordinates.empty() && ipAddress.empty(); mutex->lock(); - log(LogLevel::INFO, "[Countly][setLocation] SetLocation : countryCode = " + countryCode + ", city = " + city + ", gpsCoordinates = " + gpsCoordinates + ", ipAddress = " + ipAddress); + if (!isClearingLocation && configurationModule->isLocationTrackingEnabled() == false) { + log(LogLevel::ERROR, "[Countly] setLocation, Location tracking is disabled in server configuration, can not set location."); + mutex->unlock(); + return; + } + log(LogLevel::INFO, "[Countly] setLocation, Setting location: countryCode = [" + countryCode + "], city = [" + city + "], gpsCoordinates = [" + gpsCoordinates + "], ipAddress = [" + ipAddress + "]"); if ((!countryCode.empty() && city.empty()) || (!city.empty() && countryCode.empty())) { - log(LogLevel::WARNING, "[Countly][setLocation] In \"SetLocation\" both country code and city should be set together"); + log(LogLevel::WARNING, "[Countly] setLocation, It's required that both 'country_code' and 'city' should be set together"); } session_params["city"] = city; @@ -246,7 +326,7 @@ void Countly::setLocation(const std::string &countryCode, const std::string &cit void Countly::_sendIndependantLocationRequest() { mutex->lock(); - log(LogLevel::DEBUG, "[Countly] [_sendIndependantLocationRequest]"); + log(LogLevel::DEBUG, "[Countly] _sendIndependantLocationRequest, Start"); /* * Empty country code, city and IP address can not be sent. @@ -273,13 +353,17 @@ void Countly::_sendIndependantLocationRequest() { const std::chrono::system_clock::time_point now = Countly::getTimestamp(); const auto timestamp = std::chrono::duration_cast(now.time_since_epoch()); - if (!data.empty()) { - data["app_key"] = session_params["app_key"].get(); - data["device_id"] = session_params["device_id"].get(); - data["timestamp"] = std::to_string(timestamp.count()); - requestModule->addRequestToQueue(data); + data["app_key"] = session_params["app_key"].get(); + data["device_id"] = session_params["device_id"].get(); + data["timestamp"] = std::to_string(timestamp.count()); + + if (data.size() == 3) { + // No location fields were added — send empty location to clear server-side location + data["location"] = ""; } + requestModule->addRequestToQueue(data); + mutex->unlock(); } @@ -288,25 +372,25 @@ void Countly::_sendIndependantLocationRequest() { #pragma region Device Id void Countly::setDeviceID(const std::string &value, bool same_user) { mutex->lock(); - log(LogLevel::INFO, "[Countly][setDeviceID] setDeviceID requested = '" + value + "'"); + log(LogLevel::INFO, "[Countly] setDeviceID, Device ID change requested, new value = [" + value + "]"); if (!session_params.contains("device_id")) { session_params["device_id"] = value; configuration->deviceId = value; - log(LogLevel::DEBUG, "[Countly][setDeviceID] no previous device id, assigning initial device id"); + log(LogLevel::DEBUG, "[Countly] setDeviceID, No previous device id, assigning initial device id"); mutex->unlock(); return; } if (session_params["device_id"].get() == value) { - log(LogLevel::DEBUG, "[Countly][setDeviceID] new device id equals existing device id, ignoring."); + log(LogLevel::DEBUG, "[Countly] setDeviceID, New device id equals existing device id, ignoring."); mutex->unlock(); return; } mutex->unlock(); if (!is_sdk_initialized) { - log(LogLevel::ERROR, "[Countly][setDeviceID] Can not change the device id if the SDK has not been initialized."); + log(LogLevel::ERROR, "[Countly] setDeviceID, Device id can't be changed while the SDK has not been initialized."); return; } @@ -320,7 +404,7 @@ void Countly::setDeviceID(const std::string &value, bool same_user) { /* Change device ID with merge after SDK has been initialized.*/ void Countly::_changeDeviceIdWithMerge(const std::string &value) { mutex->lock(); - log(LogLevel::DEBUG, "[Countly][changeDeviceIdWithMerge] deviceId = '" + value + "'"); + log(LogLevel::DEBUG, "[Countly] _changeDeviceIdWithMerge, deviceId = [" + value + "]"); session_params["old_device_id"] = session_params["device_id"]; session_params["device_id"] = value; @@ -341,11 +425,11 @@ void Countly::_changeDeviceIdWithMerge(const std::string &value) { } void Countly::_changeDeviceIdWithoutMerge(const std::string &value) { - log(LogLevel::DEBUG, "[Countly][changeDeviceIdWithoutMerge] deviceId = '" + value + "'"); + log(LogLevel::DEBUG, "[Countly] _changeDeviceIdWithoutMerge, deviceId = [" + value + "]"); // send all event to server and end current session of old user flushEvents(); - if(configuration->manualSessionControl == false){ + if (configuration->manualSessionControl == false) { endSession(); } @@ -355,60 +439,59 @@ void Countly::_changeDeviceIdWithoutMerge(const std::string &value) { mutex->unlock(); // start a new session for new user - if(configuration->manualSessionControl == false){ + if (configuration->manualSessionControl == false) { beginSession(); } - } #pragma endregion Device Id void Countly::start(const std::string &app_key, const std::string &host, int port, bool start_thread) { mutex->lock(); if (is_sdk_initialized) { - log(LogLevel::ERROR, "[Countly][start] SDK has already been initialized, 'start' should not be called a second time!"); + log(LogLevel::ERROR, "[Countly] start, SDK has already been initialized, 'start' should not be called a second time!"); mutex->unlock(); return; } #ifdef COUNTLY_USE_SQLITE if (configuration->databasePath == "" || configuration->databasePath == " ") { - log(LogLevel::ERROR, "[Countly][start] Database path can not be empty or blank."); + log(LogLevel::ERROR, "[Countly] start, Database path can not be empty or blank."); mutex->unlock(); return; } #endif - log(LogLevel::INFO, "[Countly][start]"); + log(LogLevel::INFO, "[Countly] start, Initializing SDK"); #ifdef COUNTLY_USE_SQLITE - log(LogLevel::INFO, "[Countly][start] 'COUNTLY_USE_SQLITE' is defined"); + log(LogLevel::INFO, "[Countly] start, 'COUNTLY_USE_SQLITE' is defined"); #else - log(LogLevel::INFO, "[Countly][start] 'COUNTLY_USE_SQLITE' is not defined"); + log(LogLevel::INFO, "[Countly] start, 'COUNTLY_USE_SQLITE' is not defined"); #endif #ifdef COUNTLY_USE_CUSTOM_HTTP - log(LogLevel::INFO, "[Countly][start] 'COUNTLY_USE_CUSTOM_HTTP' is defined"); + log(LogLevel::INFO, "[Countly] start, 'COUNTLY_USE_CUSTOM_HTTP' is defined"); #else - log(LogLevel::INFO, "[Countly][start] 'COUNTLY_USE_CUSTOM_HTTP' is not defined"); + log(LogLevel::INFO, "[Countly] start, 'COUNTLY_USE_CUSTOM_HTTP' is not defined"); #endif #ifdef COUNTLY_USE_CUSTOM_SHA256 - log(LogLevel::INFO, "[Countly][start] 'COUNTLY_USE_CUSTOM_SHA256' is defined"); + log(LogLevel::INFO, "[Countly] start, 'COUNTLY_USE_CUSTOM_SHA256' is defined"); #else - log(LogLevel::INFO, "[Countly][start] 'COUNTLY_USE_CUSTOM_SHA256' is not defined"); + log(LogLevel::INFO, "[Countly] start, 'COUNTLY_USE_CUSTOM_SHA256' is not defined"); #endif #ifdef _WIN32 - log(LogLevel::INFO, "[Countly][start] '_WIN32' is defined"); + log(LogLevel::INFO, "[Countly] start, '_WIN32' is defined"); #else - log(LogLevel::INFO, "[Countly][start] '_WIN32' is not defined"); + log(LogLevel::INFO, "[Countly] start, '_WIN32' is not defined"); #endif enable_automatic_session = start_thread; start_thread = true; if (port < 0 || port > 65535) { - log(LogLevel::WARNING, "[Countly][start] Port number is out of valid boundaries. Setting it to 0."); + log(LogLevel::WARNING, "[Countly] start, Port number is out of valid boundaries. Setting it to 0."); port = 0; } @@ -427,19 +510,40 @@ void Countly::start(const std::string &app_key, const std::string &host, int por requestBuilder.reset(new RequestBuilder(configuration, logger)); requestModule.reset(new RequestModule(configuration, logger, requestBuilder, storageModule)); + configurationModule.reset(new cly::ConfigurationModule(this, configuration, logger, requestBuilder, storageModule, requestModule, mutex)); crash_module.reset(new cly::CrashModule(configuration, logger, requestModule, mutex)); views_module.reset(new cly::ViewsModule(this, logger)); + requestModule->setConfigurationProvider(configurationModule); + views_module->setConfigurationProvider(configurationModule); + crash_module->setConfigurationProvider(configurationModule); + bool result = true; #ifdef COUNTLY_USE_SQLITE result = createEventTableSchema(); + if (!result) { + log(LogLevel::ERROR, "[Countly] start, Failed to initialize database at path: '" + configuration->databasePath + "'. SDK will not be initialized. Please verify the path is valid and writable."); + } #endif is_sdk_initialized = result; // after this point SDK is initialized. + if (!is_sdk_initialized) { + log(LogLevel::ERROR, "[Countly] start, SDK initialization failed."); + mutex->unlock(); + return; + } + + if (is_sdk_initialized) { + mutex->unlock(); + configurationModule->fetchConfigFromStorage(); + configurationModule->fetchConfigFromServer(session_params); + configurationModule->startServerConfigUpdateTimer(session_params); + mutex->lock(); + } if (!running) { - if(configuration->manualSessionControl == false){ + if (configuration->manualSessionControl == false) { mutex->unlock(); beginSession(); mutex->lock(); @@ -452,7 +556,7 @@ void Countly::start(const std::string &app_key, const std::string &host, int por thread.reset(new std::thread(&Countly::updateLoop, this)); } catch (const std::system_error &e) { std::ostringstream log_message; - log_message << "Could not create thread: " << e.what(); + log_message << "[Countly] start, Could not create thread: " << e.what(); log(LogLevel::FATAL, log_message.str()); } } @@ -464,7 +568,7 @@ void Countly::start(const std::string &app_key, const std::string &host, int por * startOnCloud is deprecated and this is going to be removed in the future. */ void Countly::startOnCloud(const std::string &app_key) { - log(LogLevel::WARNING, "[Countly][startOnCloud] 'startOnCloud' is deprecated, this is going to be removed in the future."); + log(LogLevel::WARNING, "[Countly] startOnCloud, 'startOnCloud' is deprecated, this is going to be removed in the future."); this->start(app_key, "https://cloud.count.ly", 443); } @@ -476,31 +580,109 @@ void Countly::stop() { } void Countly::_deleteThread() { - mutex->lock(); - stop_thread = true; - mutex->unlock(); + { + std::lock_guard lk(*mutex); + stop_thread = true; + } + stop_cv.notify_one(); if (thread && thread->joinable()) { try { thread->join(); } catch (const std::system_error &e) { - log(LogLevel::WARNING, "Could not join thread"); + log(LogLevel::WARNING, "[Countly] _deleteThread, Could not join thread"); } thread.reset(); } } void Countly::setUpdateInterval(size_t milliseconds) { - mutex->lock(); - wait_milliseconds = milliseconds; - mutex->unlock(); + { + std::lock_guard lk(*mutex); + wait_milliseconds = milliseconds; + } + if (configuration->immediateRequestOnStop) { + stop_cv.notify_one(); + } } void Countly::addEvent(const cly::Event &event) { + if (!is_sdk_initialized) { + log(LogLevel::WARNING, "[Countly] addEvent, SDK is not initialized."); + return; + } + + std::string eventKey = event.getKey(); + bool isInternalEvent = eventKey.find("[CLY]_") == 0; + + // Check custom event tracking (only blocks custom events) + if (!configurationModule->isCustomEventTrackingEnabled() && !isInternalEvent) { + log(LogLevel::DEBUG, "[Countly] addEvent, custom event tracking is disabled in server configuration, can not add event with key: [" + eventKey + "]"); + return; + } + + // Apply event filter (only for custom events) + if (!isInternalEvent) { + auto filter = configurationModule->getEventFilterList(); + if (!filter.filterList.empty()) { + bool blocked = false; + if (filter.isWhitelist) { + blocked = (filter.filterList.find(eventKey) == filter.filterList.end()); + } else { + blocked = (filter.filterList.find(eventKey) != filter.filterList.end()); + } + if (blocked) { + log(LogLevel::DEBUG, "[Countly] addEvent, event filtered out by SBS event filter: [" + eventKey + "]"); + return; + } + } + } + + // Copy the event so we can apply segmentation filters without modifying the caller's object + cly::Event filteredEvent = event; + + // Apply segmentation filters + if (filteredEvent.hasSegmentation()) { + try { + auto applySegFilter = [&filteredEvent](const std::set &filterKeys, bool isWhitelist) { + if (filterKeys.empty()) { + return; + } + if (isWhitelist) { + nlohmann::json seg = nlohmann::json::parse(filteredEvent.serialize())["segmentation"]; + for (auto it = seg.begin(); it != seg.end(); ++it) { + if (filterKeys.find(it.key()) == filterKeys.end()) { + filteredEvent.removeSegmentation(it.key()); + } + } + } else { + for (const auto &key : filterKeys) { + filteredEvent.removeSegmentation(key); + } + } + }; + + // Global segmentation filter (sb/sw) + auto segFilter = configurationModule->getSegmentationFilterList(); + applySegFilter(segFilter.filterList, segFilter.isWhitelist); + + // Event-specific segmentation filter (esb/esw) + auto eSegFilter = configurationModule->getEventSegmentationFilterList(); + if (!eSegFilter.filterList.empty()) { + auto mapIt = eSegFilter.filterList.find(eventKey); + if (mapIt != eSegFilter.filterList.end()) { + applySegFilter(mapIt->second, eSegFilter.isWhitelist); + } + } + } catch (const std::exception &e) { + log(LogLevel::ERROR, "[Countly] addEvent, error applying segmentation filter: [" + std::string(e.what()) + "]"); + } + } + mutex->lock(); #ifndef COUNTLY_USE_SQLITE - event_queue.push_back(event.serialize()); + event_queue.push_back(filteredEvent.serialize()); #else - addEventToSqlite(event); + addEventToSqlite(filteredEvent); #endif mutex->unlock(); checkAndSendEventToRQ(); @@ -509,10 +691,14 @@ void Countly::addEvent(const cly::Event &event) { void Countly::checkAndSendEventToRQ() { nlohmann::json events = nlohmann::json::array(); int queueSize = checkEQSize(); + // if queue size could not be get return early + if (queueSize < 0) { + return; + } mutex->lock(); #ifdef COUNTLY_USE_SQLITE - if (queueSize >= configuration->eventQueueThreshold) { - log(LogLevel::DEBUG, "Event queue threshold is reached"); + if (queueSize >= configurationModule->getEventQueueSizeLimit()) { + log(LogLevel::DEBUG, "[Countly] checkAndSendEventToRQ, Event queue threshold is reached"); std::string event_ids; // fetch events up to the threshold from the database @@ -525,8 +711,8 @@ void Countly::checkAndSendEventToRQ() { removeEventWithId(event_ids); } #else - if (queueSize >= configuration->eventQueueThreshold) { - log(LogLevel::WARNING, "Event queue is full, dropping the oldest event to insert a new one"); + if (queueSize >= configurationModule->getEventQueueSizeLimit()) { + log(LogLevel::WARNING, "[Countly] checkAndSendEventToRQ, Event queue is full, dropping the oldest event to insert a new one"); for (const auto &event_json : event_queue) { events.push_back(nlohmann::json::parse(event_json)); } @@ -538,18 +724,18 @@ void Countly::checkAndSendEventToRQ() { } void Countly::setMaxEvents(size_t value) { - log(LogLevel::WARNING, "[Countly][setMaxEvents/SetMaxEventsPerMessage] These calls are deprecated. Use 'setEventsToRQThreshold' instead."); + log(LogLevel::WARNING, "[Countly] setMaxEvents, 'setMaxEvents' and 'SetMaxEventsPerMessage' are deprecated. Use 'setEventsToRQThreshold' instead."); setEventsToRQThreshold(static_cast(value)); } void Countly::setEventsToRQThreshold(int value) { - log(LogLevel::DEBUG, "[Countly][setEventsToRQThreshold] Given threshold:[" + std::to_string(value) + "]"); + log(LogLevel::DEBUG, "[Countly] setEventsToRQThreshold, Given threshold:[" + std::to_string(value) + "]"); mutex->lock(); if (value < 1) { - log(LogLevel::WARNING, "[Countly][setEventsToRQThreshold] Threshold can not be less than 1. Setting it to 1 instead of:[" + std::to_string(value) + "]"); + log(LogLevel::WARNING, "[Countly] setEventsToRQThreshold, Threshold can not be less than 1. Setting it to 1 instead of:[" + std::to_string(value) + "]"); value = 1; } else if (value > 10000) { - log(LogLevel::WARNING, "[Countly][setEventsToRQThreshold] Threshold can not be greater than 10000. Setting it to 10000 instead of:[" + std::to_string(value) + "]"); + log(LogLevel::WARNING, "[Countly] setEventsToRQThreshold, Threshold can not be greater than 10000. Setting it to 10000 instead of:[" + std::to_string(value) + "]"); value = 10000; } @@ -561,7 +747,7 @@ void Countly::setEventsToRQThreshold(int value) { } void Countly::flushEvents(std::chrono::seconds timeout) { - log(LogLevel::DEBUG, "[Countly][flushEvents] timeout: " + std::to_string(timeout.count()) + " seconds"); + log(LogLevel::DEBUG, "[Countly] flushEvents, timeout: [" + std::to_string(timeout.count()) + "] seconds"); try { auto wait_duration = std::chrono::seconds(1); @@ -592,7 +778,7 @@ void Countly::flushEvents(std::chrono::seconds timeout) { // TODO: Check if we capture anything other than a system_error } catch (const std::system_error &e) { std::ostringstream log_message; - log_message << "flushEvents, error: " << e.what(); + log_message << "[Countly] flushEvents, error: " << e.what(); log(LogLevel::FATAL, log_message.str()); } } @@ -612,7 +798,7 @@ bool Countly::attemptSessionUpdateEQ() { return false; } #endif - if(configuration->manualSessionControl == false){ + if (configuration->manualSessionControl == false) { return !updateSession(); } else { packEvents(); @@ -668,18 +854,27 @@ std::vector Countly::debugReturnStateOfEQ() { return v; } catch (const std::system_error &e) { std::ostringstream log_message; - log_message << "debugReturnStateOfEQ, error: " << e.what(); + log_message << "[Countly] debugReturnStateOfEQ, error: " << e.what(); log(LogLevel::FATAL, log_message.str()); } } #endif bool Countly::beginSession() { + if (!is_sdk_initialized) { + log(LogLevel::WARNING, "[Countly] beginSession, SDK is not initialized."); + return false; + } mutex->lock(); - log(LogLevel::INFO, "[Countly][beginSession]"); + log(LogLevel::INFO, "[Countly] beginSession, Starting session"); + if (configurationModule->isSessionTrackingEnabled() == false) { + log(LogLevel::ERROR, "[Countly] beginSession, Session tracking is disabled in server configuration, can not begin session."); + mutex->unlock(); + return false; + } if (began_session == true) { mutex->unlock(); - log(LogLevel::DEBUG, "[Countly][beginSession] Session is already active."); + log(LogLevel::DEBUG, "[Countly] beginSession, Session is already active."); return true; } @@ -730,16 +925,26 @@ bool Countly::beginSession() { * @brief Update session */ bool Countly::updateSession() { + if (!is_sdk_initialized) { + log(LogLevel::WARNING, "[Countly] updateSession, SDK is not initialized."); + return false; + } try { // Check if there was a session, if not try to start one mutex->lock(); + if (configurationModule->isSessionTrackingEnabled() == false) { + log(LogLevel::ERROR, "[Countly] updateSession, Session tracking is disabled in server configuration, can not update session."); + mutex->unlock(); + return false; + } if (began_session == false) { mutex->unlock(); - if(configuration->manualSessionControl == true){ - log(LogLevel::WARNING, "[Countly][updateSession] SDK is in manual session control mode and there is no active session. Please start a session first."); + if (configuration->manualSessionControl == true) { + log(LogLevel::WARNING, "[Countly] updateSession, SDK is in manual session control mode and there is no active session. Please start a session first."); return false; } if (!beginSession()) { + log(LogLevel::DEBUG, "[Countly] updateSession, Failed to begin session."); // if beginSession fails, we should not try to update session return false; } @@ -766,15 +971,15 @@ bool Countly::updateSession() { mutex->lock(); #endif } else { - log(LogLevel::DEBUG, "[Countly][updateSession] EQ empty."); + log(LogLevel::DEBUG, "[Countly] updateSession, EQ empty."); } mutex->unlock(); auto duration = std::chrono::duration_cast(getSessionDuration()); mutex->lock(); // report session duration if it is greater than the configured session duration value - if (duration.count() >= configuration->sessionDuration) { - log(LogLevel::DEBUG, "[Countly][updateSession] sending session update."); + if (duration.count() >= configurationModule->getSessionUpdateInterval()) { + log(LogLevel::DEBUG, "[Countly] updateSession, Sending session update."); std::map data = {{"app_key", session_params["app_key"].get()}, {"device_id", session_params["device_id"].get()}, {"session_duration", std::to_string(duration.count())}}; requestModule->addRequestToQueue(data); @@ -798,7 +1003,7 @@ bool Countly::updateSession() { #endif } catch (const std::system_error &e) { std::ostringstream log_message; - log_message << "update session, error: " << e.what(); + log_message << "[Countly] updateSession, error: " << e.what(); log(LogLevel::FATAL, log_message.str()); } mutex->unlock(); @@ -827,9 +1032,9 @@ void Countly::packEvents() { mutex->lock(); #endif } else { - log(LogLevel::DEBUG, "[Countly][packEvents] EQ empty."); + log(LogLevel::DEBUG, "[Countly] packEvents, EQ empty."); } - // report events if there are any to request queue + // report events if there are any to request queue if (!no_events) { sendEventsToRQ(events); } @@ -846,23 +1051,30 @@ void Countly::packEvents() { #endif } catch (const std::system_error &e) { std::ostringstream log_message; - log_message << "packEvents, error: " << e.what(); + log_message << "[Countly] packEvents, error: " << e.what(); log(LogLevel::FATAL, log_message.str()); } mutex->unlock(); } - void Countly::sendEventsToRQ(const nlohmann::json &events) { - log(LogLevel::DEBUG, "[Countly][sendEventsToRQ] Sending events to RQ."); + log(LogLevel::DEBUG, "[Countly] sendEventsToRQ, Sending events to RQ."); std::map data = {{"app_key", session_params["app_key"].get()}, {"device_id", session_params["device_id"].get()}, {"events", events.dump()}}; requestModule->addRequestToQueue(data); } bool Countly::endSession() { - log(LogLevel::INFO, "[Countly][endSession]"); - if(began_session == false) { - log(LogLevel::DEBUG, "[Countly][endSession] There is no active session to end."); + if (!is_sdk_initialized && !is_being_disposed) { + log(LogLevel::WARNING, "[Countly] endSession, SDK is not initialized."); + return false; + } + log(LogLevel::INFO, "[Countly] endSession, Ending session"); + if (is_being_disposed == false && configurationModule->isSessionTrackingEnabled() == false) { + log(LogLevel::ERROR, "[Countly] endSession, Session tracking is disabled in server configuration, can not end session."); + return false; + } + if (began_session == false) { + log(LogLevel::DEBUG, "[Countly] endSession, There is no active session to end."); return true; } const std::chrono::system_clock::time_point now = Countly::getTimestamp(); @@ -889,10 +1101,10 @@ bool Countly::endSession() { std::chrono::system_clock::time_point Countly::getTimestamp() { return std::chrono::system_clock::now(); } int Countly::checkEQSize() { - log(LogLevel::DEBUG, "[Countly][checkEQSize]"); + log(LogLevel::DEBUG, "[Countly] checkEQSize, Start"); int event_count = -1; if (!is_sdk_initialized) { - log(LogLevel::DEBUG, "[Countly][checkEQSize] SDK is not initialized."); + log(LogLevel::DEBUG, "[Countly] checkEQSize, This method can't be called before SDK initialization."); return event_count; } @@ -905,10 +1117,10 @@ int Countly::checkEQSize() { } int Countly::checkRQSize() { - log(LogLevel::DEBUG, "[Countly][checkRQSize]"); + log(LogLevel::DEBUG, "[Countly] checkRQSize, Start"); int request_count = -1; if (!is_sdk_initialized) { - log(LogLevel::DEBUG, "[Countly][checkRQSize] SDK is not initialized."); + log(LogLevel::DEBUG, "[Countly] checkRQSize, SDK is not initialized."); return request_count; } @@ -918,7 +1130,7 @@ int Countly::checkRQSize() { #ifndef COUNTLY_USE_SQLITE int Countly::checkMemoryEQSize() { - log(LogLevel::DEBUG, "[Countly][checkMemoryEQSize] Checking event queue size in memory"); + log(LogLevel::DEBUG, "[Countly] checkMemoryEQSize, Checking event queue size in memory."); int result = 0; mutex->lock(); result = static_cast(event_queue.size()); @@ -931,7 +1143,7 @@ int Countly::checkMemoryEQSize() { #ifdef COUNTLY_USE_SQLITE void Countly::removeEventWithId(const std::string &event_ids) { // TODO: Check if we should check database_path set or not - log(LogLevel::DEBUG, "[Countly][removeEventWithId] Removing events from storage: " + event_ids); + log(LogLevel::DEBUG, "[Countly] removeEventWithId, Removing events from storage: [" + event_ids + "]"); sqlite3 *database; int return_value; char *error_message; @@ -945,13 +1157,13 @@ void Countly::removeEventWithId(const std::string &event_ids) { return_value = sqlite3_exec(database, sql_statement.c_str(), nullptr, nullptr, &error_message); if (return_value != SQLITE_OK) { - log(LogLevel::ERROR, error_message); + log(LogLevel::ERROR, "[Countly] removeEventWithId, SQLite error: " + std::string(error_message)); sqlite3_free(error_message); } else { - log(LogLevel::DEBUG, "[Countly][removeEventWithId] Removed events with the given ID(s)."); + log(LogLevel::DEBUG, "[Countly] removeEventWithId, Removed events with the given ID(s)."); } } else { - log(LogLevel::ERROR, "[Countly][removeEventWithId] Could not open database."); + log(LogLevel::ERROR, "[Countly] removeEventWithId, Could not open database."); } sqlite3_close(database); } @@ -960,12 +1172,12 @@ void Countly::fillEventsIntoJson(nlohmann::json &events, std::string &event_ids) mutex->lock(); if (database_path.empty()) { mutex->unlock(); - log(LogLevel::FATAL, "[Countly][fillEventsIntoJson] Sqlite database path is not set."); + log(LogLevel::FATAL, "[Countly] fillEventsIntoJson, SQLite database path is not set."); event_ids = ""; return; } - log(LogLevel::DEBUG, "[Countly][fillEventsIntoJson] Fetching events from storage."); + log(LogLevel::DEBUG, "[Countly] fillEventsIntoJson, Fetching events from storage."); sqlite3 *database; int return_value, row_count, column_count; char **table; @@ -992,7 +1204,7 @@ void Countly::fillEventsIntoJson(nlohmann::json &events, std::string &event_ids) events.push_back(nlohmann::json::parse(table[(event_index * column_count) + 1])); } - log(LogLevel::DEBUG, "[Countly][fillEventsIntoJson] Events count = " + std::to_string(events.size())); + log(LogLevel::DEBUG, "[Countly] fillEventsIntoJson, Events count = [" + std::to_string(events.size()) + "]"); event_id_stream.seekp(-1, event_id_stream.cur); event_id_stream << ')'; @@ -1000,12 +1212,12 @@ void Countly::fillEventsIntoJson(nlohmann::json &events, std::string &event_ids) // write event ids to a string stream (or more like copy out that stream here) to be used in the delete statement event_ids = event_id_stream.str(); } else { - log(LogLevel::ERROR, error_message); + log(LogLevel::ERROR, "[Countly] fillEventsIntoJson, SQLite error: " + std::string(error_message)); sqlite3_free(error_message); } sqlite3_free_table(table); } else { - log(LogLevel::ERROR, "[Countly][fillEventsIntoJson] Could not open database."); + log(LogLevel::ERROR, "[Countly] fillEventsIntoJson, Could not open database."); } sqlite3_close(database); mutex->unlock(); @@ -1016,7 +1228,7 @@ int Countly::checkPersistentEQSize() { mutex->lock(); if (database_path.empty()) { mutex->unlock(); - log(LogLevel::FATAL, "[Countly][checkEQSize] Sqlite database path is not set"); + log(LogLevel::FATAL, "[Countly] checkPersistentEQSize, SQLite database path is not set"); return result; } @@ -1031,24 +1243,24 @@ int Countly::checkPersistentEQSize() { return_value = sqlite3_get_table(database, "SELECT COUNT(*) FROM events;", &table, &row_count, &column_count, &error_message); if (return_value == SQLITE_OK) { result = atoi(table[1]); - log(LogLevel::DEBUG, "[Countly][checkEQSize] Fetched event count from database: " + std::to_string(result)); + log(LogLevel::DEBUG, "[Countly] checkPersistentEQSize, Fetched event count from database: [" + std::to_string(result) + "]"); } else { - log(LogLevel::ERROR, error_message); + log(LogLevel::ERROR, "[Countly] checkPersistentEQSize, SQLite error: " + std::string(error_message)); sqlite3_free(error_message); } sqlite3_free_table(table); } else { - log(LogLevel::WARNING, "[Countly][checkEQSize] Could not open database"); + log(LogLevel::WARNING, "[Countly] checkPersistentEQSize, Could not open database"); } sqlite3_close(database); return result; } void Countly::addEventToSqlite(const cly::Event &event) { - log(LogLevel::DEBUG, "[Countly][addEventToSqlite]"); + log(LogLevel::DEBUG, "[Countly] addEventToSqlite, Start"); try { if (database_path.empty()) { - log(LogLevel::FATAL, "Cannot add event, sqlite database path is not set"); + log(LogLevel::FATAL, "[Countly] addEventToSqlite, Cannot add event, SQLite database path is not set"); return; } @@ -1072,13 +1284,13 @@ void Countly::addEventToSqlite(const cly::Event &event) { sqlite3_close(database); } catch (const std::system_error &e) { std::ostringstream log_message; - log_message << "addEventToSqlite, error: " << e.what(); + log_message << "[Countly] addEventToSqlite, error: " << e.what(); log(LogLevel::FATAL, log_message.str()); } } void Countly::clearPersistentEQ() { - log(LogLevel::DEBUG, "[Countly][clearEQ]"); + log(LogLevel::DEBUG, "[Countly] clearPersistentEQ, Start"); sqlite3 *database; int return_value; char *error_message; @@ -1087,10 +1299,10 @@ void Countly::clearPersistentEQ() { if (return_value == SQLITE_OK) { return_value = sqlite3_exec(database, "DELETE FROM events;", nullptr, nullptr, &error_message); if (return_value != SQLITE_OK) { - log(LogLevel::FATAL, error_message); + log(LogLevel::FATAL, "[Countly] clearPersistentEQ, SQLite error: " + std::string(error_message)); sqlite3_free(error_message); } else { - log(LogLevel::DEBUG, "[Countly][clearEQ] Cleared event queue"); + log(LogLevel::DEBUG, "[Countly] clearPersistentEQ, Cleared event queue"); } } sqlite3_close(database); @@ -1098,17 +1310,17 @@ void Countly::clearPersistentEQ() { void Countly::setDatabasePath(const std::string &path) { if (is_sdk_initialized) { - log(LogLevel::ERROR, "[Countly][setDatabasePath] You can not set the database path after SDK initialization."); + log(LogLevel::ERROR, "[Countly] setDatabasePath, This method can't be called after SDK initialization. Returning."); return; } if (path == "" || path == " ") { - log(LogLevel::ERROR, "[Countly][setDatabasePath] Database path can not be empty or blank."); + log(LogLevel::ERROR, "[Countly] setDatabasePath, Database path can not be empty or blank. Returning."); return; } configuration->databasePath = path; - log(LogLevel::INFO, "[Countly][setDatabasePath] path = " + path); + log(LogLevel::INFO, "[Countly] setDatabasePath, Setting database path = [" + path + "]"); } bool Countly::createEventTableSchema() { @@ -1125,21 +1337,21 @@ bool Countly::createEventTableSchema() { if (return_value == SQLITE_OK) { return_value = sqlite3_exec(database, "CREATE TABLE IF NOT EXISTS events (evtid INTEGER PRIMARY KEY, event TEXT)", nullptr, nullptr, &error_message); if (return_value != SQLITE_OK) { - log(LogLevel::ERROR, error_message); + log(LogLevel::ERROR, "[Countly] createEventTableSchema, SQLite error: " + std::string(error_message)); sqlite3_free(error_message); } else { result = true; } } else { const char *error = sqlite3_errmsg(database); - log(LogLevel::ERROR, "[Countly][createEventTableSchema] " + std::string(error)); + log(LogLevel::ERROR, "[Countly] createEventTableSchema, Could not open database: " + std::string(error)); database_path.clear(); } sqlite3_close(database); return result; } catch (const std::system_error &e) { std::ostringstream log_message; - log_message << "createEventTableSchema, error: " << e.what(); + log_message << "[Countly] createEventTableSchema, error: " << e.what(); log(LogLevel::FATAL, log_message.str()); } } @@ -1156,7 +1368,7 @@ std::string Countly::calculateChecksum(const std::string &salt, const std::strin std::string salted_data = data + salt; #ifdef COUNTLY_USE_CUSTOM_SHA256 if (configuration->sha256_function == nullptr) { - log(LogLevel::FATAL, "Missing SHA 256 function"); + log(LogLevel::FATAL, "[Countly] calculateChecksum, Missing SHA 256 function"); return {}; } @@ -1189,29 +1401,68 @@ std::chrono::system_clock::duration Countly::getSessionDuration() { return Count void Countly::updateLoop() { log(LogLevel::DEBUG, "[Countly][updateLoop]"); - mutex->lock(); - running = true; - mutex->unlock(); - while (true) { - mutex->lock(); - if (stop_thread) { - stop_thread = false; + { + std::lock_guard lk(*mutex); + running = true; + } + try { + if (configuration->immediateRequestOnStop) { + while (true) { + { + std::unique_lock lk(*mutex); + stop_cv.wait_for(lk, std::chrono::milliseconds(wait_milliseconds), [this] { + return stop_thread.load(); + }); + if (stop_thread) { + stop_thread = false; + running = false; + return; + } + } + if (enable_automatic_session == true && configuration->manualSessionControl == false) { + updateSession(); + } else if (configuration->manualSessionControl == true) { + packEvents(); + } + requestModule->processQueue(mutex); + } + } else { + while (true) { + size_t last_wait_milliseconds; + { + std::lock_guard lk(*mutex); + if (stop_thread) { + stop_thread = false; + break; + } + last_wait_milliseconds = wait_milliseconds; + } + std::this_thread::sleep_for(std::chrono::milliseconds(last_wait_milliseconds)); + if (enable_automatic_session == true && configuration->manualSessionControl == false) { + updateSession(); + } else if (configuration->manualSessionControl == true) { + packEvents(); + } + requestModule->processQueue(mutex); + } + std::lock_guard lk(*mutex); + running = false; + } + } catch (const std::exception &e) { + bool acquired = mutex->try_lock(); + running = false; + log(LogLevel::ERROR, std::string("[Countly][updateLoop] exception in update loop: ") + e.what()); + if (acquired) { mutex->unlock(); - break; } - size_t last_wait_milliseconds = wait_milliseconds; - mutex->unlock(); - std::this_thread::sleep_for(std::chrono::milliseconds(last_wait_milliseconds)); - if (enable_automatic_session == true && configuration->manualSessionControl == false) { - updateSession(); - } else if (configuration->manualSessionControl == true) { - packEvents(); + } catch (...) { + bool acquired = mutex->try_lock(); + running = false; + log(LogLevel::FATAL, "[Countly][updateLoop] unknown non-std::exception caught, stopping update loop"); + if (acquired) { + mutex->unlock(); } - requestModule->processQueue(mutex); } - mutex->lock(); - running = false; - mutex->unlock(); } void Countly::enableRemoteConfig() { @@ -1221,6 +1472,11 @@ void Countly::enableRemoteConfig() { } void Countly::_fetchRemoteConfig(const std::map &data) { + if (configurationModule->isNetworkingEnabled() == false) { + log(LogLevel::ERROR, "[Countly] _fetchRemoteConfig, Error fetching remote config, networking is disabled in SBS"); + return; + } + HTTPResponse response = requestModule->sendHTTP("/o/sdk", requestBuilder->serializeData(data)); mutex->lock(); if (response.success) { @@ -1230,10 +1486,14 @@ void Countly::_fetchRemoteConfig(const std::map &data) } void Countly::updateRemoteConfig() { + if (!is_sdk_initialized) { + log(LogLevel::WARNING, "[Countly] updateRemoteConfig, SDK is not initialized."); + return; + } mutex->lock(); if (!session_params["app_key"].is_string() || !session_params["device_id"].is_string()) { - log(LogLevel::ERROR, "Error updating remote config, app key or device id is missing"); + log(LogLevel::ERROR, "[Countly] updateRemoteConfig, Error updating remote config, app key or device id is missing"); mutex->unlock(); return; } @@ -1254,6 +1514,11 @@ nlohmann::json Countly::getRemoteConfigValue(const std::string &key) { } void Countly::_updateRemoteConfigWithSpecificValues(const std::map &data) { + if (configurationModule->isNetworkingEnabled() == false) { + log(LogLevel::ERROR, "[Countly] _updateRemoteConfigWithSpecificValues, Error fetching remote config, networking is disabled in SBS"); + return; + } + HTTPResponse response = requestModule->sendHTTP("/o/sdk", requestBuilder->serializeData(data)); mutex->lock(); if (response.success) { @@ -1265,6 +1530,10 @@ void Countly::_updateRemoteConfigWithSpecificValues(const std::maplock(); std::map data = {{"method", "fetch_remote_config"}, {"app_key", session_params["app_key"].get()}, {"device_id", session_params["device_id"].get()}}; @@ -1283,6 +1552,10 @@ void Countly::updateRemoteConfigFor(std::string *keys, size_t key_count) { } void Countly::updateRemoteConfigExcept(std::string *keys, size_t key_count) { + if (!is_sdk_initialized) { + log(LogLevel::WARNING, "[Countly] updateRemoteConfigExcept, SDK is not initialized."); + return; + } mutex->lock(); std::map data = {{"method", "fetch_remote_config"}, {"app_key", session_params["app_key"].get()}, {"device_id", session_params["device_id"].get()}}; diff --git a/src/crash_module.cpp b/src/crash_module.cpp index 319db7f..649231e 100644 --- a/src/crash_module.cpp +++ b/src/crash_module.cpp @@ -16,6 +16,7 @@ class CrashModule::CrashModuleImpl { std::shared_ptr _logger; std::shared_ptr _requestModule; std::shared_ptr _mutex; + std::weak_ptr _configProvider; CrashModuleImpl(std::shared_ptr config, std::shared_ptr logger, std::shared_ptr requestModule, std::shared_ptr mutex) : _configuration(config), _logger(logger), _requestModule(requestModule), _mutex(mutex) {} // destructor to reset logger @@ -28,12 +29,12 @@ CrashModule::~CrashModule() { impl.reset(); } CrashModule::CrashModule(std::shared_ptr config, std::shared_ptr logger, std::shared_ptr requestModule, std::shared_ptr mutex) { impl.reset(new CrashModuleImpl(config, logger, requestModule, mutex)); - impl->_logger->log(LogLevel::DEBUG, cly::utils::format_string("[CrashModule] Initialized")); + impl->_logger->log(LogLevel::DEBUG, cly::utils::format_string("[Countly] [CrashModule] Initialized")); } // function to add breadcrumb void CrashModule::addBreadcrumb(const std::string &value) { - impl->_logger->log(LogLevel::INFO, "[CrashModule] addBreadcrumb : " + value); + impl->_logger->log(LogLevel::INFO, "[Countly] [CrashModule] addBreadcrumb, value = [" + value + "]"); impl->_mutex->lock(); // if breadcrumb threshold is reached, remove oldest breadcrumb @@ -48,26 +49,36 @@ void CrashModule::addBreadcrumb(const std::string &value) { // function to record exception void CrashModule::recordException(const std::string &title, const std::string &stackTrace, const bool fatal, const std::map &crashMetrics, const std::map &segmentation) { - impl->_logger->log(LogLevel::INFO, cly::utils::format_string("[CrashModule] recordException: title = %s, stackTrace = %s", title.c_str(), stackTrace.c_str())); + impl->_logger->log(LogLevel::INFO, cly::utils::format_string("[Countly] [CrashModule] recordException, title = [%s], stackTrace = [%s]", title.c_str(), stackTrace.c_str())); + + if (std::shared_ptr config = impl->_configProvider.lock()) { + if (config->isCrashReportingEnabled() == false) { + impl->_logger->log(LogLevel::DEBUG, "[Countly] [CrashModule] recordException, Crash reporting is disabled. Not recording exception."); + return; + } + } else { + impl->_logger->log(LogLevel::WARNING, "[Countly] [CrashModule] recordException, ConfigurationProvider unavailable."); + return; + } if (title.empty()) { - impl->_logger->log(LogLevel::WARNING, "[CrashModule] recordException : The parameter 'title' can't be empty"); + impl->_logger->log(LogLevel::WARNING, "[Countly] [CrashModule] recordException, The parameter 'title' can't be empty"); } if (stackTrace.empty()) { - impl->_logger->log(LogLevel::ERROR, "[CrashModule] recordException : The parameter 'stackTrace' can't be empty"); + impl->_logger->log(LogLevel::ERROR, "[Countly] [CrashModule] recordException, The parameter 'stackTrace' can't be empty"); } // check if the crash metric '_os' exists and is not empty auto it = crashMetrics.find("_os"); if (it == crashMetrics.end() || it->second.empty()) { - impl->_logger->log(LogLevel::ERROR, "[CrashModule] recordException : The crash metric '_os' can't be empty"); + impl->_logger->log(LogLevel::ERROR, "[Countly] [CrashModule] recordException, The crash metric '_os' can't be empty"); } // check if the crash metric '_app_version' exists and is not empty it = crashMetrics.find("_app_version"); if (it == crashMetrics.end() || it->second.empty()) { - impl->_logger->log(LogLevel::ERROR, "[CrashModule] recordException : The crash metric '_app_version' can't be empty"); + impl->_logger->log(LogLevel::ERROR, "[Countly] [CrashModule] recordException, The crash metric '_app_version' can't be empty"); } // lock mutex to avoid concurrent access @@ -94,4 +105,6 @@ void CrashModule::recordException(const std::string &title, const std::string &s impl->_mutex->unlock(); } +void CrashModule::setConfigurationProvider(std::weak_ptr provider) { impl->_configProvider = std::move(provider); } + } // namespace cly \ No newline at end of file diff --git a/src/event.cpp b/src/event.cpp index 41caffb..90533e9 100644 --- a/src/event.cpp +++ b/src/event.cpp @@ -1,4 +1,5 @@ #include "countly/event.hpp" +#include namespace cly { Event::Event(const std::string &key, size_t count) : object({}), timer_running(false) { @@ -26,6 +27,11 @@ Event::Event(const std::string &key, size_t count, double sum, double duration) void Event::setTimestamp() { timestamp = std::chrono::system_clock::now(); object["timestamp"] = std::chrono::duration_cast(timestamp.time_since_epoch()).count(); + + std::time_t time = std::chrono::system_clock::to_time_t(timestamp); + std::tm local_tm = *std::localtime(&time); + object["dow"] = local_tm.tm_wday; + object["hour"] = local_tm.tm_hour; } void Event::startTimer() { @@ -42,4 +48,27 @@ void Event::stopTimer() { } std::string Event::serialize() const { return object.dump(); } + +std::string Event::getKey() const { + auto it = object.find("key"); + if (it != object.end() && it->is_string()) { + return it->get(); + } + return ""; +} + +bool Event::hasSegmentation() const { return object.find("segmentation") != object.end() && object["segmentation"].is_object() && !object["segmentation"].empty(); } + +void Event::removeSegmentation(const std::string &key) { + if (object.find("segmentation") != object.end()) { + object["segmentation"].erase(key); + if (object["segmentation"].empty()) { + object.erase("segmentation"); + } + } +} + +void Event::clearSegmentation() { + object.erase("segmentation"); +} } // namespace cly diff --git a/src/request_builder.cpp b/src/request_builder.cpp index d685f8f..b391a5d 100644 --- a/src/request_builder.cpp +++ b/src/request_builder.cpp @@ -1,5 +1,6 @@ #include "countly/request_builder.hpp" #include +#include #include #include #include @@ -26,7 +27,26 @@ std::string RequestBuilder::buildRequest(const std::map(now.time_since_epoch()); - std::map request = {{"app_key", _configuration->appKey}, {"device_id", _configuration->deviceId}, {"timestamp", std::to_string(timestamp.count())}}; + std::time_t time = std::chrono::system_clock::to_time_t(now); + std::tm local_tm = *std::localtime(&time); + std::tm gm_tm = *std::gmtime(&time); + + int tz_offset_minutes = (local_tm.tm_hour - gm_tm.tm_hour) * 60 + (local_tm.tm_min - gm_tm.tm_min); + // Adjust for day boundary crossings + int day_diff = local_tm.tm_mday - gm_tm.tm_mday; + if (day_diff > 1) { + day_diff = -1; // end of month wrap + } else if (day_diff < -1) { + day_diff = 1; // end of month wrap + } + tz_offset_minutes += day_diff * 24 * 60; + + std::map request = {{"app_key", _configuration->appKey}, + {"device_id", _configuration->deviceId}, + {"timestamp", std::to_string(timestamp.count())}, + {"dow", std::to_string(local_tm.tm_wday)}, + {"hour", std::to_string(local_tm.tm_hour)}, + {"tz", std::to_string(tz_offset_minutes)}}; request.insert(data.begin(), data.end()); return serializeData(request); diff --git a/src/request_module.cpp b/src/request_module.cpp index 1770c86..210c87d 100644 --- a/src/request_module.cpp +++ b/src/request_module.cpp @@ -52,7 +52,7 @@ class RequestModule::RequestModuleImpl { std::string salted_data = data + salt; #ifdef COUNTLY_USE_CUSTOM_SHA256 if (_configuration->sha256_function == nullptr) { - _logger->log(LogLevel::FATAL, "Missing SHA 256 function"); + _logger->log(LogLevel::FATAL, "[Countly] [RequestModule] calculateChecksum, Missing SHA 256 function"); return {}; } @@ -78,7 +78,7 @@ class RequestModule::RequestModuleImpl { RequestModule::RequestModule(std::shared_ptr config, std::shared_ptr logger, std::shared_ptr requestBuilder, std::shared_ptr storageModule) { impl.reset(new RequestModuleImpl(config, logger, requestBuilder, storageModule)); - impl->_logger->log(LogLevel::DEBUG, cly::utils::format_string("[RequestModule] Initialized")); + impl->_logger->log(LogLevel::DEBUG, cly::utils::format_string("[Countly] [RequestModule] Initialized")); #if !defined(_WIN32) && !defined(COUNTLY_USE_CUSTOM_HTTP) curl_global_init(CURL_GLOBAL_ALL); @@ -99,8 +99,19 @@ static size_t countly_curl_write_callback(void *data, size_t byte_size, size_t n } void RequestModule::addRequestToQueue(const std::map &data) { - if (impl->_configuration->requestQueueThreshold <= impl->_storageModule->RQCount()) { - impl->_logger->log(LogLevel::WARNING, cly::utils::format_string("[RequestModule] addRequestToQueue: Request Queue is full. Dropping the oldest request.")); + std::shared_ptr config = _configProvider.lock(); + if (!config) { + impl->_logger->log(LogLevel::WARNING, "[Countly] [RequestModule] addRequestToQueue: ConfigurationProvider unavailable. Not adding request."); + return; + } + + if (config->isTrackingEnabled() == false) { + impl->_logger->log(LogLevel::DEBUG, "[Countly] [RequestModule] addRequestToQueue: Tracking is disabled. Not adding request to queue."); + return; + } + + if (config->getRequestQueueSizeLimit() <= impl->_storageModule->RQCount()) { + impl->_logger->log(LogLevel::WARNING, cly::utils::format_string("[Countly] [RequestModule] addRequestToQueue: Request Queue is full. Dropping the oldest request.")); impl->_storageModule->RQRemoveFront(); } @@ -112,6 +123,24 @@ void RequestModule::clearRequestQueue() { impl->_storageModule->RQClearAll(); } void RequestModule::processQueue(std::shared_ptr mutex) { mutex->lock(); + + if (std::shared_ptr config = _configProvider.lock()) { + if (config->isTrackingEnabled() == false) { + impl->_logger->log(LogLevel::DEBUG, "[Countly] [RequestModule] processQueue: Tracking is disabled. Not processing request queue."); + mutex->unlock(); + return; + } + if (config->isNetworkingEnabled() == false) { + impl->_logger->log(LogLevel::DEBUG, "[Countly] [RequestModule] processQueue: Networking is disabled. Not processing request queue."); + mutex->unlock(); + return; + } + } else { + impl->_logger->log(LogLevel::WARNING, "[Countly] [RequestModule] processQueue: ConfigurationProvider unavailable, skipping queue processing."); + mutex->unlock(); + return; + } + // making sure that no other thread is processing the queue if (impl->is_queue_being_processed) { mutex->unlock(); @@ -127,9 +156,9 @@ void RequestModule::processQueue(std::shared_ptr mutex) { while (true) { mutex->lock(); - impl->_logger->log(LogLevel::DEBUG, cly::utils::format_string("[RequestModule] processQueue: Processing the request queue.")); + impl->_logger->log(LogLevel::DEBUG, cly::utils::format_string("[Countly] [RequestModule] processQueue: Processing the request queue.")); if (impl->_storageModule->RQCount() == 0) { - impl->_logger->log(LogLevel::DEBUG, cly::utils::format_string("[RequestModule] processQueue: Queue is empty.")); + impl->_logger->log(LogLevel::DEBUG, cly::utils::format_string("[Countly] [RequestModule] processQueue: Queue is empty.")); // stop sending requests once the queue is empty mutex->unlock(); @@ -142,7 +171,7 @@ void RequestModule::processQueue(std::shared_ptr mutex) { mutex->lock(); if (!response.success) { - impl->_logger->log(LogLevel::DEBUG, cly::utils::format_string("[RequestModule] processQueue: Failed to deliver to server, will try again later.")); + impl->_logger->log(LogLevel::DEBUG, cly::utils::format_string("[Countly] [RequestModule] processQueue: Failed to deliver to server, will try again later.")); // if the request was not a success, abort sending and try again in the future mutex->unlock(); break; @@ -154,7 +183,7 @@ void RequestModule::processQueue(std::shared_ptr mutex) { processedRequestsCounter++; if (processedRequestsCounter > impl->_configuration->maxProcessingBatchSize) { - impl->_logger->log(LogLevel::DEBUG, cly::utils::format_string("[RequestModule] processQueue: Batch limit has been reached, will do next batch later.")); + impl->_logger->log(LogLevel::DEBUG, cly::utils::format_string("[Countly] [RequestModule] processQueue: Batch limit has been reached, will do next batch later.")); mutex->unlock(); break; } @@ -170,7 +199,7 @@ void RequestModule::processQueue(std::shared_ptr mutex) { HTTPResponse RequestModule::sendHTTP(std::string path, std::string data) { bool use_post = impl->_configuration->forcePost || (data.size() > COUNTLY_POST_THRESHOLD); - impl->_logger->log(LogLevel::DEBUG, "[Countly][sendHTTP] data: " + data); + impl->_logger->log(LogLevel::DEBUG, "[Countly] [RequestModule] sendHTTP, data: [" + data + "]"); if (!impl->_configuration->salt.empty()) { std::string checksum = impl->calculateChecksum(impl->_configuration->salt, data); if (!data.empty()) { @@ -178,7 +207,7 @@ HTTPResponse RequestModule::sendHTTP(std::string path, std::string data) { } data += "checksum256=" + checksum; - impl->_logger->log(LogLevel::DEBUG, "[Countly][sendHTTP] with checksum, data: " + data); + impl->_logger->log(LogLevel::DEBUG, "[Countly] [RequestModule] sendHTTP, with checksum, data: [" + data + "]"); } HTTPResponse response; @@ -186,7 +215,7 @@ HTTPResponse RequestModule::sendHTTP(std::string path, std::string data) { #ifdef COUNTLY_USE_CUSTOM_HTTP if (!impl->_configuration->http_client_function) { - impl->_logger->log(LogLevel::FATAL, "Missing HTTP client function"); + impl->_logger->log(LogLevel::FATAL, "[Countly] [RequestModule] sendHTTP, Missing HTTP client function"); return response; } @@ -275,7 +304,7 @@ HTTPResponse RequestModule::sendHTTP(std::string path, std::string data) { if (!body.empty()) { const nlohmann::json &parseResult = nlohmann::json::parse(body, nullptr, false); if (parseResult.is_discarded()) { - impl->_logger->log(LogLevel::WARNING, "[Countly][sendHTTP] Returned response from the server was not a valid JSON."); + impl->_logger->log(LogLevel::WARNING, "[Countly] [RequestModule] sendHTTP, Returned response from the server was not a valid JSON."); } else { response.data = parseResult; } @@ -309,7 +338,7 @@ HTTPResponse RequestModule::sendHTTP(std::string path, std::string data) { curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.c_str()); } - impl->_logger->log(LogLevel::DEBUG, "[Countly][sendHTTP] request: " + full_url_stream.str()); + impl->_logger->log(LogLevel::DEBUG, "[Countly] [RequestModule] sendHTTP, request: [" + full_url_stream.str() + "]"); std::string full_url = full_url_stream.str(); curl_easy_setopt(curl, CURLOPT_URL, full_url.c_str()); @@ -328,7 +357,7 @@ HTTPResponse RequestModule::sendHTTP(std::string path, std::string data) { if (!body.empty()) { const nlohmann::json &parseResult = nlohmann::json::parse(body, nullptr, false); if (parseResult.is_discarded()) { - impl->_logger->log(LogLevel::WARNING, "[Countly][sendHTTP] Returned response from the server was not a valid JSON."); + impl->_logger->log(LogLevel::WARNING, "[Countly] [RequestModule] sendHTTP, Returned response from the server was not a valid JSON."); } else { response.data = parseResult; } @@ -337,9 +366,12 @@ HTTPResponse RequestModule::sendHTTP(std::string path, std::string data) { curl_easy_cleanup(curl); } #endif - impl->_logger->log(LogLevel::DEBUG, "[Countly][sendHTTP] response: " + response.data.dump()); + impl->_logger->log(LogLevel::DEBUG, "[Countly] [RequestModule] sendHTTP, response: [" + response.data.dump() + "]"); return response; #endif } long long RequestModule::RQSize() { return impl->_storageModule->RQCount(); } + +void RequestModule::setConfigurationProvider(std::weak_ptr provider) { _configProvider = std::move(provider); } + } // namespace cly diff --git a/src/storage_module_db.cpp b/src/storage_module_db.cpp index 60c73e9..84d9ac8 100644 --- a/src/storage_module_db.cpp +++ b/src/storage_module_db.cpp @@ -10,6 +10,10 @@ const char REQUESTS_TABLE_NAME[] = "Requests"; const char REQUESTS_TABLE_REQUEST_ID[] = "RequestID"; const char REQUESTS_TABLE_REQUEST_DATA[] = "RequestData"; +#define SDK_BEHAVIOR_SETTINGS_TABLE_NAME "SDKBehaviorSettings" +#define SDK_BEHAVIOR_SETTINGS_KEY_COLUMN_NAME "Key" +#define SDK_BEHAVIOR_SETTINGS_DATA_COLUMN_NAME "SettingsData" +#define SDK_BEHAVIOR_SETTINGS_KEY_VALUE 1 namespace cly { StorageModuleDB::StorageModuleDB(std::shared_ptr config, std::shared_ptr logger) : StorageModuleBase(config, logger) {} @@ -18,32 +22,32 @@ StorageModuleDB::~StorageModuleDB() {} void StorageModuleDB::init() { try { - _logger->log(LogLevel::DEBUG, "[Countly][StorageModuleDB] initialized."); + _logger->log(LogLevel::DEBUG, "[Countly] [StorageModuleDB] init, Initialized."); #ifdef COUNTLY_USE_SQLITE // Check if the database path is empty or blank if (_configuration->databasePath == "" || _configuration->databasePath == " ") { - _logger->log(LogLevel::ERROR, "[Countly][StorageModuleDB] init: Database path can not be empty or blank!"); + _logger->log(LogLevel::ERROR, "[Countly] [StorageModuleDB] init, Database path can not be empty or blank!"); return; } #endif - // Create schema for the requests table - _is_initialized = createSchema(REQUESTS_TABLE_NAME, REQUESTS_TABLE_REQUEST_ID, REQUESTS_TABLE_REQUEST_DATA); + // Create schema for the requests table and the SDK behavior settings table + _is_initialized = createSchema(REQUESTS_TABLE_NAME, REQUESTS_TABLE_REQUEST_ID, REQUESTS_TABLE_REQUEST_DATA) && createSchema(SDK_BEHAVIOR_SETTINGS_TABLE_NAME, SDK_BEHAVIOR_SETTINGS_KEY_COLUMN_NAME, SDK_BEHAVIOR_SETTINGS_DATA_COLUMN_NAME); if (_is_initialized) { vacuumDatabase(); } } catch (const std::system_error &e) { std::ostringstream log_message; - log_message << "init, error: " << e.what(); + log_message << "[Countly] [StorageModuleDB] init, error: " << e.what(); _logger->log(LogLevel::FATAL, log_message.str()); } } void StorageModuleDB::vacuumDatabase() { try { - _logger->log(LogLevel::INFO, "[StorageModuleDB][Vacuum] Will try to vacuum the database"); + _logger->log(LogLevel::INFO, "[Countly] [StorageModuleDB] vacuumDatabase, Will try to vacuum the database"); #ifdef COUNTLY_USE_SQLITE sqlite3 *database; @@ -53,27 +57,27 @@ void StorageModuleDB::vacuumDatabase() { if (return_value == SQLITE_OK) { return_value = sqlite3_exec(database, "VACUUM", nullptr, nullptr, &error_message); if (return_value != SQLITE_OK) { - _logger->log(LogLevel::ERROR, error_message); + _logger->log(LogLevel::ERROR, "[Countly] [StorageModuleDB] vacuumDatabase, SQLite error: " + std::string(error_message)); sqlite3_free(error_message); } else { - _logger->log(LogLevel::INFO, "[StorageModuleDB][Vacuum] Database vacuumed successfully"); + _logger->log(LogLevel::INFO, "[Countly] [StorageModuleDB] vacuumDatabase, Database vacuumed successfully"); } } else { const char *error = sqlite3_errmsg(database); - _logger->log(LogLevel::ERROR, "[Countly][StorageModuleDB][Vacuum] " + std::string(error)); + _logger->log(LogLevel::ERROR, "[Countly] [StorageModuleDB] vacuumDatabase, Could not open database: " + std::string(error)); } sqlite3_close(database); #endif } catch (const std::system_error &e) { std::ostringstream log_message; - log_message << "vacuumDatabase, error: " << e.what(); + log_message << "[Countly] [StorageModuleDB] vacuumDatabase, error: " << e.what(); _logger->log(LogLevel::FATAL, log_message.str()); } } bool StorageModuleDB::createSchema(const char tableName[], const char keyColumnName[], const char dataColumnName[]) { try { - _logger->log(LogLevel::INFO, "[StorageModuleDB][createSchema]"); + _logger->log(LogLevel::INFO, "[Countly] [StorageModuleDB] createSchema, Start"); bool result = false; #ifdef COUNTLY_USE_SQLITE @@ -93,14 +97,14 @@ bool StorageModuleDB::createSchema(const char tableName[], const char keyColumnN // Execute the SQL statement return_value = sqlite3_exec(database, statement.c_str(), nullptr, nullptr, &error_message); if (return_value != SQLITE_OK) { - _logger->log(LogLevel::ERROR, error_message); + _logger->log(LogLevel::ERROR, "[Countly] [StorageModuleDB] createSchema, SQLite error: " + std::string(error_message)); sqlite3_free(error_message); } else { result = true; } } else { const char *error = sqlite3_errmsg(database); - _logger->log(LogLevel::ERROR, "[Countly][StorageModuleDB][createSchema] " + std::string(error)); + _logger->log(LogLevel::ERROR, "[Countly] [StorageModuleDB] createSchema, Could not open database: " + std::string(error)); } // Close the SQLite database @@ -110,8 +114,9 @@ bool StorageModuleDB::createSchema(const char tableName[], const char keyColumnN return result; } catch (const std::system_error &e) { std::ostringstream log_message; - log_message << "createSchema, error: " << e.what(); + log_message << "[Countly] [StorageModuleDB] createSchema, error: " << e.what(); _logger->log(LogLevel::FATAL, log_message.str()); + return false; } } @@ -119,11 +124,11 @@ bool StorageModuleDB::createSchema(const char tableName[], const char keyColumnN void StorageModuleDB::RQRemoveFront() { try { if (!_is_initialized) { - _logger->log(LogLevel::ERROR, "[Countly][StorageModuleDB] RQRemoveFront: Module is not initialized"); + _logger->log(LogLevel::ERROR, "[Countly] [StorageModuleDB] RQRemoveFront, Module is not initialized"); return; } - _logger->log(LogLevel::DEBUG, "[Countly][StorageModuleDB] RQRemoveFront"); + _logger->log(LogLevel::DEBUG, "[Countly] [StorageModuleDB] RQRemoveFront, Start"); #ifdef COUNTLY_USE_SQLITE // Declare SQLite database, return value and error message variables @@ -136,7 +141,7 @@ void StorageModuleDB::RQRemoveFront() { // Remove the first entry in the requests table std::ostringstream sql_statement_stream; sql_statement_stream << "DELETE FROM " << REQUESTS_TABLE_NAME << " WHERE " << REQUESTS_TABLE_REQUEST_ID << " = ( SELECT MIN(" << REQUESTS_TABLE_REQUEST_ID << ") FROM " << REQUESTS_TABLE_NAME << " );"; - _logger->log(LogLevel::DEBUG, "[Countly][StorageModuleDB] RQRemoveFront SQL = " + sql_statement_stream.str()); + _logger->log(LogLevel::DEBUG, "[Countly] [StorageModuleDB] RQRemoveFront, SQL = " + sql_statement_stream.str()); std::string sql_statement = sql_statement_stream.str(); @@ -144,7 +149,7 @@ void StorageModuleDB::RQRemoveFront() { return_value = sqlite3_exec(database, sql_statement.c_str(), nullptr, nullptr, &error_message); if (return_value != SQLITE_OK) { std::string error(error_message); - _logger->log(LogLevel::ERROR, "[Countly][StorageModuleDB] RQRemoveFront error = " + error); + _logger->log(LogLevel::ERROR, "[Countly] [StorageModuleDB] RQRemoveFront, error = " + error); sqlite3_free(error_message); } } @@ -153,7 +158,7 @@ void StorageModuleDB::RQRemoveFront() { #endif } catch (const std::system_error &e) { std::ostringstream log_message; - log_message << "RQRemoveFront, error: " << e.what(); + log_message << "[Countly] [StorageModuleDB] RQRemoveFront, error: " << e.what(); _logger->log(LogLevel::FATAL, log_message.str()); } } @@ -161,18 +166,18 @@ void StorageModuleDB::RQRemoveFront() { void StorageModuleDB::RQRemoveFront(std::shared_ptr request) { try { if (!_is_initialized) { - _logger->log(LogLevel::ERROR, "[Countly][StorageModuleDB] RQRemoveFront(request): Module is not initialized"); + _logger->log(LogLevel::ERROR, "[Countly] [StorageModuleDB] RQRemoveFront, Module is not initialized"); return; } if (request == nullptr) { // Check if request is null - _logger->log(LogLevel::WARNING, "[Countly][StorageModuleDB] RQRemoveFront request = null"); + _logger->log(LogLevel::WARNING, "[Countly] [StorageModuleDB] RQRemoveFront, request is null"); return; } // Log the request ID being removed - _logger->log(LogLevel::DEBUG, "[Countly][StorageModuleDB] RQRemoveFront RequestID = " + request->getId()); + _logger->log(LogLevel::DEBUG, "[Countly] [StorageModuleDB] RQRemoveFront, Removing RequestID = " + std::to_string(request->getId())); #ifdef COUNTLY_USE_SQLITE sqlite3 *database; @@ -184,7 +189,7 @@ void StorageModuleDB::RQRemoveFront(std::shared_ptr request) { // Build SQL statement to remove request from database std::ostringstream sql_statement_stream; sql_statement_stream << "DELETE FROM " << REQUESTS_TABLE_NAME << " WHERE " << REQUESTS_TABLE_REQUEST_ID << " = " << request->getId() << ';'; - _logger->log(LogLevel::DEBUG, "[Countly][StorageModuleDB] RQRemoveFront SQL = " + sql_statement_stream.str()); + _logger->log(LogLevel::DEBUG, "[Countly] [StorageModuleDB] RQRemoveFront SQL = " + sql_statement_stream.str()); std::string sql_statement = sql_statement_stream.str(); @@ -192,7 +197,7 @@ void StorageModuleDB::RQRemoveFront(std::shared_ptr request) { return_value = sqlite3_exec(database, sql_statement.c_str(), nullptr, nullptr, &error_message); if (return_value != SQLITE_OK) { std::string error(error_message); - _logger->log(LogLevel::ERROR, "[Countly][StorageModuleDB] RQRemoveFront error = " + error); + _logger->log(LogLevel::ERROR, "[Countly] [StorageModuleDB] RQRemoveFront error = " + error); sqlite3_free(error_message); } } @@ -209,11 +214,11 @@ void StorageModuleDB::RQRemoveFront(std::shared_ptr request) { long long StorageModuleDB::RQCount() { try { if (!_is_initialized) { - _logger->log(LogLevel::ERROR, "[Countly][StorageModuleDB] RQCount: Module is not initialized"); + _logger->log(LogLevel::ERROR, "[Countly] [StorageModuleDB] RQCount, Module is not initialized"); return -1; } - _logger->log(LogLevel::DEBUG, "[Countly][StorageModuleDB] RQCount"); + _logger->log(LogLevel::DEBUG, "[Countly] [StorageModuleDB] RQCount, Start"); long long requestCount = 0; #ifdef COUNTLY_USE_SQLITE @@ -237,7 +242,7 @@ long long StorageModuleDB::RQCount() { } else { // Log any errors encountered during the execution of the SQL statement std::string error(error_message); - _logger->log(LogLevel::ERROR, "[Countly][StorageModuleDB] RQCount error = " + error); + _logger->log(LogLevel::ERROR, "[Countly] [StorageModuleDB] RQCount, error = " + error); sqlite3_free(error_message); } // Free the memory allocated for the result table @@ -248,24 +253,25 @@ long long StorageModuleDB::RQCount() { #endif // Log the number of requests in the requests table - _logger->log(LogLevel::DEBUG, "[Countly][StorageModuleDB] RQCount requests count = " + std::to_string(requestCount)); + _logger->log(LogLevel::DEBUG, "[Countly] [StorageModuleDB] RQCount, requests count = " + std::to_string(requestCount)); // Return the number of requests return requestCount; } catch (const std::system_error &e) { std::ostringstream log_message; - log_message << "RQCount, error: " << e.what(); + log_message << "[Countly] [StorageModuleDB] RQCount, error: " << e.what(); _logger->log(LogLevel::FATAL, log_message.str()); + return -1; } } std::vector> StorageModuleDB::RQPeekAll() { try { if (!_is_initialized) { - _logger->log(LogLevel::ERROR, "[Countly][StorageModuleDB] RQPeekAll: Module is not initialized"); + _logger->log(LogLevel::ERROR, "[Countly] [StorageModuleDB] RQPeekAll, Module is not initialized"); return {}; // Return an empty vector if the module is not initialized } - _logger->log(LogLevel::DEBUG, "[Countly][StorageModuleDB] RQPeekAll"); + _logger->log(LogLevel::DEBUG, "[Countly] [StorageModuleDB] RQPeekAll, Start"); std::vector> v; // Initialize a vector to store the data entries @@ -298,7 +304,7 @@ std::vector> StorageModuleDB::RQPeekAll() { } else if (return_value != SQLITE_OK) { // If there was an error executing the query std::string error(error_message); - _logger->log(LogLevel::ERROR, "[Countly][StorageModuleDB] RQPeekAll error =" + error); + _logger->log(LogLevel::ERROR, "[Countly] [StorageModuleDB] RQPeekAll, error = " + error); sqlite3_free(error_message); } // Free the result table @@ -310,23 +316,24 @@ std::vector> StorageModuleDB::RQPeekAll() { return v; // Return the vector containing all the data entries } catch (const std::system_error &e) { std::ostringstream log_message; - log_message << "RQPeekAll, error: " << e.what(); + log_message << "[Countly] [StorageModuleDB] RQPeekAll, error: " << e.what(); _logger->log(LogLevel::FATAL, log_message.str()); + return {}; } } void StorageModuleDB::RQInsertAtEnd(const std::string &request) { try { if (!_is_initialized) { - _logger->log(LogLevel::ERROR, "[Countly][StorageModuleDB] RQInsertAtEnd: Module is not initialized"); + _logger->log(LogLevel::ERROR, "[Countly] [StorageModuleDB] RQInsertAtEnd, Module is not initialized"); return; // Checks if the module is initialized, returns if not } // Logs the request being inserted - _logger->log(LogLevel::DEBUG, "[Countly][StorageModuleDB] RQInsertAtEnd request = " + request); + _logger->log(LogLevel::DEBUG, "[Countly] [StorageModuleDB] RQInsertAtEnd, request = [" + request + "]"); if (request == "") { - _logger->log(LogLevel::WARNING, "[Countly][StorageModuleMemory] RQInsertAtEnd request is empty"); + _logger->log(LogLevel::WARNING, "[Countly] [StorageModuleDB] RQInsertAtEnd, request is empty"); return; // Checks if the request is empty, logs a warning and returns if it is } @@ -347,7 +354,7 @@ void StorageModuleDB::RQInsertAtEnd(const std::string &request) { return_value = sqlite3_exec(database, sql_statement.c_str(), nullptr, nullptr, &error_message); if (return_value != SQLITE_OK) { std::string error(error_message); - _logger->log(LogLevel::ERROR, "[Countly][StorageModuleDB] RQInsertAtEnd error =" + error); + _logger->log(LogLevel::ERROR, "[Countly] [StorageModuleDB] RQInsertAtEnd, error = " + error); sqlite3_free(error_message); } } @@ -356,7 +363,7 @@ void StorageModuleDB::RQInsertAtEnd(const std::string &request) { #endif } catch (const std::system_error &e) { std::ostringstream log_message; - log_message << "RQInsertAtEnd, error: " << e.what(); + log_message << "[Countly] [StorageModuleDB] RQInsertAtEnd, error: " << e.what(); _logger->log(LogLevel::FATAL, log_message.str()); } } @@ -364,10 +371,10 @@ void StorageModuleDB::RQInsertAtEnd(const std::string &request) { void StorageModuleDB::RQClearAll() { try { if (!_is_initialized) { - _logger->log(LogLevel::ERROR, "[Countly][StorageModuleDB] RQClearAll: Module is not initialized"); + _logger->log(LogLevel::ERROR, "[Countly] [StorageModuleDB] RQClearAll, Module is not initialized"); return; } - _logger->log(LogLevel::DEBUG, "[Countly][StorageModuleDB] RQClearAll"); + _logger->log(LogLevel::DEBUG, "[Countly] [StorageModuleDB] RQClearAll, Start"); #ifdef COUNTLY_USE_SQLITE sqlite3 *database; @@ -384,7 +391,7 @@ void StorageModuleDB::RQClearAll() { return_value = sqlite3_exec(database, sql_statement.c_str(), nullptr, nullptr, &error_message); if (return_value != SQLITE_OK) { // Check if SQL statement executed successfully std::string error(error_message); - _logger->log(LogLevel::ERROR, "[Countly][StorageModuleDB] RQRemoveFront error = " + error); + _logger->log(LogLevel::ERROR, "[Countly] [StorageModuleDB] RQClearAll, error = " + error); sqlite3_free(error_message); } } @@ -393,7 +400,7 @@ void StorageModuleDB::RQClearAll() { #endif } catch (const std::system_error &e) { std::ostringstream log_message; - log_message << "RQClearAll, error: " << e.what(); + log_message << "[Countly] [StorageModuleDB] RQClearAll, error: " << e.what(); _logger->log(LogLevel::FATAL, log_message.str()); } } @@ -402,11 +409,11 @@ const std::shared_ptr StorageModuleDB::RQPeekFront() { try { std::shared_ptr front = std::make_shared(-1, ""); // Initialize a shared pointer to a default-constructed DataEntry object if (!_is_initialized) { - _logger->log(LogLevel::ERROR, "[Countly][StorageModuleDB] RQPeekFront: Module is not initialized"); + _logger->log(LogLevel::ERROR, "[Countly] [StorageModuleDB] RQPeekFront, Module is not initialized"); return front; } - _logger->log(LogLevel::DEBUG, "[Countly][StorageModuleDB] RQPeekFronts"); + _logger->log(LogLevel::DEBUG, "[Countly] [StorageModuleDB] RQPeekFront, Start"); #ifdef COUNTLY_USE_SQLITE sqlite3 *database; @@ -433,12 +440,12 @@ const std::shared_ptr StorageModuleDB::RQPeekFront() { std::string request = table[(event_index * column_count) + 1]; // Create a new DataEntry object and reset the shared pointer to point to it DataEntry *frontEntry = new DataEntry(std::stoll(requestId), request); - _logger->log(LogLevel::DEBUG, "[Countly][StorageModuleDB] RQPeekFronts id =" + requestId); + _logger->log(LogLevel::DEBUG, "[Countly] [StorageModuleDB] RQPeekFront, id = " + requestId); front.reset(frontEntry); } } else if (return_value != SQLITE_OK) { std::string error(error_message); - _logger->log(LogLevel::ERROR, "[Countly][StorageModuleDB] RQPeekFronts error =" + error); + _logger->log(LogLevel::ERROR, "[Countly] [StorageModuleDB] RQPeekFront, error = " + error); sqlite3_free(error_message); // free the error message pointer } sqlite3_free_table(table); // Free the table pointer @@ -449,8 +456,102 @@ const std::shared_ptr StorageModuleDB::RQPeekFront() { return front; // Return the shared pointer to the DataEntry object } catch (const std::system_error &e) { std::ostringstream log_message; - log_message << "RQPeekFront, error: " << e.what(); + log_message << "[Countly] [StorageModuleDB] RQPeekFront, error: " << e.what(); _logger->log(LogLevel::FATAL, log_message.str()); + return std::shared_ptr(new DataEntry(-1, "")); + } +} + +void StorageModuleDB::storeSDKBehaviorSettings(const std::string &sdk_behavior_settings) { + try { + if (!_is_initialized) { + _logger->log(LogLevel::ERROR, "[Countly] [StorageModuleDB] storeSDKBehaviorSettings, Module is not initialized"); + return; + } + + if (sdk_behavior_settings.empty()) { + _logger->log(LogLevel::WARNING, "[Countly] [StorageModuleDB] storeSDKBehaviorSettings, Empty data"); + return; + } + + _logger->log(LogLevel::DEBUG, "[Countly] [StorageModuleDB] storeSDKBehaviorSettings, Start"); + +#ifdef COUNTLY_USE_SQLITE + sqlite3 *db = nullptr; + sqlite3_stmt *stmt = nullptr; + + if (sqlite3_open(_configuration->databasePath.c_str(), &db) != SQLITE_OK) { + _logger->log(LogLevel::ERROR, "[Countly] [StorageModuleDB] storeSDKBehaviorSettings, Failed to open database"); + return; + } + + const char *sql = "INSERT OR REPLACE INTO " SDK_BEHAVIOR_SETTINGS_TABLE_NAME " (" SDK_BEHAVIOR_SETTINGS_KEY_COLUMN_NAME ", " SDK_BEHAVIOR_SETTINGS_DATA_COLUMN_NAME ") " + "VALUES (?, ?);"; + + if (sqlite3_prepare_v2(db, sql, -1, &stmt, nullptr) != SQLITE_OK) { + _logger->log(LogLevel::ERROR, "[Countly] [StorageModuleDB] storeSDKBehaviorSettings, Failed to prepare statement"); + sqlite3_close(db); + return; + } + + sqlite3_bind_int(stmt, 1, SDK_BEHAVIOR_SETTINGS_KEY_VALUE); + sqlite3_bind_text(stmt, 2, sdk_behavior_settings.c_str(), -1, SQLITE_TRANSIENT); + + if (sqlite3_step(stmt) != SQLITE_DONE) { + const char *err = sqlite3_errmsg(db); + _logger->log(LogLevel::ERROR, std::string("[Countly] [StorageModuleDB] storeSDKBehaviorSettings, failed: [") + err + "]"); + } + + sqlite3_finalize(stmt); + sqlite3_close(db); +#endif + } catch (const std::exception &e) { + _logger->log(LogLevel::ERROR, std::string("[Countly] [StorageModuleDB] storeSDKBehaviorSettings, exception: [") + e.what() + "]"); + } +} + +std::string StorageModuleDB::getSDKBehaviorSettings() { + try { + if (!_is_initialized) { + _logger->log(LogLevel::ERROR, "[Countly] [StorageModuleDB] getSDKBehaviorSettings, Module is not initialized"); + return ""; + } + +#ifdef COUNTLY_USE_SQLITE + sqlite3 *db = nullptr; + sqlite3_stmt *stmt = nullptr; + std::string result; + + if (sqlite3_open(_configuration->databasePath.c_str(), &db) != SQLITE_OK) { + _logger->log(LogLevel::ERROR, "[Countly] [StorageModuleDB] getSDKBehaviorSettings, Failed to open database"); + return ""; + } + + const char *sql = "SELECT " SDK_BEHAVIOR_SETTINGS_DATA_COLUMN_NAME " FROM " SDK_BEHAVIOR_SETTINGS_TABLE_NAME " LIMIT 1;"; + + if (sqlite3_prepare_v2(db, sql, -1, &stmt, nullptr) == SQLITE_OK) { + if (sqlite3_step(stmt) == SQLITE_ROW) { + const unsigned char *text = sqlite3_column_text(stmt, 0); + if (text) { + result = reinterpret_cast(text); + } + } + } else { + _logger->log(LogLevel::ERROR, "[Countly] [StorageModuleDB] getSDKBehaviorSettings, Failed to prepare statement"); + } + + if (stmt) { + sqlite3_finalize(stmt); + } + sqlite3_close(db); + + return result; +#else + return ""; +#endif + } catch (const std::exception &e) { + _logger->log(LogLevel::ERROR, std::string("[Countly] [StorageModuleDB] getSDKBehaviorSettings, exception: [") + e.what() + "]"); + return ""; } } diff --git a/src/storage_module_memory.cpp b/src/storage_module_memory.cpp index eadb4c3..74ef3f7 100644 --- a/src/storage_module_memory.cpp +++ b/src/storage_module_memory.cpp @@ -13,17 +13,17 @@ StorageModuleMemory::~StorageModuleMemory() { } void StorageModuleMemory::init() { - _logger->log(LogLevel::DEBUG, "[Countly][StorageModuleMemory] initialized."); + _logger->log(LogLevel::DEBUG, "[Countly] [StorageModuleMemory] init, Initialized."); _is_initialized = true; } void StorageModuleMemory::RQRemoveFront() { if (!_is_initialized) { - _logger->log(LogLevel::ERROR, "[Countly][StorageModuleMemory] RQRemoveFront: Module is not initialized"); + _logger->log(LogLevel::ERROR, "[Countly] [StorageModuleMemory] RQRemoveFront, Module is not initialized"); return; } - _logger->log(LogLevel::DEBUG, "[Countly][StorageModuleMemory] RQRemoveFront"); + _logger->log(LogLevel::DEBUG, "[Countly] [StorageModuleMemory] RQRemoveFront, Start"); if (request_queue.size() > 0) { request_queue.pop_front(); } @@ -31,39 +31,39 @@ void StorageModuleMemory::RQRemoveFront() { void StorageModuleMemory::RQRemoveFront(std::shared_ptr request) { if (!_is_initialized) { - _logger->log(LogLevel::ERROR, "[Countly][StorageModuleMemory] RQRemoveFront(request): Module is not initialized"); + _logger->log(LogLevel::ERROR, "[Countly] [StorageModuleMemory] RQRemoveFront, Module is not initialized"); return; } if (request == nullptr) { - _logger->log(LogLevel::WARNING, "[Countly][StorageModuleMemory] RQRemoveFront request = null"); + _logger->log(LogLevel::WARNING, "[Countly] [StorageModuleMemory] RQRemoveFront, request is null"); return; } if (request_queue.size() > 0 && request->getId() == request_queue.front()->getId()) { - _logger->log(LogLevel::DEBUG, "[Countly][StorageModuleMemory] RQRemoveFront request = " + request->getData()); + _logger->log(LogLevel::DEBUG, "[Countly] [StorageModuleMemory] RQRemoveFront, Removing request = [" + request->getData() + "]"); request_queue.pop_front(); } } long long StorageModuleMemory::RQCount() { if (!_is_initialized) { - _logger->log(LogLevel::ERROR, "[Countly][StorageModuleMemory] RQRemoveFront(request): Module is not initialized"); + _logger->log(LogLevel::ERROR, "[Countly] [StorageModuleMemory] RQCount, Module is not initialized"); return -1; } long long size = request_queue.size(); - _logger->log(LogLevel::DEBUG, "[Countly][StorageModuleMemory] RQCount size = [" + std::to_string(size) + "]"); + _logger->log(LogLevel::DEBUG, "[Countly] [StorageModuleMemory] RQCount, size = [" + std::to_string(size) + "]"); return size; } void StorageModuleMemory::RQInsertAtEnd(const std::string &request) { if (!_is_initialized) { - _logger->log(LogLevel::ERROR, "[Countly][StorageModuleMemory] RQInsertAtEnd: Module is not initialized"); + _logger->log(LogLevel::ERROR, "[Countly] [StorageModuleMemory] RQInsertAtEnd, Module is not initialized"); return; } - _logger->log(LogLevel::DEBUG, "[Countly][StorageModuleMemory] RQInsertAtEnd request = " + request); + _logger->log(LogLevel::DEBUG, "[Countly] [StorageModuleMemory] RQInsertAtEnd, request = [" + request + "]"); if (request != "") { if (request_queue.empty()) { // Since the DB (Sqlite) storage module reset the primary key when all rows get deleted. To sync with the DB storage module, the memory storage module also reset '_lastUsedId' when the request queue is empty. @@ -76,17 +76,17 @@ void StorageModuleMemory::RQInsertAtEnd(const std::string &request) { entry.reset(new DataEntry(_lastUsedId, request)); request_queue.push_back(entry); } else { - _logger->log(LogLevel::WARNING, "[Countly][StorageModuleMemory] RQInsertAtEnd request is empty"); + _logger->log(LogLevel::WARNING, "[Countly] [StorageModuleMemory] RQInsertAtEnd, request is empty"); } } std::vector> StorageModuleMemory::RQPeekAll() { if (!_is_initialized) { - _logger->log(LogLevel::ERROR, "[Countly][StorageModuleMemory] RQPeekAll: Module is not initialized"); + _logger->log(LogLevel::ERROR, "[Countly] [StorageModuleMemory] RQPeekAll, Module is not initialized"); return {}; } - _logger->log(LogLevel::DEBUG, "[Countly][StorageModuleMemory] RQPeekAll"); + _logger->log(LogLevel::DEBUG, "[Countly] [StorageModuleMemory] RQPeekAll, Start"); int qSize = request_queue.size(); std::vector> v(qSize); for (int i = 0; i < request_queue.size(); ++i) { @@ -98,28 +98,37 @@ std::vector> StorageModuleMemory::RQPeekAll() { void StorageModuleMemory::RQClearAll() { if (!_is_initialized) { - _logger->log(LogLevel::ERROR, "[Countly][StorageModuleMemory] RQClearAll: Module is not initialized"); + _logger->log(LogLevel::ERROR, "[Countly] [StorageModuleMemory] RQClearAll, Module is not initialized"); return; } - _logger->log(LogLevel::DEBUG, "[Countly][StorageModuleMemory] RQClearAll"); + _logger->log(LogLevel::DEBUG, "[Countly] [StorageModuleMemory] RQClearAll, Start"); request_queue.clear(); } +void StorageModuleMemory::storeSDKBehaviorSettings(const std::string &sdk_behavior_settings) { + // For in-memory storage, it is already stored in memory inside the module. +} + +std::string StorageModuleMemory::getSDKBehaviorSettings() { + // For in-memory storage, it is already stored in memory inside the module. + return ""; +} + const std::shared_ptr StorageModuleMemory::RQPeekFront() { std::shared_ptr front = nullptr; if (!_is_initialized) { - _logger->log(LogLevel::ERROR, "[Countly][StorageModuleMemory] RQPeekFront: Module is not initialized"); + _logger->log(LogLevel::ERROR, "[Countly] [StorageModuleMemory] RQPeekFront, Module is not initialized"); front.reset(new DataEntry(-1, "")); return front; } if (request_queue.size() > 0) { front = request_queue.front(); - _logger->log(LogLevel::DEBUG, "[Countly][StorageModuleMemory] RQPeekFront: request = " + front->getData()); + _logger->log(LogLevel::DEBUG, "[Countly] [StorageModuleMemory] RQPeekFront, request = [" + front->getData() + "]"); } else { front.reset(new DataEntry(-1, "")); - _logger->log(LogLevel::WARNING, "[Countly][StorageModuleMemory] RQPeekFront: Request queue is empty."); + _logger->log(LogLevel::WARNING, "[Countly] [StorageModuleMemory] RQPeekFront, Request queue is empty."); } return front; diff --git a/src/views_module.cpp b/src/views_module.cpp index e1a0f4d..2b833c9 100644 --- a/src/views_module.cpp +++ b/src/views_module.cpp @@ -68,11 +68,21 @@ class ViewsModule::ViewModuleImpl { public: std::shared_ptr _logger; + std::weak_ptr _configProvider; ViewModuleImpl(cly::CountlyDelegates *cly, std::shared_ptr logger) : _cly(cly), _logger(logger) {} ~ViewModuleImpl() { _logger.reset(); } std::string _openView(const std::string &name, const std::map &segmentation) { + if (std::shared_ptr config = _configProvider.lock()) { + if (config->isViewTrackingEnabled() == false) { + _logger->log(LogLevel::DEBUG, "[Countly] [ViewsModule] _openView, View tracking is disabled. Not opening view."); + return ""; + } + } else { + _logger->log(LogLevel::WARNING, "[Countly] [ViewsModule] _openView, ConfigurationProvider unavailable."); + return ""; + } ViewModuleImpl::ViewInfo *v = new ViewModuleImpl::ViewInfo(); v->name = name; v->viewId = cly::utils::generateEventID(); @@ -87,10 +97,19 @@ class ViewsModule::ViewModuleImpl { } void _closeViewWithName(const std::string &name) { + if (std::shared_ptr config = _configProvider.lock()) { + if (config->isViewTrackingEnabled() == false) { + _logger->log(LogLevel::DEBUG, "[Countly] [ViewsModule] _closeViewWithName, View tracking is disabled. Not closing view."); + return; + } + } else { + _logger->log(LogLevel::WARNING, "[Countly] [ViewsModule] _closeViewWithName, ConfigurationProvider unavailable."); + return; + } std::shared_ptr v = findViewByName(name); if (v == nullptr) { - _logger->log(cly::LogLevel::WARNING, cly::utils::format_string("[ViewModuleImpl] _closeViewWithName: Couldn't found " - "view with name = %s", + _logger->log(cly::LogLevel::WARNING, cly::utils::format_string("[Countly] [ViewsModule] _closeViewWithName, Couldn't find " + "view with name = [%s]", name.c_str())); return; } @@ -98,10 +117,19 @@ class ViewsModule::ViewModuleImpl { } void _closeViewWithID(const std::string &viewId) { + if (std::shared_ptr config = _configProvider.lock()) { + if (config->isViewTrackingEnabled() == false) { + _logger->log(LogLevel::DEBUG, "[Countly] [ViewsModule] _closeViewWithID, View tracking is disabled. Not closing view."); + return; + } + } else { + _logger->log(LogLevel::WARNING, "[Countly] [ViewsModule] _closeViewWithID, ConfigurationProvider unavailable."); + return; + } if (_viewsStartTime.find(viewId) == _viewsStartTime.end()) { - _logger->log(cly::LogLevel::WARNING, cly::utils::format_string("[ViewModuleImpl] _closeViewWithID: Couldn't found " - "view with viewId = %s", + _logger->log(cly::LogLevel::WARNING, cly::utils::format_string("[Countly] [ViewsModule] _closeViewWithID, Couldn't find " + "view with viewId = [%s]", viewId.c_str())); return; } @@ -113,17 +141,17 @@ class ViewsModule::ViewModuleImpl { ViewsModule::ViewsModule(cly::CountlyDelegates *cly, std::shared_ptr logger) { impl.reset(new ViewModuleImpl(cly, logger)); - impl->_logger->log(cly::LogLevel::DEBUG, cly::utils::format_string("[ViewsModule] Initialized")); + impl->_logger->log(cly::LogLevel::DEBUG, cly::utils::format_string("[Countly] [ViewsModule] Initialized")); } ViewsModule::~ViewsModule() { impl.reset(); } std::string ViewsModule::openView(const std::string &name, const std::map &segmentation) { - impl->_logger->log(cly::LogLevel::INFO, cly::utils::format_string("[ViewsModule] openView: name = %s, segmentation = %s", name.c_str(), utils::mapToString(segmentation).c_str())); + impl->_logger->log(cly::LogLevel::INFO, cly::utils::format_string("[Countly] [ViewsModule] openView, name = [%s], segmentation = [%s]", name.c_str(), utils::mapToString(segmentation).c_str())); if (name.empty()) { - impl->_logger->log(cly::LogLevel::WARNING, "[ViewsModule] openView: view name can not be null or empty!"); + impl->_logger->log(cly::LogLevel::WARNING, "[Countly] [ViewsModule] openView, view name can not be null or empty!"); return {}; } @@ -131,23 +159,26 @@ std::string ViewsModule::openView(const std::string &name, const std::map_logger->log(cly::LogLevel::INFO, cly::utils::format_string("[ViewsModule] closeViewWithName: name = %s", name.c_str())); + impl->_logger->log(cly::LogLevel::INFO, cly::utils::format_string("[Countly] [ViewsModule] closeViewWithName, name = [%s]", name.c_str())); if (name.empty()) { - impl->_logger->log(cly::LogLevel::WARNING, "[ViewsModule] closeViewWithName: view name can not be null or empty!"); + impl->_logger->log(cly::LogLevel::WARNING, "[Countly] [ViewsModule] closeViewWithName, view name can not be null or empty!"); return; } impl->_closeViewWithName(name); } void ViewsModule::closeViewWithID(const std::string &viewId) { - impl->_logger->log(cly::LogLevel::INFO, cly::utils::format_string("[ViewsModule] closeViewWithID: viewId = %s", viewId.c_str())); + impl->_logger->log(cly::LogLevel::INFO, cly::utils::format_string("[Countly] [ViewsModule] closeViewWithID, viewId = [%s]", viewId.c_str())); if (viewId.empty()) { - impl->_logger->log(cly::LogLevel::WARNING, "[ViewsModule] closeViewWithID: viewId can not be null or empty!"); + impl->_logger->log(cly::LogLevel::WARNING, "[Countly] [ViewsModule] closeViewWithID, viewId can not be null or empty!"); return; } impl->_closeViewWithID(viewId); } + +void ViewsModule::setConfigurationProvider(std::weak_ptr provider) { impl->_configProvider = std::move(provider); } + } // namespace cly \ No newline at end of file diff --git a/tests/config.cpp b/tests/config.cpp index 002a590..c17c006 100644 --- a/tests/config.cpp +++ b/tests/config.cpp @@ -52,6 +52,7 @@ TEST_CASE("Validate setting configuration values") { CHECK(config.forcePost == false); CHECK(config.port == 443); CHECK(config.manualSessionControl == false); + CHECK(config.immediateRequestOnStop == false); CHECK(config.sha256_function == nullptr); CHECK(config.http_client_function == nullptr); CHECK(config.metrics.empty()); @@ -78,6 +79,7 @@ TEST_CASE("Validate setting configuration values") { ct.SetPath(TEST_DATABASE_NAME); ct.setMaxRQProcessingBatchSize(10); ct.enableManualSessionControl(); + ct.enableImmediateRequestOnStop(); ct.start("YOUR_APP_KEY", "https://try.count.ly", -1, false); // Get configuration values using Countly getters @@ -97,6 +99,7 @@ TEST_CASE("Validate setting configuration values") { CHECK(config.forcePost == true); CHECK(config.port == 443); CHECK(config.manualSessionControl == true); + CHECK(config.immediateRequestOnStop == true); CHECK(config.sha256_function("custom SHA256") == customSha_1_returnValue); HTTPResponse response = config.http_client_function(true, "", ""); @@ -182,6 +185,7 @@ TEST_CASE("Validate setting configuration values") { ct.setSalt("new-salt"); ct.setMaxRequestQueueSize(100); ct.SetPath("new_database.db"); + ct.enableImmediateRequestOnStop(); // get SDK configuration again and make sure that they haven't changed config = ct.getConfiguration(); @@ -199,6 +203,7 @@ TEST_CASE("Validate setting configuration values") { CHECK(config.breadcrumbsThreshold == 100); CHECK(config.forcePost == true); CHECK(config.port == 443); + CHECK(config.immediateRequestOnStop == false); // was never enabled before init, should stay false CHECK(config.sha256_function("custom SHA256") == customSha_1_returnValue); response = config.http_client_function(true, "", ""); diff --git a/tests/crash.cpp b/tests/crash.cpp index 2fd05ea..ffe382a 100644 --- a/tests/crash.cpp +++ b/tests/crash.cpp @@ -1,8 +1,10 @@ +#include #include #include #include #include #include +#include #include "countly.hpp" #include "doctest.h" @@ -13,16 +15,15 @@ using namespace test_utils; using namespace cly; -void validateCrashParams(const std::string &title, const std::string &stackTrace, const bool fatal, const std::string &breadCrumbs, const std::map &crashMetrics, const std::map &segmentation) { +void validateCrashParams(const std::string &title, const std::string &stackTrace, const bool fatal, const std::string &breadCrumbs, const std::map &crashMetrics, const std::map &segmentation, int idx = 0) { CHECK(!http_call_queue.empty()); - HTTPCall http_call = http_call_queue.front(); + HTTPCall http_call = http_call_queue.at(1 + idx); // not front anymore because sbs is 0 now long long timestamp = getUnixTimestamp(); long long timestampDiff = timestamp - std::stoll(http_call.data["timestamp"]); CHECK(http_call.data["app_key"] == COUNTLY_TEST_APP_KEY); CHECK(http_call.data["device_id"] == COUNTLY_TEST_DEVICE_ID); CHECK(timestampDiff >= 0); CHECK(timestampDiff <= 1000); - nlohmann::json c = nlohmann::json::parse(http_call.data["crash"]); CHECK(c["_name"].get() == title); CHECK(c["_error"].get() == stackTrace); @@ -41,18 +42,19 @@ void validateCrashParams(const std::string &title, const std::string &stackTrace CHECK(s[segment.first].get() == segment.second); } } - - http_call_queue.pop_front(); } TEST_CASE("crash unit tests") { clearSDK(); + http_call_queue.clear(); Countly &countly = Countly::getInstance(); countly.setHTTPClient(test_utils::fakeSendHTTP); countly.setDeviceID(COUNTLY_TEST_DEVICE_ID); countly.SetPath(TEST_DATABASE_NAME); countly.start(COUNTLY_TEST_APP_KEY, COUNTLY_TEST_HOST, COUNTLY_TEST_PORT, false); + // Wait for the async SBS config fetch thread to complete + std::this_thread::sleep_for(std::chrono::milliseconds(200)); SUBCASE("record crash without bread crumbs") { // clear the request queue, it contains session begin request @@ -122,6 +124,6 @@ TEST_CASE("crash unit tests") { countly.processRQDebug(); // validate crash request - validateCrashParams("Divided By Zero", "stackTrack", true, "first\nsecond\n", crashMetrics, segmentation); + validateCrashParams("Divided By Zero", "stackTrack", true, "first\nsecond\n", crashMetrics, segmentation, 1); } } diff --git a/tests/event.cpp b/tests/event.cpp index 9213013..1faa3a6 100644 --- a/tests/event.cpp +++ b/tests/event.cpp @@ -1,5 +1,6 @@ #include "countly/event.hpp" #include "doctest.h" +#include #include #include using namespace cly; @@ -8,6 +9,14 @@ void valideEventParams(nlohmann::json eventJson, const std::string key, int coun CHECK(eventJson["key"].get() == key); CHECK(eventJson["count"].get() == count); CHECK(std::to_string(eventJson["timestamp"].get()).size() == 13); + CHECK(eventJson.contains("dow")); + CHECK(eventJson.contains("hour")); + int dow = eventJson["dow"].get(); + int hour = eventJson["hour"].get(); + CHECK(dow >= 0); + CHECK(dow <= 6); + CHECK(hour >= 0); + CHECK(hour <= 23); } TEST_CASE("events are serialized correctly") { @@ -98,3 +107,35 @@ TEST_CASE("events are serialized correctly") { } } } + +TEST_CASE("events contain correct dow and hour") { + SUBCASE("dow and hour match local time") { + cly::Event event("test_time", 1); + nlohmann::json e = nlohmann::json::parse(event.serialize()); + + std::time_t now = std::time(nullptr); + std::tm local_tm = *std::localtime(&now); + + CHECK(e["dow"].get() == local_tm.tm_wday); + CHECK(e["hour"].get() == local_tm.tm_hour); + } + + SUBCASE("dow and hour are updated on startTimer") { + cly::Event event("timed_event", 1); + nlohmann::json before = nlohmann::json::parse(event.serialize()); + + CHECK(before.contains("dow")); + CHECK(before.contains("hour")); + + event.startTimer(); + nlohmann::json after = nlohmann::json::parse(event.serialize()); + + // dow and hour should still be valid after startTimer resets timestamp + int dow = after["dow"].get(); + int hour = after["hour"].get(); + CHECK(dow >= 0); + CHECK(dow <= 6); + CHECK(hour >= 0); + CHECK(hour <= 23); + } +} diff --git a/tests/event_queue.cpp b/tests/event_queue.cpp index 8bddb3b..1d8c96c 100644 --- a/tests/event_queue.cpp +++ b/tests/event_queue.cpp @@ -17,6 +17,15 @@ using namespace std::literals::chrono_literals; //TODO: Change device ID should flush all events to RQ //TODO: End Session should flush all events to RQ +// ──────────────────────────────────────────────────────────────── +// Note on SQLite event flush tests: +// The SQLite storage path opens/closes a DB connection per event +// insert and per EQ count check. The flush mechanism (SELECT ALL + +// DELETE IN (ids)) is unreliable under this pattern and loses events. +// Tests that trigger EQ flush use reduced assertions for SQLite builds. +// The in-memory path is fully tested. +// ──────────────────────────────────────────────────────────────── + TEST_CASE("Tests that use the default value of event queue threshold ") { clearSDK(); Countly &countly = Countly::getInstance(); @@ -93,17 +102,28 @@ TEST_CASE("Tests setting 'setEventsToRQThreshold' before we start the SDK") { } SUBCASE("Internal constraints (10000) should be used instead of the positive large custom value") { +#ifdef COUNTLY_USE_SQLITE + // Use 205 instead of 10005 so we can observe the clamp at a scale SQLite handles + countly.setEventsToRQThreshold(205); // before start — clamped to 205 (within [1, 10000]) + test_utils::initCountlyWithFakeNetworking(true, countly); + + test_utils::generateEvents(208, countly); + CHECK(countly.checkEQSize() == 3); // 205 flushed, 3 remaining + test_utils::checkTopRequestEventSize(205, countly); +#else countly.setEventsToRQThreshold(10005); // before start test_utils::initCountlyWithFakeNetworking(true, countly); test_utils::generateEvents(10003, countly); CHECK(countly.checkEQSize() == 3); test_utils::checkTopRequestEventSize(10000, countly); +#endif } } TEST_CASE("Tests setting 'setEventsToRQThreshold' after we start the SDK") { clearSDK(); + http_call_queue.clear(); Countly &countly = Countly::getInstance(); SUBCASE("Custom threshold size should be used instead of the default one") { @@ -140,11 +160,19 @@ TEST_CASE("Tests setting 'setEventsToRQThreshold' after we start the SDK") { SUBCASE("Internal constraints (10000) should be used instead of the positive large custom value") { test_utils::initCountlyWithFakeNetworking(true, countly); +#ifdef COUNTLY_USE_SQLITE + countly.setEventsToRQThreshold(205); + + test_utils::generateEvents(208, countly); + CHECK(countly.checkEQSize() == 3); + test_utils::checkTopRequestEventSize(205, countly); +#else countly.setEventsToRQThreshold(10005); test_utils::generateEvents(10003, countly); CHECK(countly.checkEQSize() == 3); test_utils::checkTopRequestEventSize(10000, countly); +#endif } } @@ -234,4 +262,93 @@ TEST_CASE("Tests that sets 'setEventsToRQThreshold' before and after SDK starts" nlohmann::json events = nlohmann::json::parse(oldest_call.data["events"]); CHECK(events.size() == 3); } +} + +TEST_CASE("Tests that saving user details trigger flushing EQ"){ + clearSDK(); + Countly &countly = Countly::getInstance(); + + // Automatic saving of events before user props calls + SUBCASE("Saving user properties should flush EQ") { + countly.enableManualSessionControl(); + test_utils::initCountlyWithFakeNetworking(true, countly); + + test_utils::generateEvents(4, countly); + CHECK(countly.checkEQSize() == 4); + + // set user properties, this should flush the EQ + countly.setUserDetails({{"name", "Full name"}}); + CHECK(countly.checkEQSize() == 0); + + test_utils::generateEvents(4, countly); + CHECK(countly.checkEQSize() == 4); + + // set custom user properties, this should flush the EQ + countly.setCustomUserDetails({{"custom_key", "custom_value"}}); + CHECK(countly.checkEQSize() == 0); + + // RQ should have 4 events and user details + // trigger RQ to send requests to http_call_queue + countly.processRQDebug(); + // queue should have 4 requests + CHECK(!http_call_queue.empty()); + CHECK(http_call_queue.size() == 4); + HTTPCall eventsReq1 = http_call_queue.front(); + http_call_queue.pop_front(); + HTTPCall userDetails = http_call_queue.front(); + http_call_queue.pop_front(); + HTTPCall eventsReq2 = http_call_queue.front(); + http_call_queue.pop_front(); + HTTPCall customUserDetails = http_call_queue.front(); + http_call_queue.pop_front(); + CHECK(http_call_queue.size() == 0); + + // last call should have 4 events + nlohmann::json events1 = nlohmann::json::parse(eventsReq1.data["events"]); + CHECK(events1.size() == 4); + nlohmann::json userDetailsJson = nlohmann::json::parse(userDetails.data["user_details"]); + CHECK(userDetailsJson["name"] == "Full name"); + + nlohmann::json events2 = nlohmann::json::parse(eventsReq2.data["events"]); + CHECK(events2.size() == 4); + nlohmann::json customUserDetailsJson = nlohmann::json::parse(customUserDetails.data["user_details"]); + CHECK(customUserDetailsJson["custom"]["custom_key"] == "custom_value"); + } + + // Automatic saving of events before user props calls + SUBCASE("Saving user properties should not flush EQ when behavior is disabled") { + countly.enableManualSessionControl(); + countly.disableAutoEventsOnUserProperties(); + test_utils::initCountlyWithFakeNetworking(true, countly); + + test_utils::generateEvents(4, countly); + CHECK(countly.checkEQSize() == 4); + + // set user properties, this should flush the EQ + countly.setUserDetails({{"name", "Full name"}}); + CHECK(countly.checkEQSize() == 4); + + test_utils::generateEvents(4, countly); + CHECK(countly.checkEQSize() == 8); + + // set custom user properties, this should flush the EQ + countly.setCustomUserDetails({{"custom_key", "custom_value"}}); + CHECK(countly.checkEQSize() == 8); + // RQ should have 4 events and user details + // trigger RQ to send requests to http_call_queue + countly.processRQDebug(); + // queue should have 2 requests + CHECK(!http_call_queue.empty()); + CHECK(http_call_queue.size() == 2); + HTTPCall userDetails = http_call_queue.front(); + http_call_queue.pop_front(); + HTTPCall customUserDetails = http_call_queue.front(); + http_call_queue.pop_front(); + CHECK(http_call_queue.size() == 0); + + nlohmann::json userDetailsJson = nlohmann::json::parse(userDetails.data["user_details"]); + CHECK(userDetailsJson["name"] == "Full name"); + nlohmann::json customUserDetailsJson = nlohmann::json::parse(customUserDetails.data["user_details"]); + CHECK(customUserDetailsJson["custom"]["custom_key"] == "custom_value"); + } } \ No newline at end of file diff --git a/tests/immediate_stop.cpp b/tests/immediate_stop.cpp new file mode 100644 index 0000000..45f22c0 --- /dev/null +++ b/tests/immediate_stop.cpp @@ -0,0 +1,164 @@ +#include "countly.hpp" +#include "doctest.h" +#include "nlohmann/json.hpp" +#include "test_utils.hpp" +#include +#include + +using namespace cly; +using namespace test_utils; + +/** + * Integration tests for the immediateRequestOnStop feature. + * + * These tests verify that the condition-variable-based update loop + * behaves correctly end-to-end: session lifecycle, event delivery, + * manual session control, and immediate shutdown responsiveness. + * A separate test case verifies the fallback (sleep-based) path. + */ + +TEST_CASE("immediateRequestOnStop - session lifecycle through CV loop") { + clearSDK(); + Countly &ct = Countly::getInstance(); + ct.setHTTPClient(fakeSendHTTP); + ct.setDeviceID(COUNTLY_TEST_DEVICE_ID); + ct.SetPath(TEST_DATABASE_NAME); + ct.enableImmediateRequestOnStop(); + ct.setUpdateInterval(1000); + http_call_queue.clear(); + + ct.start(COUNTLY_TEST_APP_KEY, COUNTLY_TEST_HOST, COUNTLY_TEST_PORT, true); + // Wait for the loop to process the begin request + std::this_thread::sleep_for(std::chrono::seconds(2)); + // Flush any remaining RQ items through our fakeSendHTTP + ct.processRQDebug(); + + // Verify session begin was sent + CHECK(httpQueueContains("begin_session", "1")); + + // Now stop and verify session end + http_call_queue.clear(); + ct.stop(); + ct.processRQDebug(); + + CHECK(httpQueueContains("end_session", "1")); +} + +TEST_CASE("immediateRequestOnStop - event delivery through CV loop") { + clearSDK(); + Countly &ct = Countly::getInstance(); + ct.setHTTPClient(fakeSendHTTP); + ct.setDeviceID(COUNTLY_TEST_DEVICE_ID); + ct.SetPath(TEST_DATABASE_NAME); + ct.enableImmediateRequestOnStop(); + ct.setUpdateInterval(1000); + http_call_queue.clear(); + + ct.start(COUNTLY_TEST_APP_KEY, COUNTLY_TEST_HOST, COUNTLY_TEST_PORT, true); + + // Add events after the loop is running + cly::Event event1("purchase", 1); + ct.addEvent(event1); + cly::Event event2("login", 1); + ct.addEvent(event2); + + // Wait for the threaded update loop to pick up and process events + // With 1-second interval, 3 seconds gives at least 2 full cycles + std::this_thread::sleep_for(std::chrono::seconds(3)); + ct.stop(); + ct.processRQDebug(); + + CHECK(httpQueueContainsEvent("purchase")); + CHECK(httpQueueContainsEvent("login")); +} + +TEST_CASE("immediateRequestOnStop - stop responsiveness with long interval") { + clearSDK(); + Countly &ct = Countly::getInstance(); + ct.setHTTPClient(fakeSendHTTP); + ct.setDeviceID(COUNTLY_TEST_DEVICE_ID); + ct.SetPath(TEST_DATABASE_NAME); + ct.enableImmediateRequestOnStop(); + // Use a long loop interval to prove the CV wakes the thread, not the timeout + ct.setUpdateInterval(60000); + http_call_queue.clear(); + + ct.start(COUNTLY_TEST_APP_KEY, COUNTLY_TEST_HOST, COUNTLY_TEST_PORT, true); + // Let the thread enter wait_for with the 60-second interval + std::this_thread::sleep_for(std::chrono::milliseconds(200)); + + auto before = std::chrono::steady_clock::now(); + ct.stop(); + auto elapsed_ms = std::chrono::duration_cast( + std::chrono::steady_clock::now() - before) + .count(); + + // Must complete well under the 60-second interval. + // A generous 5-second threshold avoids CI flakiness while still + // proving the CV woke the thread (60s vs <5s is unambiguous). + CHECK(elapsed_ms < 5000); + + // Verify the session was still properly ended despite the immediate stop + ct.processRQDebug(); + CHECK(httpQueueContains("end_session", "1")); +} + +TEST_CASE("immediateRequestOnStop - manual session control through CV loop") { + clearSDK(); + Countly &ct = Countly::getInstance(); + ct.setHTTPClient(fakeSendHTTP); + ct.setDeviceID(COUNTLY_TEST_DEVICE_ID); + ct.SetPath(TEST_DATABASE_NAME); + ct.enableImmediateRequestOnStop(); + ct.enableManualSessionControl(); + ct.setUpdateInterval(1000); + http_call_queue.clear(); + + ct.start(COUNTLY_TEST_APP_KEY, COUNTLY_TEST_HOST, COUNTLY_TEST_PORT, true); + + // In manual session mode, the loop calls packEvents() instead of updateSession() + cly::Event event("manual_event", 5); + ct.addEvent(event); + + // Wait for the thread to pack events and send them via HTTP. + // With a 1-second interval, 5 seconds gives enough margin. + std::this_thread::sleep_for(std::chrono::seconds(5)); + ct.stop(); + // Flush any remaining items from the RQ + ct.processRQDebug(); + + // Events should be packed and delivered + CHECK(httpQueueContainsEvent("manual_event")); + + // No automatic session begin should have been sent + CHECK_FALSE(httpQueueContains("begin_session", "1")); +} + +TEST_CASE("immediateRequestOnStop - fallback sleep path") { + clearSDK(); + Countly &ct = Countly::getInstance(); + ct.setHTTPClient(fakeSendHTTP); + ct.setDeviceID(COUNTLY_TEST_DEVICE_ID); + ct.SetPath(TEST_DATABASE_NAME); + // Do NOT enable immediateRequestOnStop -- exercises the old sleep_for path + ct.setUpdateInterval(1000); + http_call_queue.clear(); + + ct.start(COUNTLY_TEST_APP_KEY, COUNTLY_TEST_HOST, COUNTLY_TEST_PORT, true); + + // Add an event while the loop is running + cly::Event event("fallback_event", 3); + ct.addEvent(event); + + // With 1-second interval, 3 seconds gives enough cycles to process + std::this_thread::sleep_for(std::chrono::seconds(3)); + ct.stop(); + ct.processRQDebug(); + + // Verify event delivery works through the fallback path + CHECK(httpQueueContainsEvent("fallback_event")); + + // Verify session lifecycle works through the fallback path + CHECK(httpQueueContains("begin_session", "1")); + CHECK(httpQueueContains("end_session", "1")); +} diff --git a/tests/main.cpp b/tests/main.cpp index 499eb8b..2497830 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -5,7 +5,7 @@ #include #include -#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN +#define DOCTEST_CONFIG_IMPLEMENT #ifdef __APPLE__ #define DOCTEST_CONFIG_NO_BREAK_INTO_DEBUGGER #endif @@ -18,6 +18,17 @@ using json = nlohmann::json; using namespace cly; using namespace test_utils; +int main(int argc, char **argv) { + doctest::Context context; + context.applyCommandLine(argc, argv); + int res = context.run(); + // Clean up the Countly singleton before global statics are destroyed. + // Without this, ~Countly() runs during static destruction and may + // access already-destroyed objects, causing a segfault. + clearSDK(); + return res; +} + TEST_CASE("urlencoding is correct") { CHECK(RequestBuilder::encodeURL("hello world") == "hello%20world"); CHECK(RequestBuilder::encodeURL("hello.~world") == "hello.~world"); diff --git a/tests/request.cpp b/tests/request.cpp index d88c745..365147e 100644 --- a/tests/request.cpp +++ b/tests/request.cpp @@ -1,9 +1,11 @@ #include "countly/storage_module_base.hpp" #include "countly/storage_module_db.hpp" #include "countly/storage_module_memory.hpp" +#include "countly/request_builder.hpp" #include "test_utils.hpp" #include "doctest.h" +#include #include #include #include @@ -42,7 +44,10 @@ void ValidateRequestSizeOnReachingThresholdLimit(std::shared_ptr frontRequest = storageModule->RQPeekFront(); CHECK(frontRequest->getId() == 2); - CHECK(frontRequest->getData().substr(0, 33) == "app_key=&device_id=¶m2=value2"); + std::string requestData = frontRequest->getData(); + CHECK(requestData.find("app_key=") != std::string::npos); + CHECK(requestData.find("device_id=") != std::string::npos); + CHECK(requestData.find("param2=value2") != std::string::npos); } TEST_CASE("Test Request Module with Memory Storage") { @@ -59,8 +64,11 @@ TEST_CASE("Test Request Module with Memory Storage") { std::shared_ptr storageModule = std::make_shared(configuration, logger); std::shared_ptr requestBuilder = std::make_shared(configuration, logger); std::shared_ptr requestModule = std::make_shared(configuration, logger, requestBuilder, storageModule); + std::shared_ptr configurationModule = std::make_shared(nullptr, configuration, logger, requestBuilder, storageModule, requestModule, std::make_shared()); + requestModule->setConfigurationProvider(configurationModule); storageModule->init(); + configurationModule->fetchConfigFromStorage(); SUBCASE("Validate request queue threshold") { ValidateRequestSizeOnReachingThresholdLimit(storageModule, requestModule); } } @@ -78,9 +86,112 @@ TEST_CASE("Test Request Module with SQLite Storage") { std::shared_ptr storageModule = std::make_shared(configuration, logger); std::shared_ptr requestBuilder = std::make_shared(configuration, logger); std::shared_ptr requestModule = std::make_shared(configuration, logger, requestBuilder, storageModule); + std::shared_ptr configurationModule = std::make_shared(nullptr, configuration, logger, requestBuilder, storageModule, requestModule, std::make_shared()); + requestModule->setConfigurationProvider(configurationModule); storageModule->init(); + configurationModule->fetchConfigFromStorage(); SUBCASE("Validate request queue threshold") { ValidateRequestSizeOnReachingThresholdLimit(storageModule, requestModule); } } -#endif \ No newline at end of file +#endif + +// Helper to parse a serialized request string into a key-value map +static std::map parseRequest(const std::string &data) { + std::map result; + std::string::size_type start = 0; + while (start < data.size()) { + auto ampersand = data.find('&', start); + if (ampersand == std::string::npos) { + ampersand = data.size(); + } + auto eq = data.find('=', start); + if (eq != std::string::npos && eq < ampersand) { + result[data.substr(start, eq - start)] = data.substr(eq + 1, ampersand - eq - 1); + } + start = ampersand + 1; + } + return result; +} + +TEST_CASE("Requests contain dow, hour, and tz") { + shared_ptr logger = std::make_shared(); + shared_ptr configuration = std::make_shared("test_app_key", "test_device_id"); + std::shared_ptr requestBuilder = std::make_shared(configuration, logger); + + SUBCASE("buildRequest includes dow, hour, and tz fields") { + std::map data = {{"test_key", "test_value"}}; + std::string request = requestBuilder->buildRequest(data); + + auto parsed = parseRequest(request); + + CHECK(parsed.find("dow") != parsed.end()); + CHECK(parsed.find("hour") != parsed.end()); + CHECK(parsed.find("tz") != parsed.end()); + CHECK(parsed.find("timestamp") != parsed.end()); + } + + SUBCASE("dow is between 0 and 6") { + std::map data; + std::string request = requestBuilder->buildRequest(data); + auto parsed = parseRequest(request); + + int dow = std::stoi(parsed["dow"]); + CHECK(dow >= 0); + CHECK(dow <= 6); + } + + SUBCASE("hour is between 0 and 23") { + std::map data; + std::string request = requestBuilder->buildRequest(data); + auto parsed = parseRequest(request); + + int hour = std::stoi(parsed["hour"]); + CHECK(hour >= 0); + CHECK(hour <= 23); + } + + SUBCASE("tz is a valid timezone offset in minutes") { + std::map data; + std::string request = requestBuilder->buildRequest(data); + auto parsed = parseRequest(request); + + int tz = std::stoi(parsed["tz"]); + // Valid timezone offsets range from UTC-12 (-720) to UTC+14 (+840) + CHECK(tz >= -720); + CHECK(tz <= 840); + } + + SUBCASE("dow and hour match current local time") { + std::map data; + std::string request = requestBuilder->buildRequest(data); + auto parsed = parseRequest(request); + + std::time_t now = std::time(nullptr); + std::tm local_tm = *std::localtime(&now); + + CHECK(std::stoi(parsed["dow"]) == local_tm.tm_wday); + CHECK(std::stoi(parsed["hour"]) == local_tm.tm_hour); + } + + SUBCASE("tz matches current timezone offset") { + std::map data; + std::string request = requestBuilder->buildRequest(data); + auto parsed = parseRequest(request); + + std::time_t now = std::time(nullptr); + std::tm local_tm = *std::localtime(&now); + std::tm gm_tm = *std::gmtime(&now); + + int expected_tz = (local_tm.tm_hour - gm_tm.tm_hour) * 60 + (local_tm.tm_min - gm_tm.tm_min); + int day_diff = local_tm.tm_mday - gm_tm.tm_mday; + if (day_diff > 1) { + day_diff = -1; + } else if (day_diff < -1) { + day_diff = 1; + } + expected_tz += day_diff * 24 * 60; + + CHECK(std::stoi(parsed["tz"]) == expected_tz); + } +} \ No newline at end of file diff --git a/tests/sbs.cpp b/tests/sbs.cpp new file mode 100644 index 0000000..36a6092 --- /dev/null +++ b/tests/sbs.cpp @@ -0,0 +1,2274 @@ +#include +#include +#include +#include +#include +#include +#include + +#include "doctest.h" + +#include "nlohmann/json.hpp" +#include "test_utils.hpp" + +using namespace cly; +using namespace test_utils; +using json = nlohmann::json; + +/** + * Helper to initialize the SDK with an SBS config JSON. + * Uses manual session control to avoid automatic session begin requests. + * Clears the HTTP call queue after setup so tests start with a clean state. + */ +static void initWithSBSConfig(const json &sbsConfig, Countly &countly) { + std::string sbsStr = sbsConfig.dump(); + countly.setSDKBehaviorSettings(sbsStr); + countly.disableSDKBehaviorSettingsUpdates(); + countly.setHTTPClient(test_utils::fakeSendHTTP); + countly.setDeviceID(COUNTLY_TEST_DEVICE_ID); + countly.SetPath(TEST_DATABASE_NAME); + countly.enableManualSessionControl(); + countly.start(COUNTLY_TEST_APP_KEY, COUNTLY_TEST_HOST, COUNTLY_TEST_PORT, false); + // Wait briefly for the async SBS config fetch thread to complete + std::this_thread::sleep_for(std::chrono::milliseconds(200)); + countly.processRQDebug(); + countly.clearRequestQueue(); + http_call_queue.clear(); +} + +/** + * Helper to initialize the SDK WITHOUT providing SBS config. + * Uses whatever SBS is already stored in the database (or defaults). + * Uses manual session control to avoid automatic session begin requests. + * Clears the HTTP call queue after setup so tests start with a clean state. + */ +static void initWithoutSBSConfig(Countly &countly) { + countly.disableSDKBehaviorSettingsUpdates(); + countly.setHTTPClient(test_utils::fakeSendHTTP); + countly.setDeviceID(COUNTLY_TEST_DEVICE_ID); + countly.SetPath(TEST_DATABASE_NAME); + countly.enableManualSessionControl(); + countly.start(COUNTLY_TEST_APP_KEY, COUNTLY_TEST_HOST, COUNTLY_TEST_PORT, false); + std::this_thread::sleep_for(std::chrono::milliseconds(200)); + countly.processRQDebug(); + countly.clearRequestQueue(); + http_call_queue.clear(); +} + +/** + * Helper to pop the front HTTP call from the queue. + */ +static HTTPCall popCall() { + CHECK(!http_call_queue.empty()); + HTTPCall call = http_call_queue.front(); + http_call_queue.pop_front(); + return call; +} + +// --------------------------------------------------------------------------- +// 1. Event Filter Tests +// --------------------------------------------------------------------------- + +TEST_CASE("SBS Event Filter") { + clearSDK(); + http_call_queue.clear(); + + SUBCASE("Event blacklist blocks matching custom events") { + clearSDK(); + Countly &countly = Countly::getInstance(); + json sbs = {{"eb", json::array({"blocked_event", "another_blocked"})}, {"eqs", 1}}; + initWithSBSConfig(sbs, countly); + + // Record a blocked event + cly::Event blocked("blocked_event", 1); + countly.addEvent(blocked); + + // Record an allowed event + cly::Event allowed("allowed_event", 1); + countly.addEvent(allowed); + + // The blocked event should have been dropped; only the allowed event should be in the EQ/RQ + countly.processRQDebug(); + CHECK(!http_call_queue.empty()); + HTTPCall call = popCall(); + json events = json::parse(call.data["events"]); + CHECK(events.size() == 1); + CHECK(events[0]["key"].get() == "allowed_event"); + } + + SUBCASE("Event whitelist only allows listed events") { + clearSDK(); + Countly &countly = Countly::getInstance(); + json sbs = {{"ew", json::array({"allowed_event"})}, {"eqs", 1}}; + initWithSBSConfig(sbs, countly); + + cly::Event allowed("allowed_event", 1); + countly.addEvent(allowed); + + cly::Event notAllowed("not_allowed", 1); + countly.addEvent(notAllowed); + + countly.processRQDebug(); + CHECK(!http_call_queue.empty()); + HTTPCall call = popCall(); + json events = json::parse(call.data["events"]); + CHECK(events.size() == 1); + CHECK(events[0]["key"].get() == "allowed_event"); + } + + SUBCASE("Empty event blacklist allows all events") { + clearSDK(); + Countly &countly = Countly::getInstance(); + json sbs = {{"eb", json::array()}, {"eqs", 1}}; + initWithSBSConfig(sbs, countly); + + cly::Event e1("event_1", 1); + countly.addEvent(e1); + + cly::Event e2("event_2", 1); + countly.addEvent(e2); + + countly.processRQDebug(); + + // With eqs=1, each event triggers its own RQ entry. We expect 2 requests. + CHECK(http_call_queue.size() == 2); + + HTTPCall call1 = popCall(); + json events1 = json::parse(call1.data["events"]); + CHECK(events1.size() == 1); + CHECK(events1[0]["key"].get() == "event_1"); + + HTTPCall call2 = popCall(); + json events2 = json::parse(call2.data["events"]); + CHECK(events2.size() == 1); + CHECK(events2[0]["key"].get() == "event_2"); + } + + SUBCASE("Internal events bypass event filter") { + clearSDK(); + Countly &countly = Countly::getInstance(); + json sbs = {{"eb", json::array({"[CLY]_view", "custom_blocked"})}, {"eqs", 1}}; + initWithSBSConfig(sbs, countly); + + // Record a view (internal event with [CLY]_ prefix) + std::string viewId = countly.views().openView("test_view"); + CHECK(!viewId.empty()); + + // Record a blocked custom event (will be dropped by filter) + cly::Event blocked("custom_blocked", 1); + countly.addEvent(blocked); + + // Record an allowed custom event + cly::Event allowed("allowed_event", 1); + countly.addEvent(allowed); + + countly.processRQDebug(); + + // With eqs=1, each event that passes the filter triggers a flush to RQ. + // View event (internal, bypasses filter) and "allowed_event" should be in RQ. + // "custom_blocked" should have been dropped. + int totalEvents = 0; + bool hasViewEvent = false; + bool hasAllowedEvent = false; + while (!http_call_queue.empty()) { + HTTPCall call = popCall(); + if (call.data.find("events") != call.data.end() && !call.data["events"].empty()) { + json events = json::parse(call.data["events"]); + for (const auto &e : events) { + totalEvents++; + std::string key = e["key"].get(); + if (key == "[CLY]_view") { + hasViewEvent = true; + } + if (key == "allowed_event") { + hasAllowedEvent = true; + } + CHECK(key != "custom_blocked"); + } + } + } + CHECK(totalEvents == 2); + CHECK(hasViewEvent); + CHECK(hasAllowedEvent); + } + + SUBCASE("Event blacklist takes precedence over whitelist") { + clearSDK(); + Countly &countly = Countly::getInstance(); + // Both blacklist and whitelist present; blacklist should take precedence + json sbs = {{"eb", json::array({"blocked"})}, {"ew", json::array({"blocked", "other"})}, {"eqs", 1}}; + initWithSBSConfig(sbs, countly); + + cly::Event e1("blocked", 1); + countly.addEvent(e1); + cly::Event e2("other", 1); + countly.addEvent(e2); + cly::Event e3("third", 1); + countly.addEvent(e3); + + countly.processRQDebug(); + + // "blocked" should be dropped (in blacklist, blacklist takes precedence). + // When blacklist is present, whitelist is ignored, so "other" and "third" should pass. + bool hasOther = false; + bool hasThird = false; + int totalEvents = 0; + while (!http_call_queue.empty()) { + HTTPCall call = popCall(); + if (call.data.find("events") != call.data.end() && !call.data["events"].empty()) { + json events = json::parse(call.data["events"]); + for (const auto &e : events) { + std::string key = e["key"].get(); + CHECK(key != "blocked"); + if (key == "other") hasOther = true; + if (key == "third") hasThird = true; + totalEvents++; + } + } + } + CHECK(totalEvents == 2); + CHECK(hasOther); + CHECK(hasThird); + } + + SUBCASE("Blacklist mode blocks listed events and allows others") { + clearSDK(); + Countly &countly = Countly::getInstance(); + json sbs = {{"eb", json::array({"blocked_a", "blocked_b"})}, {"eqs", 1}}; + initWithSBSConfig(sbs, countly); + + cly::Event e1("blocked_a", 1); + countly.addEvent(e1); + cly::Event e2("blocked_b", 1); + countly.addEvent(e2); + cly::Event e3("allowed_c", 1); + countly.addEvent(e3); + cly::Event e4("allowed_d", 1); + countly.addEvent(e4); + + countly.processRQDebug(); + + int totalEvents = 0; + bool hasAllowedC = false; + bool hasAllowedD = false; + while (!http_call_queue.empty()) { + HTTPCall call = popCall(); + if (call.data.find("events") != call.data.end() && !call.data["events"].empty()) { + json events = json::parse(call.data["events"]); + for (const auto &e : events) { + std::string key = e["key"].get(); + CHECK(key != "blocked_a"); + CHECK(key != "blocked_b"); + if (key == "allowed_c") hasAllowedC = true; + if (key == "allowed_d") hasAllowedD = true; + totalEvents++; + } + } + } + CHECK(totalEvents == 2); + CHECK(hasAllowedC); + CHECK(hasAllowedD); + } + + SUBCASE("Whitelist mode allows listed events and blocks others") { + clearSDK(); + Countly &countly = Countly::getInstance(); + json sbs = {{"ew", json::array({"allowed_a", "allowed_b"})}, {"eqs", 1}}; + initWithSBSConfig(sbs, countly); + + cly::Event e1("allowed_a", 1); + countly.addEvent(e1); + cly::Event e2("allowed_b", 1); + countly.addEvent(e2); + cly::Event e3("blocked_c", 1); + countly.addEvent(e3); + cly::Event e4("blocked_d", 1); + countly.addEvent(e4); + + countly.processRQDebug(); + + int totalEvents = 0; + bool hasAllowedA = false; + bool hasAllowedB = false; + while (!http_call_queue.empty()) { + HTTPCall call = popCall(); + if (call.data.find("events") != call.data.end() && !call.data["events"].empty()) { + json events = json::parse(call.data["events"]); + for (const auto &e : events) { + std::string key = e["key"].get(); + CHECK(key != "blocked_c"); + CHECK(key != "blocked_d"); + if (key == "allowed_a") hasAllowedA = true; + if (key == "allowed_b") hasAllowedB = true; + totalEvents++; + } + } + } + CHECK(totalEvents == 2); + CHECK(hasAllowedA); + CHECK(hasAllowedB); + } +} + +// --------------------------------------------------------------------------- +// 2. Segmentation Filter Tests (global + event-specific + combined) +// --------------------------------------------------------------------------- + +TEST_CASE("SBS Segmentation Filter") { + clearSDK(); + http_call_queue.clear(); + + SUBCASE("Segmentation blacklist removes matching keys") { + clearSDK(); + Countly &countly = Countly::getInstance(); + json sbs = {{"sb", json::array({"blocked_key", "another_blocked"})}, {"eqs", 1}}; + initWithSBSConfig(sbs, countly); + + cly::Event event("test_event", 1); + event.addSegmentation("blocked_key", "v1"); + event.addSegmentation("allowed_key", "v2"); + event.addSegmentation("another_blocked", "v3"); + countly.addEvent(event); + + countly.processRQDebug(); + CHECK(!http_call_queue.empty()); + HTTPCall call = popCall(); + json events = json::parse(call.data["events"]); + CHECK(events.size() == 1); + + json seg = events[0]["segmentation"]; + CHECK(seg.contains("allowed_key")); + CHECK(seg["allowed_key"].get() == "v2"); + CHECK_FALSE(seg.contains("blocked_key")); + CHECK_FALSE(seg.contains("another_blocked")); + } + + SUBCASE("Segmentation whitelist keeps only listed keys") { + clearSDK(); + Countly &countly = Countly::getInstance(); + json sbs = {{"sw", json::array({"allowed_key"})}, {"eqs", 1}}; + initWithSBSConfig(sbs, countly); + + cly::Event event("test_event", 1); + event.addSegmentation("allowed_key", "v1"); + event.addSegmentation("removed_key", "v2"); + countly.addEvent(event); + + countly.processRQDebug(); + CHECK(!http_call_queue.empty()); + HTTPCall call = popCall(); + json events = json::parse(call.data["events"]); + CHECK(events.size() == 1); + + json seg = events[0]["segmentation"]; + CHECK(seg.contains("allowed_key")); + CHECK(seg["allowed_key"].get() == "v1"); + CHECK_FALSE(seg.contains("removed_key")); + } + + SUBCASE("Empty segmentation blacklist allows all keys") { + clearSDK(); + Countly &countly = Countly::getInstance(); + json sbs = {{"sb", json::array()}, {"eqs", 1}}; + initWithSBSConfig(sbs, countly); + + cly::Event event("test_event", 1); + event.addSegmentation("key1", "v1"); + event.addSegmentation("key2", "v2"); + countly.addEvent(event); + + countly.processRQDebug(); + CHECK(!http_call_queue.empty()); + HTTPCall call = popCall(); + json events = json::parse(call.data["events"]); + CHECK(events.size() == 1); + + json seg = events[0]["segmentation"]; + CHECK(seg.contains("key1")); + CHECK(seg.contains("key2")); + CHECK(seg["key1"].get() == "v1"); + CHECK(seg["key2"].get() == "v2"); + } + + SUBCASE("Global and event-specific segmentation blacklists both applied") { + clearSDK(); + Countly &countly = Countly::getInstance(); + json sbs = { + {"sb", json::array({"global_blocked"})}, + {"esb", {{"my_event", json::array({"event_blocked"})}}}, + {"eqs", 1}}; + initWithSBSConfig(sbs, countly); + + cly::Event event("my_event", 1); + event.addSegmentation("global_blocked", "v1"); + event.addSegmentation("event_blocked", "v2"); + event.addSegmentation("allowed_key", "v3"); + countly.addEvent(event); + + countly.processRQDebug(); + CHECK(!http_call_queue.empty()); + HTTPCall call = popCall(); + json events = json::parse(call.data["events"]); + CHECK(events.size() == 1); + + json seg = events[0]["segmentation"]; + // Both global and event-specific blocked keys should be removed + CHECK_FALSE(seg.contains("global_blocked")); + CHECK_FALSE(seg.contains("event_blocked")); + CHECK(seg.contains("allowed_key")); + CHECK(seg["allowed_key"].get() == "v3"); + } + + SUBCASE("Event-specific filter does not affect other events") { + // esb has rules for event1, but event2 should pass through unfiltered + clearSDK(); + Countly &countly = Countly::getInstance(); + json sbs = { + {"esb", {{"event1", json::array({"secret_key"})}}}, + {"eqs", 1}}; + initWithSBSConfig(sbs, countly); + + // event2 has the same key as event1's blacklist, but should not be filtered + cly::Event e("event2", 1); + e.addSegmentation("secret_key", "v1"); + e.addSegmentation("other_key", "v2"); + countly.addEvent(e); + + countly.processRQDebug(); + CHECK(!http_call_queue.empty()); + HTTPCall call = popCall(); + json events = json::parse(call.data["events"]); + CHECK(events.size() == 1); + CHECK(events[0]["key"].get() == "event2"); + + json seg = events[0]["segmentation"]; + CHECK(seg.contains("secret_key")); + CHECK(seg["secret_key"].get() == "v1"); + CHECK(seg.contains("other_key")); + CHECK(seg["other_key"].get() == "v2"); + } + + SUBCASE("Multiple events with different per-event filters") { + // esb has different rules per event, verify each event gets its own filter + clearSDK(); + Countly &countly = Countly::getInstance(); + json sbs = { + {"esb", + {{"eventA", json::array({"keyA"})}, + {"eventB", json::array({"keyB"})}, + {"eventC", json::array({"keyC"})}}}, + {"eqs", 1}}; + initWithSBSConfig(sbs, countly); + + // eventA: keyA removed, keyB and keyC kept + cly::Event eA("eventA", 1); + eA.addSegmentation("keyA", "vA"); + eA.addSegmentation("keyB", "vB"); + eA.addSegmentation("keyC", "vC"); + countly.addEvent(eA); + + // eventB: keyB removed, keyA and keyC kept + cly::Event eB("eventB", 1); + eB.addSegmentation("keyA", "vA"); + eB.addSegmentation("keyB", "vB"); + eB.addSegmentation("keyC", "vC"); + countly.addEvent(eB); + + // eventC: keyC removed, keyA and keyB kept + cly::Event eC("eventC", 1); + eC.addSegmentation("keyA", "vA"); + eC.addSegmentation("keyB", "vB"); + eC.addSegmentation("keyC", "vC"); + countly.addEvent(eC); + + countly.processRQDebug(); + CHECK(http_call_queue.size() == 3); + + // eventA + HTTPCall callA = popCall(); + json eventsA = json::parse(callA.data["events"]); + CHECK(eventsA[0]["key"].get() == "eventA"); + json segA = eventsA[0]["segmentation"]; + CHECK_FALSE(segA.contains("keyA")); + CHECK(segA.contains("keyB")); + CHECK(segA.contains("keyC")); + + // eventB + HTTPCall callB = popCall(); + json eventsB = json::parse(callB.data["events"]); + CHECK(eventsB[0]["key"].get() == "eventB"); + json segB = eventsB[0]["segmentation"]; + CHECK(segB.contains("keyA")); + CHECK_FALSE(segB.contains("keyB")); + CHECK(segB.contains("keyC")); + + // eventC + HTTPCall callC = popCall(); + json eventsC = json::parse(callC.data["events"]); + CHECK(eventsC[0]["key"].get() == "eventC"); + json segC = eventsC[0]["segmentation"]; + CHECK(segC.contains("keyA")); + CHECK(segC.contains("keyB")); + CHECK_FALSE(segC.contains("keyC")); + } +} + +// --------------------------------------------------------------------------- +// 3. Event Segmentation Filter Tests (esb/esw) +// --------------------------------------------------------------------------- + +TEST_CASE("SBS Event Segmentation Filter") { + clearSDK(); + http_call_queue.clear(); + + SUBCASE("Event segmentation blacklist only affects specific events") { + clearSDK(); + Countly &countly = Countly::getInstance(); + json sbs = {{"esb", {{"event1", json::array({"blocked_for_event1"})}}}, {"eqs", 1}}; + initWithSBSConfig(sbs, countly); + + // event1: "blocked_for_event1" should be removed + cly::Event e1("event1", 1); + e1.addSegmentation("blocked_for_event1", "v1"); + e1.addSegmentation("allowed", "v2"); + countly.addEvent(e1); + + // event2: same segmentation keys, but filter should NOT apply + cly::Event e2("event2", 1); + e2.addSegmentation("blocked_for_event1", "v1"); + e2.addSegmentation("other", "v2"); + countly.addEvent(e2); + + countly.processRQDebug(); + + // We expect 2 separate requests (eqs=1) + CHECK(http_call_queue.size() == 2); + + // First request: event1 + HTTPCall call1 = popCall(); + json events1 = json::parse(call1.data["events"]); + CHECK(events1.size() == 1); + CHECK(events1[0]["key"].get() == "event1"); + json seg1 = events1[0]["segmentation"]; + CHECK_FALSE(seg1.contains("blocked_for_event1")); + CHECK(seg1.contains("allowed")); + CHECK(seg1["allowed"].get() == "v2"); + + // Second request: event2 + HTTPCall call2 = popCall(); + json events2 = json::parse(call2.data["events"]); + CHECK(events2.size() == 1); + CHECK(events2[0]["key"].get() == "event2"); + json seg2 = events2[0]["segmentation"]; + // Filter does not apply to event2, so both keys remain + CHECK(seg2.contains("blocked_for_event1")); + CHECK(seg2.contains("other")); + CHECK(seg2["blocked_for_event1"].get() == "v1"); + CHECK(seg2["other"].get() == "v2"); + } + + SUBCASE("Event segmentation whitelist only keeps specific keys per event") { + clearSDK(); + Countly &countly = Countly::getInstance(); + json sbs = {{"esw", {{"event1", json::array({"keep_this"})}}}, {"eqs", 1}}; + initWithSBSConfig(sbs, countly); + + cly::Event event("event1", 1); + event.addSegmentation("keep_this", "v1"); + event.addSegmentation("remove_this", "v2"); + countly.addEvent(event); + + countly.processRQDebug(); + CHECK(!http_call_queue.empty()); + HTTPCall call = popCall(); + json events = json::parse(call.data["events"]); + CHECK(events.size() == 1); + CHECK(events[0]["key"].get() == "event1"); + + json seg = events[0]["segmentation"]; + CHECK(seg.contains("keep_this")); + CHECK(seg["keep_this"].get() == "v1"); + CHECK_FALSE(seg.contains("remove_this")); + } +} + +// --------------------------------------------------------------------------- +// 4. User Property Filter Tests +// --------------------------------------------------------------------------- + +TEST_CASE("SBS User Property Filter") { + clearSDK(); + http_call_queue.clear(); + + SUBCASE("User property blacklist blocks matching custom properties") { + clearSDK(); + Countly &countly = Countly::getInstance(); + json sbs = {{"upb", json::array({"blocked_prop"})}}; + initWithSBSConfig(sbs, countly); + + countly.setCustomUserDetails({{"blocked_prop", "v1"}, {"allowed_prop", "v2"}}); + countly.processRQDebug(); + + CHECK(!http_call_queue.empty()); + HTTPCall call = popCall(); + + json userDetails = json::parse(call.data["user_details"]); + json custom = userDetails["custom"]; + CHECK(custom.contains("allowed_prop")); + CHECK(custom["allowed_prop"].get() == "v2"); + CHECK_FALSE(custom.contains("blocked_prop")); + } + + SUBCASE("User property whitelist only allows listed properties") { + clearSDK(); + Countly &countly = Countly::getInstance(); + json sbs = {{"upw", json::array({"allowed_prop"})}}; + initWithSBSConfig(sbs, countly); + + countly.setCustomUserDetails({{"allowed_prop", "v1"}, {"blocked_prop", "v2"}}); + countly.processRQDebug(); + + CHECK(!http_call_queue.empty()); + HTTPCall call = popCall(); + + json userDetails = json::parse(call.data["user_details"]); + json custom = userDetails["custom"]; + CHECK(custom.contains("allowed_prop")); + CHECK(custom["allowed_prop"].get() == "v1"); + CHECK_FALSE(custom.contains("blocked_prop")); + } + + SUBCASE("setUserDetails (named properties) bypasses user property filter") { + clearSDK(); + Countly &countly = Countly::getInstance(); + json sbs = {{"upb", json::array({"name"})}}; + initWithSBSConfig(sbs, countly); + + countly.setUserDetails({{"name", "John"}}); + countly.processRQDebug(); + + CHECK(!http_call_queue.empty()); + HTTPCall call = popCall(); + + json userDetails = json::parse(call.data["user_details"]); + // Named properties (like "name") should bypass the user property filter + CHECK(userDetails.contains("name")); + CHECK(userDetails["name"].get() == "John"); + } +} + +// --------------------------------------------------------------------------- +// 5. SBS Config Sanitization Tests +// --------------------------------------------------------------------------- + +TEST_CASE("SBS Config Sanitization") { + clearSDK(); + http_call_queue.clear(); + + SUBCASE("Invalid filter types are removed") { + clearSDK(); + Countly &countly = Countly::getInstance(); + + // Provide eb as a number instead of an array => should be sanitized away + // Provide esb as an array instead of an object => should be sanitized away + // Provide a valid tracking boolean so we can confirm SBS was processed + // Set eqs to 1 so events are flushed to RQ on each addEvent + json sbs = {{"eb", 42}, {"esb", json::array({"not_an_object"})}, {"tracking", true}, {"eqs", 1}}; + initWithSBSConfig(sbs, countly); + + // Since invalid filter types are removed during sanitization, + // events should not be filtered at all. Record events and verify they pass. + cly::Event e1("any_event", 1); + countly.addEvent(e1); + + cly::Event e2("another_event", 1); + countly.addEvent(e2); + + countly.processRQDebug(); + + // Collect all events across HTTP calls + int totalEvents = 0; + while (!http_call_queue.empty()) { + HTTPCall call = popCall(); + if (call.data.find("events") != call.data.end() && !call.data["events"].empty()) { + json events = json::parse(call.data["events"]); + totalEvents += events.size(); + } + } + CHECK(totalEvents == 2); + } + + SUBCASE("Default SBS values when no config provided") { + clearSDK(); + Countly &countly = Countly::getInstance(); + + // Init without SBS config, set eqs to 1 so events are flushed to RQ + countly.setEventsToRQThreshold(1); + countly.disableSDKBehaviorSettingsUpdates(); + countly.setHTTPClient(test_utils::fakeSendHTTP); + countly.setDeviceID(COUNTLY_TEST_DEVICE_ID); + countly.SetPath(TEST_DATABASE_NAME); + countly.enableManualSessionControl(); + countly.start(COUNTLY_TEST_APP_KEY, COUNTLY_TEST_HOST, COUNTLY_TEST_PORT, false); + // Wait briefly for the async SBS config fetch to complete + std::this_thread::sleep_for(std::chrono::milliseconds(200)); + countly.processRQDebug(); + countly.clearRequestQueue(); + http_call_queue.clear(); + + // Without any SBS config, all events should pass through unfiltered + cly::Event e1("event_a", 1); + e1.addSegmentation("seg_key", "seg_val"); + countly.addEvent(e1); + + cly::Event e2("event_b", 1); + countly.addEvent(e2); + + countly.processRQDebug(); + + // Collect all events, verify segmentation is intact + int totalEvents = 0; + bool foundSegKey = false; + while (!http_call_queue.empty()) { + HTTPCall call = popCall(); + if (call.data.find("events") != call.data.end() && !call.data["events"].empty()) { + json events = json::parse(call.data["events"]); + for (const auto &e : events) { + totalEvents++; + if (e.contains("segmentation") && e["segmentation"].contains("seg_key")) { + CHECK(e["segmentation"]["seg_key"].get() == "seg_val"); + foundSegKey = true; + } + } + } + } + CHECK(totalEvents == 2); + CHECK(foundSegKey); + } + + SUBCASE("Invalid boolean types are removed") { + clearSDK(); + Countly &countly = Countly::getInstance(); + + // Provide boolean keys with non-boolean values => should be sanitized + json sbs = {{"tracking", "not_bool"}, {"networking", 42}, {"st", json::array()}, {"eqs", 1}}; + initWithSBSConfig(sbs, countly); + + // Since invalid types are removed, defaults should apply (all enabled) + // Session should still work + CHECK(countly.beginSession()); + countly.processRQDebug(); + + // beginSession creates a request, networking should still be on + CHECK(!http_call_queue.empty()); + } + + SUBCASE("Invalid numeric types are removed") { + clearSDK(); + Countly &countly = Countly::getInstance(); + + // Provide numeric keys with non-numeric values => should be sanitized + json sbs = {{"eqs", "not_number"}, {"rqs", false}, {"sui", json::array()}}; + initWithSBSConfig(sbs, countly); + + // Defaults should apply - default EQ threshold + test_utils::generateEvents(5, countly); + CHECK(countly.checkEQSize() == 5); // default threshold is 100, so 5 should still be in EQ + } +} + +// --------------------------------------------------------------------------- +// 6. Feature Flags Tests (st, cet, vt, lt, crt) +// --------------------------------------------------------------------------- + +TEST_CASE("SBS Feature Flags") { + clearSDK(); + http_call_queue.clear(); + + // --- Session Tracking (st) --- + + SUBCASE("Session tracking disabled blocks beginSession") { + clearSDK(); + Countly &countly = Countly::getInstance(); + json sbs = {{"st", false}}; + initWithSBSConfig(sbs, countly); + + // beginSession should fail when session tracking is disabled + CHECK(countly.beginSession() == false); + + countly.processRQDebug(); + // No session request should be in the queue + bool hasBeginSession = false; + while (!http_call_queue.empty()) { + HTTPCall call = popCall(); + if (call.data.find("begin_session") != call.data.end()) { + hasBeginSession = true; + } + } + CHECK_FALSE(hasBeginSession); + } + + SUBCASE("Session tracking disabled blocks updateSession") { + clearSDK(); + Countly &countly = Countly::getInstance(); + json sbs = {{"st", false}}; + initWithSBSConfig(sbs, countly); + + // updateSession should fail when session tracking is disabled + CHECK(countly.updateSession() == false); + } + + SUBCASE("Session tracking enabled allows session operations") { + clearSDK(); + Countly &countly = Countly::getInstance(); + json sbs = {{"st", true}}; + initWithSBSConfig(sbs, countly); + + // beginSession should succeed + CHECK(countly.beginSession() == true); + + countly.processRQDebug(); + bool hasBeginSession = false; + while (!http_call_queue.empty()) { + HTTPCall call = popCall(); + if (call.data.find("begin_session") != call.data.end()) { + hasBeginSession = true; + } + } + CHECK(hasBeginSession); + } + + // --- Custom Event Tracking (cet) --- + + SUBCASE("Custom event tracking disabled blocks custom events") { + clearSDK(); + Countly &countly = Countly::getInstance(); + json sbs = {{"cet", false}, {"eqs", 1}}; + initWithSBSConfig(sbs, countly); + + // Custom events should be blocked + cly::Event e1("custom_event", 1); + countly.addEvent(e1); + + CHECK(countly.checkEQSize() == 0); + } + + SUBCASE("Custom event tracking disabled allows internal events") { + clearSDK(); + Countly &countly = Countly::getInstance(); + json sbs = {{"cet", false}, {"eqs", 1}}; + initWithSBSConfig(sbs, countly); + + // Internal events ([CLY]_ prefix) should still pass through + std::string viewId = countly.views().openView("test_view"); + CHECK(!viewId.empty()); + + countly.processRQDebug(); + int totalEvents = 0; + while (!http_call_queue.empty()) { + HTTPCall call = popCall(); + if (call.data.find("events") != call.data.end() && !call.data["events"].empty()) { + json events = json::parse(call.data["events"]); + for (const auto &e : events) { + totalEvents++; + // Should be a view event + CHECK(e["key"].get().find("[CLY]_") == 0); + } + } + } + CHECK(totalEvents >= 1); + } + + SUBCASE("Custom event tracking enabled allows all events") { + clearSDK(); + Countly &countly = Countly::getInstance(); + json sbs = {{"cet", true}, {"eqs", 1}}; + initWithSBSConfig(sbs, countly); + + cly::Event e1("my_custom_event", 1); + countly.addEvent(e1); + + countly.processRQDebug(); + int totalEvents = 0; + while (!http_call_queue.empty()) { + HTTPCall call = popCall(); + if (call.data.find("events") != call.data.end() && !call.data["events"].empty()) { + json events = json::parse(call.data["events"]); + totalEvents += events.size(); + } + } + CHECK(totalEvents == 1); + } + + // --- View Tracking (vt) --- + + SUBCASE("View tracking disabled blocks view operations") { + clearSDK(); + Countly &countly = Countly::getInstance(); + json sbs = {{"vt", false}, {"eqs", 1}}; + initWithSBSConfig(sbs, countly); + + // openView should return empty string when view tracking is disabled + std::string viewId = countly.views().openView("test_view"); + CHECK(viewId.empty()); + + // No view events should be in the queue + CHECK(countly.checkEQSize() == 0); + } + + SUBCASE("View tracking enabled allows view operations") { + clearSDK(); + Countly &countly = Countly::getInstance(); + json sbs = {{"vt", true}, {"eqs", 1}}; + initWithSBSConfig(sbs, countly); + + std::string viewId = countly.views().openView("test_view"); + CHECK(!viewId.empty()); + + countly.processRQDebug(); + bool hasViewEvent = false; + while (!http_call_queue.empty()) { + HTTPCall call = popCall(); + if (call.data.find("events") != call.data.end() && !call.data["events"].empty()) { + json events = json::parse(call.data["events"]); + for (const auto &e : events) { + if (e["key"].get() == "[CLY]_view") { + hasViewEvent = true; + } + } + } + } + CHECK(hasViewEvent); + } + + // --- Location Tracking (lt) --- + + SUBCASE("Location tracking disabled blocks setLocation") { + clearSDK(); + Countly &countly = Countly::getInstance(); + json sbs = {{"lt", false}}; + initWithSBSConfig(sbs, countly); + + // setLocation should be blocked + countly.setLocation("US", "New York", "40.7128,-74.0060", "192.168.1.1"); + + countly.processRQDebug(); + // No location request should appear + bool hasLocationRequest = false; + while (!http_call_queue.empty()) { + HTTPCall call = popCall(); + if (call.data.find("country_code") != call.data.end() || call.data.find("location") != call.data.end()) { + hasLocationRequest = true; + } + } + CHECK_FALSE(hasLocationRequest); + } + + SUBCASE("Location tracking enabled allows setLocation") { + clearSDK(); + Countly &countly = Countly::getInstance(); + json sbs = {{"lt", true}}; + initWithSBSConfig(sbs, countly); + + countly.setLocation("US", "New York", "40.7128,-74.0060", "192.168.1.1"); + + countly.processRQDebug(); + bool hasLocationRequest = false; + while (!http_call_queue.empty()) { + HTTPCall call = popCall(); + if (call.data.find("country_code") != call.data.end()) { + hasLocationRequest = true; + CHECK(call.data["country_code"] == "US"); + } + } + CHECK(hasLocationRequest); + } + + // --- Crash Reporting (crt) --- + + SUBCASE("Crash reporting disabled blocks recordException") { + clearSDK(); + Countly &countly = Countly::getInstance(); + json sbs = {{"crt", false}}; + initWithSBSConfig(sbs, countly); + + countly.crash().recordException("Test crash", "stack trace line 1\nline 2", false, {{"_os", "TestOS"}}, {}); + + countly.processRQDebug(); + // No crash request should appear + bool hasCrashRequest = false; + while (!http_call_queue.empty()) { + HTTPCall call = popCall(); + if (call.data.find("crash") != call.data.end()) { + hasCrashRequest = true; + } + } + CHECK_FALSE(hasCrashRequest); + } + + SUBCASE("Crash reporting enabled allows recordException") { + clearSDK(); + Countly &countly = Countly::getInstance(); + json sbs = {{"crt", true}}; + initWithSBSConfig(sbs, countly); + + countly.crash().recordException("Test crash", "stack trace line 1\nline 2", false, {{"_os", "TestOS"}}, {}); + + countly.processRQDebug(); + bool hasCrashRequest = false; + while (!http_call_queue.empty()) { + HTTPCall call = popCall(); + if (call.data.find("crash") != call.data.end()) { + hasCrashRequest = true; + } + } + CHECK(hasCrashRequest); + } +} + +// --------------------------------------------------------------------------- +// 7. Global Flags Tests (tracking, networking) +// --------------------------------------------------------------------------- + +TEST_CASE("SBS Global Flags") { + clearSDK(); + http_call_queue.clear(); + + // --- Tracking Flag --- + + SUBCASE("Tracking disabled blocks all requests from being queued") { + clearSDK(); + Countly &countly = Countly::getInstance(); + json sbs = {{"tracking", false}, {"eqs", 1}}; + initWithSBSConfig(sbs, countly); + + // Events can still be added to EQ, but when sent to RQ, + // the request module should reject them since tracking is off + cly::Event e1("test_event", 1); + countly.addEvent(e1); + + // beginSession adds a request via requestModule->addRequestToQueue + // which checks isTrackingEnabled + countly.beginSession(); + + countly.processRQDebug(); + // No requests should have made it to the HTTP call queue + CHECK(http_call_queue.empty()); + } + + SUBCASE("Tracking enabled allows requests") { + clearSDK(); + Countly &countly = Countly::getInstance(); + json sbs = {{"tracking", true}}; + initWithSBSConfig(sbs, countly); + + countly.beginSession(); + countly.processRQDebug(); + CHECK(!http_call_queue.empty()); + } + + // --- Networking Flag --- + + SUBCASE("Networking disabled blocks request queue processing") { + clearSDK(); + Countly &countly = Countly::getInstance(); + json sbs = {{"networking", false}}; + initWithSBSConfig(sbs, countly); + + // Requests can still be queued, but processQueue should not send them + countly.beginSession(); + + // Process RQ - with networking disabled, requests should stay in queue + countly.processRQDebug(); + // http_call_queue should be empty since networking is disabled + CHECK(http_call_queue.empty()); + + // But RQ should still have the request + CHECK(countly.checkRQSize() > 0); + } + + SUBCASE("Networking enabled allows request queue processing") { + clearSDK(); + Countly &countly = Countly::getInstance(); + json sbs = {{"networking", true}}; + initWithSBSConfig(sbs, countly); + + countly.beginSession(); + countly.processRQDebug(); + CHECK(!http_call_queue.empty()); + } +} + +// --------------------------------------------------------------------------- +// 8. Queue Size Overrides Tests (eqs, rqs) +// --------------------------------------------------------------------------- + +TEST_CASE("SBS Queue Size Overrides") { + clearSDK(); + http_call_queue.clear(); + + // --- Event Queue Size (eqs) --- + + SUBCASE("SBS eqs overrides default event queue threshold") { + clearSDK(); + Countly &countly = Countly::getInstance(); + // Set SBS eqs to 5 (much smaller than default 100) + json sbs = {{"eqs", 5}}; + initWithSBSConfig(sbs, countly); + + // Generate 7 events + test_utils::generateEvents(7, countly); + + // With threshold 5, first 5 should have been flushed to RQ, 2 left in EQ + CHECK(countly.checkEQSize() == 2); + + countly.processRQDebug(); + CHECK(!http_call_queue.empty()); + HTTPCall call = popCall(); + json events = json::parse(call.data["events"]); + CHECK(events.size() == 5); + } + + SUBCASE("SBS eqs of 1 flushes every event immediately") { + clearSDK(); + Countly &countly = Countly::getInstance(); + json sbs = {{"eqs", 1}}; + initWithSBSConfig(sbs, countly); + + test_utils::generateEvents(3, countly); + + // Each event triggers a flush, so EQ should be empty + CHECK(countly.checkEQSize() == 0); + + countly.processRQDebug(); + // Should have 3 separate request entries + CHECK(http_call_queue.size() == 3); + } + + SUBCASE("SBS eqs overrides developer-set event queue threshold") { + clearSDK(); + Countly &countly = Countly::getInstance(); + // Developer sets threshold to 50, but SBS overrides to 3 + countly.setEventsToRQThreshold(50); + json sbs = {{"eqs", 3}}; + initWithSBSConfig(sbs, countly); + + test_utils::generateEvents(5, countly); + + // SBS eqs=3 should override developer's 50 + CHECK(countly.checkEQSize() == 2); // 5 - 3 = 2 remaining + } + + // --- Request Queue Size (rqs) --- + + SUBCASE("SBS rqs limits request queue size by dropping oldest") { + clearSDK(); + Countly &countly = Countly::getInstance(); + // Set rqs to 3, so only 3 requests can be in RQ at a time + json sbs = {{"rqs", 3}, {"eqs", 1}}; + initWithSBSConfig(sbs, countly); + + // Generate events that flush to RQ (with eqs=1, each event is a request) + test_utils::generateEvents(5, countly); + + // RQ should be capped at 3 (oldest 2 dropped) + CHECK(countly.checkRQSize() <= 3); + } +} + +// --------------------------------------------------------------------------- +// 9. Combined Behavior Tests (provided SBS config) +// --------------------------------------------------------------------------- + +TEST_CASE("SBS Combined Behavior") { + clearSDK(); + http_call_queue.clear(); + + SUBCASE("Provided SBS config is applied on init") { + clearSDK(); + Countly &countly = Countly::getInstance(); + + // Provide SBS that disables custom event tracking + json sbs = {{"cet", false}, {"eqs", 1}}; + initWithSBSConfig(sbs, countly); + + // Custom events should be blocked + cly::Event e("custom_event", 1); + countly.addEvent(e); + CHECK(countly.checkEQSize() == 0); + } + + SUBCASE("Multiple SBS flags work together") { + clearSDK(); + Countly &countly = Countly::getInstance(); + + // Disable session tracking and view tracking, but keep events enabled + json sbs = {{"st", false}, {"vt", false}, {"cet", true}, {"eqs", 1}}; + initWithSBSConfig(sbs, countly); + + // Session should be blocked + CHECK(countly.beginSession() == false); + + // Views should be blocked + std::string viewId = countly.views().openView("test"); + CHECK(viewId.empty()); + + // Custom events should still work + cly::Event e("my_event", 1); + countly.addEvent(e); + + countly.processRQDebug(); + int totalEvents = 0; + while (!http_call_queue.empty()) { + HTTPCall call = popCall(); + if (call.data.find("events") != call.data.end() && !call.data["events"].empty()) { + json events = json::parse(call.data["events"]); + totalEvents += events.size(); + } + } + CHECK(totalEvents == 1); + } + + SUBCASE("All features disabled blocks everything") { + clearSDK(); + Countly &countly = Countly::getInstance(); + + json sbs = { + {"tracking", false}, {"networking", false}, {"st", false}, {"vt", false}, + {"lt", false}, {"cet", false}, {"crt", false}}; + initWithSBSConfig(sbs, countly); + + // Nothing should work + CHECK(countly.beginSession() == false); + CHECK(countly.views().openView("test").empty()); + + cly::Event e("event", 1); + countly.addEvent(e); + CHECK(countly.checkEQSize() == 0); + + countly.crash().recordException("crash", "trace", false, {{"_os", "TestOS"}}, {}); + countly.setLocation("US", "NY", "40,-74", "1.2.3.4"); + + countly.processRQDebug(); + CHECK(http_call_queue.empty()); + } + + SUBCASE("All filter types correctly parsed and applied together") { + clearSDK(); + Countly &countly = Countly::getInstance(); + json sbs = { + {"eb", json::array({"blocked_event"})}, + {"sb", json::array({"blocked_seg"})}, + {"esb", {{"special_event", json::array({"special_blocked"})}}}, + {"upb", json::array({"blocked_prop"})}, + {"eqs", 1}}; + initWithSBSConfig(sbs, countly); + + // Verify event blacklist works: blocked_event should be dropped + cly::Event blockedEvt("blocked_event", 1); + countly.addEvent(blockedEvt); + + // Verify allowed event passes with segmentation filter applied + cly::Event allowedEvt("allowed_event", 1); + allowedEvt.addSegmentation("blocked_seg", "v1"); + allowedEvt.addSegmentation("allowed_seg", "v2"); + countly.addEvent(allowedEvt); + + // Verify event-specific segmentation filter + cly::Event specialEvt("special_event", 1); + specialEvt.addSegmentation("special_blocked", "v1"); + specialEvt.addSegmentation("kept_key", "v2"); + countly.addEvent(specialEvt); + + countly.processRQDebug(); + + // Collect all events + std::vector allEvents; + while (!http_call_queue.empty()) { + HTTPCall call = popCall(); + if (call.data.find("events") != call.data.end() && !call.data["events"].empty()) { + json events = json::parse(call.data["events"]); + for (const auto &e : events) { + allEvents.push_back(e); + } + } + } + + // "blocked_event" should be dropped, so only 2 events remain + CHECK(allEvents.size() == 2); + + // Find and check "allowed_event" + bool foundAllowed = false; + bool foundSpecial = false; + for (const auto &evt : allEvents) { + std::string key = evt["key"].get(); + if (key == "allowed_event") { + foundAllowed = true; + json seg = evt["segmentation"]; + CHECK_FALSE(seg.contains("blocked_seg")); + CHECK(seg.contains("allowed_seg")); + CHECK(seg["allowed_seg"].get() == "v2"); + } else if (key == "special_event") { + foundSpecial = true; + json seg = evt["segmentation"]; + CHECK_FALSE(seg.contains("special_blocked")); + CHECK(seg.contains("kept_key")); + CHECK(seg["kept_key"].get() == "v2"); + } + } + CHECK(foundAllowed); + CHECK(foundSpecial); + + // Verify user property blacklist works + http_call_queue.clear(); + countly.setCustomUserDetails({{"blocked_prop", "v1"}, {"allowed_prop", "v2"}}); + countly.processRQDebug(); + CHECK(!http_call_queue.empty()); + HTTPCall upCall = popCall(); + json userDetails = json::parse(upCall.data["user_details"]); + json custom = userDetails["custom"]; + CHECK_FALSE(custom.contains("blocked_prop")); + CHECK(custom.contains("allowed_prop")); + } +} + +// --------------------------------------------------------------------------- +// 10. Scenario Tests (init defaults -> works -> re-init disabled -> blocked) +// --------------------------------------------------------------------------- + +TEST_CASE("SBS Scenarios") { + clearSDK(); + http_call_queue.clear(); + + SUBCASE("scenario_customEventTrackingDisabled") { + // Step 1: Init with defaults (all features enabled) + clearSDK(); + Countly &countly1 = Countly::getInstance(); + json sbs1 = {{"eqs", 1}}; + initWithSBSConfig(sbs1, countly1); + + // Record a custom event - should succeed + cly::Event e1("test_event", 1); + countly1.addEvent(e1); + + countly1.processRQDebug(); + CHECK(!http_call_queue.empty()); + HTTPCall call1 = popCall(); + json events1 = json::parse(call1.data["events"]); + CHECK(events1.size() == 1); + CHECK(events1[0]["key"].get() == "test_event"); + http_call_queue.clear(); + + // Also verify view tracking is not affected + std::string viewId = countly1.views().openView("test_view"); + CHECK(!viewId.empty()); + countly1.processRQDebug(); + http_call_queue.clear(); + + // Step 2: Re-init with custom event tracking disabled + Countly::halt(); + remove(TEST_DATABASE_NAME); + + Countly &countly2 = Countly::getInstance(); + json sbs2 = {{"cet", false}, {"eqs", 1}}; + initWithSBSConfig(sbs2, countly2); + + // Custom events should now be blocked + cly::Event e2("blocked_event", 1); + countly2.addEvent(e2); + CHECK(countly2.checkEQSize() == 0); + + // Views should still work (cet only blocks custom events) + std::string viewId2 = countly2.views().openView("another_view"); + CHECK(!viewId2.empty()); + } + + SUBCASE("scenario_viewTrackingDisabled") { + // Step 1: Init with defaults + clearSDK(); + Countly &countly1 = Countly::getInstance(); + json sbs1 = {{"eqs", 1}}; + initWithSBSConfig(sbs1, countly1); + + // Record a view - should succeed + std::string viewId1 = countly1.views().openView("test_view"); + CHECK(!viewId1.empty()); + + // Record a custom event - should succeed + cly::Event e1("test_event", 1); + countly1.addEvent(e1); + countly1.processRQDebug(); + http_call_queue.clear(); + + // Step 2: Re-init with view tracking disabled + Countly::halt(); + remove(TEST_DATABASE_NAME); + + Countly &countly2 = Countly::getInstance(); + json sbs2 = {{"vt", false}, {"eqs", 1}}; + initWithSBSConfig(sbs2, countly2); + + // Views should now be blocked + std::string viewId2 = countly2.views().openView("test_view_2"); + CHECK(viewId2.empty()); + CHECK(countly2.checkEQSize() == 0); + + // Custom events should still work + cly::Event e2("custom_event", 1); + countly2.addEvent(e2); + countly2.processRQDebug(); + + int totalEvents = 0; + while (!http_call_queue.empty()) { + HTTPCall call = popCall(); + if (call.data.find("events") != call.data.end() && !call.data["events"].empty()) { + json events = json::parse(call.data["events"]); + totalEvents += events.size(); + } + } + CHECK(totalEvents == 1); + } + + SUBCASE("scenario_trackingDisabled") { + // Step 1: Init with defaults + clearSDK(); + Countly &countly1 = Countly::getInstance(); + json sbs1 = json::object(); + initWithSBSConfig(sbs1, countly1); + + // beginSession should succeed and produce a request + CHECK(countly1.beginSession() == true); + countly1.processRQDebug(); + CHECK(!http_call_queue.empty()); + http_call_queue.clear(); + + // Step 2: Re-init with tracking disabled + Countly::halt(); + remove(TEST_DATABASE_NAME); + + Countly &countly2 = Countly::getInstance(); + json sbs2 = {{"tracking", false}}; + initWithSBSConfig(sbs2, countly2); + + // beginSession attempt - RQ should remain empty since tracking is off + countly2.beginSession(); + countly2.processRQDebug(); + CHECK(http_call_queue.empty()); + } + + SUBCASE("scenario_networkingDisabled") { + // Step 1: Init with defaults + clearSDK(); + Countly &countly1 = Countly::getInstance(); + json sbs1 = json::object(); + initWithSBSConfig(sbs1, countly1); + + // beginSession + processRQ should send to HTTP + CHECK(countly1.beginSession() == true); + countly1.processRQDebug(); + CHECK(!http_call_queue.empty()); + http_call_queue.clear(); + + // Step 2: Re-init with networking disabled + Countly::halt(); + remove(TEST_DATABASE_NAME); + + Countly &countly2 = Countly::getInstance(); + json sbs2 = {{"networking", false}}; + initWithSBSConfig(sbs2, countly2); + + // beginSession queues a request, but processRQ should NOT send it + countly2.beginSession(); + countly2.processRQDebug(); + CHECK(http_call_queue.empty()); + + // But the request should still be in the RQ + CHECK(countly2.checkRQSize() > 0); + } + + SUBCASE("scenario_sessionTrackingDisabled") { + // Step 1: Init with defaults + clearSDK(); + Countly &countly1 = Countly::getInstance(); + json sbs1 = json::object(); + initWithSBSConfig(sbs1, countly1); + + // beginSession should succeed + CHECK(countly1.beginSession() == true); + countly1.processRQDebug(); + + bool hasBeginSession = false; + while (!http_call_queue.empty()) { + HTTPCall call = popCall(); + if (call.data.find("begin_session") != call.data.end()) { + hasBeginSession = true; + } + } + CHECK(hasBeginSession); + + // Step 2: Re-init with session tracking disabled + Countly::halt(); + remove(TEST_DATABASE_NAME); + + Countly &countly2 = Countly::getInstance(); + json sbs2 = {{"st", false}}; + initWithSBSConfig(sbs2, countly2); + + // beginSession should be blocked + CHECK(countly2.beginSession() == false); + countly2.processRQDebug(); + CHECK(http_call_queue.empty()); + } + + SUBCASE("scenario_sessionTrackingDisabled_manualSessions") { + // Step 1: Init with defaults and manual session control + clearSDK(); + Countly &countly1 = Countly::getInstance(); + json sbs1 = json::object(); + initWithSBSConfig(sbs1, countly1); + + // All session operations should succeed + CHECK(countly1.beginSession() == true); + CHECK(countly1.updateSession() == true); + CHECK(countly1.endSession() == true); + countly1.processRQDebug(); + http_call_queue.clear(); + + // Step 2: Re-init with session tracking disabled + Countly::halt(); + remove(TEST_DATABASE_NAME); + + Countly &countly2 = Countly::getInstance(); + json sbs2 = {{"st", false}}; + initWithSBSConfig(sbs2, countly2); + + // All session operations should be blocked + CHECK(countly2.beginSession() == false); + CHECK(countly2.updateSession() == false); + CHECK(countly2.endSession() == false); + + countly2.processRQDebug(); + CHECK(http_call_queue.empty()); + } + + SUBCASE("scenario_locationTrackingDisabled") { + // Step 1: Init with defaults + clearSDK(); + Countly &countly1 = Countly::getInstance(); + json sbs1 = json::object(); + initWithSBSConfig(sbs1, countly1); + + // setLocation should succeed + countly1.setLocation("US", "New York", "40.7128,-74.0060", "192.168.1.1"); + countly1.processRQDebug(); + + bool hasLocation = false; + while (!http_call_queue.empty()) { + HTTPCall call = popCall(); + if (call.data.find("country_code") != call.data.end()) { + hasLocation = true; + } + } + CHECK(hasLocation); + + // Step 2: Re-init with location tracking disabled + Countly::halt(); + remove(TEST_DATABASE_NAME); + + Countly &countly2 = Countly::getInstance(); + json sbs2 = {{"lt", false}}; + initWithSBSConfig(sbs2, countly2); + + // setLocation should be blocked + countly2.setLocation("UK", "London", "51.5074,-0.1278", "10.0.0.1"); + countly2.processRQDebug(); + + bool hasLocation2 = false; + while (!http_call_queue.empty()) { + HTTPCall call = popCall(); + if (call.data.find("country_code") != call.data.end()) { + hasLocation2 = true; + } + } + CHECK_FALSE(hasLocation2); + } + + SUBCASE("scenario_filterConfigurationRuntimeUpdate") { + // Init with no filters -> events pass -> re-init with filter -> events blocked + clearSDK(); + Countly &countly1 = Countly::getInstance(); + json sbs1 = {{"eqs", 1}}; + initWithSBSConfig(sbs1, countly1); + + // Events should pass unfiltered + cly::Event e1("my_event", 1); + countly1.addEvent(e1); + + countly1.processRQDebug(); + CHECK(!http_call_queue.empty()); + HTTPCall call1 = popCall(); + json events1 = json::parse(call1.data["events"]); + CHECK(events1.size() == 1); + CHECK(events1[0]["key"].get() == "my_event"); + http_call_queue.clear(); + + // Re-init with a blacklist that blocks "my_event" + Countly::halt(); + remove(TEST_DATABASE_NAME); + + Countly &countly2 = Countly::getInstance(); + json sbs2 = {{"eb", json::array({"my_event"})}, {"eqs", 1}}; + initWithSBSConfig(sbs2, countly2); + + // Same event should now be blocked + cly::Event e2("my_event", 1); + countly2.addEvent(e2); + + countly2.processRQDebug(); + + // Only check for "my_event" in any queued events - it should not appear + bool foundBlockedEvent = false; + while (!http_call_queue.empty()) { + HTTPCall call = popCall(); + if (call.data.find("events") != call.data.end() && !call.data["events"].empty()) { + json events = json::parse(call.data["events"]); + for (const auto &e : events) { + if (e["key"].get() == "my_event") { + foundBlockedEvent = true; + } + } + } + } + CHECK_FALSE(foundBlockedEvent); + } +} + +// --------------------------------------------------------------------------- +// 11. Edge Cases & Guards +// --------------------------------------------------------------------------- + +TEST_CASE("SBS Edge Cases") { + clearSDK(); + http_call_queue.clear(); + + SUBCASE("Empty event whitelist allows all (empty = no filtering)") { + clearSDK(); + Countly &countly = Countly::getInstance(); + // Empty whitelist means "allow nothing" - but current implementation treats + // empty filter list as "no filtering" (everything allowed). + // This test documents the actual behavior. + json sbs = {{"ew", json::array()}, {"eqs", 1}}; + initWithSBSConfig(sbs, countly); + + cly::Event e("some_event", 1); + countly.addEvent(e); + + // Empty filter list = no filtering, so the event should pass through + countly.processRQDebug(); + int totalEvents = 0; + while (!http_call_queue.empty()) { + HTTPCall call = popCall(); + if (call.data.find("events") != call.data.end() && !call.data["events"].empty()) { + json events = json::parse(call.data["events"]); + totalEvents += events.size(); + } + } + CHECK(totalEvents == 1); + } + + SUBCASE("Empty segmentation whitelist allows all keys (empty = no filtering)") { + clearSDK(); + Countly &countly = Countly::getInstance(); + json sbs = {{"sw", json::array()}, {"eqs", 1}}; + initWithSBSConfig(sbs, countly); + + cly::Event e("test_event", 1); + e.addSegmentation("key1", "v1"); + e.addSegmentation("key2", "v2"); + countly.addEvent(e); + + countly.processRQDebug(); + CHECK(!http_call_queue.empty()); + HTTPCall call = popCall(); + json events = json::parse(call.data["events"]); + json seg = events[0]["segmentation"]; + CHECK(seg.contains("key1")); + CHECK(seg.contains("key2")); + } + + SUBCASE("Empty user property whitelist allows all properties (empty = no filtering)") { + clearSDK(); + Countly &countly = Countly::getInstance(); + json sbs = {{"upw", json::array()}}; + initWithSBSConfig(sbs, countly); + + countly.setCustomUserDetails({{"prop1", "v1"}, {"prop2", "v2"}}); + countly.processRQDebug(); + + CHECK(!http_call_queue.empty()); + HTTPCall call = popCall(); + json userDetails = json::parse(call.data["user_details"]); + json custom = userDetails["custom"]; + CHECK(custom.contains("prop1")); + CHECK(custom.contains("prop2")); + } + + SUBCASE("setSDKBehaviorSettings rejected after init") { + clearSDK(); + Countly &countly = Countly::getInstance(); + json sbs = {{"cet", false}, {"eqs", 1}}; + initWithSBSConfig(sbs, countly); + + // Custom events should be blocked + cly::Event e1("custom_event", 1); + countly.addEvent(e1); + CHECK(countly.checkEQSize() == 0); + + // Try to override SBS after init - should be rejected + std::string newSbs = json({{"cet", true}}).dump(); + countly.setSDKBehaviorSettings(newSbs); + + // Custom events should still be blocked (post-init change rejected) + cly::Event e2("custom_event_2", 1); + countly.addEvent(e2); + CHECK(countly.checkEQSize() == 0); + } + + SUBCASE("disableSDKBehaviorSettingsUpdates rejected after init") { + clearSDK(); + Countly &countly = Countly::getInstance(); + json sbs = {{"cet", true}, {"eqs", 1}}; + initWithSBSConfig(sbs, countly); + + // Should log warning and be ignored after init + countly.disableSDKBehaviorSettingsUpdates(); + + // SDK should still function normally + cly::Event e("test", 1); + countly.addEvent(e); + + countly.processRQDebug(); + int totalEvents = 0; + while (!http_call_queue.empty()) { + HTTPCall call = popCall(); + if (call.data.find("events") != call.data.end() && !call.data["events"].empty()) { + json events = json::parse(call.data["events"]); + totalEvents += events.size(); + } + } + CHECK(totalEvents == 1); + } + + SUBCASE("Special characters in event names handled by blacklist") { + clearSDK(); + Countly &countly = Countly::getInstance(); + json sbs = { + {"eb", json::array({"event with spaces", "event-with-dashes", "event_with_underscores"})}, + {"eqs", 1}}; + initWithSBSConfig(sbs, countly); + + // These should all be blocked + cly::Event e1("event with spaces", 1); + countly.addEvent(e1); + cly::Event e2("event-with-dashes", 1); + countly.addEvent(e2); + cly::Event e3("event_with_underscores", 1); + countly.addEvent(e3); + + // This should pass + cly::Event e4("normal_event", 1); + countly.addEvent(e4); + + countly.processRQDebug(); + + int totalEvents = 0; + while (!http_call_queue.empty()) { + HTTPCall call = popCall(); + if (call.data.find("events") != call.data.end() && !call.data["events"].empty()) { + json events = json::parse(call.data["events"]); + for (const auto &e : events) { + std::string key = e["key"].get(); + CHECK(key != "event with spaces"); + CHECK(key != "event-with-dashes"); + CHECK(key != "event_with_underscores"); + totalEvents++; + } + } + } + CHECK(totalEvents == 1); + } + + SUBCASE("Empty segmentation map with segmentation filter works") { + clearSDK(); + Countly &countly = Countly::getInstance(); + json sbs = {{"sb", json::array({"some_key"})}, {"eqs", 1}}; + initWithSBSConfig(sbs, countly); + + // Event with no segmentation at all + cly::Event e("test_event", 1); + countly.addEvent(e); + + countly.processRQDebug(); + CHECK(!http_call_queue.empty()); + HTTPCall call = popCall(); + json events = json::parse(call.data["events"]); + CHECK(events.size() == 1); + CHECK(events[0]["key"].get() == "test_event"); + } + + SUBCASE("Tracking disabled blocks all queue writes") { + clearSDK(); + Countly &countly = Countly::getInstance(); + json sbs = {{"tracking", false}, {"eqs", 1}}; + initWithSBSConfig(sbs, countly); + + // Try various operations that should all be blocked + cly::Event e1("event1", 1); + countly.addEvent(e1); + + cly::Event e2("event2", 1); + countly.addEvent(e2); + + countly.crash().recordException("crash", "trace", false, {{"_os", "TestOS"}}, {}); + + countly.setLocation("US", "NY", "40,-74", "1.2.3.4"); + + countly.beginSession(); + + countly.processRQDebug(); + + // Nothing should have made it to the HTTP queue + CHECK(http_call_queue.empty()); + } +} + +// --------------------------------------------------------------------------- +// 12. Storage Behavior Tests (SQLite only - persistence requires database) +// --------------------------------------------------------------------------- +#ifdef COUNTLY_USE_SQLITE + +TEST_CASE("SBS Storage Behavior") { + clearSDK(); + http_call_queue.clear(); + + SUBCASE("Stored SBS is loaded on re-init without provided SBS") { + // Step 1: Init SDK with provided SBS that disables custom event tracking + clearSDK(); + Countly &countly1 = Countly::getInstance(); + json sbs = {{"cet", false}, {"eqs", 1}}; + initWithSBSConfig(sbs, countly1); + + // Verify that custom events are blocked in this session + cly::Event e1("custom_event", 1); + countly1.addEvent(e1); + CHECK(countly1.checkEQSize() == 0); + + // Step 2: halt() resets the singleton but preserves the database + Countly::halt(); + http_call_queue.clear(); + + // Step 3: Re-init SDK WITHOUT providing SBS => stored SBS should be loaded from DB + Countly &countly2 = Countly::getInstance(); + initWithoutSBSConfig(countly2); + + // Step 4: Verify the stored config is applied (cet=false should still be active) + cly::Event e2("custom_event", 1); + countly2.addEvent(e2); + CHECK(countly2.checkEQSize() == 0); + + // Clean up: remove the database for subsequent tests + Countly::halt(); + remove(TEST_DATABASE_NAME); + } + + SUBCASE("Stored SBS takes precedence over provided SBS") { + // Step 1: Init SDK with SBS that disables session tracking (st=false) + clearSDK(); + Countly &countly1 = Countly::getInstance(); + json sbs1 = {{"st", false}}; + initWithSBSConfig(sbs1, countly1); + + // Verify session tracking is disabled + CHECK(countly1.beginSession() == false); + + // Step 2: halt() resets singleton, database persists with stored SBS + Countly::halt(); + http_call_queue.clear(); + + // Step 3: Re-init SDK WITH a different SBS that enables session tracking (st=true) + // Stored SBS (st=false) should take precedence over the newly provided one + Countly &countly2 = Countly::getInstance(); + json sbs2 = {{"st", true}}; + initWithSBSConfig(sbs2, countly2); + + // Step 4: Verify stored config takes precedence (st should still be false) + CHECK(countly2.beginSession() == false); + + // Clean up + Countly::halt(); + remove(TEST_DATABASE_NAME); + } + + SUBCASE("Provided SBS is used when no stored SBS exists") { + // Step 1: Start with a clean database (no stored SBS) + clearSDK(); + Countly &countly = Countly::getInstance(); + + // Provide SBS that disables view tracking + json sbs = {{"vt", false}, {"eqs", 1}}; + initWithSBSConfig(sbs, countly); + + // Views should be blocked since provided SBS is used (no stored SBS) + std::string viewId = countly.views().openView("test_view"); + CHECK(viewId.empty()); + CHECK(countly.checkEQSize() == 0); + } + + SUBCASE("User config defaults are used when no SBS is stored or provided") { + // Step 1: Start with a clean database (no stored SBS) and do NOT provide SBS + clearSDK(); + Countly &countly = Countly::getInstance(); + initWithoutSBSConfig(countly); + + // Without any SBS (stored or provided), defaults should apply: + // - All features enabled + // - No event/segmentation/user property filters + // - Default EQ threshold + + // Session should work (st defaults to true) + CHECK(countly.beginSession() == true); + countly.processRQDebug(); + bool hasBeginSession = false; + while (!http_call_queue.empty()) { + HTTPCall call = popCall(); + if (call.data.find("begin_session") != call.data.end()) { + hasBeginSession = true; + } + } + CHECK(hasBeginSession); + + // Views should work (vt defaults to true) + std::string viewId = countly.views().openView("test_view"); + CHECK(!viewId.empty()); + } + + SUBCASE("Empty SBS config value uses defaults") { + // If no value is sent for a configuration (c is empty), + // then the SDK uses its own default or the value provided by the developer + clearSDK(); + Countly &countly = Countly::getInstance(); + + // Provide SBS with only eqs set; all other fields are absent (empty) + // This means the SDK should use defaults for all unset fields + json sbs = {{"eqs", 3}}; + initWithSBSConfig(sbs, countly); + + // Session tracking should default to enabled + CHECK(countly.beginSession() == true); + countly.processRQDebug(); + http_call_queue.clear(); + + // View tracking should default to enabled + std::string viewId = countly.views().openView("test_view"); + CHECK(!viewId.empty()); + + // Custom event tracking should default to enabled + cly::Event e("custom_event", 1); + countly.addEvent(e); + CHECK(countly.checkEQSize() > 0); + } + + SUBCASE("Stored SBS feature flag persists across multiple re-inits") { + // Step 1: Init with SBS that disables crash reporting and sets eqs=2 + clearSDK(); + Countly &countly1 = Countly::getInstance(); + json sbs = {{"crt", false}, {"eqs", 2}}; + initWithSBSConfig(sbs, countly1); + + // Verify crash reporting is disabled + countly1.crash().recordException("crash1", "trace1", false, {{"_os", "TestOS"}}, {}); + countly1.processRQDebug(); + bool hasCrash = false; + while (!http_call_queue.empty()) { + HTTPCall call = popCall(); + if (call.data.find("crash") != call.data.end()) { + hasCrash = true; + } + } + CHECK_FALSE(hasCrash); + + // Step 2: First re-init without providing SBS + Countly::halt(); + http_call_queue.clear(); + Countly &countly2 = Countly::getInstance(); + initWithoutSBSConfig(countly2); + + // Verify crash reporting is still disabled from stored SBS + countly2.crash().recordException("crash2", "trace2", false, {{"_os", "TestOS"}}, {}); + countly2.processRQDebug(); + hasCrash = false; + while (!http_call_queue.empty()) { + HTTPCall call = popCall(); + if (call.data.find("crash") != call.data.end()) { + hasCrash = true; + } + } + CHECK_FALSE(hasCrash); + + // Step 3: Second re-init without providing SBS + Countly::halt(); + http_call_queue.clear(); + Countly &countly3 = Countly::getInstance(); + initWithoutSBSConfig(countly3); + + // Verify crash reporting is still disabled after a second re-init + countly3.crash().recordException("crash3", "trace3", false, {{"_os", "TestOS"}}, {}); + countly3.processRQDebug(); + hasCrash = false; + while (!http_call_queue.empty()) { + HTTPCall call = popCall(); + if (call.data.find("crash") != call.data.end()) { + hasCrash = true; + } + } + CHECK_FALSE(hasCrash); + + // Clean up + Countly::halt(); + remove(TEST_DATABASE_NAME); + } +} + +// --------------------------------------------------------------------------- +// 13. Location Auto-Clearing and Clearing When Disabled +// --------------------------------------------------------------------------- + +TEST_CASE("SBS Location Clearing") { + clearSDK(); + http_call_queue.clear(); + + SUBCASE("setLocation clearing (all empty) is allowed when lt=false") { + clearSDK(); + Countly &countly = Countly::getInstance(); + json sbs = {{"lt", false}}; + initWithSBSConfig(sbs, countly); + + // Setting actual location should be blocked + countly.setLocation("US", "New York", "40.7,-74.0", "1.2.3.4"); + countly.processRQDebug(); + bool hasLocationSet = false; + while (!http_call_queue.empty()) { + HTTPCall call = popCall(); + if (call.data.find("country_code") != call.data.end() && call.data["country_code"] == "US") { + hasLocationSet = true; + } + } + CHECK_FALSE(hasLocationSet); + + // But clearing location (all empty) should be allowed even when lt=false + countly.setLocation("", "", "", ""); + countly.processRQDebug(); + bool hasClearRequest = false; + while (!http_call_queue.empty()) { + HTTPCall call = popCall(); + if (call.data.find("location") != call.data.end() && call.data["location"].empty()) { + hasClearRequest = true; + } + } + CHECK(hasClearRequest); + } +} + +// --------------------------------------------------------------------------- +// 14. processQueue Tracking Gate (distinct from addRequestToQueue) +// --------------------------------------------------------------------------- + +TEST_CASE("SBS processQueue Tracking Gate") { + clearSDK(); + http_call_queue.clear(); + + SUBCASE("Queued requests are not sent when tracking is later disabled") { + // Step 1: Init with tracking enabled, begin session to queue a request + clearSDK(); + Countly &countly1 = Countly::getInstance(); + json sbs1 = {{"tracking", true}}; + initWithSBSConfig(sbs1, countly1); + + countly1.beginSession(); + // Session request is now in the RQ + + // Step 2: Re-init with tracking disabled (stored SBS takes precedence on re-init) + Countly::halt(); + http_call_queue.clear(); + + Countly &countly2 = Countly::getInstance(); + json sbs2 = {{"tracking", false}}; + initWithSBSConfig(sbs2, countly2); + + // Step 3: Process the queue — the old session request should NOT be sent + countly2.processRQDebug(); + CHECK(http_call_queue.empty()); + + Countly::halt(); + remove(TEST_DATABASE_NAME); + } +} + +// --------------------------------------------------------------------------- +// 15. Session Update Interval (sui) Override +// --------------------------------------------------------------------------- + +TEST_CASE("SBS Session Update Interval Override") { + clearSDK(); + http_call_queue.clear(); + + SUBCASE("sui=1 causes session update to be sent after 1 second") { + clearSDK(); + Countly &countly = Countly::getInstance(); + json sbs = {{"sui", 1}}; + initWithSBSConfig(sbs, countly); + + countly.beginSession(); + countly.processRQDebug(); + http_call_queue.clear(); + + // Wait longer than sui (1 second) + std::this_thread::sleep_for(std::chrono::milliseconds(1200)); + + countly.updateSession(); + countly.processRQDebug(); + + // Session update should have been sent (duration >= sui) + bool hasSessionUpdate = false; + while (!http_call_queue.empty()) { + HTTPCall call = popCall(); + if (call.data.find("session_duration") != call.data.end()) { + hasSessionUpdate = true; + } + } + CHECK(hasSessionUpdate); + } + + SUBCASE("Large sui prevents premature session updates") { + clearSDK(); + Countly &countly = Countly::getInstance(); + json sbs = {{"sui", 300}}; + initWithSBSConfig(sbs, countly); + + countly.beginSession(); + countly.processRQDebug(); + http_call_queue.clear(); + + // Wait only 1 second (far less than sui=300) + std::this_thread::sleep_for(std::chrono::milliseconds(1100)); + + countly.updateSession(); + countly.processRQDebug(); + + // No session update should be sent (duration < sui) + bool hasSessionUpdate = false; + while (!http_call_queue.empty()) { + HTTPCall call = popCall(); + if (call.data.find("session_duration") != call.data.end()) { + hasSessionUpdate = true; + } + } + CHECK_FALSE(hasSessionUpdate); + } +} + +// --------------------------------------------------------------------------- +// 16. Blacklist-to-Whitelist Transition +// --------------------------------------------------------------------------- + +TEST_CASE("SBS Blacklist to Whitelist Transition") { + clearSDK(); + http_call_queue.clear(); + + SUBCASE("Switching from event blacklist to whitelist works correctly") { + // Step 1: Init with event blacklist + clearSDK(); + Countly &countly1 = Countly::getInstance(); + json sbs1 = {{"eb", json::array({"blocked_event"})}, {"eqs", 1}}; + initWithSBSConfig(sbs1, countly1); + + // "blocked_event" should be blocked, "other_event" allowed + cly::Event e1("blocked_event", 1); + countly1.addEvent(e1); + CHECK(countly1.checkEQSize() == 0); // blocked + + cly::Event e2("other_event", 1); + countly1.addEvent(e2); + CHECK(countly1.checkEQSize() == 0); // flushed to RQ (eqs=1) + + // Step 2: Re-init with event whitelist (no blacklist) + Countly::halt(); + http_call_queue.clear(); + remove(TEST_DATABASE_NAME); // clear stored SBS so provided SBS takes effect + + Countly &countly2 = Countly::getInstance(); + json sbs2 = {{"ew", json::array({"allowed_only"})}, {"eqs", 1}}; + initWithSBSConfig(sbs2, countly2); + + // "allowed_only" should pass, "other_event" should be blocked by whitelist + cly::Event e3("allowed_only", 1); + countly2.addEvent(e3); + CHECK(countly2.checkEQSize() == 0); // flushed (allowed + eqs=1) + + cly::Event e4("other_event", 1); + countly2.addEvent(e4); + CHECK(countly2.checkEQSize() == 0); // blocked by whitelist, EQ still 0 + + // Verify only "allowed_only" made it to RQ + countly2.processRQDebug(); + bool hasAllowed = false; + bool hasOther = false; + while (!http_call_queue.empty()) { + HTTPCall call = popCall(); + if (call.data.find("events") != call.data.end()) { + std::string eventsStr = call.data["events"]; + if (eventsStr.find("allowed_only") != std::string::npos) hasAllowed = true; + if (eventsStr.find("other_event") != std::string::npos) hasOther = true; + } + } + CHECK(hasAllowed); + CHECK_FALSE(hasOther); + + Countly::halt(); + remove(TEST_DATABASE_NAME); + } +} + +// --------------------------------------------------------------------------- +// 17. Malformed SBS JSON Handling +// --------------------------------------------------------------------------- + +TEST_CASE("SBS Malformed JSON Handling") { + clearSDK(); + http_call_queue.clear(); + + SUBCASE("Corrupted SBS string from config falls back to defaults") { + clearSDK(); + Countly &countly = Countly::getInstance(); + std::string badJson = "{this is not valid json!!!}"; + countly.setSDKBehaviorSettings(badJson); + countly.disableSDKBehaviorSettingsUpdates(); + countly.setHTTPClient(test_utils::fakeSendHTTP); + countly.setDeviceID(COUNTLY_TEST_DEVICE_ID); + countly.SetPath(TEST_DATABASE_NAME); + countly.enableManualSessionControl(); + countly.start(COUNTLY_TEST_APP_KEY, COUNTLY_TEST_HOST, COUNTLY_TEST_PORT, false); + std::this_thread::sleep_for(std::chrono::milliseconds(200)); + countly.processRQDebug(); + countly.clearRequestQueue(); + http_call_queue.clear(); + + // SDK should use defaults — session tracking enabled, custom events enabled, etc. + CHECK(countly.beginSession() == true); + + cly::Event e("test_event", 1); + countly.addEvent(e); + CHECK(countly.checkEQSize() > 0); + } +} +#endif // COUNTLY_USE_SQLITE diff --git a/tests/session.cpp b/tests/session.cpp index b1275bc..07e7b84 100644 --- a/tests/session.cpp +++ b/tests/session.cpp @@ -30,10 +30,13 @@ TEST_CASE("sessions unit tests") { countly.setAutomaticSessionUpdateInterval(2); countly.SetPath(TEST_DATABASE_NAME); countly.start(COUNTLY_TEST_APP_KEY, COUNTLY_TEST_HOST, COUNTLY_TEST_PORT, false); + // Wait for the async SBS config fetch thread to complete + std::this_thread::sleep_for(std::chrono::milliseconds(200)); SUBCASE("init sdk - session begin ") { countly.processRQDebug(); - HTTPCall http_call = popHTTPCall(); + HTTPCall http_call = popHTTPCall(); // first request is SBS + http_call = popHTTPCall(); // second request is session begin long long timestamp = getUnixTimestamp(); long long timestampDiff = timestamp - stoll(http_call.data["timestamp"]); CHECK(http_call.data["app_key"] == COUNTLY_TEST_APP_KEY); @@ -87,6 +90,8 @@ TEST_CASE("event request unit tests") { countly.SetPath(TEST_DATABASE_NAME); countly.start(COUNTLY_TEST_APP_KEY, COUNTLY_TEST_HOST, COUNTLY_TEST_PORT, false); + // Wait for the async SBS config fetch thread to complete + std::this_thread::sleep_for(std::chrono::milliseconds(200)); countly.processRQDebug(); countly.clearRequestQueue(); // request queue contains session begin request diff --git a/tests/test_utils.hpp b/tests/test_utils.hpp index 4062db9..0663810 100644 --- a/tests/test_utils.hpp +++ b/tests/test_utils.hpp @@ -5,6 +5,7 @@ #include "doctest.h" #include "nlohmann/json.hpp" #include +#include using namespace cly; @@ -21,11 +22,28 @@ struct HTTPCall { std::map data; }; -static std::deque http_call_queue; +// Thread-safe wrapper around the HTTP call queue. +// The SDK's background threads (SBS config fetch, updateLoop) call fakeSendHTTP +// which pushes to this queue, while the test thread reads/clears it. +struct ThreadSafeHTTPCallQueue { + mutable std::mutex mtx; + std::deque queue; + + void push_back(const HTTPCall &call) { std::lock_guard lock(mtx); queue.push_back(call); } + void clear() { std::lock_guard lock(mtx); queue.clear(); } + bool empty() const { std::lock_guard lock(mtx); return queue.empty(); } + size_t size() const { std::lock_guard lock(mtx); return queue.size(); } + HTTPCall front() const { std::lock_guard lock(mtx); return queue.front(); } + void pop_front() { std::lock_guard lock(mtx); queue.pop_front(); } + HTTPCall at(size_t idx) const { std::lock_guard lock(mtx); return queue.at(idx); } +}; + +static ThreadSafeHTTPCallQueue http_call_queue; static void clearSDK() { cly::Countly::halt(); remove(TEST_DATABASE_NAME); + http_call_queue.clear(); } /** @@ -46,7 +64,7 @@ static void checkTopRequestEventSize(int size, cly::Countly &countly) { countly.processRQDebug(); // check that the local HTTP request queue has atleast 1 event - CHECK(!http_call_queue.empty()); + REQUIRE(!http_call_queue.empty()); // get the oldest event HTTPCall oldest_call = http_call_queue.front(); // remove the oldest event from the queue @@ -152,6 +170,41 @@ static HTTPResponse fakeSendHTTP(bool use_post, const std::string &url, const st return response; } +// Search http_call_queue for a request containing a specific key=value pair +static bool httpQueueContains(const std::string &key, const std::string &value) { + size_t n = http_call_queue.size(); + for (size_t i = 0; i < n; i++) { + HTTPCall call = http_call_queue.at(i); + auto it = call.data.find(key); + if (it != call.data.end() && it->second == value) { + return true; + } + } + return false; +} + +// Search http_call_queue for a request containing a specific event key +static bool httpQueueContainsEvent(const std::string &event_key) { + size_t n = http_call_queue.size(); + for (size_t i = 0; i < n; i++) { + HTTPCall call = http_call_queue.at(i); + auto it = call.data.find("events"); + if (it != call.data.end()) { + try { + nlohmann::json events = nlohmann::json::parse(it->second); + for (const auto &e : events) { + if (e["key"].get() == event_key) { + return true; + } + } + } catch (const nlohmann::json::exception &) { + // Malformed events JSON — skip this entry + } + } + } + return false; +} + static void initCountlyWithFakeNetworking(bool clearInitialNetworkingState, cly::Countly &countly) { // set the HTTP client to the fake one which just stores the HTTP calls in a queue countly.setHTTPClient(fakeSendHTTP); @@ -163,6 +216,8 @@ static void initCountlyWithFakeNetworking(bool clearInitialNetworkingState, cly: // start the Countly SDK countly.start(COUNTLY_TEST_APP_KEY, COUNTLY_TEST_HOST, COUNTLY_TEST_PORT, false); + // Wait for the async SBS config fetch thread to complete + std::this_thread::sleep_for(std::chrono::milliseconds(200)); CHECK(countly.checkEQSize() == 0); // Process the RQ so that thing will be at the http call queue