diff --git a/mozilla/tools/testy/Makefile.in b/mozilla/tools/testy/Makefile.in new file mode 100644 index 00000000000..b7d02fef0b4 --- /dev/null +++ b/mozilla/tools/testy/Makefile.in @@ -0,0 +1,35 @@ +DEPTH = ../.. +topsrcdir = @top_srcdir@ +srcdir = @srcdir@ +VPATH = @srcdir@ + +LIBRARY_NAME = tstysupt + +include $(DEPTH)/config/autoconf.mk + +MODULE = testy + +REQUIRES = \ + $(NULL) + +TESTCPPSRCS = Testy.cpp + +CPPSRCS = \ + TestySupport.cpp \ + $(TESTCPPSRCS) \ + $(NULL) + +SIMPLE_PROGRAMS = $(TESTCPPSRCS:.cpp=$(BIN_SUFFIX)) + +EXTRA_DSO_LDOPTS = \ + $(NSPR_LIBS) \ + $(NULL) + +LIBS = \ + tstysupt.lib \ + $(NSPR_LIBS) \ + $(NULL) + +include $(topsrcdir)/config/config.mk + +include $(topsrcdir)/config/rules.mk diff --git a/mozilla/tools/testy/Testy.cpp b/mozilla/tools/testy/Testy.cpp new file mode 100644 index 00000000000..b87ab7f2a1a --- /dev/null +++ b/mozilla/tools/testy/Testy.cpp @@ -0,0 +1,112 @@ +#include +#include +#include + +#include "prlink.h" +#include "prio.h" +#include "plstr.h" + +#include "TestySupport.h" + +#define LOG(args) printf args + +#ifdef XP_WIN +static const char kPathSep = '\\'; +#else +static const char kPathSep = '/'; +#endif +static const char kTestsDirectory[] = "tch_tests"; + +typedef int (* EntryPoint)(void); +static const char kInitMethod[] = "Testy_Init"; +static const char kTestMethod[] = "Testy_RunTest"; +static const char kShutdownMethod[] = "Testy_Shutdown"; + +static void +ProcessModule(const char *modulesDir, const char *fileName) +{ + int dLen = strlen(modulesDir); + int fLen = strlen(fileName); + + char *buf = (char *) malloc(dLen + 1 + fLen + 1); + memcpy(buf, modulesDir, dLen); + buf[dLen] = kPathSep; + memcpy(buf + dLen + 1, fileName, fLen); + buf[dLen + 1 + fLen] = '\0'; + + PRLibrary *lib = PR_LoadLibrary(buf); + if (lib) { + EntryPoint initFunc = (EntryPoint) PR_FindFunctionSymbol(lib, kInitMethod); + EntryPoint testFunc = (EntryPoint) PR_FindFunctionSymbol(lib, kTestMethod); + EntryPoint shutdownFunc = (EntryPoint) PR_FindFunctionSymbol(lib, kShutdownMethod); + + if (testFunc) { + int rv = 0; + if (initFunc) + rv = initFunc(); + // don't run test case if init fails. + if (rv == 0) + testFunc(); + if (shutdownFunc) + shutdownFunc(); + } + PR_UnloadLibrary(lib); + } + + free(buf); +} + +static void +RunTests(const char *exePath) +{ + if (!(exePath && *exePath)) + return; + + // + // load test modules + // + char *p = strrchr(exePath, kPathSep); + if (p == NULL) { + LOG(("unexpected exe path\n")); + return; + } + + int baseLen = p - exePath; + int finalLen = baseLen + 1 + sizeof(kTestsDirectory); + + // build full path to ipc modules + char *modulesDir = (char*) malloc(finalLen); + memcpy(modulesDir, exePath, baseLen); + modulesDir[baseLen] = kPathSep; + memcpy(modulesDir + baseLen + 1, kTestsDirectory, sizeof(kTestsDirectory)); + + LOG(("loading libraries in %s\n", modulesDir)); + // + // scan directory for IPC modules + // + PRDir *dir = PR_OpenDir(modulesDir); + if (dir) { + PRDirEntry *ent; + while ((ent = PR_ReadDir(dir, PR_SKIP_BOTH)) != NULL) { + // + // locate extension, and check if dynamic library + // + char *p = strrchr(ent->name, '.'); + if (p && PL_strcasecmp(p, MOZ_DLL_SUFFIX) == 0) + ProcessModule(modulesDir, ent->name); + } + PR_CloseDir(dir); + } + + free(modulesDir); +} + +int main(int argc, char **argv) +{ + Testy_LogInit("tch.log"); + + RunTests(argv[0]); + + Testy_LogShutdown(); + return 0; +} diff --git a/mozilla/tools/testy/TestySupport.cpp b/mozilla/tools/testy/TestySupport.cpp new file mode 100644 index 00000000000..54bf414d6ba --- /dev/null +++ b/mozilla/tools/testy/TestySupport.cpp @@ -0,0 +1,49 @@ +#include "nspr.h" +#include "TestySupport.h" + +FILE* gLogFile = NULL; + +int Testy_LogInit(const char* fileName) +{ + gLogFile = fopen(fileName, "r+b"); + if (!gLogFile) return -1; + return 0; +} + +void Testy_LogShutdown() +{ + if (gLogFile) + fclose(gLogFile); +} + + +void Testy_LogStart(const char* name) +{ + PR_ASSERT(gLogFile); + fprintf(gLogFile, "Test Case: %s", name); + fflush(gLogFile); +} + +void Testy_LogComment(const char* name, const char* comment) +{ + PR_ASSERT(gLogFile); + fprintf(gLogFile, "Test Case: %s\n\t%s", name, comment); + fflush(gLogFile); +} + +void Testy_LogEnd(const char* name, PRBool passed) +{ + PR_ASSERT(gLogFile); + fprintf(gLogFile, "Test Case: %s (%s)", name, passed ? "Passed" : "Failed"); + fflush(gLogFile); +} + +void Testy_GenericStartup() +{ + +} + +void Testy_GenericShutdown() +{ + +} diff --git a/mozilla/tools/testy/TestySupport.h b/mozilla/tools/testy/TestySupport.h new file mode 100644 index 00000000000..c0c465c2c73 --- /dev/null +++ b/mozilla/tools/testy/TestySupport.h @@ -0,0 +1,10 @@ +PR_BEGIN_EXTERN_C + +PR_EXPORT(int) Testy_LogInit(const char* fileName); +PR_EXPORT(void) Testy_LogShutdown(); + +PR_EXPORT(void) Testy_LogStart(const char* name); +PR_EXPORT(void) Testy_LogComment(const char* name, const char* comment); +PR_EXPORT(void) Testy_LogEnd(const char* name, PRBool passed); + +PR_END_EXTERN_C diff --git a/mozilla/tools/testy/sample/Makefile.in b/mozilla/tools/testy/sample/Makefile.in new file mode 100644 index 00000000000..a5a831a4236 --- /dev/null +++ b/mozilla/tools/testy/sample/Makefile.in @@ -0,0 +1,28 @@ +DEPTH = ../../.. +topsrcdir = @top_srcdir@ +srcdir = @srcdir@ +VPATH = @srcdir@ + +LIBRARY_NAME = SimpleTest + +include $(DEPTH)/config/autoconf.mk + +MODULE = testy +REQUIRES = \ + $(NULL) + +LOCAL_INCLUDES = \ + -I$(srcdir)/.. \ + $(NULL) + +CPPSRCS = \ + SimpleTest.cpp \ + $(NULL) + +EXTRA_DSO_LDOPTS = \ + ../tstysupt.lib \ + $(NSPR_LIBS) \ + $(NULL) + +include $(topsrcdir)/config/config.mk +include $(topsrcdir)/config/rules.mk diff --git a/mozilla/tools/testy/sample/SimpleTest.cpp b/mozilla/tools/testy/sample/SimpleTest.cpp new file mode 100644 index 00000000000..a7b372c409c --- /dev/null +++ b/mozilla/tools/testy/sample/SimpleTest.cpp @@ -0,0 +1,39 @@ + +#include "nspr.h" +#include "TestySupport.h" + +PR_BEGIN_EXTERN_C +PR_EXPORT(int) Testy_Init(); +PR_EXPORT(int) Testy_RunTest(); +PR_EXPORT(int) Testy_Shutdown(); +PR_END_EXTERN_C + + +int Testy_Init() +{ + return 0; +} + + +int Testy_RunTest() +{ + const char* testCaseName1 = "Simple Test 1"; + + Testy_LogStart(testCaseName1); + Testy_LogComment(testCaseName1, "Comment 1"); + Testy_LogEnd(testCaseName1, true); + + const char* testCaseName2 = "Simple Test 2"; + + Testy_LogStart(testCaseName2); + Testy_LogComment(testCaseName2, "Comment 2"); + Testy_LogEnd(testCaseName2, false); + + return 0; +} + +int Testy_Shutdown() +{ + return 0; +} +