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.
 
 
 
 
 
 

74 lines
2.1 KiB

  1. """
  2. Concurrence tester. Tests multiple implementations of the same thing.
  3. Error if any of them errors, or if they give difference results.
  4. """
  5. from __future__ import absolute_import
  6. class ConcurrenceTestFailure(Exception):
  7. pass
  8. class ConcurrenceTest(object):
  9. def __init__(self,*args):
  10. self.impls = list(args)
  11. assert(len(self.impls))
  12. def __repr__(self):
  13. print("ConcurrenceTest(%s)" % ",".join((g.repr for g in self.impls)))
  14. def __str__(self): return repr(self)
  15. def copy(self):
  16. return ConcurrenceTest(*[c.copy() for c in self.impls])
  17. def deepcopy(self): return self.copy()
  18. def __getattr__(self, name):
  19. """
  20. The main tester. Run the call for each impl, and compare results.
  21. """
  22. def method(*args,**kwargs):
  23. have_any_ret = False
  24. accepted_ret = None
  25. for g in self.impls:
  26. try: ret = ("OK",getattr(g,name)(*args,**kwargs))
  27. except Exception as e: ret = ("EXN",e)
  28. if not have_any_ret:
  29. accepted_ret = ret
  30. have_any_ret = True
  31. if ret != accepted_ret:
  32. raise ConcurrenceTestFailure(
  33. "%s: %s\n%s: %s" %
  34. (self.impls[0], accepted_ret, g, ret)
  35. )
  36. if accepted_ret[0] == "OK": return accepted_ret[1]
  37. else: raise accepted_ret[1]
  38. return method
  39. if __name__ == '__main__':
  40. from Strobe.Keccak import cSHAKE128, cSHAKE256
  41. from Strobe.StrobeCShake import StrobeCShake
  42. from Strobe.StrobeCWrapper import CStrobe
  43. from Strobe.Strobe import Strobe
  44. proto = "concurrence test"
  45. ct = ConcurrenceTest(Strobe(proto), StrobeCShake(proto), CStrobe(proto))
  46. ct.prf(10)
  47. ct.ad("Hello")
  48. ct.send_enc("World")
  49. ct.send_clr("foo")
  50. ct.recv_clr("bar")
  51. ct.recv_enc("baz")
  52. for i in xrange(200):
  53. ct.send_enc("X"*i)
  54. ct.prf(123)
  55. ct.send_mac()
  56. print "Concurrence test passed."