ATF_TC_BODY(hexadecimal_rounding, tc) { ATF_REQUIRE(setlocale(LC_NUMERIC, "C")); fesetround(FE_TOWARDZERO); testfmt("0X1.23456789ABCP+0", "%.11A", 0x1.23456789abcdep0); testfmt("-0x1.23456p+0", "%.5a", -0x1.23456789abcdep0); testfmt("0x1.23456p+0", "%.5a", 0x1.23456789abcdep0); testfmt("0x1.234567p+0", "%.6a", 0x1.23456789abcdep0); testfmt("-0x1.234566p+0", "%.6a", -0x1.23456689abcdep0); fesetround(FE_DOWNWARD); testfmt("0X1.23456789ABCP+0", "%.11A", 0x1.23456789abcdep0); testfmt("-0x1.23457p+0", "%.5a", -0x1.23456789abcdep0); testfmt("0x1.23456p+0", "%.5a", 0x1.23456789abcdep0); testfmt("0x1.234567p+0", "%.6a", 0x1.23456789abcdep0); testfmt("-0x1.234567p+0", "%.6a", -0x1.23456689abcdep0); fesetround(FE_UPWARD); testfmt("0X1.23456789ABDP+0", "%.11A", 0x1.23456789abcdep0); testfmt("-0x1.23456p+0", "%.5a", -0x1.23456789abcdep0); testfmt("0x1.23457p+0", "%.5a", 0x1.23456789abcdep0); testfmt("0x1.234568p+0", "%.6a", 0x1.23456789abcdep0); testfmt("-0x1.234566p+0", "%.6a", -0x1.23456689abcdep0); fesetround(FE_TONEAREST); testfmt("0x1.23456789abcdep+4", "%a", 0x1.23456789abcdep4); testfmt("0X1.23456789ABDP+0", "%.11A", 0x1.23456789abcdep0); testfmt("-0x1.23456p+0", "%.5a", -0x1.23456789abcdep0); testfmt("0x1.23456p+0", "%.5a", 0x1.23456789abcdep0); testfmt("0x1.234568p+0", "%.6a", 0x1.23456789abcdep0); testfmt("-0x1.234567p+0", "%.6a", -0x1.23456689abcdep0); testfmt("0x1.00p-1029", "%.2a", 0x1.fffp-1030); testfmt("0x1.00p-1026", "%.2a", 0xf.fffp-1030); testfmt("0x1.83p+0", "%.2a", 1.51); }
ATF_TC_BODY(current_path__enoent, tc) { char* previous; ATF_REQUIRE(!kyua_error_is_set(kyua_fs_current_path(&previous))); ATF_REQUIRE(mkdir("root", 0755) != -1); ATF_REQUIRE(chdir("root") != -1); ATF_REQUIRE(rmdir("../root") != -1); char* cwd = (char*)0xdeadbeef; kyua_error_t error = kyua_fs_current_path(&cwd); ATF_REQUIRE(kyua_error_is_set(error)); ATF_REQUIRE(kyua_error_is_type(error, "libc")); ATF_REQUIRE_EQ(ENOENT, kyua_libc_error_errno(error)); ATF_REQUIRE_EQ((char*)0xdeadbeef, cwd); kyua_error_free(error); free(previous); }
ATF_TC_BODY(setlogin_perm, tc) { char *name; pid_t pid; int sta; pid = fork(); ATF_REQUIRE(pid >= 0); if (pid == 0) { (void)setsid(); errno = 0; if (setlogin("foobar") != -1) _exit(EXIT_FAILURE); if (errno != EPERM) _exit(EXIT_FAILURE); name = getlogin(); if (name == NULL) _exit(EXIT_FAILURE); if (strcmp(name, "foobar") == 0) _exit(EXIT_FAILURE); _exit(EXIT_SUCCESS); } (void)wait(&sta); if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS) atf_tc_fail("login name was set as an unprivileged user"); }
ATF_TC_BODY(require_errno, tc) { struct test { void (*head)(atf_tc_t *); void (*body)(const atf_tc_t *); bool ok; const char *exp_regex; } *t, tests[] = { { H_REQUIRE_ERRNO_HEAD_NAME(no_error), H_REQUIRE_ERRNO_BODY_NAME(no_error), false, "Expected true value in errno_ok_stub\\(\\) == -1" }, { H_REQUIRE_ERRNO_HEAD_NAME(errno_ok), H_REQUIRE_ERRNO_BODY_NAME(errno_ok), true, NULL }, { H_REQUIRE_ERRNO_HEAD_NAME(errno_fail), H_REQUIRE_ERRNO_BODY_NAME(errno_fail), false, "Expected errno 3, got 4, in errno_fail_stub\\(4\\) == -1" }, { NULL, NULL, false, NULL } }; for (t = &tests[0]; t->head != NULL; t++) { init_and_run_h_tc("h_require_errno", t->head, t->body); ATF_REQUIRE(exists("before")); if (t->ok) { ATF_REQUIRE(atf_utils_grep_file("^passed", "result")); ATF_REQUIRE(exists("after")); } else { ATF_REQUIRE(atf_utils_grep_file( "^failed: .*macros_test.c:[0-9]+: %s$", "result", t->exp_regex)); ATF_REQUIRE(!exists("after")); } ATF_REQUIRE(unlink("before") != -1); if (t->ok) ATF_REQUIRE(unlink("after") != -1); } }
ATF_TC_BODY(config, tcin) { atf_tc_t tc; const char *const config[] = { "test-var", "test-value", NULL }; RE(atf_tc_init(&tc, "test1", ATF_TC_HEAD_NAME(empty), ATF_TC_BODY_NAME(empty), NULL, NULL)); ATF_REQUIRE(!atf_tc_has_config_var(&tc, "test-var")); ATF_REQUIRE(!atf_tc_has_md_var(&tc, "test-var")); atf_tc_fini(&tc); RE(atf_tc_init(&tc, "test1", ATF_TC_HEAD_NAME(empty), ATF_TC_BODY_NAME(empty), NULL, config)); ATF_REQUIRE(atf_tc_has_config_var(&tc, "test-var")); ATF_REQUIRE(strcmp(atf_tc_get_config_var(&tc, "test-var"), "test-value") == 0); ATF_REQUIRE(!atf_tc_has_md_var(&tc, "test-var")); ATF_REQUIRE(!atf_tc_has_config_var(&tc, "test-var2")); ATF_REQUIRE(strcmp(atf_tc_get_config_var_wd(&tc, "test-var2", "def-value"), "def-value") == 0); atf_tc_fini(&tc); }
ATF_TC_BODY(sigio, tc) { struct sockaddr_in sin; int ls; int cs; int fl; int sc; signal(SIGIO, gotsig); RZ(system("rump_server -lrumpnet -lrumpnet_net -lrumpnet_netinet " RUMPSERV)); RL(setenv("RUMP_SERVER", RUMPSERV, 1)); RL(rumpclient_init()); RL(ls = rump_sys_socket(PF_INET, SOCK_STREAM, 0)); RL(rump_sys_fcntl(ls, F_SETOWN, rump_sys_getpid())); RL(fl = rump_sys_fcntl(ls, F_GETFL)); RL(rump_sys_fcntl(ls, F_SETFL, fl | O_ASYNC)); memset(&sin, 0, sizeof(sin)); sin.sin_len = sizeof(sin); sin.sin_family = AF_INET; sin.sin_port = htons(12345); RL(rump_sys_bind(ls, (struct sockaddr *)&sin, sizeof(sin))); RL(rump_sys_listen(ls, 5)); RL(cs = rump_sys_socket(PF_INET, SOCK_STREAM, 0)); sin.sin_addr.s_addr = inet_addr("127.0.0.1"); ATF_REQUIRE_EQ(sigcnt, 0); RL(rump_sys_connect(cs, (struct sockaddr *)&sin, sizeof(sin))); sc = sigcnt; printf("sigcnt after connect: %d\n", sc); ATF_REQUIRE(sc >= 1); }
ATF_TC_BODY(fpclassify_float, tc) { float d0, d1, d2, f, ip; int e, i; d0 = FLT_MIN; ATF_REQUIRE_EQ(fpclassify(d0), FP_NORMAL); f = frexpf(d0, &e); ATF_REQUIRE_EQ(e, FLT_MIN_EXP); ATF_REQUIRE_EQ(f, 0.5); d1 = d0; /* shift a "1" bit through the mantissa (skip the implicit bit) */ for (i = 1; i < FLT_MANT_DIG; i++) { d1 /= 2; ATF_REQUIRE_EQ(fpclassify(d1), FP_SUBNORMAL); ATF_REQUIRE(d1 > 0 && d1 < d0); d2 = ldexpf(d0, -i); ATF_REQUIRE_EQ(d2, d1); d2 = modff(d1, &ip); ATF_REQUIRE_EQ(d2, d1); ATF_REQUIRE_EQ(ip, 0); f = frexpf(d1, &e); ATF_REQUIRE_EQ(e, FLT_MIN_EXP - i); ATF_REQUIRE_EQ(f, 0.5); } d1 /= 2; ATF_REQUIRE_EQ(fpclassify(d1), FP_ZERO); f = frexpf(d1, &e); ATF_REQUIRE_EQ(e, 0); ATF_REQUIRE_EQ(f, 0); }
ATF_TC_BODY(check_parsing, tc) { struct pkg_dep_formula *f; const char *cases[] = { "name", "name = 1.0", "name >= 1.0,1", "name1, name2", "name1 | name2, name3", "name1 = 1.0 | name2 != 1.0, name3 > 1.0 < 2.0 != 1.5", "name1 = 1.0 | name2 != 1.0, name3 > 1.0 < 2.0 != 1.5, name4 +opt1 -opt2" }; char *r; int i; for (i = 0; i < sizeof(cases) / sizeof(cases[0]); i ++) { f = pkg_deps_parse_formula(cases[i]); ATF_REQUIRE(f != NULL); r = pkg_deps_formula_tostring(f); ATF_REQUIRE_STREQ(r, cases[i]); free(r); pkg_deps_formula_free(f); } }
ATF_TC_BODY(pkg_current_state_remote, tc) { struct pkg *package; package = gen_pkg(PKG_REMOTE); ATF_REQUIRE(package != NULL); /* Old version newer -> available */ pkg_set(package, PKG_OLD_VERSION, "1.2.3"); ATF_CHECK_EQ(pkgutils_pkg_current_state(package), PK_INFO_ENUM_AVAILABLE); /* Old version the same -> installed */ pkg_set(package, PKG_OLD_VERSION, "1.1.4"); ATF_CHECK_EQ(pkgutils_pkg_current_state(package), PK_INFO_ENUM_INSTALLED); /* Old version older -> available */ pkg_set(package, PKG_OLD_VERSION, "1.0.0"); ATF_CHECK_EQ(pkgutils_pkg_current_state(package), PK_INFO_ENUM_AVAILABLE); pkg_free(package); }
ATF_TC_BODY(pkg_install_state, tc) { struct pkg *package; package = gen_pkg(PKG_REMOTE); ATF_REQUIRE(package != NULL); /* Old version newer -> downgrading */ pkg_set(package, PKG_OLD_VERSION, "1.2.3"); ATF_CHECK_EQ(pkgutils_pkg_install_state(package), PK_INFO_ENUM_DOWNGRADING); /* Old version the same -> reinstalling */ pkg_set(package, PKG_OLD_VERSION, "1.1.4"); ATF_CHECK_EQ(pkgutils_pkg_install_state(package), PK_INFO_ENUM_REINSTALLING); /* Old version older -> updating */ pkg_set(package, PKG_OLD_VERSION, "1.0.0"); ATF_CHECK_EQ(pkgutils_pkg_install_state(package), PK_INFO_ENUM_UPDATING); pkg_free(package); }
/* * Test creating a symlink whose length is "len" bytes, not including * the terminating NUL. */ static void symlink_len(const atf_tc_t *tc, const char *mp, size_t len) { char *buf; int r; USES_SYMLINKS; RL(rump_sys_chdir(mp)); buf = malloc(len + 1); ATF_REQUIRE(buf); memset(buf, 'a', len); buf[len] = '\0'; r = rump_sys_symlink(buf, "afile"); if (r == -1) { ATF_REQUIRE_ERRNO(ENAMETOOLONG, r); } else { RL(rump_sys_unlink("afile")); } free(buf); RL(rump_sys_chdir("/")); }
ATF_TC_BODY(inet_addr_err, tc) { static const char *addrs[] = { ". . . .", "1.2.3.", "0.0.0.256", "255.255.255.256", "................................................", "a.b.c.d", "0x0.0x1.0x2.0x3", "-1.-1.-1.-1", "", " "}; struct in_addr ia; const char *ian; in_addr_t addr; size_t i; for (i = 0; i < __arraycount(addrs); i++) { (void)fprintf(stderr, "checking %s\n", addrs[i]);; addr = inet_addr(addrs[i]); ia.s_addr = addr; ian = inet_ntoa(ia); ATF_REQUIRE(ian != NULL); ATF_CHECK(strcmp(ian, addrs[i]) != 0); } }
ATF_TC_BODY(mmap_loan, tc) { char buf[BUFSIZE]; char *vp, *vp2; int fd; fd = open(path, O_RDWR | O_CREAT, 0600); ATF_REQUIRE(fd >= 0); (void)memset(buf, 'x', sizeof(buf)); (void)write(fd, buf, sizeof(buf)); vp = mmap(NULL, BUFSIZE, PROT_READ | PROT_WRITE, MAP_FILE | MAP_PRIVATE, fd, 0); ATF_REQUIRE(vp != MAP_FAILED); vp2 = vp; testloan(vp, vp2, 'A', 0); testloan(vp, vp2, 'B', 1); ATF_REQUIRE(munmap(vp, BUFSIZE) == 0); vp = mmap(NULL, BUFSIZE, PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, fd, 0); vp2 = mmap(NULL, BUFSIZE, PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, fd, 0); ATF_REQUIRE(vp != MAP_FAILED); ATF_REQUIRE(vp2 != MAP_FAILED); testloan(vp, vp2, 'E', 1); ATF_REQUIRE(munmap(vp, BUFSIZE) == 0); ATF_REQUIRE(munmap(vp2, BUFSIZE) == 0); }
ATF_TC_BODY(error_new, tc) { atf_error_t err; int data; err = atf_error_new("test_error", NULL, 0, NULL); ATF_REQUIRE(atf_error_is(err, "test_error")); ATF_REQUIRE(!atf_error_is(err, "unknown_error")); ATF_REQUIRE(atf_error_data(err) == NULL); atf_error_free(err); data = 5; err = atf_error_new("test_data_error", &data, sizeof(data), NULL); ATF_REQUIRE(atf_error_is(err, "test_data_error")); ATF_REQUIRE(!atf_error_is(err, "unknown_error")); ATF_REQUIRE(atf_error_data(err) != NULL); ATF_REQUIRE_EQ(*((const int *)atf_error_data(err)), 5); atf_error_free(err); }
ATF_TC_BODY(fdopen_seek, tc) { FILE *f; int fd; /* * Verify that the file position associated * with the stream corresponds with the offset * set earlier for the file descriptor. */ fd = open(path, O_RDWR | O_CREAT); ATF_REQUIRE(fd >= 0); ATF_REQUIRE(write(fd, "garbage", 7) == 7); ATF_REQUIRE(lseek(fd, 3, SEEK_SET) == 3); f = fdopen(fd, "r+"); ATF_REQUIRE(f != NULL); ATF_REQUIRE(ftell(f) == 3); ATF_REQUIRE(fclose(f) == 0); ATF_REQUIRE(unlink(path) == 0); }
ATF_TC_BODY(strtod_hex, tc) { const char *str; char *end; volatile double d; str = "-0x0"; d = strtod(str, &end); /* -0.0 */ ATF_REQUIRE(end == str + 4); ATF_REQUIRE(signbit(d) != 0); ATF_REQUIRE(fabs(d) < SMALL_NUM); str = "-0x"; d = strtod(str, &end); /* -0.0 */ ATF_REQUIRE(end == str + 2); ATF_REQUIRE(signbit(d) != 0); ATF_REQUIRE(fabs(d) < SMALL_NUM); }
ATF_TC_BODY(t_parser_test2, tc) { saslc_t *ctx; saslc_sess_t *sess; const char *val; ATF_REQUIRE(ctx = saslc_alloc()); ATF_CHECK_EQ(saslc_init(ctx, "test2", NULL), 0); ATF_REQUIRE((sess = saslc_sess_init(ctx, "ANONYMOUS", NULL))); ATF_REQUIRE(val = saslc_sess_getprop(sess, "TEST")); ATF_CHECK_STREQ(val, "one"); ATF_REQUIRE(val = saslc_sess_getprop(sess, "TEST2")); ATF_CHECK_STREQ(val, "one two"); ATF_REQUIRE(val = saslc_sess_getprop(sess, "TEST3")); ATF_CHECK_STREQ(val, "one two three"); ATF_REQUIRE(val = saslc_sess_getprop(sess, "ID")); ATF_CHECK_STREQ(val, "6669"); saslc_sess_end(sess); ATF_REQUIRE_EQ(saslc_end(ctx), 0); }
ATF_TC_BODY(closefrom_basic, tc) { int fd, cur1, cur2; (void)closefrom(STDERR_FILENO + 1); fd = open(path, O_RDONLY | O_CREAT, 0400); ATF_REQUIRE(fd >= 0); cur1 = fcntl(0, F_MAXFD); ATF_REQUIRE(cur1 == STDERR_FILENO + 1); ATF_REQUIRE(closefrom(cur1) == 0); cur2 = fcntl(0, F_MAXFD); ATF_REQUIRE(cur1 - 1 == cur2); ATF_REQUIRE(close(fd) == -1); ATF_REQUIRE(unlink(path) == 0); }
ATF_TC_BODY(rmdir_eperm, tc) { atf_fs_path_t p; atf_error_t err; RE(atf_fs_path_init_fmt(&p, "test-dir/foo")); ATF_REQUIRE(mkdir("test-dir", 0755) != -1); ATF_REQUIRE(mkdir("test-dir/foo", 0755) != -1); ATF_REQUIRE(chmod("test-dir", 0555) != -1); ATF_REQUIRE(exists(&p)); err = atf_fs_rmdir(&p); if (atf_user_is_root()) { ATF_REQUIRE(!atf_is_error(err)); } else { ATF_REQUIRE(atf_is_error(err)); ATF_REQUIRE(atf_error_is(err, "libc")); ATF_REQUIRE_EQ(atf_libc_error_code(err), EACCES); atf_error_free(err); } atf_fs_path_fini(&p); }
ATF_TC_BODY(stat_chflags, tc) { struct stat sa, sb; int fd; (void)memset(&sa, 0, sizeof(struct stat)); (void)memset(&sb, 0, sizeof(struct stat)); fd = open(path, O_RDONLY | O_CREAT); ATF_REQUIRE(fd != -1); ATF_REQUIRE(stat(path, &sa) == 0); ATF_REQUIRE(chflags(path, UF_NODUMP) == 0); ATF_REQUIRE(stat(path, &sb) == 0); if (sa.st_flags == sb.st_flags) atf_tc_fail("stat(2) did not detect chflags(2)"); ATF_REQUIRE(close(fd) == 0); ATF_REQUIRE(unlink(path) == 0); }
ATF_TC_BODY(link_err, tc) { char buf[MAXPATHLEN + 1]; int fd; (void)memset(buf, 'x', sizeof(buf)); pathl = getpath(); fd = open(path, O_RDWR | O_CREAT, 0600); ATF_REQUIRE(fd >= 0); ATF_REQUIRE(pathl != NULL); errno = 0; ATF_REQUIRE(link(path, pathl) == 0); ATF_REQUIRE_ERRNO(EEXIST, link(path, pathl) == -1); errno = 0; ATF_REQUIRE_ERRNO(ENAMETOOLONG, link(buf, "xxx") == -1); errno = 0; ATF_REQUIRE_ERRNO(ENOENT, link(path, "/d/c/b/a") == -1); errno = 0; ATF_REQUIRE_ERRNO(ENOENT, link("/a/b/c/d", path) == -1); errno = 0; ATF_REQUIRE_ERRNO(ENOENT, link("/a/b/c/d", "/d/c/b/a") == -1); errno = 0; ATF_REQUIRE_ERRNO(EFAULT, link(path, (const char *)-1) == -1); errno = 0; ATF_REQUIRE_ERRNO(EFAULT, link((const char *)-1, "xxx") == -1); ATF_REQUIRE(close(fd) == 0); ATF_REQUIRE(unlink(path) == 0); ATF_REQUIRE(unlink(pathl) == 0); }
ATF_TC_BODY(mmap_err, tc) { size_t addr = SIZE_MAX; void *map; errno = 0; map = mmap(NULL, 3, PROT_READ, MAP_FILE|MAP_PRIVATE, -1, 0); ATF_REQUIRE(map == MAP_FAILED); ATF_REQUIRE(errno == EBADF); errno = 0; map = mmap(&addr, page, PROT_READ, MAP_FIXED|MAP_PRIVATE, -1, 0); ATF_REQUIRE(map == MAP_FAILED); ATF_REQUIRE(errno == EINVAL); errno = 0; map = mmap(NULL, page, PROT_READ, MAP_ANON|MAP_PRIVATE, INT_MAX, 0); ATF_REQUIRE(map == MAP_FAILED); ATF_REQUIRE(errno == EINVAL); }
ATF_TC_BODY(libc_format, tc) { atf_error_t err; char buf[1024]; err = atf_libc_error(ENOMEM, "Test message 1"); atf_error_format(err, buf, sizeof(buf)); ATF_REQUIRE(strstr(buf, strerror(ENOMEM)) != NULL); ATF_REQUIRE(strstr(buf, "Test message 1") != NULL); atf_error_free(err); err = atf_libc_error(EPERM, "Test message 2"); atf_error_format(err, buf, sizeof(buf)); ATF_REQUIRE(strstr(buf, strerror(EPERM)) != NULL); ATF_REQUIRE(strstr(buf, "Test message 2") != NULL); atf_error_free(err); err = atf_libc_error(EPERM, "%s message %d", "Test", 3); atf_error_format(err, buf, sizeof(buf)); ATF_REQUIRE(strstr(buf, strerror(EPERM)) != NULL); ATF_REQUIRE(strstr(buf, "Test message 3") != NULL); atf_error_free(err); }
ATF_TC_BODY(mmap_block, tc) { static const int mib[] = { CTL_HW, HW_DISKNAMES }; static const unsigned int miblen = __arraycount(mib); char *map, *dk, *drives, dev[PATH_MAX]; size_t len; int fd = -1; atf_tc_skip("The test case causes a panic (PR kern/38889, kern/46592)"); ATF_REQUIRE(sysctl(mib, miblen, NULL, &len, NULL, 0) == 0); drives = malloc(len); ATF_REQUIRE(drives != NULL); ATF_REQUIRE(sysctl(mib, miblen, drives, &len, NULL, 0) == 0); for (dk = strtok(drives, " "); dk != NULL; dk = strtok(NULL, " ")) { sprintf(dev, _PATH_DEV "%s%c", dk, 'a'+RAW_PART); fprintf(stderr, "trying: %s\n", dev); if ((fd = open(dev, O_RDONLY)) >= 0) { (void)fprintf(stderr, "using %s\n", dev); break; } } free(drives); if (fd < 0) atf_tc_skip("failed to find suitable block device"); map = mmap(NULL, 4096, PROT_READ, MAP_FILE, fd, 0); ATF_REQUIRE(map != MAP_FAILED); (void)fprintf(stderr, "first byte %x\n", *map); ATF_REQUIRE(close(fd) == 0); (void)fprintf(stderr, "first byte %x\n", *map); ATF_REQUIRE(munmap(map, 4096) == 0); }
static void clone_group(struct group *dest, struct group const *src) { ATF_REQUIRE(dest != NULL); ATF_REQUIRE(src != NULL); char **cp; int members_num; memset(dest, 0, sizeof(struct group)); if (src->gr_name != NULL) { dest->gr_name = strdup(src->gr_name); ATF_REQUIRE(dest->gr_name != NULL); } if (src->gr_passwd != NULL) { dest->gr_passwd = strdup(src->gr_passwd); ATF_REQUIRE(dest->gr_passwd != NULL); } dest->gr_gid = src->gr_gid; if (src->gr_mem != NULL) { members_num = 0; for (cp = src->gr_mem; *cp; ++cp) ++members_num; dest->gr_mem = calloc(1, (members_num + 1) * sizeof(char *)); ATF_REQUIRE(dest->gr_mem != NULL); for (cp = src->gr_mem; *cp; ++cp) { dest->gr_mem[cp - src->gr_mem] = strdup(*cp); ATF_REQUIRE(dest->gr_mem[cp - src->gr_mem] != NULL); } } }
ATF_TC_BODY(check_sdp_get_int, tc) { uint8_t data[] = { 0x10, 0x00, // int8 0x00 0x10, 0x7f, // int8 0x7f 0x10, 0x80, // int8 0x80 0x11, 0x01, 0x02, // int16 0x0102 0x11, 0x7f, 0xff, // int16 0x7fff 0x11, 0x80, 0x00, // int16 0x8000 0x00, // nil 0x12, 0x01, 0x02, 0x03, // int32 0x01020304 0x04, 0x12, 0x7f, 0xff, 0xff, // int32 0x7fffffff 0xff, 0x12, 0x80, 0x00, 0x00, // int32 0x80000000 0x00, 0x13, 0x01, 0x02, 0x03, // int64 0x0102030405060708 0x04, 0x05, 0x06, 0x07, 0x08, 0x13, 0x7f, 0xff, 0xff, // int64 0x7fffffffffffffff 0xff, 0xff, 0xff, 0xff, 0xff, 0x13, 0x80, 0x00, 0x00, // int64 0x8000000000000000 0x00, 0x00, 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, // int128 0x00000000000000000000000000000000 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, // int128 0x00000000000000007fffffffffffffff 0x00, 0x00, 0x00, 0x00, // (INT64_MAX) 0x00, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x14, 0x00, 0x00, 0x00, // int128 0x00000000000000008000000000000000 0x00, 0x00, 0x00, 0x00, // (INT64_MAX + 1) 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x14, 0xff, 0xff, 0xff, // int128 0xffffffffffffffff8000000000000000 0xff, 0xff, 0xff, 0xff, // (INT64_MIN) 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x14, 0xff, 0xff, 0xff, // int128 0xffffffffffffffff7fffffffffffffff 0xff, 0xff, 0xff, 0xff, // (INT64_MIN - 1) 0xff, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, }; sdp_data_t test = { data, data + sizeof(data) }; sdp_data_t nil; intmax_t value; /* * sdp_get_int expects any INT type, advancing test if successful */ ATF_REQUIRE(sdp_get_int(&test, &value)); ATF_CHECK_EQ(value, 0); ATF_REQUIRE(sdp_get_int(&test, &value)); ATF_CHECK_EQ(value, INT8_MAX); ATF_REQUIRE(sdp_get_int(&test, &value)); ATF_CHECK_EQ(value, INT8_MIN); ATF_REQUIRE(sdp_get_int(&test, &value)); ATF_CHECK_EQ(value, 0x0102); ATF_REQUIRE(sdp_get_int(&test, &value)); ATF_CHECK_EQ(value, INT16_MAX); ATF_REQUIRE(sdp_get_int(&test, &value)); ATF_CHECK_EQ(value, INT16_MIN); ATF_REQUIRE_EQ(sdp_get_int(&test, &value), false); /* not int */ ATF_REQUIRE(sdp_get_data(&test, &nil)); /* (skip) */ ATF_CHECK_EQ(sdp_data_type(&nil), SDP_DATA_NIL); ATF_REQUIRE(sdp_get_int(&test, &value)); ATF_CHECK_EQ(value, 0x01020304); ATF_REQUIRE(sdp_get_int(&test, &value)); ATF_CHECK_EQ(value, INT32_MAX); ATF_REQUIRE(sdp_get_int(&test, &value)); ATF_CHECK_EQ(value, INT32_MIN); ATF_REQUIRE(sdp_get_int(&test, &value)); ATF_CHECK_EQ(value, 0x0102030405060708); ATF_REQUIRE(sdp_get_int(&test, &value)); ATF_CHECK_EQ(value, INT64_MAX); ATF_REQUIRE(sdp_get_int(&test, &value)); ATF_CHECK_EQ(value, INT64_MIN); /* * expected failure is that we cannot decode INT128 values larger than INT64 */ ATF_REQUIRE(sdp_get_int(&test, &value)); ATF_CHECK_EQ(value, 0); ATF_REQUIRE(sdp_get_int(&test, &value)); ATF_CHECK_EQ(value, INT64_MAX); ATF_REQUIRE_EQ(sdp_get_int(&test, &value), false); /* overflow */ ATF_REQUIRE(sdp_get_data(&test, &nil)); /* (skip) */ ATF_CHECK_EQ(sdp_data_type(&nil), SDP_DATA_INT128); ATF_REQUIRE(sdp_get_int(&test, &value)); ATF_CHECK_EQ(value, INT64_MIN); ATF_REQUIRE_EQ(sdp_get_int(&test, &value), false); /* underflow */ ATF_REQUIRE(sdp_get_data(&test, &nil)); /* (skip) */ ATF_CHECK_EQ(sdp_data_type(&nil), SDP_DATA_INT128); ATF_CHECK_EQ(test.next, test.end); }
ATF_TC_BODY(check_sdp_get_uint, tc) { uint8_t data[] = { 0x08, 0x00, // uint8 0x00 0x08, 0xff, // uint8 0xff 0x09, 0x01, 0x02, // uint16 0x0102 0x09, 0xff, 0xff, // uint16 0xffff 0x00, // nil 0x0a, 0x01, 0x02, 0x03, // uint32 0x01020304 0x04, 0x0a, 0xff, 0xff, 0xff, // uint32 0xffffffff 0xff, 0x0b, 0x01, 0x02, 0x03, // uint64 0x0102030405060708 0x04, 0x05, 0x06, 0x07, 0x08, 0x0b, 0xff, 0xff, 0xff, // uint64 0xffffffffffffffff 0xff, 0xff, 0xff, 0xff, 0xff, 0x0c, 0x00, 0x00, 0x00, // uint128 0x00000000000000000000000000000000 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, // uint128 0x00000000000000010000000000000000 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, // uint128 0x0000000000000000ffffffffffffffff 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, }; sdp_data_t test = { data, data + sizeof(data) }; sdp_data_t nil; uintmax_t value; /* * sdp_get_uint expects any UINT type, advancing test if successful */ ATF_REQUIRE(sdp_get_uint(&test, &value)); ATF_CHECK_EQ(value, 0x00); ATF_REQUIRE(sdp_get_uint(&test, &value)); ATF_CHECK_EQ(value, UINT8_MAX); ATF_REQUIRE(sdp_get_uint(&test, &value)); ATF_CHECK_EQ(value, 0x0102); ATF_REQUIRE(sdp_get_uint(&test, &value)); ATF_CHECK_EQ(value, UINT16_MAX); ATF_REQUIRE_EQ(sdp_get_uint(&test, &value), false); /* not uint */ ATF_REQUIRE(sdp_get_data(&test, &nil)); /* (skip) */ ATF_CHECK_EQ(sdp_data_type(&nil), SDP_DATA_NIL); ATF_REQUIRE(sdp_get_uint(&test, &value)); ATF_CHECK_EQ(value, 0x01020304); ATF_REQUIRE(sdp_get_uint(&test, &value)); ATF_CHECK_EQ(value, UINT32_MAX); ATF_REQUIRE(sdp_get_uint(&test, &value)); ATF_CHECK_EQ(value, 0x0102030405060708); ATF_REQUIRE(sdp_get_uint(&test, &value)); ATF_CHECK_EQ(value, UINT64_MAX); /* * expected failure is that we cannot decode UINT128 values larger than UINT64 */ ATF_REQUIRE(sdp_get_uint(&test, &value)); ATF_CHECK_EQ(value, 0x00000000000000000000000000000000); ATF_REQUIRE_EQ(sdp_get_uint(&test, &value), false); /* overflow */ ATF_REQUIRE(sdp_get_data(&test, &nil)); /* (skip) */ ATF_CHECK_EQ(sdp_data_type(&nil), SDP_DATA_UINT128); ATF_REQUIRE(sdp_get_uint(&test, &value)); ATF_CHECK_EQ(value, UINT64_MAX); ATF_CHECK_EQ(test.next, test.end); }
ATF_TC_BODY(csync, tc) { struct { const char *data; isc_boolean_t ok; } text_data[] = { { "", ISC_FALSE }, { "0", ISC_FALSE }, { "0 0", ISC_TRUE }, { "0 0 A", ISC_TRUE }, { "0 0 NS", ISC_TRUE }, { "0 0 AAAA", ISC_TRUE }, { "0 0 A AAAA", ISC_TRUE }, { "0 0 A NS AAAA", ISC_TRUE }, { "0 0 A NS AAAA BOGUS", ISC_FALSE }, { NULL, ISC_FALSE }, }; struct { unsigned char data[64]; size_t len; isc_boolean_t ok; } wire_data[] = { /* short */ { { 0x00 }, 0, ISC_FALSE }, /* short */ { { 0x00 }, 1, ISC_FALSE }, /* short */ { { 0x00, 0x00 }, 2, ISC_FALSE }, /* short */ { { 0x00, 0x00, 0x00 }, 3, ISC_FALSE }, /* short */ { { 0x00, 0x00, 0x00, 0x00 }, 4, ISC_FALSE }, /* short */ { { 0x00, 0x00, 0x00, 0x00, 0x00 }, 5, ISC_FALSE }, /* serial + flags only */ { { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, 6, ISC_TRUE }, /* bad type map */ { { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, 7, ISC_FALSE }, /* bad type map */ { { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, 8, ISC_FALSE }, /* good type map */ { { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02 }, 9, ISC_TRUE } }; unsigned char buf1[1024]; unsigned char buf2[1024]; isc_buffer_t source, target1, target2; isc_result_t result; size_t i; dns_rdataclass_t rdclass = dns_rdataclass_in; dns_rdatatype_t type = dns_rdatatype_csync; isc_lex_t *lex = NULL; dns_rdatacallbacks_t callbacks; dns_rdata_csync_t csync; dns_decompress_t dctx; UNUSED(tc); result = dns_test_begin(NULL, ISC_FALSE); ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); result = isc_lex_create(mctx, 64, &lex); ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); dns_rdatacallbacks_init(&callbacks); callbacks.error = error_callback; callbacks.warn = warn_callback; for (i = 0; text_data[i].data != NULL; i++) { size_t length = strlen(text_data[i].data); isc_buffer_constinit(&source, text_data[i].data, length); isc_buffer_add(&source, length); result = isc_lex_openbuffer(lex, &source); ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); isc_buffer_init(&target1, buf1, sizeof(buf1)); result = dns_rdata_fromtext(NULL, rdclass, type, lex, dns_rootname, 0, NULL, &target1, &callbacks); if (text_data[i].ok) ATF_CHECK_EQ(result, ISC_R_SUCCESS); else ATF_CHECK(result != ISC_R_SUCCESS); } isc_lex_destroy(&lex); for (i = 0; i < sizeof(wire_data)/sizeof(wire_data[0]); i++) { dns_rdata_t rdata = DNS_RDATA_INIT; isc_buffer_init(&source, wire_data[i].data, wire_data[i].len); isc_buffer_add(&source, wire_data[i].len); isc_buffer_setactive(&source, wire_data[i].len); isc_buffer_init(&target1, buf1, sizeof(buf1)); dns_decompress_init(&dctx, -1, DNS_DECOMPRESS_ANY); result = dns_rdata_fromwire(&rdata, rdclass, type, &source, &dctx, 0, &target1); dns_decompress_invalidate(&dctx); if (wire_data[i].ok) ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); else ATF_REQUIRE(result != ISC_R_SUCCESS); if (result != ISC_R_SUCCESS) continue; result = dns_rdata_tostruct(&rdata, &csync, NULL); ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); isc_buffer_init(&target2, buf2, sizeof(buf2)); dns_rdata_reset(&rdata); result = dns_rdata_fromstruct(&rdata, rdclass, type, &csync, &target2); ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); ATF_REQUIRE_EQ(isc_buffer_usedlength(&target2), wire_data[i].len); ATF_REQUIRE_EQ(memcmp(buf2, wire_data[i].data, wire_data[i].len), 0); } }
ATF_TC_BODY(isdn, tc) { struct { unsigned char data[64]; size_t len; isc_boolean_t ok; } test_data[] = { { /* "" */ { 0x00 }, 1, ISC_TRUE }, { /* "\001" */ { 0x1, 0x01 }, 2, ISC_TRUE }, { /* "\001" "" */ { 0x1, 0x01, 0x00 }, 3, ISC_TRUE }, { /* "\000" "\001" */ { 0x1, 0x01, 0x01, 0x01 }, 4, ISC_TRUE }, { /* sentinal */ { 0x00 }, 0, ISC_FALSE } }; unsigned char buf1[1024]; unsigned char buf2[1024]; isc_buffer_t source, target1, target2; dns_rdata_t rdata; dns_decompress_t dctx; isc_result_t result; size_t i; dns_rdata_isdn_t isdn; UNUSED(tc); for (i = 0; test_data[i].len != 0; i++) { isc_buffer_init(&source, test_data[i].data, test_data[i].len); isc_buffer_add(&source, test_data[i].len); isc_buffer_setactive(&source, test_data[i].len); isc_buffer_init(&target1, buf1, sizeof(buf1)); dns_rdata_init(&rdata); dns_decompress_init(&dctx, -1, DNS_DECOMPRESS_ANY); result = dns_rdata_fromwire(&rdata, dns_rdataclass_in, dns_rdatatype_isdn, &source, &dctx, 0, &target1); dns_decompress_invalidate(&dctx); if (test_data[i].ok) ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); else ATF_REQUIRE(result != ISC_R_SUCCESS); if (result != ISC_R_SUCCESS) continue; result = dns_rdata_tostruct(&rdata, &isdn, NULL); ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); isc_buffer_init(&target2, buf2, sizeof(buf2)); dns_rdata_reset(&rdata); result = dns_rdata_fromstruct(&rdata, dns_rdataclass_in, dns_rdatatype_isdn, &isdn, &target2); ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); ATF_REQUIRE_EQ(isc_buffer_usedlength(&target2), test_data[i].len); ATF_REQUIRE_EQ(memcmp(buf2, test_data[i].data, test_data[i].len), 0); } }
ATF_TC_BODY(mincore_resid, tc) { void *addr, *addr2, *addr3, *buf; size_t npgs = 0, resident; struct stat st; int fd, rv; struct rlimit rlim; ATF_REQUIRE(getrlimit(RLIMIT_MEMLOCK, &rlim) == 0); #ifdef __FreeBSD__ /* * Bump the mlock limit to unlimited so the rest of the testcase * passes instead of failing on the mlock call. */ rlim.rlim_max = RLIM_INFINITY; #endif rlim.rlim_cur = rlim.rlim_max; ATF_REQUIRE(setrlimit(RLIMIT_MEMLOCK, &rlim) == 0); (void)memset(&st, 0, sizeof(struct stat)); fd = open(path, O_RDWR | O_CREAT, 0700); buf = malloc(page * 5); ATF_REQUIRE(fd >= 0); ATF_REQUIRE(buf != NULL); rv = write(fd, buf, page * 5); ATF_REQUIRE(rv >= 0); ATF_REQUIRE(fd >= 0); ATF_REQUIRE(fstat(fd, &st) == 0); addr = mmap(NULL, (size_t)st.st_size, PROT_READ, MAP_FILE | MAP_SHARED, fd, (off_t) 0); ATF_REQUIRE(addr != MAP_FAILED); (void)close(fd); npgs = st.st_size / page; if (st.st_size % page != 0) npgs++; (void)check_residency(addr, npgs); rv = mlock(addr, npgs * page); if (rv == -1 && errno == EAGAIN) atf_tc_skip("hit process resource limits"); ATF_REQUIRE(munmap(addr, st.st_size) == 0); npgs = 128; #ifdef __FreeBSD__ addr = mmap(NULL, npgs * page, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, (off_t)0); #else addr = mmap(NULL, npgs * page, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE | MAP_WIRED, -1, (off_t)0); #endif if (addr == MAP_FAILED) atf_tc_skip("could not mmap wired anonymous test area, system " "might be low on memory"); #ifdef __FreeBSD__ if (mlock(addr, npgs * page) == -1 && errno != ENOMEM) atf_tc_skip("could not wire anonymous test area, system might " "be low on memory"); #endif ATF_REQUIRE(check_residency(addr, npgs) == npgs); ATF_REQUIRE(munmap(addr, npgs * page) == 0); npgs = 128; addr = mmap(NULL, npgs * page, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, (off_t)0); ATF_REQUIRE(addr != MAP_FAILED); /* * Check that the in-core pages match the locked pages. */ ATF_REQUIRE(check_residency(addr, npgs) == 0); errno = 0; if (mlockall(MCL_CURRENT|MCL_FUTURE) != 0 && errno != ENOMEM) atf_tc_fail("mlockall(2) failed"); if (errno == ENOMEM) atf_tc_skip("mlockall() exceeded process resource limits"); resident = check_residency(addr, npgs); if (resident < npgs) atf_tc_fail("mlockall(MCL_FUTURE) succeeded, still only " "%zu pages of the newly mapped %zu pages are resident", resident, npgs); addr2 = mmap(NULL, npgs * page, PROT_READ, MAP_ANON, -1, (off_t)0); addr3 = mmap(NULL, npgs * page, PROT_NONE, MAP_ANON, -1, (off_t)0); if (addr2 == MAP_FAILED || addr3 == MAP_FAILED) atf_tc_skip("could not mmap more anonymous test pages with " "mlockall(MCL_FUTURE) in effect, system " "might be low on memory"); ATF_REQUIRE(check_residency(addr2, npgs) == npgs); ATF_REQUIRE(check_residency(addr3, npgs) == 0); ATF_REQUIRE(mprotect(addr3, npgs * page, PROT_READ) == 0); ATF_REQUIRE(check_residency(addr, npgs) == npgs); ATF_REQUIRE(check_residency(addr2, npgs) == npgs); (void)munlockall(); ATF_REQUIRE(madvise(addr2, npgs * page, MADV_FREE) == 0); #ifdef __NetBSD__ ATF_REQUIRE(check_residency(addr2, npgs) == 0); #endif (void)memset(addr, 0, npgs * page); ATF_REQUIRE(madvise(addr, npgs * page, MADV_FREE) == 0); #ifdef __NetBSD__ ATF_REQUIRE(check_residency(addr, npgs) == 0); #endif (void)munmap(addr, npgs * page); (void)munmap(addr2, npgs * page); (void)munmap(addr3, npgs * page); (void)unlink(path); }