summaryrefslogtreecommitdiff
path: root/db/perl/BerkeleyDB/t/env.t
blob: 2bca2b3112f9f40bffb8a256aa99382bc523f806 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#!./perl -w

use strict ;


use lib 't' ;

BEGIN {
    $ENV{LC_ALL} = 'de_DE@euro';
}

use BerkeleyDB; 
use util ;

print "1..53\n";

my $Dfile = "dbhash.tmp";

umask(0);

my $version_major  = 0;

{
    # db version stuff
    my ($major, $minor, $patch) = (0, 0, 0) ;

    ok 1, my $VER = BerkeleyDB::DB_VERSION_STRING ;
    ok 2, my $ver = BerkeleyDB::db_version($version_major, $minor, $patch) ;
    ok 3, $VER eq $ver ;
    ok 4, $version_major > 1 ;
    ok 5, defined $minor ;
    ok 6, defined $patch ;
}

{
    # Check for invalid parameters
    my $env ;
    eval ' $env = new BerkeleyDB::Env( -Stupid => 3) ; ' ;
    ok 7, $@ =~ /unknown key value\(s\) Stupid/  ;

    eval ' $env = new BerkeleyDB::Env( -Bad => 2, -Home => "/tmp", -Stupid => 3) ; ' ;
    ok 8, $@ =~ /unknown key value\(s\) (Bad,? |Stupid,? ){2}/  ;

    eval ' $env = new BerkeleyDB::Env (-Config => {"fred" => " "} ) ; ' ;
    ok 9, !$env ;
    ok 10, $BerkeleyDB::Error =~ /^(illegal name-value pair|Invalid argument)/ ;
    #print " $BerkeleyDB::Error\n";
}

{
    # create a very simple environment
    my $home = "./fred" ;
    ok 11, my $lexD = new LexDir($home) ;
    chdir "./fred" ;
    ok 12, my $env = new BerkeleyDB::Env -Flags => DB_CREATE,
					@StdErrFile;
    chdir ".." ;
    undef $env ;
}

{
    # create an environment with a Home
    my $home = "./fred" ;
    ok 13, my $lexD = new LexDir($home) ;
    ok 14, my $env = new BerkeleyDB::Env -Home => $home,
    					 -Flags => DB_CREATE ;

    undef $env ;
}

{
    # make new fail.
    my $home = "./not_there" ;
    rmtree $home ;
    ok 15, ! -d $home ;
    my $env = new BerkeleyDB::Env -Home => $home, @StdErrFile,
			          -Flags => DB_INIT_LOCK ;
    ok 16, ! $env ;
    ok 17,   $! != 0 || $^E != 0 ;

    rmtree $home ;
}

{
    # Config
    use Cwd ;
    my $cwd = cwd() ;
    my $home = "$cwd/fred" ;
    my $data_dir = "$home/data_dir" ;
    my $log_dir = "$home/log_dir" ;
    my $data_file = "data.db" ;
    ok 18, my $lexD = new LexDir($home) ;
    ok 19, -d $data_dir ? chmod 0777, $data_dir : mkdir($data_dir, 0777) ;
    ok 20, -d $log_dir ? chmod 0777, $log_dir : mkdir($log_dir, 0777) ;
    my $env = new BerkeleyDB::Env -Home   => $home, @StdErrFile,
			      -Config => { DB_DATA_DIR => $data_dir,
					   DB_LOG_DIR  => $log_dir
					 },
			      -Flags  => DB_CREATE|DB_INIT_TXN|DB_INIT_LOG|
					 DB_INIT_MPOOL|DB_INIT_LOCK ;
    ok 21, $env ;

    ok 22, my $txn = $env->txn_begin() ;

    my %hash ;
    ok 23, tie %hash, 'BerkeleyDB::Hash', -Filename => $data_file,
                                       -Flags     => DB_CREATE ,
                                       -Env       => $env,
                                       -Txn       => $txn  ;

    $hash{"abc"} = 123 ;
    $hash{"def"} = 456 ;

    $txn->txn_commit() ;

    untie %hash ;

    undef $txn ;
    undef $env ;
}

sub chkMsg
{
    my $prefix = shift || '';

    $prefix = "$prefix: " if $prefix;

    my $ErrMsg = join "|", map { "$prefix$_" }
                        'illegal flag specified to (db_open|DB->open)',
                       'DB_AUTO_COMMIT may not be specified in non-transactional environment';
    
    return 1 if $BerkeleyDB::Error =~ /^$ErrMsg/ ;
    warn "# $BerkeleyDB::Error\n" ;
    return 0;
}

