1# See the file LICENSE for redistribution information.
2#
3# Copyright (c) 2003-2009 Oracle.  All rights reserved.
4#
5# $Id$
6#
7# TEST	fop003
8# TEST
9# TEST	Test behavior of create and truncate for compatibility
10# TEST	with sendmail.
11# TEST	1.  DB_TRUNCATE is not allowed with locking or transactions.
12# TEST	2.  Can -create into zero-length existing file.
13# TEST	3.  Can -create into non-zero-length existing file if and
14# TEST	only if DB_TRUNCATE is specified.
15proc fop003 { method args } {
16	global errorInfo
17	source ./include.tcl
18	env_cleanup $testdir
19
20	if { [is_btree $method] != 1 } {
21		puts "Skipping fop003 for method $method"
22		return
23	}
24
25	set args [convert_args $method $args]
26	set omethod [convert_method $method]
27
28	set tnum "003"
29	set testfile fop$tnum.db
30	puts "Fop$tnum ($method): Test of required behavior for sendmail."
31
32	puts "\tFop$tnum.a: -truncate is not allowed within\
33	    txn or locking env."
34	set envflags "lock txn"
35	foreach flag $envflags {
36		set env [berkdb_env_noerr -create -home $testdir -$flag]
37		set db [eval {berkdb_open_noerr -create} \
38		    $omethod $args -env $env $testfile]
39		error_check_good db_open [is_valid_db $db] TRUE
40		error_check_good db_close [$db close] 0
41		catch {[berkdb_open_noerr -truncate $omethod $args -env $env \
42		    $testfile]} res
43		error_check_good "$flag env not allowed" [is_substr $res \
44		    "DB_TRUNCATE illegal with locking specified"] 1
45		error_check_good dbremove [$env dbremove $testfile] 0
46		error_check_good env_close [$env close] 0
47		error_check_good envremove [berkdb envremove -home $testdir] 0
48	}
49
50	puts "\tFop$tnum.b: -create is allowed on open of existing\
51	    zero-length file."
52	# Create an empty file, then open with -create.  We get an
53	# error message warning us that this does not look like a
54	# DB file, but the open should succeed.
55	set fd [open $testdir/foo w]
56	close $fd
57	catch {set db [eval \
58	    {berkdb_open_noerr -create} $omethod $args $testdir/foo]} res
59	error_check_good open_fail [is_substr $errorInfo \
60	    "unexpected file type or format"] 1
61	error_check_good db_open [is_valid_db $db] TRUE
62	error_check_good db_close [$db close] 0
63
64	puts "\tFop$tnum.c: -create is ignored on open of existing\
65	    non-zero-length file."
66	# Create a db file.  Close and reopen with -create.  Make
67	# sure that we still have the same file by checking the contents.
68	set key 1
69	set data "data"
70	set file "file.db"
71	set db [eval {berkdb_open -create $omethod} $args $testdir/$file]
72	error_check_good db_open [is_valid_db $db] TRUE
73	error_check_good db_put [$db put $key [chop_data $method $data]] 0
74	error_check_good db_close [$db close] 0
75	set db [eval {berkdb_open -create $omethod} $args $testdir/$file]
76	error_check_good db_open2 [is_valid_db $db] TRUE
77	set ret [$db get $key]
78	error_check_good db_get \
79	    [lindex [lindex $ret 0] 1] [pad_data $method $data]
80	error_check_good db_close2 [$db close] 0
81
82	puts "\tFop$tnum.d: -create is allowed on open -truncate of\
83	    existing non-zero-length file."
84	# Use the file we already have with -truncate flag.  The open
85	# should be successful, and when we query for the key that
86	# used to be there, we should get nothing.
87	set db [eval \
88	    {berkdb_open -create -truncate $omethod} $args $testdir/$file]
89	error_check_good db_open3 [is_valid_db $db] TRUE
90	set ret [$db get $key]
91	error_check_good db_get [lindex [lindex $ret 0] 1] ""
92	error_check_good db_close3 [$db close] 0
93
94}
95