main_41.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. def test_store_and_retrieve(self):
  2. # first store a record with random source and target ip addresses,
  3. # and see if we can recover it.
  4. col_request = pep3_pb2.StoreRequest()
  5. col_request.id = os.urandom(16)
  6. flowrecord = col_request.records.add()
  7. flowrecord.source_ip.data = os.urandom(16)
  8. flowrecord.source_ip.state = pep3_pb2.Pseudonymizable.UNENCRYPTED_NAME
  9. flowrecord.destination_ip.data = os.urandom(16)
  10. flowrecord.destination_ip.state = \
  11. pep3_pb2.Pseudonymizable.UNENCRYPTED_NAME
  12. flowrecord.anonymous_part.number_of_bytes = 123
  13. flowrecord.anonymous_part.number_of_packets = 456
  14. updates = list(self.collector.connect_to('collector').Store(
  15. iter([col_request])))
  16. self.assertEqual(len(updates), 1)
  17. self.assertEqual(updates[0].stored_id, col_request.id)
  18. # store the same flowrecord twice, to see if that causes troubles
  19. col_request.id = os.urandom(16)
  20. updates = list(self.collector.connect_to('collector').Store(
  21. iter([col_request])))
  22. self.assertEqual(len(updates), 1)
  23. self.assertEqual(updates[0].stored_id, col_request.id)
  24. query = pep3_pb2.SqlQuery()
  25. # manually compute storage_facility-local pseudonyms for query
  26. sf_name = b"PEP3 storage_facility"
  27. pseudonym_secrets = {}
  28. for peer_secrets in self.secrets.peers.values():
  29. for shard, shard_secrets in peer_secrets.by_shard.items():
  30. pseudonym_secrets[shard] \
  31. = shard_secrets.pseudonym_component_secret
  32. s = 1
  33. e = ed25519.scalar_unpack(common.sha256(sf_name))
  34. for secret in pseudonym_secrets.values():
  35. s *= pow(ed25519.scalar_unpack(secret), e, ed25519.l)
  36. s %= ed25519.l
  37. # see if the record was stored correctly by querying the
  38. # database directly.
  39. query.query = """SELECT peped_flows.p_dst_ip FROM peped_flows
  40. WHERE peped_flows.p_src_ip=:ip"""
  41. ip = query.parameters['ip'].pseudonymizable_value
  42. ip.data = (ed25519.Point.lizard(
  43. flowrecord.source_ip.data) * s).pack()
  44. ip.state = pep3_pb2.Pseudonymizable.UNENCRYPTED_PSEUDONYM
  45. row = self.sf.connect_to('database') \
  46. .Query(query).next().rows[0]
  47. self.assertEqual(row.cells[0].pseudonymizable_value.data,
  48. (ed25519.Point.lizard(flowrecord.destination_ip.data) * s
  49. ).pack())
  50. # manually compute researcher-local pseudonyms for query
  51. researcher_name = b"PEP3 researcher"
  52. pseudonym_secrets = {}
  53. for peer_secrets in self.secrets.peers.values():
  54. for shard, shard_secrets in peer_secrets.by_shard.items():
  55. pseudonym_secrets[shard] \
  56. = shard_secrets.pseudonym_component_secret
  57. s = 1
  58. e = ed25519.scalar_unpack(common.sha256(researcher_name))
  59. for secret in pseudonym_secrets.values():
  60. s *= pow(ed25519.scalar_unpack(secret), e, ed25519.l)
  61. s %= ed25519.l
  62. # now query via the researcher
  63. query.parameters['ip'].pseudonymizable_value.data \
  64. = (ed25519.Point.lizard(flowrecord.source_ip.data) * s).pack()
  65. row = self.researcher.connect_to('researcher') \
  66. .Query(query).next().rows[0]
  67. self.assertEqual(row.cells[0].pseudonymizable_value.data,
  68. (ed25519.Point.lizard(flowrecord.destination_ip.data) * s
  69. ).pack())