Commit ce1b9de7 authored by ale's avatar ale

quick test for thread safety of the main SSO API

parent c8c2bb63
......@@ -5,6 +5,7 @@ check_PROGRAMS = \
string_utils_unittest \
sso_unittest \
ticket_unittest \
thread_safety_test \
signer_unittest \
verifier_unittest
......@@ -24,3 +25,5 @@ sso_unittest_SOURCES = sso_unittest.cc
signer_unittest_SOURCES = signer_unittest.cc
verifier_unittest_SOURCES = verifier_unittest.cc
thread_safety_test_SOURCES = thread_safety_test.cc
#include <pthread.h>
#include <gtest/gtest.h>
#include "sso.h"
#include "test_common.h"
#define NCHECKS 100
#define NTHREADS 10
namespace {
struct TestContext {
sso::Signer *signer;
sso::Verifier *verifier;
};
static void* test_signer(void *ctxptr) {
TestContext *ctx = reinterpret_cast<TestContext *>(ctxptr);
sso::Ticket t("user1", "service", "domain");
std::string signed_ticket = ctx->signer->sign(&t);
for (int i = 0; i < NCHECKS; i++) {
try {
sso::Ticket *t2 = ctx->verifier->verify(signed_ticket);
delete t2;
} catch(...) {
return (void *)1;
}
}
return NULL;
}
// Test that Signer and Verifier are thread safe.
TEST(ThreadSafetyTest, RunParallel) {
std::string public_key, secret_key;
sso::generate_keys(public_key, secret_key);
sso::Signer signer(secret_key);
sso::Verifier verifier(public_key, "service", "domain");
TestContext ctx;
ctx.signer = &signer;
ctx.verifier = &verifier;
pthread_t threads[NTHREADS];
for (int i = 0; i < NTHREADS; i++) {
pthread_create(&threads[i], NULL, test_signer, &ctx);
}
for (int i = 0; i < NTHREADS; i++) {
void *retval;
pthread_join(threads[i], &retval);
EXPECT_EQ(NULL, retval);
}
}
} // namespace
int main(int argc, char **argv) {
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment