]> Sergey Matveev's repositories - public-inbox.git/commitdiff
t/thread-cycle: test self-referential messages
authorEric Wong <e@80x24.org>
Wed, 5 Oct 2016 23:47:31 +0000 (23:47 +0000)
committerEric Wong <e@80x24.org>
Wed, 5 Oct 2016 23:53:37 +0000 (23:53 +0000)
Some broken (or malicious) mailers may include a generated
Message-ID in its References header, so be prepared for it.

t/thread-cycle.t

index 4d60f7e39bcb8df3f99411c921e0c2924ae30157..0e1ecfe261507efa5e1697d999e393c3ff050176 100644 (file)
@@ -51,18 +51,7 @@ my @msgs = map {
        }
 );
 
-my $th = PublicInbox::SearchThread->new(\@msgs);
-$th->thread;
-$th->order(sub { [ sort { $a->{id} cmp $b->{id} } @{$_[0]} ] });
-my $st = '';
-my @q = map { (0, $_) } @{$th->{rootset}};
-while (@q) {
-       my $level = shift @q;
-       my $node = shift @q or next;
-       $st .= (" "x$level). "$node->{id}\n";
-       my $cl = $level + 1;
-       unshift @q, map { ($cl, $_) } @{$node->{children}}
-}
+my $st = thread_to_s(\@msgs);
 
 SKIP: {
        skip 'Mail::Thread missing', 1 unless $mt;
@@ -71,7 +60,7 @@ SKIP: {
        $mt->order(sub { sort { $a->messageid cmp $b->messageid } @_ });
        my $check = '';
 
-       @q = map { (0, $_) } $mt->rootset;
+       my @q = map { (0, $_) } $mt->rootset;
        while (@q) {
                my $level = shift @q;
                my $node = shift @q or next;
@@ -81,6 +70,28 @@ SKIP: {
        is($check, $st, 'Mail::Thread output matches');
 }
 
+@msgs = map { bless $_, 'PublicInbox::SearchMsg' } (
+       { mid => 'a@b' },
+       { mid => 'b@c', references => '<a@b> <b@c>' },
+       { mid => 'd@e', references => '<d@e>' },
+);
+
+is(thread_to_s(\@msgs), "a\@b\n b\@c\nd\@e\n", 'ok with self-references');
+
 done_testing();
 
-1;
+sub thread_to_s {
+       my $th = PublicInbox::SearchThread->new(shift);
+       $th->thread;
+       $th->order(sub { [ sort { $a->{id} cmp $b->{id} } @{$_[0]} ] });
+       my $st = '';
+       my @q = map { (0, $_) } @{$th->{rootset}};
+       while (@q) {
+               my $level = shift @q;
+               my $node = shift @q or next;
+               $st .= (" "x$level). "$node->{id}\n";
+               my $cl = $level + 1;
+               unshift @q, map { ($cl, $_) } @{$node->{children}};
+       }
+       $st;
+}