/*
 *               Copyright (C) 2005, 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, without the prior written consent of BroadOn Communications Corp.
 *
 */
#include <iostypes.h>
#include <ios.h>
#include <iop.h>
#include <ioslibc.h>
#include <priority.h>
#include <addr_map.h>

#define STACK_SIZE    2048
const u8        _initStack[STACK_SIZE];
const u32       _initStackSize = sizeof(_initStack);
const u32       _initPriority =  20;

/* Must fail */
static u32
__tstAddrs[] = {
    KERNEL_DATA_START,
    KERNEL_DATA_END,
    CRYPTO_SP_START,
    CRYPTO_SP_END,
    FS_SP_START,
    FS_SP_END,
    FS_VIRT_START,
    FS_VIRT_START + FS_SP_END - FS_SP_START,
    DI_SP_START,
    DI_SP_END,
    DI_VIRT_START,
    DI_VIRT_START + DI_SP_END - DI_SP_START,
    ES_SP_START,
    ES_SP_END,
    ES_VIRT_START,
    ES_VIRT_START + ES_SP_END - ES_SP_START
};

u8 thdStack[STACK_SIZE];
void
thd(u32 arg)
{
    u32 val = 0xcafebabe;
    IOS_DestroyThread(0, &val);
} 

static IOSError
__joinTest(void **val)
{
    u32 id;
    id = IOS_CreateThread((IOSEntryProc)thd, 0, thdStack+STACK_SIZE, STACK_SIZE, _initPriority, IOS_THREAD_CREATE_JOINABLE);
    IOS_StartThread(id);
    IOS_YieldThread();
    return IOS_JoinThread(id, val);
}

void
main(u32 arg)
{
    u32 size, i;
    void *val;
    IOSError rv;
    u32 addr1;
    u16 addr2;

    usleep(1000);
    printf("Testing IOS_GetOSVersion:\n");
    size = sizeof(__tstAddrs)/sizeof(__tstAddrs[0]);

    /* Bad 1st parameter */
    printf("Testing IOS_GetOSVersion (bad 1st parameter):\n");
    for (i = 0; i < size; i += 2) {
        u32 addr = __tstAddrs[i];
        u32 len = __tstAddrs[i + 1] - __tstAddrs[i];
        IOS_GetOSVersion((u32 *)addr, &addr2);
        IOS_GetOSVersion((u32 *)(addr + len/2), &addr2);
        IOS_GetOSVersion((u32 *)(addr + len - 8), &addr2);
    }
    /* Bad 2nd parameter */
    printf("Testing IOS_GetOSVersion (bad 2nd parameter):\n");
    for (i = 0; i < size; i += 2) {
        u32 addr = __tstAddrs[i];
        u32 len = __tstAddrs[i + 1] - __tstAddrs[i];
        IOS_GetOSVersion(&addr1, (u16*) (addr + 4));
        IOS_GetOSVersion(&addr1, (u16*) (addr + len/2 + 4));
        IOS_GetOSVersion(&addr1, (u16*) (addr + len - 4));
    }
    /* Bad 1st parameter */
    printf("Testing IOS_GetBootVersion (bad 1st parameter):\n");
    for (i = 0; i < size; i += 2) {
        u32 addr = __tstAddrs[i];
        u32 len = __tstAddrs[i + 1] - __tstAddrs[i];
        IOS_GetBootVersion((u32 *)addr, &addr2);
        IOS_GetBootVersion((u32 *)(addr + len/2), &addr2);
        IOS_GetBootVersion((u32 *)(addr + len - 8), &addr2);
    }
    /* Bad 2nd parameter */
    printf("Testing IOS_GetBootVersion (bad 2nd parameter):\n");
    for (i = 0; i < size; i += 2) {
        u32 addr = __tstAddrs[i];
        u32 len = __tstAddrs[i + 1] - __tstAddrs[i];
        IOS_GetBootVersion(&addr1, (u16*) (addr + 4));
        IOS_GetBootVersion(&addr1, (u16*) (addr + len/2 + 4));
        IOS_GetBootVersion(&addr1, (u16*) (addr + len - 4));
    }

    printf("IOS_JoinThread with null value (should work): ");
    rv = __joinTest(NULL);
    if (rv < 0) {
        printf("FAILED: %d\n", rv);
        goto out;
    } else {
        printf("PASSED\n");
    }
    printf("IOS_JoinThread with valid value (should work): ");
    rv = __joinTest(&val);
    if (rv < 0) {
        printf("FAILED: %d\n", rv);
        goto out;
    } else {
        printf("PASSED\n");
    }
    printf("IOS_JoinThread with bad values (should fail):\n");
    for (i = 0; i < size; i += 2) {
        u32 len = __tstAddrs[i + 1] - __tstAddrs[i];

        rv = __joinTest((void **)__tstAddrs[i]);
        if (rv >= 0) {
            printf("0x%x: FAILED\n", __tstAddrs[i], rv);
            goto out;
        }

        rv = __joinTest((void **)(__tstAddrs[i] + len/2));
        if (rv >= 0) {
            printf("0x%x: FAILED\n", __tstAddrs[i] + len/2, rv);
            goto out;
        }

        rv = __joinTest((void **)(__tstAddrs[i] + len - 4));
        if (rv >= 0) {
            printf("0x%x: FAILED\n", __tstAddrs[i] + len - 4, rv);
            goto out;
        }
    }
    printf("Testing IOS_CreateMessageQueue with bad addresses (should fail)\n");
    for (i = 0; i < size; i += 2) {
        rv = IOS_CreateMessageQueue((IOSMessage *)__tstAddrs[i], 4);
        if (rv != IOS_ERROR_ACCESS) {
            printf("0x%x failed: %d\n", __tstAddrs[i], rv);
            goto out;
        }
    }
    {
        IOSMessage msg[4];
        IOSMessageQueueId qid;
        printf("Testing IOS_ReceiveMessage with bad addresses (should fail)\n");
        qid = IOS_CreateMessageQueue(msg, sizeof(msg)/sizeof(IOSMessage));
        if (qid < 0) {
            printf("Creating test message queue failed. FAILED.\n");
            goto out;
        }
        for (i = 0; i < size; i += 2) {
            rv = IOS_ReceiveMessage(qid, (IOSMessage *)__tstAddrs[i], IOS_MESSAGE_NOBLOCK);
            if (rv != IOS_ERROR_ACCESS) {
                printf("0x%x failed: %d\n", __tstAddrs[i], rv);
                goto out;
            }
        }
    }
    printf("PASSED\n");

out: 
    while (1) { usleep(10000); }
}
