Example #1
0
// test MapHSOrd
int testMapHSOrd() {
	info("- Testing MapHSOrd<struct>-----");

	bool r;

	MapHSOrd map;

	// fill the array
	info("  * Filling the map with items.");
	unsigned int k0[] = { 1, 2 };
	map.set(k0, 2, 100);
	unsigned int k1[] = { 1, 3 };
	map.set(k1, 2, 101);
	unsigned int k2[] = { 1, 4 };
	map.set(k2, 2, 102);
	unsigned int k3[] = { 1, 5 };
	map.set(k3, 2, 103);

	// test the values
	r = true;
	unsigned int w;
	r &= (map.lookup(k0, 2, w) && w == 100);
	r &= (map.lookup(k1, 2, w) && w == 101);
	r &= (map.lookup(k2, 2, w) && w == 102);
	r &= (map.lookup(k3, 2, w) && w == 103);
	if (!testPrint(r, "  * Checking if all values were inserted correctly", true))
		return -1;

	// non-existent item
	unsigned int nk[] = { 100, 100 };
	r = (map.lookup(nk, 2, w));
	if (!testPrint(r, "  * Checking non-existent item", false))
		return -1;

	// replace existing item
	map.set(k3, 2, 104);
	r = (map.lookup(k3, 2, w) && w == 104);
	if (!testPrint(r, "  * Replacing existing item", true))
		return -1;

	// deleting item
	map.remove(k3, 2);
	r = (map.lookup(k3, 2, w));
	if (!testPrint(r, "  * Deleting item", false))
		return -1;

	// free
	map.remove_all();

	return 0;
}
Example #2
0
File: cSerie1.c Project: pas/ra
int main(int argc, const char * argv[]) {
    initialize();
    testPrint(create_rtype_hex(FC_ADD, 0x0000, I_T0, I_T1, I_T2, OC_ADD));
    testPrint(create_itype_hex(0x0001, I_T0, I_ZERO, OC_ADDI));
    testPrint(create_jtype_hex(0xCD1234, OC_J));
    testPrint(create_itype_hex(0xBBBB, I_T0, I_ZERO, OC_LUI));
    testPrint(create_itype_hex(0xA03B, I_T0, I_T1, OC_LW));
    testPrint(create_itype_hex(0x0101, I_T0, I_T0, OC_ORI));
    testPrint(create_rtype_hex(FC_SUB, 0x0002, I_T0, I_T1, I_T2, OC_SUB));
    testPrint(create_itype_hex(0xD070, I_T0, I_T1, OC_SW));
    testPrint(create_specialtype_hex(OC_STOP));
    return 0;
}
Example #3
0
File: main.c Project: aahud/harvey
void
main(uint32_t mbmagic, uint32_t mbaddress)
{
	void testPrint(void);
	
	while (1)
		testPrint();
}
Example #4
0
// test MapHS
int testMapHS() {
	info("- Testing MapHS<struct>-----");

	bool r;

	MapHS map;

	// fill the array
	info("  * Filling the map with items.");
	map.set((uint8_t *) "a", 1, 100);
	map.set((uint8_t *) "ab", 2, 101);
	map.set((uint8_t *) "abc", 3, 102);
	map.set((uint8_t *) "abcd", 4, 103);

	// test the values
	r = true;
	unsigned int w;
	r &= (map.lookup((uint8_t *) "a", 1, w) && w == 100);
	r &= (map.lookup((uint8_t *) "ab", 2, w) && w == 101);
	r &= (map.lookup((uint8_t *) "abc", 3, w) && w == 102);
	r &= (map.lookup((uint8_t *) "abcd", 4, w) && w == 103);
	if (!testPrint(r, "  * Checking if all values were inserted correctly", true))
		return -1;

	// non-existent item
	r = (map.lookup((uint8_t *) "xyz", 3, w));
	if (!testPrint(r, "  * Checking non-existent item", false))
		return -1;

	// replace existing item
	map.set((uint8_t *) "abcd", 4, 104);
	r = (map.lookup((uint8_t *) "abcd", 4, w) && w == 104);
	if (!testPrint(r, "  * Replacing existing item", true))
		return -1;

	// deleting item
	map.remove((uint8_t *) "abcd", 4);
	r = (map.lookup((uint8_t *) "abcd", 4, w));
	if (!testPrint(r, "  * Deleting item", false))
		return -1;

	// free
	map.remove_all();

	return 0;
}
Example #5
0
int main() {
    printf("=== %s ===\n", __FILE__);

    CGAppState__init(__FILE__);
    startRule = BBScannerRuleset__getInstance(); /* so that we initialize the token types */

    testNewDelete();
    testPrint();
    testGetters();

    CGAppState__deInit();

    printf("=== %s ok ===\n", __FILE__);
	return 0;
}
Example #6
0
int 
main(int argc, char **argv)
{
  testApply();
  testFree();
  testNew();
  testNewCopy();
  testFill();
  testGetSet();
  testIncrementDecrement();
  testPrint();
  testPrintHeader();
  testResize();

  UnitTest_report();
}
Example #7
0
int main()
{
// Setup system clock
	initializeClock();	// Will need the clock for all parts

// Part 1: KDS Timer Interrupts and RGB PWM
	// Setup timers
	setupTPM();

#ifdef PART3
	dmaSetup();	// If doing Part 3, setup DMA Controller
#endif
	// Run profiler on memory functions
	testMemory();

	// Run profiler on printf functions
	testPrint();


    return 0;
}
Example #8
0
int main()
{
	printf("I'm alive!\n");
	drawing* d = (drawing*)malloc(sizeof(drawing));

	d->canvas = makeArray(CHRCX, CHRCY);
	drawString(d,"ABC",3,5);

	rotateCw(d);

	testPrint(d);

	printf("\n\n\n");
	testPrintBoard();

	insertDrawing(d);

	printf("\n\n\n");
	testPrintBoard();


	//system("Pause");//needed for C++
	return 0;
}
Example #9
0
int testJudyArrayInt() {
	info("- Testing JudyArray<int>-----");
	JudyArray<int> int_array;
	bool r;
	unsigned int idx;

	// fill the array
	info("  * Filling the array with numbers.");
	for (int i = 0; i < 100; i++)
		int_array.set(i, i + 100);

	// test the values
	r = true;
	for (int i = 0; i < 100; i++)
		r &= int_array.get(i) == (i + 100);
	if (!testPrint(r, "  * Checking if all values were inserted correctly", true))
		return -1;

	// overwriting an existing item (checking memory leaks)
	int_array.set(0, 100);
	if (!testPrint(int_array[0] == 100, "  * Replacing existing value", true))
		return -1;

	// test [] operator
	int_array[1000] = 999;
	if (!testPrint(int_array[1000] == 999, "  * Setting value by [] operator", true))
		return -1;

	int_array[1000] = 1100;
	if (!testPrint(int_array[1000] == 1100, "  * Replacing existing value by [] operator", true))
		return -1;

	idx = int_array.by_count(101);
	if (!testPrint(int_array[1000] == int_array[idx], "  * Testing item 101th item", true))
		return -1;

	//
	info("  * Testing iterators.");
	r = true;
	for (unsigned int i = int_array.first(); i != INVALID_IDX; i = int_array.next(i))
		r &= int_array.get(i) == (int) (i + 100);
	if (!testPrint(r, "    - Forward iteration", true))
		return -1;

	r = true;
	for (unsigned int i = int_array.last(); i != INVALID_IDX; i = int_array.prev(i))
		r &= int_array.get(i) == (int) (i + 100);
	if (!testPrint(r, "    - Backward iteration", true))
		return -1;

	idx = int_array.add(2000);
	if (!testPrint(int_array[idx] == 2000, "  * Adding an item at the end of the array", true))
		return -1;

	// remove
	int_array.remove(idx);
	if (!testPrint(!int_array.exists(idx), "  * Removing an item from the array", true))
		return -1;

	// MEMORY
	unsigned int mem_used;

	// mem used
	mem_used = int_array.mem_used();
	if (!testPrint(mem_used > 2, "  * Used memory should be nonzero", true))
		return -1;

	// free memory
	int_array.remove_all();
	testPrint(true, "  * Freeing memory", true);

	mem_used = int_array.mem_used();
	if (!testPrint(mem_used == 0, "  * Used memory should be zero", true))
		return -1;

	return 0;
}
Example #10
0
int testJudyArrayPtrStruct() {
	info("- Testing JudyArrayPtr<struct>-----");

	JudyArrayPtr<Point> ptr_array;
	bool r;
	unsigned int idx;

	// fill the array
	info("  * Filling the array with items.");
	for (int i = 0; i < 100; i++) {
		Point *pt = new Point(100 + i, 1000 + i);
		ptr_array.set(i, pt);
	}

	// test the values
	r = true;
	for (int i = 0; i < 100; i++) {
		Point *pt = ptr_array.get(i);
		r &= (pt->X == i + 100) && (pt->Y == i + 1000);
	}

	if (!testPrint(r, "  * Checking if all values were inserted correctly", true))
		return -1;

	// test [] operator
	ptr_array[1000] = new Point(999, 9998);
	if (!testPrint((ptr_array[1000]->X == 999) && (ptr_array[1000]->Y == 9998), "  * Setting value by [] operator", true))
		return -1;

	delete ptr_array.get(1000);
	ptr_array[1000] = new Point(1100, 2000);
	if (!testPrint((ptr_array[1000]->X == 1100) && (ptr_array[1000]->Y == 2000), "  * Replacing existing value by [] operator", true))
		return -1;

	idx = ptr_array.by_count(101);
	if (!testPrint(ptr_array[1000] == ptr_array[idx], "  * Testing item 101th item", true))
		return -1;

	//
	info("  * Testing iterators.");
	r = true;
	for (unsigned int i = ptr_array.first(); i != INVALID_IDX; i = ptr_array.next(i)) {
		Point *pt = ptr_array.get(i);
		r &= (pt->X == (int) (i + 100)) && (pt->Y == (int) (i + 1000));
	}
	if (!testPrint(r, "    - Forward iteration", true))
		return -1;

	r = true;
	for (unsigned int i = ptr_array.last(); i != INVALID_IDX; i = ptr_array.prev(i)) {
		Point *pt = ptr_array.get(i);
		r &= (pt->X == (int) (i + 100)) && (pt->Y == (int) (i + 1000));
	}
	if (!testPrint(r, "    - Backward iteration", true))
		return -1;

	//
	idx = ptr_array.add(new Point(2000, 3000));
	if (!testPrint((ptr_array[idx]->X == 2000) && (ptr_array[idx]->Y == 3000), "  * Adding an item at the end of the array", true))
		return -1;

	// remove
	delete ptr_array.get(idx);
	ptr_array.remove(idx);
	if (!testPrint(!ptr_array.exists(idx), "  * Removing an item from the array", true))
		return -1;

	// MEMORY
	unsigned int mem_used;

	// mem used
	mem_used = ptr_array.mem_used();
	if (!testPrint(mem_used > 2, "  * Used memory should be nonzero", true))
		return -1;

	// free memory
	for (unsigned int i = ptr_array.first(); i != INVALID_IDX; i = ptr_array.next(i))
		delete ptr_array.get(i);

	ptr_array.remove_all();
	testPrint(true, "  * Freeing memory", true);

	mem_used = ptr_array.mem_used();
	if (!testPrint(mem_used == 0, "  * Used memory should be zero", true))
		return -1;


	return 0;
}
Example #11
0
thread test_netaddr(bool verbose)
{
#if NETHER
    bool passed = TRUE;
    struct netaddr a;
    struct netaddr b;

    /* Setup structures */
    a.type = NETADDR_ETHERNET;
    a.len = ETH_ADDR_LEN;
    a.addr[0] = 0xAA;
    a.addr[1] = 0xBB;
    a.addr[2] = 0xCC;
    a.addr[3] = 0xDD;
    a.addr[4] = 0xEE;
    a.addr[5] = 0xFF;

    b.type = NETADDR_ETHERNET;
    b.len = ETH_ADDR_LEN;
    b.addr[0] = 0xAA;
    b.addr[1] = 0xBB;
    b.addr[2] = 0xCC;
    b.addr[3] = 0xDD;
    b.addr[4] = 0xEE;
    b.addr[5] = 0xFF;

    testPrint(verbose, "Comparison (diff lengths)");
    b.len = ETH_ADDR_LEN - 1;
    failif((TRUE == netaddrequal(&a, &b)), "");

    testPrint(verbose, "Comparison (diff types)");
    b.len = ETH_ADDR_LEN;
    a.type = NETADDR_IPv4;
    failif((TRUE == netaddrequal(&a, &b)), "");

    testPrint(verbose, "Comparison (equal MACs)");
    a.type = NETADDR_ETHERNET;
    failif((FALSE == netaddrequal(&a, &b)), "");

    testPrint(verbose, "Comparison (diff MACs)");
    b.addr[5] = 0x00;
    failif((TRUE == netaddrequal(&a, &b)), "");

    /* Setup structures */
    a.type = NETADDR_IPv4;
    a.len = IPv4_ADDR_LEN;
    a.addr[0] = 192;
    a.addr[1] = 168;
    a.addr[2] = 1;
    a.addr[3] = 1;

    b.type = NETADDR_IPv4;
    b.len = IPv4_ADDR_LEN;
    b.addr[0] = 192;
    b.addr[1] = 168;
    b.addr[2] = 1;
    b.addr[3] = 1;

    testPrint(verbose, "Comparison (equal IPs)");
    failif((FALSE == netaddrequal(&a, &b)), "");

    testPrint(verbose, "Comparison (diff IPs)");
    a.addr[3] = 2;
    failif((TRUE == netaddrequal(&a, &b)), "");

    /* always print out the overall tests status */
    if (passed)
    {
        testPass(TRUE, "");
    }
    else
    {
        testFail(TRUE, "");
    }

#else /* NETHER */
    testSkip(TRUE, "");
#endif /* NETHER == 0 */
    return OK;
}
Example #12
0
/**
 * Tests the stdlib.h header in the Xinu Standard Library.
 * @return OK when testing is complete
 */
thread test_libStdlib(bool verbose)
{
    int i;
    char list[10] = "BCADFEHGI";
    bool passed = TRUE;

    /* atoi */
    testPrint(verbose, "ASCII to integer");
    failif((-456 != atoi("-456"))
           || (123 != atoi("+123"))
           || (2147483647 != atoi("2147483647"))
           || (-1 != atoi("-1one"))
           || (2 != atoi("+2two"))
           || (3 != atoi("3three"))
           || (0 != atoi("-on1e"))
           || (0 != atoi("+tw2o")) || (0 != atoi("thre3e")), "");

    /* atol */
    testPrint(verbose, "ASCII to long");
    failif((-456 != atol("-456"))
           || (123 != atol("+123"))
           || (2147483647 != atol("2147483647"))
           || (-1 != atol("-1one"))
           || (2 != atol("+2two"))
           || (3 != atol("3three"))
           || (0 != atol("-one"))
           || (0 != atol("+two")) || (0 != atol("three")), "");

    /* qsort */
    testPrint(verbose, "Quick sort");
    qsort(list, sizeof(list) - 1, 1, (void *)qsort_callback);
    failif((0 != strncmp(list, "ABCDEFGHI", sizeof(list))), "");

    /* bzero */
    testPrint(verbose, "bzero");
    bzero(list, sizeof(list));
    for (i = 0; i < sizeof(list); i++)
    {
        if ('\0' != *(list + i))
        {
            testFail(verbose, "");
            passed = FALSE;
            break;
        }
    }
    if (i == sizeof(list))
    {
        testPass(verbose, "");
    }

    /* abs */
    testPrint(verbose, "Absolute value");
    failif((2147483647 != abs(-2147483647))
           || (123 != abs(123)) || (0 != abs(0)), "");

    /* labs */
    testPrint(verbose, "Long absolute value");
    failif((2147483647 != labs(-2147483647))
           || (123 != labs(123)) || (0 != labs(0)), "");

    /* rand */
    testPrint(verbose, "Random number generation");
    failif((rand() == rand())
           && (rand() == rand())
           && (rand() == rand())
           && (rand() == rand()), "that was unlikely");

    /* malloc (in test_umemory.c) */

    if (passed)
    {
        testPass(TRUE, "");
    }
    else
    {
        testFail(TRUE, "");
    }

    return OK;
}
Example #13
0
/**
 * Test for snoop.
 */
