1#  MyTestSpecific.pm
2#    - module for use with test scripts
3#
4#  Copyright (C) 2000 Frank J. Tobin <ftobin@cpan.org>
5#
6#  This module is free software; you can redistribute it and/or modify it
7#  under the same terms as Perl itself.
8#
9#  This program is distributed in the hope that it will be useful,
10#  but WITHOUT ANY WARRANTY; without even the implied warranty of
11#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12#
13#  $Id: MyTestSpecific.pm,v 1.7 2001/08/21 13:31:50 ftobin Exp $
14#
15
16use strict;
17use English qw( -no_match_vars );
18use Fatal qw/ open close /;
19use IO::File;
20use IO::Handle;
21use IO::Seekable;
22use File::Compare;
23use Exporter;
24use Class::Struct;
25use File::Temp qw (tempdir);
26
27use GnuPG::Interface;
28use GnuPG::Handles;
29
30use vars qw( @ISA           @EXPORT
31             $stdin         $stdout           $stderr
32             $gpg_program   $handles          $gnupg
33             %texts
34           );
35
36@ISA    = qw( Exporter );
37@EXPORT = qw( stdin                  stdout          stderr
38              gnupg_program handles  reset_handles
39              texts                  file_match
40            );
41
42my $homedir;
43if (-f "test/gnupghome") {
44  my $record = IO::File->new( "< test/gnupghome" );
45  $homedir = <$record>;
46  $record->close();
47} else {
48  $homedir = tempdir( DIR => '/tmp');
49  my $record = IO::File->new( "> test/gnupghome" );
50  $record->write($homedir);
51  $record->close();
52}
53
54$ENV{'GNUPGHOME'} = $homedir;
55
56$gnupg = GnuPG::Interface->new( passphrase => 'test' );
57$gnupg->options->hash_init( homedir              => $homedir,
58                            armor                => 1,
59                            meta_interactive     => 0,
60                            meta_signing_key_id  => '0x93AFC4B1B0288A104996B44253AE596EF950DA9C',
61                            always_trust         => 1,
62                          );
63
64struct( Text => { fn => "\$", fh => "\$", data => "\$" } );
65
66$texts{plain} = Text->new();
67$texts{plain}->fn( 'test/plain.1.txt' );
68
69$texts{alt_plain} = Text->new();
70$texts{alt_plain}->fn( 'test/plain.2.txt' );
71
72$texts{encrypted} = Text->new();
73$texts{encrypted}->fn( 'test/encrypted.1.gpg' );
74
75$texts{alt_encrypted} = Text->new();
76$texts{alt_encrypted}->fn( 'test/encrypted.2.gpg' );
77
78$texts{signed} = Text->new();
79$texts{signed}->fn( 'test/signed.1.asc' );
80
81$texts{key} = Text->new();
82$texts{key}->fn( 'test/key.1.asc' );
83
84$texts{temp} = Text->new();
85$texts{temp}->fn( 'test/temp' );
86
87
88foreach my $name ( qw( plain alt_plain encrypted alt_encrypted signed key ) )
89{
90    my $entry = $texts{$name};
91    my $filename = $entry->fn();
92    my $fh = IO::File->new( $filename )
93      or die "cannot open $filename: $ERRNO";
94    $entry->data( [ $fh->getlines() ] );
95}
96
97sub reset_handles
98{
99    foreach ( $stdin, $stdout, $stderr )
100    {
101        $_ = IO::Handle->new();
102    }
103
104    $handles = GnuPG::Handles->new
105      ( stdin   => $stdin,
106        stdout  => $stdout,
107        stderr  => $stderr
108      );
109
110    foreach my $name ( qw( plain alt_plain encrypted alt_encrypted signed key ) )
111    {
112        my $entry = $texts{$name};
113        my $filename = $entry->fn();
114        my $fh = IO::File->new( $filename )
115          or die "cannot open $filename: $ERRNO";
116        $entry->fh( $fh );
117    }
118
119    {
120        my $entry = $texts{temp};
121        my $filename = $entry->fn();
122        my $fh = IO::File->new( $filename, 'w' )
123          or die "cannot open $filename: $ERRNO";
124        $entry->fh( $fh );
125    }
126}
127
128
129
130sub file_match
131{
132    my ( $orig, @compares ) = @_;
133
134    my $found_match = 0;
135
136    foreach my $file ( @compares )
137    {
138        return 1
139          if compare( $file, $orig ) == 0;
140    }
141
142    return 0;
143}
144
145
146
147# blank user_id_string and different validity for expired sig in GPG 2.2.x vs 1.x, 2.1
148sub get_expired_test_sig_params {
149    my $gnupg = shift;
150    my $version = $gnupg->version;
151
152    my %sig_params = (
153        date_string => '2000-03-16',
154        hex_id => '56FFD10A260C4FA3',
155        sig_class => 0x10,
156        algo_num => 17,
157        is_exportable => 1,
158    );
159    if ($gnupg->cmp_version($gnupg->version, '2.2') > 0) {
160        $sig_params{user_id_string} = '';
161        $sig_params{validity} = '?';
162    }
163    else {
164        $sig_params{user_id_string} = 'Frank J. Tobin <ftobin@neverending.org>',
165        $sig_params{validity} = '!';
166    }
167    return %sig_params
168}
169
1701;
171