mu/src/tests/test-mu-maildir.c

335 lines
7.9 KiB
C
Raw Normal View History

2010-01-27 07:15:08 +01:00
/*
2010-09-04 13:37:19 +02:00
** Copyright (C) 2008-2010 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
2010-01-27 07:15:08 +01:00
**
** This program is free software; you can redistribute it and/or modify it
** under the terms of the GNU General Public License as published by the
** Free Software Foundation; either version 3, or (at your option) any
** later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software Foundation,
** Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /*HAVE_CONFIG_H*/
#include <glib.h>
2010-01-31 11:15:00 +01:00
#include <glib/gstdio.h>
2010-01-27 07:15:08 +01:00
#include <stdlib.h>
#include <unistd.h>
2010-01-28 21:21:57 +01:00
#include <string.h>
#include "test-mu-common.h"
2010-01-27 07:15:08 +01:00
#include "src/mu-maildir.h"
static void
test_mu_maildir_mkmdir_01 (void)
{
int i;
gchar *tmpdir, *mdir, *tmp;
const gchar *subs[] = {"tmp", "cur", "new"};
tmpdir = test_mu_common_get_random_tmpdir ();
2010-01-27 07:15:08 +01:00
mdir = g_strdup_printf ("%s%c%s", tmpdir, G_DIR_SEPARATOR,
"cuux");
g_assert_cmpuint (mu_maildir_mkdir (mdir, 0755, FALSE, NULL),
2010-01-27 07:15:08 +01:00
==, TRUE);
for (i = 0; i != G_N_ELEMENTS(subs); ++i) {
gchar* dir;
dir = g_strdup_printf ("%s%c%s", mdir, G_DIR_SEPARATOR,
subs[i]);
g_assert_cmpuint (g_access (dir, R_OK), ==, 0);
g_assert_cmpuint (g_access (dir, W_OK), ==, 0);
g_free (dir);
}
tmp = g_strdup_printf ("%s%c%s", mdir, G_DIR_SEPARATOR, ".noindex");
g_assert_cmpuint (g_access (tmp, F_OK), !=, 0);
g_free (tmp);
g_free (tmpdir);
g_free (mdir);
}
static void
test_mu_maildir_mkmdir_02 (void)
{
int i;
gchar *tmpdir, *mdir, *tmp;
const gchar *subs[] = {"tmp", "cur", "new"};
tmpdir = test_mu_common_get_random_tmpdir ();
2010-01-27 07:15:08 +01:00
mdir = g_strdup_printf ("%s%c%s", tmpdir, G_DIR_SEPARATOR,
"cuux");
g_assert_cmpuint (mu_maildir_mkdir (mdir, 0755, TRUE, NULL),
2010-01-27 07:15:08 +01:00
==, TRUE);
for (i = 0; i != G_N_ELEMENTS(subs); ++i) {
gchar* dir;
dir = g_strdup_printf ("%s%c%s", mdir, G_DIR_SEPARATOR,
subs[i]);
g_assert_cmpuint (g_access (dir, R_OK), ==, 0);
g_assert_cmpuint (g_access (dir, W_OK), ==, 0);
g_free (dir);
}
tmp = g_strdup_printf ("%s%c%s", mdir, G_DIR_SEPARATOR, ".noindex");
g_assert_cmpuint (g_access (tmp, F_OK), ==, 0);
g_free (tmp);
g_free (tmpdir);
g_free (mdir);
}
static gboolean
ignore_error (const char* log_domain, GLogLevelFlags log_level, const gchar* msg,
gpointer user_data)
{
return FALSE; /* don't abort */
}
static void
test_mu_maildir_mkmdir_03 (void)
{
/* this must fail */
g_test_log_set_fatal_handler ((GTestLogFatalFunc)ignore_error, NULL);
g_assert_cmpuint (mu_maildir_mkdir (NULL, 0755, TRUE, NULL),
2010-01-27 07:15:08 +01:00
==, FALSE);
}
2010-01-28 21:21:57 +01:00
static gchar*
copy_test_data (void)
{
gchar *dir, *cmd;
dir = test_mu_common_get_random_tmpdir();
2010-01-28 21:21:57 +01:00
cmd = g_strdup_printf ("mkdir %s", dir);
g_assert (g_spawn_command_line_sync (cmd, NULL, NULL, NULL, NULL));
g_free (cmd);
cmd = g_strdup_printf ("cp -R %s %s", MU_TESTMAILDIR, dir);
g_assert (g_spawn_command_line_sync (cmd, NULL, NULL, NULL, NULL));
g_free (cmd);
/* unbreak make distcheck */
cmd = g_strdup_printf ("chmod -R 700 %s", dir);
2010-01-28 21:21:57 +01:00
g_assert (g_spawn_command_line_sync (cmd, NULL, NULL, NULL, NULL));
g_free (cmd);
return dir;
}
typedef struct {
int _file_count;
int _dir_entered;
int _dir_left;
} WalkData;
static MuError
2010-01-28 21:21:57 +01:00
dir_cb (const char *fullpath, gboolean enter, WalkData *data)
{
if (enter)
2010-01-28 21:21:57 +01:00
++data->_dir_entered;
else
++data->_dir_left;
/* g_print ("%s: %s: %s\n", __FUNCTION__, enter ? "entering" : "leaving", */
/* fullpath); */
2010-01-28 21:21:57 +01:00
return MU_OK;
}
static MuError
msg_cb (const char *fullpath, const char* mdir, struct stat *statinfo,
2010-02-08 20:25:53 +01:00
WalkData *data)
2010-01-28 21:21:57 +01:00
{
++data->_file_count;
return MU_OK;
}
static void
test_mu_maildir_walk_01 (void)
{
char *tmpdir;
WalkData data;
MuError rv;
2010-01-28 21:21:57 +01:00
tmpdir = copy_test_data ();
memset (&data, 0, sizeof(WalkData));
/* g_print ("tmpdir: %s\n", tmpdir); */
2010-01-28 21:21:57 +01:00
rv = mu_maildir_walk (tmpdir,
(MuMaildirWalkMsgCallback)msg_cb,
(MuMaildirWalkDirCallback)dir_cb,
&data);
g_assert_cmpuint (MU_OK, ==, rv);
2011-06-30 22:43:08 +02:00
g_assert_cmpuint (data._file_count, ==, 13);
2010-01-28 21:21:57 +01:00
g_assert_cmpuint (data._dir_entered,==, 5);
g_assert_cmpuint (data._dir_left,==, 5);
g_free (tmpdir);
}
static void
test_mu_maildir_walk_02 (void)
{
char *tmpdir, *cmd;
WalkData data;
MuError rv;
2010-01-28 21:21:57 +01:00
tmpdir = copy_test_data ();
memset (&data, 0, sizeof(WalkData));
2010-01-28 21:21:57 +01:00
/* mark the 'new' dir with '.noindex', to ignore it */
cmd = g_strdup_printf ("touch %s%ctestdir%cnew%c.noindex", tmpdir,
G_DIR_SEPARATOR, G_DIR_SEPARATOR,
G_DIR_SEPARATOR);
g_assert (g_spawn_command_line_sync (cmd, NULL, NULL, NULL, NULL));
g_free (cmd);
rv = mu_maildir_walk (tmpdir,
(MuMaildirWalkMsgCallback)msg_cb,
(MuMaildirWalkDirCallback)dir_cb,
&data);
g_assert_cmpuint (MU_OK, ==, rv);
2011-06-30 22:43:08 +02:00
g_assert_cmpuint (data._file_count, ==, 9);
2010-01-28 21:21:57 +01:00
g_assert_cmpuint (data._dir_entered,==, 4);
g_assert_cmpuint (data._dir_left,==, 4);
g_free (tmpdir);
}
2010-01-27 07:15:08 +01:00
static void
test_mu_maildir_get_flags_from_path (void)
{
int i;
struct {
const char *path;
2011-08-11 21:44:23 +02:00
MuFlags flags;
} paths[] = {
{
"/home/foo/Maildir/test/cur/123456:2,FSR",
2011-08-11 21:44:23 +02:00
MU_FLAG_REPLIED | MU_FLAG_SEEN | MU_FLAG_FLAGGED}, {
"/home/foo/Maildir/test/new/123456",
2011-08-11 21:44:23 +02:00
MU_FLAG_NEW | MU_FLAG_UNREAD}, {
"/home/foo/Maildir/test/new/123456:2,FR",
2011-08-11 21:44:23 +02:00
MU_FLAG_NEW | MU_FLAG_UNREAD}, {
"/home/foo/Maildir/test/cur/123456:2,DTP",
2011-08-11 21:44:23 +02:00
MU_FLAG_DRAFT | MU_FLAG_TRASHED |
MU_FLAG_PASSED | MU_FLAG_UNREAD }, {
"/home/foo/Maildir/test/cur/123456:2,S",
2011-08-11 21:44:23 +02:00
MU_FLAG_SEEN}
};
for (i = 0; i != G_N_ELEMENTS(paths); ++i) {
2011-08-11 21:44:23 +02:00
MuFlags flags;
flags = mu_maildir_get_flags_from_path(paths[i].path);
g_assert_cmpuint(flags, ==, paths[i].flags);
}
}
static void
test_mu_maildir_get_path_from_flags (void)
{
int i;
struct {
const char *oldpath;
2011-08-11 21:44:23 +02:00
MuFlags flags;
const char *newpath;
} paths[] = {
{
"/home/foo/Maildir/test/cur/123456:2,FR",
2011-08-11 21:44:23 +02:00
MU_FLAG_REPLIED,
"/home/foo/Maildir/test/cur/123456:2,R"
}, {
"/home/foo/Maildir/test/cur/123456:2,FR",
2011-08-11 21:44:23 +02:00
MU_FLAG_NEW,
"/home/foo/Maildir/test/new/123456:2,"
}, {
"/home/foo/Maildir/test/new/123456:2,FR",
2011-08-11 21:44:23 +02:00
MU_FLAG_SEEN | MU_FLAG_REPLIED,
"/home/foo/Maildir/test/cur/123456:2,RS"
}, {
"/home/foo/Maildir/test/new/1313038887_0.697:2,",
2011-08-11 21:44:23 +02:00
MU_FLAG_SEEN | MU_FLAG_FLAGGED | MU_FLAG_PASSED,
"/home/foo/Maildir/test/cur/1313038887_0.697:2,FPS"
2011-08-11 21:44:23 +02:00
}, {
"/home/djcb/Maildir/trash/new/1312920597.2206_16.cthulhu",
2011-08-11 21:44:23 +02:00
MU_FLAG_SEEN,
"/home/djcb/Maildir/trash/cur/1312920597.2206_16.cthulhu:2,S"
}
};
for (i = 0; i != G_N_ELEMENTS(paths); ++i) {
gchar *str;
str = mu_maildir_get_path_from_flags(paths[i].oldpath,
paths[i].flags);
g_assert_cmpstr(str, ==, paths[i].newpath);
g_free(str);
}
}
2010-01-27 07:15:08 +01:00
int
main (int argc, char *argv[])
{
g_test_init (&argc, &argv, NULL);
2010-01-28 21:21:57 +01:00
/* mu_util_maildir_mkmdir */
2010-01-27 07:15:08 +01:00
g_test_add_func ("/mu-maildir/mu-maildir-mkmdir-01",
test_mu_maildir_mkmdir_01);
g_test_add_func ("/mu-maildir/mu-maildir-mkmdir-02",
test_mu_maildir_mkmdir_02);
g_test_add_func ("/mu-maildir/mu-maildir-mkmdir-03",
test_mu_maildir_mkmdir_03);
2010-01-28 21:21:57 +01:00
/* mu_util_maildir_walk */
g_test_add_func ("/mu-maildir/mu-maildir-walk-01",
test_mu_maildir_walk_01);
g_test_add_func ("/mu-maildir/mu-maildir-walk-02",
test_mu_maildir_walk_02);
/* get/set flags */
g_test_add_func("/mu-maildir/mu-maildir-get-path-from-flags",
test_mu_maildir_get_path_from_flags);
g_test_add_func("/mu-maildir/mu-maildir-get-flags-from-path",
test_mu_maildir_get_flags_from_path);
2010-01-28 21:21:57 +01:00
2010-01-27 07:15:08 +01:00
g_log_set_handler (NULL,
G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL| G_LOG_FLAG_RECURSION,
2010-09-26 14:23:17 +02:00
(GLogFunc)black_hole, NULL);
2010-01-27 07:15:08 +01:00
return g_test_run ();
}