thread test_snoop(bool verbose)
{
    bool passed = TRUE;
    struct snoop cap;
    struct netaddr dst;
    struct netaddr src;
    struct netaddr mask;
    struct netif *netptr;
    struct pcap_file_header pcap;
    struct pcap_pkthdr phdr;
    struct packet *pktA;
    struct packet *pktB;
    uchar *data;
    int i;
    uint nmatch;

    src.len = IPv4_ADDR_LEN;
    src.type = NETADDR_IPv4;
    dst.len = IPv4_ADDR_LEN;
    dst.type = NETADDR_IPv4;
    mask.len = IPv4_ADDR_LEN;
    mask.type = NETADDR_IPv4;

    src.addr[0] = 192;
    src.addr[1] = 168;
    src.addr[2] = 1;
    src.addr[3] = 6;

    dst.addr[0] = 192;
    dst.addr[1] = 168;
    dst.addr[2] = 1;
    dst.addr[3] = 1;

    /* Initialization */
    testPrint(verbose, "Test case initialization");
    pktA = netGetbuf();
    failif((SYSERR == (int)pktA), "Failed get buffer");
    if (!passed)
    {
        testFail(TRUE, "");
        return OK;
    }

    /* Test filter */

    /* Filter type */
    testPrint(verbose, "Filter type");
    bzero(&cap, sizeof(struct snoop));
    cap.caplen = USHRT_MAX;
    cap.type = SNOOP_FILTER_ARP;
    failif((7 != filterTest(&cap, pktA)), "");

    /* Test open */
    testPrint(verbose, "Open capture (bad params)");
    bzero(&cap, sizeof(struct snoop));
    failif((SYSERR != snoopOpen(NULL, NULL)), "");

    testPrint(verbose, "Open capture (bad device)");
    failif((SYSERR != snoopOpen(&cap, "crap")), "");

    testPrint(verbose, "Open capture all (no netif)");
    failif((SYSERR != snoopOpen(&cap, "ALL")), "");

    src.addr[0] = 192;
    src.addr[1] = 168;
    src.addr[2] = 1;
    src.addr[3] = 6;

    dst.addr[0] = 192;
    dst.addr[1] = 168;
    dst.addr[2] = 1;
    dst.addr[3] = 1;

    mask.addr[0] = 192;
    mask.addr[1] = 168;
    mask.addr[2] = 1;
    mask.addr[3] = 1;

    open(ELOOP);
    netUp(ELOOP, &src, &dst, &mask);

    testPrint(verbose, "Open capture all");
    if (SYSERR == snoopOpen(&cap, "ALL"))
    {
        failif(TRUE, "Returned SYSERR");
    }
    else
    {
        for (i = 0; i < NNETIF; i++)
        {
            if ((NET_ALLOC == netiftab[i].state)
                && (NULL == netiftab[i].capture))
            {
                break;
            }
        }
        failif((i < NNETIF), "Not attached to all");
    }

    testPrint(verbose, "Close capture (bad params)");
    failif((SYSERR != snoopClose(NULL)), "");

    testPrint(verbose, "Close capture");
    if (SYSERR == snoopClose(&cap))
    {
        failif(TRUE, "Returned SYSERR");
    }
    else
    {
        for (i = 0; i < NNETIF; i++)
        {
            if (&cap == netiftab[i].capture)
            {
                break;
            }
        }
        failif((i < NNETIF), "Not removed from all");
    }
    testPrint(verbose, "Open capture on ELOOP");
    bzero(&cap, sizeof(struct snoop));
    netptr = NULL;
    if (SYSERR == snoopOpen(&cap, "ELOOP"))
    {
        failif(TRUE, "Returned SYSERR");
    }
    else
    {
        for (i = 0; i < NNETIF; i++)
        {
            if (ELOOP == netiftab[i].dev)
            {
                netptr = &netiftab[i];
                break;
            }
        }
        failif(((NULL == netptr) || (&cap != netptr->capture)),
               "Not attached to ELOOP");
    }

    testPrint(verbose, "Capture (bad params)");
    failif((SYSERR != snoopCapture(NULL, NULL)), "");

    /* Reset data stream to beginning of PCAP file */
    data = (uchar *)(&_binary_data_testsnoop_pcap_start);
    memcpy(&pcap, data, sizeof(pcap));
    data += sizeof(pcap);
    memcpy(&phdr, data, sizeof(phdr));
    data += sizeof(phdr);
    if (PCAP_MAGIC != pcap.magic)
    {
        phdr.caplen = endswap(phdr.caplen);
    }

    testPrint(verbose, "Capture no match");
    memcpy(pktA->data, data, phdr.caplen);
    pktA->len = phdr.caplen;
    pktA->nif = netptr;
    pktA->curr = pktA->data;
    cap.caplen = USHRT_MAX;
    cap.type = SNOOP_FILTER_IPv4;
    failif(((SYSERR == snoopCapture(&cap, pktA))
            || (0 != cap.nmatch) || (mailboxCount(cap.queue) > 0)), "");

    testPrint(verbose, "Capture match");
    cap.type = SNOOP_FILTER_ALL;
    nmatch = cap.nmatch;
    if (SYSERR == snoopCapture(&cap, pktA))
    {
        failif(TRUE, "Returned SYSERR");
    }
    else if (1 != cap.nmatch)
    {
        failif(TRUE, "Packet did not match");
    }
    else if (mailboxCount(cap.queue) != 1)
    {
        failif(TRUE, "Packet not enqueued");
    }
    else
    {
        pktB = (struct packet *)mailboxReceive(cap.queue);
        failif((0 !=
                memcmp(pktB->data, pktA->data, phdr.caplen)),
               "Dequeued packet doesn't match");
    }

    testPrint(verbose, "Capture overrun");
    cap.type = SNOOP_FILTER_ALL;
    for (i = 0; i < SNOOP_QLEN; i++)
    {
        if (SYSERR == snoopCapture(&cap, pktA))
        {
            break;
        }
    }
    if (i < SNOOP_QLEN)
    {
        failif(TRUE, "Returned SYSERR");
    }
    else
    {
        failif(((SYSERR != snoopCapture(&cap, pktA)) || (1 != cap.novrn)),
               "Packet did not overrun");
    }

    testPrint(verbose, "Close capture");
    failif((SYSERR == snoopClose(&cap)), "Returned SYSERR");

    /* TODO: RESUME HERE */

    netDown(ELOOP);
    close(ELOOP);

    /* always print out the overall tests status */
    if (passed)
    {
        testPass(TRUE, "");
    }
    else
    {
        testFail(TRUE, "");
    }

    return OK;
}
Example #14
0
thread test_bufpool(bool verbose)
{
    bool passed = TRUE;
    int id, i;
    void *pbuf;
    void *chain[TBUFNUM];
    irqmask im;
    ulong memsize;
    char datums[] = { "abcdefghijklmnopqrstuvwxyz1234567" };

    /* Create buffer pool */
    testPrint(verbose, "Create buffer pool");
    im = disable();
    memsize = memlist.length;
    id = bfpalloc(TBUFSIZE, TBUFNUM);
    if (memlist.length !=
        memsize - ((TBUFSIZE + sizeof(struct poolbuf)) * TBUFNUM))
    {
        restore(im);
        testFail(verbose,
                 "\nmemlist.length reduction does not match pool size");
        return OK;
    }
    else
    {
        restore(im);
        testPass(verbose, "");
    }

    /* Allocate single buffer */
    testPrint(verbose, "Allocate single buffer");
    pbuf = bufget(id);
    if (SYSERR == (ulong)pbuf)
    {
        passed = FALSE;
        testFail(verbose, "\nbufget() returns SYSERR");
    }
    else
    {
        testPass(verbose, "");
        /* Return single buffer */
        testPrint(verbose, "Return single buffer");
        if (SYSERR == buffree(pbuf))
        {
            passed = FALSE;
            testFail(verbose, "\nbuffree() returns SYSERR");
        }
        else
        {
            testPass(verbose, "");
        }
    }

    /* Allocate and fill all buffers in pool */
    testPrint(verbose, "Allocate and fill all buffers");
    for (i = 0; i < TBUFNUM; i++)
    {
        pbuf = bufget(id);
        chain[i] = pbuf;
        if (SYSERR == (ulong)pbuf)
        {
            break;
        }
        memcpy(pbuf, datums, TBUFSIZE);
    }
    if (TBUFNUM != i)
    {
        passed = FALSE;
        testFail(verbose, "\ngetbuf() returns SYSERR");
    }
    else
    {
        for (i = 0; i < TBUFNUM; i++)
        {
            pbuf = chain[i];
            if (0 != memcmp(pbuf, datums, TBUFSIZE))
            {
                break;
            }
        }
        if (TBUFNUM != i)
        {
            passed = FALSE;
            testFail(verbose, "\nbuffer data does not match source");
        }
        else
        {
            testPass(verbose, "");
            /* Free all buffers in pool */
            testPrint(verbose, "Free all buffers");
            for (i = 0; i < TBUFNUM; i++)
            {
                pbuf = chain[i];
                if (SYSERR == buffree(pbuf))
                {
                    break;
                }
            }
            if (TBUFNUM != i)
            {
                passed = FALSE;
                testFail(verbose, "\nbuffree() returns SYSERR");
            }
            else
            {
                testPass(verbose, "");
            }
        }
    }

    /* Release pool */
    testPrint(verbose, "Free buffer pool");
    im = disable();
    memsize = memlist.length;
    bfpfree(id);
    if (memlist.length !=
        memsize + ((TBUFSIZE + sizeof(struct poolbuf)) * TBUFNUM))
    {
        restore(im);
        passed = FALSE;
        testFail(verbose,
                 "\nmemlist.length increase does not match pool size");
    }
    else
    {
        restore(im);
        testPass(verbose, "");
    }

    /* Final report */
    if (TRUE == passed)
    {
        testPass(TRUE, "");
    }
    else
    {
        testFail(TRUE, "");
    }

    return OK;
}
Example #15
0
/**
 * Tests the stdio.h header in the Xinu Standard Library.
 * @return OK when testing is complete
 */
thread test_libStdio(bool verbose)
{
#if LOOP
    char str[50];
    int stdsav;
    ulong ul;
    bool passed = TRUE;

    enable();

    open(LOOP);

    /* fprintf */
    testPrint(verbose, "fprintf");
    fprintf(LOOP, "%d %o %x %c %s", 75, 75, 75, 75, "ABC");
    read(LOOP, str, 15);
    failif((0 != strncmp(str, "75 113 4b K ABC", 15)), "");

    /* printf */
    testPrint(verbose, "printf");
    stdsav = stdout;
    stdout = LOOP;
    printf("%d %o %x %c %s", 75, 75, 75, 75, "ABC");
    stdout = stdsav;
    read(LOOP, str, 15);
    failif((0 != strncmp(str, "75 113 4b K ABC", 15)), "");

    /* sprintf */
    testPrint(verbose, "sprintf");
    sprintf(str, "%d %o %x %c %s", 75, 75, 75, 75, "ABC");
    failif((0 != strncmp(str, "75 113 4b K ABC", 15)), "");

    /* fscanf */

    /* scanf */

    /* sscanf */

    /* fgetc */
    testPrint(verbose, "fgetc");
    putc(LOOP, 'a');
    putc(LOOP, '\n');
    putc(LOOP, 4);
    str[0] = fgetc(LOOP);
    str[1] = fgetc(LOOP);
    str[2] = fgetc(LOOP);

    failif((('a' != str[0]) || ('\n' != str[1]) || (4 != str[2])), "");

    /* fgets */
    testPrint(verbose, "fgets");
    write(LOOP, "Test sentence.\n", 15);
    fgets(str, 20, LOOP);
    failif((0 != strncmp(str, "Test sentence.\n", 15)), "");

    /* fputc */
    testPrint(verbose, "fputc");
    ul = fputc('a', LOOP);
    failif((((ulong)'a' != ul) || ((ulong)'a' != getc(LOOP))), "");

    /* fputs */
    testPrint(verbose, "fputs");
    fputs("Put test.", LOOP);
    read(LOOP, str, 9);
    failif((0 != strncmp(str, "Put test.", 9)), "");

    /* getchar */
    testPrint(verbose, "getchar");
    putc(LOOP, 'a');
    putc(LOOP, '\n');
    putc(LOOP, 4);
    stdsav = stdin;
    stdin = LOOP;
    str[0] = getchar();
    str[1] = getchar();
    str[2] = getchar();
    stdin = stdsav;;
    failif((('a' != str[0]) || ('\n' != str[1]) || (4 != str[2])), "");

    /* putchar */
    testPrint(verbose, "putchar");
    stdsav = stdout;
    stdout = LOOP;
    ul = putchar('a');
    stdout = stdsav;
    failif(((ulong)'a' != ul) || ((int)'a' != getc(LOOP)), "");

    if (passed)
    {
        testPass(TRUE, "");
    }
    else
    {
        testFail(TRUE, "");
    }

    close(LOOP);
#endif                          /* LOOP */
    return OK;
}
Example #16
0
/**
 * Tests UDP
 * @return OK when testing is complete
 */
thread test_udp(bool verbose)
{
#ifdef UDP1
    struct udp *udpptr = NULL;
    ushort pta;
    ushort ptb;
    struct netaddr ipc;
    struct netaddr ipd;
    struct netaddr ipl;
    struct netaddr src;
    struct netaddr dst;
    struct netaddr mask;
    struct netaddr ipzero;
    struct packet *pkt[10];
    struct udpPkt *udppkt;
    struct udpPseudoHdr *pseudo;
    uchar buffera[12];
    uchar bufferb[12];
    uchar bufferc[12];
    uchar bufferd[12];
    uchar bufferp[40];
    bool passed = TRUE;

    /*   struct pcap_pkthdr phdr;
       struct netif *netptr;
       int nproc;
       int i;
       int wait;
       struct packet *pktA;
       uchar buf[100];
       uchar *data;
       uchar *buffer;
     */
    pta = 20000;
    ptb = 30000;

    /* IP address "C" */
    ipc.type = NETADDR_IPv4;
    ipc.len = IPv4_ADDR_LEN;
    ipc.addr[0] = 192;
    ipc.addr[1] = 168;
    ipc.addr[2] = 1;
    ipc.addr[3] = 5;

    /* IP address "D" */
    ipd.type = NETADDR_IPv4;
    ipd.len = IPv4_ADDR_LEN;
    ipd.addr[0] = 192;
    ipd.addr[1] = 168;
    ipd.addr[2] = 1;
    ipd.addr[3] = 7;

    /* "Local" IP address */
    ipl.type = NETADDR_IPv4;
    ipl.len = IPv4_ADDR_LEN;
    ipl.addr[0] = 192;
    ipl.addr[1] = 168;
    ipl.addr[2] = 1;
    ipl.addr[3] = 8;

    /* Source IP address */
    src.type = NETADDR_IPv4;
    src.len = IPv4_ADDR_LEN;
    src.addr[0] = 192;
    src.addr[1] = 168;
    src.addr[2] = 1;
    src.addr[3] = 6;

    /* Destination IP address */
    dst.type = NETADDR_IPv4;
    dst.len = IPv4_ADDR_LEN;
    dst.addr[0] = 192;
    dst.addr[1] = 168;
    dst.addr[2] = 1;
    dst.addr[3] = 1;

    /* Mask */
    mask.type = NETADDR_IPv4;
    mask.len = IPv4_ADDR_LEN;
    mask.addr[0] = 255;
    mask.addr[1] = 255;
    mask.addr[2] = 255;
    mask.addr[3] = 0;

    /* Empty address */
    ipzero.type = 0;
    ipzero.len = 0;
    ipzero.addr[0] = 0;
    ipzero.addr[1] = 0;
    ipzero.addr[2] = 0;
    ipzero.addr[3] = 0;
    ipzero.addr[4] = 0;
    ipzero.addr[5] = 0;

    /* Test udpPkt structure */
    //testPrint(verbose, "Header structure");
    /* TODO: Figure out how this should be done */

    /* Test udpOpen */
    testPrint(verbose, "Open UDP devices (NULL local port)");
    if (SYSERR == open(UDP0, &ipl, &ipd, NULL, ptb))
    {
        failif(TRUE, "");
    }

    if (SYSERR == open(UDP1, &ipl, &ipc, NULL, pta))
    {
        failif(TRUE, "");
    }
    failif((udptab[0].localpt == udptab[1].localpt)
           || (udptab[0].localpt < UDP_PSTART)
           || (udptab[0].localpt > UDP_PMAX)
           || (udptab[1].localpt < UDP_PSTART)
           || (udptab[1].localpt > UDP_PMAX), "");
    if (SYSERR == close(UDP0))
    {
        failif(TRUE, "");
    }
    if (SYSERR == close(UDP1))
    {
        failif(TRUE, "");
    }

    /* Test udpOpen */
    testPrint(verbose, "Open UDP device (NULL remote port)");
    if (SYSERR == open(UDP0, &ipl, &ipd, pta, NULL))
    {
        failif(TRUE, "");
    }
    failif(udptab[0].remotept != NULL, "");
    if (SYSERR == close(UDP0))
    {
        failif(TRUE, "");
    }

    /* Test udpOpen */
    testPrint(verbose, "Open UDP device (NULL remote ip)");
    if (SYSERR == open(UDP0, &ipl, NULL, pta, ptb))
    {
        failif(TRUE, "");
    }
    failif(0 == netaddrequal(&udptab[0].remoteip, &ipzero), "");
    if (SYSERR == close(UDP0))
    {
        failif(TRUE, "");
    }


    /* Test udpOpen */
    testPrint(verbose, "Open UDP devices (same local port)");
    if (SYSERR == open(UDP0, &ipl, &ipd, pta, ptb))
    {
        failif(TRUE, "");
    }

    if (SYSERR == open(UDP1, &ipl, &ipd, pta, pta))
    {
        failif(TRUE, "");
    }
    failif(udptab[0].localpt != udptab[1].localpt, "");

    /* Only close the second UDP device this time, we want to test close
     * using UDP0 */
    if (SYSERR == close(UDP1))
    {
        failif(TRUE, "");
    }

    /* Test udpClose */
    testPrint(verbose, "Close UDP device");
    if (SYSERR == close(UDP0))
    {
        failif(TRUE, "");
    }
    failif((udptab[0].dev != 0) || (udptab[0].icount != 0)
           || (udptab[0].istart != 0) || (udptab[0].isem != 0)
           || (udptab[0].localpt != 0) || (udptab[0].remotept != 0)
           || (FALSE == netaddrequal(&udptab[0].localip, &ipzero))
           || (FALSE == netaddrequal(&udptab[0].remoteip, &ipzero))
           || (udptab[0].state != 0) || (udptab[0].flags != 0), "");

    /* Test udpControl */
    testPrint(verbose, "UDP Control: Binding");
    /* Open UDP device to resume testing of that device */
    if (SYSERR == open(UDP0, &ipl, NULL, NULL, NULL))
    {
        failif(TRUE, "");
    }
    control(UDP0, UDP_CTRL_ACCEPT, pta, (long)&ipl);
    control(UDP0, UDP_CTRL_BIND, ptb, (long)&ipc);
    failif((udptab[0].localpt != pta)
           || (FALSE == netaddrequal(&udptab[0].localip, &ipl))
           || (udptab[0].remotept != ptb)
           || (FALSE == netaddrequal(&udptab[0].remoteip, &ipc)), "");

    /* Test udpControl */
    testPrint(verbose, "UDP Control: Flags");
    control(UDP0, UDP_CTRL_SETFLAG, UDP_FLAG_PASSIVE | UDP_FLAG_NOBLOCK
            | UDP_FLAG_BINDFIRST, NULL);
    control(UDP0, UDP_CTRL_CLRFLAG, UDP_FLAG_NOBLOCK, NULL);
    /* At this point NOBLOCK should be the only flag that is off */
    failif((FALSE == (udptab[0].flags & UDP_FLAG_PASSIVE))
           || (udptab[0].flags & UDP_FLAG_NOBLOCK)
           || (FALSE == (udptab[0].flags & UDP_FLAG_BINDFIRST)), "");

    /* Test udpDemux */
    testPrint(verbose, "UDP Demux (2 sockets)");
    open(UDP1, &ipl, NULL, pta, NULL);
    udpptr = udpDemux(pta, ptb, &ipl, &ipc);
    failif((udpptr == &udptab[1]) || (NULL == udpptr), "");

    /* Test udpRecv and udpRead */
    testPrint(verbose, "Receive and read UDP packets");

    control(UDP0, UDP_CTRL_CLRFLAG, UDP_FLAG_PASSIVE
            | UDP_FLAG_BINDFIRST, NULL);

    control(UDP0, UDP_CTRL_ACCEPT, pta, (long)&ipl);
    control(UDP0, UDP_CTRL_BIND, ptb, (long)&ipc);

    pkt[0] = makePkt(ptb, pta, &ipl, &ipc, 5, "ABCDE");
    pkt[1] = makePkt(ptb, pta, &ipl, &ipc, 4, "FGHI");
    pkt[2] = makePkt(ptb, pta, &ipl, &ipc, 3, "JKL");
    pkt[3] = makePkt(ptb, pta, &ipl, &ipc, 2, "MN");
    if (SYSERR == udpRecv(pkt[0], &ipc, &ipl))
    {
        failif(TRUE, "recva");
    }
    if (SYSERR == udpRecv(pkt[1], &ipc, &ipl))
    {
        failif(TRUE, "recvb");
    }
    if (SYSERR == udpRecv(pkt[2], &ipc, &ipl))
    {
        failif(TRUE, "recvc");
    }
    if (SYSERR == udpRecv(pkt[3], &ipc, &ipl))
    {
        failif(TRUE, "recvd");
    }
    if (SYSERR == read(UDP0, buffera, 5))
    {
        failif(TRUE, "reada");
    }
    if (SYSERR == read(UDP0, bufferb, 5))
    {
        failif(TRUE, "readb");
    }
    if (SYSERR == read(UDP0, bufferc, 5))
    {
        failif(TRUE, "readc");
    }
    if (SYSERR == read(UDP0, bufferd, 5))
    {
        failif(TRUE, "readd");
    }
    failif((0 != strncmp((char *)buffera, "ABCDE", 5))
           || (0 != strncmp((char *)bufferb, "FGHI", 4))
           || (0 != strncmp((char *)bufferc, "JKL", 3))
           || (0 != strncmp((char *)bufferd, "MN", 2)), "");

    /* Test udpRecv and udpRead */
    testPrint(verbose, "Two UDP sockets");
    pkt[0] = makePkt(pta, pta, &ipc, &ipl, 9, "ABCDEFGHI");
    udpRecv(pkt[0], &ipc, &ipl);
    read(UDP1, bufferc, 9);
    failif(0 != strncmp((char *)bufferc, "ABCDEFGHI", 9), "");

    /* Test udpRecv and udpRead */
    testPrint(verbose, "Receive and read UDP packets (again)");

    control(UDP0, UDP_CTRL_CLRFLAG, UDP_FLAG_PASSIVE
            | UDP_FLAG_BINDFIRST, NULL);

    control(UDP0, UDP_CTRL_ACCEPT, pta, (long)&ipl);
    control(UDP0, UDP_CTRL_BIND, ptb, (long)&ipc);

    pkt[0] = makePkt(ptb, pta, &ipl, &ipc, 5, "OPQRS");
    pkt[1] = makePkt(ptb, pta, &ipl, &ipc, 3, "TUV");
    pkt[2] = makePkt(ptb, pta, &ipl, &ipc, 2, "WX");
    pkt[3] = makePkt(ptb, pta, &ipl, &ipc, 2, "YZ");
    udpRecv(pkt[0], &ipc, &ipl);
    udpRecv(pkt[1], &ipc, &ipl);
    udpRecv(pkt[2], &ipc, &ipl);
    udpRecv(pkt[3], &ipc, &ipl);
    if (SYSERR == read(UDP0, buffera, 5))
    {
        failif(TRUE, "reada");
    }
    if (SYSERR == read(UDP0, bufferb, 3))
    {
        failif(TRUE, "readb");
    }
    if (SYSERR == read(UDP0, bufferc, 2))
    {
        failif(TRUE, "readc");
    }
    if (SYSERR == read(UDP0, bufferd, 2))
    {
        failif(TRUE, "readd");
    }
    failif(0 != strncmp((char *)buffera, "OPQRS", 5)
           || (0 != strncmp((char *)bufferb, "TUV", 3))
           || (0 != strncmp((char *)bufferc, "WX", 2))
           || (0 != strncmp((char *)bufferd, "YZ", 2)), "");

    /* Test udpRecv and udpRead on multiple sockets */
    testPrint(verbose, "Recv/read with varying sockets (1)");

    /* Test 1 */
    control(UDP0, UDP_CTRL_ACCEPT, pta, (long)&ipl);
    control(UDP0, UDP_CTRL_BIND, NULL, NULL);
    pkt[0] = makePkt(ptb, pta, &ipc, &ipl, 5, "test1");
    udpRecv(pkt[0], &ipc, &ipl);
    read(UDP0, buffera, 5);
    failif(strncmp((char *)buffera, "test1", 5), "");

    /* Test 2 */
    testPrint(verbose, "Recv/read socket test 2");
    control(UDP0, UDP_CTRL_ACCEPT, pta, (long)&ipl);
    control(UDP0, UDP_CTRL_BIND, ptb, NULL);

    control(UDP1, UDP_CTRL_ACCEPT, pta, (long)&ipl);
    control(UDP1, UDP_CTRL_BIND, 0, NULL);

    pkt[0] = makePkt(ptb, pta, &ipc, &ipl, 6, "test2a");
    pkt[1] = makePkt(pta, pta, &ipc, &ipl, 6, "test2b");
    udpRecv(pkt[0], &ipc, &ipl);
    udpRecv(pkt[1], &ipc, &ipl);
    read(UDP0, buffera, 6);
    read(UDP1, bufferb, 6);

    failif(0 != strncmp((char *)buffera, "test2a", 6)
           || (0 != strncmp((char *)bufferb, "test2b", 6)), "");

    /* Test 3 */
    testPrint(verbose, "Recv/read socket test 3");
    control(UDP0, UDP_CTRL_BIND, ptb, (long)&ipc);
    control(UDP1, UDP_CTRL_BIND, ptb, NULL);

    pkt[0] = makePkt(ptb, pta, &ipc, &ipl, 6, "test3a");
    pkt[1] = makePkt(ptb, pta, &ipd, &ipl, 6, "test3b");
    udpRecv(pkt[0], &ipc, &ipl);
    udpRecv(pkt[1], &ipd, &ipl);
    read(UDP0, buffera, 6);
    read(UDP1, bufferb, 6);

    failif((0 != strncmp((char *)buffera, "test3a", 6))
           || (0 != strncmp((char *)bufferb, "test3b", 6)), "");

    /* Test 4 */
    testPrint(verbose, "Recv/read socket test 4");
    control(UDP0, UDP_CTRL_BIND, ptb, (long)&ipc);
    control(UDP1, UDP_CTRL_BIND, ptb, (long)&ipd);

    pkt[0] = makePkt(ptb, pta, &ipc, &ipl, 6, "test4a");
    pkt[1] = makePkt(ptb, pta, &ipd, &ipl, 6, "test4b");
    udpRecv(pkt[0], &ipc, &ipl);
    udpRecv(pkt[1], &ipd, &ipl);
    read(UDP0, buffera, 6);
    read(UDP1, bufferb, 6);

    failif((0 != strncmp((char *)buffera, "test4a", 6))
           || (0 != strncmp((char *)bufferb, "test4b", 6)), "");

    /* Test 5 */
    testPrint(verbose, "Recv/read socket test 5");
    control(UDP0, UDP_CTRL_BIND, 0, NULL);
    control(UDP1, UDP_CTRL_ACCEPT, ptb, (long)&ipl);
    control(UDP1, UDP_CTRL_BIND, 0, NULL);

    pkt[0] = makePkt(ptb, pta, &ipc, &ipl, 6, "test5a");
    pkt[1] = makePkt(pta, ptb, &ipc, &ipl, 6, "test5b");
    udpRecv(pkt[0], &ipc, &ipl);
    udpRecv(pkt[1], &ipc, &ipl);
    read(UDP0, buffera, 6);
    read(UDP1, bufferb, 6);

    failif((0 != strncmp((char *)buffera, "test5a", 6))
           || (0 != strncmp((char *)bufferb, "test5b", 6)), "");

    /* Read entire UDP packet */
    testPrint(verbose, "Read entire UDP packet");
    control(UDP0, UDP_CTRL_SETFLAG, UDP_FLAG_PASSIVE, NULL);
    pkt[0] = makePkt(ptb, pta, &ipc, &ipl, 7, "passive");
    udpRecv(pkt[0], &ipc, &ipl);
    read(UDP0, bufferp, UDP_HDR_LEN + 7 + sizeof(struct udpPseudoHdr));
    pseudo = (struct udpPseudoHdr *)bufferp;
    udppkt = (struct udpPkt *)(pseudo + 1);
    failif((0 != memcmp(pseudo->srcIp, ipc.addr, IPv4_ADDR_LEN))
           || (0 != memcmp(pseudo->dstIp, ipl.addr, IPv4_ADDR_LEN))
           || (udppkt->srcPort != ptb) || (udppkt->dstPort != pta)
           || (udppkt->len != UDP_HDR_LEN + 7)
           || (0 != strncmp((char *)udppkt->data, "passive", 7)), "");

    /* Done testing, attempt to close all UDP devices (MAKE SURE BOTH
     * DEVICES ARE OPEN BEFORE YOU CLOSE THEM!!!) */
    close(UDP0);
    close(UDP1);

    /* Print out the overall test's status (pass or fail) */
    if (passed)
    {
        testPass(TRUE, "");
    }
    else
    {
        testFail(TRUE, "");
    }
#else /* UDP1 */
    testSkip(TRUE, "");
#endif /* !UDP1 */
    return OK;
}
Example #17
0
thread test_ip(bool verbose)
{
#if NETHER
    struct netaddr dst;
    struct netaddr src;
    struct netaddr mask;
    struct netif *netptr;
    struct pcap_file_header pcap;
    struct pcap_pkthdr phdr;
    struct packet *pktA;
    struct packet *pktB;
    uchar *data;
    uchar buf[500];
    int i;
    int nproc;
    int wait;
    bool passed = TRUE;

    src.type = NETADDR_IPv4;
    src.len = IPv4_ADDR_LEN;
    dst.type = NETADDR_IPv4;
    dst.len = IPv4_ADDR_LEN;
    mask.type = NETADDR_IPv4;
    mask.len = IPv4_ADDR_LEN;

    src.addr[0] = 192;
    src.addr[1] = 168;
    src.addr[2] = 1;
    src.addr[3] = 6;

    dst.addr[0] = 192;
    dst.addr[1] = 168;
    dst.addr[2] = 1;
    dst.addr[3] = 1;

    mask.addr[0] = 255;
    mask.addr[1] = 255;
    mask.addr[2] = 255;
    mask.addr[3] = 0;

    testPrint(verbose, "Initialization");
    sleep(500);
    if (SYSERR == open(ELOOP))
    {
        failif(TRUE, "Open returned SYSERR");
    }
    else
    {
        if (SYSERR == netUp(ELOOP, &src, &mask, &dst))
        {
            failif(TRUE, "netUp returned SYSERR");
        }
        else
        {
            netptr = NULL;
            for (i = 0; i < NNETIF; i++)
            {
                if (ELOOP == netiftab[i].dev)
                {
                    netptr = &netiftab[i];
                    break;
                }
            }
            pktA = netGetbuf();
            pktB = netGetbuf();
            failif(((NULL == netptr)
                    || (SYSERR == (int)pktA)
                    || (SYSERR == (int)pktB)),
                   "Buffer allocation failed");
        }
    }
    if (!passed)
    {
        testFail(TRUE, "");
        return OK;
    }

    data = (uchar *)(&_binary_data_testip_pcap_start);
    memcpy(&pcap, data, sizeof(pcap));
    data += sizeof(pcap);

    testPrint(verbose, "Ipv4Send");
    /* Get 1st Packet */
    memcpy(&phdr, data, sizeof(phdr));
    data += sizeof(phdr);
    if (PCAP_MAGIC != pcap.magic)
    {
        phdr.caplen = endswap(phdr.caplen);
    }
    nproc = netptr->nproc;
    write(ELOOP, data, phdr.caplen);
    wait = 0;
    while ((wait < MAX_WAIT) && (netptr->nproc == nproc))
    {
        wait++;
        sleep(10);
    }
    if (MAX_WAIT == wait)
    {
        failif(TRUE, "Wait time expired");
    }
    else
    {
        /* Get 2nd Packet */
        data += phdr.caplen;
        memcpy(&phdr, data, sizeof(phdr));
        data += sizeof(phdr);
        if (PCAP_MAGIC != pcap.magic)
        {
            phdr.caplen = endswap(phdr.caplen);
        }
        memcpy(pktA->data, data, phdr.caplen);
        pktA->len = phdr.caplen;
        pktA->nif = netptr;
        pktA->linkhdr = pktA->data;
        pktA->nethdr = pktA->linkhdr + ETH_HDR_LEN;
        pktA->curr = pktA->nethdr + IPv4_HDR_LEN;

        pktB->curr -= UDP_HDR_LEN + 4;
        pktB->len += UDP_HDR_LEN + 4;
        memcpy(pktB->curr, pktA->curr, pktB->len);

        control(ELOOP, ELOOP_CTRL_SETFLAG, ELOOP_FLAG_HOLDNXT, NULL);

        if (OK != ipv4Send(pktB, &src, &dst, IPv4_PROTO_UDP))
        {
            failif(TRUE, "ipv4Send didn't return okay");
        }
        else
        {
            control(ELOOP, ELOOP_CTRL_GETHOLD, (long)buf, 500);
            failif(0 != memcmp(buf, pktA->linkhdr, pktA->len), "");
        }
    }

    /* ipv4Recv Testing */
    //TODO: Finish ipv4Recv
/*	testPrint(verbose, "ipv4Recv");
	pktA->curr -= IPv4_HDR_LEN;

	if (SYSERR == rawOpen(RAW0, IPv4_PROTO_UDP, ))
    {
        failif(TRUE, "Open returned SYSERR");
    }

	

	if (OK != ipv4Recv(pktA))
	{
		failif(TRUE, "Didn't return okay");
	}
	else
	{
	}
*/
    netDown(ELOOP);
    close(ELOOP);

    /* always print out the overall tests status */
    if (passed)
    {
        testPass(TRUE, "");
    }
    else
    {
        testFail(TRUE, "");
    }
#else /* NETHER */
    testSkip(TRUE, "");
#endif /* NETHER == 0 */
    return OK;
}
Example #18
0
// test bit array
int testBitJudyArray() {
	BitJudyArray bit_array;
	bool r;
	unsigned int count;
	unsigned int index, value;
	unsigned int mem_used;

	info("- Testing BitJudyArray -----");

	// first set a value in the empty array
	r = bit_array.set(1);
	if (!testPrint(r, "  * Putting a non-existent value (1) into the array", true))
		return -1;

	// put the same value again (it should exist)
	r = bit_array.set(1);
	if (!testPrint(r, "  * Putting the value that already exist in the array", false))
		return -1;

	// test the value
	r = bit_array.is_set(1);
	if (!testPrint(r, "  * Testing if the value exist in the array", true))
		return -1;

	// set next value
	r = bit_array.set(3);
	if (!testPrint(r, "  * Putting another value (3) into the array", true))
		return -1;

	// test the count of items in the array
	count = bit_array.count();
	if (!testPrint(count == 2, "  * Number of values in the array", true))
		return -1;

	// n-th index
	value = bit_array.by_count(2);
	if (!testPrint(value == 3, "  * The value at position 2 should be 3", true))
		return -1;

	// put one more value into the array
	r = bit_array.set(2);
	if (!testPrint(r, "  * Putting one more value (2) into the array", true))
		return -1;

	// ITERATION

	// test the first index
	index = bit_array.first();
	if (!testPrint(index == 1, "  * The first index in the array should be one (1)", true))
		return -1;

	index = bit_array.next(index);
	if (!testPrint(index == 2, "  * The next index in the array should be two (2)", true))
		return -1;

	index = bit_array.last();
	if (!testPrint(index == 3, "  * The last index in the array should be two (3)", true))
		return -1;

	index = bit_array.prev(index);
	if (!testPrint(index == 2, "  * The previous index in the array should be two (2)", true))
		return -1;

	// COPY
	BitJudyArray dup;
	r = dup.copy(&bit_array);
	if (!testPrint(r, "  * Making copy of the array", true))
		return -1;

	// test copied values
	value = bit_array.by_count(1);
	if (!testPrint(value == 1, "    - The value at position 1 should be 1", true))
		return -1;

	value = bit_array.by_count(2);
	if (!testPrint(value == 2, "    - The value at position 2 should be 3", true))
		return -1;

	value = bit_array.by_count(3);
	if (!testPrint(value == 3, "    - The value at position 3 should be 3", true))
		return -1;

	// MEMORY

	// mem used
	mem_used = bit_array.mem_used();
	if (!testPrint(mem_used > 2, "  * Used memory should be nonzero", true))
		return -1;

	// free memory
	bit_array.free();
	testPrint(true, "  * Freeing memory", true);

	mem_used = bit_array.mem_used();
	if (!testPrint(mem_used == 0, "  * Used memory should be zero", true))
		return -1;

	return 0;
}
Example #19
0
/**
 * Tests raw sockets.
 * @return OK when testing is complete
 */
