Rework uri_encode/uri_decode to more cleanly work with bytes/strings.
[cumulus.git] / python / cumulus / util_test.py
1 #!/usr/bin/python
2 # coding: utf-8
3 #
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.
7 #
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.
12 #
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.
17 #
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.
21
22 """Unit tests for the cumulus.util module."""
23
24 from __future__ import division, print_function, unicode_literals
25
26 import six
27 import unittest
28
29 from cumulus import util
30
31 class UtilCodecs(unittest.TestCase):
32     def test_pathnames(self):
33         self.assertEqual(util.ENCODING, "utf-8")
34         if six.PY2:
35             self.assertEqual(util.bytes_to_pathname(b"ext\xc3\xa9nsion"),
36                              b"ext\xc3\xa9nsion")
37             self.assertEqual(util.pathname_to_bytes(b"ext\xc3\xa9nsion"),
38                              b"ext\xc3\xa9nsion")
39             self.assertEqual(util.pathname_to_bytes(u"exténsion"),
40                              b"ext\xc3\xa9nsion")
41         elif six.PY3:
42             self.assertEqual(util.bytes_to_pathname(b"ext\xc3\xa9nsion"),
43                              "exténsion")
44             self.assertEqual(util.pathname_to_bytes("exténsion"),
45                              b"ext\xc3\xa9nsion")
46             self.assertEqual(util.bytes_to_pathname(b"inv\xe1lid"),
47                              "inv\udce1lid")
48             self.assertEqual(util.pathname_to_bytes("inv\udce1lid"),
49                              b"inv\xe1lid")
50
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")
55
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")
60
61     def test_uri_decode_pathname(self):
62         if six.PY2:
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
68             # bytestrings.
69             self.assertEqual(util.uri_decode_pathname(b"inv%e1lid"),
70                              b"inv\xe1lid")
71             self.assertEqual(util.uri_decode_pathname(b"inv\xe1lid"),
72                              b"inv\xe1lid")
73         elif six.PY3:
74             self.assertEqual(util.uri_decode_pathname("sample%20ext%c3%a9nded"),
75                              "sample exténded")
76             self.assertEqual(util.uri_decode_pathname("sample%20exténded"),
77                              "sample exténded")
78             # In Python 3, non-UTF-8 sequences are represented using surrogate
79             # escapes to allow lossless conversion back to the appropriate
80             # bytestring.
81             self.assertEqual(util.uri_decode_pathname("inv%e1lid"),
82                              "inv\udce1lid")
83             self.assertEqual(
84                 util.pathname_to_bytes(util.uri_decode_pathname("inv%e1lid")),
85                 b"inv\xe1lid")
86
87
88 if __name__ == "__main__":
89     unittest.main()