File: 01_imap_base.t

package info (click to toggle)
libnetxap-perl 0.02-6
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 232 kB
  • ctags: 276
  • sloc: perl: 1,595; sh: 156; makefile: 49
file content (289 lines) | stat: -rw-r--r-- 7,954 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
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
#-*-perl-*-
print "1..73\n";

use Net::IMAP;

my $host = '/usr/sbin/imapd';

my $testmsg = "From: joe\@example.com
To: bob\@example.com
Subject: an imap test
MIME-Version: 1.0
Content-ID: greeble
Content-Description: snorf

atest
";

my $multipartmsg = "From joe\@example.com
To: bob\@example.com
Sobject: a multipart imap test
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary=bork
Content-Disposition: inline
Content-Language: en

--bork

snorfle-foo

--bork
Content-Type: message/rfc822

From: mary\@example.com
To: larry\@example.com
Date: Sat, 28 Aug 1999 21:15:05 -0700

an embedded message

--bork
Content-Type: multipart/alternative; boundary=inner

--inner

inner1

--inner

inner2

--inner--

--bork--
";

my $time = time;

my $testfolder = "net\"xap.$$.$time";

my $imap = new Net::IMAP($host);
my $response;
my $status = undef;		# used in status callback

ok_if(1, defined($imap));

my @capabilities = $imap->capabilities;

my $fetch_resp = undef;
my @search_items = ();

$imap->set_untagged_callback('fetch', \&do_fetch);
$imap->set_untagged_callback('status', \&do_status);
$imap->set_untagged_callback('search', \&do_search);
$imap->set_untagged_callback('list', \&do_list);

ok_if(2, scalar(@capabilities));

ok_if(3, $imap->has_capability('imap4rev1'));

$response = $imap->noop;
ok_if(4, defined($response));
ok_if(5, $response->status eq 'ok');

warn "expect a warning message about an unknown command:\n";

$response = $imap->notacommand('arf');
ok_if(6, !defined($response));

$response = $imap->select($testfolder);
ok_if(7, $response->status eq 'no');

$response = $imap->create($testfolder);
ok_if(8, $response->status eq 'ok');

$response = $imap->append($testfolder, $testmsg);
ok_if(9, $response->status eq 'ok');

$response = $imap->append($testfolder, $testmsg,
			  Date => '28-Aug-1999 15:16:17 -0700');
ok_if(10, $response->status eq 'ok');

$response = $imap->append($testfolder, $testmsg,
			  Date => [$time, '-0700']);
ok_if(11, $response->status eq 'ok');

$response = $imap->append($testfolder, $multipartmsg,
			  Flags => ["\\Draft"]);
ok_if(12, $response->status eq 'ok');

$response = $imap->select($testfolder);
ok_if(13, $response->status eq 'ok');
ok_if(14, $imap->qty_messages == 4);
ok_if(15, $imap->qty_recent == 4);

$response = $imap->copy(1, $testfolder);
ok_if(16, $response->status eq 'ok');

ok_if(17, $imap->qty_messages == 5);

$response = $imap->store('1:4', 'flags', '\\deleted');
ok_if(18, $response->status eq 'ok');

