1# Before `make install' is performed this script should be runnable with
2# `make test'. After `make install' it should work as `perl test.pl'
3
4#use Test;
5
6use strict;
7use Test::More;
8
9use constant USERID	=> "GnuPG Test";
10use constant PASSWD	=> "test";
11use constant UNTRUSTED	=> "Francis";
12
13use Symbol;
14use GnuPG;
15use GnuPG::Tie::Encrypt;
16use GnuPG::Tie::Decrypt;
17
18BEGIN {
19    $| = 1;
20}
21
22my @tests = qw(
23                gen_key_test
24                import_test
25                import2_test
26                import3_test
27                export_test
28                export2_test
29                export_secret_test
30                encrypt_test
31                pipe_encrypt_test
32                pipe_decrypt_test
33                encrypt_sign_test
34                encrypt_sym_test
35                encrypt_notrust_test
36                decrypt_test
37                decrypt_sign_test
38                decrypt_sym_test
39                sign_test
40                detachsign_test
41                clearsign_test
42                verify_sign_test
43                verify_detachsign_test
44                verify_clearsign_test
45                tie_encrypt_test
46                tie_decrypt_test
47                tie_decrypt_para_mode_test
48                multiple_recipients
49    	        );
50
51if ( defined $ENV{TESTS} ) {
52    @tests = split /\s+/, $ENV{TESTS};
53}
54
55plan tests => scalar @tests;
56
57my $gpg = new GnuPG( homedir => "test", trace => $ENV{TRACING} );
58
59for ( @tests ) {
60    eval {
61	    no strict 'refs';   # We are using symbolic references
62	    &$_();
63    };
64
65    (ok !$@, $_) || diag $@;
66}
67
68sub multiple_recipients {
69    $gpg->encrypt(
70          recipient => [ USERID, UNTRUSTED ],
71		  output    => "test/file.txt.gpg",
72		  armor	    => 1,
73		  plaintext => "test/file.txt",
74    );
75
76    open my $fh, '<', "test/file.txt.gpg";
77
78    die "'test/file.txt.gpg' is empty\n" unless -s 'test/file.txt.gpg';
79
80}
81
82
83sub gen_key_test {
84    diag "Generating a key - can take some time";
85    $gpg->gen_key(
86		  passphrase => PASSWD,
87		  name	     => USERID,
88    );
89}
90
91sub import_test {
92    $gpg->import_keys( keys => "test/key1.pub" );
93}
94
95sub import2_test {
96    $gpg->import_keys( keys => "test/key1.pub" );
97}
98
99sub import3_test {
100    $gpg->import_keys( keys => [ qw( test/key1.pub test/key2.pub ) ] );
101}
102sub export_test {
103    $gpg->export_keys( keys	=> USERID,
104		       armor	=> 1,
105		       output	=> "test/key.pub",
106		     );
107}
108
109sub export2_test {
110    $gpg->export_keys( armor	=> 1,
111		       output	=> "test/keyring.pub",
112		     );
113}
114
115sub export_secret_test {
116    $gpg->export_keys( secret	=> 1,
117		       armor	=> 1,
118		       output	=> "test/key.sec",
119		     );
120}
121
122sub encrypt_test {
123    $gpg->encrypt(
124		  recipient => USERID,
125		  output    => "test/file.txt.gpg",
126		  armor	    => 1,
127		  plaintext => "test/file.txt",
128		 );
129}
130
131sub pipe_encrypt_test {
132    open CAT, "| cat > test/pipe-file.txt.gpg"
133      or die "can't fork: $!\n";
134    $gpg->encrypt(
135		  recipient => USERID,
136		  output    => \*CAT,
137		  armor	    => 1,
138		  plaintext => "test/file.txt",
139		 );
140    close CAT;
141}
142
143sub encrypt_sign_test {
144    $gpg->encrypt(
145		  recipient	=> USERID,
146		  output	=> "test/file.txt.sgpg",
147		  armor		=> 1,
148		  sign		=> 1,
149		  plaintext	=> "test/file.txt",
150		  passphrase	=> PASSWD,
151		 );
152}
153
154sub encrypt_sym_test {
155    $gpg->encrypt(
156		  output	=> "test/file.txt.cipher",
157		  armor		=> 1,
158		  plaintext	=> "test/file.txt",
159		  symmetric	=> 1,
160		  passphrase	=> PASSWD,
161		 );
162}
163
164sub encrypt_notrust_test {
165    $gpg->encrypt(
166		  recipient	=> UNTRUSTED,
167		  output	=> "test/file.txt.dist.gpg",
168		  armor		=> 1,
169		  sign		=> 1,
170		  plaintext	=> "test/file.txt",
171		  passphrase	=> PASSWD,
172		 );
173}
174
175sub sign_test {
176    $gpg->sign(
177		  recipient	=> USERID,
178		  output	=> "test/file.txt.sig",
179		  armor		=> 1,
180		  plaintext	=> "test/file.txt",
181		  passphrase	=> PASSWD,
182		 );
183}
184
185sub detachsign_test {
186    $gpg->sign(
187		  recipient	=> USERID,
188		  output	=> "test/file.txt.asc",
189		  "detach-sign" => 1,
190		  armor		=> 1,
191		  plaintext	=> "test/file.txt",
192		  passphrase	=> PASSWD,
193		 );
194}
195
196sub clearsign_test {
197    $gpg->clearsign(
198		    output	=> "test/file.txt.clear",
199		    armor	=> 1,
200		    plaintext	=> "test/file.txt",
201		    passphrase  => PASSWD,
202		 );
203}
204
205sub decrypt_test {
206    $gpg->decrypt(
207		    output	=> "test/file.txt.plain",
208		    ciphertext	=> "test/file.txt.gpg",
209		    passphrase  => PASSWD,
210		 );
211}
212sub pipe_decrypt_test {
213    open CAT, "cat test/file.txt.gpg|"
214      or die "can't fork: $!\n";
215    $gpg->decrypt(
216		    output	=> "test/file.txt.plain",
217		    ciphertext	=> \*CAT,
218		    passphrase  => PASSWD,
219		 );
220    close CAT;
221}
222
223sub decrypt_sign_test {
224    $gpg->decrypt(
225		    output	=> "test/file.txt.plain2",
226		    ciphertext	=> "test/file.txt.sgpg",
227		    passphrase  => PASSWD,
228		 );
229}
230
231sub decrypt_sym_test {
232    $gpg->decrypt(
233		    output	=> "test/file.txt.plain3",
234		    ciphertext	=> "test/file.txt.cipher",
235		    symmetric	=> 1,
236		    passphrase  => PASSWD,
237		 );
238}
239
240sub verify_sign_test {
241    $gpg->verify( signature	=> "test/file.txt.sig" );
242}
243
244sub verify_detachsign_test {
245    $gpg->verify( signature	=> "test/file.txt.asc",
246		  file		=> "test/file.txt",
247		);
248}
249
250sub verify_clearsign_test {
251    $gpg->verify( signature => "test/file.txt.clear" );
252}
253
254sub encrypt_from_fh_test {
255    open ( FH, "test/file.txt" )
256      or die "error opening file: $!\n";
257    $gpg->encrypt(
258		  recipient => UNTRUSTED,
259		  output    => "test/file-fh.txt.gpg",
260		  armor	    => 1,
261		  plaintext => \*FH,
262		 );
263    close ( FH )
264      or die "error closing file: $!\n";
265}
266
267sub encrypt_to_fh_test {
268    open ( FH, ">test/file-fho.txt.gpg" )
269      or die "error opening file: $!\n";
270    $gpg->encrypt(
271		  recipient => UNTRUSTED,
272		  output    => \*FH,
273		  armor	    => 1,
274		  plaintext => "test/file.txt",
275		 );
276    close ( FH )
277      or die "error closing file: $!\n";
278}
279
280sub tie_encrypt_test {
281    open( PLAINTEXT, "test/file.txt" )
282      or die "error opening file: $!\n";
283    open( CIPHER_OUT, ">test/file-tie.txt.asc" )
284      or die "error writing encrypting file\n";
285    tie *CIPHER, 'GnuPG::Tie::Encrypt', homedir => "test",
286      recipient => 'GnuPG', armor => 1, trace => $ENV{TRACING};
287    while (<PLAINTEXT>) {
288	print CIPHER $_;
289    }
290    close PLAINTEXT;
291
292    while (<CIPHER>) {
293	print CIPHER_OUT $_;
294    }
295    close CIPHER;
296    untie *CIPHER;
297    close CIPHER_OUT;
298}
299
300sub tie_decrypt_test {
301    open( PLAINTEXT, "test/file.txt" )
302      or die "error opening plaintext file: $!\n";
303    my $plaintext_orig = "";
304    $plaintext_orig .= $_ while ( <PLAINTEXT>);
305    close PLAINTEXT;
306
307    open( CIPHER, "test/file-tie.txt.asc" )
308      or die "error opening encrypted file\n";
309    tie *GNUPG, 'GnuPG::Tie::Decrypt', homedir => "test",
310      passphrase => PASSWD, trace => $ENV{TRACING};
311
312    while ( <CIPHER> ) {
313	print GNUPG $_;
314    }
315    my $plaintext = "";
316    while ( <GNUPG> ) {
317	$plaintext .= $_;
318    }
319    close GNUPG;
320    untie *GNUPG;
321    close CIPHER;
322
323    die "plaintext doesn't match\n" unless $plaintext_orig eq $plaintext;
324}
325
326sub tie_decrypt_para_mode_test {
327    my $plaintext = <<EOF;
328This is a paragraph.
329
330This is another paragraph
331which continue on another line.
332
333
334
335This is the final paragraph.
336EOF
337    tie *CIPHER, 'GnuPG::Tie::Encrypt', homedir => "test",
338      recipient => 'GnuPG', armor => 1, trace => $ENV{TRACING};
339
340    print CIPHER $plaintext;
341    local $/ = undef;
342    my $cipher = <CIPHER>;
343    close CIPHER;
344    untie *CIPHER;
345
346    local $/ = "";
347    tie *TEST, 'GnuPG::Tie::Decrypt', homedir => "test", passphrase => PASSWD;
348    print TEST $cipher;
349
350    my @para = <TEST>;
351    close TEST;
352    untie *TEST;
353
354    my $count = @para;
355    die "paragraph count should be 3: $count\n" unless $count == 3;
356    die "plaintext doesn't match input\n" unless join( "", @para) eq $plaintext;
357}
358