#!/usr/bin/env python
# -*- Mode: Python; indent-tabs-mode: nil; tab-width: 2; coding: utf-8 -*-
#
# This file is part of Déjà Dup.
# For copyright information, see AUTHORS.
#
# Déjà Dup 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 of the License, or
# (at your option) any later version.
#
# Déjà Dup 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 Déjà Dup.  If not, see <http://www.gnu.org/licenses/>.

# Test whether we correctly ask or not for the encryption password

import sys
scriptdir=sys.path[0]
sys.path.insert(0, scriptdir+'/..')
import base
import os
import ldtp
import time

def wait_for_volume(destdir, basenum):
  count = 0
  while len(os.listdir(destdir)) < basenum+2 and count < 200:
    time.sleep(1)
    count += 1

def encrypt():
  base.setup()
  os.environ['PATH'] = os.path.join(scriptdir, 'encrypt-bin') + ":" + os.environ['PATH']
  base.backup_simple(backend='file', encrypt=True, includes=['data/simple'])
  basenum = len(os.listdir(base.get_temp_name('local')))
  base.backup_simple(encrypt=True, finish=False, includes=['/usr/bin'])
  # Test resuming a backup too
  wait_for_volume(base.get_temp_name('local'), basenum)
  ldtp.click('frmBackUp', 'btnResumeLater')
  base.backup_simple(encrypt=True)
  base.restore_simple(base.get_temp_name('restore'), encrypt=True)

def encrypt_nocache():
  base.setup()
  base.backup_simple(backend='file', encrypt=True, includes=['data/simple'])
  os.system('rm -r %s' % base.get_temp_name('cache/deja-dup'))
  basenum = len(os.listdir(base.get_temp_name('local')))
  base.backup_simple(encrypt=True, finish=False, includes=['/usr/bin'])
  # Test resuming a backup too
  wait_for_volume(base.get_temp_name('local'), basenum)
  ldtp.click('frmBackUp', 'btnResumeLater')
  base.backup_simple(encrypt=True)
  os.system('rm -r %s' % base.get_temp_name('cache/deja-dup'))
  base.restore_simple(base.get_temp_name('restore'), encrypt=True)

def noencrypt():
  base.setup()
  base.backup_simple(backend='file', encrypt=False, includes=['data/simple'])
  basenum = len(os.listdir(base.get_temp_name('local')))
  base.backup_simple(encrypt=None, finish=False, includes=['/usr/bin'])
  # Test resuming a backup too
  wait_for_volume(base.get_temp_name('local'), basenum)
  ldtp.click('frmBackUp', 'btnResumeLater')
  base.backup_simple(encrypt=None)
  base.restore_simple(base.get_temp_name('restore'), encrypt=None)

def noencrypt_nocache():
  base.setup()
  base.backup_simple(backend='file', encrypt=False, includes=['data/simple'])
  os.system('rm -r %s' % base.get_temp_name('cache/deja-dup'))
  basenum = len(os.listdir(base.get_temp_name('local')))
  base.backup_simple(encrypt=None, finish=False, includes=['/usr/bin'])
  # Test resuming a backup too
  wait_for_volume(base.get_temp_name('local'), basenum)
  ldtp.click('frmBackUp', 'btnResumeLater')
  base.backup_simple(encrypt=None)
  os.system('rm -r %s' % base.get_temp_name('cache/deja-dup'))
  base.restore_simple(base.get_temp_name('restore'), encrypt=None)

base.run(encrypt)
base.run(encrypt_nocache)
base.run(noencrypt)
base.run(noencrypt_nocache)