thread test_raw(bool verbose)
{
#if RAW0
    /* the failif macro depends on 'passed' and 'verbose' vars */
    bool passed = TRUE;
    device *devptr;
    struct raw *rawptr;
    struct netif *netptr;
    struct netaddr lip;
    struct netaddr rip;
    struct netaddr mask;
    struct packet *pkt;
    struct packet *pktA;
    struct pcap_pkthdr phdr;
    struct pcap_file_header pcap;
    uchar *data;
    uchar buf[500];
    int nproc;
    int wait;
    int i;

    lip.type = NETADDR_IPv4;
    lip.len = IPv4_ADDR_LEN;
    lip.addr[0] = 192;
    lip.addr[1] = 168;
    lip.addr[2] = 1;
    lip.addr[3] = 6;

    rip.type = NETADDR_IPv4;
    rip.len = IPv4_ADDR_LEN;
    rip.addr[0] = 192;
    rip.addr[1] = 168;
    rip.addr[2] = 1;
    rip.addr[3] = 1;

    mask.type = NETADDR_IPv4;
    mask.len = IPv4_ADDR_LEN;
    mask.addr[0] = 255;
    mask.addr[1] = 255;
    mask.addr[2] = 255;
    mask.addr[3] = 0;

    /* Initialization */
    testPrint(verbose, "Test case initialization");
    data = (uchar *)(&_binary_data_testraw_pcap_start);
    memcpy(&pcap, data, sizeof(pcap));
    data += sizeof(pcap);
    if (SYSERR == open(ELOOP))
    {
        failif(TRUE, "");
    }
    else
    {
        if (SYSERR == netUp(ELOOP, &lip, &mask, NULL))
        {
            close(ELOOP);
            failif(TRUE, "");
        }
        else
        {
            netptr = NULL;
            for (i = 0; i < NNETIF; i++)
            {
                if (ELOOP == netiftab[i].dev)
                {
                    netptr = &netiftab[i];
                    break;
                }
            }
            pkt = netGetbuf();
            failif(((NULL == netptr) || (SYSERR == (int)pkt)), "");
        }
    }
    if (!passed)
    {
        testFail(TRUE, "");
        return OK;
    }

    /* Test Open */
    testPrint(verbose, "Open RAW (Proto Only)");
    rawptr = NULL;
    if (SYSERR == open(RAW0, NULL, NULL, IPv4_PROTO_ICMP))
    {
        failif(TRUE, "Returned SYSERR");
    }
    else
    {
        devptr = (device *)&devtab[RAW0];
        rawptr = &rawtab[devptr->minor];
        failif(((rawptr->state != RAW_ALLOC)
                || (rawptr->dev != devptr)
                || (rawptr->proto != IPv4_PROTO_ICMP)
                || (rawptr->localip.type != NULL)
                || (rawptr->remoteip.type != NULL)
                || (rawptr->icount != 0)
                || (rawptr->istart != 0)
                || (semcount(rawptr->isema) != 0)
                || (rawptr->flags != 0)), "Incorrect control block");
    }

    testPrint(verbose, "Open RAW (Already Open)");
    failif((SYSERR != open(RAW0, NULL, NULL, IPv4_PROTO_ICMP)),
           "Double open() succeeded");

    /* Test Control */
    testPrint(verbose, "Control (Closed Socket)");
    failif((control(RAW1, RAW_CTRL_SETFLAG, NULL, NULL) != SYSERR), "");

    testPrint(verbose, "Control (Bad Params)");
    failif((control(RAW0, -1, NULL, NULL) != SYSERR), "");

    testPrint(verbose, "Control (Set Flag)");
    failif(((NULL != control(RAW0, RAW_CTRL_SETFLAG,
                             (RAW_IACCEPT | RAW_IHDR), NULL))
            || (0 == (rawptr->flags & RAW_IACCEPT))
            || (0 == (rawptr->flags & RAW_IHDR))), "");

    testPrint(verbose, "Control (Clear Flag)");
    failif(((RAW_IHDR != control(RAW0, RAW_CTRL_CLRFLAG,
                                 RAW_IHDR, NULL))
            || (0 == (rawptr->flags & RAW_IACCEPT))
            || (1 == (rawptr->flags & RAW_IHDR))), "");

    /* Test Close */
    testPrint(verbose, "Close RAW");
    if (SYSERR == close(RAW0))
    {
        failif(TRUE, "Returned SYSERR");
    }
    else
    {
        devptr = (device *)&devtab[RAW0];
        rawptr = &rawtab[devptr->minor];
        failif(((rawptr->state != RAW_FREE)
                || (rawptr->dev != NULL)), "Control block not clear");
    }

    testPrint(verbose, "Close RAW (Already Closed)");
    failif((SYSERR != close(RAW0)), "Did not SYSERR");

    /* Test demux */
    testPrint(verbose, "Demulitplexing (No sockets)");
    failif((NULL != rawDemux(&rip, &lip, IPv4_PROTO_ICMP)), "");

    testPrint(verbose, "Demulitplexing (All Protos)");
    if ((SYSERR == open(RAW0, &lip, &rip, IPv4_PROTO_IGMP))
        || (SYSERR == open(RAW1, NULL, NULL, NULL)))
    {
        failif(TRUE, "Open failed");
    }
    else
    {
        devptr = (device *)&devtab[RAW1];
        rawptr = &rawtab[devptr->minor];
        if (rawptr != rawDemux(&rip, &lip, IPv4_PROTO_ICMP))
        {
            failif(TRUE, "Incorrect socket");
        }
        else
        {
            failif(((SYSERR == close(RAW0)) || (SYSERR == close(RAW1))),
                   "Close failed");
        }
    }

    testPrint(verbose, "Demulitplexing (Proto)");
    if ((SYSERR == open(RAW0, NULL, NULL, IPv4_PROTO_ICMP))
        || (SYSERR == open(RAW1, NULL, NULL, IPv4_PROTO_IGMP)))
    {
        failif(TRUE, "Open failed");
    }
    else
    {
        devptr = (device *)&devtab[RAW0];
        rawptr = &rawtab[devptr->minor];
        if (rawptr != rawDemux(&rip, &lip, IPv4_PROTO_ICMP))
        {
            failif(TRUE, "Incorrect socket");
        }
        else
        {
            failif(((SYSERR == close(RAW0)) || (SYSERR == close(RAW1))),
                   "Close failed");
        }
    }

    testPrint(verbose, "Demulitplexing (Remote IP)");
    if ((SYSERR == open(RAW0, NULL, NULL, IPv4_PROTO_ICMP))
        || (SYSERR == open(RAW1, NULL, &rip, IPv4_PROTO_ICMP)))
    {
        failif(TRUE, "Open failed");
    }
    else
    {
        devptr = (device *)&devtab[RAW1];
        rawptr = &rawtab[devptr->minor];
        if (rawptr != rawDemux(&rip, &lip, IPv4_PROTO_ICMP))
        {
            failif(TRUE, "Incorrect socket");
        }
        else
        {
            failif(((SYSERR == close(RAW0)) || (SYSERR == close(RAW1))),
                   "Close failed");
        }
    }

    testPrint(verbose, "Demulitplexing (Local IP)");
    if ((SYSERR == open(RAW0, NULL, &rip, IPv4_PROTO_ICMP))
        || (SYSERR == open(RAW1, &lip, &rip, IPv4_PROTO_ICMP)))
    {
        failif(TRUE, "Open failed");
    }
    else
    {
        devptr = (device *)&devtab[RAW1];
        rawptr = &rawtab[devptr->minor];
        if (rawptr != rawDemux(&rip, &lip, IPv4_PROTO_ICMP))
        {
            failif(TRUE, "Incorrect socket");
        }
        else
        {
            failif(((SYSERR == close(RAW0)) || (SYSERR == close(RAW1))),
                   "Close failed");
        }
    }

    /* Test Open */
    testPrint(verbose, "Open RAW (Full Spec)");
    if (SYSERR == open(RAW0, &lip, &rip, IPv4_PROTO_ICMP))
    {
        failif(TRUE, "Returned SYSERR");
    }
    else
    {
        devptr = (device *)&devtab[RAW0];
        rawptr = &rawtab[devptr->minor];
        failif(((rawptr->state != RAW_ALLOC)
                || (rawptr->dev != devptr)
                || (rawptr->proto != IPv4_PROTO_ICMP)
                || (FALSE == netaddrequal(&lip, &rawptr->localip))
                || (FALSE == netaddrequal(&rip, &rawptr->remoteip))
                || (rawptr->icount != 0)
                || (rawptr->istart != 0)
                || (semcount(rawptr->isema) != 0)
                || (rawptr->flags != 0)), "Incorrect control block");
    }

    /* Test receive */
    testPrint(verbose, "Receive (Bad Params)");
    /* Get 1st packet */
    memcpy(&phdr, data, sizeof(phdr));
    data += sizeof(phdr);
    if (PCAP_MAGIC != pcap.magic)
    {
        phdr.caplen = endswap(phdr.caplen);
    }
    pkt->len = phdr.caplen;
    memcpy(pkt->data, data, phdr.caplen);
    pkt->linkhdr = pkt->data;
    pkt->nethdr = pkt->linkhdr + ETH_HDR_LEN;
    pkt->curr = pkt->nethdr + IPv4_HDR_LEN;
    failif(((SYSERR != rawRecv(NULL, NULL, NULL, NULL))
            || (SYSERR != rawRecv(pkt, &rip, NULL, NULL))), "");

    testPrint(verbose, "Receive (No Match)");
    pktA = netGetbuf();
    memcpy(pktA, pkt, sizeof(struct packet) + pkt->len);
    pktA->linkhdr = pktA->data;
    pktA->nethdr = pktA->linkhdr + ETH_HDR_LEN;
    pktA->curr = pktA->nethdr + IPv4_HDR_LEN;
    if (SYSERR == rawRecv(pktA, &rip, &lip, IPv4_PROTO_IGMP))
    {
        failif(TRUE, "Returned SYSERR");
    }
    else
    {
        failif(((rawptr->icount != 0)
                || (semcount(rawptr->isema) != 0)
                || (TRUE == netaddrequal(&rawptr->src[0], &rip))
                || (rawptr->in[0] == pktA)), "Packet enqueued");

    }

    testPrint(verbose, "Receive (One Pkt)");
    pktA = netGetbuf();
    memcpy(pktA, pkt, sizeof(struct packet) + pkt->len);
    pktA->linkhdr = pktA->data;
    pktA->nethdr = pktA->linkhdr + ETH_HDR_LEN;
    pktA->curr = pktA->nethdr + IPv4_HDR_LEN;
    if (SYSERR == rawRecv(pktA, &rip, &lip, IPv4_PROTO_ICMP))
    {
        failif(TRUE, "Returned SYSERR");
    }
    else
    {
        failif(((rawptr->istart != 0)
                || (rawptr->icount != 1)
                || (semcount(rawptr->isema) != 1)
                || (FALSE == netaddrequal(&rawptr->src[0], &rip))
                || (rawptr->in[0] != pktA)), "Incorrectly enqueued");
    }

    testPrint(verbose, "Read (Closed socket)");
    failif((SYSERR != read(RAW1, buf, 500)), "");

    testPrint(verbose, "Read (One Pkt)");
    failif(((read(RAW0, buf, 500) != 8)
            || (memcmp(buf, (pkt->data + ETH_HDR_LEN + IPv4_HDR_LEN), 8)
                != 0)
            || (rawptr->icount != 0)
            || (rawptr->istart != 1)
            || (semcount(rawptr->isema) != 0)
            || (rawptr->in[0] != NULL)), "");

    testPrint(verbose, "Read (Include header)");
    pktA = netGetbuf();
    memcpy(pktA, pkt, sizeof(struct packet) + pkt->len);
    pktA->linkhdr = pktA->data;
    pktA->nethdr = pktA->linkhdr + ETH_HDR_LEN;
    pktA->curr = pktA->nethdr + IPv4_HDR_LEN;
    rawptr->flags = RAW_IHDR;
    if (SYSERR == rawRecv(pktA, &rip, &lip, IPv4_PROTO_ICMP))
    {
        failif(TRUE, "Recv returned SYSERR");
    }
    else
    {
        failif(((read(RAW0, buf, 500) != (IPv4_HDR_LEN + 8))
                ||
                (memcmp(buf, (pkt->data + ETH_HDR_LEN), IPv4_HDR_LEN + 8)
                 != 0) || (rawptr->icount != 0) || (rawptr->istart != 2)
                || (semcount(rawptr->isema) != 0)
                || (rawptr->in[0] != NULL)), "");
    }


    testPrint(verbose, "Read (Accept)");
    pktA = netGetbuf();
    memcpy(pktA, pkt, sizeof(struct packet) + pkt->len);
    pktA->linkhdr = pktA->data;
    pktA->nethdr = pktA->linkhdr + ETH_HDR_LEN;
    pktA->curr = pktA->nethdr + IPv4_HDR_LEN;
    rawptr->flags = RAW_IACCEPT;
    rawptr->remoteip.type = NULL;
    if (SYSERR == rawRecv(pktA, &rip, &lip, IPv4_PROTO_ICMP))
    {
        failif(TRUE, "Recv returned SYSERR");
    }
    else
    {
        failif(((read(RAW0, buf, 500) != 8)
                || (FALSE == netaddrequal(&rawptr->remoteip, &rip))), "");
    }

    testPrint(verbose, "Read (Small buf)");
    pktA = netGetbuf();
    memcpy(pktA, pkt, sizeof(struct packet) + pkt->len);
    pktA->linkhdr = pktA->data;
    pktA->nethdr = pktA->linkhdr + ETH_HDR_LEN;
    pktA->curr = pktA->nethdr + IPv4_HDR_LEN;
    if (SYSERR == rawRecv(pktA, &rip, &lip, IPv4_PROTO_ICMP))
    {
        failif(TRUE, "Recv returned SYSERR");
    }
    else
    {
        failif(((read(RAW0, buf, 2) != 2)
                ||
                (memcmp(buf, (pkt->data + ETH_HDR_LEN + IPv4_HDR_LEN), 2)
                 != 0)), "");
    }

    testPrint(verbose, "Receive (Full buf)");
    for (i = 0; i < RAW_IBLEN; i++)
    {
        memcpy(pktA, pkt, sizeof(struct packet) + pkt->len);
        pktA->linkhdr = pktA->data;
        pktA->nethdr = pktA->linkhdr + ETH_HDR_LEN;
        pktA->curr = pktA->nethdr + IPv4_HDR_LEN;
        if (SYSERR == rawRecv(pktA, &rip, &lip, IPv4_PROTO_ICMP))
        {
            break;
        }
    }
    if (i < RAW_IBLEN)
    {
        failif(TRUE, "Unable to fill buffer");
    }
    else
    {
        memcpy(pktA, pkt, sizeof(struct packet) + pkt->len);
        pktA->linkhdr = pktA->data;
        pktA->nethdr = pktA->linkhdr + ETH_HDR_LEN;
        pktA->curr = pktA->nethdr + IPv4_HDR_LEN;
        failif((SYSERR != rawRecv(pktA, &rip, &lip, IPv4_PROTO_ICMP)),
               "Did not return SYSERR");
    }

    testPrint(verbose, "Close RAW");
    failif((SYSERR == close(RAW0)), "");

    /* Test Write/Send */
    testPrint(verbose, "Open RAW");
    if (SYSERR == open(RAW0, &lip, NULL, IPv4_PROTO_ICMP))
    {
        failif(TRUE, "Returned SYSERR");
    }
    else
    {
        devptr = (device *)&devtab[RAW0];
        rawptr = &rawtab[devptr->minor];
        failif(FALSE, "");
    }

    testPrint(verbose, "Send (Bad Params)");
    failif((rawSend(NULL, NULL, 0) != SYSERR), "");

    testPrint(verbose, "Send (Incomplete Spec)");
    failif((rawSend(rawptr, pkt->curr, 8) != SYSERR), "");

    testPrint(verbose, "Send (Incomplete Spec Hdr Inc)");
    rawptr->flags = RAW_OHDR;
    failif((rawSend(rawptr, pkt->curr, 8) != SYSERR), "");

    testPrint(verbose, "Send (Net Hdr Included)");
    /* Add ARP entry */
    /* Get 2nd Packet */
    data += phdr.caplen;
    memcpy(&phdr, data, sizeof(phdr));
    data += sizeof(phdr);
    if (PCAP_MAGIC != pcap.magic)
    {
        phdr.caplen = endswap(phdr.caplen);
    }
    nproc = netptr->nproc;
    write(ELOOP, data, phdr.caplen);
    /* Get 3rd Packet */
    data += phdr.caplen;
    memcpy(&phdr, data, sizeof(phdr));
    data += sizeof(phdr);
    if (PCAP_MAGIC != pcap.magic)
    {
        phdr.caplen = endswap(phdr.caplen);
    }
    memcpy(pkt->data, data, phdr.caplen);
    pkt->len = phdr.caplen;
    pkt->linkhdr = pkt->data;
    pkt->nethdr = pkt->linkhdr + ETH_HDR_LEN;
    pkt->curr = pkt->nethdr + IPv4_HDR_LEN;
    /* Wait for ARP entry to be added */
    wait = 0;
    while ((wait < MAX_WAIT) && (netptr->nproc == nproc))
    {
        wait++;
        sleep(10);
    }
    if (MAX_WAIT == wait)
    {
        failif(MAX_WAIT, "ARP entry failed");
    }
    else
    {
        netaddrcpy(&rawptr->remoteip, &rip);
        control(ELOOP, ELOOP_CTRL_SETFLAG, ELOOP_FLAG_HOLDNXT, NULL);
        if (SYSERR == rawSend(rawptr, pkt->nethdr, IPv4_HDR_LEN + 8))
        {
            failif(TRUE, "Returned SYSERR");
        }
        else
        {
            control(ELOOP, ELOOP_CTRL_GETHOLD, (long)buf, 500);
            failif((memcmp(pkt->data, buf, pkt->len) != 0),
                   "Incorrect Packet");
        }
    }

    testPrint(verbose, "Send");
    rawptr->flags = NULL;
    netaddrcpy(&rawptr->remoteip, &rip);
    control(ELOOP, ELOOP_CTRL_SETFLAG, ELOOP_FLAG_HOLDNXT, NULL);
    if (SYSERR == rawSend(rawptr, pkt->curr, 8))
    {
        failif(TRUE, "Returned SYSERR");
    }
    else
    {
        control(ELOOP, ELOOP_CTRL_GETHOLD, (long)buf, 500);
        failif((memcmp(pkt->data, buf, pkt->len) != 0),
               "Incorrect Packet");
    }

    testPrint(verbose, "Write");
    control(ELOOP, ELOOP_CTRL_SETFLAG, ELOOP_FLAG_HOLDNXT, NULL);
    if (SYSERR == write(RAW0, pkt->curr, 8))
    {
        failif(TRUE, "Returned SYSERR");
    }
    else
    {
        control(ELOOP, ELOOP_CTRL_GETHOLD, (long)buf, 500);
        failif((memcmp(pkt->data, buf, pkt->len) != 0),
               "Incorrect Packet");
    }

    close(RAW0);
    netDown(ELOOP);
    close(ELOOP);

    /* always print out the overall tests status */
    if (passed)
    {
        testPass(TRUE, "");
    }
    else
    {
        testFail(TRUE, "");
    }
#endif                          /* RAW0 */
    return OK;
}
Example #20
0
/**
 * Tests ARP.
 * @return OK when testing is complete
 */
