~nataliabidart/ubuntuone-client/stable-3-0-update-2.99.91.1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
# -*- encoding: utf-8 -*-
#
# Copyright 2011 Canonical Ltd.
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 3, as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranties of
# MERCHANTABILITY, SATISFACTORY QUALITY, 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, see <http://www.gnu.org/licenses/>.

"""Specific tests for the os_helper on Windows."""

import errno
import os
import shutil
import sys
import platform

from twisted.internet import defer
from twisted.trial.unittest import TestCase

from contrib.testing.testcase import BaseTwistedTestCase

from ntsecuritycon import (
    FILE_ALL_ACCESS,
    FILE_GENERIC_READ,
    FILE_GENERIC_WRITE,
)
from win32file import (
    FILE_ATTRIBUTE_NORMAL,
    FILE_ATTRIBUTE_SYSTEM,
    GetFileAttributesW,
    SetFileAttributesW
)

from ubuntuone.platform.windows import os_helper
from ubuntuone.platform.windows.os_helper import (
    _set_file_attributes,
    _unicode_to_bytes,
    EVERYONE_SID,
    LONG_PATH_PREFIX,
    USER_SID,
    access,
    assert_windows_path,
    can_write,
    get_syncdaemon_valid_path,
    get_user_sid,
    get_windows_valid_path,
    normpath,
    set_dir_readwrite,
    set_file_readwrite,
    set_no_rights,
    WINDOWS_ILLEGAL_CHARS_MAP,
)
from tests.platform.test_os_helper import OSWrapperTests, WalkTests


# ugly trick to stop pylint for complaining about
# WindowsError on Linux
if sys.platform != 'win32':
    WindowsError = Exception


def _build_invalid_windows_bytes_name():
    invalid_unicodes = u''.join(WINDOWS_ILLEGAL_CHARS_MAP)
    invalid_filename = 'test_file' + invalid_unicodes.encode('utf8')
    return invalid_filename


class TestIllegalPaths(OSWrapperTests):
    """Test all the operations using illegal paths."""

    @defer.inlineCallbacks
    def setUp(self, test_dir_name=None, test_file_name=None,
              valid_file_path_builder=None):
        """Setup for the tests."""
        test_file_name = _build_invalid_windows_bytes_name()
        yield super(TestIllegalPaths, self).setUp(
            test_dir_name=test_dir_name, test_file_name=test_file_name,
            valid_file_path_builder=get_windows_valid_path)

    def test_rename_file(self, target=None):
        """Rename a file."""
        target = os.path.join(self.basedir, 'target?!><:')
        super(TestIllegalPaths, self).test_rename_file(target)

    def test_rename_dir(self, source=None, target=None):
        """Rename a dir."""
        source = os.path.join(self.basedir, 'source?!><:')
        os.mkdir(get_windows_valid_path(source))
        target = os.path.join(self.basedir, 'target?!><:')
        super(TestIllegalPaths, self).test_rename_dir(source, target)

    def test_listdir(self, expected_result=None):
        """Return a list of the files in a dir."""
        _, valid_path_name = os.path.split(self.valid_path)
        expected_result = [_unicode_to_bytes(valid_path_name)]
        super(TestIllegalPaths, self).test_listdir(expected_result)

    def test_make_link(self):
        """The link is properly made."""
        # XXX: make_link will not work when passing literal paths or
        # invalid characters. We need to do something about that.
        self.testfile = os.path.join(self.basedir, 'test me')
        super(TestIllegalPaths, self).test_make_link()


class TestSpecialOSCalls(BaseTwistedTestCase):
    """Test those calls that have extra logic."""

    def test_normpath_with_longprefix(self):
        """Ensure that the normpath is correct when it uses the long prefix."""
        paths = [os.path.join('A', 'B?'),
                 os.path.join('A', 'B?') + os.path.sep,
                 os.path.join('A:C', '.', 'B?'),
                 os.path.join('A', 'foo', '..', 'B?')]
        for current_path in paths:
            valid_path = get_windows_valid_path(current_path)
            normalized_path = os.path.normpath(valid_path)
            self.assertEqual(get_syncdaemon_valid_path(normalized_path),
                             normpath(current_path))
            self.assertFalse(LONG_PATH_PREFIX in current_path)


class FakeSecurityInfo(object):

    user_sid = 'user_sid'

    # pylint: disable=C0103
    def GetSecurityDescriptorOwner(self):
        return self.user_sid
    # pylint: enable=C0103