{
    # -ErrFile with a filename
    my $errfile = "./errfile" ;
    my $home = "./fred" ;
    ok 24, my $lexD = new LexDir($home) ;
    my $lex = new LexFile $errfile ;
    ok 25, my $env = new BerkeleyDB::Env( -ErrFile => $errfile, 
    					  -Flags => DB_CREATE,
					  -Home   => $home) ;
    my $db = new BerkeleyDB::Hash -Filename => $Dfile,
			     -Env      => $env,
			     -Flags    => -1;
    ok 26, !$db ;

    my $ErrMsg = join "'", 'illegal flag specified to (db_open|DB->open)',
                           'DB_AUTO_COMMIT may not be specified in non-transactional environment';
    
    ok 27, chkMsg();
    ok 28, -e $errfile ;
    my $contents = docat($errfile) ;
    chomp $contents ;
    ok 29, $BerkeleyDB::Error eq $contents ;

    undef $env ;
}

{
    # -ErrFile with a filehandle
    use IO::File ;
    my $errfile = "./errfile" ;
    my $home = "./fred" ;
    ok 30, my $lexD = new LexDir($home) ;
    my $lex = new LexFile $errfile ;
    my $fh = new IO::File ">$errfile" ;
    ok 31, my $env = new BerkeleyDB::Env( -ErrFile => $fh, 
    					  -Flags => DB_CREATE,
					  -Home   => $home) ;
    my $db = new BerkeleyDB::Hash -Filename => $Dfile,
			     -Env      => $env,
			     -Flags    => -1;
    ok 32, !$db ;

    ok 33, chkMsg();
    ok 34, -e $errfile ;
    my $contents = docat($errfile) ;
    chomp $contents ;
    ok 35, $BerkeleyDB::Error eq $contents ;

    undef $env ;
}

{
    # -ErrPrefix
    my $home = "./fred" ;
    ok 36, my $lexD = new LexDir($home) ;
    my $errfile = "./errfile" ;
    my $lex = new LexFile $errfile ;
    ok 37, my $env = new BerkeleyDB::Env( -ErrFile => $errfile,
					-ErrPrefix => "PREFIX",
    					  -Flags => DB_CREATE,
					  -Home   => $home) ;
    my $db = new BerkeleyDB::Hash -Filename => $Dfile,
			     -Env      => $env,
			     -Flags    => -1;
    ok 38, !$db ;

    ok 39, chkMsg('PREFIX');
    ok 40, -e $errfile ;
    my $contents = docat($errfile) ;
    chomp $contents ;
    ok 41, $BerkeleyDB::Error eq $contents ;

    # change the prefix on the fly
    my $old = $env->errPrefix("NEW ONE") ;
    ok 42, $old eq "PREFIX" ;

    $db = new BerkeleyDB::Hash -Filename => $Dfile,
			     -Env      => $env,
			     -Flags    => -1;
    ok 43, !$db ;
    ok 44, chkMsg('NEW ONE');
    $contents = docat($errfile) ;
    chomp $contents ;
    ok 45, $contents =~ /$BerkeleyDB::Error$/ ;
    undef $env ;
}

{
    # test db_appexit
    use Cwd ;
    my $cwd = cwd() ;
    my $home = "$cwd/fred" ;
    my $data_dir = "$home/data_dir" ;
    my $log_dir = "$home/log_dir" ;
    my $data_file = "data.db" ;
    ok 46, my $lexD = new LexDir($home);
    ok 47, -d $data_dir ? chmod 0777, $data_dir : mkdir($data_dir, 0777) ;
    ok 48, -d $log_dir ? chmod 0777, $log_dir : mkdir($log_dir, 0777) ;
    my $env = new BerkeleyDB::Env -Home   => $home, @StdErrFile,
			      -Config => { DB_DATA_DIR => $data_dir,
					   DB_LOG_DIR  => $log_dir
					 },
			      -Flags  => DB_CREATE|DB_INIT_TXN|DB_INIT_LOG|
					 DB_INIT_MPOOL|DB_INIT_LOCK ;
    ok 49, $env ;

    ok 50, my $txn_mgr = $env->TxnMgr() ;

    ok 51, $env->db_appexit() == 0 ;

}

{
    # attempt to open a new environment without DB_CREATE
    # should fail with Berkeley DB 3.x or better.

    my $home = "./fred" ;
    ok 52, my $lexD = new LexDir($home) ;
    chdir "./fred" ;
    my $env = new BerkeleyDB::Env -Home => $home, -Flags => DB_CREATE ;
    ok 53, $version_major == 2 ? $env : ! $env ;

    # The test below is not portable -- the error message returned by
    # $BerkeleyDB::Error is locale dependant.

    #ok 54, $version_major == 2 ? 1 
    #                           : $BerkeleyDB::Error =~ /No such file or directory/ ;
    #    or print "# BerkeleyDB::Error is $BerkeleyDB::Error\n";
    chdir ".." ;
    undef $env ;
}

# test -Verbose
# test -Flags
# db_value_set