t_basic.c revision 1.1
11.1Spooka/* $NetBSD: t_basic.c,v 1.1 2010/03/30 01:02:47 pooka Exp $ */ 21.1Spooka 31.1Spooka#include <sys/types.h> 41.1Spooka#include <sys/mount.h> 51.1Spooka 61.1Spooka#include <atf-c.h> 71.1Spooka#include <err.h> 81.1Spooka#include <errno.h> 91.1Spooka#include <fcntl.h> 101.1Spooka#include <stdio.h> 111.1Spooka#include <unistd.h> 121.1Spooka#include <string.h> 131.1Spooka#include <stdlib.h> 141.1Spooka 151.1Spooka#include <rump/rump.h> 161.1Spooka#include <rump/rump_syscalls.h> 171.1Spooka 181.1Spooka#include <miscfs/nullfs/null.h> 191.1Spooka#include <fs/tmpfs/tmpfs_args.h> 201.1Spooka 211.1Spooka#define USE_ATF 221.1Spooka#include "../../h_macros.h" 231.1Spooka 241.1Spooka#ifdef USE_ATF 251.1SpookaATF_TC(basic); 261.1SpookaATF_TC_HEAD(basic, tc) 271.1Spooka{ 281.1Spooka atf_tc_set_md_var(tc, "descr", "basic nullfs functionality"); 291.1Spooka} 301.1Spooka#else 311.1Spooka#define atf_tc_fail(...) errx(1, __VA_ARGS__) 321.1Spooka#endif 331.1Spooka 341.1Spooka#define MSTR "magic bus" 351.1Spooka 361.1Spookastatic void 371.1Spookaxput_tfile(const char *path, const char *mstr) 381.1Spooka{ 391.1Spooka int fd; 401.1Spooka 411.1Spooka fd = rump_sys_open(path, O_CREAT | O_RDWR, 0777); 421.1Spooka if (fd == -1) 431.1Spooka atf_tc_fail_errno("create %s", path); 441.1Spooka if (rump_sys_write(fd, MSTR, sizeof(MSTR)) != sizeof(MSTR)) 451.1Spooka atf_tc_fail_errno("write to testfile"); 461.1Spooka rump_sys_close(fd); 471.1Spooka} 481.1Spooka 491.1Spookastatic int 501.1Spookaxread_tfile(const char *path, const char *mstr) 511.1Spooka{ 521.1Spooka char buf[128]; 531.1Spooka int fd; 541.1Spooka 551.1Spooka fd = rump_sys_open(path, O_RDONLY); 561.1Spooka if (fd == -1) 571.1Spooka return errno; 581.1Spooka if (rump_sys_read(fd, buf, sizeof(buf)) == -1) 591.1Spooka atf_tc_fail_errno("read tfile"); 601.1Spooka rump_sys_close(fd); 611.1Spooka if (strcmp(buf, MSTR) == 0) 621.1Spooka return 0; 631.1Spooka return EPROGMISMATCH; 641.1Spooka} 651.1Spooka 661.1Spooka#ifdef USE_ATF 671.1SpookaATF_TC_BODY(basic, tc) 681.1Spooka#else 691.1Spookaint main(int argc, char *argv[]) 701.1Spooka#endif 711.1Spooka{ 721.1Spooka struct null_args nargs; 731.1Spooka struct tmpfs_args targs; 741.1Spooka struct stat sb; 751.1Spooka int error; 761.1Spooka 771.1Spooka rump_init(); 781.1Spooka if (rump_sys_mkdir("/td1", 0777) == -1) 791.1Spooka atf_tc_fail_errno("mp1"); 801.1Spooka if (rump_sys_mkdir("/td2", 0777) == -1) 811.1Spooka atf_tc_fail_errno("mp1"); 821.1Spooka 831.1Spooka /* use tmpfs because rumpfs doesn't support regular files */ 841.1Spooka memset(&targs, 0, sizeof(targs)); 851.1Spooka targs.ta_version = TMPFS_ARGS_VERSION; 861.1Spooka targs.ta_root_mode = 0777; 871.1Spooka if (rump_sys_mount(MOUNT_TMPFS, "/td1", 0, &targs, sizeof(targs)) == -1) 881.1Spooka atf_tc_fail_errno("could not mount tmpfs td1"); 891.1Spooka 901.1Spooka memset(&nargs, 0, sizeof(nargs)); 911.1Spooka nargs.nulla_target = __UNCONST("/td1"); 921.1Spooka if (rump_sys_mount(MOUNT_NULL, "/td2", 0, &nargs, sizeof(nargs)) == -1) 931.1Spooka atf_tc_fail_errno("could not mount nullfs"); 941.1Spooka 951.1Spooka /* test unnull -> null */ 961.1Spooka xput_tfile("/td1/tensti", "jeppe"); 971.1Spooka error = xread_tfile("/td2/tensti", "jeppe"); 981.1Spooka if (error != 0) 991.1Spooka atf_tc_fail("null compare failed: %d (%s)", 1001.1Spooka error, strerror(error)); 1011.1Spooka 1021.1Spooka /* test null -> unnull */ 1031.1Spooka xput_tfile("/td2/kiekko", "keppi"); 1041.1Spooka error = xread_tfile("/td1/kiekko", "keppi"); 1051.1Spooka if (error != 0) 1061.1Spooka atf_tc_fail("unnull compare failed: %d (%s)", 1071.1Spooka error, strerror(error)); 1081.1Spooka 1091.1Spooka /* test unnull -> null overwrite */ 1101.1Spooka xput_tfile("/td1/tensti", "se oolannin sota"); 1111.1Spooka error = xread_tfile("/td2/tensti", "se oolannin sota"); 1121.1Spooka if (error != 0) 1131.1Spooka atf_tc_fail("unnull compare failed: %d (%s)", 1141.1Spooka error, strerror(error)); 1151.1Spooka 1161.1Spooka /* test that /td2 is unaffected in "real life" */ 1171.1Spooka if (rump_sys_unmount("/td2", 0) == -1) 1181.1Spooka atf_tc_fail_errno("cannot unmount nullfs"); 1191.1Spooka if ((error = rump_sys_stat("/td2/tensti", &sb)) != -1 1201.1Spooka || errno != ENOENT) { 1211.1Spooka atf_tc_fail("stat tensti should return ENOENT, got %d", error); 1221.1Spooka } 1231.1Spooka if ((error = rump_sys_stat("/td2/kiekko", &sb)) != -1 1241.1Spooka || errno != ENOENT) { 1251.1Spooka atf_tc_fail("stat kiekko should return ENOENT, got %d", error); 1261.1Spooka } 1271.1Spooka 1281.1Spooka /* done */ 1291.1Spooka} 1301.1Spooka 1311.1Spooka#ifdef USE_ATF 1321.1SpookaATF_TP_ADD_TCS(tp) 1331.1Spooka{ 1341.1Spooka ATF_TP_ADD_TC(tp, basic); 1351.1Spooka return 0; /*XXX?*/ 1361.1Spooka} 1371.1Spooka#endif 138