class TestAccess(BaseTwistedTestCase):
    """Test specific windows implementation access details."""

    @defer.inlineCallbacks
    def setUp(self):
        """Setup for the tests."""
        yield super(TestAccess, self).setUp()

        self.basedir = self.mktemp('test_root')
        self.addCleanup(set_dir_readwrite, self.basedir)

        self.testfile = os.path.join(self.basedir, "test_file")
        self.valid_path = get_windows_valid_path(self.testfile)
        open(self.testfile, 'w').close()
        self.addCleanup(set_file_readwrite, self.testfile)

    def test_access_no_rights(self):
        """Test when the sid is not present."""
        # remove all the rights from the test file so that
        # we cannot read or write
        set_no_rights(self.testfile)
        self.assertFalse(access(self.testfile))

    def test_access_read_write_user(self):
        """Test when the user sid has rw rights."""
        # set the file to be read and write just by the user
        groups = [(USER_SID, FILE_GENERIC_READ | FILE_GENERIC_WRITE)]
        _set_file_attributes(self.valid_path, groups)
        self.assertTrue(access(self.testfile))

    def test_access_read_write_everyone(self):
        """Test when the everyone sid has rw rights."""
        groups = [(EVERYONE_SID, FILE_GENERIC_READ | FILE_GENERIC_WRITE)]
        _set_file_attributes(self.valid_path, groups)
        self.assertTrue(access(self.testfile))

    def test_access_write_user_everyone_read(self):
        """Test when the user sid has w rights."""
        groups = [
            (USER_SID, FILE_GENERIC_WRITE),
            (EVERYONE_SID, FILE_GENERIC_READ),
        ]
        _set_file_attributes(self.valid_path, groups)
        self.assertTrue(access(self.testfile))

    def test_access_write_everyone_user_read(self):
        """Test when the everyone sid has w rights"""
        groups = [
            (USER_SID, FILE_GENERIC_READ),
            (EVERYONE_SID, FILE_GENERIC_WRITE),
        ]
        _set_file_attributes(self.valid_path, groups)
        self.assertTrue(access(self.testfile))

    def test_access_write_user_everyone(self):
        """Test when everyone and user have w rights."""
        groups = [
            (USER_SID, FILE_GENERIC_WRITE),
            (EVERYONE_SID, FILE_GENERIC_WRITE),
        ]
        _set_file_attributes(self.valid_path, groups)
        self.assertFalse(access(self.testfile))

    def test_access_read_user(self):
        """Test when the sid has r rights."""
        groups = [(USER_SID, FILE_GENERIC_READ)]
        _set_file_attributes(self.valid_path, groups)
        self.assertTrue(access(self.testfile))

    def test_access_read_everyone(self):
        """Test when everyone has r rights."""
        groups = [(EVERYONE_SID, FILE_GENERIC_READ)]
        _set_file_attributes(self.valid_path, groups)
        self.assertTrue(access(self.testfile))

    def test_access_read_user_everyone(self):
        """Test when user and everyone have r rights."""
        groups = [
            (USER_SID, FILE_GENERIC_READ),
            (EVERYONE_SID, FILE_GENERIC_READ),
        ]
        _set_file_attributes(self.valid_path, groups)
        self.assertTrue(access(self.testfile))

    def test_access_full_user(self):
        """Test when the sid has full control."""
        groups = [(USER_SID, FILE_ALL_ACCESS)]
        _set_file_attributes(self.valid_path, groups)
        self.assertTrue(access(self.testfile))

    def test_access_full_everyone(self):
        """test when everyone has full control."""
        groups = [(EVERYONE_SID, FILE_ALL_ACCESS)]
        _set_file_attributes(self.valid_path, groups)
        self.assertTrue(access(self.testfile))

    def test_canwrite_no_rights(self):
        """Test when the sid is not present."""
        # remove all the rights from the test file so that
        # we cannot read or write
        set_no_rights(self.testfile)
        self.assertFalse(can_write(self.testfile))

    def test_can_write_read_write_user(self):
        """Test when the user sid has rw rights."""
        # set the file to be read and write just by the user
        groups = [(USER_SID, FILE_GENERIC_READ | FILE_GENERIC_WRITE)]
        _set_file_attributes(self.valid_path, groups)
        self.assertTrue(can_write(self.testfile))

    def test_can_write_read_write_everyone(self):
        """Test when the everyone sid has rw rights."""
        groups = [(EVERYONE_SID, FILE_GENERIC_READ | FILE_GENERIC_WRITE)]
        _set_file_attributes(self.valid_path, groups)
        self.assertTrue(can_write(self.testfile))

    def test_can_write_write_user_everyone_read(self):
        """Test when the user sid has w rights."""
        groups = [
            (USER_SID, FILE_GENERIC_WRITE),
            (EVERYONE_SID, FILE_GENERIC_READ),
        ]
        _set_file_attributes(self.valid_path, groups)
        self.assertTrue(can_write(self.testfile))

    def test_can_write_write_everyone_user_read(self):
        """Test when the everyone sid has w rights"""
        groups = [
            (USER_SID, FILE_GENERIC_READ),
            (EVERYONE_SID, FILE_GENERIC_WRITE),
        ]
        _set_file_attributes(self.valid_path, groups)
        self.assertTrue(can_write(self.testfile))

    def test_can_write_full_user(self):
        """Test when the sid has full control."""
        groups = [(USER_SID, FILE_ALL_ACCESS)]
        _set_file_attributes(self.valid_path, groups)
        self.assertTrue(can_write(self.testfile))

    def test_can_write_full_everyone(self):
        """test when everyone has full control."""
        groups = [(EVERYONE_SID, FILE_ALL_ACCESS)]
        _set_file_attributes(self.valid_path, groups)
        self.assertTrue(can_write(self.testfile))

    def fake_security_info(self, *args):
        return FakeSecurityInfo()

    def test_get_user_sid(self):
        self.patch(os_helper, "GetSecurityInfo", self.fake_security_info)
        user_sid = get_user_sid()
        self.assertEqual(user_sid, FakeSecurityInfo.user_sid)

    def test_set_file_attributes_missing_path(self):
        """Set file attr for a missing file."""
        groups = [(EVERYONE_SID, FILE_ALL_ACCESS)]
        # file does not exist.
        self.patch(os_helper.os.path, 'exists', lambda f: False)
        exc = self.assertRaises(WindowsError, _set_file_attributes,
                                self.valid_path, groups)
        self.assertEqual(errno.ENOENT, exc.errno,
                        'Errno should be file not found.')


