/*
 *               Copyright (C) 2007, BroadOn Communications Corp.
 *
 *  These coded instructions, statements, and computer programs contain
 *  unpublished proprietary information of BroadOn Communications Corp.,
 *  and are protected by federal copyright law. They may not be disclosed
 *  to third parties or copied or duplicated in any form, in whole or in
 *  part, withend the prior written consent of BroadOn Communications Corp.
 *
 */


#include "ectest.h"
#include "ectest_utils.h"


/*
 * TODO: if we port this test to other platforms later, remember to check
 * the cache alignment restrictions on those platforms
 */


ECTestLog ecTestLog;
ECTestSleep ecTestSleep;


ECError
ecTest(ECTestLog testLog, ECTestSleep testSleep, const ECNameValue *initArgs, u32 nInitArgs, const char *urls[5], ECTestEntry *testEntries, u32 nTestEntries)
{
    ECError rv = EC_ERROR_OK, rv_shutdown = EC_ERROR_OK;
    u32 i, logFailed = 0;

    ecTestLog = testLog;
    ecTestSleep = testSleep;

    rv = EC_Init((ECNameValue *) initArgs, nInitArgs);
    if (rv != EC_ERROR_OK) {
        ecTestLog("EC_Init() failed, rv=%d\n", rv);
        logFailed = 1;
        goto end;
    }
    ecTestLog("EC_Init() succeeded\n");

    rv = EC_GetVersion();
    ecTestLog("EC library version is %d\n", rv);

    rv = EC_SetWebSvcUrls(urls[0], urls[1], urls[2]);
    if (rv != EC_ERROR_OK) {
        ecTestLog("EC_SetWebSvcUrls() returned %d\n", rv);
        logFailed = 1;
        goto shutdown;
    }
    ecTestLog("EC_SetWebSvcUrls() succeeded\n");

    rv = EC_SetContentUrls(urls[3], urls[4]);
    if (rv != EC_ERROR_OK) {
        ecTestLog("EC_SetContentUrls() returned %d\n", rv);
        logFailed = 1;
        goto shutdown;
    }
    ecTestLog("EC_SetContentUrls() succeeded\n"); 

    /* Run the tests */
    for (i = 0; i < nTestEntries; i++) {
        rv = testEntries[i].testFunc(testEntries[i].testParam);
        if (rv != EC_ERROR_OK) {
            goto shutdown;
        }
    }
    ecTestLog("All tests passed\n");

shutdown:
    rv_shutdown = EC_Shutdown();
    if (rv_shutdown != EC_ERROR_OK) {
        ecTestLog("EC_Shutdown() failed, rv=%d\n", rv_shutdown);
        logFailed = 1;
        goto end;
    }
    ecTestLog("EC_Shutdown() succeeded\n");

end:
    if (logFailed) {
        TUL_LogFail(EC_TEST, "Failed to (de-)initialize test");
    }

    if (rv == EC_ERROR_OK && rv_shutdown == EC_ERROR_OK) {
        ecTestLog("EC test succeeded\n");
    } else {
        ecTestLog("EC test failed, rv=%d, rv_shutdown=%d\n", rv, rv_shutdown);
        if (rv == EC_ERROR_OK) {
            rv = rv_shutdown;
        }
    }

    return rv;
}