thread test_arp(bool verbose)
{
    bool passed = TRUE;
    int i = 0;
    struct netaddr ip;
    struct netaddr mask;
    struct netaddr praddr;
    struct netaddr hwaddr;
    struct netaddr addrbuf;
    struct pcap_file_header pcap;
    struct pcap_pkthdr phdr;
    struct packet *pkt;
    struct netif *netptr;
    struct ethloop *pelp;
    uchar *data;
    uchar *request;
    struct arpPkt *arp;
    struct arpEntry *entry;
    uchar buf[ELOOP_BUFSIZE];
    int nproc;
    int nout;
    int wait;
    tid_typ tids[ARP_NTHRWAIT];
    tid_typ tid;
    irqmask im;

    enable();

    ip.type = NETADDR_IPv4;
    ip.len = IPv4_ADDR_LEN;
    ip.addr[0] = 192;
    ip.addr[1] = 168;
    ip.addr[2] = 1;
    ip.addr[3] = 6;

    mask.type = NETADDR_IPv4;
    mask.len = IPv4_ADDR_LEN;
    mask.addr[0] = 255;
    mask.addr[1] = 255;
    mask.addr[2] = 255;
    mask.addr[3] = 0;

    /* Initialize loopback ethernet and network interface */
    testPrint(verbose, "Test case initialization");

    data = (uchar *)(&_binary_data_testarp_pcap_start);
    memcpy(&pcap, data, sizeof(pcap));
    data += sizeof(pcap);

    if (SYSERR == open(ELOOP))
    {
        failif(TRUE, "");
    }
    else
    {
        failif((SYSERR == netUp(ELOOP, &ip, &mask, NULL)), "");
    }
    if (!passed)
    {
        testFail(TRUE, "");
        return OK;
    }

    /* Setup pointers to underlying structures */
#if NNETIF
    for (i = 0; i < NNETIF; i++)
    {
        if ((NET_ALLOC == netiftab[i].state)
            && (ELOOP == netiftab[i].dev))
        {
            break;
        }
    }
#endif
    netptr = &netiftab[i];
    pelp = &elooptab[devtab[ELOOP].minor];

    pkt = netGetbuf();
    if ((int)pkt != SYSERR)
    {
        pkt->nif = netptr;
    }

    praddr.type = NETADDR_IPv4;
    praddr.len = IPv4_ADDR_LEN;
    praddr.addr[0] = 192;
    praddr.addr[1] = 168;
    praddr.addr[2] = 1;
    praddr.addr[3] = 3;

    hwaddr.type = NETADDR_ETHERNET;
    hwaddr.len = ETH_ADDR_LEN;
    hwaddr.addr[0] = 0xAA;
    hwaddr.addr[1] = 0xBB;
    hwaddr.addr[2] = 0xCC;
    hwaddr.addr[3] = 0xDD;
    hwaddr.addr[4] = 0xEE;
    hwaddr.addr[5] = 0xFF;

    /* Test arpPkt structure */
    testPrint(verbose, "Header structure (Request)");
    /* Get 1st packet */
    memcpy(&phdr, data, sizeof(phdr));
    data += sizeof(phdr);
    if (PCAP_MAGIC != pcap.magic)
    {
        phdr.caplen = endswap(phdr.caplen);
    }
    pkt = netGetbuf();
    pkt->len +=
        ARP_CONST_HDR_LEN + (2 * IPv4_ADDR_LEN) + (2 * ETH_ADDR_LEN);
    pkt->curr -= pkt->len;
    arp = (struct arpPkt *)pkt->curr;
    arp->hwtype = hs2net(ARP_HWTYPE_ETHERNET);
    arp->prtype = hs2net(ARP_PRTYPE_IPv4);
    arp->hwalen = ETH_ADDR_LEN;
    arp->pralen = IPv4_ADDR_LEN;
    arp->op = hs2net(ARP_OP_RQST);
    memcpy(&arp->addrs[ARP_ADDR_SHA(arp)], netptr->hwaddr.addr,
           arp->hwalen);
    memcpy(&arp->addrs[ARP_ADDR_SPA(arp)], netptr->ip.addr, arp->pralen);
    memcpy(&arp->addrs[ARP_ADDR_DPA(arp)], praddr.addr, arp->pralen);
    failif((0 != memcmp(data + ELOOP_LINKHDRSIZE, arp, pkt->len)), "");

    /* Test arpPkt structure */
    testPrint(verbose, "Header structure (Reply)");
    /* Get 2nd packet */
    data += phdr.caplen;
    memcpy(&phdr, data, sizeof(phdr));
    data += sizeof(phdr);
    if (PCAP_MAGIC != pcap.magic)
    {
        phdr.caplen = endswap(phdr.caplen);
    }
    arp->op = hs2net(ARP_OP_REPLY);
    memcpy(&arp->addrs[ARP_ADDR_DHA(arp)], &arp->addrs[ARP_ADDR_SHA(arp)],
           arp->hwalen);
    arp->addrs[ARP_ADDR_DHA(arp) + ETH_ADDR_LEN - 1] = 0xCC;
    memcpy(&arp->addrs[ARP_ADDR_DPA(arp)], praddr.addr, arp->pralen);
    memcpy(&arp->addrs[ARP_ADDR_SHA(arp)], netptr->hwaddr.addr,
           arp->hwalen);
    memcpy(&arp->addrs[ARP_ADDR_SPA(arp)], netptr->ip.addr, arp->pralen);
    failif((0 != memcmp(data + ELOOP_LINKHDRSIZE, arp, pkt->len)), "");

    /* Test arpAlloc, free entry exists */
    testPrint(verbose, "Allocate free entry");
    /* Make first entry used */
    arptab[0].state = ARP_USED;
    arptab[0].expires = clktime + ARP_TTL_UNRESOLVED;
    entry = arpAlloc();
    failif(((NULL == entry) || (entry == &arptab[0])
            || (0 == (entry->state & ARP_USED))), "");

    /* Test arpAlloc, free entry exists */
    testPrint(verbose, "Allocate used entry");
    arptab[1].state = ARP_USED;
    arptab[1].expires = clktime + 1;
    /* Make all entries (except the first 2) have ARP_TTL_RESOLVED */
    for (i = 2; i < ARP_NENTRY; i++)
    {
        arptab[i].state = ARP_USED;
        arptab[i].expires = clktime + ARP_TTL_RESOLVED;
    }
    entry = arpAlloc();
    failif(((NULL == entry) || (entry != &arptab[1])
            || (0 == (entry->state & ARP_USED))), "");

    /* Test arpNotify */
    testPrint(verbose, "Notify waiting threads (bad params)");
    failif((SYSERR != arpNotify(NULL, TIMEOUT)), "");

    /* Test arpNotify */
    testPrint(verbose, "Notify waiting threads");
    entry = &arptab[0];
    entry->state = ARP_UNRESOLVED;
    for (i = 0; i < ARP_NTHRWAIT; i++)
    {
        tid =
            create((void *)notifyTest, INITSTK, INITPRIO, "notifyTest",
                   0);
        tids[i] = tid;
        entry->waiting[i] = tid;
        entry->count++;
        ready(tid, RESCHED_NO);
    }
    recvclr();
    arpNotify(entry, ARP_MSG_RESOLVED);
    nout = FALSE;
    nproc = 0;
    tid = recvtime(100);
    while ((tid != TIMEOUT) && (nproc < ARP_NTHRWAIT))
    {
        for (i = 0; i < ARP_NTHRWAIT; i++)
        {
            if (tid == tids[i])
            {
                tids[i] = NULL;
            }
        }
        nproc++;
        tid = recvtime(100);
    }
    for (i = 0; i < ARP_NTHRWAIT; i++)
    {
        if (tids[i] != NULL)
        {
            kill(tids[i]);
            nout = TRUE;
        }
    }
    failif(nout || (entry->count != 0), "");

    /* Test arpFree */
    testPrint(verbose, "Free entry (bad params)");
    failif((SYSERR != arpFree(NULL)), "");

    /* Test arpFree */
    testPrint(verbose, "Free resolved entry");
    entry = &arptab[0];
    entry->state = ARP_RESOLVED;
    entry->expires = clktime;
    failif(((SYSERR == arpFree(entry)) || (entry->expires != 0)), "");

    /* Test arpFree */
    testPrint(verbose, "Free unresolved entry");
    entry = &arptab[0];
    entry->state = ARP_UNRESOLVED;
    for (i = 0; i < ARP_NTHRWAIT; i++)
    {
        tid = create((void *)freeTest, INITSTK, INITPRIO, "freeTest", 0);
        tids[i] = tid;
        entry->waiting[i] = tid;
        entry->count++;
        ready(tid, RESCHED_NO);
    }
    recvclr();
    if (SYSERR == arpFree(entry))
    {
        failif(TRUE, "Returned SYSERR");
    }
    else
    {
        nout = FALSE;
        nproc = 0;
        tid = recvtime(100);
        while ((tid != TIMEOUT) && (nproc < ARP_NTHRWAIT))
        {
            for (i = 0; i < ARP_NTHRWAIT; i++)
            {
                if (tid == tids[i])
                {
                    tids[i] = NULL;
                }
            }
            nproc++;
            tid = recvtime(100);
        }
        for (i = 0; i < ARP_NTHRWAIT; i++)
        {
            if (tids[i] != NULL)
            {
                kill(tids[i]);
                nout = TRUE;
            }
        }
        failif(nout || (entry->count != 0), "");
    }

    /* Test arpGetEntry */
    testPrint(verbose, "Get entry");
    for (i = 0; i < ARP_NENTRY; i++)
    {
        arpFree(&arptab[i]);
    }
    nout = 4;
    if (ARP_NENTRY < nout)
    {
        nout = ARP_NENTRY;
    }
    for (i = 1; i < nout; i++)
    {
        entry = &arptab[i];
        entry->state = ARP_RESOLVED;
        entry->nif = netptr;
        praddr.addr[3] = i + 1;
        hwaddr.addr[5] = ((i + 0xA) << 4) + (i + 0xA);
        netaddrcpy(&entry->hwaddr, &hwaddr);
        netaddrcpy(&entry->praddr, &praddr);
        entry->expires = clktime + ARP_TTL_RESOLVED;
    }
    for (i = 1; i < nout; i++)
    {
        praddr.addr[3] = i + 1;
        entry = arpGetEntry(&praddr);
        if (entry != &arptab[i])
        {
            break;
        }
    }
    failif((i != nout), "");

    /* Test arpGetEntry with timeout */
    testPrint(verbose, "Get entry (timeout entries)");
    arptab[i].expires = clktime - 1;
    praddr.addr[3] = 2;
    entry = arpGetEntry(&praddr);
    if (entry != &arptab[1])
    {
        failif(TRUE, "Incorrect entry");
    }
    else
    {
        failif((arptab[0].state != ARP_FREE),
               "Did not free expired entry");
    }
    for (i = 0; i < ARP_NENTRY; i++)
    {
        arpFree(&arptab[i]);
    }

    /* Test receive reply for non-existing ARP table entry */
    testPrint(verbose, "Receive ARP reply, new entry");
    /* Get 3rd packet */
    data += phdr.caplen;
    memcpy(&phdr, data, sizeof(phdr));
    data += sizeof(phdr);
    if (PCAP_MAGIC != pcap.magic)
    {
        phdr.caplen = endswap(phdr.caplen);
    }
    praddr.addr[3] = 3;
    hwaddr.addr[5] = 0xCC;
    nout = pelp->nout;
    nproc = netptr->nproc;
    write(ELOOP, data, phdr.caplen);
    /* Wait until packet is processed */
    wait = 0;
    while ((wait < MAX_WAIT) && (netptr->nproc == nproc))
    {
        wait++;
        sleep(10);
    }
    if (MAX_WAIT == wait)
    {
        failif(TRUE, "Wait time expired");
    }
    else
    {
        /* Check entry and ensure reply was not sent */
        entry = NULL;
        for (i = 0; i < ARP_NENTRY; i++)
        {
            if (ARP_RESOLVED == arptab[i].state)
            {
                entry = &arptab[i];
                break;
            }
        }
        if (NULL == entry)
        {
            failif(TRUE, "No entry inserted");
        }
        else if ((FALSE == netaddrequal(&praddr, &entry->praddr))
                 || (FALSE == netaddrequal(&hwaddr, &entry->hwaddr))
                 || (entry->nif != netptr) || (entry->count != 0))
        {
            failif(TRUE, "Entry incorrect");
        }
        else if (pelp->nout > (nout + 1))
        {
            failif(TRUE, "Reply sent to reply");
        }
        else
        {
            failif(FALSE, "");
        }
    }

    /* Test receive request for non-existing ARP table entry */
    testPrint(verbose, "Receive ARP request, new entry");
    /* Get 4th packet */
    data += phdr.caplen;
    memcpy(&phdr, data, sizeof(phdr));
    data += sizeof(phdr);
    if (PCAP_MAGIC != pcap.magic)
    {
        phdr.caplen = endswap(phdr.caplen);
    }
    praddr.addr[3] = 1;
    hwaddr.addr[5] = 0xAA;
    nout = pelp->nout;
    nproc = netptr->nproc;
    im = disable();
    write(ELOOP, data, phdr.caplen);
    control(ELOOP, ELOOP_CTRL_SETFLAG, ELOOP_FLAG_HOLDNXT, NULL);
    restore(im);
    /* Wait until packet is processed */
    wait = 0;
    while ((wait < MAX_WAIT) && (netptr->nproc == nproc))
    {
        wait++;
        sleep(100);
    }
    if (MAX_WAIT == wait)
    {
        failif(TRUE, "Wait time expired");
    }
    else
    {
        /* Check entry and ensure reply was not sent */
        entry = NULL;
        for (i = 0; i < ARP_NENTRY; i++)
        {
            if ((ARP_RESOLVED == arptab[i].state)
                && (netaddrequal(&praddr, &arptab[i].praddr)))
            {
                entry = &arptab[i];
                break;
            }
        }
        if (NULL == entry)
        {
            failif(TRUE, "No entry inserted");
        }
        else if ((FALSE == netaddrequal(&praddr, &entry->praddr))
                 || (FALSE == netaddrequal(&hwaddr, &entry->hwaddr))
                 || (entry->nif != netptr) || (entry->count != 0))
        {
            failif(TRUE, "Entry incorrect");
        }
        else
        {
            control(ELOOP, ELOOP_CTRL_GETHOLD, (int)buf, ELOOP_BUFSIZE);
            /* Get 5th packet */
            data += phdr.caplen;
            memcpy(&phdr, data, sizeof(phdr));
            data += sizeof(phdr);
            if (PCAP_MAGIC != pcap.magic)
            {
                phdr.caplen = endswap(phdr.caplen);
            }
            failif((memcmp(data, buf, phdr.caplen) != 0),
                   "Invalid reply");
        }
    }

    /* Test receive request for non-supported hardware type */
    testPrint(verbose, "Receive ARP request, bad HW type");
    /* Get 6th packet */
    data += phdr.caplen;
    memcpy(&phdr, data, sizeof(phdr));
    data += sizeof(phdr);
    if (PCAP_MAGIC != pcap.magic)
    {
        phdr.caplen = endswap(phdr.caplen);
    }
    praddr.addr[3] = 2;
    hwaddr.addr[5] = 0xBB;
    nproc = netptr->nproc;
    write(ELOOP, data, phdr.caplen);
    /* Wait until packet is processed */
    wait = 0;
    while ((wait < MAX_WAIT) && (netptr->nproc == nproc))
    {
        wait++;
        sleep(100);
    }
    if (MAX_WAIT == wait)
    {
        failif(TRUE, "Wait time expired");
    }
    else
    {
        /* Ensure entry was not added */
        entry = NULL;
        for (i = 0; i < ARP_NENTRY; i++)
        {
            if ((ARP_RESOLVED == arptab[i].state)
                && (netaddrequal(&praddr, &arptab[i].praddr)))
            {
                entry = &arptab[i];
                break;
            }
        }
        failif((entry != NULL), "Entry inserted");
    }

    /* Test receive reply for non-supported protocol type */
    testPrint(verbose, "Receive ARP reply, bad protocol type");
    /* Get 7th packet */
    data += phdr.caplen;
    memcpy(&phdr, data, sizeof(phdr));
    data += sizeof(phdr);
    if (PCAP_MAGIC != pcap.magic)
    {
        phdr.caplen = endswap(phdr.caplen);
    }
    nproc = netptr->nproc;
    write(ELOOP, data, phdr.caplen);
    /* Wait until packet is processed */
    wait = 0;
    while ((wait < MAX_WAIT) && (netptr->nproc == nproc))
    {
        wait++;
        sleep(100);
    }
    if (MAX_WAIT == wait)
    {
        failif(TRUE, "Wait time expired");
    }
    else
    {
        /* Ensure entry was not added */
        entry = NULL;
        for (i = 0; i < ARP_NENTRY; i++)
        {
            if ((ARP_RESOLVED == arptab[i].state)
                && (netaddrequal(&praddr, &arptab[i].praddr)))
            {
                entry = &arptab[i];
                break;
            }
        }
        failif((entry != NULL), "Entry inserted");
    }

    /* Test receive reply for existing resolved ARP table entry */
    testPrint(verbose, "Receive ARP reply, resolved entry");
    /* Get 8th packet */
    data += phdr.caplen;
    memcpy(&phdr, data, sizeof(phdr));
    data += sizeof(phdr);
    if (PCAP_MAGIC != pcap.magic)
    {
        phdr.caplen = endswap(phdr.caplen);
    }
    praddr.addr[3] = 3;
    hwaddr.addr[5] = 0x0C;
    nout = pelp->nout;
    nproc = netptr->nproc;
    write(ELOOP, data, phdr.caplen);
    /* Wait until packet is processed */
    wait = 0;
    while ((wait < MAX_WAIT) && (netptr->nproc == nproc))
    {
        wait++;
        sleep(100);
    }
    if (MAX_WAIT == wait)
    {
        failif(TRUE, "Wait time expired");
    }
    else
    {
        /* Check entry */
        entry = NULL;
        for (i = 0; i < ARP_NENTRY; i++)
        {
            if ((ARP_RESOLVED == arptab[i].state)
                && (netaddrequal(&praddr, &arptab[i].praddr)))
            {
                entry = &arptab[i];
                break;
            }
        }
        failif((FALSE == netaddrequal(&hwaddr, &entry->hwaddr)),
               "Entry incorrect");
    }

    /* Test receive request not for me */
    testPrint(verbose, "Receive ARP request, not mine");
    /* Get 9th packet */
    data += phdr.caplen;
    memcpy(&phdr, data, sizeof(phdr));
    data += sizeof(phdr);
    if (PCAP_MAGIC != pcap.magic)
    {
        phdr.caplen = endswap(phdr.caplen);
    }
    praddr.addr[3] = 4;
    hwaddr.addr[5] = 0xDD;
    nproc = netptr->nproc;
    write(ELOOP, data, phdr.caplen);
    /* Wait until packet is processed */
    wait = 0;
    while ((wait < MAX_WAIT) && (netptr->nproc == nproc))
    {
        wait++;
        sleep(100);
    }
    if (MAX_WAIT == wait)
    {
        failif(TRUE, "Wait time expired");
    }
    else
    {
        /* Ensure entry was not added */
        entry = NULL;
        for (i = 0; i < ARP_NENTRY; i++)
        {
            if ((ARP_RESOLVED == arptab[i].state)
                && (netaddrequal(&praddr, &arptab[i].praddr)))
            {
                entry = &arptab[i];
                break;
            }
        }
        failif((entry != NULL), "Entry inserted");
    }

    /* Test arpSendRequest */
    testPrint(verbose, "Send ARP request (bad params)");
    failif((SYSERR != arpSendRqst(NULL)), "");

    /* Test arpSendRequest */
    testPrint(verbose, "Send ARP request");
    /* Get 10th packet */
    data += phdr.caplen;
    memcpy(&phdr, data, sizeof(phdr));
    data += sizeof(phdr);
    if (PCAP_MAGIC != pcap.magic)
    {
        phdr.caplen = endswap(phdr.caplen);
    }
    entry = &arptab[0];
    entry->state = ARP_UNRESOLVED;
    entry->nif = netptr;
    praddr.addr[3] = 2;
    hwaddr.addr[5] = 0xBB;
    netaddrcpy(&entry->hwaddr, &hwaddr);
    netaddrcpy(&entry->praddr, &praddr);
    entry->expires = clktime + ARP_TTL_UNRESOLVED;
    control(ELOOP, ELOOP_CTRL_SETFLAG, ELOOP_FLAG_HOLDNXT, NULL);
    if (SYSERR == arpSendRqst(entry))
    {
        failif(TRUE, "Returned SYSERR");
    }
    else
    {
        control(ELOOP, ELOOP_CTRL_GETHOLD, (int)buf, ELOOP_BUFSIZE);
        failif((memcmp(buf, data, phdr.caplen) != 0), "");
    }

    /* Test arpLookup */
    testPrint(verbose, "Lookup address (bad params)");
    failif((SYSERR != arpLookup(NULL, NULL, NULL)), "");

    /* Test arpLookup */
    testPrint(verbose, "Lookup existing resolved address");
    for (i = 0; i < ARP_NENTRY; i++)
    {
        arpFree(&arptab[i]);
    }
    entry = &arptab[0];
    entry->state = ARP_RESOLVED;
    entry->nif = netptr;
    praddr.addr[3] = 1;
    hwaddr.addr[5] = 0xAA;
    netaddrcpy(&entry->hwaddr, &hwaddr);
    netaddrcpy(&entry->praddr, &praddr);
    entry->expires = clktime + ARP_TTL_UNRESOLVED;
    i = arpLookup(netptr, &praddr, &addrbuf);
    if ((SYSERR == i) || (TIMEOUT == i))
    {
        failif(TRUE, "Returned SYSERR or TIMEOUT");
    }
    else
    {
        failif((FALSE == netaddrequal(&addrbuf, &hwaddr)),
               "Wrong address");
    }

    /* Test arpLookup */
    testPrint(verbose, "Lookup existing unresolved address");
    entry = &arptab[1];
    entry->state = ARP_UNRESOLVED;
    entry->nif = netptr;
    praddr.addr[3] = 2;
    hwaddr.addr[5] = 0xBB;
    netaddrcpy(&entry->hwaddr, &hwaddr);
    netaddrcpy(&entry->praddr, &praddr);
    entry->expires = clktime + ARP_TTL_UNRESOLVED;
    control(ELOOP, ELOOP_CTRL_SETFLAG, ELOOP_FLAG_HOLDNXT, NULL);
    request = data;
    wait = phdr.caplen;
    /* Get 11th packet */
    data += phdr.caplen;
    memcpy(&phdr, data, sizeof(phdr));
    data += sizeof(phdr);
    if (PCAP_MAGIC != pcap.magic)
    {
        phdr.caplen = endswap(phdr.caplen);
    }
    tid =
        ready(create
              ((void *)lookupTest, INITSTK, INITPRIO, "lookupTest", 4,
               request, wait, data, phdr.caplen), RESCHED_NO);
    i = arpLookup(netptr, &praddr, &addrbuf);
    if ((SYSERR == i) || (TIMEOUT == i))
    {
        kill(tid);
        failif(TRUE, "Returned SYSERR or TIMEOUT");
    }
    else
    {
        failif((FALSE == netaddrequal(&addrbuf, &hwaddr)),
               "Wrong address");
    }

    /* Test arpLookup */
    testPrint(verbose, "Lookup max threads wait for resolve");
    entry->state = ARP_UNRESOLVED;
    entry->count = ARP_NTHRWAIT;
    failif((arpLookup(netptr, &praddr, &addrbuf) != SYSERR), "");

    /* Test arpLookup */
    testPrint(verbose, "Lookup non-existing unresolved addr");
    praddr.addr[3] = 3;
    hwaddr.addr[5] = 0xCC;
    control(ELOOP, ELOOP_CTRL_SETFLAG, ELOOP_FLAG_HOLDNXT, NULL);
    request = data;
    wait = phdr.caplen;
    /* Get 12th packet */
    data += phdr.caplen;
    memcpy(&phdr, data, sizeof(phdr));
    data += sizeof(phdr);
    if (PCAP_MAGIC != pcap.magic)
    {
        phdr.caplen = endswap(phdr.caplen);
    }
    request = data;
    wait = phdr.caplen;
    /* Get 13th packet */
    data += phdr.caplen;
    memcpy(&phdr, data, sizeof(phdr));
    data += sizeof(phdr);
    if (PCAP_MAGIC != pcap.magic)
    {
        phdr.caplen = endswap(phdr.caplen);
    }
    tid =
        ready(create
              ((void *)lookupTest, INITSTK, INITPRIO, "lookupTest", 4,
               request, wait, data, phdr.caplen), RESCHED_NO);
    i = arpLookup(netptr, &praddr, &addrbuf);
    if ((SYSERR == i) || (TIMEOUT == i))
    {
        kill(tid);
        failif(TRUE, "Returned SYSERR or TIMEOUT");
    }
    else
    {
        failif((FALSE == netaddrequal(&addrbuf, &hwaddr)),
               "Wrong address");
    }

    /* Test arpLookup */
    testPrint(verbose, "Lookup timeout");
    praddr.addr[3] = 4;
    hwaddr.addr[5] = 0xDD;
    control(ELOOP, ELOOP_CTRL_SETFLAG, ELOOP_FLAG_DROPALL, NULL);
    failif((SYSERR != arpLookup(netptr, &praddr, &addrbuf)), "");

    /* Stop loopback ethernet and network interface */
    testPrint(verbose, "Test case cleanup");
    for (i = 0; i < ARP_NENTRY; i++)
    {
        arpFree(&arptab[i]);
    }
    failif(((SYSERR == netFreebuf(pkt)) || (SYSERR == netDown(ELOOP))
            || (SYSERR == close(ELOOP))), "");

    if (passed)
    {
        testPass(TRUE, "");
    }
    else
    {
        testFail(TRUE, "");
    }

    return OK;
}
Example #21
0
process test_semaphore(bool8 verbose)
{
    pid32 apid;
    bool8 passed = TRUE;
    sid32 s;
    byte testResult = 0;
    char msg[50];

    /* Single semaphore tests */
    testPrint(verbose, "Semaphore creation: ");
    s = semcreate(0);
    if (isbadsem(s))
    {
        passed = FALSE;
        sprintf(msg, "%d", s);
        testFail(verbose, msg);
    }
    else if (test_checkSemCount(s, 0, verbose))
    {
        testPass(verbose, "");
    }
    else
    {
        passed = FALSE;
    }

    testPrint(verbose, "Wait on semaphore: ");
    if ((SYSERR != resume(apid =
          create((void *)test_semWaiter, INITSTK, 31,
                 "SEMAPHORE-A", 3, s, 1, &testResult)))
    	&& test_checkProcState(apid, PR_WAIT, verbose)
        && test_checkSemCount(s, -1, verbose)
        && test_checkResult(testResult, 0, verbose))
    {
        testPass(verbose, "");
    }
    else
    {
        passed = FALSE;
    }


    testPrint(verbose, "Signal semaphore: ");
    if ((OK == signal(s))
    	&& test_checkProcState(apid, PR_FREE, verbose)
        && test_checkSemCount(s, 0, verbose)
        && test_checkResult(testResult, 1, verbose))
    {
        testPass(verbose, "");
    }
    else
    {
        passed = FALSE;
    }

    testPrint(verbose, "Signaln semaphore (valid count): ");
    if ((OK == signaln(s, 5))
    	&& test_checkProcState(apid, PR_FREE, verbose)
        && test_checkSemCount(s, 5, verbose)
        && test_checkResult(testResult, 1, verbose))
    {
        testPass(verbose, "");
    }
    else
    {
        passed = FALSE;
    }

    testPrint(verbose, "Signaln semaphore (invalid count): ");
    if (SYSERR == signaln(s, -5))
    {
        testPass(verbose, "");
    }
    else
    {
        passed = FALSE;
    }


    /* Free semaphore, single semaphore tests */
    testPrint(verbose, "Delete valid semaphore: ");
    if ((OK == semdelete(s))
	&& (semtab[s].sstate == S_FREE)
	&& isempty(semtab[s].squeue))
    {
	testPass(verbose, "");
    }
    else
    {
        passed = FALSE;
    }

    testPrint(verbose, "Delete invalid semaphore: ");
    if (SYSERR == semdelete(-1))
    {   
        testPass(verbose, "");
    }
    else
    {
        passed = FALSE;
    }
 
    testPrint(verbose, "Delete free semaphore: ");
    if (SYSERR == semdelete(s))
    {   
        testPass(verbose, "");
    }
    else
    {
        passed = FALSE;
    }
 
    testPrint(verbose, "Signal bad semaphore id: ");
    if (SYSERR == signal(-1))
    {
        testPass(verbose, "");
    }
    else
    {
        passed = FALSE;
    }

    testPrint(verbose, "Signal free semaphore: ");
    if (SYSERR == signal(s))
    {
        testPass(verbose, "");
    }
    else
    {
        passed = FALSE;
    }


    testPrint(verbose, "Signaln bad semaphore id: ");
    if (SYSERR == signaln(-1, 4))
    {
        testPass(verbose, "");
    }
    else
    {
        passed = FALSE;
    }

    testPrint(verbose, "Signaln free semaphore: ");
    if (SYSERR == signaln(s, 4))
    {
        testPass(verbose, "");
    }
    else
    {
        passed = FALSE;
    }

    testPrint(verbose, "Wait on bad semaphore id: ");
    if (SYSERR == wait(-1))
    {
        testPass(verbose, "");
    }
    else
    {
        passed = FALSE;
    }
 
    testPrint(verbose, "Wait on free semaphore: ");
    if (SYSERR == wait(s))
    {
        testPass(verbose, "");
    }
    else
    {
        passed = FALSE;
    }



    /* Process A should be dead, but in case the test failed. */
    kill(apid);

    /* General semaphore pass/fail */
    if (TRUE == passed)
    {
        testPass(TRUE, "");
    }
    else
    {
        testFail(TRUE, "");
    }

    return OK;
}
Example #22
0
/**
 * Tests the network interfaces.
 * @return OK when testing is complete
 */
