test-mail-index-sync-ext.c revision d7c8280f58292cc292eba9209dcc9024fe96cf06
02c335c23bf5fa225a467c19f2c063fb0dc7b8c3Timo Sirainen/* Copyright (c) 2010-2012 Dovecot authors, see the included COPYING file */
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen#include "lib.h"
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen#include "array.h"
78fa3c578c14ee8a612f86cf73b6181c7f16463fTimo Sirainen#include "test-common.h"
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen#include "mail-index-sync-private.h"
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen#include "mail-index-modseq.h"
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen#include <stdlib.h>
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainenvoid mail_index_sync_set_corrupted(struct mail_index_sync_map_ctx *ctx ATTR_UNUSED,
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen const char *fmt ATTR_UNUSED, ...) {}
f7423cbbd9dea363a5df18ebb96da055a977ae79Timo Sirainenstruct mail_index_map *
2cd2518bab14292a67cf8a490b58ab9ef89879daTimo Sirainenmail_index_sync_get_atomic_map(struct mail_index_sync_map_ctx *ctx) { return ctx->view->map; }
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainenuint32_t
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainenmail_index_map_register_ext(struct mail_index_map *map ATTR_UNUSED,
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen const char *name ATTR_UNUSED, uint32_t ext_offset ATTR_UNUSED,
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen const struct mail_index_ext_header *ext_hdr ATTR_UNUSED) { return 0; }
6ec925f52d04ec8700e47cb005bd7ddc65ac5614Timo Sirainenbool mail_index_ext_lookup(struct mail_index *index ATTR_UNUSED,
c60178b2610a9b193ff72aa18448398ef72529a1Timo Sirainen const char *name ATTR_UNUSED,
4d4d6d4745682790c20d759ba93dbea46b812c5dTimo Sirainen uint32_t *ext_id_r ATTR_UNUSED) { return FALSE; }
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainenbool mail_index_map_lookup_ext(struct mail_index_map *map ATTR_UNUSED,
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen const char *name ATTR_UNUSED,
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen uint32_t *idx_r ATTR_UNUSED) { return FALSE; }
cf2e6953d03a1c22f272ec19432fc03c136ac1bbTimo Sirainenint mail_index_map_ext_hdr_check(const struct mail_index_header *hdr ATTR_UNUSED,
5d4c793b4e3dbc07f08daa4465594b1857f80725Timo Sirainen const struct mail_index_ext_header *ext_hdr ATTR_UNUSED,
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen const char *name ATTR_UNUSED,
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen const char **error_r ATTR_UNUSED) { return -1; }
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainenvoid mail_index_modseq_hdr_update(struct mail_index_modseq_sync *ctx ATTR_UNUSED) {}
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainenbool mail_index_lookup_seq(struct mail_index_view *view ATTR_UNUSED,
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen uint32_t uid, uint32_t *seq_r) {
617e13833c798435e2be425b99c27ecaad1b8393Timo Sirainen *seq_r = uid;
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen return TRUE;
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen}
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainenstatic void test_mail_index_sync_ext_atomic_inc(void)
5d4c793b4e3dbc07f08daa4465594b1857f80725Timo Sirainen{
5d4c793b4e3dbc07f08daa4465594b1857f80725Timo Sirainen struct mail_index_sync_map_ctx ctx;
5d4c793b4e3dbc07f08daa4465594b1857f80725Timo Sirainen struct mail_transaction_ext_atomic_inc u;
5d4c793b4e3dbc07f08daa4465594b1857f80725Timo Sirainen struct mail_index_ext *ext;
5d4c793b4e3dbc07f08daa4465594b1857f80725Timo Sirainen void *ptr;
5d4c793b4e3dbc07f08daa4465594b1857f80725Timo Sirainen
5d4c793b4e3dbc07f08daa4465594b1857f80725Timo Sirainen test_begin("mail index sync ext atomic inc");
5d4c793b4e3dbc07f08daa4465594b1857f80725Timo Sirainen
5d4c793b4e3dbc07f08daa4465594b1857f80725Timo Sirainen memset(&ctx, 0, sizeof(ctx));
5d4c793b4e3dbc07f08daa4465594b1857f80725Timo Sirainen ctx.view = t_new(struct mail_index_view, 1);
5d4c793b4e3dbc07f08daa4465594b1857f80725Timo Sirainen ctx.view->map = t_new(struct mail_index_map, 1);
5d4c793b4e3dbc07f08daa4465594b1857f80725Timo Sirainen ctx.view->map->hdr.next_uid = 2;
5d4c793b4e3dbc07f08daa4465594b1857f80725Timo Sirainen ctx.view->map->hdr.record_size = sizeof(struct mail_index_record) + 16;
5d4c793b4e3dbc07f08daa4465594b1857f80725Timo Sirainen ctx.view->map->rec_map = t_new(struct mail_index_record_map, 1);
5d4c793b4e3dbc07f08daa4465594b1857f80725Timo Sirainen ctx.view->map->rec_map->records =
5d4c793b4e3dbc07f08daa4465594b1857f80725Timo Sirainen t_malloc(ctx.view->map->hdr.record_size);
5d4c793b4e3dbc07f08daa4465594b1857f80725Timo Sirainen t_array_init(&ctx.view->map->extensions, 4);
5d4c793b4e3dbc07f08daa4465594b1857f80725Timo Sirainen ext = array_append_space(&ctx.view->map->extensions);
78fa3c578c14ee8a612f86cf73b6181c7f16463fTimo Sirainen ext->record_offset = sizeof(struct mail_index_record);
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainen ptr = PTR_OFFSET(ctx.view->map->rec_map->records, ext->record_offset);
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen memset(&u, 0, sizeof(u));
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen test_assert(mail_index_sync_ext_atomic_inc(&ctx, &u) == -1);
71056e0f5e1f68cb9ac002a7827eb98435c40d62Timo Sirainen
e9ac518aaf49a06d976bf7f24ab14a3e2d6d86abTimo Sirainen u.uid = 2;
71056e0f5e1f68cb9ac002a7827eb98435c40d62Timo Sirainen test_assert(mail_index_sync_ext_atomic_inc(&ctx, &u) == -1);
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen u.uid = 1;
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen#define TEST_ATOMIC(_type, _value, _diff, _ret) \
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen { _type *n = ptr; *n = _value; } \
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen ext->record_size = sizeof(_type); \
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen u.diff = _diff; \
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen test_assert(mail_index_sync_ext_atomic_inc(&ctx, &u) == _ret);
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen#define TEST_ATOMIC_BLOCK(_type, _max) \
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen TEST_ATOMIC(_type, 1, -1, 1); \
78fa3c578c14ee8a612f86cf73b6181c7f16463fTimo Sirainen TEST_ATOMIC(_type, 1, -2, -1); \
78fa3c578c14ee8a612f86cf73b6181c7f16463fTimo Sirainen TEST_ATOMIC(_type, 0, -1, -1); \
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen TEST_ATOMIC(_type, 0, _max, 1); \
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen TEST_ATOMIC(_type, 1, _max, -1); \
e9ac518aaf49a06d976bf7f24ab14a3e2d6d86abTimo Sirainen TEST_ATOMIC(_type, 0, (_max+1), -1); \
e9ac518aaf49a06d976bf7f24ab14a3e2d6d86abTimo Sirainen TEST_ATOMIC(_type, _max, 1, -1); \
78fa3c578c14ee8a612f86cf73b6181c7f16463fTimo Sirainen TEST_ATOMIC(_type, _max, -_max, 1); \
e9ac518aaf49a06d976bf7f24ab14a3e2d6d86abTimo Sirainen TEST_ATOMIC(_type, _max, -(_max+1), -1);
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen TEST_ATOMIC_BLOCK(uint8_t, 255);
230ef558135f16a66b86cbe3762524eaa9ae9d81Timo Sirainen TEST_ATOMIC_BLOCK(uint16_t, 65535);
230ef558135f16a66b86cbe3762524eaa9ae9d81Timo Sirainen
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen ext->record_size = 5;
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen u.diff = 0;
e9ac518aaf49a06d976bf7f24ab14a3e2d6d86abTimo Sirainen test_assert(mail_index_sync_ext_atomic_inc(&ctx, &u) == -1);
e9ac518aaf49a06d976bf7f24ab14a3e2d6d86abTimo Sirainen
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen test_end();
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen}
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainenint main(void)
78fa3c578c14ee8a612f86cf73b6181c7f16463fTimo Sirainen{
78fa3c578c14ee8a612f86cf73b6181c7f16463fTimo Sirainen static void (*test_functions[])(void) = {
71056e0f5e1f68cb9ac002a7827eb98435c40d62Timo Sirainen test_mail_index_sync_ext_atomic_inc,
71056e0f5e1f68cb9ac002a7827eb98435c40d62Timo Sirainen NULL
78fa3c578c14ee8a612f86cf73b6181c7f16463fTimo Sirainen };
71056e0f5e1f68cb9ac002a7827eb98435c40d62Timo Sirainen return test_run(test_functions);
71056e0f5e1f68cb9ac002a7827eb98435c40d62Timo Sirainen}
71056e0f5e1f68cb9ac002a7827eb98435c40d62Timo Sirainen