From f72f7ff051d6aced54354087f0572fabac49c1c6 Mon Sep 17 00:00:00 2001 From: Ryan Hu Date: Wed, 5 Aug 2020 20:53:32 +1200 Subject: [PATCH] Removed osdp-diff folder, replaced print() with log.debug(); updated test cases --- osdp-diff/_bus_diff.txt | 6 ------ osdp-diff/_command_diff.txt | 8 -------- osdp-diff/_connection_diff.txt | 0 osdp-diff/_control_panel_diff.txt | 0 osdp-diff/_device_diff.txt | 10 ---------- osdp-diff/_message_diff.txt | 0 osdp-diff/_reply_diff.txt | 8 -------- osdp-diff/_secure_channel_diff.txt | 8 -------- osdp-diff/_types_diff.txt | 0 osdp/_bus.py | 3 +-- osdp/_command.py | 12 ++++++++---- osdp/_connection.py | 2 +- osdp/_control_panel.py | 1 - osdp/_device.py | 7 +++++-- osdp/_reply.py | 7 +++++-- tests/context.py | 10 ++++++++++ tests/puppet_connection.py | 7 ++++--- tests/run_test.sh | 5 +++++ tests/test_all.py | 10 ++++++---- tests/test_bus.py | 8 ++++---- tests/test_command.py | 5 ++--- tests/test_control_panel.py | 6 +++--- tests/test_reply.py | 5 ++--- 23 files changed, 56 insertions(+), 72 deletions(-) delete mode 100644 osdp-diff/_bus_diff.txt delete mode 100644 osdp-diff/_command_diff.txt delete mode 100644 osdp-diff/_connection_diff.txt delete mode 100644 osdp-diff/_control_panel_diff.txt delete mode 100644 osdp-diff/_device_diff.txt delete mode 100644 osdp-diff/_message_diff.txt delete mode 100644 osdp-diff/_reply_diff.txt delete mode 100644 osdp-diff/_secure_channel_diff.txt delete mode 100644 osdp-diff/_types_diff.txt create mode 100644 tests/context.py create mode 100755 tests/run_test.sh diff --git a/osdp-diff/_bus_diff.txt b/osdp-diff/_bus_diff.txt deleted file mode 100644 index 0a4285d..0000000 --- a/osdp-diff/_bus_diff.txt +++ /dev/null @@ -1,6 +0,0 @@ -132,134c132 -< if device.validate_secure_channel_establishment(reply): -< print("Secure session established.") -< ---- -> device.validate_secure_channel_establishment(reply) diff --git a/osdp-diff/_command_diff.txt b/osdp-diff/_command_diff.txt deleted file mode 100644 index a752c8d..0000000 --- a/osdp-diff/_command_diff.txt +++ /dev/null @@ -1,8 +0,0 @@ -41d40 -< print("Building secure message...") -46,47c45,46 -< additional_length = 4 + (2 if device.message_control.use_crc else 1) -< self.add_packet_length(command_buffer, additional_length) ---- -> # additional_length = 4 + (device.message_control.use_crc ? 2 : 1) -> # self.add_packet_length(command_buffer, additional_length) diff --git a/osdp-diff/_connection_diff.txt b/osdp-diff/_connection_diff.txt deleted file mode 100644 index e69de29..0000000 diff --git a/osdp-diff/_control_panel_diff.txt b/osdp-diff/_control_panel_diff.txt deleted file mode 100644 index e69de29..0000000 diff --git a/osdp-diff/_device_diff.txt b/osdp-diff/_device_diff.txt deleted file mode 100644 index db147b4..0000000 --- a/osdp-diff/_device_diff.txt +++ /dev/null @@ -1,10 +0,0 @@ -33c33 -< ---- -> -38c38 -< return ServerCryptogramCommand(self.address, self._secure_channel.server_cryptogram) ---- -> return ServerCryptogramCommand(self.address, self._secure_channel.serverCryptogram) -59d58 -< print("Cryptogram not accepted") diff --git a/osdp-diff/_message_diff.txt b/osdp-diff/_message_diff.txt deleted file mode 100644 index e69de29..0000000 diff --git a/osdp-diff/_reply_diff.txt b/osdp-diff/_reply_diff.txt deleted file mode 100644 index 77f5f8c..0000000 --- a/osdp-diff/_reply_diff.txt +++ /dev/null @@ -1,8 +0,0 @@ -111c111 -< reply = UnknownReply(data, connection_id, issuing_command, device) ---- -> reply = UnknownReply(data, connection_id, issuing_command, Device) -115d114 -< print("Secure block data: ", self.secure_block_data[0]) -159d157 -< print("Extract reply data: ", self.extract_reply_data.hex()) diff --git a/osdp-diff/_secure_channel_diff.txt b/osdp-diff/_secure_channel_diff.txt deleted file mode 100644 index 99adac4..0000000 --- a/osdp-diff/_secure_channel_diff.txt +++ /dev/null @@ -1,8 +0,0 @@ -37c37 -< if client_cryptogram != self.generate_key(self.server_random_number, client_random_number, self._enc): ---- -> if client_cryptogram != self.generate_key(self.server_random_number, client_cryptogram, self._enc): -61c61 -< self.server_random_number, ---- -> self._server_random_number, diff --git a/osdp-diff/_types_diff.txt b/osdp-diff/_types_diff.txt deleted file mode 100644 index e69de29..0000000 diff --git a/osdp/_bus.py b/osdp/_bus.py index e4505de..6e04120 100644 --- a/osdp/_bus.py +++ b/osdp/_bus.py @@ -130,8 +130,7 @@ def process_reply(self, reply: Reply, device: Device): device.initialize_secure_channel(reply) elif reply.type == ReplyType.InitialRMac: if device.validate_secure_channel_establishment(reply): - print("Secure session established.") - + log.debug("Secure session established.") if self._on_reply_received is not None: self._on_reply_received(reply) diff --git a/osdp/_command.py b/osdp/_command.py index cccace2..e1c9e6f 100644 --- a/osdp/_command.py +++ b/osdp/_command.py @@ -1,9 +1,12 @@ from abc import abstractmethod +import logging from ._types import OutputControls, ReaderLedControls, ReaderBuzzerControl, ReaderTextOutput from ._message import Message import datetime +log = logging.getLogger('osdp') + class Command(Message): @@ -38,7 +41,7 @@ def build_command(self, device) -> bytes: command_buffer.append(self.command_code) if device.is_security_established: - print("Building secure message...") + log.debug("Building secure message...") command_buffer.extend(self.encrypted_data(device)) # TODO: I don't think this needed @@ -367,6 +370,7 @@ def data(self) -> bytes: def custom_command_update(self, command_buffer: bytearray): pass + class KeySetCommand(Command): def __init__(self, address: int, scbk: bytes): @@ -389,8 +393,8 @@ def custom_command_update(self, command_buffer: bytearray): def keyset_data(self): header = [] type = 0x01 - len = 0x10 + length = 0x10 scbk = [0x41, 0x02, 0x31, 0x84, 0xF1, 0xA2, 0xDE, 0x7C, 0x32, 0x98, 0x01, 0xB8, 0x7B, 0x56, 0xB3, 0x60] header.append(type) - header.append(len) - return bytes(header + scbk) \ No newline at end of file + header.append(length) + return bytes(header + scbk) diff --git a/osdp/_connection.py b/osdp/_connection.py index e0c103e..e081663 100644 --- a/osdp/_connection.py +++ b/osdp/_connection.py @@ -36,7 +36,7 @@ def read(self, size: int = 1) -> bytes: class SerialPortOsdpConnection(OsdpConnection): - def __init__(self, port: str, baud_rate: int, raspberry_pi: bool=False): + def __init__(self, port: str, baud_rate: int, raspberry_pi: bool = False): self._port = port self._baud_rate = baud_rate self.serial_port = None diff --git a/osdp/_control_panel.py b/osdp/_control_panel.py index 5d91837..2484f05 100644 --- a/osdp/_control_panel.py +++ b/osdp/_control_panel.py @@ -68,7 +68,6 @@ def keyset(self, connection_id: UUID, address: int) -> bool: reply = self.send_command(connection_id, KeySetCommand(address, bytes([]))) return reply.type == ReplyType.Ack - def is_online(self, connection_id: UUID, address: int) -> bool: bus = self._buses.get(connection_id) if bus is None: diff --git a/osdp/_device.py b/osdp/_device.py index ee4c0af..b16d938 100644 --- a/osdp/_device.py +++ b/osdp/_device.py @@ -1,3 +1,4 @@ +import logging import queue import datetime @@ -7,6 +8,8 @@ ) from ._secure_channel import SecureChannel +log = logging.getLogger('osdp') + class Device(object): @@ -30,7 +33,7 @@ def is_online(self) -> bool: def get_next_command_data(self): if self.message_control.sequence == 0: return PollCommand(self.address) - + if self._use_secure_channel and not self._secure_channel.is_initialized: return SecurityInitializationRequestCommand(self.address, self._secure_channel.server_random_number) @@ -56,7 +59,7 @@ def initialize_secure_channel(self, reply): def validate_secure_channel_establishment(self, reply) -> bool: if not reply.secure_cryptogram_has_been_accepted(): - print("Cryptogram not accepted") + log.debug("Cryptogram not accepted") return False self._secure_channel.establish(reply.extract_reply_data) diff --git a/osdp/_reply.py b/osdp/_reply.py index 5c76726..654debe 100644 --- a/osdp/_reply.py +++ b/osdp/_reply.py @@ -1,4 +1,5 @@ from abc import abstractmethod +import logging from uuid import UUID from ._types import SecurityBlockType, ReplyType, Control @@ -6,6 +7,8 @@ from ._command import Command from ._device import Device +log = logging.getLogger('osdp') + class Reply(Message): @@ -112,7 +115,7 @@ def parse(data: bytes, connection_id: UUID, issuing_command: Command, device: De return reply def secure_cryptogram_has_been_accepted(self) -> bool: - print("Secure block data: ", self.secure_block_data[0]) + log.debug("Secure block data: %s", self.secure_block_data[0]) return self.secure_block_data[0] != 0 def match_issuing_command(self, command: Command) -> bool: @@ -156,7 +159,7 @@ def __repr__(self): return "Connection ID: {0} Address: {1} Type: {2}".format(self._connection_id, self.address, self.type) def decrypt_data(self, device: Device) -> bytes: - print("Extract reply data: ", self.extract_reply_data.hex()) + log.debug("Extract reply data: %s", self.extract_reply_data.hex()) return device.decrypt_data(self.extract_reply_data) diff --git a/tests/context.py b/tests/context.py new file mode 100644 index 0000000..90674d3 --- /dev/null +++ b/tests/context.py @@ -0,0 +1,10 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +"""Context for tests for OSDP Python Module.""" + +import os +import sys + +sys.path.insert(0, os.path.abspath('..')) +from osdp import * diff --git a/tests/puppet_connection.py b/tests/puppet_connection.py index ca3f8a4..50550ae 100644 --- a/tests/puppet_connection.py +++ b/tests/puppet_connection.py @@ -1,14 +1,15 @@ import os import sys -sys.path.insert(0, os.path.abspath('..')) -from osdp import * import time import logging import random +from context import OsdpConnection + log = logging.getLogger('osdp') + class PuppetOsdpConnection(OsdpConnection): def __init__(self): @@ -40,4 +41,4 @@ def read(self, size: int=1) -> bytes: taken = self.should_reply[:size] remain = self.should_reply[size:] self.should_reply = remain - return taken \ No newline at end of file + return taken diff --git a/tests/run_test.sh b/tests/run_test.sh new file mode 100755 index 0000000..ff32871 --- /dev/null +++ b/tests/run_test.sh @@ -0,0 +1,5 @@ +python3 -m unittest -v test_all.py +python3 -m unittest -v test_bus.py +python3 -m unittest -v test_command.py +#python3 -m unittest -v test_control_panel.py +python3 -m unittest -v test_reply.py diff --git a/tests/test_all.py b/tests/test_all.py index 48068bf..7ff1dee 100644 --- a/tests/test_all.py +++ b/tests/test_all.py @@ -1,8 +1,9 @@ import unittest -from .test_command import CommandTestCase -from .test_reply import ReplyTestCase -from .test_bus import BusTestCase +from test_command import CommandTestCase +from test_reply import ReplyTestCase +from test_bus import BusTestCase + def create_suite(): test_suite = unittest.TestSuite() @@ -11,8 +12,9 @@ def create_suite(): test_suite.addTest(BusTestCase()) return test_suite + if __name__ == '__main__': suite = create_suite() runner=unittest.TextTestRunner() - runner.run(suite) \ No newline at end of file + runner.run(suite) diff --git a/tests/test_bus.py b/tests/test_bus.py index 68ce19b..77b5f78 100644 --- a/tests/test_bus.py +++ b/tests/test_bus.py @@ -8,12 +8,12 @@ import sys import unittest -sys.path.insert(0, os.path.abspath('..')) -from osdp import * -from .puppet_connection import PuppetOsdpConnection +from puppet_connection import PuppetOsdpConnection +from context import * log = logging.getLogger('osdp') + class BusTestCase(unittest.TestCase): """Test Bus for OSDP Python Module.""" @@ -203,4 +203,4 @@ def test_reader_led_control_denied_checksum(self): self.assertEqual(device.message_control.sequence, 2) if __name__ == '__main__': - unittest.main() \ No newline at end of file + unittest.main() diff --git a/tests/test_command.py b/tests/test_command.py index db59441..f4c94a3 100644 --- a/tests/test_command.py +++ b/tests/test_command.py @@ -8,8 +8,7 @@ import unittest import datetime -sys.path.insert(0, os.path.abspath('..')) -from osdp import * +from context import * class CommandTestCase(unittest.TestCase): @@ -489,4 +488,4 @@ def test_reader_mfg_command_crc(self): self.assertEqual(content.hex().upper(), '537F0C0007800B0E0E0FEDCC') if __name__ == '__main__': - unittest.main() \ No newline at end of file + unittest.main() diff --git a/tests/test_control_panel.py b/tests/test_control_panel.py index 1183a74..e99ca58 100644 --- a/tests/test_control_panel.py +++ b/tests/test_control_panel.py @@ -9,11 +9,11 @@ import time import unittest -sys.path.insert(0, os.path.abspath('..')) -from osdp import * +from context import * log = logging.getLogger('osdp') + class ControlPanelTestCase(unittest.TestCase): """Test Bus for OSDP Python Module.""" @@ -106,4 +106,4 @@ def test_cp_checksum_unsecure(self): if __name__ == '__main__': - unittest.main() \ No newline at end of file + unittest.main() diff --git a/tests/test_reply.py b/tests/test_reply.py index 06ded0b..e844630 100644 --- a/tests/test_reply.py +++ b/tests/test_reply.py @@ -9,8 +9,7 @@ import datetime from uuid import UUID, uuid4 -sys.path.insert(0, os.path.abspath('..')) -from osdp import * +from context import * class ReplyTestCase(unittest.TestCase): @@ -163,4 +162,4 @@ def test_poll_reply_key_data_crc(self): self.assertEqual(device.message_control.sequence, 3) if __name__ == '__main__': - unittest.main() \ No newline at end of file + unittest.main()