108 lines
2.8 KiB
C++
108 lines
2.8 KiB
C++
|
|
// Copyright (c) 2013-2015 IONU Security, Inc. All rights reserved.
|
||
|
|
|
||
|
|
// Test program for Eye Container class and C interface
|
||
|
|
#include <cstdio>
|
||
|
|
#include <cstring>
|
||
|
|
#include <sys/stat.h>
|
||
|
|
#include <unistd.h>
|
||
|
|
|
||
|
|
#include <fstream>
|
||
|
|
#include <string>
|
||
|
|
|
||
|
|
#include "eyelog.h"
|
||
|
|
#include "eyelock.h"
|
||
|
|
#include "eyeutils.h"
|
||
|
|
|
||
|
|
using namespace std;
|
||
|
|
|
||
|
|
|
||
|
|
static const char* cionu = "test/ctest.ionu";
|
||
|
|
|
||
|
|
int testfailed (const char* inf, const char* msg)
|
||
|
|
{
|
||
|
|
printf ("FAILED: %s: %s\n", inf, msg);
|
||
|
|
return 1;
|
||
|
|
}
|
||
|
|
|
||
|
|
void filedump (const char* filename)
|
||
|
|
{
|
||
|
|
ifstream file (filename, ios::in | ios::binary | ios::ate);
|
||
|
|
if (file.is_open())
|
||
|
|
{
|
||
|
|
size_t bytes = file.tellg();
|
||
|
|
file.seekg (0, ios::beg);
|
||
|
|
char* big = new char[bytes + 1];
|
||
|
|
if (big) {
|
||
|
|
file.read (big, bytes);
|
||
|
|
file.close();
|
||
|
|
big[bytes] = '\0';
|
||
|
|
printf ("%s\n", big);
|
||
|
|
delete[] big;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
bool filecompare (const char* f1, const char* f2)
|
||
|
|
{
|
||
|
|
std::string f1md5 = ionu::DigestFile ("md5", f1);
|
||
|
|
std::string f2md5 = ionu::DigestFile ("md5", f2);
|
||
|
|
return f1md5 == f2md5;
|
||
|
|
}
|
||
|
|
|
||
|
|
int locktestfailed (const char* inf, const char* msg)
|
||
|
|
{
|
||
|
|
printf ("FAILED: %s: %s\n", inf, msg);
|
||
|
|
filedump ("test/ctest.ionu.lk");
|
||
|
|
return 1;
|
||
|
|
}
|
||
|
|
|
||
|
|
void* IONULockTests(void*)
|
||
|
|
{
|
||
|
|
pthread_t ptid = pthread_self();
|
||
|
|
long threadId = 0;
|
||
|
|
int locks = 0;
|
||
|
|
memcpy(&threadId, &ptid, std::min(sizeof(threadId), sizeof(ptid)));
|
||
|
|
char tid[16];
|
||
|
|
sprintf (tid, "%ld", threadId);
|
||
|
|
IONU_FILE_LOCK_STATUS st;
|
||
|
|
for (int i = 0; i < 100; ++i) {
|
||
|
|
IONUTRACE ("%d lock %d", ptid, i);
|
||
|
|
st = ionu::LockFile (cionu, tid, i & 1 ? IONU_FILE_LOCK_WRITE : IONU_FILE_LOCK_READ); // My write lock
|
||
|
|
if (st == IONU_FILE_LOCK_ERROR)
|
||
|
|
locktestfailed ("LockFile() read/write lock", cionu);
|
||
|
|
else if (st == IONU_FILE_LOCK_OK)
|
||
|
|
locks++;
|
||
|
|
if (st != IONU_FILE_LOCK_TIMEOUT) {
|
||
|
|
st = ionu::LockFile (cionu, tid, IONU_FILE_LOCK_CLEAR); // Clear my lock
|
||
|
|
if (st != IONU_FILE_LOCK_OK)
|
||
|
|
locktestfailed ("LockFile() clear lock", cionu);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
IONUTRACE ("Thread %s got W lock %d times\n", tid, locks);
|
||
|
|
}
|
||
|
|
|
||
|
|
int main ()
|
||
|
|
{
|
||
|
|
remove ("test/mylog.log");
|
||
|
|
IONUCONFIGFILELOG ("thread", "urn:sl:000000:17C38E5A:C60D:79916D07C42C - Nexus-7", EyeLog::IONU_TRACE, "test/mylog.log", EyeLog::IONU_FILE, "%m");
|
||
|
|
|
||
|
|
pthread_t t1;
|
||
|
|
pthread_t t2;
|
||
|
|
pthread_t t3;
|
||
|
|
pthread_t t4;
|
||
|
|
pthread_t t5;
|
||
|
|
pthread_t t6;
|
||
|
|
pthread_t t7;
|
||
|
|
|
||
|
|
pthread_create(&t1, NULL, &IONULockTests, NULL);
|
||
|
|
pthread_create(&t2, NULL, &IONULockTests, NULL);
|
||
|
|
pthread_create(&t3, NULL, &IONULockTests, NULL);
|
||
|
|
pthread_create(&t4, NULL, &IONULockTests, NULL);
|
||
|
|
pthread_create(&t5, NULL, &IONULockTests, NULL);
|
||
|
|
pthread_create(&t6, NULL, &IONULockTests, NULL);
|
||
|
|
pthread_create(&t7, NULL, &IONULockTests, NULL);
|
||
|
|
sleep (1);
|
||
|
|
IONUCLOSELOG();
|
||
|
|
}
|
||
|
|
|