1# See the file LICENSE for redistribution information.
2#
3# Copyright (c) 2000,2008 Oracle.  All rights reserved.
4#
5# $Id: fop002.tcl,v 12.6 2008/01/08 20:58:53 bostic Exp $
6#
7# TEST	fop002.tcl
8# TEST	Test file system operations in the presence of bad permissions.
9proc fop002 { method args } {
10	source ./include.tcl
11
12	set args [convert_args $method $args]
13	set omethod [convert_method $method]
14
15	env_cleanup $testdir
16	puts "\nFop002: ($method) File system ops and permissions."
17	if { $is_windows_test == 1 } {
18		puts "\tSkipping permissions test for Windows platform."
19		return
20	}
21
22	# Create database with -rw-r--r-- permissions.
23	set perms "0644"
24	set testfile $testdir/a.db
25	set destfile $testdir/b.db
26
27	set db [eval \
28	    {berkdb_open -create} $omethod $args -mode $perms $testfile]
29	error_check_good db_open [is_valid_db $db] TRUE
30	error_check_good db_put [$db put 1 [chop_data $method a]] 0
31	error_check_good db_close [$db close] 0
32
33	# Eliminate all read and write permission, and try to execute
34	# file ops.  They should fail.
35	set res [exec chmod 0000 $testfile]
36	error_check_good remove_permissions [llength $res] 0
37	# Put remove last on the list of ops since it should succeed
38	# at the end of the test, removing the test file.
39	set ops [list open_create open rename remove]
40	set rdonly 0
41
42	puts "\tFop002.a: Test with neither read nor write permission."
43	foreach op $ops {
44		puts "\t\tFop002.a: Testing $op for failure."
45		switch $op {
46			open {
47				test_$op $testfile $omethod $args $rdonly 1
48			}
49			rename {
50				test_$op $testfile $destfile 1
51			}
52			open_create {
53				test_$op $testfile $omethod $args 1
54			}
55			remove {
56				test_$op $testfile 1
57			}
58		}
59	}
60
61	# Change permissions to read-only.
62	puts "\tFop002.b: Test with read-only permission."
63	set rdonly 1
64
65	set res [exec chmod 0444 $testfile]
66	error_check_good set_readonly [llength $res] 0
67
68	foreach op $ops {
69		puts "\t\tFop002.b: Testing $op for success."
70		switch $op {
71			open {
72				test_$op $testfile $omethod $args $rdonly 0
73			}
74			rename {
75				test_$op $testfile $destfile 0
76				# Move it back so later tests work
77				test_$op $destfile $testfile 0
78			}
79			open_create {
80				puts "\t\tSkipping open_create with read-only."
81			}
82			remove {
83				test_$op $testfile 0
84			}
85		}
86	}
87}
88
89proc test_remove { testfile {expectfail 0} } {
90	catch { berkdb dbremove $testfile } res
91	if { $expectfail == 1 } {
92		error_check_good remove_err $res "db remove:permission denied"
93	} else {
94		error_check_good remove $res 0
95	}
96}
97
98proc test_rename { testfile destfile {expectfail 0} } {
99	catch { berkdb dbrename $testfile $destfile } res
100	if { $expectfail == 1 } {
101		error_check_good rename_err $res "db rename:permission denied"
102	} else {
103		error_check_good rename $res 0
104	}
105}
106
107proc test_open_create { testfile omethod args {expectfail 0} } {
108	set stat [catch { set db \
109	    [eval {berkdb_open -create} $omethod $args $testfile]} res]
110	if { $expectfail == 1 } {
111		error_check_good open_create_err $res \
112		    "db open:permission denied"
113	} else {
114		error_check_good open_create $stat 0
115		# Since we succeeded, we have to close the db.
116		error_check_good db_close [$db close] 0
117	}
118}
119
120proc test_open { testfile omethod args {readonly 0} {expectfail 0} } {
121	if { $readonly == 1 } {
122		set stat [catch {set db \
123		    [eval {berkdb_open -rdonly} $omethod $args $testfile]} res]
124	} else {
125		set stat [catch {set db [berkdb_open $omethod $testfile]} res]
126	}
127	if { $expectfail == 1 } {
128		error_check_good open_err $res \
129		    "db open:permission denied"
130	} else {
131		error_check_good db_open $stat 0
132		error_check_good db_close [$db close] 0
133	}
134}
135
136