File: Test.pm

package info (click to toggle)
libmail-box-perl 4.01-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,744 kB
  • sloc: perl: 9,021; makefile: 6
file content (391 lines) | stat: -rw-r--r-- 8,062 bytes parent folder | download
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
# This code is part of Perl distribution Mail-Box version 4.01.
# The POD got stripped from this file by OODoc version 3.05.
# For contributors see file ChangeLog.

# This software is copyright (c) 2001-2025 by Mark Overmeer.

# This is free software; you can redistribute it and/or modify it under
# the same terms as the Perl 5 programming language system itself.
# SPDX-License-Identifier: Artistic-1.0-Perl OR GPL-1.0-or-later

# This code is part of distribution Mail-Message.  Meta-POD processed with
# OODoc into POD and HTML manual-pages.  See README.md
# Copyright Mark Overmeer.  Licensed under the same terms as Perl itself.

package Mail::Box::Test;{
our $VERSION = '4.01';
}

use parent 'Exporter';

use strict;
use warnings;

use Log::Report      'mail-box', import => [ qw// ];

use File::Copy            qw/copy/;
use List::Util            qw/first/;
use File::Spec::Functions qw/catdir catfile devnull/;;
use File::Temp            qw/tempdir/;
use Cwd                   qw/getcwd/;
use Sys::Hostname         qw/hostname/;
use Test::More;


our @EXPORT = qw/
	clean_dir copy_dir
	unpack_mbox2mh unpack_mbox2maildir
	compare_lists listdir
	compare_message_prints reproducable_text
	compare_thread_dumps

	$folderdir
	$workdir
	$src $unixsrc $winsrc
	$fn  $unixfn  $winfn
	$cpy $cpyfn
	$raw_html_data
	$crlf_platform $windows
/;

our ($logfile, $folderdir);
our ($src, $unixsrc, $winsrc);
our ($fn,  $unixfn,  $winfn);
our ($cpy, $cpyfn);
our ($crlf_platform, $windows);
our $workdir;

BEGIN {
	$windows       = $^O =~ m/mswin32/i;
	$crlf_platform = $windows;

	$folderdir     = catdir 't','folders';
	$workdir       = tempdir(CLEANUP => 1);

	$logfile = catfile getcwd(), 'run-log';
	$unixfn  = 'mbox.src';
	$winfn   = 'mbox.win';
	$cpyfn   = 'mbox.cpy';

	$unixsrc = catfile $folderdir, $unixfn;
	$winsrc  = catfile $folderdir, $winfn;
	$cpy     = catfile $workdir, $cpyfn;

	($src, $fn) = $windows ? ($winsrc, $winfn) : ($unixsrc, $unixfn);

	# ensure to test the Perl Parser not the C-Parser (separate distribution)
	require Mail::Box::Parser::Perl;
	Mail::Box::Parser->defaultParserType('Mail::Box::Parser::Perl');
}

#
# CLEAN_DIR
# Clean a directory structure, typically created by unpack_mbox()
#

sub clean_dir($);
sub clean_dir($)
{	my $dir = shift;
	opendir my $dh, $dir or return;

	my @items = map m/(.*)/ && "$dir/$1",   # untainted
		grep !/^\.\.?$/, readdir $dh;

	foreach (@items)
	{	if(-d)  { clean_dir $_ }
		else    { unlink $_ }
	}

	closedir $dh;
	rmdir $dir;
}

#
# COPY_DIR FROM, TO
# Copy directory to other place (not recursively), cleaning the
# destination first.
#

sub copy_dir($$)
{	my ($orig, $dest) = @_;

	clean_dir $dest;

	mkdir $dest
		or die "Cannot create copy destination $dest: $!\n";

	opendir my $dh, $orig
		or die "Cannot open directory $orig: $!\n";

	foreach my $name (map { !m/^\.\.?$/ && m/(.*)/ ? $1 : () } readdir $dh)
	{	my $from = catfile($orig, $name);
		next if -d $from;

		my $to   = catfile($dest, $name);
		copy($from, $to) or die "Couldn't copy $from,$to: $!\n";
	}

	close $dh;
}

# UNPACK_MBOX2MH
# Unpack an mbox-file into an MH-directory.
# This skips message-nr 13 for testing purposes.
# Blanks before "From" are removed.

sub unpack_mbox2mh($$)
{	my ($file, $dir) = @_;

	clean_dir $dir;
	mkdir $dir, 0700;

	my $count = 1;
	my $blank;

	open my $fh, '<:raw', $file or die;
	open my $out, '>:raw', devnull;

	local $_;
	while(<$fh>)
	{	if( /^From / )
		{	$out->close;
			undef $blank;
			open $out, '>', "$dir/".$count++ or die;
			$count++ if $count==13;  # skip 13 for test
			next;                    # from line not included in file.
		}

		$out->print($blank) if defined $blank;

		if( m/^\015?\012$/ )
		{	$blank = $_;
			next;
		}

		undef $blank;
		$out->print($_);
	}
	$out->close;
	$fh->close;
}

# UNPACK_MBOX2MAILDIR
# Unpack an mbox-file into an Maildir-directory.

our @maildir_names =
(   '8000000.localhost.23:2,',
	'90000000.localhost.213:2,',
	'110000000.localhost.12:2,',
	'110000001.l.42:2,',
	'110000002.l.42:2,',
	'110000002.l.43:2,',
	'110000004.l.43:2,',
	'110000005.l.43:2,',
	'110000006.l.43:2,',
	'110000007.l.43:2,D',
	'110000008.l.43:2,DF',
	'110000009.l.43:2,DFR',
	'110000010.l.43:2,DFRS',
	'110000011.l.43:2,DFRST',
	'110000012.l.43:2,F',
	'110000013.l.43:2,FR',
	'110000014.l.43:2,FRS',
	'110000015.l.43:2,FRST',
	'110000016.l.43:2,DR',
	'110000017.l.43:2,DRS',
	'110000018.l.43:2,DRST',
	'110000019.l.43:2,FS',
	'110000020.l.43:2,FST',
	'110000021.l.43:2,R',
	'110000022.l.43:2,RS',
	'110000023.l.43:2,RST',
	'110000024.l.43:2,S',
	'110000025.l.43:2,ST',
	'110000026.l.43:2,T',
	'110000027.l.43:2,',
	'110000028.l.43:2,',
	'110000029.l.43:2,',
	'110000030.l.43:2,',
	'110000031.l.43:2,',
	'110000032.l.43:2,',
	'110000033.l.43:2,',
	'110000034.l.43:2,',
	'110000035.l.43:2,',
	'110000036.l.43:2,',
	'110000037.l.43:2,',
	'110000038.l.43',
	'110000039.l.43',
	'110000040.l.43',
	'110000041.l.43',
	'110000042.l.43',
);

sub unpack_mbox2maildir($$)
{	my ($file, $dir) = @_;
	clean_dir $dir;

	@maildir_names==45 or die;
	mkdir $dir or die;
	mkdir catfile($dir, 'cur') or die;
	mkdir catfile($dir, 'new') or die;
	mkdir catfile($dir, 'tmp') or die;

	open my $fh, '<:raw', $file or die;
	open my $out, '>:raw', devnull;

	my $msgnr      = 0;
	my $last_empty = 0;
	my $blank;

	local $_;
	while(<$fh>)
	{	if( m/^From / )
		{	$out->close;
			undef $blank;
			my $now      = time;
			my $hostname = hostname;

			my $msgfile  = catfile $dir, ($msgnr > 40 ? 'new' : 'cur'), $maildir_names[$msgnr++];

			open $out, ">:raw", $msgfile
				 or die "Create $msgfile: $!\n";

			next;                    # from line not included in file.
		}

		$out->print($blank) if defined $blank;

		if( m/^\015?\012$/ )
		{	$blank = $_;
			next;
		}

		undef $blank;
		$out->print($_);
	}

	$out->close;
	$fh->close;
}

#
# Compare two lists.
#

sub compare_lists($$)
{	my ($first, $second) = @_;
#warn "[@$first]==[@$second]\n";
	return 0 unless @$first == @$second;
	for(my $i=0; $i<@$first; $i++)
	{	return 0 unless $first->[$i] eq $second->[$i];
	}
	1;
}

#
# Compare the text of two messages, rather strict.
# On CRLF platforms, the Content-Length may be different.
#

sub compare_message_prints($$$)
{	my ($first, $second, $label) = @_;

	if($crlf_platform)
	{	s/Content-Length: \d+/Content-Length: <removed>/g for $first, $second;
	}

	is($first, $second, $label);
}

#
# Strip message text down the things which are the same on all
# platforms and all situations.
#

sub reproducable_text($)
{	my $text  = shift;
	my @lines = split /^/m, $text;
	foreach (@lines)
	{	s/((?:references|message-id|date|content-length)\: ).*/$1<removed>/i;
		s/boundary-\d+/boundary-<removed>/g;
	}
	join '', @lines;
}

#
# Compare two outputs of thread details.
# On CRLF platforms, the reported sizes are ignored.
#

sub compare_thread_dumps($$$)
{	my ($first, $second, $label) = @_;

	if($crlf_platform)
	{	$first  =~ s/^..../    /gm;
		$second =~ s/^..../    /gm;
	}

	is($first, $second, $label);
}

#
# List directory
# This removes '.' and '..'
#

sub listdir($)
{	my $dir = shift;
	opendir my $list, $dir or return ();
	my @entities = grep !/^\.\.?$/, readdir $list;
	closedir $list;
	@entities;
}

#
# A piece of HTML text which is used in some tests.
#

our $raw_html_data = <<'TEXT';
<HTML>
<HEAD>
<TITLE>My home page</TITLE>
</HEAD>
<BODY BGCOLOR=red>

<H1>Life according to Brian</H1>

This is normal text, but not in a paragraph.<P>New paragraph
in a bad way.

And this is just a continuation.  When texts get long, they must be
auto-wrapped; and even that is working already.

<H3>Silly subsection at once</H3>
<H1>and another chapter</H1>
<H2>again a section</H2>
<P>Normal paragraph, which contains an <IMG
SRC=image.gif>, some
<I>italics with linebreak
</I> and <TT>code</TT>

<PRE>
And now for the preformatted stuff
   it should stay as it was
      even   with   strange blanks
  and indentations
</PRE>

And back to normal text...
<UL>
<LI>list item 1
    <OL>
    <LI>list item 1.1
    <LI>list item 1.2
    </OL>
<LI>list item 2
</UL>
</BODY>
</HTML>
TEXT

1;