thread test_netif(bool verbose)
{
    bool passed = TRUE;
    int i = 0;
    struct netaddr ip;
    struct netaddr mask;
    struct netaddr gate;
    struct netaddr hw;
    struct netaddr brc;
    struct pcap_file_header pcap;
    struct pcap_pkthdr phdr;
    struct packet *pkt;
    struct netif *netptr;
    uchar *data;

    enable();

    ip.type = NETADDR_IPv4;
    ip.len = IPv4_ADDR_LEN;
    ip.addr[0] = 192;
    ip.addr[1] = 168;
    ip.addr[2] = 1;
    ip.addr[3] = 6;

    mask.type = NETADDR_IPv4;
    mask.len = IPv4_ADDR_LEN;
    mask.addr[0] = 255;
    mask.addr[1] = 255;
    mask.addr[2] = 255;
    mask.addr[3] = 0;

    gate.type = NETADDR_IPv4;
    gate.len = IPv4_ADDR_LEN;
    gate.addr[0] = 192;
    gate.addr[1] = 168;
    gate.addr[2] = 1;
    gate.addr[3] = 1;

    brc.type = NETADDR_IPv4;
    brc.len = IPv4_ADDR_LEN;
    brc.addr[0] = 255;
    brc.addr[1] = 255;
    brc.addr[2] = 255;
    brc.addr[3] = 255;

    testPrint(verbose, "Open loopback ethernet device");
    failif((SYSERR == open(ELOOP)), "");

    testPrint(verbose, "Start network interface");
    if (SYSERR == netUp(ELOOP, &ip, &mask, &gate))
    {
        failif(TRUE, "Returned SYSERR");
    }
    else
    {
        for (i = 0; i < NNETIF; i++)
        {
            if ((NET_ALLOC == netiftab[i].state)
                && (ELOOP == netiftab[i].dev))
            {
                break;
            }
        }

        netptr = &netiftab[i];
        if ((i >= NNETIF)
            || (netptr->state != NET_ALLOC)
            || (netptr->mtu != ELOOP_MTU)
            || (netptr->linkhdrlen != ELOOP_LINKHDRSIZE)
            || (FALSE == netaddrequal(&netptr->ip, &ip))
            || (FALSE == netaddrequal(&netptr->mask, &mask))
            || (FALSE == netaddrequal(&netptr->gateway, &gate)))
        {
            failif(TRUE, "Incorrect structure setup");
        }
        else
        {
            for (i = 0; i < NET_NTHR; i++)
            {
                if (isbadtid(netptr->recvthr[i]))
                {
                    failif(TRUE, "Bad recvthr");
                    break;
                }
            }
            if (NET_NTHR == i)
            {
                failif(FALSE, "");
            }
        }
    }

    testPrint(verbose, "Stop network interface");
    failif((SYSERR == netDown(ELOOP)), "");

    testPrint(verbose, "Start network interface (no gateway)");
    failif((SYSERR == netUp(ELOOP, &ip, &mask, NULL)), "");
    for (i = 0; i < NNETIF; i++)
    {
        if ((NET_ALLOC == netiftab[i].state)
            && (ELOOP == netiftab[i].dev))
        {
            break;
        }
    }

    /* Kill receiver threads */
    netptr = &netiftab[i];
    for (i = 0; i < NET_NTHR; i++)
    {
        kill(netptr->recvthr[i]);
        netptr->recvthr[i] = BADTID;
    }

    testPrint(verbose, "Get packet buffer");
    pkt = netGetbuf();
    failif((SYSERR == (int)pkt), "");

    data = (uchar *)&_binary_data_testnetif_pcap_start;
    memcpy(&pcap, data, sizeof(pcap));
    data += sizeof(pcap);
    memcpy(&phdr, data, sizeof(phdr));
    data += sizeof(phdr);
    if (PCAP_MAGIC != pcap.magic)
    {
        phdr.caplen = endswap(phdr.caplen);
    }
    pkt->nif = netptr;
    pkt->len = phdr.caplen - netptr->linkhdrlen;
    pkt->curr = pkt->curr - pkt->len;
    memcpy(pkt->curr, data + netptr->linkhdrlen, pkt->len);

    hw.type = NETADDR_ETHERNET;
    hw.len = ETH_ADDR_LEN;
    hw.addr[0] = 0xAA;
    hw.addr[1] = 0xBB;
    hw.addr[2] = 0xCC;
    hw.addr[3] = 0xDD;
    hw.addr[4] = 0xEE;
    hw.addr[5] = 0xAA;

    testPrint(verbose, "Send packet (known hardware address)");
    if (SYSERR == netSend(pkt, &hw, NULL, ETHER_TYPE_ARP))
    {
        failif(TRUE, "Returned SYSERR");
    }
    else
    {
        if ((SYSERR == read(netptr->dev, pkt, phdr.caplen))
            || (memcmp(pkt, data, phdr.caplen) != 0))
        {
            failif(TRUE, "Packet did not match");
        }
        else
        {
            failif(FALSE, "");
        }
    }

    testPrint(verbose, "Free packet buffer");
    failif((SYSERR == netFreebuf(pkt)), "");

    testPrint(verbose, "Stop network interface");
    failif(((SYSERR == netDown(ELOOP)) || (SYSERR == close(ELOOP))), "");

    if (passed)
    {
        testPass(TRUE, "");
    }
    else
    {
        testFail(TRUE, "");
    }

    return OK;
}
Example #23
0
process test_semaphore5(bool8 verbose)
{
    pid32 atid;
    bool8 passed = TRUE;
    sid32 s;
    byte testResult = 0;
    char msg[50];

    testPrint(verbose, "Semaphore creation: ");
    s = semcreate(0);
    if (isbadsem(s))
    {
        passed = FALSE;
        sprintf(msg, "%d", s);
        testFail(verbose, msg);
    }
    else if (test_checkSemCount(s, 0))
    {
        testPass(verbose, "");
    }
    else
    {
        passed = FALSE;
    }

    ready(atid =
          create((void *)test_semWaiter, INITSTK, getprio(getpid()) + 10,
                 "SEMAPHORE-A", 3, s, 1, &testResult), RESCHED_YES);

    testPrint(verbose, "Wait on semaphore: ");
    if (test_checkProcState(atid, PR_WAIT)
        && test_checkSemCount(s, -1) && test_checkResult(testResult, 0))
    {
        testPass(verbose, "");
    }
    else
    {
        passed = FALSE;
    }

    testPrint(verbose, "Reset semaphore: ");
    if ((OK == semreset(s, 0))
	&& test_checkProcState(atid, PR_FREE)
        && test_checkSemCount(s, 0) && test_checkResult(testResult, 1))
    {
        testPass(verbose, "");
    }
    else
    {
        passed = FALSE;
    }

    testPrint(verbose, "Reset semaphore (invalid count): ");
    if (SYSERR == semreset(s, -5))
    {
        testPass(verbose, "");
    }
    else
    {
        passed = FALSE;
    }

    testPrint(verbose, "Reset invalid semaphore: ");
    if (SYSERR == semreset(-1, 0))
    {   
        testPass(verbose, "");
    }
    else
    {
        passed = FALSE;
    }

    semdelete(s);

    testPrint(verbose, "Reset free semaphore: ");
    if (SYSERR == semreset(s, 0))
    {   
        testPass(verbose, "");
    }
    else
    {
        passed = FALSE;
    }

    if (TRUE == passed)
    {
        testPass(TRUE, "");
    }
    else
    {
        testFail(TRUE, "");
    }

    return OK;
}
Example #24
0
int testArrayStruct() {
	info("- Testing Array<struct>-----");

	Array<Point> pt_array;
	bool r;
	unsigned int idx;

	// fill the array
	info("  * Filling the array with numbers.");
	for (int i = 0; i < 100; i++) {
		pt_array.set(i, Point(100 + i, 1000 + i));
	}

	// test the values
	r = true;
	for (int i = 0; i < 100; i++) {
		r &= (pt_array.get(i).X == i + 100) && (pt_array.get(i).Y == i + 1000);
	}
	if (!testPrint(r, "  * Checking if all values were inserted correctly", true))
		return -1;

	// overwriting an existing item (checking memory leaks)
	pt_array.set(0, Point(100, 1000));
	if (!testPrint((pt_array[0].X == 100) && (pt_array[0].Y == 1000), "  * Replacing existing value", true))
		return -1;

	// test [] operator
	pt_array[1000] = Point(199, 1999);
	if (!testPrint((pt_array[1000].X == 199) && (pt_array[1000].Y == 1999), "  * Setting value by [] operator", true))
		return -1;

	pt_array[1000] = Point(1100, 2000);
	if (!testPrint((pt_array[1000].X == 1100) && (pt_array[1000].Y == 2000), "  * Replacing existing value by [] operator", true))
		return -1;

	idx = pt_array.by_count(101);
	if (!testPrint(pt_array[1000] == pt_array[idx], "  * Testing item 101th item", true))
		return -1;

	//
	info("  * Testing iterators.");
	r = true;
	for (unsigned int i = pt_array.first(); i != INVALID_IDX; i = pt_array.next(i)) {
		r &= (pt_array.get(i).X == (int) (i + 100)) && (pt_array.get(i).Y == (int) (i + 1000));
	}
	if (!testPrint(r, "    - Forward iteration", true))
		return -1;

	r = true;
	for (unsigned int i = pt_array.last(); i != INVALID_IDX; i = pt_array.prev(i)) {
		r &= (pt_array.get(i).X == (int) (i + 100)) && (pt_array.get(i).Y == (int) (i + 1000));
	}
	if (!testPrint(r, "    - Backward iteration", true))
		return -1;

	idx = pt_array.add(Point(2000, 3000));
	if (!testPrint((pt_array[idx].X == 2000) && (pt_array[idx].Y == 3000), "  * Adding an item at the end of the array", true))
		return -1;

	// remove
	pt_array.remove(idx);
	if (!testPrint(!pt_array.exists(idx), "  * Removing an item from the array", true))
		return -1;

	// MEMORY
	unsigned int mem_used;

	// mem used
	mem_used = pt_array.mem_used();
	if (!testPrint(mem_used > 2, "  * Used memory should be nonzero", true))
		return -1;

	// free memory
	pt_array.remove_all();
	testPrint(true, "  * Freeing memory", true);

	mem_used = pt_array.mem_used();
	if (!testPrint(mem_used == 0, "  * Used memory should be zero", true))
		return -1;

	return 0;
}
Example #25
0
/**
 * Tests the string.h header in the Xinu Standard Library.
 * @return OK when testing is complete
 */
