#define CATCH_CONFIG_MAIN

// clazy:excludeall=non-pod-global-static

#include "pluginloader.h"
#include <catch2/catch.hpp>

TEST_CASE("PluginLoader::signature_state")
{
    // ignore
    REQUIRE(PluginLoader::signature_state(true, false, false) == PluginLoader::SigIgnored);

    // check
    REQUIRE(PluginLoader::signature_state(false, true, false) >= PluginLoader::SigChecked);
    REQUIRE(PluginLoader::signature_state(false, true, false) < PluginLoader::SigEnforced);
    REQUIRE(PluginLoader::signature_state(true, true, false) >= PluginLoader::SigChecked);
    REQUIRE(PluginLoader::signature_state(true, true, false) < PluginLoader::SigEnforced);

    // enfore
    REQUIRE(PluginLoader::signature_state(false, false, true) >= PluginLoader::SigEnforced);
    REQUIRE(PluginLoader::signature_state(true, false, true) >= PluginLoader::SigEnforced);
    REQUIRE(PluginLoader::signature_state(false, true, true) >= PluginLoader::SigEnforced);
    REQUIRE(PluginLoader::signature_state(true, true, true) >= PluginLoader::SigEnforced);
}

TEST_CASE("files")
{
    REQUIRE(qEnvironmentVariableIsSet("UNSIGNEDFILE"));
    REQUIRE(qEnvironmentVariableIsSet("SIGNEDFILE"));
    REQUIRE(qEnvironmentVariableIsSet("BADSIGNEDFILE"));
}

TEST_CASE("PluginLoader::verify missing plugin")
{
    const auto state = PluginLoader::signature_state(false, false, false);
    PluginLoader loader("", state);

    REQUIRE_FALSE(loader.verify());
    REQUIRE_FALSE(loader.errorString().isEmpty());
}

TEST_CASE("PluginLoader::verify signature ignored")
{
    const auto state = PluginLoader::signature_state(true, false, false);
    PluginLoader loader(qgetenv("UNSIGNEDFILE"), state);

    REQUIRE(loader.verify());
}

TEST_CASE("PluginLoader::verify signature checked [avialable]")
{
    const auto state = PluginLoader::signature_state(false, true, false);
    PluginLoader loader(qgetenv("SIGNEDFILE"), state);

    REQUIRE(loader.verify());
}

TEST_CASE("PluginLoader::verify signature checked [missing]")
{
    const auto state = PluginLoader::signature_state(false, true, false);
    PluginLoader loader(qgetenv("UNSIGNEDFILE"), state);

    REQUIRE(loader.verify());
}

TEST_CASE("PluginLoader::verify signature checked [bad]")
{
    const auto state = PluginLoader::signature_state(false, true, false);
    PluginLoader loader(qgetenv("BADSIGNEDFILE"), state);

    REQUIRE_FALSE(loader.verify());
    REQUIRE_FALSE(loader.errorString().isEmpty());
}

TEST_CASE("PluginLoader::verify signature enforced [avialable]")
{
    const auto state = PluginLoader::signature_state(false, false, true);
    PluginLoader loader(qgetenv("SIGNEDFILE"), state);

    REQUIRE(loader.verify());
}

TEST_CASE("PluginLoader::verify signature enforced [missing]")
{
    const auto state = PluginLoader::signature_state(false, false, true);
    PluginLoader loader(qgetenv("UNSIGNEDFILE"), state);

    REQUIRE_FALSE(loader.verify());
    REQUIRE_FALSE(loader.errorString().isEmpty());
}

TEST_CASE("PluginLoader::verify signature enforced [bad]")
{
    const auto state = PluginLoader::signature_state(false, false, true);
    PluginLoader loader(qgetenv("BADSIGNEDFILE"), state);

    REQUIRE_FALSE(loader.verify());
    REQUIRE_FALSE(loader.errorString().isEmpty());
}