• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2018 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import socket
6
7from fake_printer import FakePrinter
8
9
10def main():
11    """
12    Unit tests for fake_printer.py.
13
14    """
15    # Simple unit test - it should start and stop without errors
16    p = FakePrinter(12345)
17    p.stop()
18
19    # The same test but other way
20    with FakePrinter(23456) as p:
21        pass
22
23    # Another test - let's try to send something
24    message = b'lkds;fsdjfsdjflsdjfsd;lfsad;adfsfa324dsfcxvdsvdf'
25    port = 12345
26    with FakePrinter(port) as printer:
27        # Opens a socket and sends the message
28        my_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
29        my_socket.connect( ('localhost', port) )
30        my_socket.send(message)
31        my_socket.close()
32        # Reads the message from the FakePrinter
33        doc = printer.fetch_document(10)
34        # The printer is stopped at the end of "with" statement
35    assert message == doc
36
37
38if __name__ == '__main__':
39    main()
40