Debian Codegraph @

debian/tests/daemon

raw
1 #!/usr/bin/perl
2 # vim: sw=4
3
4 use v5.14;
5 use utf8;
6 use warnings;
7 use autodie qw(:all);
8
9 use Path::Tiny;
10 use Net::NNTP;
11 use POSIX qw(strftime);
12
13 ##############################################################################
14 sub create_instance {
15 my $t = path('/tmp/news/');
16 my $e = $t->child('etc/');
17
18 $ENV{INNCONF} = "$e/inn.conf";
19
20 $t->remove_tree;
21 $t->child($_)->mkdir foreach qw(
22 etc/
23 spool/ spool/incoming/ spool/overview/
24 lib/ lib/http/ lib/log/ run/
25 );
26
27 path($ENV{INNCONF})->spew(<< "END");
28 mta: /bin/true
29 pathhost: inn2.packages.debian.org
30 domain: autopkgtest.debian.org
31 server: localhost
32 patharticles: $t/spool/articles
33 pathbin: /usr/lib/news/bin
34 pathdb: $t/lib
35 pathetc: $e
36 pathnews: $t/lib
37 pathrun: $t/run
38 pathspool: $t/spool
39 enableoverview: true
40 ovmethod: tradindexed
41 hismethod: hisv6
42 nnrpdloadlimit: 0
43 END
44
45 $e->child('storage.conf')->spew(<< 'END');
46 method tradspool {
47 newsgroups: *
48 class: 0
49 }
50 END
51
52 $e->child('incoming.conf')->spew(<< 'END');
53 peer ME { hostname: "127.0.0.1, ::1" }
54 END
55
56 $e->child('newsfeeds')->spew(<< 'END');
57 ME:!*::
58 END
59
60 $e->child('readers.conf')->spew(<< 'END');
61 auth "localhost" {
62 hosts: "localhost, 127.0.0.1, ::1, stdin"
63 default: "<localhost>"
64 }
65
66 access "localhost" {
67 users: "<localhost>"
68 newsgroups: "*"
69 access: RPA
70 addcanlockuser: none
71 }
72 END
73
74 $e->child('inn-secrets.conf')->touch;
75
76 $t->child('spool/tradspool.map')->touch;
77
78 $t->child('lib/active')->spew(<< 'END');
79 control 0000000000 0000000001 n
80 control.cancel 0000000000 0000000001 n
81 junk 0000000000 0000000001 y
82 END
83
84 $t->child('lib/history')->touch;
85
86 ##############################################################################
87 system(qw(chown news: -R), $t);
88 say "\nRun makehistory...";
89 system('/usr/lib/news/bin/makehistory');
90 system(qw(chown news: -R), $t);
91
92 ##############################################################################
93 say "\nStart innd...";
94
95 my $child = fork();
96 if (not $child) {
97 system(qw(
98 systemd-run --uid=9 --gid=9 --pipe --wait --collect -u testinn
99 ),
100 'systemd-socket-activate',
101 -l => 1119,
102 -E => "INNCONF=$ENV{INNCONF}",
103 '/usr/lib/news/bin/innd', '-f',
104 );
105 exit;
106 }
107
108 }
109
110 ##############################################################################
111 if (not -d '/run/systemd/system/') {
112 warn "IGNORED: systemd is not active!\n";
113 exit 77;
114 }
115
116 my $rc = eval { system(qw(systemd-run --version)) };
117
118 if (not defined $rc or $rc) {
119 warn "IGNORED: systemd-run is not available!\n";
120 exit 77;
121 }
122
123 create_instance();
124
125 $SIG{__DIE__} = sub {
126 print "\n\n";
127 eval { system(qw(systemctl status --no-pager --lines=5 --full testinn.service)); };
128 print "\n\n";
129 };
130
131 my $nntp;
132 my $attempts = 5;
133
134 while ($attempts-- > 0) {
135 say "\nConnect to innd...";
136 $nntp = Net::NNTP->new(
137 Host => 'localhost',
138 Port => 1119,
139 Timeout => 5,
140 Reader => 0,
141 );
142 last if $nntp;
143 sleep 2;
144 }
145
146 die 'FAILED: could not connect to innd' if not $nntp;
147
148 my $help = $nntp->help;
149 die 'FAILED: could not get help' if not $help or not @$help;
150
151 say "\nRun ctlinnd mode...";
152 system(qw(ctlinnd mode));
153
154 post_ihave($nntp);
155 post_nntp($nntp);
156 post_rnews();
157 post_inews();
158 test_reader($nntp);
159
160 $nntp->quit;
161
162 say "\nShut down innd...";
163 system(qw(ctlinnd shutdown), 'end of testing');
164
165 say "\nStop the testinn.service unit...";
166 eval { system(qw(systemctl stop testinn.service)); };
167
168 say "\nExiting $0.";
169 exit;
170
171 ##############################################################################
172 sub date822 {
173 return strftime("%a, %d %b %Y %H:%M:%S %z", localtime(time()));
174 }
175
176 ##############################################################################
177 sub post_ihave {
178 my ($nntp) = @_;
179 say "\nPost an article with NNTP IHAVE...";
180
181 my $id = "<test.ihave.$$\@inn2.packages.debian.org>";
182 my $date = date822();
183 $nntp->ihave($id, << "END") or die 'FAILURE: cannot post: ' . $nntp->message;
184 Path: not-for-mail
185 From: Marco d'Itri <md\@linux.it>
186 Newsgroups: junk
187 Subject: NNTP ihave test
188 Date: $date
189 Message-ID: $id
190
191 test nntp ihave
192 END
193 return;
194 }
195
196 ##############################################################################
197 sub post_nntp {
198 my ($nntp) = @_;
199
200 say "\nPost an article with NNTP POST...";
201 $nntp->reader or die 'FAILURE: cannot switch to reader mode: ' . $nntp->message;
202 $nntp->postok or die 'FAILURE: not OK to post: ' . $nntp->message;
203 $nntp->post([
204 "From: Marco d'Itri <md\@linux.it>\n",
205 "Newsgroups: junk\n",
206 "Subject: NNTP post test\n",
207 "\n",
208 "test nntp post\n",
209 ]) or die 'FAILURE: cannot post: ' . $nntp->message;
210 return;
211 }
212
213 ##############################################################################
214 sub post_rnews {
215 say "\nPost an article with rnews...";
216
217 my $id = "<test.rnews.$$\@inn2.packages.debian.org>";
218 my $date = date822();
219 my $article = << "END";
220 Path: not-for-mail
221 From: Marco d'Itri <md\@linux.it>
222 Newsgroups: junk
223 Subject: rnews test
224 Date: $date
225 Message-ID: $id
226
227 test rnews
228 END
229 open(my $fh, '|-', '/usr/bin/rnews -N -v');
230 print $fh $article;
231 close($fh);
232 }
233
234 ##############################################################################
235 sub post_inews {
236 say "\nPost an article with inews...";
237
238 my $id = "<test.inews.$$\@inn2.packages.debian.org>";
239 my $article = << "END";
240 From: Marco d'Itri <md\@linux.it>
241 Newsgroups: junk
242 Subject: inews test
243 Message-ID: $id
244
245 test inews
246 END
247 open(my $fh, '|-', '/usr/bin/inews -p 1119 -h');
248 print $fh $article;
249 close($fh);
250 }
251
252 ##############################################################################
253 sub test_reader {
254 my ($nntp) = @_;
255
256 $nntp->reader;
257
258 $nntp->group('junk');
259 my ($id, $article, $article_id);
260
261 $id = $nntp->next;
262 die 'FAILURE: no ID after next' if not $id;
263 $article = $nntp->article($id);
264 die "FAILURE: no article for $id" if not $article;
265 foreach (@$article) {
266 next if not /^Message-ID: (.+)/;
267 $article_id = $1;
268 last;
269 }
270 die "FAILURE: $id != $article_id" if not $id eq $article_id;
271
272 $id = 4;
273 $article = $nntp->article($id);
274 die "FAILURE: no article for $id" if not $article;
275 die "FAILURE: wrong article for $id"
276 if not grep { /Marco d'Itri/ } @$article;
277 return;
278 }
279