class DecoratorsTestCase(TestCase):
    """Test case for all the validators and transformers."""

    def assert_error_raised(self, path, method_name=None):
        if method_name is None:
            self.assertRaises(AssertionError, assert_windows_path, path)
        else:
            exc = self.assertRaises(AssertionError, assert_windows_path, path,
                                     method_name)
            self.assertTrue(method_name in exc.message)

    def test_assert_windows_path_slash(self):
        """A path with a / is invalid."""
        path = LONG_PATH_PREFIX + u'/a/b/'
        self.assert_error_raised(path)

    def test_assert_windows_method_name_path_slash(self):
        """A path with a / is invalid."""
        path = LONG_PATH_PREFIX + u'/a/b/'
        method_name = 'method_name'
        self.assert_error_raised(path, method_name)

    def test_assert_windows_path_non_unicode(self):
        """A non-unicode path is invalid."""
        path = (LONG_PATH_PREFIX + u'C:\\Yadda').encode('utf8')
        self.assert_error_raised(path)

    def test_assert_windows_method_name_path_non_unicode(self):
        """A non-unicode path is invalid."""
        path = (LONG_PATH_PREFIX + u'C:\\Yadda').encode('utf8')
        method_name = 'method_name'
        self.assert_error_raised(path, method_name)

    def test_assert_windows_path_non_literal(self):
        """A non-literal path is invalid."""
        path = u'C:\\Yadda'
        self.assert_error_raised(path)

    def test_assert_windows_method_name_path_non_literal(self):
        """A non-literal path is invalid."""
        path = u'C:\\Yadda'
        method_name = 'method_name'
        self.assert_error_raised(path, method_name)

    def test_assert_windows_path_non_absolute(self):
        """A non-absolute path is invalid."""
        path = u'./yadda'
        self.assert_error_raised(path)

    def test_assert_windows_method_name_path_non_absolute(self):
        """A non-absolute path is invalid."""
        path = u'./yadda'
        method_name = 'method_name'
        self.assert_error_raised(path, method_name)

    def test_assert_windows_path_with_illegal_chars(self):
        """A path with illegal chars is invalid."""
        path = u'./yadda' + u''.join(WINDOWS_ILLEGAL_CHARS_MAP)
        self.assert_error_raised(path)

    def test_assert_windows_method_name_path_with_illegal_chars(self):
        """A path with illegal chars is invalid."""
        path = u'./yadda' + u''.join(WINDOWS_ILLEGAL_CHARS_MAP)
        method_name = 'method_name'
        self.assert_error_raised(path, method_name)