$response = $imap->search('deleted');
ok_if(19, $response->status eq 'ok');
ok_if(20, $#search_items == 3);
ok_if(21, ($#search_items == 3) && ($search_items[0] == 1));

$response = $imap->store(5, 'flags', '\\flagged');
ok_if(22, $response->status eq 'ok');

$response = $imap->fetch(5, qw(rfc822.size internaldate flags
			       envelope bodystructure body.peek[]));
ok_if(23, $response->status eq 'ok');

ok_if(24, $fetch_resp->msgnum == 5);
ok_if(25, scalar($fetch_resp->items) == 6);

my $crlfstring = $testmsg;
$crlfstring =~ s/\n/\r\n/mg;
$msglen = length($crlfstring);

my $n = $fetch_resp->item('rfc822.size');

ok_if(26, defined($n));
ok_if(27, defined($n) && ($n == $msglen));

ok_if(28, defined($fetch_resp->item('internaldate')));

my $flags = $fetch_resp->item('flags');

ok_if(29, defined($flags));
ok_if(30, defined($flags) && $flags->has_flag('\flagged'));

my $envelope = $fetch_resp->item('envelope');

ok_if(31, defined($envelope));

my $subject;
$subject = $envelope->subject if defined($envelope);

ok_if(32, defined($subject));
ok_if(33, defined($subject && ($subject eq 'an imap test')));

my $to;
$to = $envelope->to if defined($envelope);

ok_if(34, scalar(@{$to}) == 1);
ok_if(35, (scalar(@{$to}) == 1) && ($to->[0]->domain eq 'example.com'));
ok_if(36, ((scalar(@{$to}) == 1)
	   && ($to->[0]->as_string eq 'bob@example.com')));

my $bodystructure = $fetch_resp->item('bodystructure');
ok_if(37, defined($bodystructure));
ok_if(38, $bodystructure->type eq 'text');
ok_if(39, $bodystructure->subtype eq 'plain');

ok_if(40, 1);

my $parms = $bodystructure->parameters if defined($bodystructure);
ok_if(41, defined($parms) && (ref($parms) eq 'HASH'));
my $id = $bodystructure->id if defined($bodystructure);
ok_if(42, $id eq 'greeble');
my $description = $bodystructure->description if defined($bodystructure);
ok_if(43, $description eq 'snorf');
my $encoding = $bodystructure->encoding if defined($bodystructure);
ok_if(44, $encoding eq '7bit');
my $size = $bodystructure->size if defined($bodystructure);
ok_if(45, defined($size) && ($size == 7));
my $lines = $bodystructure->lines if defined($bodystructure);
ok_if(46, defined($lines) && ($lines == 1));
ok_if(47, defined($bodystructure) && !defined($bodystructure->envelope));
ok_if(48, defined($bodystructure) && !defined($bodystructure->bodystructure));

my $body = $fetch_resp->item('body[]');

ok_if(49, defined($body));
ok_if(50, defined($body) && ($body eq $testmsg));
#------------------------------------------------------------------------------
$response = $imap->fetch(4, qw(bodystructure));
ok_if(51, $response->status eq 'ok');

$bodystructure = $fetch_resp->item('bodystructure');
ok_if(52, defined($bodystructure));
ok_if(53, defined($bodystructure) && ($bodystructure->type eq 'multipart'));
ok_if(54, defined($bodystructure) && ($bodystructure->subtype eq 'mixed'));

my $parts = $bodystructure->parts if defined($bodystructure);
ok_if(55, scalar(@{$parts}) == 3);
$parms = $bodystructure->parameters if defined($bodystructure);
ok_if(56, ref($parms) eq 'HASH');
my $list = $bodystructure->language if defined($bodystructure);
ok_if(57, (scalar(@{$list}) == 1) && ($list->[0] eq 'en'));

$envelope = $parts->[1]->envelope if defined($parts);
$to = $envelope->to if defined($envelope);
$addr = $to->[0] if defined($to);
ok_if(58, defined($envelope));
ok_if(59, defined($to));
ok_if(60, defined($addr) && $addr->isa('Net::IMAP::Addr'));

my $parts2 = $parts->[2]->parts;
ok_if(61, defined($parts2) && (scalar(@{$parts2}) == 2));

#------------------------------------------------------------------------------
# test crlf<->lf mapping
$imap->{Options}{EOL} = 'crlf';
$response = $imap->fetch(1, qw(body.peek[]));
ok_if(62, $response->status eq 'ok');
$body = $fetch_resp->item('body[]');
ok_if(63, $body ne $testmsg);
$body =~ s/\r$//mg if defined($body);
ok_if(64, $body eq $testmsg);
#------------------------------------------------------------------------------
my $list_has_inbox = 0;
$response = $imap->list('', 'inbox');
ok_if(65, $list_has_inbox);
#------------------------------------------------------------------------------
$response = $imap->expunge;
ok_if(66, $response->status eq 'ok');
ok_if(67, $imap->qty_messages == 1);
#------------------------------------------------------------------------------
$response = $imap->close;
ok_if(68, $response->status eq 'ok');

$response = $imap->status($testfolder, 'messages');
ok_if(69, $response->status eq 'ok');
ok_if(70, defined($status));

ok_if(71, (defined($status->item('messages'))
	   && ($status->item('messages') == 1)));

$response = $imap->delete($testfolder);
ok_if(72, $response->status eq 'ok');

$response = $imap->logout;
ok_if(73, defined($response));
###############################################################################
sub do_fetch {
  my $self = shift;
  my $resp = shift;

  $fetch_resp = $resp;		# make it available to the test
}

sub do_status {
  my $self = shift;
  my $resp = shift;

  $status = $resp if ($resp->mailbox eq $testfolder);
}

sub do_search {
  my $self = shift;
  my $resp = shift;

  @search_items = $resp->msgnums;
}

sub do_list {
  my $self = shift;
  my $resp = shift;

  $list_has_inbox++ if (lc($resp->mailbox) eq 'inbox');
}
#------------------------------------------------------------------------------
sub ok_if {
  print "not " unless $_[1];
  print "ok $_[0]\n";
}
###############################################################################