4 # Cumulus: Efficient Filesystem Backup to the Cloud
5 # Copyright (C) 2014 The Cumulus Developers
6 # See the AUTHORS file for a list of contributors.
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License along
19 # with this program; if not, write to the Free Software Foundation, Inc.,
20 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 """Unit tests for the cumulus.util module."""
24 from __future__ import division, print_function, unicode_literals
29 from cumulus import util
31 class UtilCodecs(unittest.TestCase):
32 def test_pathnames(self):
33 self.assertEqual(util.ENCODING, "utf-8")
35 self.assertEqual(util.bytes_to_pathname(b"ext\xc3\xa9nsion"),
37 self.assertEqual(util.pathname_to_bytes(b"ext\xc3\xa9nsion"),
39 self.assertEqual(util.pathname_to_bytes(u"exténsion"),
42 self.assertEqual(util.bytes_to_pathname(b"ext\xc3\xa9nsion"),
44 self.assertEqual(util.pathname_to_bytes("exténsion"),
46 self.assertEqual(util.bytes_to_pathname(b"inv\xe1lid"),
48 self.assertEqual(util.pathname_to_bytes("inv\udce1lid"),
51 def test_uri_encode_raw(self):
52 self.assertEqual(util.uri_encode_raw(b"sample ASCII"), "sample%20ASCII")
53 self.assertEqual(util.uri_encode_raw(b"sample ext\xc3\xa9nded"),
54 "sample%20ext%c3%a9nded")
56 def test_uri_decode_raw(self):
57 self.assertEqual(util.uri_decode_raw("sample%20ASCII"), b"sample ASCII")
58 self.assertEqual(util.uri_decode_raw("sample%20ext%c3%a9nded"),
59 b"sample ext\xc3\xa9nded")
61 def test_uri_decode_pathname(self):
63 self.assertEqual(util.uri_decode_pathname("sample%20ext%c3%a9nded"),
64 b"sample ext\xc3\xa9nded")
65 self.assertEqual(util.uri_decode_pathname("sample%20exténded"),
66 b"sample ext\xc3\xa9nded")
67 # In Python 2, non-UTF-8 sequences are just passed through as
69 self.assertEqual(util.uri_decode_pathname(b"inv%e1lid"),
71 self.assertEqual(util.uri_decode_pathname(b"inv\xe1lid"),
74 self.assertEqual(util.uri_decode_pathname("sample%20ext%c3%a9nded"),
76 self.assertEqual(util.uri_decode_pathname("sample%20exténded"),
78 # In Python 3, non-UTF-8 sequences are represented using surrogate
79 # escapes to allow lossless conversion back to the appropriate
81 self.assertEqual(util.uri_decode_pathname("inv%e1lid"),
84 util.pathname_to_bytes(util.uri_decode_pathname("inv%e1lid")),
88 if __name__ == "__main__":