File: header.t

package info (click to toggle)
libmailtools-perl 2.22-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 432 kB
  • sloc: perl: 2,224; makefile: 18
file content (221 lines) | stat: -rw-r--r-- 5,967 bytes parent folder | download | duplicates (6)
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
require Mail::Header;

print "1..34\n";

my $h = Mail::Header->new;
my $t = 0;

$h->header_hashref({hhrtest1 => 1, 
	hhrtest2 => [1, "this test line was written by TobiX\n"]});
$h->add(Date => "a test header");
$h->add(Date => "a longer test header");
$h->add(Date => "an even longer test header");

$h->print;
$str = $h->get(Date => 0);
print "#$str#\nnot "
	unless $str eq "a test header\n";
printf "ok %d\n",++$t;

$str = $h->get(Date => 1);
print "#$str#\nnot "
	unless $str eq "a longer test header\n";
printf "ok %d\n",++$t;

$str = $h->get(Date => 2);
print "#$str#\nnot "
	unless $str eq "an even longer test header\n";
printf "ok %d\n",++$t;

$str = $h->get('hhrtest2',1);
print "#$str#\nnot "
	unless $str eq "this test line was written by TobiX\n";
printf "ok %d\n",++$t;

$href=$h->header_hashref();

print "not "
	unless $href->{Date}->[0] eq "a test header\n";
printf "ok %d\n",++$t;

print "not "
	unless $href->{Hhrtest2}->[0];
printf "ok %d\n",++$t;

print "not "
	unless $href->{Hhrtest1}->[0];
printf "ok %d\n",++$t;

$h->fold(30);

print "not "
	unless $h->get(Date => 0) eq "a test header\n";
printf "ok %d\n",++$t;

print "not "
	unless $h->get(Date => 1) eq "a longer test header\n";
printf "ok %d\n",++$t;

print "not "
	unless $h->get(Date => 2) eq "an even longer test\n header\n";
printf "ok %d\n",++$t;

$h->fold(20);

print "not "
	unless $h->get(Date => 0) eq "a test header\n";
printf "ok %d\n",++$t;

print "not "
	unless $h->get(Date => 1) eq "a longer\n test header\n";
printf "ok %d\n",++$t;

print "not "
	unless $h->get(Date => 2) eq "an even\n longer test\n header\n";
printf "ok %d\n",++$t;

$h->fold(30);

print "not "
       unless $h->get(Date => 0) eq "a test header\n";
printf "ok %d\n",++$t;

print "not "
       unless $h->get(Date => 1) eq "a longer test header\n";
printf "ok %d\n",++$t;

print "not "
       unless $h->get(Date => 2) eq "an even longer test\n header\n";
printf "ok %d\n",++$t;

$h->unfold;

print "not "
	unless $h->get(Date => 0) eq "a test header\n";
printf "ok %d\n",++$t;

print "not "
	unless $h->get(Date => 1) eq "a longer test header\n";
printf "ok %d\n",++$t;

print "not "
	unless $h->get(Date => 2) eq "an even longer test header\n";
printf "ok %d\n",++$t;

$head = <<EOF;
From from_
To: to
From: from
Subject:subject
EOF
$body = "body\n";
$mail = "$head\n$body";
@mail = map { "$_\n" } split /\n/, $mail;

print "not "
	unless $h = new Mail::Header \@mail, Modify => 0;
printf "ok %d\n",++$t;

print "not "
	unless $h->as_string eq $head;
printf "ok %d\n",++$t;

print "not "
	unless $h->get('Subject') eq "subject\n";
printf "ok %d\n",++$t;

print "not "
	unless $h->get('To') eq "to\n";
printf "ok %d\n",++$t;

$headin = <<EOF;
Content-Type: multipart/mixed;
       boundary="---- =_NextPart_000_01BDBF1F.DA8F77EE"
Content-Type: multipart/mixed;
       boundary="---- =_NextPart_000_01BDBF1F.DA8F77EE"hkjhgkfhgfhgf"hfkjdhf fhjf fghjghf fdshjfhdsj" hgjhgfjk
Content-Type: multipart/mixed;
       boundary="---- =_NextPart_000_01BDBF1F.DA8F77EE"hkjhg kfhgfhgf"hfkjdhf fhjf fghjghf fdshjfhdsj" hgjhgfjk
Content-Type: multipart/mixed;
       boundary="---- =_NextPart_000_01BDBF1F.DA8F77EE"hhhhhhhhhhhhhhhhhhhhhhhhh fjsdhfkjsd fhdjsfhkj
Content-Type: multipart/mixed;
       boundary="---- =_NextPart_000_01BDBF1F.DA8F77EE" abc def ghfdgfdsgj fdshfgfsdgfdsg hfsdgjfsdg fgsfgjsg
mime-type: text/plain
test1: _abc _def _ghi _fdjhfd _fhdjkfh _dkhkjd _fdjkf _dshfdks _fhdjfdkhfk _dshfds _fdsjk _fdkhfdks _fdsjf _dkf
test1: _abc _def _ghi _fdjhfd _fhdjkfh _dkhaaaaaaaaaaakjdfdjkfdshfdksfhdjfdkhfkdshfdsfdsjkfdkhfdksfdsjf _dkf
EOF
$headout = <<EOF;
Content-Type: multipart/mixed;
 boundary="---- =_NextPart_000_01BDBF1F.DA8F77EE"
Content-Type: multipart/mixed;
 boundary="---- =_NextPart_000_01BDBF1F.DA8F77EE"hkjhgkfhgfhgf"hfkjdhf fhjf fghjghf fdshjfhdsj" hgjhgfjk
Content-Type: multipart/mixed;
 boundary="---- =_NextPart_000_01BDBF1F.DA8F77EE"hkjhg
 kfhgfhgf"hfkjdhf fhjf fghjghf fdshjfhdsj" hgjhgfjk
Content-Type: multipart/mixed;
 boundary="---- =_NextPart_000_01BDBF1F.DA8F77EE"hhhhhhhhhhhhhhhhhhhhhhhhh
 fjsdhfkjsd fhdjsfhkj
Content-Type: multipart/mixed;
 boundary="---- =_NextPart_000_01BDBF1F.DA8F77EE" abc
 def ghfdgfdsgj fdshfgfsdgfdsg hfsdgjfsdg fgsfgjsg
MIME-Type: text/plain
Test1: _abc _def _ghi _fdjhfd _fhdjkfh _dkhkjd _fdjkf _dshfdks _fhdjfdkhfk
 _dshfds _fdsjk _fdkhfdks _fdsjf _dkf
Test1: _abc _def _ghi _fdjhfd _fhdjkfh _dkhaaaaaaaaaaakjdfdjkfdshfdksfhdjfdkhfkdshfdsfdsjkfdkhfdksfdsjf
 _dkf
EOF
@mail = map { "$_\n" } split /\n/, $headin;

print "not "
	unless $h = new Mail::Header \@mail, Modify => 1;
printf "ok %d\n",++$t;

print $h->as_string,"\n----\n",$headout,"\nnot "
	unless $h->as_string eq $headout;
printf "ok %d\n",++$t;

{   # Contributed by Thomas Sibley, introduced in v2.12
    my $bad_continuation = "foo\@example.com\nBcc: evil\@example.com\n";
    my $bad_header       = "To: $bad_continuation";

    my @warnings;
    local $SIG{__WARN__} = sub { push @warnings, "@_" };

    print "not "
        unless $h = new Mail::Header [$bad_header];
    printf "ok %d\n",++$t;

    print "not "
        unless @warnings == 1 and shift(@warnings) =~ /bad header continuation/i;
    printf "ok %d\n",++$t;

    print "not "
        if $h->get("To") or $h->get("Bcc");
    printf "ok %d\n",++$t;

    print "not "
        unless $h = new Mail::Header [$bad_header], Modify => 1;
    printf "ok %d\n",++$t;

    print "not "
        if @warnings;
    printf "ok %d\n",++$t;

    (my $to = $bad_continuation) =~ s/\n//; # replace the first newline only
    print "not "
        unless $h->get("To") eq $to;
    printf "ok %d\n",++$t;
     
    print "not "
        if $h->get("Bcc");
    printf "ok %d\n",++$t;

    my $continued = "foo\@example.com,\n bar\@example.com\n";
    print "not "
        unless $h = new Mail::Header ["To: $continued"];
    printf "ok %d\n",++$t;

    print "not "
        unless $h->get("To") eq $continued;
    printf "ok %d\n",++$t;
}