Debian Codegraph @

lang/perl/BerkeleyDB/t/encrypt.t

raw
1 #!./perl -w
2
3 # ID: %I%, %G%
4
5 use strict ;
6
7 use lib 't' ;
8 use BerkeleyDB;
9 use util ;
10 use Test::More;
11
12 BEGIN {
13 plan(skip_all => "this needs BerkeleyDB 4.1.x or better" )
14 if $BerkeleyDB::db_version < 4.1;
15
16 # Is encryption available?
17 my $env = new BerkeleyDB::Env @StdErrFile,
18 -Encrypt => {Password => "abc",
19 Flags => DB_ENCRYPT_AES
20 };
21
22 plan skip_all => "encryption support not present"
23 if $BerkeleyDB::Error =~ /Operation not supported/;
24
25 plan tests => 80;
26 }
27
28
29 umask(0);
30
31 {
32 eval
33 {
34 my $env = new BerkeleyDB::Env @StdErrFile,
35 -Encrypt => 1,
36 -Flags => DB_CREATE ;
37 };
38 ok $@ =~ /^Encrypt parameter must be a hash reference at/;
39
40 eval
41 {
42 my $env = new BerkeleyDB::Env @StdErrFile,
43 -Encrypt => {},
44 -Flags => DB_CREATE ;
45 };
46 ok $@ =~ /^Must specify Password and Flags with Encrypt parameter at/;
47
48 eval
49 {
50 my $env = new BerkeleyDB::Env @StdErrFile,
51 -Encrypt => {Password => "fred"},
52 -Flags => DB_CREATE ;
53 };
54 ok $@ =~ /^Must specify Password and Flags with Encrypt parameter at/;
55
56 eval
57 {
58 my $env = new BerkeleyDB::Env @StdErrFile,
59 -Encrypt => {Flags => 1},
60 -Flags => DB_CREATE ;
61 };
62 ok $@ =~ /^Must specify Password and Flags with Encrypt parameter at/;
63
64 eval
65 {
66 my $env = new BerkeleyDB::Env @StdErrFile,
67 -Encrypt => {Fred => 1},
68 -Flags => DB_CREATE ;
69 };
70 ok $@ =~ /^\Qunknown key value(s) Fred at/;
71
72 }
73
74 {
75 # new BerkeleyDB::Env -Encrypt =>
76
77 # create an environment with a Home
78 my $home = "./fred" ;
79 #mkdir $home;
80 ok my $lexD = new LexDir($home) ;
81 ok my $env = new BerkeleyDB::Env @StdErrFile,
82 -Home => $home,
83 -Encrypt => {Password => "abc",
84 Flags => DB_ENCRYPT_AES
85 },
86 -Flags => DB_CREATE | DB_INIT_MPOOL ;
87
88
89
90 my $Dfile = "abc.enc";
91 my $lex = new LexFile $Dfile ;
92 my %hash ;
93 my ($k, $v) ;
94 ok my $db = new BerkeleyDB::Hash -Filename => $Dfile,
95 -Env => $env,
96 -Flags => DB_CREATE,
97 -Property => DB_ENCRYPT ;
98
99 # create some data
100 my %data = (
101 "red" => 2,
102 "green" => "house",
103 "blue" => "sea",
104 ) ;
105
106 my $ret = 0 ;
107 while (($k, $v) = each %data) {
108 $ret += $db->db_put($k, $v) ;
109 }
110 ok $ret == 0 ;
111
112 # check there are three records
113 ok countRecords($db) == 3 ;
114
115 undef $db;
116
117 # once the database is created, do not need to specify DB_ENCRYPT
118 ok my $db1 = new BerkeleyDB::Hash -Filename => $Dfile,
119 -Env => $env,
120 -Flags => DB_CREATE ;
121 $v = '';
122 ok ! $db1->db_get("red", $v) ;
123 ok $v eq $data{"red"},
124 undef $db1;
125 undef $env;
126
127 # open a database without specifying encryption
128 ok ! new BerkeleyDB::Hash -Filename => "$home/$Dfile";
129
130 ok ! new BerkeleyDB::Env
131 -Home => $home,
132 -Encrypt => {Password => "def",
133 Flags => DB_ENCRYPT_AES
134 },
135 -Flags => DB_CREATE | DB_INIT_MPOOL ;
136 }
137
138 {
139 eval
140 {
141 my $env = new BerkeleyDB::Hash
142 -Encrypt => 1,
143 -Flags => DB_CREATE ;
144 };
145 ok $@ =~ /^Encrypt parameter must be a hash reference at/;
146
147 eval
148 {
149 my $env = new BerkeleyDB::Hash
150 -Encrypt => {},
151 -Flags => DB_CREATE ;
152 };
153 ok $@ =~ /^Must specify Password and Flags with Encrypt parameter at/;
154
155 eval
156 {
157 my $env = new BerkeleyDB::Hash
158 -Encrypt => {Password => "fred"},
159 -Flags => DB_CREATE ;
160 };
161 ok $@ =~ /^Must specify Password and Flags with Encrypt parameter at/;
162
163 eval
164 {
165 my $env = new BerkeleyDB::Hash
166 -Encrypt => {Flags => 1},
167 -Flags => DB_CREATE ;
168 };
169 ok $@ =~ /^Must specify Password and Flags with Encrypt parameter at/;
170
171 eval
172 {
173 my $env = new BerkeleyDB::Hash
174 -Encrypt => {Fred => 1},
175 -Flags => DB_CREATE ;
176 };
177 ok $@ =~ /^\Qunknown key value(s) Fred at/;
178
179 }
180
181 {
182 eval
183 {
184 my $env = new BerkeleyDB::Btree
185 -Encrypt => 1,
186 -Flags => DB_CREATE ;
187 };
188 ok $@ =~ /^Encrypt parameter must be a hash reference at/;
189
190 eval
191 {
192 my $env = new BerkeleyDB::Btree
193 -Encrypt => {},
194 -Flags => DB_CREATE ;
195 };
196 ok $@ =~ /^Must specify Password and Flags with Encrypt parameter at/;
197
198 eval
199 {
200 my $env = new BerkeleyDB::Btree
201 -Encrypt => {Password => "fred"},
202 -Flags => DB_CREATE ;
203 };
204 ok $@ =~ /^Must specify Password and Flags with Encrypt parameter at/;
205
206 eval
207 {
208 my $env = new BerkeleyDB::Btree
209 -Encrypt => {Flags => 1},
210 -Flags => DB_CREATE ;
211 };
212 ok $@ =~ /^Must specify Password and Flags with Encrypt parameter at/;
213
214 eval
215 {
216 my $env = new BerkeleyDB::Btree
217 -Encrypt => {Fred => 1},
218 -Flags => DB_CREATE ;
219 };
220 ok $@ =~ /^\Qunknown key value(s) Fred at/;
221
222 }
223
224 {
225 eval
226 {
227 my $env = new BerkeleyDB::Queue
228 -Encrypt => 1,
229 -Flags => DB_CREATE ;
230 };
231 ok $@ =~ /^Encrypt parameter must be a hash reference at/;
232
233 eval
234 {
235 my $env = new BerkeleyDB::Queue
236 -Encrypt => {},
237 -Flags => DB_CREATE ;
238 };
239 ok $@ =~ /^Must specify Password and Flags with Encrypt parameter at/;
240
241 eval
242 {
243 my $env = new BerkeleyDB::Queue
244 -Encrypt => {Password => "fred"},
245 -Flags => DB_CREATE ;
246 };
247 ok $@ =~ /^Must specify Password and Flags with Encrypt parameter at/;
248
249 eval
250 {
251 my $env = new BerkeleyDB::Queue
252 -Encrypt => {Flags => 1},
253 -Flags => DB_CREATE ;
254 };
255 ok $@ =~ /^Must specify Password and Flags with Encrypt parameter at/;
256
257 eval
258 {
259 my $env = new BerkeleyDB::Queue
260 -Encrypt => {Fred => 1},
261 -Flags => DB_CREATE ;
262 };
263 ok $@ =~ /^\Qunknown key value(s) Fred at/;
264
265 }
266
267 {
268 eval
269 {
270 my $env = new BerkeleyDB::Recno
271 -Encrypt => 1,
272 -Flags => DB_CREATE ;
273 };
274 ok $@ =~ /^Encrypt parameter must be a hash reference at/;
275
276 eval
277 {
278 my $env = new BerkeleyDB::Recno
279 -Encrypt => {},
280 -Flags => DB_CREATE ;
281 };
282 ok $@ =~ /^Must specify Password and Flags with Encrypt parameter at/;
283
284 eval
285 {
286 my $env = new BerkeleyDB::Recno
287 -Encrypt => {Password => "fred"},
288 -Flags => DB_CREATE ;
289 };
290 ok $@ =~ /^Must specify Password and Flags with Encrypt parameter at/;
291
292 eval
293 {
294 my $env = new BerkeleyDB::Recno
295 -Encrypt => {Flags => 1},
296 -Flags => DB_CREATE ;
297 };
298 ok $@ =~ /^Must specify Password and Flags with Encrypt parameter at/;
299
300 eval
301 {
302 my $env = new BerkeleyDB::Recno
303 -Encrypt => {Fred => 1},
304 -Flags => DB_CREATE ;
305 };
306 ok $@ =~ /^\Qunknown key value(s) Fred at/;
307
308 }
309
310
311 {
312 # new BerkeleyDB::Hash -Encrypt =>
313
314 my $Dfile = "abcd.enc";
315 my $lex = new LexFile $Dfile ;
316 my %hash ;
317 my ($k, $v) ;
318 ok my $db = new BerkeleyDB::Hash
319 -Filename => $Dfile,
320 -Flags => DB_CREATE,
321 -Encrypt => {Password => "beta",
322 Flags => DB_ENCRYPT_AES
323 },
324 -Property => DB_ENCRYPT ;
325
326 # create some data
327 my %data = (
328 "red" => 2,
329 "green" => "house",
330 "blue" => "sea",
331 ) ;
332
333 my $ret = 0 ;
334 while (($k, $v) = each %data) {
335 $ret += $db->db_put($k, $v) ;
336 }
337 ok $ret == 0 ;
338
339 # check there are three records
340 ok countRecords($db) == 3 ;
341
342 undef $db;
343
344 # attempt to open a database without specifying encryption
345 ok ! new BerkeleyDB::Hash -Filename => $Dfile,
346 -Flags => DB_CREATE ;
347
348
349 # try opening with the wrong password
350 ok ! new BerkeleyDB::Hash -Filename => $Dfile,
351 -Filename => $Dfile,
352 -Encrypt => {Password => "def",
353 Flags => DB_ENCRYPT_AES
354 },
355 -Property => DB_ENCRYPT ;
356
357
358 # read the encrypted data
359 ok my $db1 = new BerkeleyDB::Hash -Filename => $Dfile,
360 -Filename => $Dfile,
361 -Encrypt => {Password => "beta",
362 Flags => DB_ENCRYPT_AES
363 },
364 -Property => DB_ENCRYPT ;
365
366
367 $v = '';
368 ok ! $db1->db_get("red", $v) ;
369 ok $v eq $data{"red"};
370 # check there are three records
371 ok countRecords($db1) == 3 ;
372 undef $db1;
373 }
374
375 {
376 # new BerkeleyDB::Btree -Encrypt =>
377
378 my $Dfile = "abcd.enc";
379 my $lex = new LexFile $Dfile ;
380 my %hash ;
381 my ($k, $v) ;
382 ok my $db = new BerkeleyDB::Btree
383 -Filename => $Dfile,
384 -Flags => DB_CREATE,
385 -Encrypt => {Password => "beta",
386 Flags => DB_ENCRYPT_AES
387 },
388 -Property => DB_ENCRYPT ;
389
390 # create some data
391 my %data = (
392 "red" => 2,
393 "green" => "house",
394 "blue" => "sea",
395 ) ;
396
397 my $ret = 0 ;
398 while (($k, $v) = each %data) {
399 $ret += $db->db_put($k, $v) ;
400 }
401 ok $ret == 0 ;
402
403 # check there are three records
404 ok countRecords($db) == 3 ;
405
406 undef $db;
407
408 # attempt to open a database without specifying encryption
409 ok ! new BerkeleyDB::Btree -Filename => $Dfile,
410 -Flags => DB_CREATE ;
411
412
413 # try opening with the wrong password
414 ok ! new BerkeleyDB::Btree -Filename => $Dfile,
415 -Filename => $Dfile,
416 -Encrypt => {Password => "def",
417 Flags => DB_ENCRYPT_AES
418 },
419 -Property => DB_ENCRYPT ;
420
421
422 # read the encrypted data
423 ok my $db1 = new BerkeleyDB::Btree -Filename => $Dfile,
424 -Filename => $Dfile,
425 -Encrypt => {Password => "beta",
426 Flags => DB_ENCRYPT_AES
427 },
428 -Property => DB_ENCRYPT ;
429
430
431 $v = '';
432 ok ! $db1->db_get("red", $v) ;
433 ok $v eq $data{"red"};
434 # check there are three records
435 ok countRecords($db1) == 3 ;
436 undef $db1;
437 }
438
439 {
440 # new BerkeleyDB::Queue -Encrypt =>
441
442 my $Dfile = "abcd.enc";
443 my $lex = new LexFile $Dfile ;
444 my %hash ;
445 my ($k, $v) ;
446 ok my $db = new BerkeleyDB::Queue
447 -Filename => $Dfile,
448 -Len => 5,
449 -Pad => "x",
450 -Flags => DB_CREATE,
451 -Encrypt => {Password => "beta",
452 Flags => DB_ENCRYPT_AES
453 },
454 -Property => DB_ENCRYPT ;
455
456 # create some data
457 my %data = (
458 1 => 2,
459 2 => "house",
460 3 => "sea",
461 ) ;
462
463 my $ret = 0 ;
464 while (($k, $v) = each %data) {
465 $ret += $db->db_put($k, $v) ;
466 }
467 ok $ret == 0 ;
468
469 # check there are three records
470 ok countRecords($db) == 3 ;
471
472 undef $db;
473
474 # attempt to open a database without specifying encryption
475 ok ! new BerkeleyDB::Queue -Filename => $Dfile,
476 -Len => 5,
477 -Pad => "x",
478 -Flags => DB_CREATE ;
479
480
481 # try opening with the wrong password
482 ok ! new BerkeleyDB::Queue -Filename => $Dfile,
483 -Len => 5,
484 -Pad => "x",
485 -Encrypt => {Password => "def",
486 Flags => DB_ENCRYPT_AES
487 },
488 -Property => DB_ENCRYPT ;
489
490
491 # read the encrypted data
492 ok my $db1 = new BerkeleyDB::Queue -Filename => $Dfile,
493 -Len => 5,
494 -Pad => "x",
495 -Encrypt => {Password => "beta",
496 Flags => DB_ENCRYPT_AES
497 },
498 -Property => DB_ENCRYPT ;
499
500
501 $v = '';
502 ok ! $db1->db_get(3, $v) ;
503 ok $v eq fillout($data{3}, 5, 'x');
504 # check there are three records
505 ok countRecords($db1) == 3 ;
506 undef $db1;
507 }
508
509 {
510 # new BerkeleyDB::Recno -Encrypt =>
511
512 my $Dfile = "abcd.enc";
513 my $lex = new LexFile $Dfile ;
514 my %hash ;
515 my ($k, $v) ;
516 ok my $db = new BerkeleyDB::Recno
517 -Filename => $Dfile,
518 -Flags => DB_CREATE,
519 -Encrypt => {Password => "beta",
520 Flags => DB_ENCRYPT_AES
521 },
522 -Property => DB_ENCRYPT ;
523
524 # create some data
525 my %data = (
526 1 => 2,
527 2 => "house",
528 3 => "sea",
529 ) ;
530
531 my $ret = 0 ;
532 while (($k, $v) = each %data) {
533 $ret += $db->db_put($k, $v) ;
534 }
535 ok $ret == 0 ;
536
537 # check there are three records
538 ok countRecords($db) == 3 ;
539
540 undef $db;
541
542 # attempt to open a database without specifying encryption
543 ok ! new BerkeleyDB::Recno -Filename => $Dfile,
544 -Flags => DB_CREATE ;
545
546
547 # try opening with the wrong password
548 ok ! new BerkeleyDB::Recno -Filename => $Dfile,
549 -Filename => $Dfile,
550 -Encrypt => {Password => "def",
551 Flags => DB_ENCRYPT_AES
552 },
553 -Property => DB_ENCRYPT ;
554
555
556 # read the encrypted data
557 ok my $db1 = new BerkeleyDB::Recno -Filename => $Dfile,
558 -Filename => $Dfile,
559 -Encrypt => {Password => "beta",
560 Flags => DB_ENCRYPT_AES
561 },
562 -Property => DB_ENCRYPT ;
563
564
565 $v = '';
566 ok ! $db1->db_get(3, $v) ;
567 ok $v eq $data{3};
568 # check there are three records
569 ok countRecords($db1) == 3 ;
570 undef $db1;
571 }
572
573 {
574 # new BerkeleyDB::Unknown -Encrypt =>
575
576 my $Dfile = "abcd.enc";
577 my $lex = new LexFile $Dfile ;
578 my %hash ;
579 my ($k, $v) ;
580 ok my $db = new BerkeleyDB::Hash
581 -Filename => $Dfile,
582 -Flags => DB_CREATE,
583 -Encrypt => {Password => "beta",
584 Flags => DB_ENCRYPT_AES
585 },
586 -Property => DB_ENCRYPT ;
587
588 # create some data
589 my %data = (
590 "red" => 2,
591 "green" => "house",
592 "blue" => "sea",
593 ) ;
594
595 my $ret = 0 ;
596 while (($k, $v) = each %data) {
597 $ret += $db->db_put($k, $v) ;
598 }
599 ok $ret == 0 ;
600
601 # check there are three records
602 ok countRecords($db) == 3 ;
603
604 undef $db;
605
606 # attempt to open a database without specifying encryption
607 ok ! new BerkeleyDB::Unknown -Filename => $Dfile,
608 -Flags => DB_CREATE ;
609
610
611 # try opening with the wrong password
612 ok ! new BerkeleyDB::Unknown -Filename => $Dfile,
613 -Filename => $Dfile,
614 -Encrypt => {Password => "def",
615 Flags => DB_ENCRYPT_AES
616 },
617 -Property => DB_ENCRYPT ;
618
619
620 # read the encrypted data
621 ok my $db1 = new BerkeleyDB::Unknown -Filename => $Dfile,
622 -Filename => $Dfile,
623 -Encrypt => {Password => "beta",
624 Flags => DB_ENCRYPT_AES
625 },
626 -Property => DB_ENCRYPT ;
627
628
629 $v = '';
630 ok ! $db1->db_get("red", $v) ;
631 ok $v eq $data{"red"};
632 # check there are three records
633 ok countRecords($db1) == 3 ;
634 undef $db1;
635 }
636