~didrocks/ubuntuone-client/use_result_var

« back to all changes in this revision

Viewing changes to tests/platform/windows/test_os_helper.py

  • Committer: Bazaar Package Importer
  • Author(s): Rodney Dawes
  • Date: 2011-02-11 16:18:11 UTC
  • mto: This revision was merged to the branch mainline in revision 67.
  • Revision ID: james.westby@ubuntu.com-20110211161811-n18dj9lde7dxqjzr
Tags: upstream-1.5.4
ImportĀ upstreamĀ versionĀ 1.5.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
2
# Author: Manuel de la Pena <manuel@canonical.com>
 
3
#
 
4
# Copyright 2011 Canonical Ltd.
 
5
#
 
6
# This program is free software: you can redistribute it and/or modify it
 
7
# under the terms of the GNU General Public License version 3, as published
 
8
# by the Free Software Foundation.
 
9
#
 
10
# This program is distributed in the hope that it will be useful, but
 
11
# WITHOUT ANY WARRANTY; without even the implied warranties of
 
12
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 
13
# PURPOSE.  See the GNU General Public License for more details.
 
14
#
 
15
# You should have received a copy of the GNU General Public License along
 
16
# with this program.  If not, see <http://www.gnu.org/licenses/>.
 
17
"""Specific tests for the os_helper on Windows."""
 
18
 
 
19
import os
 
20
 
 
21
from win32api import GetUserName
 
22
 
 
23
from ntsecuritycon import (
 
24
    FILE_ALL_ACCESS,
 
25
    FILE_GENERIC_READ,
 
26
    FILE_GENERIC_WRITE,
 
27
)
 
28
from ubuntuone.platform.windows.os_helper import (
 
29
    _set_file_attributes,
 
30
    access,
 
31
    set_no_rights
 
32
)
 
33
from contrib.testing.testcase import BaseTwistedTestCase
 
34
 
 
35
class OSWrapperTests(BaseTwistedTestCase):
 
36
    """Test specific windows implementation details."""
 
37
    
 
38
    def setUp(self):
 
39
        """Setup for the tests."""
 
40
        super(OSWrapperTests, self).setUp()
 
41
        self.basedir = self.mktemp('test_root')
 
42
        self.testfile = os.path.join(self.basedir, "test_file")
 
43
        self.fd = open(self.testfile, 'w')
 
44
 
 
45
    def tearDown(self):
 
46
        """Clean the env for the next test."""
 
47
        self.fd.close();
 
48
        self.rmtree(self.basedir)
 
49
        super(OSWrapperTests, self).tearDown()
 
50
 
 
51
    def test_access_no_rights(self):
 
52
        """Test when the sid is not present."""
 
53
        # remove all the rights from the test file so that
 
54
        # we cannot read or write
 
55
        set_no_rights(self.testfile)
 
56
        self.assertFalse(access(self.testfile))
 
57
 
 
58
    def test_access_read_write_user(self):
 
59
        """Test when the user sid has rw rights."""
 
60
        # set the file to be read and write just by the user
 
61
        groups = {}
 
62
        groups[GetUserName()] = FILE_GENERIC_READ | FILE_GENERIC_WRITE
 
63
        _set_file_attributes(self.testfile, groups)
 
64
        self.assertTrue(access(self.testfile))
 
65
 
 
66
    def test_access_read_write_everyone(self):
 
67
        """Test when the everyone sid has rw rights."""
 
68
        groups = {}
 
69
        groups['Everyone'] = FILE_GENERIC_READ | FILE_GENERIC_WRITE
 
70
        _set_file_attributes(self.testfile, groups)
 
71
        self.assertTrue(access(self.testfile))
 
72
        
 
73
    def test_access_write_user_everyone_read(self):
 
74
        """Test when the user sid has w rights."""
 
75
        groups = {}
 
76
        groups[GetUserName()] = FILE_GENERIC_WRITE
 
77
        groups['Everyone'] = FILE_GENERIC_READ
 
78
        _set_file_attributes(self.testfile, groups)
 
79
        self.assertTrue(access(self.testfile))
 
80
 
 
81
    def test_access_write_everyone_user_read(self):
 
82
        """Test when the everyone sid has w rights"""
 
83
        groups = {}
 
84
        groups[GetUserName()] = FILE_GENERIC_READ 
 
85
        groups['Everyone'] = FILE_GENERIC_WRITE
 
86
        _set_file_attributes(self.testfile, groups)
 
87
        self.assertTrue(access(self.testfile))
 
88
 
 
89
    def test_access_write_user_everyone(self):
 
90
        """Test when everyone and user have w rights."""
 
91
        groups = {}
 
92
        groups[GetUserName()] = FILE_GENERIC_WRITE
 
93
        groups['Everyone'] = FILE_GENERIC_WRITE
 
94
        _set_file_attributes(self.testfile, groups)
 
95
        self.assertFalse(access(self.testfile))
 
96
 
 
97
    def test_access_read_user(self):
 
98
        """Test when the sid has r rights."""
 
99
        groups = {}
 
100
        groups[GetUserName()] = FILE_GENERIC_READ
 
101
        _set_file_attributes(self.testfile, groups)
 
102
        self.assertTrue(access(self.testfile))
 
103
 
 
104
    def test_access_read_everyone(self):
 
105
        """Test when everyone has r rights."""
 
106
        groups = {}
 
107
        groups['Everyone'] = FILE_GENERIC_READ
 
108
        _set_file_attributes(self.testfile, groups)
 
109
        self.assertTrue(access(self.testfile))
 
110
    
 
111
    def test_access_read_user_everyone(self):
 
112
        """Test when user and everyone have r rights."""
 
113
        groups = {}
 
114
        groups[GetUserName()] = FILE_GENERIC_READ
 
115
        groups['Everyone'] = FILE_GENERIC_READ
 
116
        _set_file_attributes(self.testfile, groups)
 
117
        self.assertTrue(access(self.testfile))
 
118
 
 
119
    def test_access_full_user(self):
 
120
        """Test when the sid has full control."""
 
121
        groups = {}
 
122
        groups[GetUserName()] = FILE_ALL_ACCESS
 
123
        _set_file_attributes(self.testfile, groups)
 
124
        self.assertTrue(access(self.testfile))
 
125
 
 
126
    def test_access_full_everyone(self):
 
127
        """test when everyone has full control."""
 
128
        groups = {}
 
129
        groups['Everyone'] = FILE_ALL_ACCESS
 
130
        _set_file_attributes(self.testfile, groups)
 
131
        self.assertTrue(access(self.testfile))