You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
libpEpAdapter/test/test_passphrase_cache.cc

102 lines
2.9 KiB
C++

// This file is under GNU General Public License 3.0
// see LICENSE.txt
#include "framework/framework.hh"
#include <iostream>
#include <assert.h>
#include <sys/param.h>
#include "../src/passphrase_cache.hh"
#include "../src/status_to_string.hh"
#include "../src/Adapter.hh"
using namespace pEp;
extern "C" {
::PEP_STATUS api_test1(::PEP_SESSION session, const char *str, char *bytes, int n, ::stringlist_t *sl)
{
std::cout << "called api_test1\n";
return PEP_WRONG_PASSPHRASE;
}
::PEP_STATUS api_test2(::PEP_SESSION session, int n, const char *str, char *bytes, ::stringlist_t *sl)
{
std::cout << "called api_test2\n";
return PEP_STATUS_OK;
}
};
int main(int argc, char **argv)
{
Test::setup(argc, argv);
Adapter::session.initialize();
const char *str = "23";
char *bytes = NULL;
int n = 42;
::stringlist_t *sl = NULL;
PassphraseCache cache{ 2, std::chrono::seconds(1) };
3 years ago
cache.add("say");
cache.add("hello");
cache.add("world");
std::cout << "expected: two passphrases\n";
cache.for_each_passphrase([&](std::string passphrase) {
std::cout << "'" << passphrase << "'\n";
return false;
});
std::cout << "expected: one passphrase\n";
cache.for_each_passphrase([&](std::string passphrase) {
std::cout << "'" << passphrase << "'\n";
return passphrase != "";
});
std::cout << "expected: two passphrases but reverse order\n";
cache.for_each_passphrase([&](std::string passphrase) {
std::cout << "'" << passphrase << "'\n";
return false;
});
::PEP_STATUS status = cache.api(api_test1, Adapter::session(), "23", bytes, n, (::stringlist_t *)NULL);
3 years ago
assert(status == PEP_WRONG_PASSPHRASE);
status = cache.api(api_test2, Adapter::session(), n, str, bytes, sl);
3 years ago
assert(status == PEP_STATUS_OK);
3 years ago
cache.add("hello");
cache.add("world");
std::cout << "expected: two passphrases in reverse order\n";
PassphraseCache _cache = cache;
3 years ago
try {
while (1) {
3 years ago
std::cout << "'" << _cache.latest_passphrase(_cache) << "'\n";
3 years ago
}
} catch (std::underflow_error &) {
}
passphrase_cache.add("hello");
passphrase_cache.add("world");
std::cout << "two times PEP_STATUS_OK (0), one time PEP_WRONG_PASSPHRASE (2561)\n";
3 years ago
do {
status = PassphraseCache::config_next_passphrase();
std::cout << status_to_string(status) << " (" << status << ")\n";
3 years ago
} while (status == PEP_STATUS_OK);
sleep(2);
std::cout << "expected: no passphrase\n";
cache.for_each_passphrase([&](std::string passphrase) {
std::cout << "'" << passphrase << "'\n";
return false;
});
status = cache.api(api_test1, Adapter::session(), str, bytes, n, sl);
3 years ago
assert(status == PEP_WRONG_PASSPHRASE);
status = cache.api(api_test2, Adapter::session(), 23, str, bytes, sl);
3 years ago
assert(status == PEP_STATUS_OK);
Adapter::session.release();
return 0;
}