Exemplo n.º 1
0
int
main()
{
	int fd;
	uint32_t magic = 0xffff5555;
	uint32_t w1 = 0xcafef00d, w2 = 0xdeadbeef, w3 = 0xd00d, w4 = 0xbabe;
	const char *s1 = "0123456789abcdef, end here.";
	const char *s2 = "some other more reasonable string";
	uint32_t w;
	const char *s;
	comm_msg_t *msg;

	/* Serializing. */

	msg = comm_msg_new(20, 0);

	comm_msg_put_magic(msg, magic);

	comm_msg_put_int(msg, w1);
	comm_msg_put_int(msg, w2);
	comm_msg_put_str(msg, s1);
	comm_msg_put_str(msg, s2);
	comm_msg_put_int(msg, w3);
	comm_msg_put_int(msg, w4);

	fd = open(test_file, O_WRONLY | O_CREAT, 0644);
	if (fd < 0)
		die(1, "cannot create file");

	comm_msg_send(fd, msg);
	comm_msg_destroy(msg);

	close(fd);

	/* Deserializing. */

	msg = comm_msg_new(5, 0);

	fd = open(test_file, O_RDONLY, 0644);
	if (fd < 0)
		die(2, "cannot open file");

	comm_msg_recv(fd, msg);

	tests_init(8);

	if (comm_msg_get_magic(msg, &w))
		test_cmp_int(magic, w);

	if (comm_msg_get_int(msg, &w))
		test_cmp_int(w1, w);
	if (comm_msg_get_int(msg, &w))
		test_cmp_int(w2, w);
	if (comm_msg_get_str(msg, &s))
		test_cmp_str(s1, s);
	if (comm_msg_get_str(msg, &s))
		test_cmp_str(s2, s);
	if (comm_msg_get_int(msg, &w))
		test_cmp_int(w3, w);
	if (comm_msg_get_int(msg, &w))
		test_cmp_int(w4, w);

	comm_msg_destroy(msg);

	close(fd);

	unlink(test_file);

	/* Capped buffer. */

	msg = comm_msg_new(4, 8);

	comm_msg_put_int(msg, w1);
	comm_msg_put_int(msg, w2);
	test_failure(comm_msg_put_int(msg, w3));

	comm_msg_destroy(msg);

	return tests_summary() ? 0 : 1;
}
Exemplo n.º 2
0
int
tests_begin(int argc, char * const *argv)
{
    const char *testcase = NULL;
    bool initialized = false;
    int verbose=0;
    int testix = -1;
	int ch;
    int retval;
    char *seed=NULL;
    for (;;) {
        while (!testcase && (ch = getopt(argc, argv, "vws:")) != -1)
        {
            switch  (ch)
            {
            case 's':
                seed = optarg;
                break;
            case 'w':
                sleep(100);
                break;
            case 'v':
                verbose=1;
                break;
            case '?':
            default:
                printf("invalid option %c\n",ch); 
                usage(argv[0]);
            }
        }

        if (optind < argc) {
            testix = tests_named_index(argv[optind]);
            if(testix<0) {
                printf("invalid test %s\n",argv[optind]); 
                usage(argv[0]);
            }
            argc -= optind;
            argv += optind;
        }

        if (testix < 0) {
            if (!initialized) {
                //initialized = true;
                if (tests_init(seed)!=0) return -1;
                tests_run_all(argc, argv);
            }
            break;
        } else {
            if (!initialized) {
                if (tests_init(seed)!=0) return -1;
                initialized = true;
            }
            tests_run_index(testix, argc, argv);
            testix = -1;
        }
    }

    retval=tests_summary(verbose);

    /* Cleanups */
    tests_end();
    
    return retval;
}
Exemplo n.º 3
0
int main()
{
    /*
     * The tests will be:
     * 	Feeding too much data.
     * 	Recovering and feeding an empty message.
     * 	Feeding a normal message.
     * 	Feeding a full-length message.
     * 	Feeding a message with separated \r and \n in the middle.
     * 	Feeding too much data again.
     * 	Recovering and feeding a normal message.
     */
    static const char *blocks[] = {
        "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
        "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
        "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
        "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
        "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
        "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
        "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
        "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
        "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
        "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
        "xxxxxxxxxxx\r\n\r\nPRIVMSG gregor :We don't want any bugs ",
        "in here\r\n" "01234567890123456789012345678901234567890123456789",
        "01234567890123456789012345678901234567890123456789",
        "01234567890123456789012345678901234567890123456789",
        "01234567890123456789012345678901234567890123456789",
        "01234567890123456789012345678901234567890123456789",
        "01234567890123456789012345678901234567890123456789",
        "01234567890123456789012345678901234567890123456789",
        "01234567890123456789012345678901234567890123456789",
        "01234567890123456789012345678901234567890123456789",
        "01234567890123456789012345678901234567890123456789",
        "0123456789\r\n" "This is \ra weird message\r\r\n",
        "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
        "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
        "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
        "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
        "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
        "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
        "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
        "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
        "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
        "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
        "xxxxxxxxxxxxxxxxxxxxx\r\n",
        "JOIN #kafka\r\n",
        NULL
    };

    int i;
    int pipefd[2];
    ssize_t l;
    int ret;
    ssize_t wret;

    struct reader r;
    struct buffer b;

    ret = pipe(pipefd);
    assert(ret == 0);
    ret = nonblock(pipefd[0]);
    assert(ret == 0);
    ret = buffer_create(&b, MAX_MESSAGE_LEN);
    assert(ret == 0);
    reader_init(&r, pipefd + 0, test_callback, &b);

    init_counters();


    for (i = 0; blocks[i] != NULL; ++i) {
        l = (ssize_t)strlen(blocks[i]);
        wret = write(pipefd[1], blocks[i], l);
        assert(wret == l);
        read_and_callback(&r, NULL);
    }

    close(pipefd[1]);

    ++tests_counter;
    if (read_and_callback(&r, NULL) != 0) {
        printf("Unable to correctly receive EOF\n");
        ++tests_failed;
    } else
        ++tests_passed;

    close(pipefd[0]);
    ret = buffer_destroy(&b);
    assert(ret == 0);
    return tests_summary();
}