Debian Codegraph @

cassandane/Cassandane/Cyrus/ImapTest.pm

raw
1 #!/usr/bin/perl
2 #
3 # Copyright (c) 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
32 # THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
33 # AND FITNESS, IN NO EVENT SHALL OPERA SOFTWARE AUSTRALIA BE LIABLE
34 # FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
35 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
36 # AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
37 # OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
38 #
39
40 package Cassandane::Cyrus::ImapTest;
41 use strict;
42 use warnings;
43 use Cwd qw(abs_path);
44 use File::Path qw(mkpath);
45 use DateTime;
46
47 use lib '.';
48 use base qw(Cassandane::Cyrus::TestCase);
49 use Cassandane::Util::Log;
50 use Cassandane::Cassini;
51
52 my $basedir;
53 my $binary;
54 my $testdir;
55 my %suppressed;
56
57 sub init
58 {
59 my $cassini = Cassandane::Cassini->instance();
60 $basedir = $cassini->val('imaptest', 'basedir');
61 return unless defined $basedir;
62 $basedir = abs_path($basedir);
63
64 my $supp = $cassini->val('imaptest', 'suppress',
65 'listext subscribe');
66 map { $suppressed{$_} = 1; } split(/\s+/, $supp);
67
68 $binary = "$basedir/src/imaptest";
69 $testdir = "$basedir/src/tests";
70 }
71 init;
72
73 sub new
74 {
75 my $class = shift;
76
77 my $config = Cassandane::Config->default()->clone();
78 $config->set(servername => "127.0.0.1"); # urlauth needs matching servername
79 $config->set(virtdomains => 'userid');
80 $config->set(unixhierarchysep => 'on');
81 $config->set(altnamespace => 'yes');
82
83 return $class->SUPER::new({ config => $config }, @_);
84
85 }
86
87 sub set_up
88 {
89 my ($self) = @_;
90 $self->SUPER::set_up();
91
92 $self->{instance}->create_user('user2', subdirs => ['imaptest']);
93 }
94
95 sub tear_down
96 {
97 my ($self) = @_;
98 $self->SUPER::tear_down();
99 }
100
101 sub list_tests
102 {
103 my @tests;
104
105 if (!defined $basedir)
106 {
107 return ( 'test_warning_imaptest_is_not_installed' );
108 }
109
110 opendir TESTS, $testdir
111 or die "Cannot open directory $testdir: $!";
112 while (my $e = readdir TESTS)
113 {
114 next if $e =~ m/^\./;
115 next if $e =~ m/\.mbox$/;
116 next if $suppressed{$e};
117 next if ( ! -f "$testdir/$e" );
118 push(@tests, "test_$e");
119 }
120 closedir TESTS;
121
122 return @tests;
123 }
124
125 sub run_test
126 {
127 my ($self) = @_;
128
129 if (!defined $basedir)
130 {
131 xlog $self, "ImapTests are not enabled. To enabled them, please";
132 xlog $self, "install ImapTest from http://www.imapwiki.org/ImapTest/";
133 xlog $self, "and edit [imaptest]basedir in cassandane.ini";
134 xlog $self, "This is not a failure";
135 return;
136 }
137
138 my $name = $self->name();
139 $name =~ s/^test_//;
140
141 my $logdir = "$self->{instance}->{basedir}/rawlog/";
142 mkdir($logdir);
143
144 my $svc = $self->{instance}->get_service('imap');
145 my $params = $svc->store_params();
146
147 my $errfile = $self->{instance}->{basedir} . "/$name.errors";
148 my $status;
149 $self->{instance}->run_command({
150 redirects => { stderr => $errfile },
151 workingdir => $logdir,
152 handlers => {
153 exited_normally => sub { $status = 1; },
154 exited_abnormally => sub { $status = 0; },
155 },
156 },
157 $binary,
158 "host=" . $params->{host},
159 "port=" . $params->{port},
160 "user=" . $params->{username},
161 "user2=" . "user2",
162 "pass=" . $params->{password},
163 "rawlog",
164 "test=$testdir/$name");
165
166 if ((!$status || get_verbose)) {
167 if (-f $errfile) {
168 open FH, '<', $errfile
169 or die "Cannot open $errfile for reading: $!";
170 while (readline FH) {
171 xlog $self, $_;
172 }
173 close FH;
174 }
175 opendir(DH, $logdir) or die "Can't open logdir $logdir";
176 while (my $item = readdir(DH)) {
177 next unless $item =~ m/^rawlog\./;
178 print "============> $item <=============\n";
179 open(FH, '<', "$logdir/$item") or die "Can't open $logdir/$item";
180 while (readline FH) {
181 print $_;
182 }
183 close(FH);
184 }
185 }
186
187 $self->assert($status);
188 }
189
190 1;