3 # Copyright 2011, Google Inc.
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions are
10 # * Redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer.
12 # * Redistributions in binary form must reproduce the above
13 # copyright notice, this list of conditions and the following disclaimer
14 # in the documentation and/or other materials provided with the
16 # * Neither the name of Google Inc. nor the names of its
17 # contributors may be used to endorse or promote products derived from
18 # this software without specific prior written permission.
20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 """Tests for util module."""
40 import set_sys_path # Update sys.path to locate mod_pywebsocket module.
42 from mod_pywebsocket import util
45 _TEST_DATA_DIR = os.path.join(os.path.split(__file__)[0], 'testdata')
48 class UtilTest(unittest.TestCase):
49 """A unittest for util module."""
51 def test_get_stack_trace(self):
52 self.assertEqual('None\n', util.get_stack_trace())
54 a = 1 / 0 # Intentionally raise exception.
56 trace = util.get_stack_trace()
57 self.failUnless(trace.startswith('Traceback'))
58 self.failUnless(trace.find('ZeroDivisionError') != -1)
60 def test_prepend_message_to_exception(self):
61 exc = Exception('World')
62 self.assertEqual('World', str(exc))
63 util.prepend_message_to_exception('Hello ', exc)
64 self.assertEqual('Hello World', str(exc))
66 def test_get_script_interp(self):
67 cygwin_path = 'c:\\cygwin\\bin'
68 cygwin_perl = os.path.join(cygwin_path, 'perl')
69 self.assertEqual(None, util.get_script_interp(
70 os.path.join(_TEST_DATA_DIR, 'README')))
71 self.assertEqual(None, util.get_script_interp(
72 os.path.join(_TEST_DATA_DIR, 'README'), cygwin_path))
73 self.assertEqual('/usr/bin/perl -wT', util.get_script_interp(
74 os.path.join(_TEST_DATA_DIR, 'hello.pl')))
75 self.assertEqual(cygwin_perl + ' -wT', util.get_script_interp(
76 os.path.join(_TEST_DATA_DIR, 'hello.pl'), cygwin_path))
78 def test_hexify(self):
79 self.assertEqual('61 7a 41 5a 30 39 20 09 0d 0a 00 ff',
80 util.hexify('azAZ09 \t\r\n\x00\xff'))
83 class RepeatedXorMaskerTest(unittest.TestCase):
84 """A unittest for RepeatedXorMasker class."""
87 # Sample input e6,97,a5 is U+65e5 in UTF-8
88 masker = util.RepeatedXorMasker('\xff\xff\xff')
89 result = masker.mask('\xe6\x97\xa5')
90 self.assertEqual('\x19\x68\x5a', result)
92 masker = util.RepeatedXorMasker('\x00\x00\x00')
93 result = masker.mask('\xe6\x97\xa5')
94 self.assertEqual('\xe6\x97\xa5', result)
96 masker = util.RepeatedXorMasker('\xe6\x97\xa5')
97 result = masker.mask('\xe6\x97\xa5')
98 self.assertEqual('\x00\x00\x00', result)
100 def test_mask_twice(self):
101 masker = util.RepeatedXorMasker('\x00\x7f\xff')
102 # mask[0], mask[1], ... will be used.
103 result = masker.mask('\x00\x00\x00\x00\x00')
104 self.assertEqual('\x00\x7f\xff\x00\x7f', result)
105 # mask[2], mask[0], ... will be used for the next call.
106 result = masker.mask('\x00\x00\x00\x00\x00')
107 self.assertEqual('\xff\x00\x7f\xff\x00', result)
110 if __name__ == '__main__':