thread test_libString(bool verbose)
{
    char *s1 = NULL;
    char *s2 = NULL;
    char str[LEN_STR] = "ZYXWYZ";

    bool passed = TRUE;

    /* strncmp */
    testPrint(verbose, "String n comparison");
    failif(((0 > strncmp("ZYX", "WVU", 2))
            || (0 < strncmp("wvu", "zyx", 2))
            || (0 < strncmp("wvu", "zyx", 50))
            || (0 > strncmp("zyx", "wvu", 50))
            || (0 != strncmp("abc", "abc", 50))
            || (0 != strncmp("987", "987", 2))
            || (0 < strncmp("NOP", "NOQ", 3))
            || (0 > strncmp("noq", "nop", 3))
            || (0 != strncmp("450", "456", 2))), "");

    /* strncpy */
    testPrint(verbose, "String n copy");
    char sB[6] = "abcde";

    s1 = strncpy(sB, "fghij", 3);
    failif(((0 != strncmp(sB, "fghde", 5)) || (s1 != sB)), "");

    /* strncat */
    testPrint(verbose, "String n concantenate");
    char sD[6] = "zyx";

    s1 = strncat(sD, "wv", 1);
    failif(((0 != strncmp(sD, "zyxw", 4))
            || (0 != strncmp(s1, "zyxw", 4))), "");

    /* * * strchr * * */
    /* fail to find char */
    testPrint(verbose, "Fail to find; strchr()");
    s1 = strchr(str, 'a');
    failif(NULL != s1, "");

    /* find a uniq char */
    testPrint(verbose, "Find unique char; strchr()");
    s1 = strchr(str, 'X');
    failif('W' != *(s1 + 1), "");

    /* find a non-uniq char */
    testPrint(verbose, "Find non-unique char; strchr()");
    s1 = strchr(str, 'Y');
    failif('X' != *(s1 + 1), "");

    /* find null char */
    testPrint(verbose, "Find null char; strchr()");
    s1 = strchr(str, '\0');
    failif(str + (LEN_STR - 1) != s1 || '\0' != *s1, "");

    /* * * strrchr * * */
    /* fail to find char */
    testPrint(verbose, "Fail to find; strrchr()");
    s1 = strrchr(str, 'a');
    failif(NULL != s1, "");

    /* find a uniq char */
    testPrint(verbose, "Find unique char; strrchr()");
    s1 = strrchr(str, 'X');
    failif('W' != *(s1 + 1), "");

    /* find a non-uniq char */
    testPrint(verbose, "Find non-unique char; strrchr()");
    s1 = strrchr(str, 'Y');
    failif('Z' != *(s1 + 1), "");

    /* find null char */
    testPrint(verbose, "Find null char; strrchr()");
    s1 = strrchr(str, '\0');
    failif(str + (LEN_STR - 1) != s1 || '\0' != *s1, "");

    /* strstr */
    testPrint(verbose, "String search");
    char sG[9] = "ABBCDEBA";

    s1 = strstr(sG, "BCD");
    s1 += 3;
    failif(('E' != *s1), "");

    /* strnlen */
    testPrint(verbose, "String n length");
    failif(((5 != strnlen("12345", 100))
            || (0 != strnlen("", 5))
            || (3 != strnlen("12345", 3))
            || (0 != strnlen("12345", 0))), "");

    /* memcmp */
    testPrint(verbose, "Memory comparsion");
    failif(((0 < memcmp("ABC", "DEF", 3))
            || (0 > memcmp("def", "abc", 3))
            || (0 != memcmp("123", "123", 3))), "");

    /* memcpy */
    testPrint(verbose, "Memory copy");
    char sH[6] = "ABCDE";

    s1 = memcpy(sH, "FGHIJ", 5);
    failif(((0 != memcmp(sH, "FGHIJ", 5))
            || (0 != memcmp(s1, "FGHIJ", 5))), "");

    /* memchr */
    testPrint(verbose, "Memory character search");
    char sI[7] = "abcdba";

    s1 = memchr(sI, 'b', 6) + 1;
    s2 = memchr(sI, 'c', 6) + 1;
    failif((('c' != *s1) || ('d' != *s2)), "");

    /* memset */
    testPrint(verbose, "Memory set");
    char sJ[6] = "ABCDE";

    s1 = memset(sJ, 'F', 3);
    failif(((0 != memcmp(sJ, "FFFDE", 5)) || (s1 != sJ)), "");

    if (passed)
    {
        testPass(TRUE, "");
    }
    else
    {
        testFail(TRUE, "");
    }

    return OK;
}
Example #26
0
/**
 * Tests the stdio.h header in the Xinu Standard Library.
 * @return OK when testing is complete
 */
