Debian Codegraph @

cassandane/Cassandane/Cyrus/CtlMboxlist.pm

raw
1 #!/usr/bin/perl
2 #
3 # Copyright (c) 2011-2022 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::CtlMboxlist;
41 use strict;
42 use warnings;
43
44 use Data::Dumper;
45
46 use lib '.';
47 use base qw(Cassandane::Cyrus::TestCase);
48 use Cassandane::Util::Log;
49 use Cassandane::Util::Slurp;
50 use Cassandane::Instance;
51
52 sub new
53 {
54 my $class = shift;
55 return $class->SUPER::new({}, @_);
56 }
57
58 sub set_up
59 {
60 my ($self) = @_;
61 $self->SUPER::set_up();
62 }
63
64 sub tear_down
65 {
66 my ($self) = @_;
67 $self->SUPER::tear_down();
68 }
69
70 sub fudge_mtimes
71 {
72 my ($hash) = @_;
73
74 foreach my $v (values %{$hash}) {
75 if (exists $v->{mtime}) {
76 $v->{mtime} = 1;
77 }
78 }
79 }
80
81 sub test_dump_undump
82 :needs_component_sieve :AltNamespace :UnixHierarchySep
83 {
84 my ($self) = @_;
85
86 # set up some mailboxes for the cassandane user
87 my $expected = $self->populate_user(
88 $self->{instance},
89 $self->{store},
90 [qw(INBOX Drafts Big Big/Red Big/Red/Dog)]
91 );
92
93 # sanity check
94 $self->check_user($self->{instance}, $self->{store}, $expected);
95
96 # stop the instance
97 $self->{store}->disconnect();
98 $self->{instance}->stop();
99 $self->{instance}->{re_use_dir} = 1;
100
101 # will refer to this a lot
102 my $basedir = $self->{instance}->get_basedir();
103
104 # get a dump
105 my $dump1file = "$basedir/$$-dump.out1";
106 my $dump1content = $self->{instance}->read_mailboxes_db({
107 outfile => $dump1file
108 });
109
110 # move aside original mailboxes.db
111 my $mboxlist_db_path = $self->{instance}->{config}->get('mboxlist_db_path');
112 $mboxlist_db_path //= "$basedir/conf/mailboxes.db";
113 rename $mboxlist_db_path, "$mboxlist_db_path.orig"
114 or die "rename $mboxlist_db_path $mboxlist_db_path.orig: $!";
115
116 # undump the dump into a new mailboxes.db
117 my $errfile = $basedir . "/$$-undump.err";
118 $self->{instance}->run_command({
119 cyrus => 1,
120 redirects => {
121 stdin => $dump1file,
122 stderr => $errfile,
123 },
124 }, 'ctl_mboxlist', '-u');
125
126 my $errors = slurp_file($errfile);
127
128 # should be no errors reported by the undump
129 $self->assert_str_equals(q{}, $errors);
130
131 # start the instance back up and reconnect the store
132 $self->{instance}->start();
133 $self->{store}->connect();
134
135 # user's mailboxes should be as they were
136 $self->check_user($self->{instance}, $self->{store}, $expected);
137
138 # a second dump should produce the same output
139 # ... though mtimes will differ, so fudge those first
140 my $dump2file = "$basedir/$$-dump.out2";
141 my $dump2content = $self->{instance}->read_mailboxes_db({
142 outfile => $dump2file
143 });
144 fudge_mtimes($dump1content);
145 fudge_mtimes($dump2content);
146 $self->assert_deep_equals($dump1content, $dump2content);
147 }
148
149 1;