Skip to content

Commit

Permalink
Removed osdp-diff folder, replaced print() with log.debug(); updated …
Browse files Browse the repository at this point in the history
…test cases
  • Loading branch information
ryanhz committed Aug 5, 2020
1 parent 613969b commit f72f7ff
Show file tree
Hide file tree
Showing 23 changed files with 56 additions and 72 deletions.
6 changes: 0 additions & 6 deletions osdp-diff/_bus_diff.txt

This file was deleted.

8 changes: 0 additions & 8 deletions osdp-diff/_command_diff.txt

This file was deleted.

Empty file removed osdp-diff/_connection_diff.txt
Empty file.
Empty file removed osdp-diff/_control_panel_diff.txt
Empty file.
10 changes: 0 additions & 10 deletions osdp-diff/_device_diff.txt

This file was deleted.

Empty file removed osdp-diff/_message_diff.txt
Empty file.
8 changes: 0 additions & 8 deletions osdp-diff/_reply_diff.txt

This file was deleted.

8 changes: 0 additions & 8 deletions osdp-diff/_secure_channel_diff.txt

This file was deleted.

Empty file removed osdp-diff/_types_diff.txt
Empty file.
3 changes: 1 addition & 2 deletions osdp/_bus.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 8 additions & 4 deletions osdp/_command.py
Original file line number Diff line number Diff line change
@@ -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):

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand All @@ -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)
header.append(length)
return bytes(header + scbk)
2 changes: 1 addition & 1 deletion osdp/_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion osdp/_control_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
7 changes: 5 additions & 2 deletions osdp/_device.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
import queue
import datetime

Expand All @@ -7,6 +8,8 @@
)
from ._secure_channel import SecureChannel

log = logging.getLogger('osdp')


class Device(object):

Expand All @@ -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)

Expand All @@ -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)
Expand Down
7 changes: 5 additions & 2 deletions osdp/_reply.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
from abc import abstractmethod
import logging
from uuid import UUID

from ._types import SecurityBlockType, ReplyType, Control
from ._message import Message
from ._command import Command
from ._device import Device

log = logging.getLogger('osdp')


class Reply(Message):

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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)


Expand Down
10 changes: 10 additions & 0 deletions tests/context.py
Original file line number Diff line number Diff line change
@@ -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 *
7 changes: 4 additions & 3 deletions tests/puppet_connection.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -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
return taken
5 changes: 5 additions & 0 deletions tests/run_test.sh
Original file line number Diff line number Diff line change
@@ -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
10 changes: 6 additions & 4 deletions tests/test_all.py
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -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)
runner.run(suite)
8 changes: 4 additions & 4 deletions tests/test_bus.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down Expand Up @@ -203,4 +203,4 @@ def test_reader_led_control_denied_checksum(self):
self.assertEqual(device.message_control.sequence, 2)

if __name__ == '__main__':
unittest.main()
unittest.main()
5 changes: 2 additions & 3 deletions tests/test_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -489,4 +488,4 @@ def test_reader_mfg_command_crc(self):
self.assertEqual(content.hex().upper(), '537F0C0007800B0E0E0FEDCC')

if __name__ == '__main__':
unittest.main()
unittest.main()
6 changes: 3 additions & 3 deletions tests/test_control_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down Expand Up @@ -106,4 +106,4 @@ def test_cp_checksum_unsecure(self):


if __name__ == '__main__':
unittest.main()
unittest.main()
5 changes: 2 additions & 3 deletions tests/test_reply.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -163,4 +162,4 @@ def test_poll_reply_key_data_crc(self):
self.assertEqual(device.message_control.sequence, 3)

if __name__ == '__main__':
unittest.main()
unittest.main()

0 comments on commit f72f7ff

Please sign in to comment.