test_signatures.py 4.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. # SPDX-License-Identifier: Apache-2.0
  2. # Copyright 2020 Contributors to OpenLEADR
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. # Unless required by applicable law or agreed to in writing, software
  8. # distributed under the License is distributed on an "AS IS" BASIS,
  9. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. # See the License for the specific language governing permissions and
  11. # limitations under the License.
  12. from openleadr.utils import generate_id, certificate_fingerprint
  13. from openleadr.messaging import create_message, parse_message
  14. from hashlib import sha256
  15. from base64 import b64encode
  16. from datetime import datetime, timedelta, timezone
  17. import os
  18. with open(os.path.join(os.path.abspath(os.path.dirname(__file__)), 'cert.pem'), 'rb') as file:
  19. TEST_CERT = file.read()
  20. with open(os.path.join(os.path.abspath(os.path.dirname(__file__)), 'key.pem'), 'rb') as file:
  21. TEST_KEY = file.read()
  22. TEST_KEY_PASSWORD = 'openadr'
  23. def test_message_validation():
  24. msg = create_message('oadrPoll', ven_id='123', cert=TEST_CERT, key=TEST_KEY, passphrase='openadr')
  25. parsed_type, parsed_message = parse_message(msg, cert=TEST_CERT)
  26. assert parsed_type == 'oadrPoll'
  27. def test_message_validation_complex():
  28. now = datetime.now(timezone.utc)
  29. event_id = generate_id()
  30. active_period = {"dtstart": now + timedelta(minutes=1),
  31. "duration": timedelta(minutes=9)}
  32. event_descriptor = {"event_id": event_id,
  33. "modification_number": 1,
  34. "modification_date_time": now,
  35. "priority": 1,
  36. "market_context": "http://MarketContext1",
  37. "created_date_time": now,
  38. "event_status": "near",
  39. "test_event": "false",
  40. "vtn_comment": "This is an event"}
  41. event_signals = [{"intervals": [{"duration": timedelta(minutes=1), "uid": 1, "signal_payload": 8},
  42. {"duration": timedelta(minutes=1), "uid": 2, "signal_payload": 10},
  43. {"duration": timedelta(minutes=1), "uid": 3, "signal_payload": 12},
  44. {"duration": timedelta(minutes=1), "uid": 4, "signal_payload": 14},
  45. {"duration": timedelta(minutes=1), "uid": 5, "signal_payload": 16},
  46. {"duration": timedelta(minutes=1), "uid": 6, "signal_payload": 18},
  47. {"duration": timedelta(minutes=1), "uid": 7, "signal_payload": 20},
  48. {"duration": timedelta(minutes=1), "uid": 8, "signal_payload": 10},
  49. {"duration": timedelta(minutes=1), "uid": 9, "signal_payload": 20}],
  50. "signal_name": "LOAD_CONTROL",
  51. #"signal_name": "simple",
  52. #"signal_type": "level",
  53. "signal_type": "x-loadControlCapacity",
  54. "signal_id": generate_id(),
  55. "current_value": 9.99}]
  56. event_targets = [{"ven_id": 'VEN001'}, {"ven_id": 'VEN002'}]
  57. event = {'active_period': active_period,
  58. 'event_descriptor': event_descriptor,
  59. 'event_signals': event_signals,
  60. 'targets': event_targets,
  61. 'response_required': 'always'}
  62. msg = create_message('oadrDistributeEvent',
  63. request_id=generate_id(),
  64. response={'request_id': 123, 'response_code': 200, 'response_description': 'OK'},
  65. events=[event],
  66. cert=TEST_CERT,
  67. key=TEST_KEY,
  68. passphrase='openadr')
  69. parsed_type, parsed_msg = parse_message(msg, cert=TEST_CERT)
  70. if __name__ == "__main__":
  71. msg = create_message('oadrPoll', ven_id='123', signing_certificate=TEST_CERT, signing_key=TEST_KEY, signing_key_passphrase=b'openadr')
  72. parsed_type, parsed_message = parse_message(msg)
  73. validate_message(msg, public_key=TEST_CERT)
  74. print(msg)