bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2009-2018 Dovecot authors, see the included COPYING file */
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen#include "lib.h"
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen#include "array.h"
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen#include "test-common.h"
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen#include "mail-index-private.h"
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen#include "mail-index-modseq.h"
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen#include "mail-index-transaction-private.h"
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainenstatic struct mail_index_record recs[20];
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainenstatic uint64_t modseqs[N_ELEMENTS(recs)];
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainenbool mail_index_map_get_ext_idx(struct mail_index_map *map ATTR_UNUSED,
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen uint32_t ext_id ATTR_UNUSED,
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen uint32_t *idx_r ATTR_UNUSED) { return FALSE; }
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainenvoid mail_index_ext_set_reset_id(struct mail_index_transaction *t ATTR_UNUSED,
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen uint32_t ext_id ATTR_UNUSED,
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen uint32_t reset_id ATTR_UNUSED) { }
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainenvoid mail_index_transaction_set_log_updates(struct mail_index_transaction *t ATTR_UNUSED) { }
d97f939b1c09e9f90b01b6f81bfb1c05da990148Timo Sirainenvoid mail_index_update_day_headers(struct mail_index_transaction *t ATTR_UNUSED, time_t day_stamp ATTR_UNUSED) {}
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainenbool mail_index_cancel_flag_updates(struct mail_index_transaction *t ATTR_UNUSED,
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen uint32_t seq ATTR_UNUSED) { return TRUE; }
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainenbool mail_index_cancel_keyword_updates(struct mail_index_transaction *t ATTR_UNUSED,
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen uint32_t seq ATTR_UNUSED) { return TRUE; }
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainenvoid mail_index_transaction_sort_appends(struct mail_index_transaction *t ATTR_UNUSED) {}
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainenint mail_index_map(struct mail_index *index ATTR_UNUSED,
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen enum mail_index_sync_handler_type type ATTR_UNUSED) { return 1; }
4b89231f4ec9cc69f4aea715e1d34f405c7e317dTimo Sirainenvoid mail_index_update_modseq(struct mail_index_transaction *t ATTR_UNUSED, uint32_t seq ATTR_UNUSED,
4b89231f4ec9cc69f4aea715e1d34f405c7e317dTimo Sirainen uint64_t min_modseq ATTR_UNUSED) {}
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainenconst struct mail_index_record *
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainenmail_index_lookup(struct mail_index_view *view ATTR_UNUSED, uint32_t seq)
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen{
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen i_assert(seq < N_ELEMENTS(recs));
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen return &recs[seq];
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen}
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainenstruct mail_index_record *
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainenmail_index_transaction_lookup(struct mail_index_transaction *t ATTR_UNUSED,
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen uint32_t seq)
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen{
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen i_assert(seq < N_ELEMENTS(recs));
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen return &recs[seq];
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen}
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainenuint64_t mail_index_modseq_lookup(struct mail_index_view *view ATTR_UNUSED,
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen uint32_t seq)
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen{
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen i_assert(seq < N_ELEMENTS(modseqs));
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen return modseqs[seq];
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen}
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainenuint64_t mail_index_modseq_get_highest(struct mail_index_view *view ATTR_UNUSED)
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen{
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen return modseqs[0];
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen}
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen
a42d489d99bcf7d18cfd0352fdf0c9348cb224fcTimo Sirainen#define MAIL_INDEX_TRANSACTION_FINISH(t, n_so_far) \
a42d489d99bcf7d18cfd0352fdf0c9348cb224fcTimo Sirainen for (unsigned int sofar = 0; sofar < n_so_far; sofar++) \
a42d489d99bcf7d18cfd0352fdf0c9348cb224fcTimo Sirainen mail_index_transaction_finish_so_far(t); \
a42d489d99bcf7d18cfd0352fdf0c9348cb224fcTimo Sirainen mail_index_transaction_finish(t);
a42d489d99bcf7d18cfd0352fdf0c9348cb224fcTimo Sirainen
a42d489d99bcf7d18cfd0352fdf0c9348cb224fcTimo Sirainenstatic void
a42d489d99bcf7d18cfd0352fdf0c9348cb224fcTimo Sirainentest_mail_index_transaction_finish_flag_updates(unsigned int n_so_far)
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen{
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen struct mail_index_transaction *t;
5da1aa5197a43d83f0fb3eeb83125c7cd73d1b62Timo Sirainen const struct mail_index_flag_update *updates;
5da1aa5197a43d83f0fb3eeb83125c7cd73d1b62Timo Sirainen struct mail_index_flag_update u;
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen unsigned int count;
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen t = t_new(struct mail_index_transaction, 1);
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen t->drop_unnecessary_flag_updates = TRUE;
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen
efe78d3ba24fc866af1c79b9223dc0809ba26cadStephan Bosch i_zero(&u);
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen u.add_flags = MAIL_SEEN; u.remove_flags = MAIL_DRAFT;
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen
a42d489d99bcf7d18cfd0352fdf0c9348cb224fcTimo Sirainen test_begin(t_strdup_printf("mail index transaction finish flag updates n_so_far=%u", n_so_far));
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen /* test fast path: all changed */
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen t_array_init(&t->updates, 10);
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen u.uid1 = 1; u.uid2 = 2;
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen array_append(&t->updates, &u, 1);
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen u.uid1 = 4; u.uid2 = 5;
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen array_append(&t->updates, &u, 1);
a42d489d99bcf7d18cfd0352fdf0c9348cb224fcTimo Sirainen MAIL_INDEX_TRANSACTION_FINISH(t, n_so_far);
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen updates = array_get(&t->updates, &count);
bddd52cb7f3e5a894c080f60750aa76b5aeaf103Timo Sirainen test_assert(count == 4);
bddd52cb7f3e5a894c080f60750aa76b5aeaf103Timo Sirainen test_assert(updates[0].uid1 == 1*2 && updates[0].uid2 == 1*2);
bddd52cb7f3e5a894c080f60750aa76b5aeaf103Timo Sirainen test_assert(updates[1].uid1 == 2*2 && updates[1].uid2 == 2*2);
bddd52cb7f3e5a894c080f60750aa76b5aeaf103Timo Sirainen test_assert(updates[2].uid1 == 4*2 && updates[2].uid2 == 4*2);
bddd52cb7f3e5a894c080f60750aa76b5aeaf103Timo Sirainen test_assert(updates[3].uid1 == 5*2 && updates[3].uid2 == 5*2);
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen /* nothing changed */
bddd52cb7f3e5a894c080f60750aa76b5aeaf103Timo Sirainen t_array_init(&t->updates, 10);
bddd52cb7f3e5a894c080f60750aa76b5aeaf103Timo Sirainen u.uid1 = 1; u.uid2 = 2;
bddd52cb7f3e5a894c080f60750aa76b5aeaf103Timo Sirainen array_append(&t->updates, &u, 1);
bddd52cb7f3e5a894c080f60750aa76b5aeaf103Timo Sirainen u.uid1 = 4; u.uid2 = 5;
bddd52cb7f3e5a894c080f60750aa76b5aeaf103Timo Sirainen array_append(&t->updates, &u, 1);
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen recs[1].flags = MAIL_SEEN;
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen recs[2].flags = MAIL_SEEN;
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen recs[4].flags = MAIL_SEEN;
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen recs[5].flags = MAIL_SEEN;
a42d489d99bcf7d18cfd0352fdf0c9348cb224fcTimo Sirainen MAIL_INDEX_TRANSACTION_FINISH(t, n_so_far);
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen test_assert(!array_is_created(&t->updates));
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen /* some changes */
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen t_array_init(&t->updates, 10);
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen u.uid1 = 2; u.uid2 = 3;
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen array_append(&t->updates, &u, 1);
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen u.uid1 = 5; u.uid2 = 6;
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen array_append(&t->updates, &u, 1);
a42d489d99bcf7d18cfd0352fdf0c9348cb224fcTimo Sirainen MAIL_INDEX_TRANSACTION_FINISH(t, n_so_far);
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen updates = array_get(&t->updates, &count);
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen test_assert(count == 2);
bddd52cb7f3e5a894c080f60750aa76b5aeaf103Timo Sirainen test_assert(updates[0].uid1 == 3*2 && updates[0].uid2 == 3*2);
bddd52cb7f3e5a894c080f60750aa76b5aeaf103Timo Sirainen test_assert(updates[1].uid1 == 6*2 && updates[1].uid2 == 6*2);
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen test_end();
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen}
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen
a42d489d99bcf7d18cfd0352fdf0c9348cb224fcTimo Sirainenstatic void
a42d489d99bcf7d18cfd0352fdf0c9348cb224fcTimo Sirainentest_mail_index_transaction_finish_check_conflicts(unsigned int n_so_far)
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen{
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen struct mail_index_transaction *t;
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen const struct seq_range *conflicts;
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen ARRAY_TYPE(seq_range) conflict_seqs = ARRAY_INIT;
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen unsigned int count;
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen t = t_new(struct mail_index_transaction, 1);
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen t->view = t_new(struct mail_index_view, 1);
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen t->min_flagupdate_seq = 5;
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen t->max_flagupdate_seq = 8;
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen t->conflict_seqs = &conflict_seqs;
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen modseqs[0] = 1234;
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen modseqs[5] = 5;
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen modseqs[6] = 8;
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen modseqs[7] = 6;
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen modseqs[8] = 7;
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen
a42d489d99bcf7d18cfd0352fdf0c9348cb224fcTimo Sirainen test_begin(t_strdup_printf("mail index transaction finish check conflicts n_so_far=%u", n_so_far));
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen /* fast path: no conflicts */
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen t->max_modseq = 1234;
a42d489d99bcf7d18cfd0352fdf0c9348cb224fcTimo Sirainen MAIL_INDEX_TRANSACTION_FINISH(t, n_so_far);
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen test_assert(!array_is_created(&conflict_seqs));
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen /* try some conflicts */
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen t->max_modseq = 6;
a42d489d99bcf7d18cfd0352fdf0c9348cb224fcTimo Sirainen MAIL_INDEX_TRANSACTION_FINISH(t, n_so_far);
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen
108bb19d23b091ce2a025660a1d2ffcdb90a2d19Aki Tuomi i_assert(array_is_created(&conflict_seqs));
108bb19d23b091ce2a025660a1d2ffcdb90a2d19Aki Tuomi
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen conflicts = array_get(&conflict_seqs, &count);
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen test_assert(count == 2);
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen test_assert(conflicts[0].seq1 == 6 && conflicts[0].seq2 == 6);
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen test_assert(conflicts[1].seq1 == 8 && conflicts[1].seq2 == 8);
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen test_end();
8fcf84e6b7a55049a6d407b17af50d6ae8c6386fPhil Carmody array_free(t->conflict_seqs);
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen}
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen
a42d489d99bcf7d18cfd0352fdf0c9348cb224fcTimo Sirainenstatic void
a42d489d99bcf7d18cfd0352fdf0c9348cb224fcTimo Sirainentest_mail_index_transaction_finish_modseq_updates(unsigned int n_so_far)
ad48319996942463675b53877092ab7e13a7a75aTimo Sirainen{
ad48319996942463675b53877092ab7e13a7a75aTimo Sirainen struct mail_index_transaction *t;
ad48319996942463675b53877092ab7e13a7a75aTimo Sirainen const struct mail_transaction_modseq_update *ups;
ad48319996942463675b53877092ab7e13a7a75aTimo Sirainen struct mail_transaction_modseq_update u;
ad48319996942463675b53877092ab7e13a7a75aTimo Sirainen unsigned int count;
ad48319996942463675b53877092ab7e13a7a75aTimo Sirainen
ad48319996942463675b53877092ab7e13a7a75aTimo Sirainen t = t_new(struct mail_index_transaction, 1);
ad48319996942463675b53877092ab7e13a7a75aTimo Sirainen
a42d489d99bcf7d18cfd0352fdf0c9348cb224fcTimo Sirainen test_begin(t_strdup_printf("mail index transaction finish modseq updates n_so_far=%u", n_so_far));
ad48319996942463675b53877092ab7e13a7a75aTimo Sirainen
ad48319996942463675b53877092ab7e13a7a75aTimo Sirainen t_array_init(&t->modseq_updates, 10);
ad48319996942463675b53877092ab7e13a7a75aTimo Sirainen u.modseq_low32 = 1234567890;
ad48319996942463675b53877092ab7e13a7a75aTimo Sirainen u.modseq_high32 = 987654321;
ad48319996942463675b53877092ab7e13a7a75aTimo Sirainen u.uid = 1; array_append(&t->modseq_updates, &u, 1);
ad48319996942463675b53877092ab7e13a7a75aTimo Sirainen u.modseq_low32++;
ad48319996942463675b53877092ab7e13a7a75aTimo Sirainen u.modseq_high32++;
ad48319996942463675b53877092ab7e13a7a75aTimo Sirainen u.uid = 2; array_append(&t->modseq_updates, &u, 1);
ad48319996942463675b53877092ab7e13a7a75aTimo Sirainen u.modseq_low32++;
ad48319996942463675b53877092ab7e13a7a75aTimo Sirainen u.modseq_high32++;
ad48319996942463675b53877092ab7e13a7a75aTimo Sirainen u.uid = 5; array_append(&t->modseq_updates, &u, 1);
ad48319996942463675b53877092ab7e13a7a75aTimo Sirainen u.modseq_low32 = 1234;
ad48319996942463675b53877092ab7e13a7a75aTimo Sirainen u.modseq_high32 = 0;
ad48319996942463675b53877092ab7e13a7a75aTimo Sirainen u.uid = 2; array_append(&t->modseq_updates, &u, 1);
ad48319996942463675b53877092ab7e13a7a75aTimo Sirainen
a42d489d99bcf7d18cfd0352fdf0c9348cb224fcTimo Sirainen MAIL_INDEX_TRANSACTION_FINISH(t, n_so_far);
ad48319996942463675b53877092ab7e13a7a75aTimo Sirainen
ad48319996942463675b53877092ab7e13a7a75aTimo Sirainen ups = array_get(&t->modseq_updates, &count);
ad48319996942463675b53877092ab7e13a7a75aTimo Sirainen test_assert(count == 4);
ad48319996942463675b53877092ab7e13a7a75aTimo Sirainen
ad48319996942463675b53877092ab7e13a7a75aTimo Sirainen test_assert(ups[0].uid == 1*2);
ad48319996942463675b53877092ab7e13a7a75aTimo Sirainen test_assert(ups[0].modseq_low32 == 1234567890 &&
ad48319996942463675b53877092ab7e13a7a75aTimo Sirainen ups[0].modseq_high32 == 987654321);
ad48319996942463675b53877092ab7e13a7a75aTimo Sirainen test_assert(ups[1].uid == 2*2);
ad48319996942463675b53877092ab7e13a7a75aTimo Sirainen test_assert(ups[1].modseq_low32 == 1234567891 &&
ad48319996942463675b53877092ab7e13a7a75aTimo Sirainen ups[1].modseq_high32 == 987654322);
ad48319996942463675b53877092ab7e13a7a75aTimo Sirainen test_assert(ups[2].uid == 5*2);
ad48319996942463675b53877092ab7e13a7a75aTimo Sirainen test_assert(ups[2].modseq_low32 == 1234567892 &&
ad48319996942463675b53877092ab7e13a7a75aTimo Sirainen ups[2].modseq_high32 == 987654323);
ad48319996942463675b53877092ab7e13a7a75aTimo Sirainen test_assert(ups[3].uid == 2*2);
ad48319996942463675b53877092ab7e13a7a75aTimo Sirainen test_assert(ups[3].modseq_low32 == 1234 &&
ad48319996942463675b53877092ab7e13a7a75aTimo Sirainen ups[3].modseq_high32 == 0);
ad48319996942463675b53877092ab7e13a7a75aTimo Sirainen test_end();
ad48319996942463675b53877092ab7e13a7a75aTimo Sirainen}
ad48319996942463675b53877092ab7e13a7a75aTimo Sirainen
a42d489d99bcf7d18cfd0352fdf0c9348cb224fcTimo Sirainenstatic void
a42d489d99bcf7d18cfd0352fdf0c9348cb224fcTimo Sirainentest_mail_index_transaction_finish_expunges(unsigned int n_so_far)
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen{
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen struct mail_index_transaction *t;
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen guid_128_t guid1, guid2, guid3;
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen const struct mail_transaction_expunge_guid *expunges;
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen struct mail_transaction_expunge_guid expunge;
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen unsigned int i, count;
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen for (i = 0; i < sizeof(guid2); i++) {
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen guid1[i] = i + 1;
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen guid2[i] = i ^ 0xff;
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen guid3[i] = i + 0x80;
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen }
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen recs[1].uid = 12;
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen recs[2].uid = 15;
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen recs[3].uid = 18;
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen t = t_new(struct mail_index_transaction, 1);
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen t->expunges_nonsorted = TRUE;
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen
a42d489d99bcf7d18cfd0352fdf0c9348cb224fcTimo Sirainen test_begin(t_strdup_printf("mail index transaction finish expunges n_so_far=%u", n_so_far));
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen t_array_init(&t->expunges, 3);
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen expunge.uid = 2;
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen memcpy(expunge.guid_128, guid2, sizeof(expunge.guid_128));
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen array_append(&t->expunges, &expunge, 1);
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen array_append(&t->expunges, &expunge, 1);
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen expunge.uid = 1;
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen memcpy(expunge.guid_128, guid1, sizeof(expunge.guid_128));
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen array_append(&t->expunges, &expunge, 1);
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen array_append(&t->expunges, &expunge, 1);
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen expunge.uid = 3;
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen memcpy(expunge.guid_128, guid3, sizeof(expunge.guid_128));
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen array_append(&t->expunges, &expunge, 1);
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen array_append(&t->expunges, &expunge, 1);
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen
a42d489d99bcf7d18cfd0352fdf0c9348cb224fcTimo Sirainen MAIL_INDEX_TRANSACTION_FINISH(t, n_so_far);
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen expunges = array_get(&t->expunges, &count);
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen test_assert(count == 3);
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen test_assert(expunges[0].uid == 12);
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen test_assert(memcmp(expunges[0].guid_128, guid1, sizeof(guid1)) == 0);
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen test_assert(expunges[1].uid == 15);
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen test_assert(memcmp(expunges[1].guid_128, guid2, sizeof(guid2)) == 0);
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen test_assert(expunges[2].uid == 18);
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen test_assert(memcmp(expunges[2].guid_128, guid3, sizeof(guid3)) == 0);
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen test_end();
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen}
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen
a42d489d99bcf7d18cfd0352fdf0c9348cb224fcTimo Sirainenstatic void test_state_reset(void)
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen{
a42d489d99bcf7d18cfd0352fdf0c9348cb224fcTimo Sirainen memset(recs, 0, sizeof(recs));
a42d489d99bcf7d18cfd0352fdf0c9348cb224fcTimo Sirainen memset(modseqs, 0, sizeof(modseqs));
a42d489d99bcf7d18cfd0352fdf0c9348cb224fcTimo Sirainen for (unsigned int n = 1; n < N_ELEMENTS(recs); n++)
a42d489d99bcf7d18cfd0352fdf0c9348cb224fcTimo Sirainen recs[n].uid = n*2;
a42d489d99bcf7d18cfd0352fdf0c9348cb224fcTimo Sirainen}
a42d489d99bcf7d18cfd0352fdf0c9348cb224fcTimo Sirainen
a42d489d99bcf7d18cfd0352fdf0c9348cb224fcTimo Sirainenstatic void test_mail_index_transaction_finish(void)
a42d489d99bcf7d18cfd0352fdf0c9348cb224fcTimo Sirainen{
a42d489d99bcf7d18cfd0352fdf0c9348cb224fcTimo Sirainen void (*const test_finish_functions[])(unsigned int) = {
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen test_mail_index_transaction_finish_flag_updates,
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen test_mail_index_transaction_finish_check_conflicts,
ad48319996942463675b53877092ab7e13a7a75aTimo Sirainen test_mail_index_transaction_finish_modseq_updates,
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen test_mail_index_transaction_finish_expunges,
a42d489d99bcf7d18cfd0352fdf0c9348cb224fcTimo Sirainen };
a42d489d99bcf7d18cfd0352fdf0c9348cb224fcTimo Sirainen unsigned int i, j;
a42d489d99bcf7d18cfd0352fdf0c9348cb224fcTimo Sirainen
a42d489d99bcf7d18cfd0352fdf0c9348cb224fcTimo Sirainen for (i = 0; i < N_ELEMENTS(test_finish_functions); i++) {
a42d489d99bcf7d18cfd0352fdf0c9348cb224fcTimo Sirainen for (j = 0; j < 3; j++) {
a42d489d99bcf7d18cfd0352fdf0c9348cb224fcTimo Sirainen test_state_reset();
a42d489d99bcf7d18cfd0352fdf0c9348cb224fcTimo Sirainen test_finish_functions[i](j);
a42d489d99bcf7d18cfd0352fdf0c9348cb224fcTimo Sirainen }
a42d489d99bcf7d18cfd0352fdf0c9348cb224fcTimo Sirainen }
a42d489d99bcf7d18cfd0352fdf0c9348cb224fcTimo Sirainen}
a42d489d99bcf7d18cfd0352fdf0c9348cb224fcTimo Sirainen
a42d489d99bcf7d18cfd0352fdf0c9348cb224fcTimo Sirainenint main(void)
a42d489d99bcf7d18cfd0352fdf0c9348cb224fcTimo Sirainen{
baf3e87e186453fda13bd21f7cbcb2efc8492e8bTimo Sirainen static void (*const test_functions[])(void) = {
a42d489d99bcf7d18cfd0352fdf0c9348cb224fcTimo Sirainen test_mail_index_transaction_finish,
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen NULL
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen };
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen return test_run(test_functions);
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen}