#!/usr/bin/env python
# -*- Mode: Python; indent-tabs-mode: nil; tab-width: 2; coding: utf-8 -*-
#
# This file is part of Déjà Dup.
# © 2010 Michael Terry <mike@mterry.name>
#
# 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 handle backup destinations with odd permissions

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

def nowrite():
  backupdir = base.get_temp_name('backup')
  os.makedirs(backupdir)
  os.system('chmod a-w %s' % backupdir)
  base.setup('file', sources=['data/simple'], dest=backupdir)
  if not base.dup_meets_version(0, 7, 0): return
  base.backup_simple(error='lblPermissiondeniedwhentryingtocreate')

def noread():
  backupdir = base.get_temp_name('backup')
  os.makedirs(backupdir)
  os.system('chmod a-r %s' % backupdir)
  base.setup('file', sources=['data/simple'], dest=backupdir)
  base.set_gconf_value("last-run", "fake") # to avoid error when setting backup dir
  if not base.dup_meets_version(0, 7, 0): return
  base.backup_simple(error=u'lblPermissiondeniedwhentryingtoread\u2018%s\u2019' % backupdir)

def noexec():
  backupdir = base.get_temp_name('backup')
  os.makedirs(backupdir)
  os.system('chmod a-x %s' % backupdir)
  base.setup('file', sources=['data/simple'], dest=backupdir)
  if not base.dup_meets_version(0, 7, 0): return
  base.backup_simple(error='lblPermissiondeniedwhentryingtocreate')

def noget():
  backupdir = base.get_temp_name('backup')
  os.makedirs(backupdir)
  os.system('cp %s/* %s' % (sys.path[0]+'/vols/simple', backupdir))
  os.system('chmod a-r %s/*' % backupdir)
  base.setup('file', sources=['data/simple'], dest=backupdir, encrypt=False)
  if not base.dup_meets_version(0, 7, 0): return
  base.backup_simple(error=u'lblPermissiondeniedwhentryingtoread\u2018%s\u2019' %
                           (backupdir+'/duplicity-full-signatures20090802T011441Zsigtargz'))

def noexist():
  backupdir = base.get_temp_name('backup')
  base.setup('file', sources=['data/simple'], dest=backupdir)
  base.set_gconf_value("last-run", "fake") # to avoid losing missing dir name when using UI
  if not base.dup_meets_version(0, 7, 0): return
  base.backup_simple(error=u'lblBackuplocation\u2018%s\u2019doesnotexist' % backupdir)

base.run(nowrite)
base.run(noread)
base.run(noexec)
base.run(noget)
base.run(noexist)
