1168514Spjd# $NetBSD: t_symlink.sh,v 1.4 2010/06/04 08:39:40 jmmv Exp $
2168514Spjd#
3168514Spjd# Copyright (c) 2005, 2006, 2007, 2008 The NetBSD Foundation, Inc.
4168514Spjd# All rights reserved.
5168514Spjd#
6168514Spjd# Redistribution and use in source and binary forms, with or without
7168514Spjd# modification, are permitted provided that the following conditions
8168514Spjd# are met:
9168514Spjd# 1. Redistributions of source code must retain the above copyright
10168514Spjd#    notice, this list of conditions and the following disclaimer.
11168514Spjd# 2. Redistributions in binary form must reproduce the above copyright
12168514Spjd#    notice, this list of conditions and the following disclaimer in the
13168514Spjd#    documentation and/or other materials provided with the distribution.
14168514Spjd#
15168514Spjd# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
16168514Spjd# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17168514Spjd# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18168514Spjd# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
19168514Spjd# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20168514Spjd# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21168514Spjd# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22168514Spjd# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23168514Spjd# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24168514Spjd# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25168514Spjd# POSSIBILITY OF SUCH DAMAGE.
26168514Spjd#
27168514Spjd
28168514Spjd#
29168514Spjd# Verifies that the symlink and readlink operations work.
30168514Spjd#
31168514Spjd
32168514Spjdatf_test_case file
33168514Spjdfile_head() {
34168514Spjd	atf_set "descr" "Tests that symlinks to files work"
35168514Spjd	atf_set "require.user" "root"
36168514Spjd}
37168514Spjdfile_body() {
38179035Sattilio	test_mount
39168514Spjd
40168514Spjd	atf_check -s eq:0 -o empty -e empty touch a
41168514Spjd	atf_check -s eq:0 -o empty -e empty ln -s a b
42168514Spjd	[ $(md5 b | cut -d ' ' -f 4) = d41d8cd98f00b204e9800998ecf8427e ] || \
43168514Spjd	    atf_fail "Symlink points to an incorrect file"
44168514Spjd
45168514Spjd	atf_check -s eq:0 -o empty -e empty -x 'echo foo >a'
46	[ $(md5 b | cut -d ' ' -f 4) = d3b07384d113edec49eaa6238ad5ff00 ] || \
47	    atf_fail "Symlink points to an incorrect file"
48
49	test_unmount
50}
51
52atf_test_case exec
53exec_head() {
54	atf_set "descr" "Tests symlinking to a known system binary and" \
55	                "executing it through the symlink"
56	atf_set "require.user" "root"
57}
58exec_body() {
59	test_mount
60
61	atf_check -s eq:0 -o empty -e empty touch b
62	atf_check -s eq:0 -o empty -e empty ln -s /bin/cp cp
63	atf_check -s eq:0 -o empty -e empty ./cp b c
64	atf_check -s eq:0 -o empty -e empty test -f c
65
66	test_unmount
67}
68
69atf_test_case dir
70dir_head() {
71	atf_set "descr" "Tests that symlinks to directories work"
72	atf_set "require.user" "root"
73}
74dir_body() {
75	test_mount
76
77	atf_check -s eq:0 -o empty -e empty mkdir d
78	atf_check -s eq:1 -o empty -e empty test -f d/foo
79	atf_check -s eq:1 -o empty -e empty test -f e/foo
80	atf_check -s eq:0 -o empty -e empty ln -s d e
81	atf_check -s eq:0 -o empty -e empty touch d/foo
82	atf_check -s eq:0 -o empty -e empty test -f d/foo
83	atf_check -s eq:0 -o empty -e empty test -f e/foo
84
85	test_unmount
86}
87
88atf_test_case kqueue
89kqueue_head() {
90	atf_set "descr" "Tests that creating a symlink raises the" \
91	                "appropriate kqueue events"
92	atf_set "require.user" "root"
93}
94kqueue_body() {
95	test_mount
96
97	atf_check -s eq:0 -o empty -e empty mkdir dir
98	echo 'ln -s non-existent dir/a' | kqueue_monitor 1 dir
99	kqueue_check dir NOTE_WRITE
100	atf_check -s eq:0 -o empty -e empty rm dir/a
101	atf_check -s eq:0 -o empty -e empty rmdir dir
102
103	test_unmount
104}
105
106atf_init_test_cases() {
107	. $(atf_get_srcdir)/../h_funcs.subr
108	. $(atf_get_srcdir)/h_funcs.subr
109
110	atf_add_test_case file
111	atf_add_test_case exec
112	atf_add_test_case dir
113	atf_add_test_case kqueue
114}
115