1#!/bin/ksh -p
2#
3# CDDL HEADER START
4#
5# The contents of this file are subject to the terms of the
6# Common Development and Distribution License (the "License").
7# You may not use this file except in compliance with the License.
8#
9# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10# or https://opensource.org/licenses/CDDL-1.0.
11# See the License for the specific language governing permissions
12# and limitations under the License.
13#
14# When distributing Covered Code, include this CDDL HEADER in each
15# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16# If applicable, add the following below this CDDL HEADER, with the
17# fields enclosed by brackets "[]" replaced with your own identifying
18# information: Portions Copyright [yyyy] [name of copyright owner]
19#
20# CDDL HEADER END
21#
22
23#
24# Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
25# Use is subject to license terms.
26#
27
28#
29# Copyright (c) 2013 by Delphix. All rights reserved.
30#
31. $STF_SUITE/include/libtest.shlib
32. $STF_SUITE/tests/functional/userquota/userquota_common.kshlib
33
34#
35#
36# DESCRIPTION:
37#       Check whether chattr works as expected
38#
39#
40# STRATEGY:
41#       1. Create 3 files
42#       2. Use chattr to make them writable, immutable and appendonly
43#       3. Try to write and append to each file
44#
45
46set -A files writable immutable append
47
48function cleanup
49{
50	for i in ${files[*]}; do
51		if is_freebsd ; then
52			log_must chflags noschg $TESTDIR/$i
53			log_must rm -f $TESTDIR/$i
54		else
55			log_must chattr -ia $TESTDIR/$i
56			log_must rm -f $TESTDIR/$i
57		fi
58	done
59}
60
61log_onexit cleanup
62
63if is_freebsd ; then
64	log_assert "Check whether chflags works as expected"
65else
66	log_assert "Check whether chattr works as expected"
67fi
68
69log_must touch $TESTDIR/writable
70log_must touch $TESTDIR/immutable
71log_must touch $TESTDIR/append
72
73if is_freebsd ; then
74	log_must chflags noschg $TESTDIR/writable
75	log_must chflags schg $TESTDIR/immutable
76	log_must chflags sappnd $TESTDIR/append
77else
78	log_must chattr -i $TESTDIR/writable
79	log_must chattr +i $TESTDIR/immutable
80	log_must chattr +a $TESTDIR/append
81fi
82
83log_must eval "echo test > $TESTDIR/writable"
84log_must eval "echo test >> $TESTDIR/writable"
85log_mustnot eval "echo test > $TESTDIR/immutable"
86log_mustnot eval "echo test >> $TESTDIR/immutable"
87log_mustnot eval "echo test > $TESTDIR/append"
88log_must eval "echo test >> $TESTDIR/append"
89
90if is_freebsd ; then
91	log_pass "chflags works as expected"
92else
93	log_pass "chattr works as expected"
94fi
95