GPG/Test.pm

filter: Statementstmt 66.6% 4 / 6 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 2 / 2 Podpod n/a totaltotal 75.0% 6 / 8 CC 1 SCAR 1.4
CC
1
Cov
67%
BEGIN
1.3
BEGIN
1.0
CRAP 1.1
« GPG/Result.pm ↑ Summary GPG.pm »
1 package Mail::GPG::Test;
2  
3 # $Id: Test.pm,v 1.6 2006/11/18 08:48:28 joern Exp $
4  
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 0 ▶use Mail::GPG;
Subroutine BEGIN: called CC=1 SCAR=2.6
8 use MIME::Entity;
9 use MIME::Parser;
10 use Data::Dumper;
11 use File::Path;
12  
13 use File::Temp qw(tempdir);
14  
15 my $TIMEIT = 0;
16  
17 our $DUMPDIR;
18  
19 BEGIN {
20 $DUMPDIR = $ENV{DUMPDIR} || './mail-gpg-test';
21  
22 if (not -d $DUMPDIR ) {
23 File::Path::make_path($DUMPDIR) or die "Cannot create '$DUMPDIR' - $!";
24 }
25 }
26  
27  
28  
29 my $has_encode = eval { require Encode; 1 };
30  
31 sub get_gpg_home_dir { shift->{gpg_home_dir} }
32 sub get_use_long_key_ids { shift->{use_long_key_ids} }
33  
34 sub set_gpg_home_dir { shift->{gpg_home_dir} = $_[1] }
35 sub set_use_long_key_ids { shift->{use_long_key_ids} = $_[1] }
36  
37 #-- These methods return information about the shipped test key.
38 #-- The email adress has a German umlaut and colons
39 #-- to test the proper decoding of gpg --list-keys output.
40 sub get_key_id { $_[0]->get_use_long_key_ids ?
41 '062F00DAE20F5035' : 'E20F5035' }
42 sub get_key_sub_id { $_[0]->get_use_long_key_ids ?
43 '6C187D0F196ED9E3' : '196ED9E3' }
44 sub get_key_mail { 'Jörn Reder Mail::GPG Test Key <mailgpg@localdomain>' }
45 sub get_passphrase { 'test' }
46  
47 sub new {
48 my $class = shift;
49 my %par = @_;
50 my ($use_long_key_ids) = $par{'use_long_key_ids'};
51  
52 my $gpg_home_dir = tempdir("mgpgXXXX");
53  
54 my $self = bless {
55 gpg_home_dir => $gpg_home_dir,
56 use_long_key_ids => $use_long_key_ids,
57 }, $class;
58  
59 return $self;
60 }
61  
62 sub DESTROY {
63 my $self = shift;
64  
65 #-- tempdir ( CLEANUP => 1 ) seem not to work if
66 #-- an exception occured, so we use this destructor
67 #-- to remove the gpg home dir on exit.
68 rmtree( [ $self->get_gpg_home_dir ], 0, 0 );
69  
70 1;
71 }
72  
73 sub init {
74 my $self = shift;
75  
76 my $gpg_home_dir = $self->get_gpg_home_dir;
77  
78 my $command = "gpg --batch --no-tty --homedir $gpg_home_dir"
79 . " --import t/mgpg-test-key.pub.asc"
80 . " >/dev/null 2>&1 && "
81 . "gpg --batch --no-tty --homedir $gpg_home_dir"
82 . " --allow-secret-key-import"
83 . " --import t/mgpg-test-key.sec.asc"
84 . " >/dev/null 2>&1 && echo MGPG_OK";
85  
86 my $output = qx[ $command ];
87  
88 return $output =~ /MGPG_OK/;
89 }
90  
91 sub get_mail_gpg {
92 my $self = shift;
93  
94 my $mg = Mail::GPG->new(
95 debug => $ENV{DUMPFILES},
96 default_key_id => $self->get_key_id,
97 default_passphrase => $self->get_passphrase,
98 use_long_key_ids => $self->get_use_long_key_ids,
99 gnupg_hash_init => {
100 homedir => $self->get_gpg_home_dir,
101 always_trust => 1,
102  
103 },
104 );
105  
106 return $mg;
107 }
108  
109 sub get_test_mail_body {
110 "This is a test mail body,\n"
111 . "with special characters: ÄÜÖß\n"
112 . "and lines with whitespace \n"
113 . "and a cr/lf line ending\r\n" . "and\n"
114 . "From at the beginning\n"
115 . "Let's see what happens.\n";
116 }
117  
118 sub print_parse_entity {
119 my $self = shift;
120 my %par = @_;
121 my ($entity, $modify) =
122 @par{'entity','modify'};
123  
124 my ( $fh, $file ) = File::Temp::tempfile(
125 'mgpgXXXXXXXX',
126 DIR => $DUMPDIR,
127 UNLINK => 1,
128 );
129  
130 $entity->print($fh);
131 close $fh;
132  
133 if ($modify) {
134 open( $fh, $file ) or die "can't read $file";
135 my $data = join( '', <$fh> );
136 close $fh;
137 $data =~ s/whitespace/spacewhite/g;
138 $data =~ tr/L/l/;
139 open( $fh, ">$file" ) or die "can't write $file";
140 print $fh $data;
141 close $fh;
142 }
143  
144 open( $fh, $file ) or die "can't read $file";
145 my $mg = $self->get_mail_gpg;
146 my $parsed_entity = $mg->parse( mail_fh => $fh );
147 close $fh;;
148 return $parsed_entity;
149 }
150  
151 sub sign_test {
152 my $self = shift;
153 my %par = @_;
154 my ($mg, $method, $encoding, $attach, $invalid) =
155 @par{'mg','method','encoding','attach','invalid'};
156  
157 $attach = "" if not defined $attach;
158 $invalid = "" if not defined $invalid;
159  
160 $attach = " (w/ attachmnt)" if $attach;
161 $invalid = "" if not $invalid;
162 $invalid = " (invalid)" if $invalid;
163  
164 my $test_name = "$method:$encoding Signature $attach$invalid";
165  
166 my $entity = MIME::Entity->build(
167 From => $self->get_key_mail,
168 Subject => "Mail::GPG Testmail",
169 Data => [ $self->get_test_mail_body ],
170 Encoding => $encoding,
171 Charset => "iso-8859-1",
172 );
173  
174 if ($attach) {
175 $entity->attach(
176 Type => "application/octet-stream",
177 Disposition => "inline",
178 Data => [ "A great Ättächment. \n" x 10 ],
179 Encoding => "base64",
180 );
181 }
182  
183 my $signed_entity = $mg->$method( entity => $entity );
184  
185 if ( not $mg->is_signed( entity => $signed_entity ) ) {
186 ok( 0, "$test_name: Entity not signed" );
187 return;
188 }
189  
190 my $signed_entity_string = $signed_entity->as_string;
191  
192 my $parsed_entity = $self->print_parse_entity(
193 entity => $signed_entity,
194 modify => $invalid,
195 );
196  
197 if ( $ENV{DUMPFILES} ) {
198 my $tmp_file = "$DUMPDIR/$method-$encoding-"
199 . ( $attach ? "attach" : "noattach" ) . "-"
200 . ( $invalid ? "invalid" : "valid" );
201  
202 open( SEND, ">$tmp_file.send" );
203 open( RETR, ">$tmp_file.retr" );
204  
205 print SEND $signed_entity->as_string;
206 print RETR $parsed_entity->as_string;
207  
208 close SEND;
209 close RETR;
210 }
211  
212 if ( not $invalid
213 and not( $encoding eq 'base64' and $method eq 'armor_sign' ) ) {
214 if ( !Mail::GPG->is_signed( entity => $signed_entity ) ) {
215 ok( 0, "$test_name: MIME::Entity sign check failed" );
216 return;
217 }
218 if (!Mail::GPG->is_signed_quick(
219 mail_sref => \$signed_entity_string
220 )
221 ) {
222 ok( 0, "$test_name: mail_sref sign check failed" );
223 return;
224 }
225 my $tmp_file = "$DUMPDIR/Mail-GPG-Test-$$.txt";
226 open( TMP, "+>$tmp_file" ) or die "can't write $tmp_file";
227 print TMP $signed_entity_string;
228 if ( !Mail::GPG->is_signed_quick( mail_fh => \*TMP ) ) {
229 ok( 0, "$test_name: mail_fh sign check failed" );
230 close TMP;
231 unlink $tmp_file;
232 return;
233 }
234 close TMP;
235 unlink $tmp_file;
236 }
237  
238 my $result = eval { $mg->verify( entity => $parsed_entity, ); };
239  
240 my $error = $@;
241  
242 if ( not $invalid and $@ ) {
243 ok( 0, "$test_name: $@" );
244 return;
245 }
246  
247 if (not $invalid
248 and ( $result->get_sign_key_id ne $self->get_key_id
249 or $result->get_sign_mail ne $self->get_key_mail )
250 ) {
251 ok( 0, "Key/Email wrong" );
252 return;
253 }
254  
255 if ( not $invalid and $result->get_sign_trust ne '-' ) {
256 ok( 0, "Owner trust wrong" );
257 }
258  
259 if ($invalid) {
260 if ($error) {
261 ok( 1, $test_name );
262 }
263 else {
264 ok( !$result->get_sign_ok, $test_name );
265 }
266 }
267 else {
268 ok( $result->get_sign_ok, $test_name );
269 }
270  
271 1;
272 }
273  
274 sub enc_test {
275 my $self = shift;
276 my %par = @_;
277 my ($mg, $method, $encoding, $attach) =
278 @par{'mg','method','encoding','attach'};
279  
280 $attach = " (w/ attachmnt)" if $attach;
281 $attach = "" if not defined $attach;
282  
283 my $entity = MIME::Entity->build(
284 From => $self->get_key_mail,
285 Subject => "Mail::GPG Testmail",
286 Data => [ $self->get_test_mail_body ],
287 Encoding => $encoding,
288 Charset => "iso-8859-1",
289 );
290  
291 if ($attach) {
292 $entity->attach(
293 Type => "application/octet-stream",
294 Disposition => "inline",
295 Data => [ "A great Ättächment. \n" x 10 ],
296 Encoding => "base64",
297 );
298 }
299  
300 my $enc_entity = $mg->$method(
301 entity => $entity,
302 recipients => [ $self->get_key_mail ],
303 );
304  
305 if ( not $mg->is_encrypted( entity => $enc_entity ) ) {
306 ok( 0, "Entity not encrypted" );
307 return;
308 }
309  
310 my $parsed_entity = $self->print_parse_entity(
311 entity => $enc_entity,
312 );
313  
314 my ( $dec_key_id, $dec_key_mail )
315 = $mg->get_decrypt_key( entity => $parsed_entity, );
316  
317 if ($has_encode) {
318 if ( $dec_key_id ne $self->get_key_id ) {
319 ok( 0,
320 "Decryption key wrong: "
321 . "$dec_key_id=="
322 . $self->get_key_id
323 );
324 return;
325 }
326 if ( $dec_key_mail ne $self->get_key_mail ) {
327 ok( 0,
328 "Decryption email wrong: "
329 . "$dec_key_mail=="
330 . $self->get_key_mail
331 );
332 return;
333 }
334 }
335 else {
336 if ( $dec_key_id ne $self->get_key_id ) {
337 ok( 0,
338 "Decryption key or email wrong: "
339 . "$dec_key_id=="
340 . $self->get_key_id );
341 return;
342 }
343 }
344  
345 my ( $dec_entity, $result )
346 = eval { $mg->decrypt( entity => $parsed_entity, ); };
347  
348 if ( $ENV{DUMPFILES} ) {
349 my $tmp_file
350 = "$DUMPDIR/$method-$encoding-" . ( $attach ? "attach" : "noattach" );
351  
352 open( SEND, ">$tmp_file.send" );
353 open( RETR, ">$tmp_file.retr" );
354 }
355  
356 if ( $method =~ /encrypt/
357 and $method !~ /sign/
358 and ( $result->get_is_signed
359 or $result->get_sign_key_id
360 or $result->get_sign_mail
361 or $result->get_sign_ok )
362 ) {
363 ok( 0, "Signature reported but message not signed" );
364 return;
365 }
366  
367 if ($method =~ /sign/
368 and ( not $result->get_sign_ok
369 or not $result->get_is_signed
370 or not $result->get_sign_key_id eq $self->get_key_id
371 or not $result->get_sign_mail eq $self->get_key_mail )
372 ) {
373 ok( 0, "Signature bad" );
374 return;
375 }
376  
377 if ($has_encode) {
378 if ( not $result->get_is_encrypted
379 or not $result->get_enc_ok
380 or not $result->get_enc_key_id eq $self->get_key_sub_id
381 or not $result->get_enc_mail eq $self->get_key_mail ) {
382 ok( 0, "Decryption failed" );
383 return;
384 }
385 }
386 else {
387 if ( not $result->get_is_encrypted
388 or not $result->get_enc_ok
389 or not $result->get_enc_key_id eq $self->get_key_sub_id ) {
390 ok( 0, "Decryption failed" );
391 return;
392 }
393 }
394 if ( $method =~ /armor/ ) {
395 my $entity_body = $entity->bodyhandle->as_string;
396 ok( $dec_entity->bodyhandle->as_string eq $entity_body,
397 "$method:$encoding Decryption$attach" );
398 if ( $ENV{DUMPFILES} ) {
399 print SEND $entity_body;
400 print RETR $dec_entity->bodyhandle->as_string;
401 }
402 }
403 elsif ( not $attach ) {
404 ok( $dec_entity->body_as_string eq $entity->body_as_string,
405 "$method:$encoding Decryption$attach" );
406 if ( $ENV{DUMPFILES} ) {
407 print SEND $entity->body_as_string;
408 print RETR $dec_entity->body_as_string;
409 }
410 }
411 else {
412 ok( ( $dec_entity->parts(0)->body_as_string eq
413 $entity->parts(0)->body_as_string
414 and $dec_entity->parts(1)->body_as_string eq
415 $entity->parts(1)->body_as_string
416 ),
417 "$method:$encoding Decryption$attach"
418 );
419 if ( $ENV{DUMPFILES} ) {
420 print SEND $entity->body_as_string;
421 print RETR $dec_entity->body_as_string;
422 }
423 }
424  
425 if ( $ENV{DUMPFILES} ) {
426 close SEND;
427 close RETR;
428 }
429  
430 1;
431 }
432  
433 sub big_test {
434 my $self = shift;
435 my %par = @_;
436 my ($mg, $chunks) = @par{'mg','chunks'};
437  
438 $chunks ||= 200000;
439  
440 srand($chunks);
441  
442 my $line = (join "", map { chr(32+rand(80)) } (1..40))."\n";
443  
444 my @big_data = ( $line x $chunks );
445  
446 my $entity = MIME::Entity->build(
447 From => $self->get_key_mail,
448 Subject => "Mail::GPG Testmail",
449 Data => \@big_data,
450 Encoding => "base64",
451 Charset => "iso-8859-1",
452 );
453  
454 my ($start, $dur);
455 if ( $TIMEIT ) {
456 use Time::HiRes qw(time);
457 $start = time();
458 print "encrypt... ";
459 }
460 my $enc_entity = $mg->mime_sign_encrypt(
461 entity => $entity,
462 recipients => [ $self->get_key_mail ],
463 );
464 if ($TIMEIT ) {
465 $dur = time-$start;
466 print "$dur !\n";
467 }
468 if ( not $mg->is_encrypted( entity => $enc_entity ) ) {
469 ok( 0, "Entity not encrypted" );
470 return;
471 }
472  
473 if ($TIMEIT ) {
474 $start = time();
475 print "print_parse... ";
476 }
477 my $parsed_entity = $self->print_parse_entity(
478 entity => $enc_entity,
479 );
480 if ($TIMEIT ) {
481 $dur = time-$start;
482 print "$dur !\n";
483 }
484  
485 if ($TIMEIT ) {
486 $start = time();
487 print "get_decrypt_key... ";
488 }
489 my ( $dec_key_id, $dec_key_mail )
490 = $mg->get_decrypt_key( entity => $parsed_entity, );
491 if ($TIMEIT ) {
492 $dur = time-$start;
493 print "$dur !\n";
494 }
495  
496 if ($has_encode) {
497 if ( $dec_key_id ne $self->get_key_id ) {
498 ok( 0,
499 "Decryption - key wrong: "
500 . "$dec_key_id=="
501 . $self->get_key_id );
502 return;
503 }
504  
505 if ( $dec_key_mail ne $self->get_key_mail ) {
506 ok( 0,
507 "Decryption - email wrong: "
508 . "$dec_key_mail=="
509 . $self->get_key_mail );
510 return;
511 }
512  
513  
514 }
515 else {
516 if ( $dec_key_id ne $self->get_key_id ) {
517 ok( 0,
518 "Decryption key or email wrong: "
519 . "$dec_key_id=="
520 . $self->get_key_id );
521 return;
522 }
523 }
524  
525 if ($TIMEIT ) {
526 print "decrypt... ";
527 $start = time();
528 }
529 my ( $dec_entity, $result )
530 = eval { $mg->decrypt( entity => $parsed_entity, ); };
531 if ($TIMEIT ) {
532 $dur = time-$start;
533 print "$dur !\n";
534 }
535 if ( not $result->get_sign_ok
536 or not $result->get_is_signed
537 or not $result->get_sign_key_id eq $self->get_key_id
538 or not $result->get_sign_mail eq $self->get_key_mail ) {
539 ok( 0, "Signature bad" );
540 return;
541 }
542  
543 if ($has_encode) {
544 if ( not $result->get_is_encrypted
545 or not $result->get_enc_ok
546 or not $result->get_enc_key_id eq $self->get_key_sub_id
547 or not $result->get_enc_mail eq $self->get_key_mail ) {
548 ok( 0, "Decryption failed" );
549 return;
550 }
551 }
552 else {
553 if ( not $result->get_is_encrypted
554 or not $result->get_enc_ok
555 or not $result->get_enc_key_id eq $self->get_key_sub_id ) {
556 ok( 0, "Decryption failed" );
557 return;
558 }
559 }
560  
561 ok( 1, "Big entity" );
562  
563 1;
564 }
565  
566 1;
« GPG/Result.pm ↑ Summary GPG.pm »