Debian Codegraph @

cassandane/Cassandane/Cyrus/Thread.pm

raw
1 #!/usr/bin/perl
2 #
3 # Copyright (c) 2011-2017 FastMail Pty Ltd. All rights reserved.
4 #
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions
7 # are met:
8 #
9 # 1. Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
11 #
12 # 2. Redistributions in binary form must reproduce the above copyright
13 # notice, this list of conditions and the following disclaimer in
14 # the documentation and/or other materials provided with the
15 # distribution.
16 #
17 # 3. The name "Fastmail Pty Ltd" must not be used to
18 # endorse or promote products derived from this software without
19 # prior written permission. For permission or any legal
20 # details, please contact
21 # FastMail Pty Ltd
22 # PO Box 234
23 # Collins St West 8007
24 # Victoria
25 # Australia
26 #
27 # 4. Redistributions of any form whatsoever must retain the following
28 # acknowledgment:
29 # "This product includes software developed by Fastmail Pty. Ltd."
30 #
31 # FASTMAIL PTY LTD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
32 # INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
33 # EVENT SHALL OPERA SOFTWARE AUSTRALIA BE LIABLE FOR ANY SPECIAL, INDIRECT
34 # OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
35 # USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
36 # TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
37 # OF THIS SOFTWARE.
38 #
39
40 package Cassandane::Cyrus::Thread;
41 use strict;
42 use warnings;
43 use DateTime;
44 use Data::Dumper;
45
46 use lib '.';
47 use base qw(Cassandane::Cyrus::TestCase);
48 use Cassandane::Util::Log;
49
50 sub new
51 {
52 my $class = shift;
53 return $class->SUPER::new({}, @_);
54 }
55
56 sub test_unrelated
57 {
58 my ($self) = @_;
59
60 xlog $self, "test THREAD with no inter-message references";
61 xlog $self, "and all different subjects";
62 my $talk = $self->{store}->get_client();
63 my $res;
64
65 xlog $self, "append some messages";
66 my %exp;
67 my $N = 20;
68 for (1..$N)
69 {
70 my $msg = $self->make_message("Message $_");
71 $exp{$_} = $msg;
72 }
73 xlog $self, "check the messages got there";
74 $self->check_messages(\%exp);
75
76 xlog $self, "The REFERENCES algorithm gives each message in a singleton thread";
77 $res = $talk->thread('REFERENCES', 'US-ASCII', 'ALL');
78 $self->assert_deep_equals([ map { [ $_ ] } (1..$N) ], $res);
79
80 xlog $self, "The ORDEREDSUBJECT algorithm gives each message in a singleton thread";
81 $res = $talk->thread('ORDEREDSUBJECT', 'US-ASCII', 'ALL');
82 $self->assert_deep_equals([ map { [ $_ ] } (1..$N) ], $res);
83
84 xlog $self, "Double-check the messages are still there";
85 $self->check_messages(\%exp);
86 }
87
88 sub test_subjects
89 {
90 my ($self) = @_;
91
92 xlog $self, "test THREAD with no inter-message references";
93 xlog $self, "but apparently similar subjects";
94 my $talk = $self->{store}->get_client();
95 my $res;
96
97 xlog $self, "append some messages";
98 my %exp;
99 my %exp_by_sub;
100 my $N = 20;
101 my @subjects = ( 'quinoa', 'selvedge', 'messenger bag' );
102 for (1..$N)
103 {
104 my $sub = $subjects[($_ - 1) % scalar(@subjects)];
105 $exp_by_sub{$sub} ||= [];
106 my $msg = $self->make_message(("Re: " x scalar(@{$exp_by_sub{$sub}})) . $sub);
107 push(@{$exp_by_sub{$sub}}, $msg);
108 $exp{$_} = $msg;
109 }
110 xlog $self, "check the messages got there";
111 $self->check_messages(\%exp);
112
113 my @expthreads;
114 foreach my $sub (@subjects)
115 {
116 my @thread = ( map { $_->uid } @{$exp_by_sub{$sub}} );
117 my $parent = shift(@thread);
118 push(@expthreads, [ $parent, map { [ $_ ] } @thread ] );
119 }
120
121 xlog $self, "The REFERENCES algorithm gives one thread per subject, even";
122 xlog $self, "though the References headers are completely missing";
123 $res = $talk->thread('REFERENCES', 'US-ASCII', 'ALL');
124 $self->assert_deep_equals(\@expthreads, $res);
125
126 xlog $self, "The ORDEREDSUBJECT algorithm gives one thread per subject";
127 $res = $talk->thread('ORDEREDSUBJECT', 'US-ASCII', 'ALL');
128 $self->assert_deep_equals(\@expthreads, $res);
129
130 xlog $self, "Double-check the messages are still there";
131 $self->check_messages(\%exp);
132 }
133
134 sub test_references_chain
135 {
136 my ($self) = @_;
137
138 xlog $self, "test THREAD with a linear chain of inter-message references";
139 xlog $self, "and apparently similar subjects";
140 my $talk = $self->{store}->get_client();
141 my $res;
142
143 xlog $self, "append some messages";
144 my %exp;
145 my %exp_by_sub;
146 my $N = 20;
147 my @subjects = ( 'cosby sweater', 'brooklyn', 'portland' );
148 for (1..$N)
149 {
150 my $sub = $subjects[($_ - 1) % scalar(@subjects)];
151 $exp_by_sub{$sub} ||= [];
152 my $msg;
153 if (scalar @{$exp_by_sub{$sub}})
154 {
155 my $parent = $exp_by_sub{$sub}->[-1];
156 $msg = $self->make_message("Re: " . $parent->subject,
157 references => [ $parent ]);
158 }
159 else
160 {
161 $msg = $self->make_message($sub);
162 }
163 push(@{$exp_by_sub{$sub}}, $msg);
164 $exp{$_} = $msg;
165 }
166 xlog $self, "check the messages got there";
167 $self->check_messages(\%exp);
168
169 my @expthreads;
170
171 xlog $self, "The REFERENCES algorithm gives the true thread structure which is deep";
172 foreach my $sub (@subjects)
173 {
174 push(@expthreads, [ map { $_->uid } @{$exp_by_sub{$sub}} ]);
175 }
176 $res = $talk->thread('REFERENCES', 'US-ASCII', 'ALL');
177 $self->assert_deep_equals(\@expthreads, $res);
178
179 # From RFC5256
180 # The top level or "root" in ORDEREDSUBJECT threading contains
181 # the first message of every thread. All messages in the root
182 # are siblings of each other. The second message of a thread is
183 # the child of the first message, and subsequent messages of the
184 # thread are siblings of the second message and hence children of
185 # the message at the root. Hence, there are no grandchildren in
186 # ORDEREDSUBJECT threading.
187 xlog $self, "The ORDEREDSUBJECT algorithm gives a false more flat view of the structure";
188 @expthreads = ();
189 foreach my $sub (@subjects)
190 {
191 my @thread = ( map { $_->uid } @{$exp_by_sub{$sub}} );
192 my $parent = shift(@thread);
193 push(@expthreads, [ $parent, map { [ $_ ] } @thread ] );
194 }
195 $res = $talk->thread('ORDEREDSUBJECT', 'US-ASCII', 'ALL');
196 $self->assert_deep_equals(\@expthreads, $res);
197
198 xlog $self, "Double-check the messages are still there";
199 $self->check_messages(\%exp);
200 }
201
202 sub test_references_star
203 {
204 my ($self) = @_;
205
206 xlog $self, "test THREAD with a star configuration of inter-message references";
207 xlog $self, "and apparently similar subjects";
208 my $talk = $self->{store}->get_client();
209 my $res;
210
211 xlog $self, "append some messages";
212 my %exp;
213 my %exp_by_sub;
214 my $N = 20;
215 my @subjects = ( 'cosby sweater', 'brooklyn', 'portland' );
216 foreach my $uid (1..$N)
217 {
218 my $sub = $subjects[($uid - 1) % scalar(@subjects)];
219 $exp_by_sub{$sub} ||= [];
220 my $msg;
221 if (scalar @{$exp_by_sub{$sub}})
222 {
223 my $parent = $exp_by_sub{$sub}->[0];
224 $msg = $self->make_message("Re: " . $parent->subject,
225 references => [ $parent ]);
226 }
227 else
228 {
229 $msg = $self->make_message($sub);
230 }
231 push(@{$exp_by_sub{$sub}}, $msg);
232 $exp{$uid} = $msg;
233 }
234 xlog $self, "check the messages got there";
235 $self->check_messages(\%exp, keyed_on => 'uid');
236
237 my @expthreads;
238 foreach my $sub (@subjects)
239 {
240 my @thread = ( map { $_->uid } @{$exp_by_sub{$sub}} );
241 my $parent = shift(@thread);
242 push(@expthreads, [ $parent, map { [ $_ ] } @thread ] );
243 }
244
245 xlog $self, "The REFERENCES algorithm gives the true thread structure which is flat";
246 $res = $talk->thread('REFERENCES', 'US-ASCII', 'ALL');
247 $self->assert_deep_equals(\@expthreads, $res);
248
249 xlog $self, "The ORDEREDSUBJECT algorithm gives the same flat view";
250 $res = $talk->thread('ORDEREDSUBJECT', 'US-ASCII', 'ALL');
251 $self->assert_deep_equals(\@expthreads, $res);
252
253 xlog $self, "Double-check the messages are still there";
254 $self->check_messages(\%exp, keyed_on => 'uid');
255 }
256
257 sub test_references_missing_parent
258 {
259 my ($self) = @_;
260
261 xlog $self, "test THREAD with two messages which share a common parent";
262 xlog $self, "which is not seen on the server";
263 my $talk = $self->{store}->get_client();
264 my $res;
265 my %exp;
266
267 xlog $self, "Message A is never seen by the server";
268 my $msgA = $self->{gen}->generate(subject => "put a bird on it");
269
270 xlog $self, "Generate message B, which References message A";
271 my $msgB = $self->make_message("Re: " . $msgA->subject,
272 uid => 1,
273 references => [ $msgA ]);
274 $exp{1} = $msgB;
275
276 xlog $self, "Generate message C, which References message A";
277 my $msgC = $self->make_message("Re: " . $msgA->subject,
278 uid => 2,
279 references => [ $msgA ]);
280 $exp{2} = $msgC;
281
282 xlog $self, "check the messages got there";
283 $self->check_messages(\%exp, keyed_on => 'uid');
284
285 xlog $self, "The REFERENCES algorithm gives the true thread";
286 xlog $self, "structure which is flat with a missing common parent";
287 $res = $talk->thread('REFERENCES', 'US-ASCII', 'ALL');
288 $self->assert_deep_equals([[[1],[2]]], $res);
289
290 xlog $self, "The ORDEREDSUBJECT algorithm gives a false more flat view of the structure";
291 $res = $talk->thread('ORDEREDSUBJECT', 'US-ASCII', 'ALL');
292 $self->assert_deep_equals([[1, 2]], $res);
293
294 xlog $self, "Double-check the messages are still there";
295 $self->check_messages(\%exp, keyed_on => 'uid');
296 }
297
298
299 sub test_references_loop
300 {
301 my ($self) = @_;
302
303 xlog $self, "test THREAD with a loop configuration of inter-message references";
304 xlog $self, "and a missing common parent (Bug 3784)";
305 my $talk = $self->{store}->get_client();
306 my $res;
307 my %exp;
308
309 xlog $self, "Generate message B, which References itself and some other messages";
310 my $msgB = $self->{gen}->generate(subject => "Re: put a bird on it", uid => 1);
311 $msgB->set_headers('Message-Id', '<[email protected]>');
312 $msgB->set_headers('References',
313 '<[email protected]>' . "\n" .
314 '<[email protected]>' . "\n" .
315 '<[email protected]>' . "\n" .
316 '<[email protected]>');
317 $msgB->set_headers('In-Reply-To', '<[email protected]>');
318 $self->_save_message($msgB);
319 $exp{1} = $msgB;
320
321 xlog $self, "Generate message C, which References itself and some other messages";
322 my $msgC = $self->{gen}->generate(subject => "Re: put a bird on it", uid => 2);
323 $msgC->set_headers('Message-Id', '<[email protected]>');
324 $msgC->set_headers('References',
325 '<[email protected]>' . "\n" .
326 '<[email protected]>' . "\n" .
327 '<[email protected]>' . "\n" .
328 '<[email protected]>' . "\n" .
329 '<[email protected]>' . "\n" .
330 '<[email protected]>' . "\n" .
331 '<[email protected]>');
332 $msgC->set_headers('In-Reply-To', '<[email protected]>');
333 $self->_save_message($msgC);
334 $exp{2} = $msgC;
335
336 xlog $self, "check the messages got there";
337 $self->check_messages(\%exp, keyed_on => 'uid');
338
339 xlog $self, "The REFERENCES algorithm gives the true thread";
340 xlog $self, "structure which is flat";
341 $res = $talk->thread('REFERENCES', 'US-ASCII', 'ALL');
342 $self->assert_deep_equals([[[1], [2]]], $res);
343
344 xlog $self, "The ORDEREDSUBJECT algorithm gives a false more flat view of the structure";
345 $res = $talk->thread('ORDEREDSUBJECT', 'US-ASCII', 'ALL');
346 $self->assert_deep_equals([[1, 2]], $res);
347
348 xlog $self, "Double-check the messages are still there";
349 $self->check_messages(\%exp, keyed_on => 'uid');
350 }
351
352 1;