DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Untracked file

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#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;
}