thread test_libStdio(bool verbose)
{
#if defined(LOOP0)
    char str[50];
    int stdsav;
    int d, o, x;
    char c;
    char s[50];
    bool passed = TRUE;
    int ret;
    char *pret;

    ret = open(LOOP0);
    failif(ret == SYSERR, "failed to open loopback device");

    ret = control(LOOP0, LOOP_CTRL_SET_FLAG, LOOP_NONBLOCK, 0);
    failif(ret == SYSERR, "failed to set loopback device to nonblocking");

    /* fputc, fgetc.  Note: return value of fgetc() must be stored in 'int', not
     * 'char', because it can be 257 possible values (all characters, plus EOF).
     * */
    testPrint(verbose, "fgetc, fputc: basic functionality");
    ret = fputc('a', LOOP0);
    failif(ret != 'a', "fputc failed to return put character");
    ret = fputc('\n', LOOP0);
    failif(ret != '\n', "fputc failed to return put character");
    ret = fputc(4, LOOP0);
    failif(ret != 4, "fputc failed to return put character");
    ret = fgetc(LOOP0);
    failif(ret != 'a', "fgetc failed to return character");
    ret = fgetc(LOOP0);
    failif(ret != '\n', "fgetc failed to return character");
    ret = fgetc(LOOP0);
    failif(ret != 4, "fgetc failed to return character");

    /* fgets */

    /* fgets - test basic functionality */
    testPrint(verbose, "fgets: basic functionality");
    ret = write(LOOP0, "Test sentence.\n", 15);
    failif(ret != 15, "failed to write data to loopback device");
    memset(str, 'X', 20);
    pret = fgets(str, 20, LOOP0);
    failif(pret != str, "fgets failed to return output buffer");
    failif((0 != memcmp(str, "Test sentence.\n\0XXXX", 20)),
           "fgets failed to correctly return written data");

    /* fgets - Test partial reads */
    testPrint(verbose, "fgets: partial reads");
    ret = write(LOOP0, "Test sentence.\n", 15);
    failif(ret != 15, "failed to write data to loopback device");
    memset(str, 'X', 20);
    pret = fgets(str, 8, LOOP0);
    failif(pret != str, "fgets failed to return output buffer");
    failif((0 != memcmp(str, "Test se\0XX", 10)),
           "fgets failed to correctly return written data");
    memset(str, 'X', 20);
    pret = fgets(str, 9, LOOP0);
    failif(pret != str, "fgets failed to return output buffer");
    failif((0 != memcmp(str, "ntence.\n\0X", 10)),
           "fgets failed to correctly return written data");

    /* fgets - check returns NULL at end-of-file */
    testPrint(verbose, "fgets: return NULL on end-of-file");
    pret = fgets(str, 9, LOOP0);
    failif(pret != NULL, "fgets failed to return NULL on end-of-file");

    /* fgets - Test reading stops at newline */
    testPrint(verbose, "fgets: only read until newline");
    ret = write(LOOP0, "line1\nline2\n", 12);
    failif(ret != 12, "failed to write data to loopback device");
    memset(str, 'X', 20);
    pret = fgets(str, 16, LOOP0);
    failif(pret != str, "fgets failed to return output buffer");
    failif(0 != memcmp(str, "line1\n\0XXX", 10),
           "fgets failed to read only partial line");

    memset(str, 'X', 20);
    pret = fgets(str, 16, LOOP0);
    failif(pret != str, "fgets failed to return output buffer");
    failif(0 != memcmp(str, "line2\n\0XXX", 10),
           "fgets failed to read only partial line");

    /* fputs */
    testPrint(verbose, "fputs: basic functionality");
    ret = fputs("Put test.", LOOP0);
    failif(ret < 0, "fputs failed to return nonnegative value on success");
    ret = read(LOOP0, str, 9);
    failif(ret != 9, "failed to read data put with fputs");
    failif((0 != strncmp(str, "Put test.", 9)),
           "data read back with fputs was not the same as written");

    /* putchar, getchar */
    testPrint(verbose, "putchar, getchar: basic functionality");
    stdsav = stdout;
    stdout = LOOP0;
    ret = putchar('a');
    stdout = stdsav;
    failif(ret != 'a', "putchar failed to return character written");
    stdsav = stdin;
    stdin = LOOP0;
    ret = getchar();
    stdin = stdsav;
    failif(ret != 'a', "getchar failed to return character previously written");

    testPrint(verbose, "getchar: return EOF on end-of-file");
    stdsav = stdin;
    stdin = LOOP0;
    ret = getchar();
    stdin = stdsav;
    failif(ret != EOF, "getchar failed to return EOF on end-of-file");

    /* fprintf */
    testPrint(verbose, "fprintf: basic functionality");
    ret = fprintf(LOOP0, "%d %o %x %c %s", 75, 75, 75, 75, "ABC");

    failif(ret != TEST_STR_LEN,
           "fprintf() did not correctly return number of characters written");

    ret = read(LOOP0, str, TEST_STR_LEN);
    failif(ret != TEST_STR_LEN,
           "failed to read data back from loop device");

    failif((0 != strncmp(str, TEST_STR, TEST_STR_LEN)),
           "fprintf() failed to print the data correctly");

    /* printf */
    testPrint(verbose, "printf: basic functionality");
    stdsav = stdout;
    stdout = LOOP0;
    ret = printf("%d %o %x %c %s", 75, 75, 75, 75, "ABC");
    stdout = stdsav;
    failif(ret != TEST_STR_LEN,
           "printf() did not correctly return number of characters written");
    ret = read(LOOP0, str, TEST_STR_LEN);
    failif(ret != TEST_STR_LEN,
           "failed to read data back from loop device");
    failif((0 != strncmp(str, TEST_STR, TEST_STR_LEN)),
           "printf() failed to print the data correctly");

    /* sprintf */
    testPrint(verbose, "sprintf: basic functionality");
    ret = sprintf(str, "%d %o %x %c %s", 75, 75, 75, 75, "ABC");
    failif(ret != TEST_STR_LEN,
           "sprintf() did not correctly return number of characters written");
    failif((0 != strncmp(str, TEST_STR, TEST_STR_LEN)),
           "sprintf() failed to print the data correctly");

    /* sscanf */
    testPrint(verbose, "sscanf: basic functionality");
    strncpy(str, TEST_STR, TEST_STR_LEN + 1);
    d = o = x = c = 0;
    ret = sscanf(str, "%d %o %x %c %s", &d, &o, &x, &c, s);
    failif(ret != 5,
           "sscanf did not correctly return number of matches");

    failif(75 != d || 75 != o || 75 != x || 75 != c ||
            0 != strncmp(s, "ABC", 3),
            "sscanf did not scan data correctly");

    /* fscanf */
    testPrint(verbose, "fscanf: basic functionality");
    d = o = x = c = 0;
    ret = write(LOOP0, TEST_STR, TEST_STR_LEN);
    failif(ret != TEST_STR_LEN,
           "failed to write data to loopback device");
    ret = fscanf(LOOP0, "%d %o %x %c %s", &d, &o, &x, &c, s);
    failif(ret != 5,
           "fscanf did not correctly return number of matches");
    failif(75 != d || 75 != o || 75 != x || 75 != c ||
            0 != strncmp(s, "ABC", 3),
            "fscanf did not scan data correctly");

    /* scanf */

    testPrint(verbose, "scanf: basic functionality");
    d = o = x = c = 0;
    ret = write(LOOP0, TEST_STR, TEST_STR_LEN);
    failif(ret != TEST_STR_LEN,
           "failed to write data to loopback device");
    stdsav = stdin;
    stdin = LOOP0;
    ret = scanf("%d %o %x %c %s", &d, &o, &x, &c, s);
    stdin = stdsav;
    failif(ret != 5, "scanf did not correctly return number of matches");
    failif(75 != d || 75 != o || 75 != x || 75 != c ||
            0 != strncmp(s, "ABC", 3), "");

    /* More detailed fprintf tests */
    passed = do_detailed_fprintf_tests(verbose, passed);

    /* More detailed fscanf tests */
    passed = do_detailed_fscanf_tests(verbose, passed);
    
    control(LOOP0, LOOP_CTRL_CLR_FLAG, LOOP_NONBLOCK, 0);

    if (passed)
    {
        testPass(TRUE, "");
    }
    else
    {
        testFail(TRUE, "");
    }

    close(LOOP0);
#else  /* defined(LOOP0) */
    testSkip(TRUE, "");
#endif  /* !defined(LOOP0) */
    return OK;
}
Example #27
0
thread test_semaphore3(bool verbose)
{
#if NSEM
    tid_typ atid, btid;
    bool passed = TRUE;
    semaphore s;
    uchar testResult = 0;
    char msg[50];

    testPrint(verbose, "Semaphore creation: ");
    s = semcreate(1);
    if (isbadsem(s))
    {
        passed = FALSE;
        sprintf(msg, "%d", s);
        testFail(verbose, msg);
    }
    else if (test_checkSemCount(s, 1))
    {
        testPass(verbose, "");
    }
    else
    {
        passed = FALSE;
    }

    /* We use a higher priority for thread A to ensure it is not rescheduled to
     * thread B before it is even able to wait on the semaphore.  */
    ready(atid =
          create((void *)test_semWaiter, INITSTK, 32,
                 "SEMAPHORE-A", 3, s, 1, &testResult), RESCHED_NO);
    ready(btid =
          create((void *)test_semWaiter, INITSTK, 31,
                 "SEMAPHORE-B", 3, s, 1, &testResult), RESCHED_YES);

    testPrint(verbose, "Wait on semaphore: ");
    /* Process A should be admitted, but B should wait. */
    if (test_checkProcState(atid, THRFREE)
        && test_checkProcState(btid, THRWAIT)
        && test_checkSemCount(s, -1) && test_checkResult(testResult, 1))
    {
        testPass(verbose, "");
    }
    else
    {
        passed = FALSE;
    }

    signal(s);

    /* Process B waited, so signal should release it. */
    testPrint(verbose, "Signal waiting semaphore: ");
    if (test_checkProcState(btid, THRFREE)
        && test_checkSemCount(s, 0) && test_checkResult(testResult, 2))
    {
        testPass(verbose, "");
    }
    else
    {
        passed = FALSE;
    }

    if (TRUE == passed)
    {
        testPass(TRUE, "");
    }
    else
    {
        testFail(TRUE, "");
    }

    /* Processes should be dead, but in case the test failed. */
    kill(atid);
    kill(btid);
    semfree(s);

#else /* NSEM */
    testSkip(TRUE, "");
#endif /* NSEM == 0 */
    return OK;
}
Example #28
0
static bool do_detailed_fprintf_tests(bool verbose, bool passed)
{
    size_t i;
    size_t j;

    for (i = 0; i < sizeof(fprintf_specs) / sizeof(fprintf_specs[0]); i++)
    {
        const char *format = fprintf_specs[i].format;
        const char *expected_output = fprintf_specs[i].expected_output;
        uint nargs = fprintf_specs[i].nargs;
        const uint *args = fprintf_specs[i].args;
        int ret;
        int len = strlen(expected_output);
        uchar obuf[len + 1];
        char test_strbuf[50 + strlen(format)];
        sprintf(test_strbuf, "fprintf: test format string \"%s\"", format);

        testPrint(verbose, test_strbuf);

        /* Print a formatted string to the loopback device.  */
        switch (nargs)
        {
            case 0:
                ret = fprintf(LOOP0, format);
                break;
            case 1:
                ret = fprintf(LOOP0, format, args[0]);
                break;
            case 2:
                ret = fprintf(LOOP0, format, args[0], args[1]);
                break;
            case 3:
                ret = fprintf(LOOP0, format, args[0], args[1], args[2]);
                break;
            case 4:
                ret = fprintf(LOOP0, format, args[0], args[1], args[2], args[3]);
                break;
            default:
                failif(1, "invalid number of arguments");
                ret = -1;
                break;
        }

        /* Read the string back and compare with expected output.  */

        sprintf(test_strbuf, "wrote %d chars, expected %d chars", ret, len);
        failif(ret != len, test_strbuf);

        memset(obuf, 'X', len + 1);
        ret = read(LOOP0, obuf, len + 1);
        failif(ret != len, "failed to read all data printed");

        failif(0 != memcmp(obuf, expected_output, len),
               "fprintf did not print data correctly");

        /* Flush loopback device.  */
        for (j = 0; j < 10000; j++)
        {
            if (fgetc(LOOP0) == EOF)
            {
                break;
            }
        }
    }
    return passed;
}
Example #29
0
int main() {
  testPrint();
  testCppStreamPrint();
  printf("ok\n");
}
Example #30
0
static int ethloopn_test(bool verbose, int dev)
{
    bool passed = TRUE;
    bool subpass;
    uint memsize;
    int i, value, len;
    struct etherGram *inpkt;
    struct etherGram *outpkt;
    char *payload;
    char str[80];
    int devminor;
    struct ethloop *pelp;
    struct netaddr addr;
    device *pdev;

    pdev = (device *)&devtab[dev];
    devminor = pdev->minor;
    pelp = &elooptab[devminor];

    sprintf(str, "%s Open", pdev->name);
    testPrint(verbose, str);
    failif((SYSERR == open(dev)), "");

    /* Allocate temporary buffers.  */
    memsize = sizeof(struct etherGram) + MAX_PAYLOAD - 1;
    inpkt = memget(memsize);
    outpkt = memget(memsize);

    payload = &(outpkt->payload[0]);

    control(dev, NET_GET_HWADDR, (int)&addr, NULL);
    memcpy(outpkt->dst, addr.addr, addr.len);
    memcpy(outpkt->src, addr.addr, addr.len);
    outpkt->type_len = hs2net(ETH_TYPE_ARP);

    /* generate payload content */
    for (i = 0; i < MAX_PAYLOAD; i++)
    {
        /* Cycle through 0x20 to 0x7d (range of 0x5e) */
        value = (i % 0x5e) + 0x20;
        payload[i] = value;
    }

    /* oversized packet (paylod 1502 bytes + 14 byte header) */
    sprintf(str, "%s 1516 byte packet", pelp->dev->name);
    testPrint(verbose, str);
    len = write(dev, outpkt, 1516);
    failif((SYSERR != len), "");

    /* max packet (payload 1500 bytes + 14 byte header) */
    sprintf(str, "%s 1514 byte packet (write)", pelp->dev->name);
    testPrint(verbose, str);
    len = write(dev, outpkt, 1514);
    failif((len != 1514), "");

    sprintf(str, "%s 1514 byte packet (read)", pelp->dev->name);
    testPrint(verbose, str);
    bzero(inpkt, memsize);
    len = read(dev, inpkt, 1514);
    failif((len != 1514) || (0 != memcmp(outpkt, inpkt, 1514)), "");

    /* 'normal' packet (payload 686 bytes + 14 byte header) */
    sprintf(str, "%s  700 byte packet (write)", pelp->dev->name);
    testPrint(verbose, str);
    len = write(dev, outpkt, 700);
    failif((len != 700), "");

    sprintf(str, "%s  700 byte packet (read)", pelp->dev->name);
    testPrint(verbose, str);
    bzero(inpkt, memsize);
    len = read(dev, inpkt, 700);
    failif((len != 700) || (0 != memcmp(outpkt, inpkt, 700)), "");

    /* small packet (payload 16 bytes + 14 byte header) */
    sprintf(str, "%s   30 byte packet (write)", pelp->dev->name);
    testPrint(verbose, str);
    len = write(dev, outpkt, 30);
    failif((len != 30), "");

    sprintf(str, "%s   30 byte packet (read)", pelp->dev->name);
    testPrint(verbose, str);
    bzero(inpkt, memsize);
    len = read(dev, inpkt, 30);
    failif((len != 30) || (0 != memcmp(outpkt, inpkt, 30)), "");

    /* micro packet (12 bytes) */
    sprintf(str, "%s   12 byte packet", pelp->dev->name);
    testPrint(verbose, str);
    len = write(dev, outpkt, 12);
    failif((SYSERR != len), "");

    /* send 512 random sized packets */
    sprintf(str, "%s  512 random-sized packets", pelp->dev->name);
    testPrint(verbose, str);
    subpass = TRUE;

    for (i = 0; i < 512; i++)
    {
        len = 32 + (rand() % 1200);
        value = write(dev, outpkt, len);
        if (value != len)
        {
            subpass = FALSE;
        }

        bzero(inpkt, memsize);
        value = read(dev, inpkt, len);
        if ((value != len) || (0 != memcmp(outpkt, inpkt, len)))
        {
            subpass = FALSE;
        }
    }
    failif((TRUE != subpass), "");

    /* hold packet (payload 686 bytes + 14 byte header) */
    control(dev, ELOOP_CTRL_SETFLAG, ELOOP_FLAG_HOLDNXT, NULL);
    sprintf(str, "%s  700 byte packet (write hold)", pelp->dev->name);
    testPrint(verbose, str);
    len = write(dev, outpkt, 700);
    failif((len != 700), "");

    sprintf(str, "%s  700 byte packet (read hold)", pelp->dev->name);
    testPrint(verbose, str);
    bzero(inpkt, memsize);
    len = control(dev, ELOOP_CTRL_GETHOLD, (int)inpkt, 700);
    failif((0 != memcmp(outpkt, inpkt, 700)), "");

    /* drop packet (payload 686 bytes + 14 byte header) */
    control(dev, ELOOP_CTRL_SETFLAG, ELOOP_FLAG_DROPNXT, NULL);
    sprintf(str, "%s  700 byte packet (write drop)", pelp->dev->name);
    testPrint(verbose, str);
    len = write(dev, outpkt, 700);
    failif((len != 700), "");

    sprintf(str, "%s  700 byte packet (write)", pelp->dev->name);
    testPrint(verbose, str);
    outpkt->dst[0] += 1;
    len = write(dev, outpkt, 700);
    failif((len != 700), "");

    sprintf(str, "%s  700 byte packet (read)", pelp->dev->name);
    testPrint(verbose, str);
    bzero(inpkt, memsize);
    len = read(dev, inpkt, 700);
    failif((len != 700) || (0 != memcmp(outpkt, inpkt, 700)), "");

    /* Free temporary buffers. */
    memfree(outpkt, memsize);
    memfree(inpkt, memsize);

    sprintf(str, "%s Close", pdev->name);
    testPrint(verbose, str);
    failif((SYSERR == close(dev)), "");

    return passed;
}