Welcome to mirror list, hosted at ThFree Co, Russian Federation.

test_sanitize_filename.py « no_gui « test - dev.gajim.org/gajim/gajim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5187dfae59bc3ed733bc586c235db3303ec9582a (plain)
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

import sys
import unittest
from unittest.mock import patch

from gajim.common.helpers import sanitize_filename


class SanitizeTest(unittest.TestCase):
    '''Tests for the sanitize_filename function.'''

    @patch.object(sys, 'platform', 'win32')
    def test_invalid_chars(self):
        '''Make sure invalid characters are removed in filenames'''
        self.assertEqual(sanitize_filename('A/B/C'), 'ABC')
        self.assertEqual(sanitize_filename('A*C.d'), 'AC.d')
        self.assertEqual(sanitize_filename('A?C.d'), 'AC.d')

    @patch.object(sys, 'platform', 'win32')
    def test_invalid_suffix(self):
        '''Dots are not allowed at the end'''
        self.assertEqual(sanitize_filename('def.'), 'def')
        self.assertEqual(sanitize_filename('def.ghi'), 'def.ghi')
        self.assertTrue(sanitize_filename('X' * 1000 + '.').endswith('X'))

    @patch.object(sys, 'platform', 'win32')
    def test_reserved_words(self):
        '''Make sure reserved Windows words are prefixed'''
        self.assertEqual(sanitize_filename('NUL'), '__NUL')
        self.assertEqual(sanitize_filename('..'), '__')

    @patch.object(sys, 'platform', 'win32')
    def test_long_names(self):
        '''Make sure long names are truncated'''
        self.assertEqual(len(sanitize_filename('X' * 300)), 50)
        self.assertEqual(len(sanitize_filename(
            '.'.join(['X' * 100, 'X' * 100, 'X' * 100]))), 50)
        self.assertEqual(len(sanitize_filename(
            '.'.join(['X' * 300, 'X' * 300, 'X' * 300]))), 50)
        self.assertEqual(len(sanitize_filename('.' * 300 + '.txt')), 50)

    @patch.object(sys, 'platform', 'win32')
    def test_unicode_normalization(self):
        '''Names should be NFKD normalized'''
        self.assertEqual(sanitize_filename('ў'), chr(1091) + chr(774))

    @patch.object(sys, 'platform', 'win32')
    def test_extensions(self):
        '''Filename extensions should be preserved when possible.'''
        really_long_name = 'X' * 1000 + '.pdf'
        self.assertTrue(sanitize_filename(really_long_name).endswith('.pdf'))
        self.assertTrue(sanitize_filename('X' * 1000).endswith('X'))
        self.assertTrue(sanitize_filename(
            'X' * 100 + '.' + 'X' * 100 + '.pdf').endswith('.pdf'))
        self.assertTrue(sanitize_filename(
            'X' * 100 + '.' + 'X' * 400).endswith('X'))
        self.assertTrue(sanitize_filename(
            'X' * 100 + '.' + 'X' * 400 + '.pdf').endswith('.pdf'))


if __name__ == '__main__':
    unittest.main()