run-tests.pl 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  1. #! /usr/bin/perl -w
  2. #
  3. # Copyright (C) 2006 Oswald Buddenhagen <ossi@users.sf.net>
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. #
  18. use strict;
  19. use File::Path;
  20. -d "tmp" or mkdir "tmp";
  21. chdir "tmp" or die "Cannot enter temp direcory.\n";
  22. sub show($$@);
  23. sub test($$$);
  24. ################################################################################
  25. # generic syncing tests
  26. my @x01 = (
  27. [ 8,
  28. 1, 1, "F", 2, 2, "", 3, 3, "FS", 4, 4, "", 5, 5, "T", 6, 6, "F", 7, 7, "FT", 9, 0, "" ],
  29. [ 8,
  30. 1, 1, "", 2, 2, "F", 3, 3, "F", 4, 4, "", 5, 5, "", 7, 7, "", 8, 8, "", 10, 0, "" ],
  31. [ 8, 0, 0,
  32. 1, 1, "", 2, 2, "", 3, 3, "", 4, 4, "", 5, 5, "", 6, 6, "", 7, 7, "", 8, 8, "" ],
  33. );
  34. #show("01", "01", "", "", "");
  35. my @X01 = (
  36. [ "", "", "" ],
  37. [ 10,
  38. 1, 1, "F", 2, 2, "F", 3, 3, "FS", 4, 4, "", 5, 5, "T", 6, 6, "FT", 7, 7, "FT", 9, 9, "", 10, 10, "" ],
  39. [ 10,
  40. 1, 1, "F", 2, 2, "F", 3, 3, "FS", 4, 4, "", 5, 5, "T", 7, 7, "FT", 8, 8, "T", 9, 10, "", 10, 9, "" ],
  41. [ 9, 0, 9,
  42. 1, 1, "F", 2, 2, "F", 3, 3, "FS", 4, 4, "", 5, 5, "T", 6, 0, "", 7, 7, "FT", 0, 8, "", 10, 9, "", 9, 10, "" ],
  43. );
  44. test("full", \@x01, \@X01);
  45. #show("01", "02", "", "", "Expunge Both\n");
  46. my @X02 = (
  47. [ "", "", "Expunge Both\n" ],
  48. [ 10,
  49. 1, 1, "F", 2, 2, "F", 3, 3, "FS", 4, 4, "", 9, 9, "", 10, 10, "" ],
  50. [ 10,
  51. 1, 1, "F", 2, 2, "F", 3, 3, "FS", 4, 4, "", 9, 10, "", 10, 9, "" ],
  52. [ 9, 0, 9,
  53. 1, 1, "F", 2, 2, "F", 3, 3, "FS", 4, 4, "", 10, 9, "", 9, 10, "" ],
  54. );
  55. test("full + expunge both", \@x01, \@X02);
  56. #show("01", "03", "", "", "Expunge Slave\n");
  57. my @X03 = (
  58. [ "", "", "Expunge Slave\n" ],
  59. [ 10,
  60. 1, 1, "F", 2, 2, "F", 3, 3, "FS", 4, 4, "", 5, 5, "T", 6, 6, "FT", 7, 7, "FT", 9, 9, "", 10, 10, "" ],
  61. [ 10,
  62. 1, 1, "F", 2, 2, "F", 3, 3, "FS", 4, 4, "", 9, 10, "", 10, 9, "" ],
  63. [ 9, 0, 9,
  64. 1, 1, "F", 2, 2, "F", 3, 3, "FS", 4, 4, "", 5, 0, "T", 6, 0, "", 7, 0, "T", 10, 9, "", 9, 10, "" ],
  65. );
  66. test("full + expunge slave", \@x01, \@X03);
  67. #show("01", "04", "", "", "Sync Pull\n");
  68. my @X04 = (
  69. [ "", "", "Sync Pull\n" ],
  70. [ 9,
  71. 1, 1, "F", 2, 2, "", 3, 3, "FS", 4, 4, "", 5, 5, "T", 6, 6, "F", 7, 7, "FT", 9, 9, "" ],
  72. [ 9,
  73. 1, 1, "F", 2, 2, "F", 3, 3, "FS", 4, 4, "", 5, 5, "T", 7, 7, "FT", 8, 8, "T", 9, 9, "", 10, 0, "" ],
  74. [ 9, 0, 0,
  75. 1, 1, "F", 2, 2, "", 3, 3, "FS", 4, 4, "", 5, 5, "T", 6, 6, "", 7, 7, "FT", 0, 8, "", 9, 9, "" ],
  76. );
  77. test("pull", \@x01, \@X04);
  78. #show("01", "05", "", "", "Sync Flags\n");
  79. my @X05 = (
  80. [ "", "", "Sync Flags\n" ],
  81. [ 8,
  82. 1, 1, "F", 2, 2, "F", 3, 3, "FS", 4, 4, "", 5, 5, "T", 6, 6, "F", 7, 7, "FT", 9, 0, "" ],
  83. [ 8,
  84. 1, 1, "F", 2, 2, "F", 3, 3, "FS", 4, 4, "", 5, 5, "T", 7, 7, "FT", 8, 8, "", 10, 0, "" ],
  85. [ 8, 0, 0,
  86. 1, 1, "F", 2, 2, "F", 3, 3, "FS", 4, 4, "", 5, 5, "T", 6, 6, "", 7, 7, "FT", 8, 8, "" ],
  87. );
  88. test("flags", \@x01, \@X05);
  89. #show("01", "06", "", "", "Sync Delete\n");
  90. my @X06 = (
  91. [ "", "", "Sync Delete\n" ],
  92. [ 8,
  93. 1, 1, "F", 2, 2, "", 3, 3, "FS", 4, 4, "", 5, 5, "T", 6, 6, "FT", 7, 7, "FT", 9, 0, "" ],
  94. [ 8,
  95. 1, 1, "", 2, 2, "F", 3, 3, "F", 4, 4, "", 5, 5, "", 7, 7, "", 8, 8, "T", 10, 0, "" ],
  96. [ 8, 0, 0,
  97. 1, 1, "", 2, 2, "", 3, 3, "", 4, 4, "", 5, 5, "", 6, 0, "", 7, 7, "", 0, 8, "" ],
  98. );
  99. test("deletions", \@x01, \@X06);
  100. #show("01", "07", "", "", "Sync New\n");
  101. my @X07 = (
  102. [ "", "", "Sync New\n" ],
  103. [ 10,
  104. 1, 1, "F", 2, 2, "", 3, 3, "FS", 4, 4, "", 5, 5, "T", 6, 6, "F", 7, 7, "FT", 9, 9, "", 10, 10, "" ],
  105. [ 10,
  106. 1, 1, "", 2, 2, "F", 3, 3, "F", 4, 4, "", 5, 5, "", 7, 7, "", 8, 8, "", 9, 10, "", 10, 9, "" ],
  107. [ 9, 0, 9,
  108. 1, 1, "", 2, 2, "", 3, 3, "", 4, 4, "", 5, 5, "", 6, 6, "", 7, 7, "", 8, 8, "", 10, 9, "", 9, 10, "" ],
  109. );
  110. test("new", \@x01, \@X07);
  111. #show("01", "08", "", "", "Sync PushFlags PullDelete\n");
  112. my @X08 = (
  113. [ "", "", "Sync PushFlags PullDelete\n" ],
  114. [ 8,
  115. 1, 1, "F", 2, 2, "F", 3, 3, "FS", 4, 4, "", 5, 5, "T", 6, 6, "F", 7, 7, "FT", 9, 0, "" ],
  116. [ 8,
  117. 1, 1, "", 2, 2, "F", 3, 3, "F", 4, 4, "", 5, 5, "", 7, 7, "", 8, 8, "T", 10, 0, "" ],
  118. [ 8, 0, 0,
  119. 1, 1, "", 2, 2, "F", 3, 3, "F", 4, 4, "", 5, 5, "", 6, 6, "", 7, 7, "", 0, 8, "" ],
  120. );
  121. test("push flags + pull deletions", \@x01, \@X08);
  122. # size restriction tests
  123. my @x10 = (
  124. [ 0,
  125. 1, 0, "", 2, 0, "*" ],
  126. [ 0,
  127. 3, 0, "*" ],
  128. [ 0, 0, 0,
  129. ],
  130. );
  131. #show("10", "11", "MaxSize 1k\n", "MaxSize 1k\n", "");
  132. my @X11 = (
  133. [ "MaxSize 1k\n", "MaxSize 1k\n", "" ],
  134. [ 2,
  135. 1, 1, "", 2, 2, "*" ],
  136. [ 2,
  137. 3, 1, "*", 1, 2, "" ],
  138. [ 2, 0, 1,
  139. -1, 1, "", 1, 2, "", 2, -1, "" ],
  140. );
  141. test("max size", \@x10, \@X11);
  142. my @x20 = @X11[1,2,3];
  143. #show("20", "11", "MaxSize 1k\n", "MaxSize 1k\n", ""); # sic! - 11
  144. test("max size verification", \@x20, \@X11);
  145. #show("20", "22", "", "MaxSize 1k\n", "");
  146. my @X22 = (
  147. [ "", "MaxSize 1k\n", "" ],
  148. [ 3,
  149. 1, 1, "", 2, 2, "*", 3, 3, "*" ],
  150. [ 2,
  151. 3, 1, "*", 1, 2, "" ],
  152. [ 2, 0, 1,
  153. 3, 1, "", 1, 2, "", 2, -1, "" ],
  154. );
  155. test("slave max size", \@x20, \@X22);
  156. # expiration tests
  157. my @x30 = (
  158. [ 0,
  159. 1, 0, "F", 2, 0, "S", 3, 0, "S", 4, 0, "", 5, 0, "" ],
  160. [ 0,
  161. ],
  162. [ 0, 0, 0,
  163. ],
  164. );
  165. #show("30", "31", "", "", "MaxMessages 3\n");
  166. my @X31 = (
  167. [ "", "", "MaxMessages 3\n" ],
  168. [ 5,
  169. 1, 1, "F", 2, 2, "S", 3, 3, "S", 4, 4, "", 5, 5, "" ],
  170. [ 5,
  171. 1, 1, "F", 2, 2, "S", 3, 3, "S", 4, 4, "", 5, 5, "" ],
  172. [ 5, 0, 0,
  173. 1, 1, "F", 2, 2, "S", 3, 3, "S", 4, 4, "", 5, 5, "" ],
  174. );
  175. test("max messages", \@x30, \@X31);
  176. my @x40 = @X31[1,2,3];
  177. #show("40", "41", "", "", "MaxMessages 3\nExpunge Both\n");
  178. my @X41 = (
  179. [ "", "", "MaxMessages 3\nExpunge Both\n" ],
  180. [ 5,
  181. 1, 1, "F", 2, 2, "S", 3, 3, "S", 4, 4, "", 5, 5, "" ],
  182. [ 5,
  183. 1, 1, "F", 3, 3, "S", 4, 4, "", 5, 5, "" ],
  184. [ 5, 2, 0,
  185. 1, 1, "F", 3, 3, "S", 4, 4, "", 5, 5, "" ],
  186. );
  187. test("max messages catch-up", \@x40, \@X41);
  188. my @x50 = (
  189. [ 5,
  190. 1, 1, "FS", 2, 2, "FS", 3, 3, "", 4, 4, "", 5, 5, "" ],
  191. [ 5,
  192. 1, 1, "S", 2, 2, "ST", 3, 3, "", 4, 4, "", 5, 5, "" ],
  193. [ 5, 2, 0,
  194. 1, 1, "FS", 2, 2, "XS", 3, 3, "", 4, 4, "", 5, 5, "" ],
  195. );
  196. #show("50", "51", "", "", "MaxMessages 3\nExpunge Both\n");
  197. my @X51 = (
  198. [ "", "", "MaxMessages 3\nExpunge Both\n" ],
  199. [ 5,
  200. 1, 1, "S", 2, 2, "FS", 3, 3, "", 4, 4, "", 5, 5, "" ],
  201. [ 5,
  202. 2, 2, "FS", 3, 3, "", 4, 4, "", 5, 5, "" ],
  203. [ 5, 2, 0,
  204. 2, 2, "FS", 3, 3, "", 4, 4, "", 5, 5, "" ],
  205. );
  206. test("max messages + expire", \@x50, \@X51);
  207. ################################################################################
  208. chdir "..";
  209. rmdir "tmp";
  210. print "OK.\n";
  211. exit 0;
  212. sub qm($)
  213. {
  214. shift;
  215. s/\\/\\\\/g;
  216. s/\"/\\"/g;
  217. s/\"/\\"/g;
  218. s/\n/\\n/g;
  219. return $_;
  220. }
  221. # $master, $slave, $channel
  222. sub writecfg($$$)
  223. {
  224. open(FILE, ">", ".mbsyncrc") or
  225. die "Cannot open .mbsyncrc.\n";
  226. print FILE
  227. "FSync None
  228. MaildirStore master
  229. Path ./
  230. Inbox ./master
  231. ".shift()."
  232. MaildirStore slave
  233. Path ./
  234. Inbox ./slave
  235. ".shift()."
  236. Channel test
  237. Master :master:
  238. Slave :slave:
  239. SyncState *
  240. ".shift();
  241. close FILE;
  242. }
  243. sub killcfg()
  244. {
  245. unlink ".mbsyncrc";
  246. }
  247. # $options
  248. sub runsync($)
  249. {
  250. # open FILE, "valgrind -q --log-fd=3 ../mbsync ".shift()." -c .mbsyncrc test 3>&2 2>&1 |";
  251. open FILE, "../mbsync -D -Z ".shift()." -c .mbsyncrc test 2>&1 |";
  252. my @out = <FILE>;
  253. close FILE or push(@out, $! ? "*** error closing mbsync: $!\n" : "*** mbsync exited with signal ".($?&127).", code ".($?>>8)."\n");
  254. return $?, @out;
  255. }
  256. # $path
  257. sub readbox($)
  258. {
  259. my $bn = shift;
  260. (-d $bn) or
  261. die "No mailbox '$bn'.\n";
  262. (-d $bn."/tmp" and -d $bn."/new" and -d $bn."/cur") or
  263. die "Invalid mailbox '$bn'.\n";
  264. open(FILE, "<", $bn."/.uidvalidity") or die "Cannot read UID validity of mailbox '$bn'.\n";
  265. my $dummy = <FILE>;
  266. chomp(my $mu = <FILE>);
  267. close FILE;
  268. my %ms = ();
  269. for my $d ("cur", "new") {
  270. opendir(DIR, $bn."/".$d) or next;
  271. for my $f (grep(!/^\.\.?$/, readdir(DIR))) {
  272. my ($uid, $flg, $num);
  273. if ($f =~ /^\d+\.\d+_\d+\.[-[:alnum:]]+,U=(\d+):2,(.*)$/) {
  274. ($uid, $flg) = ($1, $2);
  275. } elsif ($f =~ /^\d+\.\d+_(\d+)\.[-[:alnum:]]+:2,(.*)$/) {
  276. ($uid, $flg) = (0, $2);
  277. } else {
  278. print STDERR "unrecognided file name '$f' in '$bn'.\n";
  279. exit 1;
  280. }
  281. open(FILE, "<", $bn."/".$d."/".$f) or die "Cannot read message '$f' in '$bn'.\n";
  282. my $sz = 0;
  283. while (<FILE>) {
  284. /^Subject: (\d+)$/ && ($num = $1);
  285. $sz += length($_);
  286. }
  287. close FILE;
  288. if (!defined($num)) {
  289. print STDERR "message '$f' in '$bn' has no identifier.\n";
  290. exit 1;
  291. }
  292. @{ $ms{$num} } = ($uid, $flg.($sz>1000?"*":""));
  293. }
  294. }
  295. return ($mu, %ms);
  296. }
  297. # $boxname
  298. # Output:
  299. # [ maxuid,
  300. # serial, uid, "flags", ... ],
  301. sub showbox($)
  302. {
  303. my ($bn) = @_;
  304. my ($mu, %ms) = readbox($bn);
  305. print " [ $mu,\n ";
  306. my $frst = 1;
  307. for my $num (sort { $a <=> $b } keys %ms) {
  308. if ($frst) {
  309. $frst = 0;
  310. } else {
  311. print ", ";
  312. }
  313. print "$num, $ms{$num}[0], \"$ms{$num}[1]\"";
  314. }
  315. print " ],\n";
  316. }
  317. # $filename
  318. # Output:
  319. # [ maxuid[M], smaxxuid, maxuid[S],
  320. # uid[M], uid[S], "flags", ... ],
  321. sub showstate($)
  322. {
  323. my ($fn) = @_;
  324. if (!open(FILE, "<", $fn)) {
  325. print STDERR " Cannot read sync state $fn: $!\n";
  326. return;
  327. }
  328. $_ = <FILE>;
  329. if (!defined $_) {
  330. print STDERR " Missing sync state header.\n";
  331. close FILE;
  332. return;
  333. }
  334. if (!/^1:(\d+) 1:(\d+):(\d+)\n$/) {
  335. chomp;
  336. print STDERR " Malformed sync state header '$_'.\n";
  337. close FILE;
  338. return;
  339. }
  340. print " [ $1, $2, $3,\n ";
  341. my $frst = 1;
  342. for (<FILE>) {
  343. if ($frst) {
  344. $frst = 0;
  345. } else {
  346. print ", ";
  347. }
  348. if (!/^(-?\d+) (-?\d+) (.*)\n$/) {
  349. print "??, ??, \"??\"";
  350. } else {
  351. print "$1, $2, \"$3\"";
  352. }
  353. }
  354. print " ],\n";
  355. close FILE;
  356. }
  357. # $filename
  358. sub showchan($)
  359. {
  360. my ($fn) = @_;
  361. showbox("master");
  362. showbox("slave");
  363. showstate($fn);
  364. }
  365. # $source_state_name, $target_state_name, $master_configs, $slave_configs, $channel_configs
  366. sub show($$@)
  367. {
  368. my ($sx, $tx, @sfx) = @_;
  369. my @sp;
  370. eval "\@sp = \@x$sx";
  371. mkchan($sp[0], $sp[1], @{ $sp[2] });
  372. print "my \@x$sx = (\n";
  373. showchan("slave/.mbsyncstate");
  374. print ");\n";
  375. &writecfg(@sfx);
  376. runsync("");
  377. killcfg();
  378. print "my \@X$tx = (\n";
  379. print " [ ".join(", ", map('"'.qm($_).'"', @sfx))." ],\n";
  380. showchan("slave/.mbsyncstate");
  381. print ");\n";
  382. print "test(\\\@x$sx, \\\@X$tx);\n\n";
  383. rmtree "slave";
  384. rmtree "master";
  385. }
  386. # $boxname, $maxuid, @msgs
  387. sub mkbox($$@)
  388. {
  389. my ($bn, $mu, @ms) = @_;
  390. rmtree($bn);
  391. (mkdir($bn) and mkdir($bn."/tmp") and mkdir($bn."/new") and mkdir($bn."/cur")) or
  392. die "Cannot create mailbox $bn.\n";
  393. open(FILE, ">", $bn."/.uidvalidity") or die "Cannot create UID validity for mailbox $bn.\n";
  394. print FILE "1\n$mu\n";
  395. close FILE;
  396. while (@ms) {
  397. my ($num, $uid, $flg) = (shift @ms, shift @ms, shift @ms);
  398. if ($uid) {
  399. $uid = ",U=".$uid;
  400. } else {
  401. $uid = "";
  402. }
  403. my $big = $flg =~ s/\*//;
  404. open(FILE, ">", $bn."/".($flg =~ /S/ ? "cur" : "new")."/0.1_".$num.".local".$uid.":2,".$flg) or
  405. die "Cannot create message $num in mailbox $bn.\n";
  406. print FILE "From: foo\nTo: bar\nDate: Thu, 1 Jan 1970 00:00:00 +0000\nSubject: $num\n\n".(("A"x50)."\n")x($big*30);
  407. close FILE;
  408. }
  409. }
  410. # \@master, \@slave, @syncstate
  411. sub mkchan($$@)
  412. {
  413. my ($m, $s, @t) = @_;
  414. &mkbox("master", @{ $m });
  415. &mkbox("slave", @{ $s });
  416. open(FILE, ">", "slave/.mbsyncstate") or
  417. die "Cannot create sync state.\n";
  418. print FILE "1:".shift(@t)." 1:".shift(@t).":".shift(@t)."\n";
  419. while (@t) {
  420. print FILE shift(@t)." ".shift(@t)." ".shift(@t)."\n";
  421. }
  422. close FILE;
  423. }
  424. # $config, $boxname, $maxuid, @msgs
  425. sub ckbox($$$@)
  426. {
  427. my ($bn, $MU, @MS) = @_;
  428. my ($mu, %ms) = readbox($bn);
  429. if ($mu != $MU) {
  430. print STDERR "MAXUID mismatch for '$bn'.\n";
  431. return 1;
  432. }
  433. while (@MS) {
  434. my ($num, $uid, $flg) = (shift @MS, shift @MS, shift @MS);
  435. if (!defined $ms{$num}) {
  436. print STDERR "No message $bn:$num.\n";
  437. return 1;
  438. }
  439. if ($ms{$num}[0] ne $uid) {
  440. print STDERR "UID mismatch for $bn:$num.\n";
  441. return 1;
  442. }
  443. if ($ms{$num}[1] ne $flg) {
  444. print STDERR "Flag mismatch for $bn:$num.\n";
  445. return 1;
  446. }
  447. delete $ms{$num};
  448. }
  449. if (%ms) {
  450. print STDERR "Excess messages in '$bn': ".join(", ", sort({$a <=> $b } keys(%ms))).".\n";
  451. return 1;
  452. }
  453. return 0;
  454. }
  455. # $filename, @syncstate
  456. sub ckstate($@)
  457. {
  458. my ($fn, @T) = @_;
  459. open(FILE, "<", $fn) or die "Cannot read sync state $fn.\n";
  460. my $l = <FILE>;
  461. chomp(my @ls = <FILE>);
  462. close FILE;
  463. if (!defined $l) {
  464. print STDERR "Sync state header missing.\n";
  465. return 1;
  466. }
  467. chomp($l);
  468. my $xl = "1:".shift(@T)." 1:".shift(@T).":".shift(@T);
  469. if ($l ne $xl) {
  470. print STDERR "Sync state header mismatch: '$l' instead of '$xl'.\n";
  471. return 1;
  472. } else {
  473. for $l (@ls) {
  474. $xl = shift(@T)." ".shift(@T)." ".shift(@T);
  475. if ($l ne $xl) {
  476. print STDERR "Sync state entry mismatch: '$l' instead of '$xl'.\n";
  477. return 1;
  478. }
  479. }
  480. }
  481. return 0;
  482. }
  483. # \@master, \@slave, @syncstate
  484. sub ckchan($$@)
  485. {
  486. my ($M, $S, @T) = @_;
  487. my $rslt = ckstate("slave/.mbsyncstate.new", @T);
  488. $rslt |= &ckbox("master", @{ $M });
  489. $rslt |= &ckbox("slave", @{ $S });
  490. return $rslt;
  491. }
  492. sub printbox($$@)
  493. {
  494. my ($bn, $mu, @ms) = @_;
  495. print " [ $mu,\n ";
  496. my $frst = 1;
  497. while (@ms) {
  498. if ($frst) {
  499. $frst = 0;
  500. } else {
  501. print ", ";
  502. }
  503. print shift(@ms).", ".shift(@ms).", \"".shift(@ms)."\"";
  504. }
  505. print " ],\n";
  506. }
  507. # @syncstate
  508. sub printstate(@)
  509. {
  510. my (@t) = @_;
  511. print " [ ".shift(@t).", ".shift(@t).", ".shift(@t).",\n ";
  512. my $frst = 1;
  513. while (@t) {
  514. if ($frst) {
  515. $frst = 0;
  516. } else {
  517. print ", ";
  518. }
  519. print shift(@t).", ".shift(@t).", \"".shift(@t)."\"";
  520. }
  521. print " ],\n";
  522. close FILE;
  523. }
  524. # \@master, \@slave, @syncstate
  525. sub printchan($$@)
  526. {
  527. my ($m, $s, @t) = @_;
  528. &printbox("master", @{ $m });
  529. &printbox("slave", @{ $s });
  530. printstate(@t);
  531. }
  532. # $title, \@source_state, \@target_state
  533. sub test($$$)
  534. {
  535. my ($ttl, $sx, $tx) = @_;
  536. print "Testing: ".$ttl." ...\n";
  537. mkchan($$sx[0], $$sx[1], @{ $$sx[2] });
  538. &writecfg(@{ $$tx[0] });
  539. my ($xc, @ret) = runsync("-J");
  540. if ($xc) {
  541. print "Input:\n";
  542. printchan($$sx[0], $$sx[1], @{ $$sx[2] });
  543. print "Options:\n";
  544. print " [ ".join(", ", map('"'.qm($_).'"', @{ $$tx[0] }))." ]\n";
  545. print "Expected result:\n";
  546. printchan($$tx[1], $$tx[2], @{ $$tx[3] });
  547. print "Debug output:\n";
  548. print @ret;
  549. exit 1;
  550. }
  551. if (ckchan($$tx[1], $$tx[2], @{ $$tx[3] })) {
  552. print "Input:\n";
  553. printchan($$sx[0], $$sx[1], @{ $$sx[2] });
  554. print "Options:\n";
  555. print " [ ".join(", ", map('"'.qm($_).'"', @{ $$tx[0] }))." ]\n";
  556. print "Expected result:\n";
  557. printchan($$tx[1], $$tx[2], @{ $$tx[3] });
  558. print "Actual result:\n";
  559. showchan("slave/.mbsyncstate.new");
  560. print "Debug output:\n";
  561. print @ret;
  562. exit 1;
  563. }
  564. open(FILE, "<", "slave/.mbsyncstate.journal") or
  565. die "Cannot read journal.\n";
  566. my @nj = <FILE>;
  567. close FILE;
  568. ($xc, @ret) = runsync("-0 --no-expunge");
  569. killcfg();
  570. if ($xc) {
  571. print "Journal replay failed.\n";
  572. print "Input == Expected result:\n";
  573. printchan($$tx[1], $$tx[2], @{ $$tx[3] });
  574. print "Options:\n";
  575. print " [ ".join(", ", map('"'.qm($_).'"', @{ $$tx[0] }))." ], [ \"-0\", \"--no-expunge\" ]\n";
  576. print "Debug output:\n";
  577. print @ret;
  578. exit 1;
  579. }
  580. if (ckstate("slave/.mbsyncstate", @{ $$tx[3] })) {
  581. print "Journal replay failed.\n";
  582. print "Options:\n";
  583. print " [ ".join(", ", map('"'.qm($_).'"', @{ $$tx[0] }))." ]\n";
  584. print "Old State:\n";
  585. printstate(@{ $$sx[2] });
  586. print "Journal:\n".join("", @nj)."\n";
  587. print "Expected New State:\n";
  588. printstate(@{ $$tx[3] });
  589. print "New State:\n";
  590. showstate("slave/.mbsyncstate");
  591. print "Debug output:\n";
  592. print @ret;
  593. exit 1;
  594. }
  595. rmtree "slave";
  596. rmtree "master";
  597. }