1# Copyright (c) 1999, 2020 Oracle and/or its affiliates.  All rights reserved.
2#
3# See the file LICENSE for license information.
4#
5# $Id$
6#
7# TEST	test051
8# TEST	Fixed-length record Recno test.
9# TEST		0. Test various flags (legal and illegal) to open
10# TEST		1. Test partial puts where dlen != size (should fail)
11# TEST		2. Partial puts for existent record -- replaces at beg, mid, and
12# TEST			end of record, as well as full replace
13proc test051 { method { args "" } } {
14	global fixed_len
15	global errorInfo
16	global errorCode
17	source ./include.tcl
18
19	set args [convert_args $method $args]
20	set omethod [convert_method $method]
21
22	puts "Test051 ($method): Test of the fixed length records."
23	if { [is_fixed_length $method] != 1 } {
24		puts "Test051: skipping for method $method"
25		return
26	}
27	if { [is_partitioned $args] } {
28		puts "Test051 skipping for partitioned $omethod"
29		return
30	}
31
32	# Create the database and open the dictionary
33	set txnenv 0
34	set eindex [lsearch -exact $args "-env"]
35	#
36	# If we are using an env, then testfile should just be the db name.
37	# Otherwise it is the test directory and the name.
38	if { $eindex == -1 } {
39		set testfile $testdir/test051.db
40		set testfile1 $testdir/test051a.db
41		set env NULL
42	} else {
43		set testfile test051.db
44		set testfile1 test051a.db
45		incr eindex
46		set env [lindex $args $eindex]
47		set txnenv [is_txnenv $env]
48		if { $txnenv == 1 } {
49			append args " -auto_commit "
50		}
51		set testdir [get_home $env]
52	}
53	cleanup $testdir $env
54	set oflags "-create -mode 0644 $args"
55
56	# Test various flags (legal and illegal) to open
57	puts "\tTest051.a: Test correct flag behavior on open."
58	set errorCode NONE
59	foreach f { "-dup" "-dup -dupsort" "-recnum" } {
60		puts "\t\tTest051.a: Test flag $f"
61		set stat [catch {eval {berkdb_open_noerr} $oflags $f $omethod \
62		    $testfile} ret]
63		error_check_good dbopen:flagtest:catch $stat 1
64		error_check_good \
65		    dbopen:flagtest:$f [is_substr $errorCode EINVAL] 1
66		set errorCode NONE
67	}
68	set f "-renumber"
69	puts "\t\tTest051.a: Test $f"
70	if { [is_frecno $method] == 1 } {
71		set db [eval {berkdb_open} $oflags $f $omethod $testfile]
72		error_check_good dbopen:flagtest:$f [is_valid_db $db] TRUE
73		$db close
74	} else {
75		error_check_good \
76		    dbopen:flagtest:catch [catch {eval {berkdb_open_noerr}\
77			$oflags $f $omethod $testfile} ret] 1
78		error_check_good \
79		    dbopen:flagtest:$f [is_substr $errorCode EINVAL] 1
80	}
81
82	# Test partial puts where dlen != size (should fail)
83	# it is an error to specify a partial put w/ different
84	# dlen and size in fixed length recno/queue
85	set key 1
86	set data ""
87	set txn ""
88	set test_char "a"
89
90	set db [eval {berkdb_open_noerr} $oflags $omethod $testfile1]
91	error_check_good dbopen [is_valid_db $db] TRUE
92
93	if { $txnenv == 1 } {
94		set t [$env txn]
95		error_check_good txn [is_valid_txn $t $env] TRUE
96		set txn "-txn $t"
97	}
98	puts "\tTest051.b: Partial puts with dlen != size."
99	foreach dlen { 1 16 20 32 } {
100		foreach doff { 0 10 20 32 } {
101			# dlen < size
102			puts "\t\tTest051.e: dlen: $dlen, doff: $doff, \
103			    size: [expr $dlen+1]"
104			set data [repeat $test_char [expr $dlen + 1]]
105			error_check_good \
106			    catch:put 1 [catch {eval {$db put -partial \
107			    [list $doff $dlen]} $txn {$key $data}} ret]
108
109			# We don't get back the server error string just
110			# the result.
111			if { $eindex == -1 } {
112				error_check_good "dbput:partial: dlen < size" \
113				    [is_substr \
114				    $errorInfo "ecord length"] 1
115			} else {
116				error_check_good "dbput:partial: dlen < size" \
117				    [is_substr $errorCode "EINVAL"] 1
118			}
119
120			# dlen > size
121			puts "\t\tTest051.e: dlen: $dlen, doff: $doff, \
122			    size: [expr $dlen-1]"
123			set data [repeat $test_char [expr $dlen - 1]]
124			error_check_good \
125			    catch:put 1 [catch {eval {$db put -partial \
126			    [list $doff $dlen]} $txn {$key $data}} ret]
127			if { $eindex == -1 } {
128				error_check_good "dbput:partial: dlen > size" \
129				    [is_substr \
130				    $errorInfo "ecord length"] 1
131			} else {
132				error_check_good "dbput:partial: dlen < size" \
133				    [is_substr $errorCode "EINVAL"] 1
134			}
135		}
136	}
137
138	if { $txnenv == 1 } {
139		error_check_good txn [$t commit] 0
140	}
141	$db close
142
143	# Partial puts for existent record -- replaces at beg, mid, and
144	# end of record, as well as full replace
145	puts "\tTest051.f: Partial puts within existent record."
146	set db [eval {berkdb_open} $oflags $omethod $testfile]
147	error_check_good dbopen [is_valid_db $db] TRUE
148
149	puts "\t\tTest051.f: First try a put and then a full replace."
150	set data [repeat "a" $fixed_len]
151
152	if { $txnenv == 1 } {
153		set t [$env txn]
154		error_check_good txn [is_valid_txn $t $env] TRUE
155		set txn "-txn $t"
156	}
157	set ret [eval {$db put} $txn {1 $data}]
158	error_check_good dbput $ret 0
159	set ret [eval {$db get} $txn {-recno 1}]
160	error_check_good dbget $data [lindex [lindex $ret 0] 1]
161
162	set data [repeat "b" $fixed_len]
163	set ret [eval {$db put -partial [list 0 $fixed_len]} $txn {1 $data}]
164	error_check_good dbput $ret 0
165	set ret [eval {$db get} $txn {-recno 1}]
166	error_check_good dbget $data [lindex [lindex $ret 0] 1]
167	if { $txnenv == 1 } {
168		error_check_good txn [$t commit] 0
169	}
170
171	set data "InitialData"
172	set pdata "PUT"
173	set dlen [string length $pdata]
174	set ilen [string length $data]
175	set mid [expr $ilen/2]
176
177	# put initial data
178	set key 0
179
180	set offlist [list 0 $mid [expr $ilen -1] [expr $fixed_len - $dlen]]
181	puts "\t\tTest051.g: Now replace at different offsets ($offlist)."
182	foreach doff $offlist {
183		incr key
184		if { $txnenv == 1 } {
185			set t [$env txn]
186			error_check_good txn [is_valid_txn $t $env] TRUE
187			set txn "-txn $t"
188		}
189		set ret [eval {$db put} $txn {$key $data}]
190		error_check_good dbput:init $ret 0
191
192		puts "\t\tTest051.g: Replace at offset $doff."
193		set ret [eval {$db put -partial [list $doff $dlen]} $txn \
194		    {$key $pdata}]
195		error_check_good dbput:partial $ret 0
196		if { $txnenv == 1 } {
197			error_check_good txn [$t commit] 0
198		}
199
200		if { $doff == 0} {
201			set beg ""
202			set end [string range $data $dlen $ilen]
203		} else {
204			set beg [string range $data 0 [expr $doff - 1]]
205			set end [string range $data [expr $doff + $dlen] $ilen]
206		}
207		if { $doff > $ilen } {
208			# have to put padding between record and inserted
209			# string
210			set newdata [format %s%s $beg $end]
211			set diff [expr $doff - $ilen]
212			set nlen [string length $newdata]
213			set newdata [binary \
214			    format a[set nlen]x[set diff]a$dlen $newdata $pdata]
215		} else {
216			set newdata [make_fixed_length \
217			    frecno [format %s%s%s $beg $pdata $end]]
218		}
219		set ret [$db get -recno $key]
220		error_check_good compare($newdata,$ret) \
221		    [binary_compare [lindex [lindex $ret 0] 1] $newdata] 0
222	}
223
224	$db close
225}
226