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