1
# See the file LICENSE for redistribution information.
3
# Copyright (c) 1999-2002
4
# Sleepycat Software. All rights reserved.
9
# TEST Test of the DB_RDONLY flag to DB->open
10
# TEST Attempt to both DB->put and DBC->c_put into a database
11
# TEST that has been opened DB_RDONLY, and check for failure.
12
proc test063 { method args } {
16
set args [convert_args $method $args]
17
set omethod [convert_method $method]
21
set eindex [lsearch -exact $args "-env"]
23
# If we are using an env, then testfile should just be the db name.
24
# Otherwise it is the test directory and the name.
25
if { $eindex == -1 } {
26
set testfile $testdir/test0$tnum.db
29
set testfile test0$tnum.db
31
set env [lindex $args $eindex]
32
set txnenv [is_txnenv $env]
34
append args " -auto_commit "
36
set testdir [get_home $env]
42
set key2 "another_key"
48
if { [is_record_based $method] == 1 } {
51
append gflags " -recno"
54
puts "Test0$tnum: $method ($args) DB_RDONLY test."
56
# Create a test database.
57
puts "\tTest0$tnum.a: Creating test database."
58
set db [eval {berkdb_open_noerr -create -mode 0644} \
59
$omethod $args $testfile]
60
error_check_good db_create [is_valid_db $db] TRUE
62
# Put and get an item so it's nonempty.
65
error_check_good txn [is_valid_txn $t $env] TRUE
68
set ret [eval {$db put} $txn {$key [chop_data $method $data]}]
69
error_check_good initial_put $ret 0
71
set dbt [eval {$db get} $txn $gflags {$key}]
72
error_check_good initial_get $dbt \
73
[list [list $key [pad_data $method $data]]]
76
error_check_good txn [$t commit] 0
78
error_check_good db_close [$db close] 0
80
if { $eindex == -1 } {
81
# Confirm that database is writable. If we are
82
# using an env (that may be remote on a server)
83
# we cannot do this check.
84
error_check_good writable [file writable $testfile] 1
87
puts "\tTest0$tnum.b: Re-opening DB_RDONLY and attempting to put."
89
# Now open it read-only and make sure we can get but not put.
90
set db [eval {berkdb_open_noerr -rdonly} $args {$testfile}]
91
error_check_good db_open [is_valid_db $db] TRUE
95
error_check_good txn [is_valid_txn $t $env] TRUE
98
set dbt [eval {$db get} $txn $gflags {$key}]
99
error_check_good db_get $dbt \
100
[list [list $key [pad_data $method $data]]]
102
set ret [catch {eval {$db put} $txn \
103
{$key2 [chop_data $method $data]}} res]
104
error_check_good put_failed $ret 1
105
error_check_good db_put_rdonly [is_substr $errorCode "EACCES"] 1
106
if { $txnenv == 1 } {
107
error_check_good txn [$t commit] 0
112
puts "\tTest0$tnum.c: Attempting cursor put."
114
if { $txnenv == 1 } {
116
error_check_good txn [is_valid_txn $t $env] TRUE
119
set dbc [eval {$db cursor} $txn]
120
error_check_good cursor_create [is_valid_cursor $dbc $db] TRUE
122
error_check_good cursor_set [$dbc get -first] $dbt
123
set ret [catch {eval {$dbc put} -current $data} res]
124
error_check_good c_put_failed $ret 1
125
error_check_good dbc_put_rdonly [is_substr $errorCode "EACCES"] 1
127
set dbt [eval {$db get} $gflags {$key2}]
128
error_check_good db_get_key2 $dbt ""
130
puts "\tTest0$tnum.d: Attempting ordinary delete."
133
set ret [catch {eval {$db del} $txn {$key}} 1]
134
error_check_good del_failed $ret 1
135
error_check_good db_del_rdonly [is_substr $errorCode "EACCES"] 1
137
set dbt [eval {$db get} $txn $gflags {$key}]
138
error_check_good db_get_key $dbt \
139
[list [list $key [pad_data $method $data]]]
141
puts "\tTest0$tnum.e: Attempting cursor delete."
142
# Just set the cursor to the beginning; we don't care what's there...
144
set dbt2 [$dbc get -first]
145
error_check_good db_get_first_key $dbt2 $dbt
147
set ret [catch {$dbc del} res]
148
error_check_good c_del_failed $ret 1
149
error_check_good dbc_del_rdonly [is_substr $errorCode "EACCES"] 1
151
set dbt2 [$dbc get -current]
152
error_check_good db_get_key $dbt2 $dbt
154
puts "\tTest0$tnum.f: Close, reopen db; verify unchanged."
156
error_check_good dbc_close [$dbc close] 0
157
if { $txnenv == 1 } {
158
error_check_good txn [$t commit] 0
160
error_check_good db_close [$db close] 0
162
set db [eval {berkdb_open} $omethod $args $testfile]
163
error_check_good db_reopen [is_valid_db $db] TRUE
166
error_check_good cursor_create [is_valid_cursor $dbc $db] TRUE
168
error_check_good first_there [$dbc get -first] \
169
[list [list $key [pad_data $method $data]]]
170
error_check_good nomore_there [$dbc get -next] ""
172
error_check_good dbc_close [$dbc close] 0
173
error_check_good db_close [$db close] 0