tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_mock.py (5236B)


      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 """Tests for mock module."""
     32 
     33 from __future__ import absolute_import
     34 
     35 import threading
     36 import unittest
     37 
     38 import six.moves.queue
     39 
     40 import set_sys_path  # Update sys.path to locate pywebsocket3 module.
     41 from test import mock
     42 
     43 
     44 class MockConnTest(unittest.TestCase):
     45    """A unittest for MockConn class."""
     46    def setUp(self):
     47        self._conn = mock.MockConn(b'ABC\r\nDEFG\r\n\r\nHIJK')
     48 
     49    def test_readline(self):
     50        self.assertEqual(b'ABC\r\n', self._conn.readline())
     51        self.assertEqual(b'DEFG\r\n', self._conn.readline())
     52        self.assertEqual(b'\r\n', self._conn.readline())
     53        self.assertEqual(b'HIJK', self._conn.readline())
     54        self.assertEqual(b'', self._conn.readline())
     55 
     56    def test_read(self):
     57        self.assertEqual(b'ABC\r\nD', self._conn.read(6))
     58        self.assertEqual(b'EFG\r\n\r\nHI', self._conn.read(9))
     59        self.assertEqual(b'JK', self._conn.read(10))
     60        self.assertEqual(b'', self._conn.read(10))
     61 
     62    def test_read_and_readline(self):
     63        self.assertEqual(b'ABC\r\nD', self._conn.read(6))
     64        self.assertEqual(b'EFG\r\n', self._conn.readline())
     65        self.assertEqual(b'\r\nHIJK', self._conn.read(9))
     66        self.assertEqual(b'', self._conn.readline())
     67 
     68    def test_write(self):
     69        self._conn.write(b'Hello\r\n')
     70        self._conn.write(b'World\r\n')
     71        self.assertEqual(b'Hello\r\nWorld\r\n', self._conn.written_data())
     72 
     73 
     74 class MockBlockingConnTest(unittest.TestCase):
     75    """A unittest for MockBlockingConn class."""
     76    def test_read(self):
     77        """Tests that data put to MockBlockingConn by put_bytes method can be
     78        read from it.
     79        """
     80        class LineReader(threading.Thread):
     81            """A test class that launches a thread, calls readline on the
     82            specified conn repeatedly and puts the read data to the specified
     83            queue.
     84            """
     85            def __init__(self, conn, queue):
     86                threading.Thread.__init__(self)
     87                self._queue = queue
     88                self._conn = conn
     89                self.setDaemon(True)
     90                self.start()
     91 
     92            def run(self):
     93                while True:
     94                    data = self._conn.readline()
     95                    self._queue.put(data)
     96 
     97        conn = mock.MockBlockingConn()
     98        queue = six.moves.queue.Queue()
     99        reader = LineReader(conn, queue)
    100        self.assertTrue(queue.empty())
    101        conn.put_bytes(b'Foo bar\r\n')
    102        read = queue.get()
    103        self.assertEqual(b'Foo bar\r\n', read)
    104 
    105 
    106 class MockTableTest(unittest.TestCase):
    107    """A unittest for MockTable class."""
    108    def test_create_from_dict(self):
    109        table = mock.MockTable({'Key': 'Value'})
    110        self.assertEqual('Value', table.get('KEY'))
    111        self.assertEqual('Value', table['key'])
    112 
    113    def test_create_from_list(self):
    114        table = mock.MockTable([('Key', 'Value')])
    115        self.assertEqual('Value', table.get('KEY'))
    116        self.assertEqual('Value', table['key'])
    117 
    118    def test_create_from_tuple(self):
    119        table = mock.MockTable((('Key', 'Value'), ))
    120        self.assertEqual('Value', table.get('KEY'))
    121        self.assertEqual('Value', table['key'])
    122 
    123    def test_set_and_get(self):
    124        table = mock.MockTable()
    125        self.assertEqual(None, table.get('Key'))
    126        table['Key'] = 'Value'
    127        self.assertEqual('Value', table.get('Key'))
    128        self.assertEqual('Value', table.get('key'))
    129        self.assertEqual('Value', table.get('KEY'))
    130        self.assertEqual('Value', table['Key'])
    131        self.assertEqual('Value', table['key'])
    132        self.assertEqual('Value', table['KEY'])
    133 
    134 
    135 if __name__ == '__main__':
    136    unittest.main()
    137 
    138 # vi:sts=4 sw=4 et