tests.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. __filename__ = "tests.py"
  2. __author__ = "Bob Mottram"
  3. __license__ = "AGPL3+"
  4. __version__ = "0.0.1"
  5. __maintainer__ = "Bob Mottram"
  6. __email__ = "bob@freedombone.net"
  7. __status__ = "Production"
  8. import base64
  9. import time
  10. from person import createPerson
  11. from Crypto.Hash import SHA256
  12. from httpsig import signPostHeaders
  13. from httpsig import verifyPostHeaders
  14. from cache import storePersonInCache
  15. from cache import getPersonFromCache
  16. from threads import threadWithTrace
  17. def testHttpsigBase(withDigest):
  18. print('testHttpsig(' + str(withDigest) + ')')
  19. username='socrates'
  20. domain='argumentative.social'
  21. https=True
  22. port=80
  23. privateKeyPem,publicKeyPem,person,wfEndpoint=createPerson(username,domain,port,https,False)
  24. messageBodyJson = '{"a key": "a value", "another key": "A string"}'
  25. if not withDigest:
  26. headers = {'host': domain}
  27. else:
  28. bodyDigest = base64.b64encode(SHA256.new(messageBodyJson.encode()).digest())
  29. headers = {'host': domain, 'digest': f'SHA-256={bodyDigest}'}
  30. path='/inbox'
  31. signatureHeader = signPostHeaders(privateKeyPem, username, domain, path, https, None)
  32. headers['signature'] = signatureHeader
  33. assert verifyPostHeaders(https, publicKeyPem, headers, '/inbox' ,False, messageBodyJson)
  34. assert verifyPostHeaders(https, publicKeyPem, headers, '/parambulator/inbox', False , messageBodyJson) == False
  35. assert verifyPostHeaders(https, publicKeyPem, headers, '/inbox', True, messageBodyJson) == False
  36. if not withDigest:
  37. # fake domain
  38. headers = {'host': 'bogon.domain'}
  39. else:
  40. # correct domain but fake message
  41. messageBodyJson = '{"a key": "a value", "another key": "Fake GNUs"}'
  42. bodyDigest = base64.b64encode(SHA256.new(messageBodyJson.encode()).digest())
  43. headers = {'host': domain, 'digest': f'SHA-256={bodyDigest}'}
  44. headers['signature'] = signatureHeader
  45. assert verifyPostHeaders(https, publicKeyPem, headers, '/inbox', True, messageBodyJson) == False
  46. def testHttpsig():
  47. testHttpsigBase(False)
  48. testHttpsigBase(True)
  49. def testCache():
  50. print('testCache')
  51. personUrl="cat@cardboard.box"
  52. personJson={ "id": 123456, "test": "This is a test" }
  53. storePersonInCache(personUrl,personJson)
  54. result=getPersonFromCache(personUrl)
  55. assert result['id']==123456
  56. assert result['test']=='This is a test'
  57. def testThreadsFunction(param: str):
  58. for i in range(10000):
  59. time.sleep(2)
  60. def testThreads():
  61. print('testThreads')
  62. thr = threadWithTrace(target=testThreadsFunction,args=('test',),daemon=True)
  63. thr.start()
  64. assert thr.isAlive()==True
  65. time.sleep(1)
  66. thr.kill()
  67. thr.join()
  68. assert thr.isAlive()==False