Permalink
Browse files

Fix bug ($topdir/.Trash appears always unsecure).

  • Loading branch information...
1 parent 627e267 commit 624695174884655d27facc0570e0e60626da8071 @andreafrancia committed Oct 3, 2012
Showing with 106 additions and 109 deletions.
  1. +89 −103 integration_tests/test_trash_put.py
  2. +10 −3 problems-activated-by-the-fixing-of-bug.txt
  3. +6 −2 trashcli/put.py
  4. +1 −1 trashcli/trash.py
View
192 integration_tests/test_trash_put.py
@@ -13,6 +13,10 @@ class TrashPutTest:
def setUp(self):
self.prepare_fixture()
+ self.setUp2()
+
+ def setUp2(self):
+ pass
def prepare_fixture(self):
require_empty_dir('sandbox')
@@ -37,126 +41,55 @@ def run_trashput(self, *argv):
self.stderr = self.err.getvalue()
@istest
-class trash_put_stderr(TrashPutTest):
- @istest
- def should_tell_where_a_file_is_trashed(self):
- having_file('foo')
- self.run_trashput('trash-put', '-v', 'foo')
-
- assert_in("trash-put: `foo' trashed in sandbox/XDG_DATA_HOME/Trash",
- self.stderr.splitlines())
-
-
-from textwrap import dedent
-@istest
-class TestUnsecureTrashDirMessages(TrashPutTest):
- def setUp(self):
- TrashPutTest.setUp(self)
- having_empty_dir('fake-vol')
- self.fstab.add_mount('fake-vol')
- having_file('fake-vol/foo')
+class when_deleting_an_existing_file(TrashPutTest):
+ def setUp2(self):
+ having_file('sandbox/foo')
+ self.run_trashput('trash-put', 'sandbox/foo')
@istest
- def when_is_unsticky(self):
- having_empty_dir('fake-vol/.Trash')
-
- self.run_trashput('trash-put', '-v', 'fake-vol/foo')
-
- assert_line_in_text(
- 'trash-put: found unsecure .Trash dir (should be sticky): '
- 'fake-vol/.Trash', self.stderr)
+ def it_should_remove_the_file(self):
+ file_should_have_been_deleted('sandbox/foo')
@istest
- def when_it_is_not_a_dir(self):
- having_file('fake-vol/.Trash')
-
- self.run_trashput('trash-put', '-v', 'fake-vol/foo')
-
- assert_line_in_text(
- 'trash-put: found unusable .Trash dir (should be a dir): '
- 'fake-vol/.Trash', self.stderr)
+ def it_should_remove_it_silently(self):
+ self.output_should_be('')
@istest
- def when_is_a_symlink(self):
- make_sticky_dir('fake-vol/link-destination')
- os.symlink('link-destination', 'fake-vol/.Trash')
-
- self.run_trashput('trash-put', '-v', 'fake-vol/foo')
-
- assert_line_in_text(
- 'trash-put: found unsecure .Trash dir (should not be a symlink): '
- 'fake-vol/.Trash', self.stderr)
-
-def assert_line_in_text(line, text):
- assert_in(line, text.splitlines(), dedent('''\
- Line not found in text
- Line:
-
- %s
-
- Text:
-
- ---
- %s---''')
- %(repr(line), text))
-
-
-
-def should_fail(func):
- from nose.tools import assert_raises
- with assert_raises(AssertionError):
- func()
+ def a_trashinfo_file_should_have_been_created(self):
+ file('sandbox/XDG_DATA_HOME/Trash/info/foo.trashinfo').read()
@istest
-class exit_code(TrashPutTest):
- @istest
- def should_be_zero_on_success(self):
- having_file('foo')
- self.run_trashput('trash-put', 'foo')
- self.exit_code_should_be_successfull()
+class when_deleting_an_existing_file_in_verbose_mode(TrashPutTest):
+ def setUp2(self):
+ having_file('sandbox/foo')
+ self.run_trashput('trash-put', '-v', 'sandbox/foo')
@istest
- def should_be_non_zero_on_failure(self):
- self.run_trashput('trash-put', 'non-existent')
- self.exit_code_should_be_not_successfull()
+ def should_tell_where_a_file_is_trashed(self):
+ assert_in("trash-put: `sandbox/foo' trashed in sandbox/XDG_DATA_HOME/Trash",
+ self.stderr.splitlines())
- def exit_code_should_be_successfull(self):
+ @istest
+ def should_be_succesfull(self):
assert_equals(0, self.exit_code)
- def exit_code_should_be_not_successfull(self):
- assert_not_equals(0, self.exit_code)
-
@istest
-class when_deleting_a_file(TrashPutTest):
-
- def setUp(self):
- self.prepare_fixture()
-
- having_file('sandbox/foo')
- self.run_trashput('trash-put', 'sandbox/foo')
+class when_deleting_a_non_existing_file(TrashPutTest):
+ def setUp2(self):
+ self.run_trashput('trash-put', '-v', 'non-existent')
@istest
- def it_should_remove_the_file(self):
-
- file_should_have_been_deleted('sandbox/foo')
-
- @istest
- def it_should_remove_it_silently(self):
-
- self.output_should_be('')
-
- def a_trashinfo_file_should_have_been_created(self):
-
- file('sandbox/XDG_DATA_HOME/Trash/info/foo.trashinfo').read()
+ def should_be_succesfull(self):
+ assert_not_equals(0, self.exit_code)
@istest
class when_fed_with_dot_arguments(TrashPutTest):
- def setUp(self):
- self.prepare_fixture()
+ def setUp2(self):
+ having_empty_dir('sandbox/')
+ having_file('other_argument')
def test_dot_argument_is_skipped(self):
- having_file('other_argument')
self.run_trashput("trash-put", ".", "other_argument")
@@ -169,7 +102,6 @@ def test_dot_argument_is_skipped(self):
assert not exists('other_argument')
def test_dot_dot_argument_is_skipped(self):
- having_file('other_argument')
self.run_trashput("trash-put", "..", "other_argument")
@@ -182,8 +114,6 @@ def test_dot_dot_argument_is_skipped(self):
assert not exists('other_argument')
def test_dot_argument_is_skipped_even_in_subdirs(self):
- having_empty_dir('sandbox/')
- having_file('other_argument')
self.run_trashput("trash-put", "sandbox/.", "other_argument")
@@ -197,8 +127,6 @@ def test_dot_argument_is_skipped_even_in_subdirs(self):
assert exists('sandbox')
def test_dot_dot_argument_is_skipped_even_in_subdirs(self):
- having_empty_dir('sandbox')
- having_file('other_argument')
self.run_trashput("trash-put", "sandbox/..", "other_argument")
@@ -211,6 +139,64 @@ def test_dot_dot_argument_is_skipped_even_in_subdirs(self):
assert not exists('other_argument')
assert exists('sandbox')
+from textwrap import dedent
+@istest
+class TestUnsecureTrashDirMessages(TrashPutTest):
+ def setUp(self):
+ TrashPutTest.setUp(self)
+ having_empty_dir('fake-vol')
+ self.fstab.add_mount('fake-vol')
+ having_file('fake-vol/foo')
+
+ @istest
+ def when_is_unsticky(self):
+ having_empty_dir('fake-vol/.Trash')
+
+ self.run_trashput('trash-put', '-v', 'fake-vol/foo')
+
+ assert_line_in_text(
+ 'trash-put: found unsecure .Trash dir (should be sticky): '
+ 'fake-vol/.Trash', self.stderr)
+
+ @istest
+ def when_it_is_not_a_dir(self):
+ having_file('fake-vol/.Trash')
+
+ self.run_trashput('trash-put', '-v', 'fake-vol/foo')
+
+ assert_line_in_text(
+ 'trash-put: found unusable .Trash dir (should be a dir): '
+ 'fake-vol/.Trash', self.stderr)
+
+ @istest
+ def when_is_a_symlink(self):
+ make_sticky_dir('fake-vol/link-destination')
+ os.symlink('link-destination', 'fake-vol/.Trash')
+
+ self.run_trashput('trash-put', '-v', 'fake-vol/foo')
+
+ assert_line_in_text(
+ 'trash-put: found unsecure .Trash dir (should not be a symlink): '
+ 'fake-vol/.Trash', self.stderr)
+
+def assert_line_in_text(line, text):
+ assert_in(line, text.splitlines(), dedent('''\
+ Line not found in text
+ Line:
+
+ %s
+
+ Text:
+
+ ---
+ %s---''')
+ %(repr(line), text))
+
+def should_fail(func):
+ from nose.tools import assert_raises
+ with assert_raises(AssertionError):
+ func()
+
def file_should_have_been_deleted(path):
import os
assert not os.path.exists('sandbox/foo')
View
13 problems-activated-by-the-fixing-of-bug.txt
@@ -18,7 +18,9 @@ Traceback (most recent call last):
self.test(*self.arg)
File "/Users/andrea/code/trash-cli/integration_tests/test_trash_put.py", line 47, in should_tell_where_a_file_is_trashed
self.stderr.splitlines())
-AssertionError: "trash-put: `foo' trashed in sandbox/XDG_DATA_HOME/Trash" not found in ['trash-put: found unsecure .Trash dir (should be sticky): .Trash', "trash-put: cannot trash regular empty file `foo'"]
+AssertionError: "trash-put: `foo' trashed in sandbox/XDG_DATA_HOME/Trash" not
+found in ['trash-put: found unsecure .Trash dir (should be sticky): .Trash',
+"trash-put: cannot trash regular empty file `foo'"]
======================================================================
FAIL: integration_tests.test_trash_put.when_deleting_a_file.it_should_remove_the_file
@@ -46,7 +48,8 @@ Traceback (most recent call last):
unidiff(expected, actual))
AssertionError:
Expected:"trash-put: cannot trash directory `.'\n"
- Actual:"trash-put: cannot trash directory `.'\ntrash-put: cannot trash regular empty file `other_argument'\n"
+ Actual:"trash-put: cannot trash directory `.'\ntrash-put: cannot trash
+ regular empty file `other_argument'\n"
--- Expected
+++ Actual
@@ -1 +1,2 @@
@@ -128,7 +131,11 @@ Traceback (most recent call last):
self.test(*self.arg)
File "/Users/andrea/code/trash-cli/unit_tests/test_home_fallback.py", line 45, in test_should_skip_top_trash_if_does_not_exists
], self.fs.mock_calls)
-AssertionError: [call.isdir('.Trash'), call.islink('.Trash'), call.ensure_dir('.Trash/123/info', 448), call.atomic_write('.Trash/123/info/foo.trashinfo', <ANY>), call.ensure_dir('.Trash/123/files', 448), call.move('sandbox/foo', '.Trash/123/files/foo')] != [call.isdir('.Trash'), call.islink('.Trash')]
+AssertionError: [call.isdir('.Trash'), call.islink('.Trash'),
+call.ensure_dir('.Trash/123/info', 448),
+call.atomic_write('.Trash/123/info/foo.trashinfo', <ANY>),
+call.ensure_dir('.Trash/123/files', 448), call.move('sandbox/foo',
+'.Trash/123/files/foo')] != [call.isdir('.Trash'), call.islink('.Trash')]
----------------------------------------------------------------------
Ran 162 tests in 0.925s
View
8 trashcli/put.py
@@ -257,29 +257,33 @@ def trash(self, file) :
except (IOError, OSError), error:
self.reporter.unable_to_trash_file_in_because(
file, trash_dir.name(), str(error))
+
if file_has_been_trashed: break
if not file_has_been_trashed:
self.reporter.unable_to_trash_file(file)
def _is_trash_dir_secure(self, trash_dir):
class ValidationOutput:
def __init__(self):
- self.valid = False
+ self.valid = True
def not_valid_should_be_a_dir(_):
self.reporter.invalid_top_trash_is_not_a_dir(
os.path.dirname(trash_dir.path))
+ self.valid = False
def not_valid_parent_should_not_be_a_symlink(_):
self.reporter.found_unsercure_trash_dir_symlink(
os.path.dirname(trash_dir.path))
+ self.valid = False
def not_valid_parent_should_be_sticky(_):
self.reporter.found_unsecure_trash_dir_unsticky(
os.path.dirname(trash_dir.path))
+ self.valid = False
def is_valid(self):
self.valid = True
output = ValidationOutput()
trash_dir.checker.fs = self.fs
trash_dir.checker.valid_to_be_written(trash_dir.path, output)
- return output.is_valid # <--- this is the BUG
+ return output.valid
def _should_skipped_by_specs(self, file):
basename = os.path.basename(file)
View
2 trashcli/trash.py
@@ -1,7 +1,7 @@
# Copyright (C) 2007-2011 Andrea Francia Trivolzio(PV) Italy
from __future__ import absolute_import
-version='0.12.9.14~'
+version='0.12.10.3~'
import os
import logging

0 comments on commit 6246951

Please sign in to comment.