class TestIllegalPathsWalk(WalkTests):
    """Tests for os wrapper functions."""

    @defer.inlineCallbacks
    def setUp(self, test_dir_name=None, test_file_name=None,
              valid_file_path_builder=None, valid_file_name_builder=None):
        """Setup for the tests."""
        test_file_name = _build_invalid_windows_bytes_name()
        yield super(TestIllegalPathsWalk, self).setUp(
            test_dir_name=test_dir_name, test_file_name=test_file_name,
            valid_file_path_builder=get_windows_valid_path)

    def test_top_down(self, topdown=True, expected=None):
        """Walk the tree top-down."""
        valid_base_dir = get_windows_valid_path(self.basedir)
        result = os.walk(valid_base_dir, topdown)
        expected = self._build_dict_from_walk(result,
            path_transformer=get_syncdaemon_valid_path,
            name_transformer=_unicode_to_bytes)
        super(TestIllegalPathsWalk, self).test_top_down(topdown=topdown,
                                                        expected=expected)


class TestSystemPaths(TestCase):
    """Tests related with the system paths."""

    @defer.inlineCallbacks
    def setUp(self):
        """Set tests."""
        yield super(TestSystemPaths, self).setUp()
        self.system_paths = ['My Music', 'My Pictures']
        self.dirs = ['One', 'Two', 'Tree']
        self.files = ['File', 'Second File', 'Last file']
        self.temp = self.mktemp()
        self._make_test_files()
        self.addCleanup(shutil.rmtree, self.temp)

    def _make_test_files(self):
        """Create the temp test files."""

        # lets make the files for the tests
        for d in self.dirs:
            os.makedirs(os.path.join(self.temp, d))

        for s in self.system_paths:
            path = os.path.join(self.temp, s)
            os.makedirs(path)
            self._set_as_system_path(path)

        for f in self.files:
            path = os.path.join(self.temp, f)
            with open(path, 'w') as fd:
                fd.write('Im a test, blame TestSystemPaths!')

    def _set_as_system_path(self, path):
        """Set a path to have the system attr."""
        attrs = GetFileAttributesW(path)
        attrs = attrs | FILE_ATTRIBUTE_SYSTEM
        SetFileAttributesW(path, attrs)

    def test_os_listdir(self):
        """Test the list dir."""
        expected_result = self.dirs + self.files
        self.assertEqual(sorted(expected_result),
                sorted(os_helper.listdir(self.temp)))

    def test_os_walk(self):
        """Test the walk."""
        expected_dirs = ['One', 'Two', 'Tree']
        expected_files = ['File', 'Second File', 'Last file']
        result_dirs = []
        result_files = []
        for dirpath, dirs, files in os_helper.walk(self.temp):
            result_dirs.extend(dirs)
            result_files.extend(files)
        self.assertEqual(sorted(expected_dirs), sorted(result_dirs))
        self.assertEqual(sorted(expected_files), sorted(result_files))

    def test_native_is_system_path_true(self):
        """Test the function that returns if is a sytem folder."""

        def fake_get_attrs(path):
            """Fake the GetFileAttributes method."""
            return FILE_ATTRIBUTE_NORMAL | FILE_ATTRIBUTE_SYSTEM

        self.patch(os_helper, 'GetFileAttributesW', fake_get_attrs)
        self.assertTrue(os_helper.native_is_system_path(self.temp))

    def test_native_is_system_path_false(self):
        """Test the function that returns if is a sytem folder."""

        def fake_get_attrs(path):
            """Fake the GetFileAttributes method."""
            return FILE_ATTRIBUTE_NORMAL

        self.patch(os_helper, 'GetFileAttributesW', fake_get_attrs)
        self.assertFalse(os_helper.native_is_system_path(self.temp))


class TestIsRoot(TestCase):

    """Tests for the is_root function."""

    def test_isanadmin_called(self):
        """Test that shell.IsUserAnAdmin is called."""
        expected = object()
        self.patch(os_helper.shell, 'IsUserAnAdmin', lambda: expected)
        actual = os_helper.is_root()
        self.assertEqual(expected, actual)

    def test_is_root_on_xp(self):
        """Test that os_helper.is_root always returns False on XP"""
        expected = False
        self.patch(platform, "version", "5.1.2600")
        actual = os_helper.is_root()
        self.assertEqual(expected, actual)