Home | History | Annotate | Download | only in test
      1 #!/usr/bin/env python
      2 #
      3 # Copyright 2011, Google Inc.
      4 # All rights reserved.
      5 #
      6 # Redistribution and use in source and binary forms, with or without
      7 # modification, are permitted provided that the following conditions are
      8 # met:
      9 #
     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
     15 # distribution.
     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.
     19 #
     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.
     31 
     32 
     33 """Tests for util module."""
     34 
     35 
     36 import os
     37 import sys
     38 import unittest
     39 
     40 import set_sys_path  # Update sys.path to locate mod_pywebsocket module.
     41 
     42 from mod_pywebsocket import util
     43 
     44 
     45 _TEST_DATA_DIR = os.path.join(os.path.split(__file__)[0], 'testdata')
     46 
     47 
     48 class UtilTest(unittest.TestCase):
     49     """A unittest for util module."""
     50 
     51     def test_get_stack_trace(self):
     52         self.assertEqual('None\n', util.get_stack_trace())
     53         try:
     54             a = 1 / 0  # Intentionally raise exception.
     55         except Exception:
     56             trace = util.get_stack_trace()
     57             self.failUnless(trace.startswith('Traceback'))
     58             self.failUnless(trace.find('ZeroDivisionError') != -1)
     59 
     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))
     65 
     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))
     77 
     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'))
     81 
     82 
     83 class RepeatedXorMaskerTest(unittest.TestCase):
     84     """A unittest for RepeatedXorMasker class."""
     85 
     86     def test_mask(self):
     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)
     91 
     92         masker = util.RepeatedXorMasker('\x00\x00\x00')
     93         result = masker.mask('\xe6\x97\xa5')
     94         self.assertEqual('\xe6\x97\xa5', result)
     95 
     96         masker = util.RepeatedXorMasker('\xe6\x97\xa5')
     97         result = masker.mask('\xe6\x97\xa5')
     98         self.assertEqual('\x00\x00\x00', result)
     99 
    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)
    108 
    109 
    110 if __name__ == '__main__':
    111     unittest.main()
    112 
    113 
    114 # vi:sts=4 sw=4 et
    115