2012-01-09 13:12:28 +01:00
|
|
|
# Copyright (C) 2012- Sebastian Spaeth & contributors
|
|
|
|
#
|
|
|
|
# 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 2 of the License, 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 St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
import os
|
2012-02-17 10:12:53 +01:00
|
|
|
import re
|
2012-01-09 13:12:28 +01:00
|
|
|
import shutil
|
|
|
|
import subprocess
|
|
|
|
import tempfile
|
2012-02-24 09:39:39 +01:00
|
|
|
import random
|
2020-08-29 21:10:16 +02:00
|
|
|
import offlineimap.virtual_imaplib2 as imaplib
|
2020-08-29 20:26:29 +02:00
|
|
|
|
2012-02-24 09:39:39 +01:00
|
|
|
random.seed()
|
2012-04-03 01:34:59 +02:00
|
|
|
|
|
|
|
from offlineimap.CustomConfig import CustomConfigParser
|
2012-01-18 22:39:25 +01:00
|
|
|
from . import default_conf
|
2012-01-09 13:12:28 +01:00
|
|
|
|
2012-02-24 09:39:39 +01:00
|
|
|
|
2012-01-09 13:12:28 +01:00
|
|
|
class OLITestLib():
|
|
|
|
cred_file = None
|
|
|
|
testdir = None
|
|
|
|
"""Absolute path of the current temporary test directory"""
|
|
|
|
cmd = None
|
|
|
|
"""command that will be executed to invoke offlineimap"""
|
|
|
|
|
2020-08-29 20:26:29 +02:00
|
|
|
def __init__(self, cred_file=None, cmd='offlineimap'):
|
2012-01-09 13:12:28 +01:00
|
|
|
"""
|
|
|
|
|
|
|
|
:param cred_file: file of the configuration
|
|
|
|
snippet for authenticating against the test IMAP server(s).
|
|
|
|
:param cmd: command that will be executed to invoke offlineimap"""
|
|
|
|
OLITestLib.cred_file = cred_file
|
2012-01-09 14:38:28 +01:00
|
|
|
if not os.path.isfile(cred_file):
|
|
|
|
raise UserWarning("Please copy 'credentials.conf.sample' to '%s' "
|
2020-08-29 20:26:29 +02:00
|
|
|
"and set your credentials there." % cred_file)
|
2012-01-09 13:12:28 +01:00
|
|
|
OLITestLib.cmd = cmd
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def create_test_dir(cls, suffix=''):
|
|
|
|
"""Creates a test directory and places OLI config there
|
|
|
|
|
|
|
|
Note that this is a class method. There can only be one test
|
|
|
|
directory at a time. OLITestLib is not suited for running
|
|
|
|
several tests in parallel. The user is responsible for
|
|
|
|
cleaning that up herself."""
|
2012-02-17 10:12:53 +01:00
|
|
|
assert cls.cred_file != None
|
2012-01-18 23:12:27 +01:00
|
|
|
# creating temporary dir for testing in same dir as credentials.conf
|
2012-01-09 13:12:28 +01:00
|
|
|
cls.testdir = os.path.abspath(
|
2020-08-29 20:26:29 +02:00
|
|
|
tempfile.mkdtemp(prefix='tmp_%s_' % suffix,
|
2012-01-18 23:12:27 +01:00
|
|
|
dir=os.path.dirname(cls.cred_file)))
|
2012-02-16 09:31:14 +01:00
|
|
|
cls.write_config_file()
|
2012-01-09 13:12:28 +01:00
|
|
|
return cls.testdir
|
|
|
|
|
|
|
|
@classmethod
|
2012-02-16 09:31:14 +01:00
|
|
|
def get_default_config(cls):
|
|
|
|
"""Creates a default ConfigParser file and returns it
|
2012-01-09 13:12:28 +01:00
|
|
|
|
2012-02-16 09:31:14 +01:00
|
|
|
The returned config can be manipulated and then saved with
|
|
|
|
write_config_file()"""
|
2020-08-29 20:26:29 +02:00
|
|
|
# TODO, only do first time and cache then for subsequent calls?
|
2012-01-09 13:12:28 +01:00
|
|
|
assert cls.cred_file != None
|
|
|
|
assert cls.testdir != None
|
2012-04-03 01:34:59 +02:00
|
|
|
config = CustomConfigParser()
|
2012-01-09 13:12:28 +01:00
|
|
|
config.readfp(default_conf)
|
2020-08-29 20:26:29 +02:00
|
|
|
default_conf.seek(0) # rewind config_file to start
|
2012-01-09 13:12:28 +01:00
|
|
|
config.read(cls.cred_file)
|
|
|
|
config.set("general", "metadata", cls.testdir)
|
2012-02-16 09:31:14 +01:00
|
|
|
return config
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def write_config_file(cls, config=None):
|
|
|
|
"""Creates a OLI configuration file
|
|
|
|
|
|
|
|
It is created in testdir (so create_test_dir has to be called
|
|
|
|
earlier) using the credentials information given (so they had
|
|
|
|
to be set earlier). Failure to do either of them will raise an
|
|
|
|
AssertionException. If config is None, a default one will be
|
|
|
|
used via get_default_config, otherwise it needs to be a config
|
|
|
|
object derived from that."""
|
|
|
|
if config is None:
|
|
|
|
config = cls.get_default_config()
|
2012-01-09 13:12:28 +01:00
|
|
|
localfolders = os.path.join(cls.testdir, 'mail')
|
|
|
|
config.set("Repository Maildir", "localfolders", localfolders)
|
2012-02-17 14:57:11 +01:00
|
|
|
with open(os.path.join(cls.testdir, 'offlineimap.conf'), "wt") as f:
|
2012-01-09 13:12:28 +01:00
|
|
|
config.write(f)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def delete_test_dir(cls):
|
|
|
|
"""Deletes the current test directory
|
|
|
|
|
|
|
|
The users is responsible for cleaning that up herself."""
|
|
|
|
if os.path.isdir(cls.testdir):
|
|
|
|
shutil.rmtree(cls.testdir)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def run_OLI(cls):
|
|
|
|
"""Runs OfflineImap
|
|
|
|
|
2012-02-17 14:57:11 +01:00
|
|
|
:returns: (rescode, stdout (as unicode))
|
2012-01-09 13:12:28 +01:00
|
|
|
"""
|
|
|
|
try:
|
|
|
|
output = subprocess.check_output(
|
|
|
|
[cls.cmd,
|
|
|
|
"-c%s" % os.path.join(cls.testdir, 'offlineimap.conf')],
|
|
|
|
shell=False)
|
|
|
|
except subprocess.CalledProcessError as e:
|
2012-02-17 14:57:11 +01:00
|
|
|
return (e.returncode, e.output.decode('utf-8'))
|
|
|
|
return (0, output.decode('utf-8'))
|
2012-01-09 13:12:28 +01:00
|
|
|
|
2012-02-17 10:12:53 +01:00
|
|
|
@classmethod
|
|
|
|
def delete_remote_testfolders(cls, reponame=None):
|
|
|
|
"""Delete all INBOX.OLITEST* folders on the remote IMAP repository
|
|
|
|
|
|
|
|
reponame: All on `reponame` or all IMAP-type repositories if None"""
|
|
|
|
config = cls.get_default_config()
|
|
|
|
if reponame:
|
2012-05-18 11:31:08 +02:00
|
|
|
sections = ['Repository {0}'.format(reponame)]
|
2012-02-17 10:12:53 +01:00
|
|
|
else:
|
|
|
|
sections = [r for r in config.sections() \
|
2020-08-29 20:26:29 +02:00
|
|
|
if r.startswith('Repository')]
|
2016-05-08 16:55:13 +02:00
|
|
|
sections = [s for s in sections if config.get(s, 'Type').lower() == 'imap']
|
2012-02-17 10:12:53 +01:00
|
|
|
for sec in sections:
|
|
|
|
# Connect to each IMAP repo and delete all folders
|
|
|
|
# matching the folderfilter setting. We only allow basic
|
|
|
|
# settings and no fancy password getting here...
|
|
|
|
# 1) connect and get dir listing
|
|
|
|
host = config.get(sec, 'remotehost')
|
|
|
|
user = config.get(sec, 'remoteuser')
|
|
|
|
passwd = config.get(sec, 'remotepass')
|
|
|
|
imapobj = imaplib.IMAP4(host)
|
|
|
|
imapobj.login(user, passwd)
|
|
|
|
res_t, data = imapobj.list()
|
|
|
|
assert res_t == 'OK'
|
|
|
|
dirs = []
|
|
|
|
for d in data:
|
2015-03-23 17:48:43 +01:00
|
|
|
if d == '':
|
|
|
|
continue
|
2015-03-23 17:43:55 +01:00
|
|
|
if isinstance(d, tuple):
|
|
|
|
# literal (unquoted)
|
|
|
|
folder = b'"%s"' % d[1].replace('"', '\\"')
|
|
|
|
else:
|
|
|
|
m = re.search(br'''
|
2015-03-23 18:20:46 +01:00
|
|
|
[ ] # space
|
|
|
|
(?P<dir>
|
|
|
|
(?P<quote>"?) # starting quote
|
2015-03-23 17:43:55 +01:00
|
|
|
([^"]|\\")* # a non-quote or a backslashded quote
|
2015-03-23 18:20:46 +01:00
|
|
|
(?P=quote))$ # ending quote
|
2015-03-23 17:43:55 +01:00
|
|
|
''', d, flags=re.VERBOSE)
|
2015-03-23 18:20:46 +01:00
|
|
|
folder = bytearray(m.group('dir'))
|
|
|
|
if not m.group('quote'):
|
|
|
|
folder = '"%s"' % folder
|
2020-08-29 20:26:29 +02:00
|
|
|
# folder = folder.replace(br'\"', b'"') # remove quoting
|
2012-02-17 10:12:53 +01:00
|
|
|
dirs.append(folder)
|
|
|
|
# 2) filter out those not starting with INBOX.OLItest and del...
|
2015-03-23 12:13:23 +01:00
|
|
|
dirs = [d for d in dirs if d.startswith(b'"INBOX.OLItest') or d.startswith(b'"INBOX/OLItest')]
|
2012-02-17 10:12:53 +01:00
|
|
|
for folder in dirs:
|
2015-03-23 12:13:23 +01:00
|
|
|
res_t, data = imapobj.delete(folder)
|
2020-08-29 20:26:29 +02:00
|
|
|
assert res_t == 'OK', "Folder deletion of {0} failed with error" \
|
|
|
|
":\n{1} {2}".format(folder.decode('utf-8'), res_t, data)
|
2012-02-17 10:12:53 +01:00
|
|
|
imapobj.logout()
|
|
|
|
|
2012-01-20 15:55:44 +01:00
|
|
|
@classmethod
|
|
|
|
def create_maildir(cls, folder):
|
|
|
|
"""Create empty maildir 'folder' in our test maildir
|
|
|
|
|
|
|
|
Does not fail if it already exists"""
|
|
|
|
assert cls.testdir != None
|
|
|
|
maildir = os.path.join(cls.testdir, 'mail', folder)
|
2020-08-29 20:26:29 +02:00
|
|
|
for subdir in ('', 'tmp', 'cur', 'new'):
|
2012-01-20 15:55:44 +01:00
|
|
|
try:
|
2012-02-16 09:55:26 +01:00
|
|
|
os.makedirs(os.path.join(maildir, subdir))
|
2012-01-20 15:55:44 +01:00
|
|
|
except OSError as e:
|
2020-08-29 20:26:29 +02:00
|
|
|
if e.errno != 17: # 'already exists' is ok.
|
2012-01-20 15:55:44 +01:00
|
|
|
raise
|
|
|
|
|
2012-02-24 09:39:39 +01:00
|
|
|
@classmethod
|
|
|
|
def delete_maildir(cls, folder):
|
|
|
|
"""Delete maildir 'folder' in our test maildir
|
|
|
|
|
|
|
|
Does not fail if not existing"""
|
|
|
|
assert cls.testdir != None
|
|
|
|
maildir = os.path.join(cls.testdir, 'mail', folder)
|
|
|
|
shutil.rmtree(maildir, ignore_errors=True)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def create_mail(cls, folder, mailfile=None, content=None):
|
|
|
|
"""Create a mail in maildir 'folder'/new
|
|
|
|
|
|
|
|
Use default mailfilename if not given.
|
|
|
|
Use some default content if not given"""
|
|
|
|
assert cls.testdir != None
|
|
|
|
while True: # Loop till we found a unique filename
|
2020-08-29 20:26:29 +02:00
|
|
|
mailfile = '{0}:2,'.format(random.randint(0, 999999999))
|
2012-02-24 09:39:39 +01:00
|
|
|
mailfilepath = os.path.join(cls.testdir, 'mail',
|
|
|
|
folder, 'new', mailfile)
|
|
|
|
if not os.path.isfile(mailfilepath):
|
|
|
|
break
|
2020-08-29 20:26:29 +02:00
|
|
|
with open(mailfilepath, "wb") as mailf:
|
|
|
|
mailf.write(b'''From: test <test@offlineimap.org>
|
2012-02-24 09:39:39 +01:00
|
|
|
Subject: Boo
|
|
|
|
Date: 1 Jan 1980
|
|
|
|
To: test@offlineimap.org
|
|
|
|
|
|
|
|
Content here.''')
|
|
|
|
|
2012-01-20 15:55:44 +01:00
|
|
|
@classmethod
|
|
|
|
def count_maildir_mails(cls, folder):
|
|
|
|
"""Returns the number of mails in maildir 'folder'
|
|
|
|
|
|
|
|
Counting only those in cur&new (ignoring tmp)."""
|
|
|
|
assert cls.testdir != None
|
|
|
|
maildir = os.path.join(cls.testdir, 'mail', folder)
|
2012-01-09 13:12:28 +01:00
|
|
|
|
2012-01-20 15:55:44 +01:00
|
|
|
boxes, mails = 0, 0
|
|
|
|
for dirpath, dirs, files in os.walk(maildir, False):
|
|
|
|
if set(dirs) == set(['cur', 'new', 'tmp']):
|
|
|
|
# New maildir folder
|
|
|
|
boxes += 1
|
2020-08-29 20:26:29 +02:00
|
|
|
# raise RuntimeError("%s is not Maildir" % maildir)
|
2012-01-20 15:55:44 +01:00
|
|
|
if dirpath.endswith(('/cur', '/new')):
|
|
|
|
mails += len(files)
|
|
|
|
return boxes, mails
|
2012-02-24 09:39:39 +01:00
|
|
|
|
|
|
|
# find UID in a maildir filename
|
|
|
|
re_uidmatch = re.compile(',U=(\d+)')
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def get_maildir_uids(cls, folder):
|
|
|
|
"""Returns a list of maildir mail uids, 'None' if no valid uid"""
|
|
|
|
assert cls.testdir != None
|
|
|
|
mailfilepath = os.path.join(cls.testdir, 'mail', folder)
|
|
|
|
assert os.path.isdir(mailfilepath)
|
|
|
|
ret = []
|
|
|
|
for dirpath, dirs, files in os.walk(mailfilepath):
|
|
|
|
if not dirpath.endswith((os.path.sep + 'new', os.path.sep + 'cur')):
|
2020-08-29 20:26:29 +02:00
|
|
|
continue # only /new /cur are interesting
|
2012-02-24 09:39:39 +01:00
|
|
|
for file in files:
|
|
|
|
m = cls.re_uidmatch.search(file)
|
|
|
|
uid = m.group(1) if m else None
|
|
|
|
ret.append(uid)
|
|
|
|
return ret
|