Home | History | Annotate | Line # | Download | only in ffs
t_extattr.c revision 1.2.2.2
      1 /*	$NetBSD: t_extattr.c,v 1.2.2.2 2020/04/13 08:05:23 martin Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2020 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Christos Zoulas.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 #include <sys/cdefs.h>
     32 __RCSID("$NetBSD: t_extattr.c,v 1.2.2.2 2020/04/13 08:05:23 martin Exp $");
     33 
     34 #include <sys/types.h>
     35 #include <sys/mount.h>
     36 #include <sys/extattr.h>
     37 
     38 #include <atf-c.h>
     39 #include <errno.h>
     40 #include <fcntl.h>
     41 #include <pthread.h>
     42 #include <stdio.h>
     43 #include <stdlib.h>
     44 #include <unistd.h>
     45 #include <string.h>
     46 
     47 #include <rump/rump.h>
     48 #include <rump/rump_syscalls.h>
     49 
     50 #include <ufs/ufs/ufsmount.h>
     51 
     52 #include "h_macros.h"
     53 
     54 ATF_TC_WITH_CLEANUP(extattr);
     55 ATF_TC_HEAD(extattr, tc)
     56 {
     57 	atf_tc_set_md_var(tc, "descr", "test extended attribute support in "
     58 	    "ffsv2");
     59 	atf_tc_set_md_var(tc, "timeout", "5");
     60 }
     61 
     62 #define IMGNAME "extattr.img"
     63 
     64 #define G "/garage"
     65 #define M "mercedes"
     66 #define C "carburator"
     67 #define E "engine"
     68 #define T "trunk"
     69 #define S "suitcase"
     70 
     71 static const char *attrs[] = { E, T };
     72 
     73 static void
     74 check_list(const char *buf, ssize_t nr)
     75 {
     76 	size_t p = 0;
     77 	for (size_t i = 0; i < __arraycount(attrs); i++) {
     78 		size_t l = buf[p++];
     79 		if (l != strlen(attrs[i]))
     80 		    atf_tc_fail("got invalid len %zu expected %zu", l,
     81 			strlen(attrs[i]));
     82 		if (strncmp(&buf[p], attrs[i], l) != 0)
     83 			atf_tc_fail("got invalid str %.*s expected %s", (int)l,
     84 			    &buf[p], attrs[i]);
     85 		p += l;
     86 	}
     87 }
     88 
     89 // Make it ffsv2
     90 const char *newfs = "newfs -O 2 -F -s 10000 " IMGNAME;
     91 #define FAKEBLK "/dev/formula1"
     92 
     93 ATF_TC_BODY(extattr, tc)
     94 {
     95 	struct ufs_args args;
     96 	ssize_t nr;
     97 	int fd;
     98 	char buf[512];
     99 
    100 	if (system(newfs) == -1)
    101 		atf_tc_fail_errno("newfs failed");
    102 
    103 	memset(&args, 0, sizeof(args));
    104 	args.fspec = __UNCONST(FAKEBLK);
    105 
    106 	rump_init();
    107 	if (rump_sys_mkdir(G, 0777) == -1)
    108 		atf_tc_fail_errno("cannot create mountpoint");
    109 	rump_pub_etfs_register(FAKEBLK, IMGNAME, RUMP_ETFS_BLK);
    110 	if (rump_sys_mount(MOUNT_FFS, G, 0, &args, sizeof(args))==-1)
    111 		atf_tc_fail_errno("rump_sys_mount failed");
    112 
    113 	/* create extattr */
    114 	if (rump_sys_chdir(G) == 1)
    115 		atf_tc_fail_errno("chdir");
    116 	if ((fd = rump_sys_open(M, O_RDWR | O_CREAT, 0600)) == -1)
    117 		atf_tc_fail_errno("open");
    118 	if (rump_sys_write(fd, "hi mom\n", 7) != 7)
    119 		atf_tc_fail_errno("write");
    120 
    121 	if (rump_sys_extattr_set_fd(fd, EXTATTR_NAMESPACE_USER, E, C, 11) == -1)
    122 		atf_tc_fail_errno("exattr_set_fd");
    123 	if ((nr = rump_sys_extattr_get_file(M, EXTATTR_NAMESPACE_USER,
    124 	    E, buf, sizeof(buf))) != 11)
    125 		atf_tc_fail_errno("extattr_get_file");
    126 	if (strcmp(buf, C) != 0)
    127 		atf_tc_fail("got invalid str %s expected %s", buf, C);
    128 
    129 	if (rump_sys_extattr_set_file(M, EXTATTR_NAMESPACE_USER, T, S, 9) == -1)
    130 		atf_tc_fail_errno("extattr_set_file");
    131 	if ((nr = rump_sys_extattr_get_fd(fd, EXTATTR_NAMESPACE_USER,
    132 	    T, buf, sizeof(buf))) != 9)
    133 		atf_tc_fail_errno("extattr_get_fd");
    134 	if (strcmp(buf, S) != 0)
    135 		atf_tc_fail("got invalid str %s expected %s", buf, S);
    136 
    137 	if ((nr = rump_sys_extattr_list_fd(fd, EXTATTR_NAMESPACE_USER,
    138 	    buf, sizeof(buf))) != 13)
    139 		atf_tc_fail_errno("extattr_list_fd %zd", nr);
    140 	check_list(buf, __arraycount(attrs));
    141 
    142 	if ((nr = rump_sys_extattr_list_file(M, EXTATTR_NAMESPACE_USER,
    143 	    buf, sizeof(buf))) != 13)
    144 		atf_tc_fail_errno("extattr_list_file %zd", nr);
    145 	check_list(buf, __arraycount(attrs));
    146 
    147 	if (rump_sys_extattr_delete_fd(fd, EXTATTR_NAMESPACE_USER, T) == -1)
    148 		atf_tc_fail_errno("extattr_delete_fd");
    149 	if ((nr = rump_sys_extattr_list_fd(fd, EXTATTR_NAMESPACE_USER,
    150 	    buf, sizeof(buf))) != 7)
    151 		atf_tc_fail_errno("extattr_list_fd %zd", nr);
    152 	check_list(buf, 1);
    153 
    154 	if (rump_sys_extattr_delete_file(M, EXTATTR_NAMESPACE_USER, E) == -1)
    155 		atf_tc_fail_errno("extattr_delete_file");
    156 	if ((nr = rump_sys_extattr_list_fd(fd, EXTATTR_NAMESPACE_USER,
    157 	    buf, sizeof(buf))) != 0)
    158 		atf_tc_fail_errno("extattr_list_fd %zd", nr);
    159 
    160 	if (rump_sys_close(fd) == -1)
    161 		atf_tc_fail_errno("close");
    162 	if (rump_sys_unlink(M) == -1)
    163 		atf_tc_fail_errno("unlink");
    164 }
    165 
    166 ATF_TC_CLEANUP(extattr, tc)
    167 {
    168 
    169 	unlink(IMGNAME);
    170 }
    171 
    172 ATF_TP_ADD_TCS(tp)
    173 {
    174 	ATF_TP_ADD_TC(tp, extattr);
    175 	return atf_no_error();
    176 }
    177