static int unittest_http_parse(int print_success)
{
	int success = 1, i;
	char *ut_successes[] = {
		"GET / HTTP/1.1\r\nUser-Agent: httperf/0.9.0\r\nHost: localhost\r\n\r\n",
		NULL
	};
	char *ut_pend[] = {
		"G",		/* 1 */
		"GET",
		"GET ",
		"GET /",
		"GET / ",	/* 5 */
		"GET / HT",
		"GET / HTTP/1.",
		"GET / HTTP/1.1",
		"GET / HTTP/1.1\r",
		"GET / HTTP/1.1\r\n", /* 10 */
		"GET / HTTP/1.1\r\nUser-",
		"GET / HTTP/1.1\r\nUser-blah:blah\r",
		NULL
	};
	char *ut_fail[] = {
		"GET / HTTP/1.2",
		"GET / HTTP/1.2\r\nUser-blah:blah\r", /* 14 */
		NULL
	};
	get_ret_t ret;

	for (i = 0 ; ut_successes[i] ; i++) {
		if (print_ut(ut_successes[i], strlen(ut_successes[i]), &ret, UT_SUCCESS, print_success)) {
			success = 0;
		}
	}
	for (i = 0 ; ut_pend[i] ; i++) {
		if (print_ut(ut_pend[i], strlen(ut_pend[i]), &ret, UT_PENDING, print_success)) {
			success = 0;
		}
	}
	for (i = 0 ; ut_fail[i] ; i++) {
		if (print_ut(ut_fail[i], strlen(ut_fail[i]), &ret, UT_FAIL, print_success)) {
			success = 0;
		}
	}

	if (success) return 0;
	return 1;
}
Example #2
0
int
main(int argc, char **argv)
{
    int c;
    int all, is_utmpx, do_getut;
    int f;
    char *fn;
    size_t recsize;
    size_t nread;
    union {
        struct utmp ut;
#ifdef UTMPX
        struct utmpx utx;
#endif
    } u;

    all = is_utmpx = do_getut = 0;
    recsize = sizeof(struct utmp);

    while ((c = getopt(argc, argv, OPTS)) != EOF) {
        switch (c) {
        case 'a':
            all = 1;
            break;
#ifdef UTMPX
        case 'x':
            is_utmpx = 1;
            recsize = sizeof(struct utmpx);
            break;
#endif
#ifdef UTN
        case 'g':
            do_getut = 1;
            break;
#endif
        default:
            usage(argv[0]);
        }
    }
    if (argc <= optind)
        usage(argv[0]);
    fn = argv[optind];
    if (!do_getut) {
        f = open(fn, O_RDONLY);
        if (f == -1) {
            perror(fn);
            exit(1);
        }
        while ((nread = read(f, &u, recsize)) > 0) {
            if (nread < recsize) {
                fprintf(stderr, "short read");
                close(f);
                exit(1);
            }
            if (is_utmpx) {
#ifdef UTMPX
                print_utx(all, &u.utx);
#else
                abort();
#endif
            } else {
                print_ut(all, &u.ut);
            }
        }
        if (nread == -1) {
            perror("read");
            exit(1);
        }
        close(f);
    } else {
        if (is_utmpx) {
#ifdef UTMPX
#ifdef HAVE_UTMPXNAME
            struct utmpx *utxp;
            utmpxname(fn);
            setutxent();
            while ((utxp = getutxent()) != NULL)
                print_utx(all, utxp);
#else
            fprintf(stderr, "no utmpxname(); can't use getutxent()\n");
            exit(1);
#endif
#else
            abort();
#endif
        } else {
#ifdef HAVE_UTMPNAME
            struct utmp *utp;
            utmpname(fn);
            setutxent();
            while ((utp = getutent()) != NULL)
                print_ut(all, utp);
#else
            fprintf(stderr, "no utmpname(); can't use getutent()\n");
            exit(1);
#endif
        }
    }
    exit(0);
}