1# Copyright (c) 2013, 2020 Oracle and/or its affiliates. All rights reserved.
2#
3# See the file LICENSE for license information.
4#
5# $Id$
6#
7# TEST	env025
8# TEST	Test db_recover with all allowed option combinations.
9proc env025 { } {
10	source ./include.tcl
11	global has_crypto
12
13	set encrypt 0
14	if { $has_crypto == 1 } {
15		lappend encrypt 1
16	}
17
18	# Test with -P -c -e -f -t -v and -b.
19	foreach e $encrypt {
20		foreach catastrophic { 1 0 } {
21			foreach retain_env { 1 0 } {
22				foreach show_percent { 1 0 } {
23					foreach use_timestamp { 1 0 } {
24						foreach verbose { 1 0 } {
25							foreach  blob_dir \
26							    { 1 0 } {
27								env025_subtest \
28								    $e \
29								    $catastrophic \
30								    $retain_env \
31								    $show_percent \
32								    $use_timestamp \
33								    $verbose\
34								    $blob_dir
35							}
36
37						}
38					}
39				}
40			}
41		}
42	}
43
44	set binname db_recover
45	set std_redirect "> /dev/null"
46	if { $is_windows_test } {
47		set std_redirect "> /nul"
48		append binname ".exe"
49	}
50
51	# Print version.
52 	puts "\tEnv025: $util_path/$binname -V $std_redirect"
53	set ret [catch {eval exec $util_path/$binname -V $std_redirect} r]
54	error_check_good db_recover($r) $ret 0
55}
56
57proc env025_subtest { encrypt catastrophic retain_env show_percent \
58    use_timestamp verbose blob_dir } {
59	source ./include.tcl
60
61	set passwd "passwd"
62	set envargs ""
63	set recover_args "-h $testdir"
64
65	if { $catastrophic } {
66		append recover_args " -c"
67	}
68	if { $retain_env } {
69		append recover_args " -e"
70	}
71	if { $show_percent } {
72		append recover_args " -f"
73	}
74	if { $verbose } {
75		append recover_args " -v"
76	}
77	if { $encrypt } {
78		append recover_args " -P $passwd"
79		append envargs " -encryptaes $passwd"
80	}
81
82	set binname db_recover
83	set std_redirect "> /dev/null"
84	if { $is_windows_test } {
85		set std_redirect "> /nul"
86		append binname ".exe"
87	}
88
89	foreach region_dir { 1 0 } {
90
91		puts "Env025: Test with options: (encrypt:$encrypt\
92		    catastrophic:$catastrophic\
93		    retain_env:$retain_env\
94		    show_percent:$show_percent\
95		    use_timestamp:$use_timestamp\
96		    verbose:$verbose\
97		    blob_dir:$blob_dir\
98		    region_dir:$region_dir)"
99
100		set key 1
101		set data 1
102		set skdata 2
103
104		set method "-btree"
105		set dbfile "env025.db"
106
107		env_cleanup $testdir
108
109		set recover_temp_args $recover_args
110		set env_temp_args $envargs
111
112		if { $blob_dir } {
113			set blob_directory "__db_bl"
114			file mkdir "$testdir/$blob_directory"
115
116			append recover_temp_args " -b $blob_directory"
117			append env_temp_args " -blob_dir $blob_directory"
118		}
119
120		# If the retain_env is not set, the environment will be set
121		# as private. The region_dir should not be set with the flag
122		# DB_PRIVATE or DB_SYSTEM_MEM.
123		if { $retain_env && $region_dir } {
124			set regdir "REGDIR"
125			file mkdir "$testdir/$regdir"
126
127			append recover_temp_args " -r $regdir"
128			append env_temp_args " -region_dir $regdir"
129		}
130
131		set env [eval berkdb_env \
132		    $env_temp_args -create -txn -home $testdir]
133		error_check_good env [is_valid_env $env] TRUE
134
135		set db [eval {berkdb_open -env $env -create $method\
136		    -mode 0644 -auto_commit $dbfile}]
137		error_check_good dbopen [is_valid_db $db] TRUE
138
139		# Initialize checkpoint.
140		error_check_good "Initialize Checkpoint" [$env txn_checkpoint] 0
141
142		set txn [$env txn]
143		error_check_good txn_begin [is_valid_txn $txn $env] TRUE
144		error_check_good db_put [$db put -txn $txn $key $data] 0
145		error_check_good txn_commit [$txn commit] 0
146
147		# We need to sleep before taking the timestamp to guarantee
148		# that the timestamp is *after* this transaction commits.
149		tclsleep 1
150
151		set timestamp [clock format [clock seconds] \
152		    -format %Y%m%d%H%M.%S]
153
154		# Sleep again to ensure that the next insert operation
155		# definitely occurs after the timestamp.
156		tclsleep 1
157
158		set txn [$env txn]
159		error_check_good txn_begin [is_valid_txn $txn $env] TRUE
160		error_check_good db_put [$db put -txn $txn $key $skdata] 0
161		error_check_good txn_commit [$txn commit] 0
162
163		# We need to sleep before proceeding checkpoint to guarantee
164		# that the checkpoint is * after * this transaction commits.
165		tclsleep 1
166		error_check_good checkpoint [$env txn_checkpoint] 0
167
168		if { $use_timestamp } {
169			append recover_temp_args " -t $timestamp"
170		}
171
172		error_check_good db_close [$db close] 0
173		error_check_good env_close [$env close] 0
174
175  		puts "\tEnv025: $util_path/$binname \
176		    $recover_temp_args $std_redirect"
177		set ret [catch {eval exec \
178		    $util_path/$binname $recover_temp_args $std_redirect} r]
179		error_check_good db_recover($r) $ret 0
180
181		if { $use_timestamp } {
182			set db [eval berkdb_open $method $testdir/$dbfile]
183			set dbt [$db get $key]
184			set data [lindex [lindex $dbt 0] 1]
185
186			error_check_good check_recover $data 1
187			error_check_good db_close [$db close] 0
188		}
189		if { $retain_env } {
190			# The environment should be retained.
191			set env [eval berkdb_env_noerr \
192			    -home $testdir $env_temp_args]
193			error_check_good env_close [$env close] 0
194		}
195	}
196}
197