Implement a secure ICS protocol targeting LoRa Node151 microcontroller for controlling irrigation.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

88 lines
4.2 KiB

  1. # -*- coding: utf-8 -*-
  2. from .context import encdec8b10b
  3. from encdec8b10b import EncDec8B10B
  4. import random
  5. import unittest
  6. verbose = True # Set to True to get test output
  7. class TestSuite(unittest.TestCase):
  8. """All test cases"""
  9. def test_comma(self):
  10. print("Testing K28.5 Comma...")
  11. running_disparity = 0
  12. test_data = 0xBC
  13. test_ctrl = 1
  14. running_disparity, data_encoded = EncDec8B10B.enc_8b10b(test_data, running_disparity, test_ctrl, verbose)
  15. ctrl, data_decoded = EncDec8B10B.dec_8b10b(data_encoded, verbose)
  16. running_disparity, data_encoded = EncDec8B10B.enc_8b10b(data_decoded, running_disparity, 1, verbose)
  17. ctrl, data_decoded = EncDec8B10B.dec_8b10b(data_encoded, verbose)
  18. assert data_decoded == test_data and ctrl == test_ctrl, "K28.5 Comma Test Failed"
  19. def test_known_seq(self):
  20. print("Testing Known Sequence...")
  21. known_seq = [0xa0, 0x7a, 0xFF, 0xc2, 0x48, 0xda, 0x1b, 0x2e, 0x1f, 0x5b, 0xa5, 0x20, 0xb6, 0x10, 0xc3, 0x4d, 0xa0, 0x17, 0x83,
  22. 0x3b, 0x2e, 0x7d, 0x61, 0x73, 0x4d, 0xc5, 0x42, 0x59, 0x45, 0x7c, 0x12, 0x1c, 0x03, 0x52, 0xdd, 0x30, 0xa5]
  23. encoded_seq = [0x146, 0xda, 0x235, 0x1ad, 0x298, 0x19a, 0x9b, 0x24e, 0xb5, 0x29b, 0x165, 0x246, 0x156, 0xb6, 0x1a3, 0x28d, 0x179, 0x368, 0x123,
  24. 0x25b, 0x24e, 0xe2, 0x32e, 0x313, 0x28d, 0x1a5, 0x292, 0x299, 0x2a5, 0x0dc, 0x372, 0x9c, 0x363, 0x2b2, 0x1a2, 0x276, 0x165]
  25. result_encoded = list()
  26. result_decoded = list()
  27. # Manually set running disparity to known start
  28. running_disparity = 1
  29. test_ctrl = 0
  30. for byte, encoded in zip(known_seq, encoded_seq):
  31. running_disparity, data_encoded = EncDec8B10B.enc_8b10b(byte, running_disparity, test_ctrl, verbose)
  32. assert data_encoded == encoded, "Data Encoded (0x{:03X}) does not match known sequence (0x{:03X})".format(data_encoded, encoded)
  33. ctrl, data_decoded = EncDec8B10B.dec_8b10b(data_encoded, verbose)
  34. assert data_decoded == byte, "Data Decoded (0x{:02X}) does not match input byte (0x{:02X})".format(data_decoded, byte)
  35. def test_rand_seq(self):
  36. print("Testing Random Data Sequence...")
  37. test_ctrl = 0
  38. running_disparity = 0
  39. for i in range(100000):
  40. rand_byte = random.randint(0, 0xFF)
  41. running_disparity, data_encoded = EncDec8B10B.enc_8b10b(rand_byte, running_disparity, test_ctrl, verbose)
  42. ctrl, data_decoded = EncDec8B10B.dec_8b10b(data_encoded, verbose)
  43. assert rand_byte == data_decoded, "Data Decoded (0x{:02X}) does not match input byte (0x{:02X})".format(data_decoded, rand_byte)
  44. running_disparity = 1
  45. for i in range(100000):
  46. rand_byte = random.randint(0, 0xFF)
  47. running_disparity, data_encoded = EncDec8B10B.enc_8b10b(rand_byte, running_disparity, test_ctrl, verbose)
  48. ctrl, data_decoded = EncDec8B10B.dec_8b10b(data_encoded, verbose)
  49. assert rand_byte == data_decoded, "Data Decoded (0x{:02X}) does not match input byte (0x{:02X})".format(data_decoded, rand_byte)
  50. def test_ctrl_symbol(self):
  51. print("Testing All Control symbols...")
  52. test_ctrl = 1
  53. ctrl_symbols = [0x1c, 0x3c, 0x5c, 0x7c, 0x9c,
  54. 0xbc, 0xdc, 0xfc, 0xf7, 0xfb, 0xfd, 0xfe]
  55. running_disparity = 0
  56. for symbol in ctrl_symbols:
  57. running_disparity, data_encoded = EncDec8B10B.enc_8b10b(
  58. symbol, running_disparity, test_ctrl, verbose)
  59. ctrl, data_decoded = EncDec8B10B.dec_8b10b(data_encoded, verbose)
  60. assert symbol == data_decoded, "Data Decoded (0x{:02X}) does not match input byte (0x{:02X})".format(
  61. data_decoded, symbol)
  62. running_disparity = 1
  63. for symbol in ctrl_symbols:
  64. running_disparity, data_encoded = EncDec8B10B.enc_8b10b(symbol, running_disparity, test_ctrl, verbose)
  65. ctrl, data_decoded = EncDec8B10B.dec_8b10b(data_encoded, verbose)
  66. assert symbol == data_decoded, "Data Decoded (0x{:02X}) does not match input byte (0x{:02X})".format(data_decoded, symbol)
  67. if __name__ == '__main__':
  68. unittest.main()